Search in sources :

Example 6 with NodeTypeManagerImpl

use of org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl in project jackrabbit by apache.

the class AddMixinOperation method perform.

public Object perform(SessionContext context) throws RepositoryException {
    int permissions = Permission.NODE_TYPE_MNGMT;
    // is required in addition.
    if (MIX_VERSIONABLE.equals(mixinName) || MIX_SIMPLE_VERSIONABLE.equals(mixinName)) {
        permissions |= Permission.VERSION_MNGMT;
    }
    context.getItemValidator().checkModify(node, CHECK_LOCK | CHECK_CHECKED_OUT | CHECK_CONSTRAINTS | CHECK_HOLD, permissions);
    NodeTypeManagerImpl ntMgr = context.getNodeTypeManager();
    NodeTypeImpl mixin = ntMgr.getNodeType(mixinName);
    if (!mixin.isMixin()) {
        throw new RepositoryException(context.getJCRName(mixinName) + " is not a mixin node type");
    }
    Name primaryTypeName = node.getNodeState().getNodeTypeName();
    NodeTypeImpl primaryType = ntMgr.getNodeType(primaryTypeName);
    if (primaryType.isDerivedFrom(mixinName)) {
        // new mixin is already included in primary type
        return this;
    }
    // build effective node type of mixin's & primary type in order
    // to detect conflicts
    NodeTypeRegistry ntReg = context.getNodeTypeRegistry();
    EffectiveNodeType entExisting;
    try {
        // existing mixin's
        Set<Name> mixins = new HashSet<Name>(node.getNodeState().getMixinTypeNames());
        // build effective node type representing primary type including
        // existing mixin's
        entExisting = ntReg.getEffectiveNodeType(primaryTypeName, mixins);
        if (entExisting.includesNodeType(mixinName)) {
            // new mixin is already included in existing mixin type(s)
            return this;
        }
        // add new mixin
        mixins.add(mixinName);
        // try to build new effective node type (will throw in case
        // of conflicts)
        ntReg.getEffectiveNodeType(primaryTypeName, mixins);
    } catch (NodeTypeConflictException e) {
        throw new ConstraintViolationException(e.getMessage(), e);
    }
    // try to revert the changes in case an exception occurs
    try {
        // modify the state of this node
        NodeState thisState = (NodeState) node.getOrCreateTransientItemState();
        // add mixin name
        Set<Name> mixins = new HashSet<Name>(thisState.getMixinTypeNames());
        mixins.add(mixinName);
        thisState.setMixinTypeNames(mixins);
        // set jcr:mixinTypes property
        node.setMixinTypesProperty(mixins);
        // add 'auto-create' properties defined in mixin type
        for (PropertyDefinition aPda : mixin.getAutoCreatedPropertyDefinitions()) {
            PropertyDefinitionImpl pd = (PropertyDefinitionImpl) aPda;
            // make sure that the property is not already defined
            // by primary type or existing mixin's
            NodeTypeImpl declaringNT = (NodeTypeImpl) pd.getDeclaringNodeType();
            if (!entExisting.includesNodeType(declaringNT.getQName())) {
                node.createChildProperty(pd.unwrap().getName(), pd.getRequiredType(), pd);
            }
        }
        // recursively add 'auto-create' child nodes defined in mixin type
        for (NodeDefinition aNda : mixin.getAutoCreatedNodeDefinitions()) {
            NodeDefinitionImpl nd = (NodeDefinitionImpl) aNda;
            // make sure that the child node is not already defined
            // by primary type or existing mixin's
            NodeTypeImpl declaringNT = (NodeTypeImpl) nd.getDeclaringNodeType();
            if (!entExisting.includesNodeType(declaringNT.getQName())) {
                node.createChildNode(nd.unwrap().getName(), (NodeTypeImpl) nd.getDefaultPrimaryType(), null);
            }
        }
    } catch (RepositoryException re) {
        // try to undo the modifications by removing the mixin
        try {
            node.removeMixin(mixinName);
        } catch (RepositoryException re1) {
        // silently ignore & fall through
        }
        throw re;
    }
    return this;
}
Also used : NodeTypeImpl(org.apache.jackrabbit.core.nodetype.NodeTypeImpl) NodeState(org.apache.jackrabbit.core.state.NodeState) NodeTypeConflictException(org.apache.jackrabbit.core.nodetype.NodeTypeConflictException) NodeDefinition(javax.jcr.nodetype.NodeDefinition) PropertyDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl) RepositoryException(javax.jcr.RepositoryException) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Name(org.apache.jackrabbit.spi.Name) EffectiveNodeType(org.apache.jackrabbit.core.nodetype.EffectiveNodeType) NodeDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) NodeTypeRegistry(org.apache.jackrabbit.core.nodetype.NodeTypeRegistry) NodeTypeManagerImpl(org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl) HashSet(java.util.HashSet)

