Search in sources :

Example 16 with NodeTypeManager

use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit-oak by apache.

the class RepositoryTest method importWithRegisteredType.

// OAK-2038
@Test
public void importWithRegisteredType() throws Exception {
    Session session = getAdminSession();
    NodeTypeManager ntMgr = getAdminSession().getWorkspace().getNodeTypeManager();
    NodeTypeTemplate ntd = ntMgr.createNodeTypeTemplate();
    ntd.setName("fooType");
    PropertyDefinitionTemplate propDefTemplate = ntMgr.createPropertyDefinitionTemplate();
    propDefTemplate.setName("fooProp");
    propDefTemplate.setRequiredType(PropertyType.STRING);
    ntd.getPropertyDefinitionTemplates().add(propDefTemplate);
    ntMgr.registerNodeType(ntd, false);
    Node node = session.getRootNode().addNode("node", "fooType");
    node.setProperty("fooProp", "fooValue");
    session.save();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    session.exportDocumentView("/node", out, true, false);
    node.remove();
    session.save();
    session.getWorkspace().importXML("/", new ByteArrayInputStream(out.toByteArray()), IMPORT_UUID_CREATE_NEW);
    session.save();
    assertEquals("fooValue", session.getProperty("/node/fooProp").getString());
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) ByteArrayInputStream(java.io.ByteArrayInputStream) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Session(javax.jcr.Session) Test(org.junit.Test)

Example 17 with NodeTypeManager

use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit-oak by apache.

the class CompatibilityIssuesTest method testBinaryCoercion.

