Search in sources :

Example 26 with Property

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

the class MandatoryItemTest method testRemoval.

public void testRemoval() throws NotExecutableException, RepositoryException {
    Node n;
    Node childN = null;
    Property childP = null;
    try {
        n = testRootNode.addNode(nodeName1, testNodeType);
        if (childNodeDef != null) {
            childN = n.addNode(childNodeDef.getName(), childNodeDef.getDefaultPrimaryType().getName());
        }
        if (childPropDef != null) {
            // TODO: check if definition defines default values
            childP = n.setProperty(childPropDef.getName(), "any value");
        }
        testRootNode.save();
    } catch (RepositoryException e) {
        throw new NotExecutableException();
    }
    // remove the mandatory items ((must succeed))
    if (childN != null) {
        childN.remove();
    }
    if (childP != null) {
        childP.remove();
    }
    // ... however, saving must not be allowed.
    try {
        testRootNode.save();
        fail("removing mandatory child items without re-adding them must fail.");
    } catch (ConstraintViolationException e) {
    // success.
    }
    // re-add the mandatory items
    if (childNodeDef != null) {
        childN = n.addNode(childNodeDef.getName(), childNodeDef.getDefaultPrimaryType().getName());
    }
    if (childPropDef != null) {
        // TODO: check if definition defines default values
        childP = n.setProperty(childPropDef.getName(), "any value");
    }
    // save must succeed now.
    testRootNode.save();
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Node(javax.jcr.Node) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property)

Example 27 with Property

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

the class ClearWorkspace method execute.

/**
     * {@inheritDoc}
     */
public boolean execute(Context ctx) throws Exception {
    Session s = CommandHelper.getSession(ctx);
    if (log.isDebugEnabled()) {
        log.debug("removing all content from workspace " + s.getWorkspace().getName());
    }
    // Set current node to root
    CommandHelper.setCurrentNode(ctx, s.getRootNode());
    NodeIterator iter = s.getRootNode().getNodes();
    while (iter.hasNext()) {
        Node n = (Node) iter.next();
        if (!n.getName().equals(JcrConstants.JCR_SYSTEM)) {
            n.remove();
        }
    }
    PropertyIterator pIter = s.getRootNode().getProperties();
    while (pIter.hasNext()) {
        Property p = pIter.nextProperty();
        if (!p.getName().equals(JcrConstants.JCR_PRIMARYTYPE)) {
            p.remove();
        }
    }
    return false;
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) Property(javax.jcr.Property) Session(javax.jcr.Session)

Example 28 with Property

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

the class AbstractLsItems method execute.

/**
     * {@inheritDoc}
     */
public final boolean execute(Context ctx) throws Exception {
    int nodes = 0;
    int properties = 0;
    // header
    int[] width = new int[] { nameWidth, typeWidth, longWidth, longWidth, longWidth };
    String[] header = new String[] { bundle.getString("word.name"), bundle.getString("word.type"), bundle.getString("word.node"), bundle.getString("word.new"), bundle.getString("word.modified") };
    // print header
    PrintHelper.printRow(ctx, width, header);
    // print separator
    PrintHelper.printSeparatorRow(ctx, width, '-');
    // nodes
    Iterator iter = getItems(ctx);
    int index = 0;
    int maxItems = getMaxItems(ctx);
    // Print nodes
    while (iter.hasNext() && index < maxItems) {
        Item i = (Item) iter.next();
        String type = null;
        // Show name or path
        String name = null;
        if (this.isPath()) {
            name = i.getPath();
        } else {
            name = i.getName();
        }
        if (i.isNode()) {
            nodes++;
            // name
            Node n = (Node) i;
            if (!isPath() && n.getIndex() > 1) {
                name = n.getName() + "[" + n.getIndex() + "]";
            }
            // type
            type = n.getPrimaryNodeType().getName();
        } else {
            properties++;
            type = PropertyType.nameFromValue(((Property) i).getType());
        }
        PrintHelper.printRow(ctx, width, new String[] { name, type, Boolean.toString(i.isNode()), Boolean.valueOf(i.isNew()).toString(), Boolean.valueOf(i.isModified()).toString() });
        index++;
    }
    // Footer
    printFooter(ctx, iter);
    return false;
}
Also used : Item(javax.jcr.Item) Node(javax.jcr.Node) Iterator(java.util.Iterator) Property(javax.jcr.Property)

