Пример #1
0
  private static DirectoryEntry getDirectoryEntryFromNode(
      JsonNode propertiesNode, Directory directory) throws DirectoryException, IOException {

    String schema = directory.getSchema();
    String id = propertiesNode.get(directory.getIdField()).getTextValue();

    try (Session session = directory.getSession()) {
      DocumentModel entry = session.getEntry(id);

      if (entry == null) {
        entry = BaseSession.createEntryModel(null, schema, id, new HashMap<>());
      }

      Properties props = new Properties();
      Iterator<Entry<String, JsonNode>> fields = propertiesNode.getFields();
      while (fields.hasNext()) {
        Entry<String, JsonNode> fieldEntry = fields.next();
        props.put(schema + ":" + fieldEntry.getKey(), fieldEntry.getValue().getTextValue());
      }

      DocumentHelper.setProperties(null, entry, props);

      return new DirectoryEntry(directory.getName(), entry);
    }
  }
  public String getVocabularyLabel(String voc_name, String key) {

    DirectoryService ds = Framework.getLocalService(DirectoryService.class);
    if (ds == null) {
      return key;
    }

    List<String> vocs = new ArrayList<String>();
    List<String> keys = new ArrayList<String>();
    if (voc_name.contains("/")) {
      String[] parts = voc_name.split("\\/");
      for (String part : parts) {
        vocs.add(part);
      }
      parts = key.split("\\/");
      for (String part : parts) {
        keys.add(part);
      }
    } else {
      vocs.add(voc_name);
      keys.add(key);
    }

    List<String> values = new ArrayList<String>();

    for (int i = 0; i < vocs.size(); i++) {
      String voc = vocs.get(i);
      String keyValue = keys.get(i);
      if (ds.getDirectoryNames().contains(voc)) {
        Directory dir = ds.getDirectory(voc);
        String schema = dir.getSchema();
        if ("vocabulary".equals(schema) || "xvocabulary".equals(schema)) {
          try (Session session = dir.getSession()) {
            DocumentModel entry = session.getEntry(keyValue);
            if (entry != null) {
              values.add((String) entry.getProperty(schema, "label"));
            }
          }
        }
      }
    }
    if (values.size() == 0) {
      return key;
    } else if (values.size() == 1) {
      return values.get(0);
    } else {
      String result = "";
      for (int i = 0; i < values.size(); i++) {
        if (i > 0) {
          result = result + " / ";
        }
        result = result + values.get(i);
      }
      return result;
    }
  }