Search in sources :

Example 36 with NodeState

use of org.apache.jackrabbit.core.state.NodeState in project jackrabbit by apache.

the class WorkspaceImporter method endNode.

/**
     * {@inheritDoc}
     */
public void endNode(NodeInfo nodeInfo) throws RepositoryException {
    if (aborted) {
        // the import has been aborted, get outta here...
        return;
    }
    NodeState node = parents.pop();
    if (node == null) {
        // node was skipped, nothing to do here
        return;
    }
    boolean succeeded = false;
    try {
        // check sanity of workspace/session first
        wsp.sanityCheck();
        // post-process node (initialize properties with special semantics etc.)
        postProcessNode(node);
        // make sure node is valid according to its definition
        itemOps.validate(node);
        // we're done with that node, now store its state
        itemOps.store(node);
        succeeded = true;
    } finally {
        if (!succeeded) {
            // update operation failed, cancel all modifications
            aborted = true;
            itemOps.cancel();
        }
    }
}
Also used : NodeState(org.apache.jackrabbit.core.state.NodeState)

Example 37 with NodeState

use of org.apache.jackrabbit.core.state.NodeState in project jackrabbit by apache.

the class NodeImpl method setMixins.

/**
     * {@inheritDoc}
     */
public void setMixins(String[] mixinNames) throws NoSuchNodeTypeException, VersionException, ConstraintViolationException, LockException, RepositoryException {
    // check state of this instance
    sanityCheck();
    NodeTypeManagerImpl ntMgr = sessionContext.getNodeTypeManager();
    Set<Name> newMixins = new HashSet<Name>();
    for (String name : mixinNames) {
        Name qName = sessionContext.getQName(name);
        if (!ntMgr.getNodeType(qName).isMixin()) {
            throw new RepositoryException(sessionContext.getJCRName(qName) + " is not a mixin node type");
        }
        newMixins.add(qName);
    }
    // make sure this node is checked-out, neither protected nor locked and
    // the editing session has sufficient permission to change the mixin types.
    // special handling of mix:(simple)versionable. since adding the
    // mixin alters the version storage jcr:versionManagement privilege
    // is required in addition.
    int permissions = Permission.NODE_TYPE_MNGMT;
    if (newMixins.contains(MIX_VERSIONABLE) || newMixins.contains(MIX_SIMPLE_VERSIONABLE)) {
        permissions |= Permission.VERSION_MNGMT;
    }
    int options = ItemValidator.CHECK_CHECKED_OUT | ItemValidator.CHECK_LOCK | ItemValidator.CHECK_CONSTRAINTS | ItemValidator.CHECK_HOLD;
    sessionContext.getItemValidator().checkModify(this, options, permissions);
    final NodeState state = data.getNodeState();
    // build effective node type of primary type & new mixin's
    // in order to detect conflicts
    NodeTypeRegistry ntReg = ntMgr.getNodeTypeRegistry();
    EffectiveNodeType entNew, entOld, entAll;
    try {
        entNew = ntReg.getEffectiveNodeType(newMixins);
        entOld = ntReg.getEffectiveNodeType(state.getMixinTypeNames());
        // try to build new effective node type (will throw in case of conflicts)
        entAll = ntReg.getEffectiveNodeType(state.getNodeTypeName(), newMixins);
    } catch (NodeTypeConflictException ntce) {
        throw new ConstraintViolationException(ntce.getMessage());
    }
    // added child item definitions
    Set<QItemDefinition> addedDefs = new HashSet<QItemDefinition>(Arrays.asList(entNew.getAllItemDefs()));
    addedDefs.removeAll(Arrays.asList(entOld.getAllItemDefs()));
    // referential integrity check
    boolean referenceableOld = getEffectiveNodeType().includesNodeType(NameConstants.MIX_REFERENCEABLE);
    boolean referenceableNew = entAll.includesNodeType(NameConstants.MIX_REFERENCEABLE);
    if (referenceableOld && !referenceableNew) {
        // node would become non-referenceable;
        // make sure no references exist
        PropertyIterator iter = getReferences();
        if (iter.hasNext()) {
            throw new ConstraintViolationException("the new mixin types cannot be set as it would render " + "this node 'non-referenceable' while it is still being " + "referenced through at least one property of type REFERENCE");
        }
    }
    // gather currently assigned definitions *before* doing actual modifications
    Map<ItemId, ItemDefinition> oldDefs = new HashMap<ItemId, ItemDefinition>();
    for (Name name : getNodeState().getPropertyNames()) {
        PropertyId id = new PropertyId(getNodeId(), name);
        try {
            PropertyState propState = (PropertyState) stateMgr.getItemState(id);
            oldDefs.put(id, itemMgr.getDefinition(propState));
        } catch (ItemStateException ise) {
            String msg = name + ": failed to retrieve property state";
            log.error(msg, ise);
            throw new RepositoryException(msg, ise);
        }
    }
    for (ChildNodeEntry cne : getNodeState().getChildNodeEntries()) {
        try {
            NodeState nodeState = (NodeState) stateMgr.getItemState(cne.getId());
            oldDefs.put(cne.getId(), itemMgr.getDefinition(nodeState));
        } catch (ItemStateException ise) {
            String msg = cne + ": failed to retrieve node state";
            log.error(msg, ise);
            throw new RepositoryException(msg, ise);
        }
    }
    // now do the actual modifications in content as mandated by the new mixins
    // modify the state of this node
    NodeState thisState = (NodeState) getOrCreateTransientItemState();
    thisState.setMixinTypeNames(newMixins);
    // set jcr:mixinTypes property
    setMixinTypesProperty(newMixins);
    // walk through properties and child nodes and change definition as necessary
    // use temp set to avoid ConcurrentModificationException
    HashSet<Name> set = new HashSet<Name>(thisState.getPropertyNames());
    for (Name propName : set) {
        PropertyState propState = null;
        try {
            propState = (PropertyState) stateMgr.getItemState(new PropertyId(thisState.getNodeId(), propName));
            // the following call triggers ConstraintViolationException
            // if there isn't any suitable definition anymore
            itemMgr.getDefinition(propState);
        } catch (ConstraintViolationException cve) {
            // redefine property if possible
            try {
                if (oldDefs.get(propState.getId()).isProtected()) {
                    // remove 'orphaned' protected properties immediately
                    removeChildProperty(propName);
                    continue;
                }
                PropertyDefinitionImpl pdi = getApplicablePropertyDefinition(propName, propState.getType(), propState.isMultiValued(), false);
                PropertyImpl prop = (PropertyImpl) itemMgr.getItem(propState.getId());
                if (pdi.getRequiredType() != PropertyType.UNDEFINED && pdi.getRequiredType() != propState.getType()) {
                    // value conversion required
                    if (propState.isMultiValued()) {
                        // convert value
                        Value[] values = ValueHelper.convert(prop.getValues(), pdi.getRequiredType(), getSession().getValueFactory());
                        // redefine property
                        prop.onRedefine(pdi.unwrap());
                        // set converted values
                        prop.setValue(values);
                    } else {
                        // convert value
                        Value value = ValueHelper.convert(prop.getValue(), pdi.getRequiredType(), getSession().getValueFactory());
                        // redefine property
                        prop.onRedefine(pdi.unwrap());
                        // set converted values
                        prop.setValue(value);
                    }
                } else {
                    // redefine property
                    prop.onRedefine(pdi.unwrap());
                }
                // update collection of added definitions
                addedDefs.remove(pdi.unwrap());
            } catch (ValueFormatException vfe) {
                // value conversion failed, remove it
                removeChildProperty(propName);
            } catch (ConstraintViolationException cve1) {
                // no suitable definition found for this property,
                // remove it
                removeChildProperty(propName);
            }
        } catch (ItemStateException ise) {
            String msg = propName + ": failed to retrieve property state";
            log.error(msg, ise);
            throw new RepositoryException(msg, ise);
        }
    }
    // use temp array to avoid ConcurrentModificationException
    ArrayList<ChildNodeEntry> list = new ArrayList<ChildNodeEntry>(thisState.getChildNodeEntries());
    // start from tail to avoid problems with same-name siblings
    for (int i = list.size() - 1; i >= 0; i--) {
        ChildNodeEntry entry = list.get(i);
        NodeState nodeState = null;
        try {
            nodeState = (NodeState) stateMgr.getItemState(entry.getId());
            // the following call triggers ConstraintViolationException
            // if there isn't any suitable definition anymore
            itemMgr.getDefinition(nodeState);
        } catch (ConstraintViolationException cve) {
            // redefine node if possible
            try {
                if (oldDefs.get(nodeState.getId()).isProtected()) {
                    // remove 'orphaned' protected child node immediately
                    removeChildNode(entry.getId());
                    continue;
                }
                NodeDefinitionImpl ndi = getApplicableChildNodeDefinition(entry.getName(), nodeState.getNodeTypeName());
                NodeImpl node = (NodeImpl) itemMgr.getItem(nodeState.getId());
                // redefine node
                node.onRedefine(ndi.unwrap());
                // update collection of added definitions
                addedDefs.remove(ndi.unwrap());
            } catch (ConstraintViolationException cve1) {
                // no suitable definition found for this child node,
                // remove it
                removeChildNode(entry.getId());
            }
        } catch (ItemStateException ise) {
            String msg = entry + ": failed to retrieve node state";
            log.error(msg, ise);
            throw new RepositoryException(msg, ise);
        }
    }
    // and at the same time were not present with the old mixins
    for (QItemDefinition def : addedDefs) {
        if (def.isAutoCreated()) {
            if (def.definesNode()) {
                NodeDefinitionImpl ndi = ntMgr.getNodeDefinition((QNodeDefinition) def);
                createChildNode(def.getName(), (NodeTypeImpl) ndi.getDefaultPrimaryType(), null);
            } else {
                PropertyDefinitionImpl pdi = ntMgr.getPropertyDefinition((QPropertyDefinition) def);
                createChildProperty(pdi.unwrap().getName(), pdi.getRequiredType(), pdi);
            }
        }
    }
}
Also used : NodeState(org.apache.jackrabbit.core.state.NodeState) HashMap(java.util.HashMap) NodeTypeConflictException(org.apache.jackrabbit.core.nodetype.NodeTypeConflictException) QItemDefinition(org.apache.jackrabbit.spi.QItemDefinition) ItemDefinition(javax.jcr.nodetype.ItemDefinition) ArrayList(java.util.ArrayList) ItemId(org.apache.jackrabbit.core.id.ItemId) Name(org.apache.jackrabbit.spi.Name) NodeDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) NodeTypeManagerImpl(org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl) HashSet(java.util.HashSet) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) PropertyIterator(javax.jcr.PropertyIterator) PropertyDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl) RepositoryException(javax.jcr.RepositoryException) QItemDefinition(org.apache.jackrabbit.spi.QItemDefinition) PropertyId(org.apache.jackrabbit.core.id.PropertyId) PropertyState(org.apache.jackrabbit.core.state.PropertyState) InvalidItemStateException(javax.jcr.InvalidItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) EffectiveNodeType(org.apache.jackrabbit.core.nodetype.EffectiveNodeType) Value(javax.jcr.Value) InternalValue(org.apache.jackrabbit.core.value.InternalValue) ValueFormatException(javax.jcr.ValueFormatException) NodeTypeRegistry(org.apache.jackrabbit.core.nodetype.NodeTypeRegistry)