@Test
public void testBinaryCoercion() throws RepositoryException, IOException {
    Session session = getAdminSession();
    // node type with default child-node type of to nt:base
    String ntName = "binaryCoercionTest";
    NodeTypeManager ntm = session.getWorkspace().getNodeTypeManager();
    NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
    ntt.setName(ntName);
    PropertyDefinitionTemplate propertyWithType = ntm.createPropertyDefinitionTemplate();
    propertyWithType.setName("javaObject");
    propertyWithType.setRequiredType(PropertyType.STRING);
    PropertyDefinitionTemplate unnamed = ntm.createPropertyDefinitionTemplate();
    unnamed.setName("*");
    unnamed.setRequiredType(PropertyType.UNDEFINED);
    List<PropertyDefinition> properties = ntt.getPropertyDefinitionTemplates();
    properties.add(propertyWithType);
    properties.add(unnamed);
    ntm.registerNodeType(ntt, false);
    Node node = session.getRootNode().addNode("testNodeForBinary", ntName);
    ByteArrayOutputStream bos = serializeObject("testValue");
    node.setProperty("javaObject", session.getValueFactory().createBinary(new ByteArrayInputStream(bos.toByteArray())));
    Assert.assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(bos.toByteArray()), node.getProperty("javaObject").getStream()));
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) ByteArrayInputStream(java.io.ByteArrayInputStream) Node(javax.jcr.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Session(javax.jcr.Session) Test(org.junit.Test)

Example 18 with NodeTypeManager

use of javax.jcr.nodetype.NodeTypeManager in project sling by apache.

the class NodeTypeConfigurationPrinter method printConfiguration.

/**
     * {@inheritDoc}
     */
public void printConfiguration(PrintWriter pw, String mode) {
    if (slingRepository != null) {
        Session session = null;
        try {
            session = slingRepository.loginAdministrative(null);
            NodeTypeManager ntm = session.getWorkspace().getNodeTypeManager();
            NodeTypeIterator it = ntm.getAllNodeTypes();
            List<NodeType> sortedTypes = sortTypes(it);
            for (NodeType nt : sortedTypes) {
                pw.printf("[%s]", nt.getName());
                printSuperTypes(pw, nt);
                if (nt.hasOrderableChildNodes()) {
                    pw.print(" orderable");
                }
                if (nt.isMixin()) {
                    pw.print(" mixin");
                }
                linebreak(pw, mode);
                for (PropertyDefinition prop : nt.getPropertyDefinitions()) {
                    if (prop.getDeclaringNodeType() == nt) {
                        startBold(pw, mode);
                    }
                    pw.printf("- %s", prop.getName());
                    printDefaultValues(pw, prop);
                    if (prop.getName().equals(nt.getPrimaryItemName())) {
                        pw.print(" primary");
                    }
                    if (prop.isMandatory()) {
                        pw.print(" mandatory");
                    }
                    if (prop.isAutoCreated()) {
                        pw.print(" autocreated");
                    }
                    if (prop.isProtected()) {
                        pw.print(" protected");
                    }
                    if (prop.isMultiple()) {
                        pw.print(" multiple");
                    }
                    pw.printf(" %s", OnParentVersionAction.nameFromValue(prop.getOnParentVersion()));
                    printConstraints(pw, prop);
                    if (prop.getDeclaringNodeType() == nt) {
                        stopBold(pw, mode);
                    }
                    linebreak(pw, mode);
                }
                for (NodeDefinition child : nt.getChildNodeDefinitions()) {
                    if (child.getDeclaringNodeType() == nt) {
                        startBold(pw, mode);
                    }
                    pw.printf("+ %s", child.getName());
                    printRequiredChildTypes(pw, child);
                    if (child.getDefaultPrimaryType() != null) {
                        pw.printf(" = %s", child.getDefaultPrimaryType().getName());
                    }
                    if (child.isMandatory()) {
                        pw.print(" mandatory");
                    }
                    if (child.isAutoCreated()) {
                        pw.print(" autocreated");
                    }
                    if (child.isProtected()) {
                        pw.print(" protected");
                    }
                    if (child.allowsSameNameSiblings()) {
                        pw.print(" multiple");
                    }
                    pw.printf(" %s", OnParentVersionAction.nameFromValue(child.getOnParentVersion()));
                    if (child.getDeclaringNodeType() == nt) {
                        stopBold(pw, mode);
                    }
                    linebreak(pw, mode);
                }
                linebreak(pw, mode);
            }
        } catch (RepositoryException e) {
            pw.println("Unable to output namespace mappings.");
            e.printStackTrace(pw);
        } finally {
            if (session != null) {
                session.logout();
            }
        }
    } else {
        pw.println("SlingRepository is not available.");
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeType(javax.jcr.nodetype.NodeType) NodeDefinition(javax.jcr.nodetype.NodeDefinition) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) RepositoryException(javax.jcr.RepositoryException) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Session(javax.jcr.Session)

Example 19 with NodeTypeManager

use of javax.jcr.nodetype.NodeTypeManager in project sling by apache.

the class NodeTypeDefinitionScanner method registerNodeTypes.

/**
     * Registers node types found in classpath in JCR repository.
     * @param session Session
     * @param nodeTypeResources List of classpath resource URLs pointing to node type definitions
     */
private void registerNodeTypes(Session session, List<String> nodeTypeResources) throws RepositoryException {
    ClassLoader classLoader = getClass().getClassLoader();
    Workspace workspace = session.getWorkspace();
    NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
    NamespaceRegistry namespaceRegistry = workspace.getNamespaceRegistry();
    ValueFactory valueFactory = session.getValueFactory();
    // try registering node types multiple times because the exact order is not known
    int iteration = 0;
    List<String> remainingNodeTypeResources = new ArrayList<String>(nodeTypeResources);
    while (!remainingNodeTypeResources.isEmpty()) {
        registerNodeTypesAndRemoveSucceeds(remainingNodeTypeResources, classLoader, nodeTypeManager, namespaceRegistry, valueFactory, false);
        iteration++;
        if (iteration >= MAX_ITERATIONS) {
            break;
        }
    }
    if (!remainingNodeTypeResources.isEmpty()) {
        registerNodeTypesAndRemoveSucceeds(remainingNodeTypeResources, classLoader, nodeTypeManager, namespaceRegistry, valueFactory, true);
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NamespaceRegistry(javax.jcr.NamespaceRegistry) ArrayList(java.util.ArrayList) ValueFactory(javax.jcr.ValueFactory) Workspace(javax.jcr.Workspace)

Example 20 with NodeTypeManager

use of javax.jcr.nodetype.NodeTypeManager in project archiva by apache.

the class JcrMetadataRepository method initialize.

public static void initialize(Session session) throws RepositoryException {
    // TODO: consider using namespaces for facets instead of the current approach:
    // (if used, check if actually called by normal injection)
    // for ( String facetId : metadataFacetFactories.keySet() )
    // {
    // session.getWorkspace().getNamespaceRegistry().registerNamespace( facetId, facetId );
    // }
    Workspace workspace = session.getWorkspace();
    NamespaceRegistry registry = workspace.getNamespaceRegistry();
    if (!Arrays.asList(registry.getPrefixes()).contains("archiva")) {
        registry.registerNamespace("archiva", "http://archiva.apache.org/jcr/");
    }
    NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
    registerMixinNodeType(nodeTypeManager, JcrMetadataRepository.NAMESPACE_NODE_TYPE);
    registerMixinNodeType(nodeTypeManager, JcrMetadataRepository.PROJECT_NODE_TYPE);
    registerMixinNodeType(nodeTypeManager, JcrMetadataRepository.PROJECT_VERSION_NODE_TYPE);
    registerMixinNodeType(nodeTypeManager, JcrMetadataRepository.ARTIFACT_NODE_TYPE);
    registerMixinNodeType(nodeTypeManager, JcrMetadataRepository.FACET_NODE_TYPE);
    registerMixinNodeType(nodeTypeManager, JcrMetadataRepository.DEPENDENCY_NODE_TYPE);
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NamespaceRegistry(javax.jcr.NamespaceRegistry) Workspace(javax.jcr.Workspace)

Aggregations

NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)103 NodeType (javax.jcr.nodetype.NodeType)47 Node (javax.jcr.Node)38 Session (javax.jcr.Session)35 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)31 NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)29 Test (org.junit.Test)25 RepositoryException (javax.jcr.RepositoryException)18 ByteArrayInputStream (java.io.ByteArrayInputStream)15 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)15 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)14 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)11 ArrayList (java.util.ArrayList)10 Workspace (javax.jcr.Workspace)10 InputStream (java.io.InputStream)9 NodeDefinition (javax.jcr.nodetype.NodeDefinition)9 PropertyDefinitionTemplate (javax.jcr.nodetype.PropertyDefinitionTemplate)9 InputStreamReader (java.io.InputStreamReader)8 NamespaceRegistry (javax.jcr.NamespaceRegistry)8 Value (javax.jcr.Value)8