/** * Return the string literal in a form ready to embed in an NXQL statement. */ public static String prepareStringLiteral(String s, boolean quoteParameter, boolean escape) { if (escape) { if (quoteParameter) { return NXQL.escapeString(s); } else { return NXQL.escapeStringInner(s); } } else { if (quoteParameter) { return "'" + s + "'"; } else { return s; } } }
/** * SELECT * FROM type WHERE ecm:path STARTSWITH doc.getPathAsString AND prop = value */ public static String select(String type, DocumentModel doc, String prop, String value) { return select(type, doc) + " AND " + prop + " = " + NXQL.escapeString(value); }
protected String buildTemplateSearchByNameQuery(String name) { StringBuffer sb = new StringBuffer( "select * from Document where ecm:mixinType = 'Template' AND tmpl:templateName = " + NXQL.escapeString(name)); if (Boolean.parseBoolean(Framework.getProperty(FILTER_VERSIONS_PROPERTY))) { sb.append(" AND ecm:isVersion = 0"); } return sb.toString(); }
/** * @deprecated since 6.0: page providers implement this logic instead. */ @Deprecated public static String formatStringList(List<String> actors) { StringBuffer sb = new StringBuffer(); Iterator<String> actorIterator = actors.iterator(); while (actorIterator.hasNext()) { String userName = actorIterator.next(); sb.append(NXQL.escapeString(userName)); if (actorIterator.hasNext()) { sb.append(','); } } return sb.toString(); }
public static String serializeFullText(String value) { value = sanitizeFulltextInput(value); return "= " + NXQL.escapeString(value); }
private List<DocumentModel> fetchFromVcsChunk(final List<String> ids, CoreSession session) { StringBuilder sb = new StringBuilder(); sb.append("SELECT * FROM Document, Relation WHERE ecm:uuid IN ("); for (int i = 0; i < ids.size(); i++) { sb.append(NXQL.escapeString(ids.get(i))); if (i < ids.size() - 1) { sb.append(", "); } } sb.append(")"); return session.query(sb.toString()); }
/** * SELECT * FROM type WHERE ecm:path STARTSWITH doc.getPathAsString() */ public static String select(String type, DocumentModel doc) { return "SELECT * FROM " + type + " WHERE " + NXQL.ECM_PATH + " STARTSWITH " + NXQL.escapeString(doc.getPathAsString()) + " AND " + NOT_DELETED; }
private List<DocumentModel> fetchDocuments(CoreSession session, List<String> ids) { StringBuilder sb = new StringBuilder(); sb.append("SELECT * FROM Document, Relation WHERE ecm:uuid IN ("); for (int i = 0; i < ids.size(); i++) { sb.append(NXQL.escapeString(ids.get(i))); if (i < ids.size() - 1) { sb.append(", "); } } sb.append(")"); // read invalidation session.save(); return session.query(sb.toString()); }
public List<DocumentModel> findDocumentModelVariants(CoreSession session, DocumentationItem item) { String id = item.getId(); String type = item.getTargetType(); String query = "SELECT * FROM " + DocumentationItem.TYPE_NAME + " WHERE " + DocumentationItem.PROP_DOCUMENTATION_ID + " = " + NXQL.escapeString(id) + " AND " + DocumentationItem.PROP_TARGET_TYPE + " = " + NXQL.escapeString(type) + " AND " + QueryHelper.NOT_DELETED; query += " ORDER BY dc:created"; return session.query(query); }
@Override public List<DocumentModel> getSubscribedDocuments(String prefixedPrincipalName, String repositoryName) { String nxql = "SELECT * FROM Document WHERE ecm:mixinType = '" + SubscriptionAdapter.NOTIFIABLE_FACET + "' " + "AND ecm:isVersion = 0 " + "AND notif:notifications/*/subscribers/* = " + NXQL.escapeString(prefixedPrincipalName); return CoreInstance.doPrivileged(repositoryName, (CoreSession s) -> s.query(nxql).stream().map(NotificationService::detachDocumentModel).collect( toList())); }
@SuppressWarnings("unchecked") public static List<String> getListValue(DocumentModel model, PredicateFieldDefinition fieldDescriptor) { Object rawValue = getRawValue(model, fieldDescriptor); if (rawValue == null) { return null; } List<String> values = new ArrayList<>(); if (rawValue instanceof ArrayList) { rawValue = ((ArrayList<Object>) rawValue).toArray(); } for (Object element : (Object[]) rawValue) { if (element != null) { if (element instanceof Number) { values.add(element.toString()); } else { String value = element.toString().trim(); if (!value.equals("")) { values.add(NXQL.escapeString(value)); } } } } return values; }
@Override public OperationInfo getOperation(String id) { if (id.startsWith(OperationInfo.ARTIFACT_PREFIX)) { id = id.substring(OperationInfo.ARTIFACT_PREFIX.length()); } String query = QueryHelper.select(OperationInfo.TYPE_NAME, getDoc()) + " AND " + OperationInfo.PROP_NAME + " = " + NXQL.escapeString(id) + " OR " + OperationInfo.PROP_ALIASES + " = " + NXQL.escapeString(id); DocumentModelList docs = getCoreSession().query(query); return docs.isEmpty() ? null : docs.get(0).getAdapter(OperationInfo.class); }
@Override public List<DocumentationItem> findDocumentItems(CoreSession session, NuxeoArtifact nxItem) { String id = nxItem.getId(); String type = nxItem.getArtifactType(); String query = "SELECT * FROM " + DocumentationItem.TYPE_NAME + " WHERE " + DocumentationItem.PROP_TARGET + " = " + NXQL.escapeString(id) + " AND " + DocumentationItem.PROP_TARGET_TYPE + " = " + NXQL.escapeString(type) + " AND " + QueryHelper.NOT_DELETED + " ORDER BY " + DocumentationItem.PROP_DOCUMENTATION_ID + ", dc:modified"; List<DocumentModel> docs = session.query(query); Map<String, List<DocumentationItem>> sortMap = new HashMap<>(); for (DocumentModel doc : docs) { DocumentationItem item = doc.getAdapter(DocumentationItem.class); List<DocumentationItem> alternatives = sortMap.get(item.getId()); if (alternatives == null) { alternatives = new ArrayList<>(); alternatives.add(item); sortMap.put(item.getId(), alternatives); } else { alternatives.add(item); } } List<DocumentationItem> result = new ArrayList<>(); for (String documentationId : sortMap.keySet()) { DocumentationItem bestDoc = findBestMatch(nxItem, sortMap.get(documentationId)); result.add(bestDoc); } return result; }
protected boolean hasContent(DocumentModel document, String query) throws IOException { try (SessionWrapper wrapper = ctx.getSession(document)) { // Limit result set to 1 as we just want to know if there's at least one child return wrapper.getSession() .queryProjection(String.format(query, NXQL.escapeString(document.getId())), 1, 0) .size() > 0; } }
@Override public DocumentModelList getTemplates(String targetTypeName) { if (documentManager == null) { log.error("Unable to access documentManager"); return null; } String query = "SELECT * FROM Document where ecm:primaryType = '%s' AND ecm:path STARTSWITH %s"; DocumentModelList tl = documentManager.query(String.format(query, TemplateRoot, NXQL.escapeString(navigationContext.getCurrentDomainPath()))); if (tl.isEmpty()) { templates = tl; } else { templates = documentManager.getChildren(tl.get(0).getRef(), targetTypeName); List<DocumentModel> deleted = new ArrayList<>(); for (DocumentModel current : templates) { if (current.isTrashed()) { deleted.add(current); } } templates.removeAll(deleted); } return templates; }
@Override public ServiceInfo getService(String id) { // Select only not overriden ticket and old imported NXService without overriden value String query = QueryHelper.select(ServiceInfo.TYPE_NAME, getDoc()) + " AND " + ServiceInfo.PROP_CLASS_NAME + " = " + NXQL.escapeString(id) + " AND (" + ServiceInfo.PROP_OVERRIDEN + " = 0 OR " + ServiceInfo.PROP_OVERRIDEN + " is NULL)"; DocumentModelList docs = getCoreSession().query(query); if (docs.size() > 1) { throw new AssertionError("Multiple services found for " + id); } return docs.get(0).getAdapter(ServiceInfo.class); }
@Override public SeamComponentInfo getSeamComponent(String id) { String name = id.replace("seam:", ""); String query = QueryHelper.select(SeamComponentInfo.TYPE_NAME, getDoc()) + " AND " + SeamComponentInfo.PROP_COMPONENT_NAME + " = " + NXQL.escapeString(name); DocumentModelList docs = getCoreSession().query(query); return docs.isEmpty() ? null : docs.get(0).getAdapter(SeamComponentInfo.class); }
protected <T> T getChild(Class<T> adapter, String docType, String idField, String id) { String query = QueryHelper.select(docType, doc) + " AND " + idField + " = " + NXQL.escapeString(id); DocumentModelList docs = getCoreSession().query(query); if (docs.isEmpty()) { log.error("Unable to find " + docType + " for id " + id); } else if (docs.size() == 1) { return docs.get(0).getAdapter(adapter); } else { log.error("multiple match for " + docType + " for id " + id); return docs.get(0).getAdapter(adapter); } return null; }
@Override public List<DocumentationItem> searchDocumentation(CoreSession session, String distribId, String fulltext, String targetType) { DistributionSnapshot snap = Framework.getService(SnapshotManager.class).getSnapshot(distribId, session); DocumentModel dist = ((RepositoryDistributionSnapshot) snap).getDoc(); String query = QueryHelper.select(DocumentationItem.TYPE_NAME, dist, NXQL.ECM_FULLTEXT, fulltext); if (targetType != null) { query += " AND " + DocumentationItem.PROP_TARGET_TYPE + " = " + NXQL.escapeString(targetType); } ElasticSearchService ess = Framework.getService(ElasticSearchService.class); DocumentModelList docs = ess.query(new NxQueryBuilder(session).nxql(query).limit(MAX_RESULTS)); List<DocumentationItem> result = new ArrayList<>(); for (DocumentModel doc : docs) { DocumentationItem docItem = doc.getAdapter(DocumentationItem.class); if (docItem != null) { result.add(docItem); } } return result; }
@Override public void write(JsonGenerator jg, DocumentModel document) throws IOException { if (!document.isFolder()) { jg.writeBooleanField(NAME, false); return; } try (SessionWrapper wrapper = ctx.getSession(document)) { String fetchFolderishChildQuery = "SELECT * FROM Document WHERE ecm:mixinType = 'Folderish'" + " AND ecm:mixinType != 'HiddenInNavigation' AND ecm:isTrashed = 0" + " AND ecm:parentId = " + NXQL.escapeString(document.getId()); // Limit result set to 1 as we just want to know if there's at least one Folderish child boolean hasChildren = !wrapper.getSession().queryProjection(fetchFolderishChildQuery, 1, 0).isEmpty(); jg.writeBooleanField(NAME, hasChildren); } }