Example 38 with NodeState

use of org.apache.jackrabbit.core.state.NodeState in project jackrabbit by apache.

the class NodeImpl method createChildNode.

protected synchronized NodeImpl createChildNode(Name name, NodeTypeImpl nodeType, NodeId id) throws RepositoryException {
    // create a new node state
    NodeState nodeState = stateMgr.createTransientNodeState(id, nodeType.getQName(), getNodeId(), ItemState.STATUS_NEW);
    // create Node instance wrapping new node state
    NodeImpl node;
    try {
        // NOTE: since the node is not yet connected to its parent, avoid
        // calling ItemManager#getItem(ItemId) which may include a permission
        // check (with subsequent usage of the hierarachy-mgr -> error).
        // just let the mgr create the new node that is known to exist and
        // which has not been accessed before.
        node = (NodeImpl) itemMgr.createItemInstance(nodeState);
    } catch (RepositoryException re) {
        // something went wrong
        stateMgr.disposeTransientItemState(nodeState);
        // re-throw
        throw re;
    }
    // modify the state of 'this', i.e. the parent node
    NodeState thisState = (NodeState) getOrCreateTransientItemState();
    // add new child node entry
    thisState.addChildNodeEntry(name, nodeState.getNodeId());
    // add 'auto-create' properties defined in node type
    for (PropertyDefinition aPda : nodeType.getAutoCreatedPropertyDefinitions()) {
        PropertyDefinitionImpl pd = (PropertyDefinitionImpl) aPda;
        node.createChildProperty(pd.unwrap().getName(), pd.getRequiredType(), pd);
    }
    // recursively add 'auto-create' child nodes defined in node type
    for (NodeDefinition aNda : nodeType.getAutoCreatedNodeDefinitions()) {
        NodeDefinitionImpl nd = (NodeDefinitionImpl) aNda;
        node.createChildNode(nd.unwrap().getName(), (NodeTypeImpl) nd.getDefaultPrimaryType(), null);
    }
    return node;
}
Also used : NodeDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl) NodeState(org.apache.jackrabbit.core.state.NodeState) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) NodeDefinition(javax.jcr.nodetype.NodeDefinition) PropertyDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl) RepositoryException(javax.jcr.RepositoryException) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 39 with NodeState