Example 29 with Property

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

the class AbstractLsProperties method execute.

/**
     * {@inheritDoc}
     */
public final boolean execute(Context ctx) throws Exception {
    int[] width = new int[] { 30, longWidth, longWidth, LENGTH_LENGTH, 18 };
    String[] header = new String[] { bundle.getString("word.name"), bundle.getString("word.multiple"), bundle.getString("word.type"), bundle.getString("word.length"), bundle.getString("word.preview") };
    PrintHelper.printRow(ctx, width, header);
    PrintHelper.printSeparatorRow(ctx, width, '-');
    int index = 0;
    Iterator iter = getProperties(ctx);
    int maxItems = getMaxItems(ctx);
    while (iter.hasNext() && index < maxItems) {
        Property p = (Property) iter.next();
        long length = 0;
        if (p.getDefinition().isMultiple()) {
            long[] lengths = p.getLengths();
            for (int i = 0; i < lengths.length; i++) {
                length += lengths[i];
            }
        } else {
            length = p.getLength();
        }
        String multiple = Boolean.toString(p.getDefinition().isMultiple());
        if (p.getDefinition().isMultiple()) {
            multiple += "[" + p.getValues().length + "]";
        }
        Collection row = new ArrayList();
        row.add(p.getName());
        row.add(multiple);
        row.add(PropertyType.nameFromValue(p.getType()));
        row.add(Long.toString(length));
        // preview
        if (p.getDefinition().isMultiple()) {
            row.add(this.getMultiplePreview(p));
        } else {
            row.add(this.getPreview(p));
        }
        PrintHelper.printRow(ctx, width, row);
        index++;
    }
    CommandHelper.getOutput(ctx).println();
    // Write footer
    printFooter(ctx, iter);
    return false;
}
Also used : Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Property(javax.jcr.Property)

Example 30 with Property

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

the class ReadDeepTreeTest method createDeepTree.

protected void createDeepTree() throws Exception {
    Node rn = adminSession.getRootNode();
    allPaths.clear();
    String testNodeName = getTestNodeName();
    long start = System.currentTimeMillis();
    if (!rn.hasNode(testNodeName)) {
        testRoot = adminSession.getRootNode().addNode(testNodeName, "nt:unstructured");
        InputStream in = getClass().getClassLoader().getResourceAsStream(getImportFileName());
        adminSession.importXML(testRoot.getPath(), in, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
        adminSession.save();
    } else {
        testRoot = rn.getNode(testNodeName);
    }
    System.out.println("Import deep tree: " + (System.currentTimeMillis() - start));
    ItemVisitor v = new TraversingItemVisitor.Default() {

        @Override
        protected void entering(Node node, int i) throws RepositoryException {
            visitingNode(node, i);
            super.entering(node, i);
        }

        @Override
        protected void entering(Property prop, int i) throws RepositoryException {
            visitingProperty(prop, i);
            super.entering(prop, i);
        }
    };
    v.visit(testRoot);
    System.out.println("All paths: " + allPaths.size());
}
Also used : ItemVisitor(javax.jcr.ItemVisitor) TraversingItemVisitor(javax.jcr.util.TraversingItemVisitor) InputStream(java.io.InputStream) Node(javax.jcr.Node) Property(javax.jcr.Property)

Aggregations

Property (javax.jcr.Property)445 Node (javax.jcr.Node)252 Value (javax.jcr.Value)99 Test (org.junit.Test)87 Session (javax.jcr.Session)78 PropertyIterator (javax.jcr.PropertyIterator)68 RepositoryException (javax.jcr.RepositoryException)64 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)47 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)37 ValueFormatException (javax.jcr.ValueFormatException)34 QValue (org.apache.jackrabbit.spi.QValue)29 ArrayList (java.util.ArrayList)24 NodeIterator (javax.jcr.NodeIterator)24 QValueValue (org.apache.jackrabbit.spi.commons.value.QValueValue)23 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)20 Item (javax.jcr.Item)19 PathNotFoundException (javax.jcr.PathNotFoundException)17 InvalidItemStateException (javax.jcr.InvalidItemStateException)16 Version (javax.jcr.version.Version)16 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)15