Search in sources :

Example 6 with PropertyDefinitionTemplate

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

the class PropertyTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    NodeTypeManager ntMgr = superuser.getWorkspace().getNodeTypeManager();
    NodeTypeTemplate template = ntMgr.createNodeTypeTemplate();
    template.setName(NT_NAME);
    template.setDeclaredSuperTypeNames(new String[] { JcrConstants.NT_BASE });
    template.setMixin(false);
    PropertyDefinitionTemplate pdt = ntMgr.createPropertyDefinitionTemplate();
    pdt.setName(BOOLEAN_PROP_NAME);
    pdt.setRequiredType(PropertyType.BOOLEAN);
    template.getPropertyDefinitionTemplates().add(pdt);
    pdt = ntMgr.createPropertyDefinitionTemplate();
    pdt.setName(LONG_PROP_NAME);
    pdt.setRequiredType(PropertyType.LONG);
    template.getPropertyDefinitionTemplates().add(pdt);
    ntMgr.registerNodeType(template, true);
    node = testRootNode.addNode(nodeName2, NT_NAME);
    assertEquals(NT_NAME, node.getPrimaryNodeType().getName());
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate)

Example 7 with PropertyDefinitionTemplate

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

the class RepositoryUpgradeTest method createSourceContent.

@Override
@SuppressWarnings("unchecked")
protected void createSourceContent(Session session) throws Exception {
    JackrabbitWorkspace workspace = (JackrabbitWorkspace) session.getWorkspace();
    NamespaceRegistry registry = workspace.getNamespaceRegistry();
    registry.registerNamespace("test", "http://www.example.org/");
    NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
    NodeTypeTemplate template = nodeTypeManager.createNodeTypeTemplate();
    template.setName("test:unstructured");
    template.setDeclaredSuperTypeNames(new String[] { "nt:unstructured" });
    PropertyDefinitionTemplate pDef1 = nodeTypeManager.createPropertyDefinitionTemplate();
    pDef1.setName("defaultString");
    pDef1.setRequiredType(PropertyType.STRING);
    Value stringValue = session.getValueFactory().createValue("stringValue");
    pDef1.setDefaultValues(new Value[] { stringValue });
    template.getPropertyDefinitionTemplates().add(pDef1);
    PropertyDefinitionTemplate pDef2 = nodeTypeManager.createPropertyDefinitionTemplate();
    pDef2.setName("defaultPath");
    pDef2.setRequiredType(PropertyType.PATH);
    Value pathValue = session.getValueFactory().createValue("/jcr:path/nt:value", PropertyType.PATH);
    pDef2.setDefaultValues(new Value[] { pathValue });
    template.getPropertyDefinitionTemplates().add(pDef2);
    nodeTypeManager.registerNodeType(template, false);
    template = nodeTypeManager.createNodeTypeTemplate();
    template.setName("test:referenceable");
    template.setDeclaredSuperTypeNames(new String[] { "nt:unstructured", "mix:referenceable" });
    nodeTypeManager.registerNodeType(template, false);
    Node root = session.getRootNode();
    Node referenceable = root.addNode("referenceable", "test:unstructured");
    referenceable.addMixin(NodeType.MIX_REFERENCEABLE);
    Node versionable = root.addNode("versionable", NT_UNSTRUCTURED);
    versionable.addMixin(MIX_VERSIONABLE);
    Node child = versionable.addNode("child", "test:referenceable");
    child.addNode("child2", NT_UNSTRUCTURED);
    session.save();
    Node sns = root.addNode("sns");
    sns.addNode("sibling");
    sns.addNode("sibling");
    sns.addNode("sibling");
    session.getWorkspace().getVersionManager().checkin("/versionable");
    Node properties = root.addNode("properties", "test:unstructured");
    properties.setProperty("boolean", true);
    Binary binary = session.getValueFactory().createBinary(new ByteArrayInputStream(BINARY));
    try {
        properties.setProperty("binary", binary);
    } finally {
        binary.dispose();
    }
    properties.setProperty("date", DATE);
    properties.setProperty("decimal", new BigDecimal(123));
    properties.setProperty("double", Math.PI);
    properties.setProperty("long", 9876543210L);
    properties.setProperty("reference", referenceable);
    properties.setProperty("weak_reference", session.getValueFactory().createValue(referenceable, true));
    properties.setProperty("mv_reference", new Value[] { session.getValueFactory().createValue(versionable, false) });
    properties.setProperty("mv_weak_reference", new Value[] { session.getValueFactory().createValue(versionable, true) });
    properties.setProperty("string", "test");
    properties.setProperty("multiple", "a,b,c".split(","));
    session.save();
    binary = properties.getProperty("binary").getBinary();
    try {
        InputStream stream = binary.getStream();
        try {
            for (byte aBINARY : BINARY) {
                assertEquals(aBINARY, (byte) stream.read());
            }
            assertEquals(-1, stream.read());
        } finally {
            stream.close();
        }
    } finally {
        binary.dispose();
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NamespaceRegistry(javax.jcr.NamespaceRegistry) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Node(javax.jcr.Node) Value(javax.jcr.Value) JackrabbitWorkspace(org.apache.jackrabbit.api.JackrabbitWorkspace) Binary(javax.jcr.Binary) BigDecimal(java.math.BigDecimal)

Example 8 with PropertyDefinitionTemplate

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

the class OpvIgnoreTest method createNodeTypeWithIgnoreOPVProperty.

private NodeTypeTemplate createNodeTypeWithIgnoreOPVProperty(String propertyName) throws RepositoryException {
    NodeTypeManager manager = superuser.getWorkspace().getNodeTypeManager();
    NodeTypeTemplate nt = manager.createNodeTypeTemplate();
    nt.setName("testType");
    nt.setMixin(true);
    PropertyDefinitionTemplate opt = manager.createPropertyDefinitionTemplate();
    opt.setMandatory(false);
    opt.setName(propertyName);
    opt.setRequiredType(PropertyType.STRING);
    opt.setOnParentVersion(OnParentVersionAction.IGNORE);
    List pdt = nt.getPropertyDefinitionTemplates();
    pdt.add(opt);
    manager.registerNodeType(nt, true);
    return nt;
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) JackrabbitAccessControlList(org.apache.jackrabbit.api.security.JackrabbitAccessControlList) List(java.util.List)

Example 9 with PropertyDefinitionTemplate

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

the class NodeTypeTest method updateNodeType.

@Test
public void updateNodeType() throws Exception {
    Session session = getAdminSession();
    Node root = session.getRootNode();
    ValueFactory vf = session.getValueFactory();
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    Node n = root.addNode("q1", "nt:query");
    n.setProperty("jcr:statement", vf.createValue("statement"));
    session.save();
    NodeTypeDefinition ntd = manager.getNodeType("nt:query");
    NodeTypeTemplate ntt = manager.createNodeTypeTemplate(ntd);
    try {
        manager.registerNodeType(ntt, true);
    // no changes to the type, so the registration should be a no-op
    } catch (ConstraintViolationException unexpected) {
        fail();
    }
    // make the (still missing) jcr:language property mandatory
    @SuppressWarnings("unchecked") List<PropertyDefinitionTemplate> pdts = ntt.getPropertyDefinitionTemplates();
    for (PropertyDefinitionTemplate pdt : pdts) {
        if ("jcr:language".equals(pdt.getName())) {
            pdt.setMandatory(true);
        }
    }
    try {
        manager.registerNodeType(ntt, true);
        fail();
    } catch (ConstraintViolationException expected) {
    // the registration fails because of the would-be invalid content
    }
    // add the jcr:language property so it can be made mandatory
    n.setProperty("jcr:language", vf.createValue("language"));
    session.save();
    try {
        manager.registerNodeType(ntt, true);
    // now the mandatory property exists, so the type change is OK
    } catch (ConstraintViolationException unexpected) {
        fail();
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) Node(javax.jcr.Node) NodeTypeDefinition(javax.jcr.nodetype.NodeTypeDefinition) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) ValueFactory(javax.jcr.ValueFactory) Session(javax.jcr.Session) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 10 with PropertyDefinitionTemplate

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

Aggregations

PropertyDefinitionTemplate (javax.jcr.nodetype.PropertyDefinitionTemplate)21 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)13 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)8 List (java.util.List)5 Node (javax.jcr.Node)5 Session (javax.jcr.Session)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)4 NodeDefinitionTemplate (javax.jcr.nodetype.NodeDefinitionTemplate)4 Test (org.junit.Test)4 Value (javax.jcr.Value)3 NodeTypeDefinition (javax.jcr.nodetype.NodeTypeDefinition)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2 BigDecimal (java.math.BigDecimal)2 Binary (javax.jcr.Binary)2 NamespaceRegistry (javax.jcr.NamespaceRegistry)2 NodeTypeExistsException (javax.jcr.nodetype.NodeTypeExistsException)2 JackrabbitWorkspace (org.apache.jackrabbit.api.JackrabbitWorkspace)2 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)2