Search in sources :

Example 66 with PropertyDefinition

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

the class PropertyTest method testMixTitleOnUnstructured2.

public void testMixTitleOnUnstructured2() throws Exception {
    Node n = testRootNode.addNode("unstructured", JcrConstants.NT_UNSTRUCTURED);
    n.addMixin("mix:title");
    superuser.save();
    // create jcr:title as STRING => declaring NT is mix:title
    Property title = n.setProperty("jcr:title", "str");
    assertEquals(PropertyType.STRING, title.getType());
    PropertyDefinition def = title.getDefinition();
    assertEquals("mix:title", def.getDeclaringNodeType().getName());
    assertEquals(PropertyType.STRING, def.getRequiredType());
    // changing value to BOOLEAN => value is converted
    title.setValue(true);
    assertEquals(PropertyType.STRING, title.getType());
    def = title.getDefinition();
    assertEquals("mix:title", def.getDeclaringNodeType().getName());
    assertEquals(PropertyType.STRING, def.getRequiredType());
    // re-setting property to type BOOLEAN => declaring NT is nt:unstructured
    title = n.setProperty("jcr:title", true);
    def = title.getDefinition();
    assertEquals(JcrConstants.NT_UNSTRUCTURED, def.getDeclaringNodeType().getName());
    assertEquals(PropertyType.UNDEFINED, def.getRequiredType());
    // same if property is set to type DOUBLE
    title = n.setProperty("jcr:title", superuser.getValueFactory().createValue(2.3));
    assertEquals(PropertyType.DOUBLE, title.getType());
    def = title.getDefinition();
    assertEquals(JcrConstants.NT_UNSTRUCTURED, def.getDeclaringNodeType().getName());
    assertEquals(PropertyType.UNDEFINED, def.getRequiredType());
    // setting property to STRING => declaring NT is back to mix:title
    title = n.setProperty("jcr:title", "str");
    assertEquals(PropertyType.STRING, title.getType());
    def = title.getDefinition();
    assertEquals("mix:title", def.getDeclaringNodeType().getName());
    assertEquals(PropertyType.STRING, def.getRequiredType());
}
Also used : Node(javax.jcr.Node) Property(javax.jcr.Property) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 67 with PropertyDefinition

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

the class NodeDefinitionTest method getAggregatedPropertyDefinitionss.

public static PropertyDefinition[] getAggregatedPropertyDefinitionss(Node node) throws RepositoryException {
    Set<PropertyDefinition> pDefs = newHashSet();
    PropertyDefinition[] pd = node.getPrimaryNodeType().getPropertyDefinitions();
    pDefs.addAll(Arrays.asList(pd));
    NodeType[] mixins = node.getMixinNodeTypes();
    for (NodeType mixin : mixins) {
        pd = mixin.getPropertyDefinitions();
        pDefs.addAll(Arrays.asList(pd));
    }
    return pDefs.toArray(new PropertyDefinition[pDefs.size()]);
}
Also used : NodeType(javax.jcr.nodetype.NodeType) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 68 with PropertyDefinition

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

the class NodeTypeDefinitionTest method testIndexedPropertyDefinition.

public void testIndexedPropertyDefinition() throws Exception {
    String ntPath = NodeTypeConstants.NODE_TYPES_PATH + '/' + NodeTypeConstants.NT_VERSION;
    assertTrue(superuser.nodeExists(ntPath + "/jcr:propertyDefinition"));
    assertTrue(superuser.nodeExists(ntPath + "/jcr:propertyDefinition[1]"));
    Node pdNode = superuser.getNode(ntPath + "/jcr:propertyDefinition[1]");
    assertEquals(ntPath + "/jcr:propertyDefinition", pdNode.getPath());
    List<String> defNames = new ArrayList();
    NodeType nt = superuser.getWorkspace().getNodeTypeManager().getNodeType(NodeTypeConstants.NT_VERSION);
    for (PropertyDefinition nd : nt.getDeclaredPropertyDefinitions()) {
        defNames.add(nd.getName());
    }
    Node ntNode = superuser.getNode(ntPath);
    NodeIterator it = ntNode.getNodes("jcr:propertyDefinition*");
    while (it.hasNext()) {
        Node def = it.nextNode();
        int index = getIndex(def);
        String name = (def.hasProperty(NodeTypeConstants.JCR_NAME)) ? def.getProperty(NodeTypeConstants.JCR_NAME).getString() : NodeTypeConstants.RESIDUAL_NAME;
        assertEquals(name, defNames.get(index - 1));
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) NodeType(javax.jcr.nodetype.NodeType) ArrayList(java.util.ArrayList) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 69 with PropertyDefinition

use of javax.jcr.nodetype.PropertyDefinition 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 70 with PropertyDefinition

use of javax.jcr.nodetype.PropertyDefinition 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)

Aggregations

PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)141 NodeType (javax.jcr.nodetype.NodeType)79 Value (javax.jcr.Value)70 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)61 Node (javax.jcr.Node)27 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)22 Property (javax.jcr.Property)20 NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)15 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)14 RepositoryException (javax.jcr.RepositoryException)13 NodeDefinition (javax.jcr.nodetype.NodeDefinition)13 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)11 Test (org.junit.Test)11 InputStream (java.io.InputStream)5 Session (javax.jcr.Session)5 ValueFormatException (javax.jcr.ValueFormatException)5 PropertyDefinitionImpl (org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 Name (org.apache.jackrabbit.spi.Name)4