Search in sources :

Example 26 with PropertyDefinitionTemplate

use of javax.jcr.nodetype.PropertyDefinitionTemplate in project kylo by Teradata.

the class JcrExtensibleTypeProvider method buildType.

/**
 * Builds the type defined by the specified builder.
 *
 * @param builder the builder for the type
 * @return the type
 * @throws MetadataRepositoryException if the repository is unavailable
 * @throws TypeAlreadyExistsException  if a type with the same name already exists
 */
@Nonnull
private ExtensibleType buildType(@Nonnull final TypeBuilder builder) {
    try {
        // Remove existing type and subgraph
        if (builder.node != null) {
            builder.node.remove();
        }
        // Get or create the type node
        final Session session = getSession();
        final Node typeNode;
        if (!session.nodeExists(JcrUtil.path(session.getRootNode().getPath(), ExtensionsConstants.TYPES + "/" + builder.name).toString())) {
            typeNode = session.getRootNode().addNode(ExtensionsConstants.TYPES + "/" + builder.name, ExtensionsConstants.TYPE_DESCRIPTOR_TYPE);
        } else {
            throw new TypeAlreadyExistsException(builder.name);
        }
        // Update type metadata
        if (builder.displayName != null) {
            typeNode.setProperty(JcrExtensibleType.NAME, builder.displayName);
        }
        if (builder.description != null) {
            typeNode.setProperty(JcrExtensibleType.DESCRIPTION, builder.description);
        }
        // Update type definition
        final NodeTypeManager typeMgr = session.getWorkspace().getNodeTypeManager();
        final NodeTypeTemplate nodeTemplate = typeMgr.createNodeTypeTemplate();
        nodeTemplate.setName(builder.name);
        if (builder.supertype != null) {
            final JcrExtensibleType superImpl = (JcrExtensibleType) builder.supertype;
            final String supername = superImpl.getJcrName();
            nodeTemplate.setDeclaredSuperTypeNames(new String[] { ExtensionsConstants.EXTENSIBLE_ENTITY_TYPE, supername });
        } else {
            nodeTemplate.setDeclaredSuperTypeNames(new String[] { ExtensionsConstants.EXTENSIBLE_ENTITY_TYPE });
        }
        // Update field definitions
        @SuppressWarnings("unchecked") final List<PropertyDefinitionTemplate> propertyDefinitionTemplates = nodeTemplate.getPropertyDefinitionTemplates();
        for (final FieldBuilder bldr : builder.fieldBuilders) {
            // Create field
            PropertyDefinitionTemplate propDef = typeMgr.createPropertyDefinitionTemplate();
            propDef.setName(bldr.name);
            propDef.setRequiredType(asCode(bldr.type));
            propDef.setMandatory(bldr.required);
            propDef.setMultiple(bldr.collection);
            propertyDefinitionTemplates.add(propDef);
            // Set field metadata
            Node fieldNode = typeNode.addNode(bldr.name, ExtensionsConstants.FIELD_DESCRIPTOR_TYPE);
            for (final Map.Entry<String, String> entry : bldr.metadata.entrySet()) {
                fieldNode.setProperty(entry.getKey(), entry.getValue());
            }
        }
        NodeType nodeType = typeMgr.registerNodeType(nodeTemplate, true);
        return new JcrExtensibleType(typeNode, nodeType);
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException("Failed to create type: " + builder.name, e);
    }
}
Also used : MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) Node(javax.jcr.Node) MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) TypeAlreadyExistsException(com.thinkbiganalytics.metadata.modeshape.TypeAlreadyExistsException) NodeType(javax.jcr.nodetype.NodeType) HashMap(java.util.HashMap) Map(java.util.Map) Session(javax.jcr.Session) Nonnull(javax.annotation.Nonnull)

Example 27 with PropertyDefinitionTemplate

use of javax.jcr.nodetype.PropertyDefinitionTemplate in project jackrabbit by apache.

the class NodeTypeCreationTest method testNewNodeTypeTemplate.

public void testNewNodeTypeTemplate() throws Exception {
    String expandedName = "{" + NS_MIX_URI + "}" + "littlemixin";
    String jcrName = superuser.getNamespacePrefix(NS_MIX_URI) + ":littlemixin";
    NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
    ntt.setName(expandedName);
    assertEquals(jcrName, ntt.getName());
    ntt.setName(jcrName);
    assertEquals(jcrName, ntt.getName());
    ntt.setAbstract(false);
    assertFalse(ntt.isAbstract());
    try {
        ntt.setDeclaredSuperTypeNames(null);
        fail("null isn't a valid array of jcr name");
    } catch (ConstraintViolationException e) {
    // success
    }
    assertNotNull(ntt.getDeclaredSupertypeNames());
    assertEquals(0, ntt.getDeclaredSupertypeNames().length);
    ntt.setDeclaredSuperTypeNames(new String[] { mixReferenceable });
    assertNotNull(ntt.getDeclaredSupertypeNames());
    assertEquals(1, ntt.getDeclaredSupertypeNames().length);
    assertEquals(mixReferenceable, ntt.getDeclaredSupertypeNames()[0]);
    ntt.setMixin(true);
    assertTrue(ntt.isMixin());
    ntt.setOrderableChildNodes(true);
    assertTrue(ntt.hasOrderableChildNodes());
    ntt.setQueryable(false);
    assertFalse(ntt.isQueryable());
    ntt.setPrimaryItemName(null);
    assertNull(ntt.getPrimaryItemName());
    ntt.setPrimaryItemName(jcrPrimaryType);
    assertEquals(jcrPrimaryType, ntt.getPrimaryItemName());
    PropertyDefinitionTemplate pdTemplate = createBooleanPropTemplate();
    List pdefs = ntt.getPropertyDefinitionTemplates();
    pdefs.add(pdTemplate);
    assertNotNull(ntt.getDeclaredPropertyDefinitions());
    assertEquals(1, ntt.getDeclaredPropertyDefinitions().length);
    assertEquals(pdTemplate, ntt.getDeclaredPropertyDefinitions()[0]);
    pdefs = ntt.getPropertyDefinitionTemplates();
    assertEquals(1, pdefs.size());
    assertEquals(pdTemplate, pdefs.get(0));
    NodeDefinitionTemplate ndTemplate = ntm.createNodeDefinitionTemplate();
    List ndefs = ntt.getNodeDefinitionTemplates();
    ndefs.add(ndTemplate);
    assertNotNull(ntt.getDeclaredChildNodeDefinitions());
    assertEquals(1, ntt.getDeclaredChildNodeDefinitions().length);
    assertEquals(ndTemplate, ntt.getDeclaredChildNodeDefinitions()[0]);
    ndefs = ntt.getNodeDefinitionTemplates();
    assertEquals(1, ndefs.size());
    assertEquals(ndTemplate, ndefs.get(0));
}
Also used : NodeDefinitionTemplate(javax.jcr.nodetype.NodeDefinitionTemplate) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) List(java.util.List)