Example 7 with NodeTypeManagerImpl

use of org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl in project jackrabbit by apache.

the class NodeImpl method getApplicableChildNodeDefinition.

/**
     * Returns the applicable child node definition for a child node with the
     * specified name and node type.
     *
     * @param nodeName
     * @param nodeTypeName
     * @return
     * @throws ConstraintViolationException if no applicable child node definition
     *                                      could be found
     * @throws RepositoryException          if another error occurs
     */
protected NodeDefinitionImpl getApplicableChildNodeDefinition(Name nodeName, Name nodeTypeName) throws ConstraintViolationException, RepositoryException {
    NodeTypeManagerImpl ntMgr = sessionContext.getNodeTypeManager();
    QNodeDefinition cnd = getEffectiveNodeType().getApplicableChildNodeDef(nodeName, nodeTypeName, sessionContext.getNodeTypeRegistry());
    return ntMgr.getNodeDefinition(cnd);
}
Also used : NodeTypeManagerImpl(org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition)

Example 8 with NodeTypeManagerImpl

use of org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl in project jackrabbit by apache.

the class LostFromCacheIssueTest method setUp.

public void setUp() throws Exception {
    super.setUp();
    Workspace workspace = superuser.getWorkspace();
    NamespaceRegistry namespaceRegistry = workspace.getNamespaceRegistry();
    NodeTypeManager ntmgr = workspace.getNodeTypeManager();
    NodeTypeRegistry nodetypeRegistry = ((NodeTypeManagerImpl) ntmgr).getNodeTypeRegistry();
    try {
        namespaceRegistry.registerNamespace(NAMESPACE_PREFIX, NAMESPACE_URI);
    } catch (NamespaceException ignore) {
    //already exists
    }
    QNodeTypeDefinition nodeTypeDefinition = new QNodeTypeDefinitionImpl(((SessionImpl) superuser).getQName(NODETYPE_1), Name.EMPTY_ARRAY, Name.EMPTY_ARRAY, true, false, true, false, null, QPropertyDefinition.EMPTY_ARRAY, QNodeDefinition.EMPTY_ARRAY);
    try {
        nodetypeRegistry.registerNodeType(nodeTypeDefinition);
    } catch (InvalidNodeTypeDefException ignore) {
    //already exists
    }
    nodeTypeDefinition = new QNodeTypeDefinitionImpl(((SessionImpl) superuser).getQName(NODETYPE_2), Name.EMPTY_ARRAY, Name.EMPTY_ARRAY, true, false, true, false, null, QPropertyDefinition.EMPTY_ARRAY, QNodeDefinition.EMPTY_ARRAY);
    try {
        nodetypeRegistry.registerNodeType(nodeTypeDefinition);
    } catch (InvalidNodeTypeDefException ignore) {
    //already exists
    }
    getOrCreate(superuser.getRootNode(), TESTNODE_PATH);
    superuser.save();
}
Also used : QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) InvalidNodeTypeDefException(org.apache.jackrabbit.core.nodetype.InvalidNodeTypeDefException) NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NamespaceRegistry(javax.jcr.NamespaceRegistry) NamespaceException(javax.jcr.NamespaceException) NodeTypeRegistry(org.apache.jackrabbit.core.nodetype.NodeTypeRegistry) NodeTypeManagerImpl(org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl) Workspace(javax.jcr.Workspace) QNodeTypeDefinitionImpl(org.apache.jackrabbit.spi.commons.QNodeTypeDefinitionImpl)

