private void fillValueWithMetadata(
     final TemplateTree tree,
     final ValueNode root,
     final Object metadata,
     final Map<String, Set<Object>> excluded,
     final boolean prune,
     final boolean overwrite)
     throws ParseException {
   final List<ValueNode> children = new ArrayList<>(root.children);
   for (ValueNode node : children) {
     final ValueNode origNode = new ValueNode(node);
     final Object obj = getValue(node, metadata, excluded, overwrite);
     if (obj instanceof Collection && !((Collection) obj).isEmpty()) {
       final Iterator it = ((Collection) obj).iterator();
       int i = node.ordinal;
       while (it.hasNext()) {
         Object child = it.next();
         node = tree.duplicateNode(origNode, i);
         if (node.isField()) {
           node.value = valueToString(node, child, !prune, overwrite);
         } else {
           fillValueWithMetadata(tree, node, child, excluded, prune, overwrite);
         }
         i++;
       }
     } else {
       if (node.isField()) {
         node.value = valueToString(node, obj, !prune, overwrite);
       } else {
         fillValueWithMetadata(tree, node, obj, excluded, prune, overwrite);
       }
     }
   }
 }
  /**
   * Write a metadata Object into a template
   *
   * @param template
   * @param metadata
   * @param prune
   * @param overwrite
   * @return
   * @throws org.constellation.json.metadata.ParseException
   */
  public RootObj writeTemplate(
      final RootObj template, final Object metadata, final boolean prune, final boolean overwrite)
      throws ParseException {
    final TemplateTree tree = TemplateTree.getTreeFromRootObj(template);

    fillValueWithMetadata(
        tree, tree.getRoot(), metadata, new HashMap<String, Set<Object>>(), prune, overwrite);
    if (prune) {
      TemplateTree.pruneTree(tree, tree.getRoot());
    }
    return TemplateTree.getRootObjFromTree(template, tree, prune);
  }
Example #3
0
  @Override
  public List<TemplateTreeItem> getTemplateTreeItems(TemplateTree tt) {
    ArrayList<TemplateTreeItem> result = new ArrayList<TemplateTreeItem>();
    final StoreDB.Api api = store.getApi();
    try {
      final List<TemplateTreeItem> fromCache = cache.getTemplateTreeItems(tt);
      if (fromCache != null) {
        return fromCache;
      }

      return cache.putTemplateTreeItems(
          tt, toClientTemplateTreeItems(api.getTemplateTreeItems(tt.getId())));
    } catch (SQLException ex) {
      log.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    } finally {
      api.close();
    }

    return result;
  }
Example #4
0
  public static void main(String[] args) {
    TemplateManager tm = TemplateManager.getInstance();

    TemplateTree root = new TemplateTree(tm.get("word[0]"));

    TemplateTree t2 = new TemplateTree(tm.get("pos[-1]"));
    root.addChild(t2);
    t2.addChild(new Features());

    TemplateTree t3 = new TemplateTree(tm.get("pos[0]"));
    root.addChild(t3);
    t3.addChild(new Features());

    TemplateTree t4 = new TemplateTree(tm.get("pos[-1]"));
    t2.addChild(t4);
    t4.addChild(new Features());

    Document doc = DocumentTester.getMockDocument();
    Context ctx = new Context(doc, 1);
    ctx.token = 2;
    root.apply(ctx);
    System.out.println(ctx);
  }