Search in sources :

Example 46 with PropertyIterator

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

the class JcrCommand method nodeToResource.

protected ResourceProxy nodeToResource(Node node) throws RepositoryException {
    ResourceProxy resource = new ResourceProxy(node.getPath());
    resource.addAdapted(Node.class, node);
    PropertyIterator properties = node.getProperties();
    while (properties.hasNext()) {
        Property property = properties.nextProperty();
        String propertyName = property.getName();
        Object propertyValue = ConversionUtils.getPropertyValue(property);
        if (propertyValue != null) {
            resource.addProperty(propertyName, propertyValue);
        }
    }
    return resource;
}
Also used : PropertyIterator(javax.jcr.PropertyIterator) Property(javax.jcr.Property) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy)

Example 47 with PropertyIterator

use of javax.jcr.PropertyIterator in project jackrabbit by apache.

the class AuthorizableImplTest method testGroupGetProperties.

public void testGroupGetProperties() throws RepositoryException, NotExecutableException {
    AuthorizableImpl group = (AuthorizableImpl) getTestGroup(superuser);
    NodeImpl n = group.getNode();
    for (PropertyIterator it = n.getProperties(); it.hasNext(); ) {
        PropertyImpl p = (PropertyImpl) it.nextProperty();
        if (p.getDefinition().isProtected()) {
            assertFalse(group.hasProperty(p.getName()));
            assertNull(group.getProperty(p.getName()));
        } else {
            // authorizable defined property
            assertTrue(group.hasProperty(p.getName()));
            assertNotNull(group.getProperty(p.getName()));
        }
    }
}
Also used : NodeImpl(org.apache.jackrabbit.core.NodeImpl) PropertyIterator(javax.jcr.PropertyIterator) PropertyImpl(org.apache.jackrabbit.core.PropertyImpl)

Example 48 with PropertyIterator

use of javax.jcr.PropertyIterator in project jackrabbit by apache.

the class VersionResourceImpl method initProperties.

/**
     * Fill the property set for this resource.
     */
@Override
protected void initProperties() {
    if (!propsInitialized) {
        super.initProperties();
        Version v = (Version) getNode();
        try {
            String creationDate = HttpDateFormat.creationDateFormat().format(v.getCreated().getTime());
            // replace dummy creation date from default collection
            properties.add(new DefaultDavProperty<String>(DavPropertyName.CREATIONDATE, creationDate));
            // required, protected DAV:version-name property
            properties.add(new DefaultDavProperty<String>(VERSION_NAME, v.getName(), true));
            // required, protected DAV:label-name-set property
            String[] labels = getVersionHistoryItem().getVersionLabels(v);
            properties.add(new LabelSetProperty(labels));
            // required DAV:predecessor-set (protected) and DAV:successor-set (computed) properties
            properties.add(getHrefProperty(VersionResource.PREDECESSOR_SET, v.getPredecessors(), true, false));
            properties.add(getHrefProperty(SUCCESSOR_SET, v.getSuccessors(), true, false));
            // required DAV:version-history (computed) property
            String vhHref = getLocatorFromNode(getVersionHistoryItem()).getHref(true);
            properties.add(new HrefProperty(VersionResource.VERSION_HISTORY, vhHref, true));
            // required DAV:checkout-set (computed) property
            PropertyIterator it = v.getReferences();
            List<Node> nodeList = new ArrayList<Node>();
            while (it.hasNext()) {
                Property p = it.nextProperty();
                if (JcrConstants.JCR_BASEVERSION.equals(p.getName())) {
                    Node n = p.getParent();
                    if (n.isCheckedOut()) {
                        nodeList.add(n);
                    }
                }
            }
            properties.add(getHrefProperty(CHECKOUT_SET, nodeList.toArray(new Node[nodeList.size()]), true, false));
        } catch (RepositoryException e) {
            log.error(e.getMessage());
        }
    }
}
Also used : HrefProperty(org.apache.jackrabbit.webdav.property.HrefProperty) Version(javax.jcr.version.Version) LabelSetProperty(org.apache.jackrabbit.webdav.version.LabelSetProperty) Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property) DefaultDavProperty(org.apache.jackrabbit.webdav.property.DefaultDavProperty) HrefProperty(org.apache.jackrabbit.webdav.property.HrefProperty) LabelSetProperty(org.apache.jackrabbit.webdav.version.LabelSetProperty) DavProperty(org.apache.jackrabbit.webdav.property.DavProperty)