Example 28 with PropertyDefinitionTemplate

use of javax.jcr.nodetype.PropertyDefinitionTemplate in project jackrabbit by apache.

the class NodeTypeCreationTest method testRegisterNodeTypes.

public void testRegisterNodeTypes() throws Exception {
    NodeTypeDefinition[] defs = new NodeTypeDefinition[5];
    for (int i = 0; i < defs.length; i++) {
        NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
        ntt.setName("mix:foo" + i);
        ntt.setAbstract(false);
        ntt.setMixin(true);
        ntt.setOrderableChildNodes(false);
        ntt.setQueryable(false);
        PropertyDefinitionTemplate pdt = ntm.createPropertyDefinitionTemplate();
        pdt.setAutoCreated(false);
        pdt.setName("foo" + i);
        pdt.setMultiple(false);
        pdt.setRequiredType(PropertyType.STRING);
        List pdefs = ntt.getPropertyDefinitionTemplates();
        pdefs.add(pdt);
        defs[i] = ntt;
    }
    ntm.registerNodeTypes(defs, true);
    try {
        ntm.registerNodeTypes(defs, false);
        fail("NodeTypeExistsException expected.");
    } catch (NodeTypeExistsException e) {
    // success
    }
}
Also used : NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) NodeTypeExistsException(javax.jcr.nodetype.NodeTypeExistsException) NodeTypeDefinition(javax.jcr.nodetype.NodeTypeDefinition) List(java.util.List)

Example 29 with PropertyDefinitionTemplate

use of javax.jcr.nodetype.PropertyDefinitionTemplate in project jackrabbit by apache.

the class NodeTypeCreationTest method testRegisterNodeType.

public void testRegisterNodeType() throws Exception {
    NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
    ntt.setName("mix:foo");
    ntt.setAbstract(false);
    ntt.setMixin(true);
    ntt.setOrderableChildNodes(false);
    ntt.setQueryable(false);
    PropertyDefinitionTemplate pdt = ntm.createPropertyDefinitionTemplate();
    pdt.setAutoCreated(false);
    pdt.setName("foo");
    pdt.setMultiple(false);
    pdt.setRequiredType(PropertyType.STRING);
    List pdefs = ntt.getPropertyDefinitionTemplates();
    pdefs.add(pdt);
    ntm.registerNodeType(ntt, true);
    try {
        ntm.registerNodeType(ntt, false);
        fail("NodeTypeExistsException expected.");
    } catch (NodeTypeExistsException e) {
    // success
    }
}
Also used : NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) NodeTypeExistsException(javax.jcr.nodetype.NodeTypeExistsException) List(java.util.List)

Example 30 with PropertyDefinitionTemplate

use of javax.jcr.nodetype.PropertyDefinitionTemplate in project jackrabbit by apache.

the class NodeTypeCreationTest method testResidualNames.

public void testResidualNames() throws Exception {
    String residualName = "*";
    NodeDefinitionTemplate ndt = ntm.createNodeDefinitionTemplate();
    ndt.setName(residualName);
    assertEquals(residualName, ndt.getName());
    PropertyDefinitionTemplate pdt = ntm.createPropertyDefinitionTemplate();
    pdt.setName(residualName);
    assertEquals(residualName, pdt.getName());
}
Also used : NodeDefinitionTemplate(javax.jcr.nodetype.NodeDefinitionTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate)

Aggregations

PropertyDefinitionTemplate (javax.jcr.nodetype.PropertyDefinitionTemplate)38 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)16 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)9 Node (javax.jcr.Node)8 Session (javax.jcr.Session)8 List (java.util.List)7 Value (javax.jcr.Value)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)4 NodeDefinitionTemplate (javax.jcr.nodetype.NodeDefinitionTemplate)4 Test (org.junit.Test)4 RepositoryException (javax.jcr.RepositoryException)3 NodeType (javax.jcr.nodetype.NodeType)3 NodeTypeDefinition (javax.jcr.nodetype.NodeTypeDefinition)3 KyloVersion (com.thinkbiganalytics.KyloVersion)2 JcrMetadataAccess (com.thinkbiganalytics.metadata.modeshape.JcrMetadataAccess)2 JcrUtil (com.thinkbiganalytics.metadata.modeshape.support.JcrUtil)2 JcrVersionUtil (com.thinkbiganalytics.metadata.modeshape.support.JcrVersionUtil)2 KyloUpgrader (com.thinkbiganalytics.server.upgrade.KyloUpgrader)2 UpgradeException (com.thinkbiganalytics.server.upgrade.UpgradeException)2