Search in sources :

Example 1 with PropertyIteratorAdapter

use of org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter in project jackrabbit by apache.

the class NodeImpl method getWeakReferences.

/**
     * {@inheritDoc}
     */
public PropertyIterator getWeakReferences(String name) throws RepositoryException {
    if (name == null) {
        return getWeakReferences();
    }
    // check state of this instance
    sanityCheck();
    // shortcut if node isn't referenceable
    if (!isNodeType(NameConstants.MIX_REFERENCEABLE)) {
        return PropertyIteratorAdapter.EMPTY;
    }
    try {
        StringBuilder stmt = new StringBuilder();
        stmt.append("//*[@").append(ISO9075.encode(name));
        stmt.append(" = '").append(data.getId()).append("']");
        Query q = getSession().getWorkspace().getQueryManager().createQuery(stmt.toString(), Query.XPATH);
        QueryResult result = q.execute();
        ArrayList<Property> l = new ArrayList<Property>();
        for (NodeIterator nit = result.getNodes(); nit.hasNext(); ) {
            Node n = nit.nextNode();
            l.add(n.getProperty(name));
        }
        if (l.isEmpty()) {
            return PropertyIteratorAdapter.EMPTY;
        } else {
            return new PropertyIteratorAdapter(l);
        }
    } catch (RepositoryException e) {
        String msg = "Unable to retrieve WEAKREFERENCE properties that refer to " + id;
        log.debug(msg);
        throw new RepositoryException(msg, e);
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) PropertyIteratorAdapter(org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property)

Example 2 with PropertyIteratorAdapter

use of org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter in project jackrabbit by apache.

the class JsonWriterTest method testDoubleOutput.

@Test
public void testDoubleOutput() throws Exception {
    StringWriter writer = new StringWriter();
    JsonWriter jsonWriter = new JsonWriter(writer);
    Node parent = createMock(Node.class);
    Property doubleProperty = createMock(Property.class);
    Value doublePropertyValue = createMock(Value.class);
    expect(doubleProperty.getType()).andReturn(PropertyType.DOUBLE).anyTimes();
    expect(doubleProperty.getName()).andReturn("singleValued").anyTimes();
    expect(doubleProperty.isMultiple()).andReturn(false).anyTimes();
    expect(doubleProperty.getValue()).andReturn(doublePropertyValue).anyTimes();
    expect(doublePropertyValue.getType()).andReturn(PropertyType.DOUBLE).anyTimes();
    expect(doublePropertyValue.getDouble()).andReturn(5d).anyTimes();
    expect(doublePropertyValue.getString()).andReturn("5").anyTimes();
    Property mvDoubleProperty = createMock(Property.class);
    Value mvDoublePropertyValue1 = createMock(Value.class);
    Value mvDoublePropertyValue2 = createMock(Value.class);
    expect(mvDoubleProperty.getType()).andReturn(PropertyType.DOUBLE).anyTimes();
    expect(mvDoubleProperty.getName()).andReturn("multiValued").anyTimes();
    expect(mvDoubleProperty.isMultiple()).andReturn(true).anyTimes();
    expect(mvDoubleProperty.getValues()).andReturn(new Value[] { mvDoublePropertyValue1, mvDoublePropertyValue2 }).anyTimes();
    expect(mvDoublePropertyValue1.getType()).andReturn(PropertyType.DOUBLE).anyTimes();
    expect(mvDoublePropertyValue1.getDouble()).andReturn(42d).anyTimes();
    expect(mvDoublePropertyValue1.getString()).andReturn("42").anyTimes();
    expect(mvDoublePropertyValue2.getType()).andReturn(PropertyType.DOUBLE).anyTimes();
    expect(mvDoublePropertyValue2.getDouble()).andReturn(98.6).anyTimes();
    expect(mvDoublePropertyValue2.getString()).andReturn("98.6").anyTimes();
    final List<Property> properties = new ArrayList<Property>();
    properties.add(doubleProperty);
    properties.add(mvDoubleProperty);
    expect(parent.getProperties()).andAnswer(new IAnswer<PropertyIterator>() {

        @Override
        public PropertyIterator answer() throws Throwable {
            return new PropertyIteratorAdapter(properties.iterator());
        }
    });
    expect(parent.getNodes()).andAnswer(new IAnswer<NodeIterator>() {

        @Override
        public NodeIterator answer() throws Throwable {
            return new NodeIteratorAdapter(Collections.<Node>emptyIterator());
        }
    });
    replayAll();
    jsonWriter.write(parent, 1);
    assertEquals("{\":singleValued\":\"Double\",\"singleValued\":5,\":multiValued\":\"Double\",\"multiValued\":[42,98.6],\"::NodeIteratorSize\":0}", writer.toString());
    verifyAll();
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) PropertyIteratorAdapter(org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter) ArrayList(java.util.ArrayList) PropertyIterator(javax.jcr.PropertyIterator) NodeIteratorAdapter(org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter) StringWriter(java.io.StringWriter) Value(javax.jcr.Value) Property(javax.jcr.Property) Test(org.junit.Test)

Example 3 with PropertyIteratorAdapter

use of org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter in project sling by apache.

the class MockNode method getProperties.

@Override
public PropertyIterator getProperties(final String namePattern) throws RepositoryException {
    final Pattern pattern = Pattern.compile(namePattern);
    RangeIterator items = getMockedSession().listChildren(getPath(), new ItemFilter() {

        @Override
        public boolean accept(final ItemData item) throws RepositoryException {
            return item.isProperty() && pattern.matcher(item.getName()).matches();
        }
    });
    return new PropertyIteratorAdapter(items, items.getSize());
}
Also used : Pattern(java.util.regex.Pattern) RangeIterator(javax.jcr.RangeIterator) PropertyIteratorAdapter(org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter) RepositoryException(javax.jcr.RepositoryException)

Example 4 with PropertyIteratorAdapter

use of org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter in project jackrabbit by apache.

the class NodeImpl method getWeakReferences.

/**
     * {@inheritDoc}
     */
public PropertyIterator getWeakReferences() throws RepositoryException {
    // check state of this instance
    sanityCheck();
    // shortcut if node isn't referenceable
    if (!isNodeType(NameConstants.MIX_REFERENCEABLE)) {
        return PropertyIteratorAdapter.EMPTY;
    }
    Value ref = getSession().getValueFactory().createValue(this, true);
    List<Property> props = new ArrayList<Property>();
    QueryManagerImpl qm = (QueryManagerImpl) getSession().getWorkspace().getQueryManager();
    for (Node n : qm.getWeaklyReferringNodes(this)) {
        for (PropertyIterator it = n.getProperties(); it.hasNext(); ) {
            Property p = it.nextProperty();
            if (p.getType() == PropertyType.WEAKREFERENCE) {
                Collection<Value> refs;
                if (p.isMultiple()) {
                    refs = Arrays.asList(p.getValues());
                } else {
                    refs = Collections.singleton(p.getValue());
                }
                if (refs.contains(ref)) {
                    props.add(p);
                }
            }
        }
    }
    return new PropertyIteratorAdapter(props);
}
Also used : JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) PropertyIteratorAdapter(org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter) Value(javax.jcr.Value) InternalValue(org.apache.jackrabbit.core.value.InternalValue) ArrayList(java.util.ArrayList) PropertyIterator(javax.jcr.PropertyIterator) Property(javax.jcr.Property) QueryManagerImpl(org.apache.jackrabbit.core.query.QueryManagerImpl)

Aggregations

PropertyIteratorAdapter (org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter)4 ArrayList (java.util.ArrayList)3 Node (javax.jcr.Node)3 Property (javax.jcr.Property)3 NodeIterator (javax.jcr.NodeIterator)2 PropertyIterator (javax.jcr.PropertyIterator)2 RepositoryException (javax.jcr.RepositoryException)2 Value (javax.jcr.Value)2 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)2 StringWriter (java.io.StringWriter)1 Pattern (java.util.regex.Pattern)1 RangeIterator (javax.jcr.RangeIterator)1 Query (javax.jcr.query.Query)1 QueryResult (javax.jcr.query.QueryResult)1 NodeIteratorAdapter (org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter)1 QueryManagerImpl (org.apache.jackrabbit.core.query.QueryManagerImpl)1 InternalValue (org.apache.jackrabbit.core.value.InternalValue)1 Test (org.junit.Test)1