use of org.apache.jackrabbit.core.state.NodeState in project jackrabbit by apache.

the class NodeImpl method setMixinTypesProperty.

void setMixinTypesProperty(Set<Name> mixinNames) throws RepositoryException {
    NodeState thisState = data.getNodeState();
    // get or create jcr:mixinTypes property
    PropertyImpl prop;
    if (thisState.hasPropertyName(NameConstants.JCR_MIXINTYPES)) {
        prop = (PropertyImpl) itemMgr.getItem(new PropertyId(thisState.getNodeId(), NameConstants.JCR_MIXINTYPES));
    } else {
        // find definition for the jcr:mixinTypes property and create property
        PropertyDefinitionImpl def = getApplicablePropertyDefinition(NameConstants.JCR_MIXINTYPES, PropertyType.NAME, true, true);
        prop = createChildProperty(NameConstants.JCR_MIXINTYPES, PropertyType.NAME, def);
    }
    if (mixinNames.isEmpty()) {
        // purge empty jcr:mixinTypes property
        removeChildProperty(NameConstants.JCR_MIXINTYPES);
        return;
    }
    // call internalSetValue for setting the jcr:mixinTypes property
    // to avoid checking of the 'protected' flag
    InternalValue[] vals = new InternalValue[mixinNames.size()];
    Iterator<Name> iter = mixinNames.iterator();
    int cnt = 0;
    while (iter.hasNext()) {
        vals[cnt++] = InternalValue.create(iter.next());
    }
    prop.internalSetValue(vals, PropertyType.NAME);
}
Also used : NodeState(org.apache.jackrabbit.core.state.NodeState) PropertyDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl) InternalValue(org.apache.jackrabbit.core.value.InternalValue) PropertyId(org.apache.jackrabbit.core.id.PropertyId) Name(org.apache.jackrabbit.spi.Name)

