Ejemplo n.º 1
0
  /**
   * Returns a {@code NodeTypeTemplate} based on the definition given in {@code ntd}. This template
   * can then be used to define a node type and passed to {@link
   * JcrNodeTypeManager#registerNodeType(NodeTypeDefinition, boolean)}
   *
   * @param ntd an existing node type definition; null values will be ignored
   * @return an empty {@code NodeTypeTemplate} which can then be used to define a node type and
   *     passed to {@link JcrNodeTypeManager#registerNodeType(NodeTypeDefinition, boolean)}.
   * @throws RepositoryException if another error occurs
   */
  @Override
  @SuppressWarnings("unchecked")
  public NodeTypeTemplate createNodeTypeTemplate(NodeTypeDefinition ntd)
      throws RepositoryException {
    NodeTypeTemplate ntt = new JcrNodeTypeTemplate(context(), true);

    if (ntd != null) {
      ntt.setName(ntd.getName());
      ntt.setAbstract(ntd.isAbstract());
      ntt.setDeclaredSuperTypeNames(ntd.getDeclaredSupertypeNames());
      ntt.setMixin(ntd.isMixin());
      ntt.setOrderableChildNodes(ntd.hasOrderableChildNodes());
      ntt.setPrimaryItemName(ntd.getPrimaryItemName());
      ntt.setQueryable(ntd.isQueryable());

      // copy child nodes and props
      for (NodeDefinition nodeDefinition : ntd.getDeclaredChildNodeDefinitions()) {
        JcrNodeDefinitionTemplate ndt = new JcrNodeDefinitionTemplate(context());

        ndt.setAutoCreated(nodeDefinition.isAutoCreated());
        ndt.setDefaultPrimaryTypeName(nodeDefinition.getDefaultPrimaryTypeName());
        ndt.setMandatory(nodeDefinition.isMandatory());
        if (nodeDefinition.getName() != null) {
          ndt.setName(nodeDefinition.getName());
        }
        ndt.setOnParentVersion(nodeDefinition.getOnParentVersion());
        ndt.setProtected(nodeDefinition.isProtected());
        ndt.setRequiredPrimaryTypeNames(nodeDefinition.getRequiredPrimaryTypeNames());
        ndt.setSameNameSiblings(nodeDefinition.allowsSameNameSiblings());

        ntt.getNodeDefinitionTemplates().add(ndt);
      }

      for (PropertyDefinition propertyDefinition : ntd.getDeclaredPropertyDefinitions()) {
        JcrPropertyDefinitionTemplate pdt = new JcrPropertyDefinitionTemplate(context());

        pdt.setAutoCreated(propertyDefinition.isAutoCreated());
        pdt.setAvailableQueryOperators(propertyDefinition.getAvailableQueryOperators());
        pdt.setDefaultValues(propertyDefinition.getDefaultValues());
        pdt.setFullTextSearchable(propertyDefinition.isFullTextSearchable());
        pdt.setMandatory(propertyDefinition.isMandatory());
        pdt.setMultiple(propertyDefinition.isMultiple());
        if (propertyDefinition.getName() != null) {
          pdt.setName(propertyDefinition.getName());
        }
        pdt.setOnParentVersion(propertyDefinition.getOnParentVersion());
        pdt.setProtected(propertyDefinition.isProtected());
        pdt.setQueryOrderable(propertyDefinition.isQueryOrderable());
        pdt.setRequiredType(propertyDefinition.getRequiredType());
        pdt.setValueConstraints(propertyDefinition.getValueConstraints());

        ntt.getPropertyDefinitionTemplates().add(pdt);
      }
    }

    return ntt;
  }
  private JcrPropertyDefinition propertyDefinitionFrom(PropertyDefinition propDefn)
      throws RepositoryException {
    Name propertyName = nameFactory.create(propDefn.getName());
    int onParentVersionBehavior = propDefn.getOnParentVersion();
    int requiredType = propDefn.getRequiredType();
    boolean mandatory = propDefn.isMandatory();
    boolean multiple = propDefn.isMultiple();
    boolean autoCreated = propDefn.isAutoCreated();
    boolean isProtected = propDefn.isProtected();
    boolean fullTextSearchable = propDefn.isFullTextSearchable();
    boolean queryOrderable = propDefn.isQueryOrderable();

    Value[] defaultValues = propDefn.getDefaultValues();
    JcrValue[] jcrDefaultValues = null;
    if (defaultValues != null) {
      jcrDefaultValues = new JcrValue[defaultValues.length];
      for (int i = 0; i != defaultValues.length; ++i) {
        Value value = defaultValues[i];
        jcrDefaultValues[i] = new JcrValue(this.context.getValueFactories(), value);
      }
    }

    String[] valueConstraints = propDefn.getValueConstraints();
    String[] queryOperators = propDefn.getAvailableQueryOperators();
    if (valueConstraints == null) valueConstraints = new String[0];
    NodeKey prototypeKey = repository.repositoryCache().getSystemKey();
    return new JcrPropertyDefinition(
        this.context,
        null,
        prototypeKey,
        propertyName,
        onParentVersionBehavior,
        autoCreated,
        mandatory,
        isProtected,
        jcrDefaultValues,
        requiredType,
        valueConstraints,
        multiple,
        fullTextSearchable,
        queryOrderable,
        queryOperators);
  }