Search in sources :

Example 16 with Name

use of org.apache.jackrabbit.spi.Name in project jackrabbit by apache.

the class InternalVersionManagerBase method createInternalVersionItem.

/**
     * Creates an {@link InternalVersionItem} based on the {@link NodeState}
     * identified by <code>id</code>.
     *
     * @param id    the node id of the version item.
     * @return the version item or <code>null</code> if there is no node state
     *         with the given <code>id</code>.
     * @throws RepositoryException if an error occurs while reading from the
     *                             version storage.
     */
protected InternalVersionItem createInternalVersionItem(NodeId id) throws RepositoryException {
    try {
        if (stateMgr.hasItemState(id)) {
            NodeState state = (NodeState) stateMgr.getItemState(id);
            NodeStateEx pNode = new NodeStateEx(stateMgr, ntReg, state, null);
            NodeId parentId = pNode.getParentId();
            InternalVersionItem parent = getItem(parentId);
            Name ntName = state.getNodeTypeName();
            if (ntName.equals(NameConstants.NT_FROZENNODE)) {
                return new InternalFrozenNodeImpl(this, pNode, parent);
            } else if (ntName.equals(NameConstants.NT_VERSIONEDCHILD)) {
                return new InternalFrozenVHImpl(this, pNode, parent);
            } else if (ntName.equals(NameConstants.NT_VERSION)) {
                return ((InternalVersionHistory) parent).getVersion(id);
            } else if (ntName.equals(NameConstants.NT_VERSIONHISTORY)) {
                return new InternalVersionHistoryImpl(this, pNode);
            } else if (ntName.equals(NameConstants.NT_ACTIVITY)) {
                return new InternalActivityImpl(this, pNode);
            } else {
                return null;
            }
        } else {
            return null;
        }
    } catch (ItemStateException e) {
        throw new RepositoryException(e);
    }
}
Also used : NodeState(org.apache.jackrabbit.core.state.NodeState) NodeId(org.apache.jackrabbit.core.id.NodeId) RepositoryException(javax.jcr.RepositoryException) Name(org.apache.jackrabbit.spi.Name) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 17 with Name

use of org.apache.jackrabbit.spi.Name in project jackrabbit by apache.

the class InternalVersionManagerBase method calculateCheckinVersionName.

/**
     * Calculates the name of the new version that will be created by a
     * checkin call. The name is determined as follows:
     * <ul>
     * <li> first the predecessor version with the shortest name is searched.
     * <li> if that predecessor version is the root version, the new version gets
     *      the name "{number of successors}+1" + ".0"
     * <li> if that predecessor version has no successor, the last digit of it's
     *      version number is incremented.
     * <li> if that predecessor version has successors but the incremented name
     *      does not exist, that name is used.
     * <li> otherwise a ".0" is added to the name until a non conflicting name
     *      is found.
     * </ul>
     *
     * Example Graph:
     * <pre>
     * jcr:rootVersion
     *  |     |
     * 1.0   2.0
     *  |
     * 1.1
     *  |
     * 1.2 ---\  ------\
     *  |      \        \
     * 1.3   1.2.0   1.2.0.0
     *  |      |
     * 1.4   1.2.1 ----\
     *  |      |        \
     * 1.5   1.2.2   1.2.1.0
     *  |      |        |
     * 1.6     |     1.2.1.1
     *  |-----/
     * 1.7
     * </pre>
     *
     * @param history the version history
     * @param node the node to checkin
     * @param simple if <code>true</code> indicates simple versioning
     * @return the new version name
     * @throws RepositoryException if an error occurs.
     */
