public static DocumentModelList getDirectoryEntries(String directoryName, String... entryIds)
     throws ClientException {
   if (entryIds == null) {
     return null;
   }
   DirectoryService dirService = Framework.getService(DirectoryService.class);
   try (Session session = dirService.open(directoryName)) {
     DocumentModelList result = new DocumentModelListImpl();
     for (String entryId : entryIds) {
       DocumentModel entry = session.getEntry(entryId);
       if (entry != null) {
         result.add(entry);
       }
     }
     return result;
   }
 }
 protected void updateUserCatalog(UserManager userManager) throws ClientException {
   DocumentModelList allUsers = userManager.searchUsers(null);
   userCatalog = new HashMap<String, DocumentModelList>();
   String userSortField = userManager.getUserSortField();
   for (DocumentModel user : allUsers) {
     // FIXME: this should use a "display name" dedicated API
     String displayName = null;
     if (userSortField != null) {
       // XXX hack, principals have only one model
       org.nuxeo.ecm.core.api.DataModel dm = user.getDataModels().values().iterator().next();
       displayName = (String) dm.getData(userSortField);
     }
     if (StringUtils.isEmpty(displayName)) {
       displayName = user.getId();
     }
     String firstLetter = displayName.substring(0, 1).toUpperCase();
     DocumentModelList list = userCatalog.get(firstLetter);
     if (list == null) {
       list = new DocumentModelListImpl();
       userCatalog.put(firstLetter, list);
     }
     list.add(user);
   }
 }
  @Override
  @SuppressWarnings("boxing")
  public DocumentModelList query(
      Map<String, Serializable> filter,
      Set<String> fulltext,
      Map<String, String> orderBy,
      boolean fetchReferences) {
    // list of entries
    final DocumentModelList results = new DocumentModelListImpl();
    if (!isCurrentUserAllowed(SecurityConstants.READ)) {
      return results;
    }
    init();

    // entry ids already seen (mapped to the source name)
    final Map<String, String> seen = new HashMap<String, String>();
    if (fulltext == null) {
      fulltext = Collections.emptySet();
    }
    Set<String> readOnlyEntries = new HashSet<String>();

    for (SourceInfo sourceInfo : sourceInfos) {
      // accumulated map for each entry
      final Map<String, Map<String, Object>> maps = new HashMap<String, Map<String, Object>>();
      // number of dirs seen for each entry
      final Map<String, Integer> counts = new HashMap<String, Integer>();

      // list of optional dirs where filter matches default values
      List<SubDirectoryInfo> optionalDirsMatching = new ArrayList<SubDirectoryInfo>();
      for (SubDirectoryInfo dirInfo : sourceInfo.subDirectoryInfos) {
        // compute filter
        final Map<String, Serializable> dirFilter = new HashMap<String, Serializable>();
        for (Entry<String, Serializable> e : filter.entrySet()) {
          final String fieldName = dirInfo.fromSource.get(e.getKey());
          if (fieldName == null) {
            continue;
          }
          dirFilter.put(fieldName, e.getValue());
        }
        if (dirInfo.isOptional) {
          // check if filter matches directory default values
          boolean matches = true;
          for (Map.Entry<String, Serializable> dirFilterEntry : dirFilter.entrySet()) {
            Object defaultValue = dirInfo.defaultEntry.get(dirFilterEntry.getKey());
            Object filterValue = dirFilterEntry.getValue();
            if (defaultValue == null && filterValue != null) {
              matches = false;
            } else if (defaultValue != null && !defaultValue.equals(filterValue)) {
              matches = false;
            }
          }
          if (matches) {
            optionalDirsMatching.add(dirInfo);
          }
        }
        // compute fulltext
        Set<String> dirFulltext = new HashSet<String>();
        for (String sourceFieldName : fulltext) {
          final String fieldName = dirInfo.fromSource.get(sourceFieldName);
          if (fieldName != null) {
            dirFulltext.add(fieldName);
          }
        }
        // make query to subdirectory
        DocumentModelList l =
            dirInfo.getSession().query(dirFilter, dirFulltext, null, fetchReferences);
        for (DocumentModel entry : l) {
          final String id = entry.getId();
          Map<String, Object> map = maps.get(id);
          if (map == null) {
            map = new HashMap<String, Object>();
            maps.put(id, map);
            counts.put(id, 1);
          } else {
            counts.put(id, counts.get(id) + 1);
          }
          for (Entry<String, String> e : dirInfo.toSource.entrySet()) {
            map.put(e.getValue(), entry.getProperty(dirInfo.dirSchemaName, e.getKey()));
          }
          if (BaseSession.isReadOnlyEntry(entry)) {
            readOnlyEntries.add(id);
          }
        }
      }
      // add default entry values for optional dirs
      for (SubDirectoryInfo dirInfo : optionalDirsMatching) {
        // add entry for every data found in other dirs
        Set<String> existingIds =
            new HashSet<String>(
                dirInfo
                    .getSession()
                    .getProjection(Collections.<String, Serializable>emptyMap(), dirInfo.idField));
        for (Entry<String, Map<String, Object>> result : maps.entrySet()) {
          final String id = result.getKey();
          if (!existingIds.contains(id)) {
            counts.put(id, counts.get(id) + 1);
            final Map<String, Object> map = result.getValue();
            for (Entry<String, String> e : dirInfo.toSource.entrySet()) {
              String value = e.getValue();
              if (!map.containsKey(value)) {
                map.put(value, dirInfo.defaultEntry.get(e.getKey()));
              }
            }
          }
        }
      }
      // intersection, ignore entries not in all subdirectories
      final int numdirs = sourceInfo.subDirectoryInfos.size();
      for (Iterator<String> it = maps.keySet().iterator(); it.hasNext(); ) {
        final String id = it.next();
        if (counts.get(id) != numdirs) {
          it.remove();
        }
      }
      // now create entries
      ((ArrayList<?>) results).ensureCapacity(results.size() + maps.size());
      for (Entry<String, Map<String, Object>> e : maps.entrySet()) {
        final String id = e.getKey();
        if (seen.containsKey(id)) {
          log.warn(
              String.format(
                  "Entry '%s' is present in source '%s' but also in source '%s'. "
                      + "The second one will be ignored.",
                  id, seen.get(id), sourceInfo.source.name));
          continue;
        }
        final Map<String, Object> map = e.getValue();
        seen.put(id, sourceInfo.source.name);
        final DocumentModel entry =
            BaseSession.createEntryModel(null, schemaName, id, map, readOnlyEntries.contains(id));
        results.add(entry);
      }
    }
    if (orderBy != null && !orderBy.isEmpty()) {
      directory.orderEntries(results, orderBy);
    }
    return results;
  }
  @Override
  @SuppressWarnings("boxing")
  public DocumentModelList getEntries() {
    if (!isCurrentUserAllowed(SecurityConstants.READ)) {
      return null;
    }
    init();

    // list of entries
    final DocumentModelList results = new DocumentModelListImpl();
    // entry ids already seen (mapped to the source name)
    final Map<String, String> seen = new HashMap<String, String>();
    Set<String> readOnlyEntries = new HashSet<String>();

    for (SourceInfo sourceInfo : sourceInfos) {
      // accumulated map for each entry
      final Map<String, Map<String, Object>> maps = new HashMap<String, Map<String, Object>>();
      // number of dirs seen for each entry
      final Map<String, Integer> counts = new HashMap<String, Integer>();
      for (SubDirectoryInfo dirInfo : sourceInfo.requiredSubDirectoryInfos) {
        final DocumentModelList entries = dirInfo.getSession().getEntries();
        for (DocumentModel entry : entries) {
          final String id = entry.getId();
          // find or create map for this entry
          Map<String, Object> map = maps.get(id);
          if (map == null) {
            map = new HashMap<String, Object>();
            maps.put(id, map);
            counts.put(id, 1);
          } else {
            counts.put(id, counts.get(id) + 1);
          }
          // put entry data in map
          for (Entry<String, String> e : dirInfo.toSource.entrySet()) {
            map.put(e.getValue(), entry.getProperty(dirInfo.dirSchemaName, e.getKey()));
          }
          if (BaseSession.isReadOnlyEntry(entry)) {
            readOnlyEntries.add(id);
          }
        }
      }
      for (SubDirectoryInfo dirInfo : sourceInfo.optionalSubDirectoryInfos) {
        final DocumentModelList entries = dirInfo.getSession().getEntries();
        Set<String> existingIds = new HashSet<String>();
        for (DocumentModel entry : entries) {
          final String id = entry.getId();
          final Map<String, Object> map = maps.get(id);
          if (map != null) {
            existingIds.add(id);
            // put entry data in map
            for (Entry<String, String> e : dirInfo.toSource.entrySet()) {
              map.put(e.getValue(), entry.getProperty(dirInfo.dirSchemaName, e.getKey()));
            }
          } else {
            log.warn(
                String.format(
                    "Entry '%s' for source '%s' is present in optional directory '%s' "
                        + "but not in any required one. "
                        + "It will be skipped.",
                    id, sourceInfo.source.name, dirInfo.dirName));
          }
        }
        for (Entry<String, Map<String, Object>> mapEntry : maps.entrySet()) {
          if (!existingIds.contains(mapEntry.getKey())) {
            final Map<String, Object> map = mapEntry.getValue();
            // put entry data in map
            for (Entry<String, String> e : dirInfo.toSource.entrySet()) {
              // fill with default values for this directory
              if (!map.containsKey(e.getValue())) {
                map.put(e.getValue(), dirInfo.defaultEntry.get(e.getKey()));
              }
            }
          }
        }
      }
      // now create entries for all full maps
      int numdirs = sourceInfo.requiredSubDirectoryInfos.size();
      ((ArrayList<?>) results).ensureCapacity(results.size() + maps.size());
      for (Entry<String, Map<String, Object>> e : maps.entrySet()) {
        final String id = e.getKey();
        if (seen.containsKey(id)) {
          log.warn(
              String.format(
                  "Entry '%s' is present in source '%s' but also in source '%s'. "
                      + "The second one will be ignored.",
                  id, seen.get(id), sourceInfo.source.name));
          continue;
        }
        final Map<String, Object> map = e.getValue();
        if (counts.get(id) != numdirs) {
          log.warn(
              String.format(
                  "Entry '%s' for source '%s' is not present in all directories. "
                      + "It will be skipped.",
                  id, sourceInfo.source.name));
          continue;
        }
        seen.put(id, sourceInfo.source.name);
        final DocumentModel entry =
            BaseSession.createEntryModel(null, schemaName, id, map, readOnlyEntries.contains(id));
        results.add(entry);
      }
    }
    return results;
  }