Search in sources :

Example 51 with PropertyIterator

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

the class ReferencesTest method testAlterReference.

/**
     * Tests changing a reference property
     */
public void testAlterReference() throws RepositoryException, NotExecutableException {
    Node n1 = testRootNode.addNode(nodeName1, testNodeType);
    ensureMixinType(n1, mixReferenceable);
    Node n2 = testRootNode.addNode(nodeName2, testNodeType);
    ensureMixinType(n2, mixReferenceable);
    // with some impls. the mixin type has only affect upon save
    testRootNode.getSession().save();
    // make sure the nodes are now referenceable
    assertTrue("test node should be mix:referenceable", n1.isNodeType(mixReferenceable));
    assertTrue("test node should be mix:referenceable", n2.isNodeType(mixReferenceable));
    // create references: n3.p1 -> n1
    Node n3 = testRootNode.addNode(nodeName3, testNodeType);
    // abort test if the repository does not allow setting
    // reference properties on this node
    ensureCanSetProperty(n3, propertyName1, n3.getSession().getValueFactory().createValue(n1));
    n3.setProperty(propertyName1, n1);
    testRootNode.getSession().save();
    assertEquals("Wrong reference target.", n3.getProperty(propertyName1).getNode().getUUID(), n1.getUUID());
    PropertyIterator iter = n1.getReferences();
    if (iter.hasNext()) {
        assertEquals("Wrong referer", iter.nextProperty().getParent().getPath(), n3.getPath());
    } else {
        fail("no referer");
    }
    if (iter.hasNext()) {
        fail("too many referers: " + iter.nextProperty().getPath());
    }
    // change reference: n3.p1 -> n2
    n3.setProperty(propertyName1, n2);
    n3.save();
    assertEquals("Wrong reference target.", n3.getProperty(propertyName1).getNode().getUUID(), n2.getUUID());
    iter = n1.getReferences();
    if (iter.hasNext()) {
        fail("too many referers: " + iter.nextProperty().getPath());
    }
    iter = n2.getReferences();
    if (iter.hasNext()) {
        assertEquals("Wrong referer", iter.nextProperty().getParent().getPath(), n3.getPath());
    } else {
        fail("no referers");
    }
    if (iter.hasNext()) {
        fail("too many referers: " + iter.nextProperty().getPath());
    }
    // clear reference by overwriting by other type
    n3.setProperty(propertyName1, "Hello, world.");
    n3.save();
    iter = n2.getReferences();
    if (iter.hasNext()) {
        fail("too many referers: " + iter.nextProperty().getPath());
    }
}
Also used : Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator)

Example 52 with PropertyIterator

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

the class PropertyUtil method nullValues.

/**
     * Helper method to assure that no property with a null value exist.
     *
     * @param node the node to start the search from.
     * @return <code>true</code> if a null value property is found;
     *         <code>false</code> in the other case.
     */
public static boolean nullValues(Node node) throws RepositoryException {
    boolean nullValue = false;
    for (PropertyIterator props = node.getProperties(); props.hasNext(); ) {
        Property property = props.nextProperty();
        if (!property.getDefinition().isMultiple()) {
            nullValue = (property.getValue() == null);
            if (nullValue) {
                break;
            }
        }
    }
    if (!nullValue) {
        for (NodeIterator nodes = node.getNodes(); nodes.hasNext(); ) {
            Node n = nodes.nextNode();
            nullValue = nullValues(n);
        }
    }
    return nullValue;
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) Property(javax.jcr.Property)

Example 53 with PropertyIterator

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

the class PropertyUtil method searchProp.

/**
     * Traverses a tree below a given node searching for a property with a given
     * type
     *
     * @param node the node to start traverse
     * @param type the property type to search for
     * @param multiple whether the property should be multivalued (<code>null</code>: does not matter)
     * @return the property found or null if no property is found
     */
public static Property searchProp(Session session, Node node, int type, Boolean multiple) throws RepositoryException, ValueFormatException {
    Property prop = null;
    int propType = PropertyType.UNDEFINED;
    if (prop == null) {
        for (PropertyIterator props = node.getProperties(); props.hasNext(); ) {
            Property property = props.nextProperty();
            propType = property.getType();
            if (propType == type && (multiple == null || multiple.booleanValue() == property.getDefinition().isMultiple())) {
                prop = property;
                break;
            }
        }
    }
    if (prop == null) {
        for (NodeIterator nodes = node.getNodes(); nodes.hasNext(); ) {
            Node n = nodes.nextNode();
            prop = searchProp(session, n, type, multiple);
            if (prop != null) {
                break;
            }
        }
    }
    return prop;
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) Property(javax.jcr.Property)

Example 54 with PropertyIterator

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

the class SysViewContentHandler method checkAllProps.

/**
     * Checks the values of all exported properties of a given node.
     *
     * @param nodeElem The nodeElem of the given node.
     * @param skipBinary Boolean if the binary properties should be exported.
     * @throws RepositoryException
     * @throws SAXException
     * @throws IOException
     */