protected String calculateCheckinVersionName(InternalVersionHistoryImpl history, NodeStateEx node, boolean simple) throws RepositoryException {
    if (history == null) {
        String message = "Node " + node.getNodeId() + " has no version history";
        log.error(message);
        throw new VersionException(message);
    }
    InternalVersion best = null;
    if (simple) {
        // 1. in simple versioning just take the 'head' version
        Name[] names = history.getVersionNames();
        best = history.getVersion(names[names.length - 1]);
    } else {
        // 1. search a predecessor, suitable for generating the new name
        InternalValue[] values = node.getPropertyValues(NameConstants.JCR_PREDECESSORS);
        if (values == null || values.length == 0) {
            String message;
            if (values == null) {
                message = "Mandatory jcr:predecessors property missing on node " + node.getNodeId();
            } else {
                message = "Mandatory jcr:predecessors property is empty on node " + node.getNodeId();
            }
            log.error(message);
            throw new VersionException(message);
        }
        for (InternalValue value : values) {
            InternalVersion pred = history.getVersion(value.getNodeId());
            if (pred == null) {
                String message = "Could not instantiate InternalVersion for nodeId " + value.getNodeId() + " (VHR + " + history.getId() + ", node " + node.getNodeId() + ")";
                log.error(message);
                throw new VersionException(message);
            }
            if (best == null || pred.getName().getLocalName().length() < best.getName().getLocalName().length()) {
                best = pred;
            }
        }
    }
    if (best == null) {
        String message = "Could not find 'best' predecessor node for " + node.getNodeId();
        log.error(message);
        throw new VersionException(message);
    }
    // 2. generate version name (assume no namespaces in version names)
    String versionName = best.getName().getLocalName();
    int pos = versionName.lastIndexOf('.');
    if (pos > 0) {
        String newVersionName = versionName.substring(0, pos + 1) + (Integer.parseInt(versionName.substring(pos + 1)) + 1);
        while (history.hasVersion(NameFactoryImpl.getInstance().create("", newVersionName))) {
            versionName += ".0";
            newVersionName = versionName;
        }
        return newVersionName;
    } else {
        // best is root version
        return String.valueOf(best.getSuccessors().size() + 1) + ".0";
    }
}
Also used : InternalValue(org.apache.jackrabbit.core.value.InternalValue) VersionException(javax.jcr.version.VersionException) Name(org.apache.jackrabbit.spi.Name)

Example 18 with Name

use of org.apache.jackrabbit.spi.Name in project jackrabbit by apache.

the class VersionManagerImplMerge method internalMerge.

/**
     * Merges/Updates this node with its corresponding ones
     *
     * @param state state to merge or update
     * @param srcRoot src workspace root node
     * @param failedIds list of failed ids
     * @param bestEffort best effort flag
     * @param shallow is shallow flag
     * @throws RepositoryException if an error occurs
     * @throws ItemStateException if an error occurs
     */
private void internalMerge(NodeStateEx state, NodeStateEx srcRoot, List<ItemId> failedIds, boolean bestEffort, boolean shallow) throws RepositoryException, ItemStateException {
    NodeStateEx srcNode = doMergeTest(state, srcRoot, failedIds, bestEffort);
    if (srcNode == null) {
        if (!shallow) {
            // nodes (see JCR-1046)
            for (NodeStateEx n : state.getChildNodes()) {
                if (n.getEffectiveNodeType().includesNodeType(NameConstants.MIX_VERSIONABLE)) {
                    internalMerge(n, srcRoot, failedIds, bestEffort, shallow);
                }
            }
        }
        return;
    }
    // check lock and hold status if node exists
    checkModify(state, ItemValidator.CHECK_LOCK | ItemValidator.CHECK_HOLD, Permission.NONE);
    // remove all properties that are not present in srcNode
    for (PropertyState prop : state.getProperties()) {
        if (!srcNode.hasProperty(prop.getName())) {
            state.removeProperty(prop.getName());
        }
    }
    // update all properties from the src node
    for (PropertyState prop : srcNode.getProperties()) {
        Name propName = prop.getName();
        // ignore system types
        if (propName.equals(NameConstants.JCR_PRIMARYTYPE) || propName.equals(NameConstants.JCR_MIXINTYPES) || propName.equals(NameConstants.JCR_UUID)) {
            continue;
        }
        state.copyFrom(prop);
    }
    // update the mixin types
    state.setMixins(srcNode.getState().getMixinTypeNames());
    // remove the child nodes in N but not in N'
    LinkedList<ChildNodeEntry> toDelete = new LinkedList<ChildNodeEntry>();
    for (ChildNodeEntry entry : state.getState().getChildNodeEntries()) {
        if (!srcNode.getState().hasChildNodeEntry(entry.getName(), entry.getIndex())) {
            toDelete.add(entry);
        }
    }
    for (ChildNodeEntry entry : toDelete) {
        state.removeNode(entry.getName(), entry.getIndex());
    }
    state.store();
    // add source ones
    for (ChildNodeEntry entry : srcNode.getState().getChildNodeEntries()) {
        NodeStateEx child = state.getNode(entry.getName(), entry.getIndex());
        if (child == null) {
            // if destination workspace already has such an node, remove it
            if (state.hasNode(entry.getId())) {
                child = state.getNode(entry.getId());
                NodeStateEx parent = child.getParent();
                parent.removeNode(child);
                parent.store();
            }
            // create new child
            NodeStateEx srcChild = srcNode.getNode(entry.getId());
            child = state.addNode(entry.getName(), srcChild.getState().getNodeTypeName(), srcChild.getNodeId());
            child.setMixins(srcChild.getState().getMixinTypeNames());
            // copy src child
            state.store();
            internalMerge(child, srcRoot, null, bestEffort, false);
        } else if (!shallow) {
            // recursively merge
            internalMerge(child, srcRoot, failedIds, bestEffort, false);
        }
    }
}
Also used : ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) LinkedList(java.util.LinkedList) PropertyState(org.apache.jackrabbit.core.state.PropertyState) Name(org.apache.jackrabbit.spi.Name)

