Package org.modeshape.jcr.cache

Examples of org.modeshape.jcr.cache.MutableCachedNode


    @Override
    public void initialize( SessionCache session,
                            MutableCachedNode parent ) {
        this.propFactory = session.getContext().getPropertyFactory();
        MutableCachedNode system = null;
        MutableCachedNode namespaces = null;

        // Create the "/jcr:system" node ...
        system = createNode(session, parent, "jcr:system", JcrLexicon.SYSTEM, ModeShapeLexicon.SYSTEM);

        // Create the "/jcr:system/jcr:nodeTypes" node ...
        createNode(session, system, "jcr:nodeTypes", JcrLexicon.NODE_TYPES, ModeShapeLexicon.NODE_TYPES);

        // Create the "/jcr:system/jcr:versionStorage" node which we don't want to index
        MutableCachedNode versionStorage = createNode(session, system, "jcr:versionStorage", JcrLexicon.VERSION_STORAGE, ModeShapeLexicon.VERSION_STORAGE);
        versionStorage.setQueryable(false);

        // Create the "/jcr:system/mode:namespaces" node ...
        namespaces = createNode(session, system, "mode:namespaces", ModeShapeLexicon.NAMESPACES, ModeShapeLexicon.NAMESPACES);

        // Create the standard namespaces ...
        // createNamespace(session, namespaces, "", ""); // Don't initialize the "" namespaces
        createNamespace(session, namespaces, JcrLexicon.Namespace.PREFIX, JcrLexicon.Namespace.URI);
        createNamespace(session, namespaces, JcrNtLexicon.Namespace.PREFIX, JcrNtLexicon.Namespace.URI);
        createNamespace(session, namespaces, JcrMixLexicon.Namespace.PREFIX, JcrMixLexicon.Namespace.URI);
        createNamespace(session, namespaces, JcrSvLexicon.Namespace.PREFIX, JcrSvLexicon.Namespace.URI);
        createNamespace(session, namespaces, ModeShapeLexicon.Namespace.PREFIX, ModeShapeLexicon.Namespace.URI);
        createNamespace(session, namespaces, JcrNamespaceRegistry.XML_NAMESPACE_PREFIX, JcrNamespaceRegistry.XML_NAMESPACE_URI);
        createNamespace(session,
                        namespaces,
                        JcrNamespaceRegistry.XMLNS_NAMESPACE_PREFIX,
                        JcrNamespaceRegistry.XMLNS_NAMESPACE_URI);
        createNamespace(session,
                        namespaces,
                        JcrNamespaceRegistry.XML_SCHEMA_NAMESPACE_PREFIX,
                        JcrNamespaceRegistry.XML_SCHEMA_NAMESPACE_URI);
        createNamespace(session,
                        namespaces,
                        JcrNamespaceRegistry.XML_SCHEMA_INSTANCE_NAMESPACE_PREFIX,
                        JcrNamespaceRegistry.XML_SCHEMA_INSTANCE_NAMESPACE_URI);

        // Create the "/jcr:system/mode:locks" node which we don't want to index
        MutableCachedNode locks = createNode(session, system, "mode:locks", ModeShapeLexicon.LOCKS, ModeShapeLexicon.LOCKS);
        locks.setQueryable(false);

        // Create the "/jcr:system/mode:indexes" node which we don't want to index
        MutableCachedNode indexes = createNode(session, system, "mode:indexes", ModeShapeLexicon.INDEXES, ModeShapeLexicon.INDEXES);
        indexes.setQueryable(false);
    }
View Full Code Here


        }

        private void aclMetadataRefresh( long aclChangesCount ) {
            // we have a session that has added and/or removed ACLs from nodes, so we need to reflect this in the repository
            // metadata
            MutableCachedNode systemNode = systemContent.mutableSystemNode();
            org.modeshape.jcr.value.Property aclCount = systemNode.getProperty(ModeShapeLexicon.ACL_COUNT, systemCache);
            if (aclCount == null && aclChangesCount > 0) {
                systemNode.setProperty(systemCache, propertyFactory().create(ModeShapeLexicon.ACL_COUNT, aclChangesCount));
                repository().repositoryCache().setAccessControlEnabled(true);
            } else if (aclCount != null) {
                long newCount = Long.valueOf(aclCount.getFirstValue().toString()) + aclChangesCount;
                if (newCount < 0) {
                    newCount = 0;
                }
                if (newCount == 0) {
                    repository().repositoryCache().setAccessControlEnabled(false);
                }
                systemNode.setProperty(systemCache, propertyFactory().create(ModeShapeLexicon.ACL_COUNT, newCount));
            }
        }
