Search in sources :

Example 26 with Operation

use of org.apache.jackrabbit.jcr2spi.operation.Operation in project jackrabbit by apache.

the class SessionImporter method importNode.

/**
     *
     * @param nodeInfo
     * @param parent
     * @return
     * @throws ConstraintViolationException
     * @throws ItemNotFoundException
     * @throws RepositoryException
     */
private NodeState importNode(NodeInfo nodeInfo, NodeState parent) throws ConstraintViolationException, ItemNotFoundException, RepositoryException {
    Name[] parentNtNames = parent.getAllNodeTypeNames();
    if (parent.hasPropertyName(nodeInfo.getName())) {
        /**
             * a property with the same name already exists; if this property
             * has been imported as well (e.g. through document view import
             * where an element can have the same name as one of the attributes
             * of its parent element) we have to rename the conflicting property;
             *
             * see http://issues.apache.org/jira/browse/JCR-61
             */
        PropertyState conflicting = parent.getPropertyState(nodeInfo.getName());
        if (conflicting.getStatus() == Status.NEW) {
            // assume this property has been imported as well;
            // rename conflicting property
            // TODO: use better reversible escaping scheme to create unique name
            Name newName = session.getNameFactory().create(nodeInfo.getName().getNamespaceURI(), nodeInfo.getName().getLocalName() + "_");
            if (parent.hasPropertyName(newName)) {
                newName = session.getNameFactory().create(newName.getNamespaceURI(), newName.getLocalName() + "_");
            }
            // since name changes, need to find new applicable definition
            QPropertyDefinition propDef;
            if (conflicting.getValues().length == 1) {
                // could be single- or multi-valued (n == 1)
                try {
                    // try single-valued
                    propDef = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, newName, conflicting.getType(), false);
                } catch (ConstraintViolationException cve) {
                    // try multi-valued
                    propDef = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, newName, conflicting.getType(), true);
                }
            } else {
                // can only be multi-valued (n == 0 || n > 1)
                propDef = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, newName, conflicting.getType(), true);
            }
            Operation ap = AddProperty.create(parent, newName, conflicting.getType(), propDef, conflicting.getValues());
            stateMgr.execute(ap);
            Operation rm = Remove.create(conflicting);
            stateMgr.execute(rm);
        }
    }
    // do create new nodeState
    QNodeDefinition def = session.getItemDefinitionProvider().getQNodeDefinition(parentNtNames, nodeInfo.getName(), nodeInfo.getNodeTypeName());
    if (def.isProtected()) {
        log.debug("Skipping protected nodeState (" + nodeInfo.getName() + ")");
        return null;
    } else {
        Name ntName = nodeInfo.getNodeTypeName();
        if (ntName == null) {
            // use default node type
            ntName = def.getDefaultPrimaryType();
        }
        Operation an = AddNode.create(parent, nodeInfo.getName(), ntName, nodeInfo.getUUID());
        stateMgr.execute(an);
        // retrieve id of state that has been created during execution of AddNode
        NodeState childState = (NodeState) ((AddNode) an).getAddedStates().get(0);
        // and set mixin types
        Name[] mixinNames = nodeInfo.getMixinNames();
        if (mixinNames != null && mixinNames.length > 0) {
            Operation sm = SetMixin.create(childState, nodeInfo.getMixinNames());
            stateMgr.execute(sm);
        }
        return childState;
    }
}
Also used : NodeState(org.apache.jackrabbit.jcr2spi.state.NodeState) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) Operation(org.apache.jackrabbit.jcr2spi.operation.Operation) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) AddNode(org.apache.jackrabbit.jcr2spi.operation.AddNode) Name(org.apache.jackrabbit.spi.Name) PropertyState(org.apache.jackrabbit.jcr2spi.state.PropertyState)

Example 27 with Operation

use of org.apache.jackrabbit.jcr2spi.operation.Operation in project jackrabbit by apache.

the class SessionImporter method importProperty.

/**
     *
     * @param pi
     * @param parentState
     * @param resolver
     * @throws RepositoryException
     * @throws ConstraintViolationException
     */
private void importProperty(PropInfo pi, NodeState parentState, NamePathResolver resolver) throws RepositoryException, ConstraintViolationException {
    Name propName = pi.getName();
    TextValue[] tva = pi.getValues();
    int infoType = pi.getType();
    PropertyState propState = null;
    QPropertyDefinition def = null;
    NodeEntry parentEntry = (NodeEntry) parentState.getHierarchyEntry();
    PropertyEntry pEntry = parentEntry.getPropertyEntry(propName);
    if (pEntry != null) {
        // a property with that name already exists...
        try {
            PropertyState existing = pEntry.getPropertyState();
            def = existing.getDefinition();
            if (def.isProtected()) {
                // skip protected property
                log.debug("skipping protected property " + LogUtil.safeGetJCRPath(existing, session.getPathResolver()));
                return;
            }
            if (def.isAutoCreated() && (existing.getType() == infoType || infoType == PropertyType.UNDEFINED) && def.isMultiple() == existing.isMultiValued()) {
                // this property has already been auto-created, no need to create it
                propState = existing;
            } else {
                throw new ItemExistsException(LogUtil.safeGetJCRPath(existing, session.getPathResolver()));
            }
        } catch (ItemNotFoundException e) {
        // property doesn't exist any more
        // -> ignore
        }
    }
    Name[] parentNtNames = parentState.getAllNodeTypeNames();
    if (def == null) {
        // there's no property with that name, find applicable definition
        if (tva.length == 1) {
            // could be single- or multi-valued (n == 1)
            def = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, propName, infoType);
        } else {
            // can only be multi-valued (n == 0 || n > 1)
            def = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, propName, infoType, true);
        }
        if (def.isProtected()) {
            // skip protected property
            log.debug("skipping protected property " + propName);
            return;
        }
    }
    // retrieve the target property type needed for creation of QValue(s)
    // including an eventual conversion. the targetType is then needed for
    // setting/updating the type of the property-state.
    int targetType = def.getRequiredType();
    if (targetType == PropertyType.UNDEFINED) {
        if (infoType == PropertyType.UNDEFINED) {
            targetType = PropertyType.STRING;
        } else {
            targetType = infoType;
        }
    }
    QValue[] values = getPropertyValues(pi, targetType, def.isMultiple(), resolver);
    if (propState == null) {
        // create new property
        Operation ap = AddProperty.create(parentState, propName, targetType, def, values);
        stateMgr.execute(ap);
        propState = parentEntry.getPropertyEntry(propName).getPropertyState();
    } else {
        // modify value of existing property
        Operation sp = SetPropertyValue.create(propState, values, targetType);
        stateMgr.execute(sp);
    }
    // store reference for later resolution
    if (propState.getType() == PropertyType.REFERENCE) {
        refTracker.processedReference(propState);
    }
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) Operation(org.apache.jackrabbit.jcr2spi.operation.Operation) Name(org.apache.jackrabbit.spi.Name) PropertyState(org.apache.jackrabbit.jcr2spi.state.PropertyState) NodeEntry(org.apache.jackrabbit.jcr2spi.hierarchy.NodeEntry) PropertyEntry(org.apache.jackrabbit.jcr2spi.hierarchy.PropertyEntry) ItemExistsException(javax.jcr.ItemExistsException) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 28 with Operation