Example 19 with Name

use of org.apache.jackrabbit.spi.Name in project jackrabbit by apache.

the class InternalVersionHistoryImpl method createVersionInstance.

/**
     * Create a version instance.
     * @param name name of the version
     * @return the new internal version
     * @throws IllegalArgumentException if the version does not exist
     */
synchronized InternalVersionImpl createVersionInstance(Name name) {
    try {
        NodeStateEx nodeStateEx = node.getNode(name, 1);
        InternalVersionImpl v = createVersionInstance(nodeStateEx);
        versionCache.put(v.getId(), v);
        vMgr.versionCreated(v);
        // add labels
        for (Name labelName : labelCache.keySet()) {
            Name versionName = labelCache.get(labelName);
            if (v.getName().equals(versionName)) {
                v.internalAddLabel(labelName);
            }
        }
        return v;
    } catch (RepositoryException e) {
        throw new InconsistentVersioningState("Failed to create version " + name + " in VHR " + historyId + ".", historyId, null);
    }
}
Also used : RepositoryException(javax.jcr.RepositoryException) Name(org.apache.jackrabbit.spi.Name)

Example 20 with Name

use of org.apache.jackrabbit.spi.Name in project jackrabbit by apache.

the class BundleBindingTest method assertValueSerialization.

private void assertValueSerialization(InternalValue value) throws Exception {
    NodePropBundle bundle = new NodePropBundle(NodeId.randomId());
    bundle.setParentId(NodeId.randomId());
    bundle.setNodeTypeName(NameConstants.NT_UNSTRUCTURED);
    bundle.setMixinTypeNames(Collections.<Name>emptySet());
    bundle.setSharedSet(Collections.<NodeId>emptySet());
    Name name = factory.create("", "test");
    PropertyEntry property = new PropertyEntry(new PropertyId(bundle.getId(), name));
    property.setType(value.getType());
    property.setMultiValued(false);
    property.setValues(new InternalValue[] { value });
    bundle.addProperty(property);
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    binding.writeBundle(buffer, bundle);
    byte[] bytes = buffer.toByteArray();
    NodePropBundle result = binding.readBundle(new ByteArrayInputStream(bytes), bundle.getId());
    assertEquals(value, result.getPropertyEntry(name).getValues()[0]);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PropertyEntry(org.apache.jackrabbit.core.persistence.util.NodePropBundle.PropertyEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Name(org.apache.jackrabbit.spi.Name) PropertyId(org.apache.jackrabbit.core.id.PropertyId)

Aggregations

Name (org.apache.jackrabbit.spi.Name)382 RepositoryException (javax.jcr.RepositoryException)101 ArrayList (java.util.ArrayList)57 QValue (org.apache.jackrabbit.spi.QValue)42 NameException (org.apache.jackrabbit.spi.commons.conversion.NameException)39 HashSet (java.util.HashSet)38 Path (org.apache.jackrabbit.spi.Path)38 NodeId (org.apache.jackrabbit.core.id.NodeId)37 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)33 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)32 NodeId (org.apache.jackrabbit.spi.NodeId)32 PropertyId (org.apache.jackrabbit.core.id.PropertyId)29 HashMap (java.util.HashMap)28 NamespaceException (javax.jcr.NamespaceException)28 NodeState (org.apache.jackrabbit.core.state.NodeState)28 Value (javax.jcr.Value)25 QNodeDefinition (org.apache.jackrabbit.spi.QNodeDefinition)25 InternalValue (org.apache.jackrabbit.core.value.InternalValue)23 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)22 PropertyState (org.apache.jackrabbit.core.state.PropertyState)22