View Full Code Here

                    // specific child node type. This likely means there is either 0 or more than 1 (possibly residual)
                    // child node definitions. We need to find all of the added/renamed child nodes and use their specific
                    // primary types. The first to fail will result in an exception ...
                    final SessionCache session = cache();
                    for (NodeKey appendedOrRenamedKey : appendedOrRenamedChildrenByName.get(childName)) {
                        MutableCachedNode appendedOrRenamedChild = session.mutable(appendedOrRenamedKey);
                        if (appendedOrRenamedChild == null) continue;
                        Name childPrimaryType = appendedOrRenamedChild.getPrimaryType(session);
                        childDefns = nodeTypeCapabilities.findChildNodeDefinitions(primaryType, mixinTypes);
                        childNodeDefinition = childDefns.findBestDefinitionForChild(childName, childPrimaryType, true,
                                                                                    siblingCounter);
                        if (childNodeDefinition == null) {
                            // Could not find a valid child node definition that allows SNS given the child's primary type and
View Full Code Here

            }
            destAncestor = destAncestor.getParent();
        }

        try {
            MutableCachedNode mutableSrcParent = srcParent.mutable();
            MutableCachedNode mutableDestParent = destParentNode.mutable();
            if (mutableSrcParent.equals(mutableDestParent)) {
                // It's just a rename ...
                mutableSrcParent.renameChild(sessionCache, srcNode.key(), destPath.getLastSegment().getName());
            } else {
                // It is a move from one parent to another ...
View Full Code Here

            List<JcrValue> newNodeSuccessors = new ArrayList<>();

            // Add each of the successors from the version's predecessor ...
            NodeKey predecessorKey = ((NodeKeyReference)((JcrValue)predecessorValue).value()).getNodeKey();
            AbstractJcrNode predecessor = session().node(predecessorKey, null);
            MutableCachedNode predecessorSystem = system.mutable(predecessor.key());

            JcrValue[] nodeSuccessors = predecessor.getProperty(JcrLexicon.SUCCESSORS).getValues();
            addValuesNotInSet(nodeSuccessors, newNodeSuccessors, versionId, addedValues);

            if (successors != null) {
                // Add each of the successors from the version being removed ...
                addValuesNotInSet(successors.getValues(), newNodeSuccessors, versionId, addedValues);
            }

            // Set the property ...
            Object[] newSuccessorReferences = extractValues(newNodeSuccessors);
            predecessorSystem.setProperty(system, session.propertyFactory().create(JcrLexicon.SUCCESSORS,
                                                                           newSuccessorReferences));
            addedValues.clear();
        }

        if (successors != null) {
            // Remove the reference to the dead version from the predecessors property of all the successors
            for (Value successorValue : successors.getValues()) {
                addedValues.clear();
                List<JcrValue> newNodePredecessors = new ArrayList<>();

                // Add each of the predecessors from the version's successor ...
                NodeKey successorKey = ((NodeKeyReference)((JcrValue)successorValue).value()).getNodeKey();
                AbstractJcrNode successor = session().node(successorKey, null);
                MutableCachedNode successorSystem = system.mutable(successor.key());

                JcrValue[] nodePredecessors = successor.getProperty(JcrLexicon.PREDECESSORS).getValues();
                addValuesNotInSet(nodePredecessors, newNodePredecessors, versionId, addedValues);

                // Add each of the predecessors from the version being removed ...
                addValuesNotInSet(predecessors.getValues(), newNodePredecessors, versionId, addedValues);

                // Set the property ...
                Object[] newPredecessorReferences = extractValues(newNodePredecessors);
                successorSystem.setProperty(system,
                                            session.propertyFactory().create(JcrLexicon.PREDECESSORS, newPredecessorReferences));
            }
        }

        system.mutable(key).removeChild(system, version.key);
View Full Code Here

        PropertyFactory propFactory = session.propertyFactory();
        Property ptProp = propFactory.create(JcrLexicon.PRIMARY_TYPE, childPrimaryNodeTypeName);

        if (JcrNtLexicon.UNSTRUCTURED.equals(childPrimaryNodeTypeName)) {
            // This is very common, and we know they don't have auto-created properties or children ...
            MutableCachedNode newChild = mutable().createChild(cache, desiredKey, childName, ptProp);

            // And get or create the JCR node ...
            AbstractJcrNode jcrNode = session.node(newChild.getKey(), null, key());

            // Set the child node definition ...
            jcrNode.setNodeDefinitionId(childDefn.getId(), nodeTypes.getVersion());
            return jcrNode;
        }

        // Auto-create the properties ...
        NodeTypes capabilities = session.repository().nodeTypeManager().getNodeTypes();
        LinkedList<Property> props = autoCreatePropertiesFor(childName, childPrimaryNodeTypeName, propFactory, capabilities);

        // Then create the node ...
        MutableCachedNode newChild = null;
        if (props != null) {
            props.addFirst(ptProp);
            newChild = mutable().createChild(cache, desiredKey, childName, props);
        } else {
            newChild = mutable().createChild(cache, desiredKey, childName, ptProp);
        }

        // Check if the child node is referenceable
        if (capabilities.getNodeType(childPrimaryNodeTypeName).isNodeType(JcrMixLexicon.REFERENCEABLE)) {
            newChild.setProperty(cache, propFactory.create(JcrLexicon.UUID, session.nodeIdentifier(newChild.getKey())));
        }

        // And get or create the JCR node ...
        AbstractJcrNode jcrNode = session.node(newChild.getKey(), null, key());

        // Set the child node definition ...
        jcrNode.setNodeDefinitionId(childDefn.getId(), nodeTypes.getVersion());

        // Create any mandatory properties or child nodes ...
View Full Code Here

            }
        }

        // Otherwise, we have to create the property, so first find a valid property definition ...
        SessionCache cache = sessionCache();
        MutableCachedNode node = mutable();
        Name primaryType = node.getPrimaryType(cache);
        Set<Name> mixinTypes = node.getMixinTypes(cache);
        NodeTypes nodeTypes = session.nodeTypes();
        JcrPropertyDefinition defn = null;
        final boolean skipProtected = !skipProtectedValidation;
        defn = nodeTypes.findPropertyDefinition(session, primaryType, mixinTypes, name, value, true, skipProtected, true);

        if (defn == null) {
            // Failed to find a valid property definition,
            // so figure out if there's a definition that would work if it had no constraints ...
            defn = nodeTypes.findPropertyDefinition(session, primaryType, mixinTypes, name, value, true, skipProtected, false);

            String propName = readable(name);
            if (defn != null) {
                String defnName = defn.getName();
                String nodeTypeName = defn.getDeclaringNodeType().getName();
                I18n msg = JcrI18n.valueViolatesConstraintsOnDefinition;
                throw new ConstraintViolationException(msg.text(propName, value.getString(), location(), defnName, nodeTypeName));
            }
            I18n msg = JcrI18n.noPropertyDefinition;
            throw new ConstraintViolationException(msg.text(propName, location(), readable(primaryType), readable(mixinTypes)));
        }

        // The 'findBestPropertyDefinition' method checks constraints for all definitions exception those with a
        // require type of REFERENCE. This is because checking such constraints may cause unnecessary loading of nodes.
        // Therefore, see if this is the case ...
        int requiredType = defn.getRequiredType();
        if (requiredType == PropertyType.REFERENCE || requiredType == PropertyType.WEAKREFERENCE) {
            // Check that the REFERENCE value satisfies the constraints ...
            if (!skipReferenceValidation && !defn.canCastToTypeAndSatisfyConstraints(value, session)) {
                // The REFERENCE value did not satisfy the constraints ...
                String propName = readable(name);
                String defnName = defn.getName();
                String nodeTypeName = defn.getDeclaringNodeType().getName();
                I18n i18n = JcrI18n.weakReferenceValueViolatesConstraintsOnDefinition;
                if (requiredType == PropertyType.REFERENCE) i18n = JcrI18n.referenceValueViolatesConstraintsOnDefinition;
                throw new ConstraintViolationException(i18n.text(propName, value.getString(), location(), defnName, nodeTypeName));
            }
        }

        if (!skipVersioningValidation && !isCheckedOut()) {
            // Node is not checked out, so changing property is only allowed if OPV of property is 'ignore' ...
            if (defn.getOnParentVersion() != OnParentVersionAction.IGNORE) {
                // Can't change this property ...
                String path = getParent().getPath();
                throw new VersionException(JcrI18n.nodeIsCheckedIn.text(path));
            }
        }

        // Create the JCR Property object ...
        if (requiredType == PropertyType.UNDEFINED) {
            requiredType = value.getType();
        }
        if (requiredType != value.getType()) {
            // Convert the value to the required type ...
            value = value.asType(requiredType);
        }
        AbstractJcrProperty jcrProp = new JcrSingleValueProperty(this, name, requiredType);
        AbstractJcrProperty otherProp = this.jcrProperties.putIfAbsent(name, jcrProp);
        if (otherProp != null) {
            // Someone snuck in and created this property while we created ours, so use that instance instead ...
            jcrProp = otherProp;
        }
        // Set the property on the cached node (even if there was an 'otherProp' instance) ...
        Property newProperty = session.propertyFactory().create(name, value.value());
        node.setProperty(cache, newProperty);
        return jcrProp;
    }
