@Test public void testQueryOrderBy() { try (Session session = userDir.getSession()) { Map<String, Serializable> filter = new HashMap<>(); Map<String, String> orderBy = new HashMap<>(); DocumentModelList entries; orderBy.put("company", "asc"); entries = session.query(filter, Collections.emptySet(), orderBy); assertEquals(4, entries.size()); assertEquals("user3", entries.get(0).getId()); assertEquals("Administrator", entries.get(1).getId()); assertEquals("user2", entries.get(2).getId()); assertEquals("user1", entries.get(3).getId()); } }
@Override public DocumentModelList query(Map<String, Serializable> filter, Set<String> fulltext, Map<String, String> orderBy, boolean fetchReferences, int limit, int offset) { if (!hasPermission(SecurityConstants.READ)) { return new DocumentModelListImpl(); } try { String[] filters = new String[filter.size()]; String[] filterArgs = new String[filter.size()]; if (fulltext == null) { fulltext = Collections.emptySet(); } int index = 0; for (String fieldName : filter.keySet()) { if (getDirectory().isReference(fieldName)) { log.warn(fieldName + " is a reference and will be ignored as a query criterion"); continue; } String backendFieldName = getDirectory().getFieldMapper().getBackendField(fieldName); Object fieldValue = filter.get(fieldName); StringBuilder currentFilter = new StringBuilder(); currentFilter.append("("); if (fieldValue == null) { currentFilter.append("!(").append(backendFieldName).append("=*)"); } else if ("".equals(fieldValue)) { if (fulltext.contains(fieldName)) { currentFilter.append(backendFieldName).append("=*"); } else { currentFilter.append("!(").append(backendFieldName).append("=*)"); } } else { currentFilter.append(backendFieldName).append("="); if (fulltext.contains(fieldName)) { switch (substringMatchType) { case subinitial: currentFilter.append("{").append(index).append("}*"); break; case subfinal: currentFilter.append("*{").append(index).append("}"); break; case subany: currentFilter.append("*{").append(index).append("}*"); break; } } else { currentFilter.append("{").append(index).append("}"); } } currentFilter.append(")"); filters[index] = currentFilter.toString(); if (fieldValue != null && !"".equals(fieldValue)) { if (fieldValue instanceof Blob) { log.warn("Binary search is not supported"); } else { filterArgs[index] = fieldValue.toString(); } } index++; } String filterExpr = "(&" + getDirectory().getBaseFilter() + StringUtils.join(filters) + ')'; SearchControls scts = getDirectory().getSearchControls(true); if (log.isDebugEnabled()) { log.debug(String.format( "LDAPSession.query(...): LDAP search base='%s' filter='%s' args='%s' scope='%s' [%s]", searchBaseDn, filterExpr, StringUtils.join(filterArgs, ","), scts.getSearchScope(), this)); } try { NamingEnumeration<SearchResult> results = getContext().search(searchBaseDn, filterExpr, filterArgs, scts); DocumentModelList entries = ldapResultsToDocumentModels(results, fetchReferences); if (orderBy != null && !orderBy.isEmpty()) { getDirectory().orderEntries(entries, orderBy); } return applyQueryLimits(entries, limit, offset); } catch (NameNotFoundException nnfe) { log.error("Unexpected response from server while performing query: " + nnfe.getMessage(), nnfe); return new DocumentModelListImpl(); } } catch (LimitExceededException e) { throw new org.nuxeo.ecm.directory.SizeLimitExceededException(e); } catch (NamingException e) { throw new DirectoryException("executeQuery failed", e); } }
LDAPSession extends BaseSession { @Override public DocumentModelList query(Map<String, Serializable> filter, Set<String> fulltext, Map<String, String> orderBy, boolean fetchReferences, int limit, int offset) { if (!hasPermission(SecurityConstants.READ)) { return new DocumentModelListImpl(); } try { String[] filters = new String[filter.size()]; String[] filterArgs = new String[filter.size()]; if (fulltext == null) { fulltext = Collections.emptySet(); } int index = 0; for (String fieldName : filter.keySet()) { if (getDirectory().isReference(fieldName)) { log.warn(fieldName + " is a reference and will be ignored as a query criterion"); continue; } String backendFieldName = getDirectory().getFieldMapper().getBackendField(fieldName); Object fieldValue = filter.get(fieldName); StringBuilder currentFilter = new StringBuilder(); currentFilter.append("("); if (fieldValue == null) { currentFilter.append("!(").append(backendFieldName).append("=*)"); } else if ("".equals(fieldValue)) { if (fulltext.contains(fieldName)) { currentFilter.append(backendFieldName).append("=*"); } else { currentFilter.append("!(").append(backendFieldName).append("=*)"); } } else { currentFilter.append(backendFieldName).append("="); if (fulltext.contains(fieldName)) { switch (substringMatchType) { case subinitial: currentFilter.append("{").append(index).append("}*"); break; case subfinal: currentFilter.append("*{").append(index).append("}"); break; case subany: currentFilter.append("*{").append(index).append("}*"); break; } } else { currentFilter.append("{").append(index).append("}"); } } currentFilter.append(")"); filters[index] = currentFilter.toString(); if (fieldValue != null && !"".equals(fieldValue)) { if (fieldValue instanceof Blob) { log.warn("Binary search is not supported"); } else { filterArgs[index] = fieldValue.toString(); } } index++; } String filterExpr = "(&" + getDirectory().getBaseFilter() + StringUtils.join(filters) + ')'; SearchControls scts = getDirectory().getSearchControls(true); if (log.isDebugEnabled()) { log.debug(String.format( "LDAPSession.query(...): LDAP search base='%s' filter='%s' args='%s' scope='%s' [%s]", searchBaseDn, filterExpr, StringUtils.join(filterArgs, ","), scts.getSearchScope(), this)); } try { NamingEnumeration<SearchResult> results = getContext().search(searchBaseDn, filterExpr, filterArgs, scts); DocumentModelList entries = ldapResultsToDocumentModels(results, fetchReferences); if (orderBy != null && !orderBy.isEmpty()) { getDirectory().orderEntries(entries, orderBy); } return applyQueryLimits(entries, limit, offset); } catch (NameNotFoundException nnfe) { log.error("Unexpected response from server while performing query: " + nnfe.getMessage(), nnfe); return new DocumentModelListImpl(); } } catch (LimitExceededException e) { throw new org.nuxeo.ecm.directory.SizeLimitExceededException(e); } catch (NamingException e) { throw new DirectoryException("executeQuery failed", e); } } }
LDAPSession extends BaseSession { @Override public DocumentModelList query(Map<String, Serializable> filter, Set<String> fulltext, Map<String, String> orderBy, boolean fetchReferences, int limit, int offset) { if (!hasPermission(SecurityConstants.READ)) { return new DocumentModelListImpl(); } try { String[] filters = new String[filter.size()]; String[] filterArgs = new String[filter.size()]; if (fulltext == null) { fulltext = Collections.emptySet(); } int index = 0; for (String fieldName : filter.keySet()) { if (getDirectory().isReference(fieldName)) { log.warn(fieldName + " is a reference and will be ignored as a query criterion"); continue; } String backendFieldName = getDirectory().getFieldMapper().getBackendField(fieldName); Object fieldValue = filter.get(fieldName); StringBuilder currentFilter = new StringBuilder(); currentFilter.append("("); if (fieldValue == null) { currentFilter.append("!(").append(backendFieldName).append("=*)"); } else if ("".equals(fieldValue)) { if (fulltext.contains(fieldName)) { currentFilter.append(backendFieldName).append("=*"); } else { currentFilter.append("!(").append(backendFieldName).append("=*)"); } } else { currentFilter.append(backendFieldName).append("="); if (fulltext.contains(fieldName)) { switch (substringMatchType) { case subinitial: currentFilter.append("{").append(index).append("}*"); break; case subfinal: currentFilter.append("*{").append(index).append("}"); break; case subany: currentFilter.append("*{").append(index).append("}*"); break; } } else { currentFilter.append("{").append(index).append("}"); } } currentFilter.append(")"); filters[index] = currentFilter.toString(); if (fieldValue != null && !"".equals(fieldValue)) { if (fieldValue instanceof Blob) { log.warn("Binary search is not supported"); } else { filterArgs[index] = fieldValue.toString(); } } index++; } String filterExpr = "(&" + getDirectory().getBaseFilter() + StringUtils.join(filters) + ')'; SearchControls scts = getDirectory().getSearchControls(true); if (log.isDebugEnabled()) { log.debug(String.format( "LDAPSession.query(...): LDAP search base='%s' filter='%s' args='%s' scope='%s' [%s]", searchBaseDn, filterExpr, StringUtils.join(filterArgs, ","), scts.getSearchScope(), this)); } try { NamingEnumeration<SearchResult> results = getContext().search(searchBaseDn, filterExpr, filterArgs, scts); DocumentModelList entries = ldapResultsToDocumentModels(results, fetchReferences); if (orderBy != null && !orderBy.isEmpty()) { getDirectory().orderEntries(entries, orderBy); } return applyQueryLimits(entries, limit, offset); } catch (NameNotFoundException nnfe) { log.error("Unexpected response from server while performing query: " + nnfe.getMessage(), nnfe); return new DocumentModelListImpl(); } } catch (LimitExceededException e) { throw new org.nuxeo.ecm.directory.SizeLimitExceededException(e); } catch (NamingException e) { throw new DirectoryException("executeQuery failed", e); } } LDAPSession(LDAPDirectory directory); }
LDAPSession extends BaseSession { @Override public DocumentModelList query(Map<String, Serializable> filter, Set<String> fulltext, Map<String, String> orderBy, boolean fetchReferences, int limit, int offset) { if (!hasPermission(SecurityConstants.READ)) { return new DocumentModelListImpl(); } try { String[] filters = new String[filter.size()]; String[] filterArgs = new String[filter.size()]; if (fulltext == null) { fulltext = Collections.emptySet(); } int index = 0; for (String fieldName : filter.keySet()) { if (getDirectory().isReference(fieldName)) { log.warn(fieldName + " is a reference and will be ignored as a query criterion"); continue; } String backendFieldName = getDirectory().getFieldMapper().getBackendField(fieldName); Object fieldValue = filter.get(fieldName); StringBuilder currentFilter = new StringBuilder(); currentFilter.append("("); if (fieldValue == null) { currentFilter.append("!(").append(backendFieldName).append("=*)"); } else if ("".equals(fieldValue)) { if (fulltext.contains(fieldName)) { currentFilter.append(backendFieldName).append("=*"); } else { currentFilter.append("!(").append(backendFieldName).append("=*)"); } } else { currentFilter.append(backendFieldName).append("="); if (fulltext.contains(fieldName)) { switch (substringMatchType) { case subinitial: currentFilter.append("{").append(index).append("}*"); break; case subfinal: currentFilter.append("*{").append(index).append("}"); break; case subany: currentFilter.append("*{").append(index).append("}*"); break; } } else { currentFilter.append("{").append(index).append("}"); } } currentFilter.append(")"); filters[index] = currentFilter.toString(); if (fieldValue != null && !"".equals(fieldValue)) { if (fieldValue instanceof Blob) { log.warn("Binary search is not supported"); } else { filterArgs[index] = fieldValue.toString(); } } index++; } String filterExpr = "(&" + getDirectory().getBaseFilter() + StringUtils.join(filters) + ')'; SearchControls scts = getDirectory().getSearchControls(true); if (log.isDebugEnabled()) { log.debug(String.format( "LDAPSession.query(...): LDAP search base='%s' filter='%s' args='%s' scope='%s' [%s]", searchBaseDn, filterExpr, StringUtils.join(filterArgs, ","), scts.getSearchScope(), this)); } try { NamingEnumeration<SearchResult> results = getContext().search(searchBaseDn, filterExpr, filterArgs, scts); DocumentModelList entries = ldapResultsToDocumentModels(results, fetchReferences); if (orderBy != null && !orderBy.isEmpty()) { getDirectory().orderEntries(entries, orderBy); } return applyQueryLimits(entries, limit, offset); } catch (NameNotFoundException nnfe) { log.error("Unexpected response from server while performing query: " + nnfe.getMessage(), nnfe); return new DocumentModelListImpl(); } } catch (LimitExceededException e) { throw new org.nuxeo.ecm.directory.SizeLimitExceededException(e); } catch (NamingException e) { throw new DirectoryException("executeQuery failed", e); } } LDAPSession(LDAPDirectory directory); @Override LDAPDirectory getDirectory(); DirContext getContext(); @Override void deleteEntryWithoutReferences(String id); @Override boolean hasEntry(String id); @Override void deleteEntry(String id, Map<String, String> map); @Override DocumentModel getEntryFromSource(String id, boolean fetchReferences); @Override DocumentModelList query(Map<String, Serializable> filter, Set<String> fulltext, Map<String, String> orderBy,
            boolean fetchReferences, int limit, int offset); @Override DocumentModelList query(QueryBuilder queryBuilder, boolean fetchReferences); @Override List<String> queryIds(QueryBuilder queryBuilder); @Override void close(); @Override boolean authenticate(String username, String password); @Override boolean isAuthenticating(); boolean rdnMatchesIdField(); @Override // useful for the log function String toString(); @Override DocumentModel createEntry(DocumentModel entry); }
LDAPSession extends BaseSession { @Override public DocumentModelList query(Map<String, Serializable> filter, Set<String> fulltext, Map<String, String> orderBy, boolean fetchReferences, int limit, int offset) { if (!hasPermission(SecurityConstants.READ)) { return new DocumentModelListImpl(); } try { String[] filters = new String[filter.size()]; String[] filterArgs = new String[filter.size()]; if (fulltext == null) { fulltext = Collections.emptySet(); } int index = 0; for (String fieldName : filter.keySet()) { if (getDirectory().isReference(fieldName)) { log.warn(fieldName + " is a reference and will be ignored as a query criterion"); continue; } String backendFieldName = getDirectory().getFieldMapper().getBackendField(fieldName); Object fieldValue = filter.get(fieldName); StringBuilder currentFilter = new StringBuilder(); currentFilter.append("("); if (fieldValue == null) { currentFilter.append("!(").append(backendFieldName).append("=*)"); } else if ("".equals(fieldValue)) { if (fulltext.contains(fieldName)) { currentFilter.append(backendFieldName).append("=*"); } else { currentFilter.append("!(").append(backendFieldName).append("=*)"); } } else { currentFilter.append(backendFieldName).append("="); if (fulltext.contains(fieldName)) { switch (substringMatchType) { case subinitial: currentFilter.append("{").append(index).append("}*"); break; case subfinal: currentFilter.append("*{").append(index).append("}"); break; case subany: currentFilter.append("*{").append(index).append("}*"); break; } } else { currentFilter.append("{").append(index).append("}"); } } currentFilter.append(")"); filters[index] = currentFilter.toString(); if (fieldValue != null && !"".equals(fieldValue)) { if (fieldValue instanceof Blob) { log.warn("Binary search is not supported"); } else { filterArgs[index] = fieldValue.toString(); } } index++; } String filterExpr = "(&" + getDirectory().getBaseFilter() + StringUtils.join(filters) + ')'; SearchControls scts = getDirectory().getSearchControls(true); if (log.isDebugEnabled()) { log.debug(String.format( "LDAPSession.query(...): LDAP search base='%s' filter='%s' args='%s' scope='%s' [%s]", searchBaseDn, filterExpr, StringUtils.join(filterArgs, ","), scts.getSearchScope(), this)); } try { NamingEnumeration<SearchResult> results = getContext().search(searchBaseDn, filterExpr, filterArgs, scts); DocumentModelList entries = ldapResultsToDocumentModels(results, fetchReferences); if (orderBy != null && !orderBy.isEmpty()) { getDirectory().orderEntries(entries, orderBy); } return applyQueryLimits(entries, limit, offset); } catch (NameNotFoundException nnfe) { log.error("Unexpected response from server while performing query: " + nnfe.getMessage(), nnfe); return new DocumentModelListImpl(); } } catch (LimitExceededException e) { throw new org.nuxeo.ecm.directory.SizeLimitExceededException(e); } catch (NamingException e) { throw new DirectoryException("executeQuery failed", e); } } LDAPSession(LDAPDirectory directory); @Override LDAPDirectory getDirectory(); DirContext getContext(); @Override void deleteEntryWithoutReferences(String id); @Override boolean hasEntry(String id); @Override void deleteEntry(String id, Map<String, String> map); @Override DocumentModel getEntryFromSource(String id, boolean fetchReferences); @Override DocumentModelList query(Map<String, Serializable> filter, Set<String> fulltext, Map<String, String> orderBy,
            boolean fetchReferences, int limit, int offset); @Override DocumentModelList query(QueryBuilder queryBuilder, boolean fetchReferences); @Override List<String> queryIds(QueryBuilder queryBuilder); @Override void close(); @Override boolean authenticate(String username, String password); @Override boolean isAuthenticating(); boolean rdnMatchesIdField(); @Override // useful for the log function String toString(); @Override DocumentModel createEntry(DocumentModel entry);  }