abstractversionmanager.java

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

JAVA
527
字号
            throws RepositoryException;    /**     * Return a flag indicating if the item specified exists.     * Subclass responsibility.     * @param id the id of the item     * @return <code>true</code> if the item exists;     *         <code>false</code> otherwise     */    protected abstract boolean hasItem(NodeId id);    /**     * Returns the item references that reference the given version item.     * Subclass responsiblity.     * <p/>     * Please note, that the overridden method must aquire the readlock before     * reading the state manager.     *     * @param item version item     * @return list of item references, may be empty.     */    protected abstract List getItemReferences(InternalVersionItem item);    /**     * Creates a new Version History.     *     * @param node the node for which the version history is to be initialized     * @return the newly created version history.     * @throws javax.jcr.RepositoryException     */    InternalVersionHistory createVersionHistory(NodeState node)            throws RepositoryException {        WriteOperation operation = startWriteOperation();        try {            // create deep path            String uuid = node.getNodeId().getUUID().toString();            NodeStateEx root = historyRoot;            for (int i = 0; i < 3; i++) {                QName name = new QName(QName.NS_DEFAULT_URI, uuid.substring(i * 2, i * 2 + 2));                if (!root.hasNode(name)) {                    root.addNode(name, QName.REP_VERSIONSTORAGE, null, false);                    root.store();                }                root = root.getNode(name, 1);            }            QName historyNodeName = new QName(QName.NS_DEFAULT_URI, uuid);            if (root.hasNode(historyNodeName)) {                // already exists                return null;            }            // create new history node in the persistent state            InternalVersionHistoryImpl hist = InternalVersionHistoryImpl.create(                    this, root, new NodeId(UUID.randomUUID()), historyNodeName, node);            // end update            operation.save();            log.debug("Created new version history " + hist.getId() + " for " + node + ".");            return hist;        } catch (ItemStateException e) {            throw new RepositoryException(e);        } finally {            operation.close();        }    }    /**     * Returns the id of the version history associated with the given node     * or <code>null</code> if that node doesn't have a version history.     *     * @param node the node whose version history's id is to be returned.     * @return the the id of the version history associated with the given node     *         or <code>null</code> if that node doesn't have a version history.     * @throws javax.jcr.RepositoryException if an error occurs     */    private NodeId getVersionHistoryId(NodeState node)            throws RepositoryException {        // build and traverse path        String uuid = node.getNodeId().getUUID().toString();        NodeStateEx n = historyRoot;        for (int i = 0; i < 3; i++) {            QName name = new QName(QName.NS_DEFAULT_URI, uuid.substring(i * 2, i * 2 + 2));            if (!n.hasNode(name)) {                return null;            }            n = n.getNode(name, 1);        }        QName historyNodeName = new QName(QName.NS_DEFAULT_URI, uuid);        if (!n.hasNode(historyNodeName)) {            return null;        }        return n.getNode(historyNodeName, 1).getNodeId();    }    /**     * Checks in a node     *     * @param history the version history     * @param node node to checkin     * @return internal version     * @throws javax.jcr.RepositoryException if an error occurs     * @see javax.jcr.Node#checkin()     */    protected InternalVersion checkin(InternalVersionHistoryImpl history, NodeImpl node)            throws RepositoryException {        WriteOperation operation = startWriteOperation();        try {            String versionName = calculateCheckinVersionName(history, node);            InternalVersionImpl v = history.checkin(new QName("", versionName), node);            operation.save();            return v;        } catch (ItemStateException e) {            throw new RepositoryException(e);        } finally {            operation.close();        }    }    /**     * Calculates the name of the new version that will be created by a     * checkin call. The name is determined as follows:     * <ul>     * <li> first the predecessor version with the shortes name is searched.     * <li> if that predecessor version is the root version, the new version gets     *      the name "{number of successors}+1" + ".0"     * <li> if that predecessor version has no successor, the last digit of it's     *      version number is incremented.     * <li> if that predecessor version has successors but the incremented name     *      does not exist, that name is used.     * <li> otherwise a ".0" is added to the name until a non conflicting name     *      is found.     * <ul>     *     * Example Graph:     * <xmp>     * jcr:rootVersion     *  |     |     * 1.0   2.0     *  |     * 1.1     *  |     * 1.2 ---\  ------\     *  |      \        \     * 1.3   1.2.0   1.2.0.0     *  |      |     * 1.4   1.2.1 ----\     *  |      |        \     * 1.5   1.2.2   1.2.1.0     *  |      |        |     * 1.6     |     1.2.1.1     *  |-----/     * 1.7     * </xmp>     *     * @param history the version history     * @param node the node to checkin     * @return the new version name     * @throws RepositoryException if an error occurs.     */    protected String calculateCheckinVersionName(InternalVersionHistoryImpl history,                                                 NodeImpl node)            throws RepositoryException {        // 1. search a predecessor, suitable for generating the new name        Value[] values = node.getProperty(QName.JCR_PREDECESSORS).getValues();        InternalVersion best = null;        for (int i = 0; i < values.length; i++) {            InternalVersion pred = history.getVersion(NodeId.valueOf(values[i].getString()));            if (best == null                    || pred.getName().getLocalName().length() < best.getName().getLocalName().length()) {                best = pred;            }        }        // 2. generate version name (assume no namespaces in version names)        String versionName = best.getName().getLocalName();        int pos = versionName.lastIndexOf('.');        if (pos > 0) {            String newVersionName = versionName.substring(0, pos + 1)                + (Integer.parseInt(versionName.substring(pos + 1)) + 1);            while (history.hasVersion(new QName("", newVersionName))) {                versionName += ".0";                newVersionName = versionName;            }            return newVersionName;        } else {            // best is root version            return String.valueOf(best.getSuccessors().length + 1) + ".0";        }    }    /**     * Removes the specified version from the history     *     * @param history the version history from where to remove the version.     * @param name the name of the version to remove.     * @throws javax.jcr.version.VersionException if the version <code>history</code> does     *  not have a version with <code>name</code>.     * @throws javax.jcr.RepositoryException if any other error occurs.     */    protected void removeVersion(InternalVersionHistoryImpl history, QName name)            throws VersionException, RepositoryException {        WriteOperation operation = startWriteOperation();        try {            history.removeVersion(name);            operation.save();        } catch (ItemStateException e) {            log.error("Error while storing: " + e.toString());        } finally {            operation.close();        }    }    /**     * Set version label on the specified version.     * @param history version history     * @param version version name     * @param label version label     * @param move <code>true</code> to move from existing version;     *             <code>false</code> otherwise     * @throws RepositoryException if an error occurs     */    protected InternalVersion setVersionLabel(InternalVersionHistoryImpl history,                                              QName version, QName label,                                              boolean move)            throws RepositoryException {        WriteOperation operation = startWriteOperation();        try {            InternalVersion v = history.setVersionLabel(version, label, move);            operation.save();            return v;        } catch (ItemStateException e) {            log.error("Error while storing: " + e.toString());            return null;        } finally {            operation.close();        }    }    /**     * Invoked when a new internal item has been created.     * @param version internal version item     */    protected void versionCreated(InternalVersion version) {    }    /**     * Invoked when a new internal item has been destroyed.     * @param version internal version item     */    protected void versionDestroyed(InternalVersion version) {    }    /**     * Invoked by the internal version item itself, when it's underlying     * persistence state was discarded.     *     * @param item     */    protected void itemDiscarded(InternalVersionItem item) {    }}

⌨️ 快捷键说明

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