internalversionhistoryimpl.java

来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 513 行 · 第 1/2 页

JAVA
513
字号
    /**     * {@inheritDoc}     */    public InternalVersion getVersionByLabel(QName label) {        return (InternalVersion) labelCache.get(label);    }    /**     * {@inheritDoc}     */    public Iterator getVersions() {        return versionCache.values().iterator();    }    /**     * {@inheritDoc}     */    public int getNumVersions() {        return versionCache.size();    }    /**     * {@inheritDoc}     */    public UUID getVersionableUUID() {        return versionableId.getUUID();    }    /**     * {@inheritDoc}     */    public QName[] getVersionLabels() {        return (QName[]) labelCache.keySet().toArray(new QName[labelCache.size()]);    }    /**     * {@inheritDoc}     */    public NodeId getVersionLabelsId() {        return labelNode.getNodeId();    }    /**     * Removes the indicated version from this VersionHistory. If the specified     * vesion does not exist, if it specifies the root version or if it is     * referenced by any node e.g. as base version, a VersionException is thrown.     * <p/>     * all successors of the removed version become successors of the     * predecessors of the removed version and vice versa. then, the entire     * version node and all its subnodes are removed.     *     * @param versionName     * @throws VersionException     */    void removeVersion(QName versionName) throws RepositoryException {        InternalVersionImpl v = (InternalVersionImpl) getVersion(versionName);        if (v.equals(rootVersion)) {            String msg = "Removal of " + versionName + " not allowed.";            log.debug(msg);            throw new VersionException(msg);        }        // check if any references (from outside the version storage) exist on this version        List refs = vMgr.getItemReferences(v);        if (!refs.isEmpty()) {            throw new ReferentialIntegrityException("Unable to remove version. At least once referenced.");        }        // unregister from labels        QName[] labels = v.internalGetLabels();        for (int i = 0; i < labels.length; i++) {            v.internalRemoveLabel(labels[i]);            labelNode.removeProperty(labels[i]);        }        // detach from the version graph        v.internalDetach();        // remove from persistance state        node.removeNode(v.getName());        // and remove from history        versionCache.remove(v.getId());        vMgr.versionDestroyed(v);        // store changes        node.store();    }    /**     * Sets the version <code>label</code> to the given <code>version</code>.     * If the label is already assigned to another version, a VersionException is     * thrown unless <code>move</code> is <code>true</code>. If <code>version</code>     * is <code>null</code>, the label is removed from the respective version.     * In either case, the version the label was previously assigned to is returned,     * or <code>null</code> of the label was not moved.     *     * @param versionName the name of the version     * @param label the label to assgign     * @param move  flag what to do by collisions     * @return the version that was previously assigned by this label or <code>null</code>.     * @throws VersionException     */    InternalVersion setVersionLabel(QName versionName, QName label, boolean move)            throws VersionException {        InternalVersion version =            (versionName != null) ? getVersion(versionName) : null;        if (versionName != null && version == null) {            throw new VersionException("Version " + versionName + " does not exist in this version history.");        }        InternalVersionImpl prev = (InternalVersionImpl) labelCache.get(label);        if (prev == null) {            if (version == null) {                return null;            }        } else {            if (prev.equals(version)) {                return version;            } else if (!move) {                // already defined elsewhere, throw                throw new VersionException("Version label " + label + " already defined for version " + prev.getName());            }        }        // update persistence        try {            if (version == null) {                labelNode.removeProperty(label);            } else {                labelNode.setPropertyValue(label, InternalValue.create(version.getId().getUUID()));            }            labelNode.store();        } catch (RepositoryException e) {            throw new VersionException(e);        }        // update internal structures        if (prev != null) {            prev.internalRemoveLabel(label);            labelCache.remove(label);        }        if (version != null) {            labelCache.put(label, version);            ((InternalVersionImpl) version).internalAddLabel(label);        }        return prev;    }    /**     * Checks in a node. It creates a new version with the given name and freezes     * the state of the given node.     *     * @param name     * @param src     * @return     * @throws RepositoryException     */    InternalVersionImpl checkin(QName name, NodeImpl src)            throws RepositoryException {        // copy predecessors from src node        Value[] preds = src.getProperty(QName.JCR_PREDECESSORS).getValues();        InternalValue[] predecessors = new InternalValue[preds.length];        for (int i = 0; i < preds.length; i++) {            UUID predId = UUID.fromString(preds[i].getString());            // check if version exist            if (!versionCache.containsKey(new NodeId(predId))) {                throw new RepositoryException("invalid predecessor in source node");            }            predecessors[i] = InternalValue.create(predId);        }        NodeId versionId = new NodeId(UUID.randomUUID());        NodeStateEx vNode = node.addNode(name, QName.NT_VERSION, versionId, true);        // initialize 'created', 'predecessors' and 'successors'        vNode.setPropertyValue(QName.JCR_CREATED, InternalValue.create(Calendar.getInstance()));        vNode.setPropertyValues(QName.JCR_PREDECESSORS, PropertyType.REFERENCE, predecessors);        vNode.setPropertyValues(QName.JCR_SUCCESSORS, PropertyType.REFERENCE, InternalValue.EMPTY_ARRAY);        // checkin source node        InternalFrozenNodeImpl.checkin(vNode, QName.JCR_FROZENNODE, src);        // update version graph        InternalVersionImpl version = new InternalVersionImpl(this, vNode, name);        version.internalAttach();        // and store        node.store();        vMgr.versionCreated(version);        // update cache        versionCache.put(version.getId(), version);        return version;    }    /**     * Creates a new <code>InternalVersionHistory</code> below the given parent     * node and with the given name.     *     * @param parent     * @param name     * @return     * @throws RepositoryException     */    static InternalVersionHistoryImpl create(AbstractVersionManager vMgr,                                             NodeStateEx parent,                                             NodeId historyId, QName name,                                             NodeState nodeState)            throws RepositoryException {        // create history node        NodeStateEx pNode = parent.addNode(name, QName.NT_VERSIONHISTORY, historyId, true);        // set the versionable uuid        String versionableUUID = nodeState.getNodeId().getUUID().toString();        pNode.setPropertyValue(QName.JCR_VERSIONABLEUUID, InternalValue.create(versionableUUID));        // create label node        pNode.addNode(QName.JCR_VERSIONLABELS, QName.NT_VERSIONLABELS, null, false);        // create root version        NodeId versionId = new NodeId(UUID.randomUUID());        NodeStateEx vNode = pNode.addNode(QName.JCR_ROOTVERSION, QName.NT_VERSION, versionId, true);        // initialize 'created' and 'predecessors'        vNode.setPropertyValue(QName.JCR_CREATED, InternalValue.create(Calendar.getInstance()));        vNode.setPropertyValues(QName.JCR_PREDECESSORS, PropertyType.REFERENCE, InternalValue.EMPTY_ARRAY);        vNode.setPropertyValues(QName.JCR_SUCCESSORS, PropertyType.REFERENCE, InternalValue.EMPTY_ARRAY);        // add also an empty frozen node to the root version        NodeStateEx node = vNode.addNode(QName.JCR_FROZENNODE, QName.NT_FROZENNODE, null, true);        // initialize the internal properties        node.setPropertyValue(QName.JCR_FROZENUUID, InternalValue.create(versionableUUID));        node.setPropertyValue(QName.JCR_FROZENPRIMARYTYPE,                InternalValue.create(nodeState.getNodeTypeName()));        Set mixins = nodeState.getMixinTypeNames();        if (mixins.size() > 0) {            InternalValue[] ivalues = new InternalValue[mixins.size()];            Iterator iter = mixins.iterator();            for (int i = 0; i < mixins.size(); i++) {                ivalues[i] = InternalValue.create((QName) iter.next());            }            node.setPropertyValues(QName.JCR_FROZENMIXINTYPES, PropertyType.NAME, ivalues);        }        parent.store();        return new InternalVersionHistoryImpl(vMgr, pNode);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?