use of org.apache.jackrabbit.jcr2spi.operation.Operation in project jackrabbit by apache.

the class VersionManagerImpl method removeActivity.

public void removeActivity(NodeState activityState) throws UnsupportedRepositoryOperationException, RepositoryException {
    Operation op = RemoveActivity.create(activityState, workspaceManager.getHierarchyManager());
    workspaceManager.execute(op);
}
Also used : Operation(org.apache.jackrabbit.jcr2spi.operation.Operation)

Example 29 with Operation

use of org.apache.jackrabbit.jcr2spi.operation.Operation in project jackrabbit by apache.

the class VersionManagerImpl method resolveMergeConflict.

public void resolveMergeConflict(NodeState nodeState, NodeState versionState, boolean done) throws RepositoryException {
    NodeId vId = versionState.getNodeId();
    PropertyState mergeFailedState = nodeState.getPropertyState(NameConstants.JCR_MERGEFAILED);
    QValue[] vs = mergeFailedState.getValues();
    NodeId[] mergeFailedIds = new NodeId[vs.length - 1];
    for (int i = 0, j = 0; i < vs.length; i++) {
        NodeId id = workspaceManager.getIdFactory().createNodeId(vs[i].getString());
        if (!id.equals(vId)) {
            mergeFailedIds[j] = id;
            j++;
        }
    // else: the version id is being solved by this call and not
    // part of 'jcr:mergefailed' any more
    }
    PropertyState predecessorState = nodeState.getPropertyState(NameConstants.JCR_PREDECESSORS);
    vs = predecessorState.getValues();
    int noOfPredecessors = (done) ? vs.length + 1 : vs.length;
    NodeId[] predecessorIds = new NodeId[noOfPredecessors];
    int i = 0;
    while (i < vs.length) {
        predecessorIds[i] = workspaceManager.getIdFactory().createNodeId(vs[i].getString());
        i++;
    }
    if (done) {
        predecessorIds[i] = vId;
    }
    Operation op = ResolveMergeConflict.create(nodeState, mergeFailedIds, predecessorIds, done);
    workspaceManager.execute(op);
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) NodeId(org.apache.jackrabbit.spi.NodeId) Operation(org.apache.jackrabbit.jcr2spi.operation.Operation) Checkpoint(org.apache.jackrabbit.jcr2spi.operation.Checkpoint) PropertyState(org.apache.jackrabbit.jcr2spi.state.PropertyState)

Example 30 with Operation

use of org.apache.jackrabbit.jcr2spi.operation.Operation in project jackrabbit by apache.

the class VersionManagerImpl method addVersionLabel.

public void addVersionLabel(NodeState versionHistoryState, NodeState versionState, Name qLabel, boolean moveLabel) throws RepositoryException {
    Operation op = AddLabel.create(versionHistoryState, versionState, qLabel, moveLabel);
    workspaceManager.execute(op);
}
Also used : Operation(org.apache.jackrabbit.jcr2spi.operation.Operation)

Aggregations

Operation (org.apache.jackrabbit.jcr2spi.operation.Operation)32 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)6 Path (org.apache.jackrabbit.spi.Path)6 Name (org.apache.jackrabbit.spi.Name)5 ItemNotFoundException (javax.jcr.ItemNotFoundException)4 RepositoryException (javax.jcr.RepositoryException)4 AddNode (org.apache.jackrabbit.jcr2spi.operation.AddNode)3 NodeState (org.apache.jackrabbit.jcr2spi.state.NodeState)3 PropertyState (org.apache.jackrabbit.jcr2spi.state.PropertyState)3 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)3 ItemExistsException (javax.jcr.ItemExistsException)2 HierarchyEntry (org.apache.jackrabbit.jcr2spi.hierarchy.HierarchyEntry)2 EffectiveNodeType (org.apache.jackrabbit.jcr2spi.nodetype.EffectiveNodeType)2 QNodeDefinition (org.apache.jackrabbit.spi.QNodeDefinition)2 QValue (org.apache.jackrabbit.spi.QValue)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 LinkedHashSet (java.util.LinkedHashSet)1 InvalidItemStateException (javax.jcr.InvalidItemStateException)1 Node (javax.jcr.Node)1