Example 40 with NodeState

use of org.apache.jackrabbit.core.state.NodeState in project jackrabbit by apache.

the class NodeImpl method createChildProperty.

/**
     * Creates a new property with the given name and <code>type</code> hint and
     * property definition. If the given property definition is not of type
     * <code>UNDEFINED</code>, then it takes precedence over the
     * <code>type</code> hint.
     *
     * @param name the name of the property to create.
     * @param type the type hint.
     * @param def  the associated property definition.
     * @return the property instance.
     * @throws RepositoryException if the property cannot be created.
     */
protected synchronized PropertyImpl createChildProperty(Name name, int type, PropertyDefinitionImpl def) throws RepositoryException {
    // create a new property state
    PropertyState propState;
    try {
        QPropertyDefinition propDef = def.unwrap();
        if (def.getRequiredType() != PropertyType.UNDEFINED) {
            type = def.getRequiredType();
        }
        propState = stateMgr.createTransientPropertyState(getNodeId(), name, ItemState.STATUS_NEW);
        propState.setType(type);
        propState.setMultiValued(propDef.isMultiple());
        // compute system generated values if necessary
        String userId = sessionContext.getSessionImpl().getUserID();
        new NodeTypeInstanceHandler(userId).setDefaultValues(propState, data.getNodeState(), propDef);
    } catch (ItemStateException ise) {
        String msg = "failed to add property " + name + " to " + this;
        log.debug(msg);
        throw new RepositoryException(msg, ise);
    }
    // create Property instance wrapping new property state
    // NOTE: since the property is not yet connected to its parent, avoid
    // calling ItemManager#getItem(ItemId) which may include a permission
    // check (with subsequent usage of the hierarachy-mgr -> error).
    // just let the mgr create the new property that is known to exist and
    // which has not been accessed before.
    PropertyImpl prop = (PropertyImpl) itemMgr.createItemInstance(propState);
    // modify the state of 'this', i.e. the parent node
    NodeState thisState = (NodeState) getOrCreateTransientItemState();
    // add new property entry
    thisState.addPropertyName(name);
    return prop;
}
Also used : NodeState(org.apache.jackrabbit.core.state.NodeState) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) RepositoryException(javax.jcr.RepositoryException) PropertyState(org.apache.jackrabbit.core.state.PropertyState) InvalidItemStateException(javax.jcr.InvalidItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Aggregations

NodeState (org.apache.jackrabbit.core.state.NodeState)114 RepositoryException (javax.jcr.RepositoryException)53 NodeId (org.apache.jackrabbit.core.id.NodeId)52 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)44 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)34 Name (org.apache.jackrabbit.spi.Name)28 NoSuchItemStateException (org.apache.jackrabbit.core.state.NoSuchItemStateException)26 PropertyState (org.apache.jackrabbit.core.state.PropertyState)25 Path (org.apache.jackrabbit.spi.Path)24 PropertyId (org.apache.jackrabbit.core.id.PropertyId)23 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)16 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)16 ItemNotFoundException (javax.jcr.ItemNotFoundException)15 ArrayList (java.util.ArrayList)14 InvalidItemStateException (javax.jcr.InvalidItemStateException)12 InternalValue (org.apache.jackrabbit.core.value.InternalValue)12 HashSet (java.util.HashSet)10 ItemExistsException (javax.jcr.ItemExistsException)10 IOException (java.io.IOException)8 NodeTypeImpl (org.apache.jackrabbit.core.nodetype.NodeTypeImpl)7