Example 49 with PropertyIterator

use of javax.jcr.PropertyIterator in project jackrabbit by apache.

the class ReferencesTest method testGetReferencesWithName.

/**
     * Tests Node.getReferences(String)
     */
public void testGetReferencesWithName() throws RepositoryException, NotExecutableException {
    Node n1 = testRootNode.addNode(nodeName1, testNodeType);
    ensureMixinType(n1, mixReferenceable);
    // with some impls. the mixin type has only affect upon save
    testRootNode.getSession().save();
    // make sure the node is now referenceable
    assertTrue("test node should be mix:referenceable", n1.isNodeType(mixReferenceable));
    // create references:
    // n2.p1 -> n1
    // n2.p2 -> n1
    // n3.p1 -> n1
    Node n2 = testRootNode.addNode(nodeName2, testNodeType);
    Node n3 = testRootNode.addNode(nodeName3, testNodeType);
    Value[] values = new Value[] { superuser.getValueFactory().createValue(n1) };
    // abort test if the repository does not allow setting
    // reference properties on this node
    ensureCanSetProperty(n2, propertyName1, values);
    ensureCanSetProperty(n2, propertyName2, values);
    ensureCanSetProperty(n3, propertyName1, values);
    Property p1 = n2.setProperty(propertyName1, values);
    Property p2 = n2.setProperty(propertyName2, values);
    Property p3 = n3.setProperty(propertyName1, n1);
    testRootNode.getSession().save();
    // get references with name propertyName1
    // (should return p1 and p3))
    PropertyIterator iter = n1.getReferences(propertyName1);
    Set<String> results = new HashSet<String>();
    while (iter.hasNext()) {
        results.add(iter.nextProperty().getPath());
    }
    assertEquals("wrong number of references reported", 2, results.size());
    assertTrue("missing reference property: " + p1.getPath(), results.contains(p1.getPath()));
    assertTrue("missing reference property: " + p3.getPath(), results.contains(p3.getPath()));
    // get references with name propertyName2
    // (should return p2))
    iter = n1.getReferences(propertyName2);
    results.clear();
    while (iter.hasNext()) {
        results.add(iter.nextProperty().getPath());
    }
    assertEquals("wrong number of references reported", 1, results.size());
    assertTrue("missing reference property: " + p2.getPath(), results.contains(p2.getPath()));
    // remove reference n3.p1 -> n1
    testRootNode.getNode(nodeName3).getProperty(propertyName1).remove();
    testRootNode.getSession().save();
    // get references with name propertyName1
    // (should return p1))
    iter = n1.getReferences(propertyName1);
    results.clear();
    while (iter.hasNext()) {
        results.add(iter.nextProperty().getPath());
    }
    assertEquals("wrong number of references reported", 1, results.size());
    assertTrue("missing reference property: " + p1.getPath(), results.contains(p1.getPath()));
    // remove reference n2.p1 -> n1
    p1.remove();
    testRootNode.getSession().save();
    // get references with name propertyName1
    // (should nothing))
    iter = n1.getReferences(propertyName1);
    results.clear();
    while (iter.hasNext()) {
        results.add(iter.nextProperty().getPath());
    }
    assertEquals("wrong number of references reported", 0, results.size());
}
Also used : Node(javax.jcr.Node) Value(javax.jcr.Value) PropertyIterator(javax.jcr.PropertyIterator) Property(javax.jcr.Property) HashSet(java.util.HashSet)

Example 50 with PropertyIterator

