Search in sources :

Example 31 with ValueFactory

use of javax.jcr.ValueFactory 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 32 with ValueFactory

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

the class L6_AccessControlContentTest method testRestrictionContent.

public void testRestrictionContent() throws RepositoryException {
    ValueFactory vf = superuser.getValueFactory();
    acl.addEntry(testPrincipal, testPrivileges, false, ImmutableMap.of(AccessControlConstants.REP_GLOB, vf.createValue("")), ImmutableMap.of(AccessControlConstants.REP_PREFIXES, new Value[] { vf.createValue("jcr"), vf.createValue("mix") }));
    acMgr.setPolicy(testRoot, acl);
    // EXERCISE
    String policyPath = null;
    Node aclNode = superuser.getNode(policyPath);
    Node ace = aclNode.getNodes().nextNode();
// EXERCISE: retrieve the restrictions defined for the single ACE node
// EXERCISE: verify the expected properties and their value(s)
}
Also used : Node(javax.jcr.Node) Value(javax.jcr.Value) ValueFactory(javax.jcr.ValueFactory)

Example 33 with ValueFactory

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

the class PropertyImpl method setValue.

@Override
public void setValue(String[] strings) throws RepositoryException {
    if (strings == null) {
        remove();
    } else {
        ValueFactory factory = getValueFactory();
        Value[] values = new Value[strings.length];
        for (int i = 0; i < strings.length; i++) {
            if (strings[i] != null) {
                values[i] = factory.createValue(strings[i]);
            }
        }
        internalSetValue(values);
    }
}
Also used : Value(javax.jcr.Value) ValueFactory(javax.jcr.ValueFactory)

Example 34 with ValueFactory

use of javax.jcr.ValueFactory in project sling by apache.

the class JcrModifiableValueMapTest method createValue.

Value createValue(final Object value, final Session session) throws RepositoryException {
    Value val;
    ValueFactory fac = session.getValueFactory();
    if (value instanceof Calendar) {
        val = fac.createValue((Calendar) value);
    } else if (value instanceof InputStream) {
        val = fac.createValue(fac.createBinary((InputStream) value));
    } else if (value instanceof Node) {
        val = fac.createValue((Node) value);
    } else if (value instanceof BigDecimal) {
        val = fac.createValue((BigDecimal) value);
    } else if (value instanceof Long) {
        val = fac.createValue((Long) value);
    } else if (value instanceof Short) {
        val = fac.createValue((Short) value);
    } else if (value instanceof Integer) {
        val = fac.createValue((Integer) value);
    } else if (value instanceof Number) {
        val = fac.createValue(((Number) value).doubleValue());
    } else if (value instanceof Boolean) {
        val = fac.createValue((Boolean) value);
    } else if (value instanceof String) {
        val = fac.createValue((String) value);
    } else {
        val = null;
    }
    return val;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AssertCalendar.assertEqualsCalendar(org.apache.sling.jcr.resource.internal.AssertCalendar.assertEqualsCalendar) Calendar(java.util.Calendar) Node(javax.jcr.Node) Value(javax.jcr.Value) ValueFactory(javax.jcr.ValueFactory) BigDecimal(java.math.BigDecimal)

Example 35 with ValueFactory

use of javax.jcr.ValueFactory in project sling by apache.

the class DefaultContentCreator method createProperty.

/**
     * @see org.apache.sling.jcr.contentloader.ContentCreator#createProperty(java.lang.String, int, java.lang.String[])
     */
public void createProperty(String name, int propertyType, String[] values) throws RepositoryException {
    final Node node = this.parentNodeStack.peek();
    // check if the property already exists and isPropertyOverwrite() is false, don't overwrite it in this case
    if (node.hasProperty(name) && !this.configuration.isPropertyOverwrite() && !node.getProperty(name).isNew()) {
        return;
    }
    if (propertyType == PropertyType.REFERENCE) {
        String propPath = node.getPath() + "/" + name;
        boolean hasAll = true;
        String[] uuids = new String[values.length];
        String[] uuidOrPaths = new String[values.length];
        for (int i = 0; i < values.length; i++) {
            uuids[i] = getUUID(node.getSession(), propPath, getAbsPath(node, values[i]));
            uuidOrPaths[i] = uuids[i] != null ? uuids[i] : getAbsPath(node, values[i]);
            if (uuids[i] == null)
                hasAll = false;
        }
        checkoutIfNecessary(node);
        node.setProperty(name, uuids, propertyType);
        if (this.importListener != null) {
            this.importListener.onCreate(node.getProperty(name).getPath());
        }
        if (!hasAll) {
            delayedMultipleReferences.put(propPath, uuidOrPaths);
        }
    } else if (propertyType == PropertyType.DATE) {
        checkoutIfNecessary(node);
        try {
            // This modification is to remove the colon in the JSON Timezone
            ValueFactory valueFactory = node.getSession().getValueFactory();
            Value[] jcrValues = new Value[values.length];
            for (int i = 0; i < values.length; i++) {
                jcrValues[i] = valueFactory.createValue(parseDateString(values[i]));
            }
            node.setProperty(name, jcrValues, propertyType);
        } catch (ParseException e) {
            // If this fails, fallback to the default
            log.warn("Could not create dates for property, falling back to defaults", e);
            node.setProperty(name, values, propertyType);
        }
        if (this.importListener != null) {
            this.importListener.onCreate(node.getProperty(name).getPath());
        }
    } else {
        checkoutIfNecessary(node);
        if (propertyType == PropertyType.UNDEFINED) {
            node.setProperty(name, values);
        } else {
            node.setProperty(name, values, propertyType);
        }
        if (this.importListener != null) {
            this.importListener.onCreate(node.getProperty(name).getPath());
        }
    }
}
Also used : Node(javax.jcr.Node) ValueFactory(javax.jcr.ValueFactory) ParseException(java.text.ParseException)

Aggregations

ValueFactory (javax.jcr.ValueFactory)105 Value (javax.jcr.Value)51 Node (javax.jcr.Node)50 Session (javax.jcr.Session)40 Test (org.junit.Test)17 RepositoryException (javax.jcr.RepositoryException)16 InputStream (java.io.InputStream)13 AccessControlManager (javax.jcr.security.AccessControlManager)13 HashMap (java.util.HashMap)12 Privilege (javax.jcr.security.Privilege)12 Property (javax.jcr.Property)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 Query (javax.jcr.query.Query)8 Calendar (java.util.Calendar)7 QueryManager (javax.jcr.query.QueryManager)7 RowIterator (javax.jcr.query.RowIterator)7 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)7 NodeIterator (javax.jcr.NodeIterator)6 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)6 IOException (java.io.IOException)5