private void checkAllProps(NodeElemData nodeElem, boolean skipBinary) throws RepositoryException, SAXException, IOException {
    boolean allFound = false;
    boolean correctVal = false;
    Node node = nodeElem.node;
    List<PropElemData> propElems = nodeElem.propElems;
    // no props exported
    if (propElems.size() == 0) {
        // if node has properties they should be of Binary type and skipBinary should be true
        if (node.hasProperties()) {
            if (skipBinary) {
                PropertyIterator iter = node.getProperties();
                while (iter.hasNext()) {
                    Property prop = iter.nextProperty();
                    checkCondition("Property " + prop.getName() + " of node " + node.getPath() + " is not exported.", prop.getType() == PropertyType.BINARY);
                }
            } else {
                checkCondition("One or more properties of node " + node.getPath() + " are not exported.", false);
            }
        } else {
        // ok
        }
    } else {
        // compare the propElems with the properties of the given node
        for (int i = 0; i < propElems.size(); i++) {
            correctVal = false;
            PropElemData propElem = propElems.get(i);
            int propType = propElem.type;
            if (node.hasProperty(propElem.name)) {
                Property prop = node.getProperty(propElem.name);
                // compare the propTypes
                correctVal = (propType == prop.getType());
                checkCondition("Property type of property " + propElem.name + " of node " + nodeElem.path + " is not exported correctly." + "expected: " + prop.getType() + " received: " + propType, correctVal);
                // property which should be exported
                if (propType == PropertyType.BINARY && !skipBinary || propType != PropertyType.BINARY) {
                    try {
                        int size = propElem.values.size();
                        // multivalue property with empty value array
                        if (size == 0) {
                            if (prop.getDefinition().isMultiple()) {
                                long length = prop.getValues().length;
                                checkCondition("Value of property " + prop.getPath() + " is not exported.", length == 0);
                            } else {
                                checkCondition("Singler value property " + prop.getPath() + " with no value is exported.", false);
                            }
                        }
                        // single value property or multivalue property with one value
                        if (size == 1) {
                            String str = "";
                            if (prop.getDefinition().isMultiple()) {
                                str = (prop.getValues()[0]).getString();
                            } else {
                                str = prop.getString();
                            }
                            String val = propElem.values.get(0);
                            if (prop.getType() == PropertyType.BINARY) {
                                // decode value
                                val = decodeBase64(val);
                            }
                            correctVal = (str.equals(val));
                            checkCondition("Property value of property " + propElem.name + " of node " + nodeElem.path + " is not exported correctly:" + " expected value: " + str + " found value: " + val, correctVal);
                        } else // multivalue property with several values
                        {
                            Value[] vals = prop.getValues();
                            checkCondition("Number of exported values of property " + prop.getPath() + " does not match the number " + "its values", vals.length == size);
                            for (int j = 0; j < size; j++) {
                                // we know that the order of the values
                                // of a mulitval prop is preserved during export
                                String val = propElem.values.get(j);
                                if (prop.getType() == PropertyType.BINARY) {
                                    // decode value
                                    val = decodeBase64(val);
                                }
                                String str = vals[j].getString();
                                correctVal = (val.equals(str));
                                checkCondition("Property value of property " + propElem.name + " of node " + nodeElem.path + " is not exported correctly.", correctVal);
                            }
                        }
                    } catch (ValueFormatException vfe) {
                        checkCondition("Error during retreiviing the value(s)" + " of property " + prop.getPath() + vfe.toString() + " .", false);
                    }
                } else // skipBinary true and propType is Binary, should be skipped
                {
                    checkCondition("Value of binary property " + prop.getPath() + " exported although skipBinary flag is true.", propElem.values.isEmpty());
                }
            } else // given node has no property with the name given by the prop element
            {
                checkCondition("Property element " + propElem.name + " found but node " + nodeElem.node.getPath() + " does not have a property with that name", false);
            }
        }
        // compare the sizes here
        long otherSize = getSize(node.getProperties());
        allFound = propElems.size() == otherSize;
        checkCondition("Not all properties of node " + nodeElem.path + " are exported.", allFound);
    }
}
Also used : Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) Value(javax.jcr.Value) ValueFormatException(javax.jcr.ValueFormatException) Property(javax.jcr.Property)

Example 55 with PropertyIterator

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

the class TreeComparator method compareProperties.

/**
     * Compares all the properties of the two nodes a and b
     *
     * @param a The node in the source tree.
     * @param b The node in the target tree.
     */
public void compareProperties(Node a, Node b) {
    PropertyIterator ai = null;
    try {
        ai = a.getProperties();
    } catch (RepositoryException e) {
        fail("Cannot access properties: " + e);
    }
    while (ai.hasNext()) {
        Property pa = (Property) ai.next();
        String pName = null;
        // todo
        String pPath = null;
        try {
            pPath = pa.getPath();
        } catch (RepositoryException e) {
        }
        int pType = 0;
        try {
            pName = pa.getName();
            if (pa.getDefinition().isMultiple()) {
                pType = -9999;
            } else {
                pType = pa.getValue().getType();
            }
        } catch (RepositoryException e) {
            fail("Cannot access property information: " + e);
        }
        if (propertyValueMayChange(pName)) {
            continue;
        }
        Property pb = null;
        // avoid skipped properties
        if (!propertySkipped(pName)) {
            try {
                pb = b.getProperty(pName);
            } catch (RepositoryException e) {
                //fail if the property is not there but should
                fail("Property '" + pPath + "' not available: " + e);
            }
            if (!(skipBinary && pType == PropertyType.BINARY)) {
                // todo
                // compare source and target value
                compareProperties(pa, pb);
            }
        }
    }
}
Also used : PropertyIterator(javax.jcr.PropertyIterator) RepositoryException(javax.jcr.RepositoryException) 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