Example 9 with NodeTypeManagerImpl

use of org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl in project jackrabbit by apache.

the class ObservationManagerImpl method createEventFilter.

/**
     * Creates a new event filter with the given restrictions.
     *
     * @param eventTypes A combination of one or more event type constants encoded as a bitmask.
     * @param absPaths absolute paths.
     * @param isDeep a <code>boolean</code>.
     * @param uuid array of UUIDs.
     * @param nodeTypeName array of node type names.
     * @param noLocal a <code>boolean</code>.
     * @param noExternal a <code>boolean</code>.
     * @param noInternal a <code>boolean</code>.
     * @return the event filter with the given restrictions.
     * @throws RepositoryException if an error occurs.
     */
public EventFilter createEventFilter(int eventTypes, List<String> absPaths, boolean isDeep, String[] uuid, String[] nodeTypeName, boolean noLocal, boolean noExternal, boolean noInternal) throws RepositoryException {
    // create NodeType instances from names
    NodeTypeImpl[] nodeTypes;
    if (nodeTypeName == null) {
        nodeTypes = null;
    } else {
        NodeTypeManagerImpl ntMgr = session.getNodeTypeManager();
        nodeTypes = new NodeTypeImpl[nodeTypeName.length];
        for (int i = 0; i < nodeTypes.length; i++) {
            nodeTypes[i] = (NodeTypeImpl) ntMgr.getNodeType(nodeTypeName[i]);
        }
    }
    List<Path> paths = new ArrayList<Path>();
    for (String absPath : absPaths) {
        try {
            Path normalizedPath = session.getQPath(absPath).getNormalizedPath();
            if (!normalizedPath.isAbsolute()) {
                throw new RepositoryException("absPath must be absolute");
            }
            paths.add(normalizedPath);
        } catch (NameException e) {
            String msg = "invalid path syntax: " + absPath;
            log.debug(msg);
            throw new RepositoryException(msg, e);
        }
    }
    NodeId[] ids = null;
    if (uuid != null) {
        ids = new NodeId[uuid.length];
        for (int i = 0; i < uuid.length; i++) {
            ids[i] = NodeId.valueOf(uuid[i]);
        }
    }
    // create filter
    return new EventFilter(session, eventTypes, paths, isDeep, ids, nodeTypes, noLocal, noExternal, noInternal);
}
Also used : Path(org.apache.jackrabbit.spi.Path) NodeTypeImpl(org.apache.jackrabbit.core.nodetype.NodeTypeImpl) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) JackrabbitEventFilter(org.apache.jackrabbit.api.observation.JackrabbitEventFilter) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) NodeId(org.apache.jackrabbit.core.id.NodeId) NodeTypeManagerImpl(org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl)

Aggregations

NodeTypeManagerImpl (org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl)9 RepositoryException (javax.jcr.RepositoryException)6 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)6 NodeTypeRegistry (org.apache.jackrabbit.core.nodetype.NodeTypeRegistry)6 Name (org.apache.jackrabbit.spi.Name)6 HashSet (java.util.HashSet)5 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)5 NodeTypeConflictException (org.apache.jackrabbit.core.nodetype.NodeTypeConflictException)5 NodeTypeImpl (org.apache.jackrabbit.core.nodetype.NodeTypeImpl)5 NodeState (org.apache.jackrabbit.core.state.NodeState)5 PropertyId (org.apache.jackrabbit.core.id.PropertyId)4 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)4 PropertyState (org.apache.jackrabbit.core.state.PropertyState)4 NodeDefinitionImpl (org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl)4 PropertyDefinitionImpl (org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl)4 ArrayList (java.util.ArrayList)3 Value (javax.jcr.Value)3 ValueFormatException (javax.jcr.ValueFormatException)3 NodeDefinition (javax.jcr.nodetype.NodeDefinition)3 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)3