View Full Code Here

            }
        }

        // Otherwise, we have to create the property, so first find a valid property definition ...
        SessionCache cache = sessionCache();
        MutableCachedNode node = mutable();
        Name primaryType = node.getPrimaryType(cache);
        Set<Name> mixinTypes = node.getMixinTypes(cache);
        NodeTypes nodeTypes = session.nodeTypes();
        JcrPropertyDefinition defn = null;
        defn = nodeTypes.findPropertyDefinition(session, primaryType, mixinTypes, name, values, !skipProtectedValidation,
                                                skipReferenceValidation);

        if (defn == null) {
            // Failed to find a valid property definition,
            // so figure out if there's a definition that would work if it had no constraints ...
            defn = nodeTypes.findPropertyDefinition(session, primaryType, mixinTypes, name, values, !skipProtectedValidation,
                                                    false);

            String propName = readable(name);
            if (defn != null) {
                String defnName = defn.getName();
                String nodeTypeName = defn.getDeclaringNodeType().getName();
                I18n msg = JcrI18n.valueViolatesConstraintsOnDefinition;
                throw new ConstraintViolationException(msg.text(propName, readable(values), location(), defnName, nodeTypeName));
            }
            // See if we can find a single-valued property definition ...
            defn = nodeTypes.findPropertyDefinition(session, primaryType, mixinTypes, name, values[0], true, false);
            if (defn == null) {
                // The cardinality of the new values does not match the available property definition ...
                I18n msg = JcrI18n.unableToSetSingleValuedPropertyUsingMultipleValues;
                throw new javax.jcr.ValueFormatException(msg.text(readable(name), location(), workspaceName()));
            }
            I18n msg = JcrI18n.noPropertyDefinition;
            throw new ConstraintViolationException(msg.text(propName, location(), readable(primaryType), readable(mixinTypes)));
        }

        // The 'findBestPropertyDefintion' method checks constraints for all definitions exception those with a
        // require type of REFERENCE. This is because checking such constraints may cause unnecessary loading of nodes.
        // Therefore, see if this is the case ...
        int requiredType = defn.getRequiredType();
        if (!skipReferenceValidation && (requiredType == PropertyType.REFERENCE || requiredType == PropertyType.WEAKREFERENCE)) {
            // Check that the REFERENCE value satisfies the constraints ...
            if (!defn.canCastToTypeAndSatisfyConstraints(values, session)) {
                // The REFERENCE value did not satisfy the constraints ...
                String propName = readable(name);
                String defnName = defn.getName();
                String nodeTypeName = defn.getDeclaringNodeType().getName();
                I18n i18n = JcrI18n.weakReferenceValueViolatesConstraintsOnDefinition;
                if (requiredType == PropertyType.REFERENCE) i18n = JcrI18n.referenceValueViolatesConstraintsOnDefinition;
                throw new ConstraintViolationException(i18n.text(propName, readable(values), location(), defnName, nodeTypeName));
            }
        }

        if (requiredType == PropertyType.UNDEFINED && values.length > 0) {
            requiredType = values[0].getType();
        }

        // Create the JCR Property object ...
        AbstractJcrProperty jcrProp = new JcrMultiValueProperty(this, name, requiredType);
        jcrProp.setPropertyDefinitionId(defn.getId(), nodeTypes.getVersion());
        AbstractJcrProperty otherProp = this.jcrProperties.putIfAbsent(name, jcrProp);
        if (otherProp != null) {
            // Someone snuck in and created this property while we created ours, so use that instance instead ...
            // Check that the cardinality is correct ...
            if (!jcrProp.isMultiple()) {
                // Overwrite the value anyway ...
                this.jcrProperties.put(name, jcrProp);
            }
            jcrProp = otherProp;
        }

        // The values may need to be converted to the definition's required type ...
        int numValues = values.length;
        Object[] objValues = new Object[numValues];
        int propertyType = defn.getRequiredType();
        if (propertyType == PropertyType.UNDEFINED || propertyType == jcrPropertyType) {
            // Can use the values as is ...
            for (int i = 0; i != numValues; ++i) {
                objValues[i] = ((JcrValue)values[i]).value();
            }
        } else {
            // A conversion is required ...
            try {
                org.modeshape.jcr.value.PropertyType msType = PropertyTypeUtil.modePropertyTypeFor(propertyType);
                org.modeshape.jcr.value.ValueFactory<?> factory = context().getValueFactories().getValueFactory(msType);
                for (int i = 0; i != numValues; ++i) {
                    objValues[i] = factory.create(((JcrValue)values[i]).value());
                }
            } catch (org.modeshape.jcr.value.ValueFormatException e) {
                throw new ValueFormatException(e.getMessage());
            }
        }

        // Set the property on the cached node (even if there was an 'otherProp' instance) ...
        Property newProperty = session.propertyFactory().create(name, objValues);
        node.setProperty(cache, newProperty);
        return jcrProp;
    }