use of javax.jcr.PropertyIterator in project jackrabbit by apache.

the class ReferencesTest method testReferences.

/**
     * Tests Node.getReferences()
     */
public void testReferences() throws RepositoryException, NotExecutableException {
    Node n1 = testRootNode.addNode(nodeName1, testNodeType);
    ensureMixinType(n1, mixReferenceable);
    // with some impls. the mixin type has only affect upon save
    testRootNode.getSession().save();
    // make sure the node is now referenceable
    assertTrue("test node should be mix:referenceable", n1.isNodeType(mixReferenceable));
    // create references: n2.p1 -> n1
    Node n2 = testRootNode.addNode(nodeName2, testNodeType);
    Value[] values = new Value[] { superuser.getValueFactory().createValue(n1) };
    // abort test if the repository does not allow setting
    // reference properties on this node
    ensureCanSetProperty(n2, propertyName1, values);
    Property p1 = n2.setProperty(propertyName1, values);
    testRootNode.getSession().save();
    PropertyIterator iter = n1.getReferences();
    if (iter.hasNext()) {
        assertEquals("Wrong referer", iter.nextProperty().getPath(), p1.getPath());
    } else {
        fail("no referer");
    }
    // create references: n3.p1 -> n1
    Node n3 = testRootNode.addNode(nodeName3, testNodeType);
    n3.setProperty(propertyName1, n1);
    testRootNode.getSession().save();
    iter = n1.getReferences();
    while (iter.hasNext()) {
        Property p = iter.nextProperty();
        if (n2 != null && p.getParent().getPath().equals(n2.getPath())) {
            n2 = null;
        } else if (n3 != null && p.getParent().getPath().equals(n3.getPath())) {
            n3 = null;
        } else {
            fail("too many referers: " + p.getPath());
        }
    }
    if (n2 != null) {
        fail("referer not in references set: " + n2.getPath());
    }
    if (n3 != null) {
        fail("referer not in references set: " + n3.getPath());
    }
    // remove reference n3.p1 -> n1
    testRootNode.getNode(nodeName3).getProperty(propertyName1).remove();
    testRootNode.getSession().save();
    iter = n1.getReferences();
    if (iter.hasNext()) {
        assertEquals("Wrong referer", iter.nextProperty().getParent().getPath(), testRootNode.getNode(nodeName2).getPath());
    } else {
        fail("no referer");
    }
    if (iter.hasNext()) {
        fail("too many referers: " + iter.nextProperty().getPath());
    }
    // remove reference n2.p1 -> n1
    testRootNode.getNode(nodeName2).getProperty(propertyName1).setValue(new Value[0]);
    testRootNode.getSession().save();
    iter = n1.getReferences();
    if (iter.hasNext()) {
        fail("too many referers: " + iter.nextProperty().getPath());
    }
}
Also used : Node(javax.jcr.Node) Value(javax.jcr.Value) PropertyIterator(javax.jcr.PropertyIterator) Property(javax.jcr.Property)

Aggregations

PropertyIterator (javax.jcr.PropertyIterator)96 Property (javax.jcr.Property)68 Node (javax.jcr.Node)57 NodeIterator (javax.jcr.NodeIterator)28 Value (javax.jcr.Value)23 Test (org.junit.Test)16 RepositoryException (javax.jcr.RepositoryException)15 Session (javax.jcr.Session)15 ArrayList (java.util.ArrayList)14 HashSet (java.util.HashSet)10 PathNotFoundException (javax.jcr.PathNotFoundException)8 HashMap (java.util.HashMap)5 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)5 NodeImpl (org.apache.jackrabbit.core.NodeImpl)5 PropertyImpl (org.apache.jackrabbit.core.PropertyImpl)5 NoSuchElementException (java.util.NoSuchElementException)3 AccessDeniedException (javax.jcr.AccessDeniedException)3 InvalidItemStateException (javax.jcr.InvalidItemStateException)3 ValueFormatException (javax.jcr.ValueFormatException)3 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)3