View Full Code Here

    }

    private void autoCreateItemsFor( JcrNodeType nodeType )
        throws InvalidItemStateException, ConstraintViolationException, AccessDeniedException, RepositoryException {

        MutableCachedNode node = mutable();
        SessionCache cache = sessionCache();
        if (nodeType.hasPropertyDefinitions()) {
            for (JcrPropertyDefinition propDefn : nodeType.allPropertyDefinitions()) {
                if (propDefn.isAutoCreated() && !propDefn.isProtected()) {
                    Name propName = propDefn.getInternalName();
                    Property autoCreatedProp = node.getProperty(propName, cache);
                    if (autoCreatedProp == null) {
                        // We have to 'auto-create' the property ...
                        JcrValue[] defaultValues = propDefn.getDefaultValues();
                        if (defaultValues != null) { // may be empty
                            if (propDefn.isMultiple()) {
                                setProperty(propDefn.getInternalName(), defaultValues, propDefn.getRequiredType(), true);
                            } else {
                                assert propDefn.getDefaultValues().length == 1;
                                // don't skip constraint checks or protected checks
                                setProperty(propDefn.getInternalName(), defaultValues[0], false, false, false, false);
                            }
                        }
                        // otherwise, we don't care
                    }
                }
            }
        }

        if (nodeType.hasChildNodeDefinitions()) {
            ChildReferences refs = null;
            for (JcrNodeDefinition nodeDefn : nodeType.allChildNodeDefinitions()) {
                if (nodeDefn.isAutoCreated() && !nodeDefn.isProtected()) {
                    Name nodeName = nodeDefn.getInternalName();
                    if (refs == null) refs = node.getChildReferences(cache);
                    if (refs.getChildCount(nodeName) == 0) {
                        JcrNodeType defaultPrimaryType = nodeDefn.getDefaultPrimaryType();
                        assert defaultPrimaryType != null;
                        Name primaryType = defaultPrimaryType.getInternalName();
                        addChildNode(nodeName, primaryType, null, false, false);
View Full Code Here

        }
        setNodeDefinitionId(childDefn.getId(), nodeTypes.getVersion());

        // Change the primary type property ...
        boolean wasReferenceable = isReferenceable();
        MutableCachedNode mutable = mutable();
        mutable.setProperty(cache, session.propertyFactory().create(JcrLexicon.PRIMARY_TYPE, newPrimaryTypeName));

        if (wasReferenceable && !isReferenceable()) {
            // Need to remove the 'jcr:uuid' reference ...
            mutable.removeProperty(cache, JcrLexicon.UUID);
        } else if (isReferenceable() && !mutable.hasProperty(JcrLexicon.UUID, cache)) {
            mutable.setProperty(cache, session.propertyFactory().create(JcrLexicon.UUID, getIdentifier()));
        }

        // And auto-create any properties that are defined by the new primary type ...
        autoCreateItemsFor(newPrimaryType);
View Full Code Here

TOP

Related Classes of org.modeshape.jcr.cache.MutableCachedNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.