Search in sources :

Example 71 with VersionManager

use of javax.jcr.version.VersionManager in project jackrabbit-oak by apache.

the class VersionedDocumentBundlingTest method restoreVersionedNode.

@Test
public void restoreVersionedNode() throws Exception {
    String assetParentPath = "/bundlingtest/par";
    Node asset = JcrUtils.getOrCreateByPath(assetParentPath + "/foo.png", "oak:Unstructured", "oak:Asset", s, false);
    Node assetParent = s.getNode(assetParentPath);
    assetParent.addMixin(JcrConstants.MIX_VERSIONABLE);
    asset.addNode("jcr:content", "oak:Unstructured");
    s.save();
    VersionManager vm = s.getWorkspace().getVersionManager();
    Version version = vm.checkin(assetParentPath);
    vm.checkout(assetParentPath);
    asset.getNode("jcr:content").setProperty("foo1", "bar1");
    s.save();
    vm.restore(version, true);
}
Also used : Version(javax.jcr.version.Version) Node(javax.jcr.Node) VersionManager(javax.jcr.version.VersionManager) Test(org.junit.Test)

Example 72 with VersionManager

use of javax.jcr.version.VersionManager in project jackrabbit-oak by apache.

the class CopyVersionHistoryTest method assertVersionableProperties.

private static void assertVersionableProperties(final Session session, final String... names) throws RepositoryException {
    VersionManager vMgr = session.getWorkspace().getVersionManager();
    for (final String mixin : MIXINS) {
        final String pathPrefix = VERSIONABLES_PATH_PREFIX + mixin + "/";
        for (final String name : names) {
            final String path = pathPrefix + name;
            Node versionable = session.getNode(path);
            String versionHistoryUuid = versionable.getProperty(JCR_VERSIONHISTORY).getString();
            assertEquals(getVersionHistoryForPath(session, path).getIdentifier(), versionHistoryUuid);
            final Version baseVersion = vMgr.getBaseVersion(path);
            assertEquals("1.2", baseVersion.getName());
            final Value[] predecessors = versionable.getProperty(JCR_PREDECESSORS).getValues();
            assertEquals(1, predecessors.length);
            assertEquals(baseVersion.getIdentifier(), predecessors[0].getString());
        }
    }
}
Also used : Version(javax.jcr.version.Version) Node(javax.jcr.Node) Value(javax.jcr.Value) VersionManager(javax.jcr.version.VersionManager)

Example 73 with VersionManager

use of javax.jcr.version.VersionManager in project jackrabbit-oak by apache.

the class BrokenVersionableTest method createSourceContent.

private NodeStore createSourceContent() throws Exception {
    SegmentNodeStore source = SegmentNodeStoreBuilders.builder(new MemoryStore()).build();
    RepositoryImpl repository = (RepositoryImpl) new Jcr(new Oak(source)).createRepository();
    Session session = repository.login(CREDENTIALS);
    List<String> versionHistoryPaths = new ArrayList<String>();
    try {
        CndImporter.registerNodeTypes(new StringReader("<test = 'http://jackrabbit.apache.org/ns/test'>\n" + "[test:Versionable] > nt:unstructured, mix:versionable"), session);
        Node root = session.getRootNode();
        Node versionable1 = root.addNode("versionable1", NT_UNSTRUCTURED);
        versionable1.addMixin(MIX_VERSIONABLE);
        versionable1.addNode("child", NT_UNSTRUCTURED);
        Node versionable2 = root.addNode("versionable2", "test:Versionable");
        versionable2.addNode("child", NT_UNSTRUCTURED);
        session.save();
        VersionManager vMgr = session.getWorkspace().getVersionManager();
        vMgr.checkin("/versionable1");
        vMgr.checkin("/versionable2");
        versionHistoryPaths.add(vMgr.getVersionHistory("/versionable1").getPath());
        versionHistoryPaths.add(vMgr.getVersionHistory("/versionable2").getPath());
    } finally {
        session.logout();
        repository.shutdown();
    }
    // remove version history to corrupt the JCR repository structure
    NodeBuilder rootBuilder = source.getRoot().builder();
    for (String versionHistoryPath : versionHistoryPaths) {
        NodeStateTestUtils.createOrGetBuilder(rootBuilder, versionHistoryPath).remove();
    }
    source.merge(rootBuilder, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    return source;
}
Also used : Node(javax.jcr.Node) ArrayList(java.util.ArrayList) VersionManager(javax.jcr.version.VersionManager) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) MemoryStore(org.apache.jackrabbit.oak.segment.memory.MemoryStore) RepositoryImpl(org.apache.jackrabbit.oak.jcr.repository.RepositoryImpl) Jcr(org.apache.jackrabbit.oak.jcr.Jcr) Oak(org.apache.jackrabbit.oak.Oak) StringReader(java.io.StringReader) SegmentNodeStore(org.apache.jackrabbit.oak.segment.SegmentNodeStore) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession)

Example 74 with VersionManager

use of javax.jcr.version.VersionManager in project jackrabbit by apache.

the class RemoveVersionTest method testRemoveAllVersions.

/**
     * Creates 3 versions and removes them afterwards. Checks if version history
     * was purged, too.
     *
     * Tests error reported in JCR-2601
     *
     * @throws Exception if an error occurs
     */
public void testRemoveAllVersions() throws Exception {
    Node n = testRootNode.addNode(nodeName1);
    n.addMixin(mixVersionable);
    superuser.save();
    String path = n.getPath();
    // create some versions
    VersionManager mgr = superuser.getWorkspace().getVersionManager();
    // v1.0
    mgr.checkpoint(path);
    // v1.1
    mgr.checkpoint(path);
    // v1.2
    mgr.checkpoint(path);
    // get version history
    VersionHistory vh = mgr.getVersionHistory(path);
    String id = vh.getIdentifier();
    // remove versionable node
    n.remove();
    superuser.save();
    // get the names of the versions
    List<String> names = new LinkedList<String>();
    VersionIterator vit = vh.getAllVersions();
    while (vit.hasNext()) {
        Version v = vit.nextVersion();
        if (!v.getName().equals("jcr:rootVersion")) {
            names.add(v.getName());
        }
    }
    assertEquals("Number of versions", 3, names.size());
    // remove all versions
    for (String name : names) {
        vh.removeVersion(name);
    }
    // assert that version history is gone
    try {
        superuser.getNodeByIdentifier(id);
        fail("Version history not removed after last version was removed.");
    } catch (RepositoryException e) {
    // ok
    }
}
Also used : Version(javax.jcr.version.Version) Node(javax.jcr.Node) VersionIterator(javax.jcr.version.VersionIterator) RepositoryException(javax.jcr.RepositoryException) VersionManager(javax.jcr.version.VersionManager) VersionHistory(javax.jcr.version.VersionHistory) LinkedList(java.util.LinkedList)

Example 75 with VersionManager

use of javax.jcr.version.VersionManager in project jackrabbit by apache.

the class VersionControlledItemCollection method resolveMergeConflict.

/**
     * Resolve one or multiple merge conflicts present on this resource. Please
     * note that the 'setProperties' or 'removeProperties' set my contain additional
     * resource properties, that need to be changed. Those properties are left
     * untouched, whereas the {@link #AUTO_MERGE_SET DAV:auto-merge-set}, is
     * removed from the list upon successful resolution of a merge conflict.<br>
     * If the removeProperties or setProperties set do not contain the mentioned
     * merge conflict resource properties or if the value of those properties do
     * not allow for a resolution of an existing merge conflict, this method
     * returns silently.
     *
     * @param changeList
     * @throws org.apache.jackrabbit.webdav.DavException
     * @see Node#doneMerge(Version)
     * @see Node#cancelMerge(Version)
     */
private void resolveMergeConflict(List<? extends PropEntry> changeList) throws DavException {
    if (!exists()) {
        throw new DavException(DavServletResponse.SC_NOT_FOUND);
    }
    try {
        Node n = (Node) item;
        VersionManager vMgr = getVersionManager();
        String path = item.getPath();
        DavProperty<?> autoMergeSet = null;
        DavProperty<?> predecessorSet = null;
        /* find DAV:auto-merge-set entries. If none exists no attempt is made
               to resolve merge conflict > return silently */
        for (int i = 0; i < changeList.size(); i++) {
            PropEntry propEntry = changeList.get(i);
            // conflicts are resolved with 'cancel'
            if (propEntry instanceof DavPropertyName && AUTO_MERGE_SET.equals(propEntry)) {
                // retrieve the current jcr:mergeFailed property values
                if (!n.hasProperty(JcrConstants.JCR_MERGEFAILED)) {
                    throw new DavException(DavServletResponse.SC_CONFLICT, "Attempt to resolve non-existing merge conflicts.");
                }
                Value[] mergeFailed = n.getProperty(JcrConstants.JCR_MERGEFAILED).getValues();
                for (Value value : mergeFailed) {
                    vMgr.cancelMerge(path, (Version) getRepositorySession().getNodeByIdentifier(value.getString()));
                }
                // remove this entry from the changeList
                changeList.remove(propEntry);
            } else if (propEntry instanceof DavProperty) {
                if (AUTO_MERGE_SET.equals(((DavProperty<?>) propEntry).getName())) {
                    autoMergeSet = (DavProperty<?>) propEntry;
                } else if (PREDECESSOR_SET.equals(((DavProperty<?>) propEntry).getName())) {
                    predecessorSet = (DavProperty<?>) propEntry;
                }
            }
        }
        // resolved individually according to the DAV:predecessor-set property.
        if (autoMergeSet != null) {
            // retrieve the current jcr:mergeFailed property values
            if (!n.hasProperty(JcrConstants.JCR_MERGEFAILED)) {
                throw new DavException(DavServletResponse.SC_CONFLICT, "Attempt to resolve non-existing merge conflicts.");
            }
            List<String> mergeset = new HrefProperty(autoMergeSet).getHrefs();
            List<String> predecL;
            if (predecessorSet == null) {
                predecL = Collections.emptyList();
            } else {
                predecL = new HrefProperty(predecessorSet).getHrefs();
            }
            Session session = getRepositorySession();
            // loop over the mergeFailed values (versions) and test whether they are
            // removed from the DAV:auto-merge-set thus indicating resolution.
            Value[] mergeFailed = n.getProperty(JcrConstants.JCR_MERGEFAILED).getValues();
            for (Value value : mergeFailed) {
                // build version-href from each entry in the jcr:mergeFailed property
                // in order to be able to compare to the entries in the HrefProperty.
                Version version = (Version) session.getNodeByIdentifier(value.getString());
                String href = getLocatorFromItem(version).getHref(true);
                // thus indicating that this merge conflict needs to be resolved.
                if (!mergeset.contains(href)) {
                    // must be called.
                    if (predecL.contains(href)) {
                        vMgr.doneMerge(path, version);
                    } else {
                        vMgr.cancelMerge(path, version);
                    }
                }
            }
            // after successful resolution of merge-conflicts according to
            // DAV:auto-merge-set and DAV:predecessor-set remove these entries
            // from the changeList.
            changeList.remove(autoMergeSet);
            if (predecessorSet != null) {
                changeList.remove(predecessorSet);
            }
        }
    } catch (RepositoryException e) {
        throw new JcrDavException(e);
    }
}
Also used : DavException(org.apache.jackrabbit.webdav.DavException) Node(javax.jcr.Node) DefaultDavProperty(org.apache.jackrabbit.webdav.property.DefaultDavProperty) DavProperty(org.apache.jackrabbit.webdav.property.DavProperty) RepositoryException(javax.jcr.RepositoryException) VersionManager(javax.jcr.version.VersionManager) DavPropertyName(org.apache.jackrabbit.webdav.property.DavPropertyName) PropEntry(org.apache.jackrabbit.webdav.property.PropEntry) HrefProperty(org.apache.jackrabbit.webdav.property.HrefProperty) Version(javax.jcr.version.Version) Value(javax.jcr.Value) Session(javax.jcr.Session)

Aggregations

VersionManager (javax.jcr.version.VersionManager)154 Node (javax.jcr.Node)97 Version (javax.jcr.version.Version)65 VersionHistory (javax.jcr.version.VersionHistory)17 RepositoryException (javax.jcr.RepositoryException)11 Value (javax.jcr.Value)10 Test (org.junit.Test)9 Property (javax.jcr.Property)8 Session (javax.jcr.Session)8 NodeIterator (javax.jcr.NodeIterator)7 VersionException (javax.jcr.version.VersionException)5 ItemNotFoundException (javax.jcr.ItemNotFoundException)4 MergeException (javax.jcr.MergeException)4 UnsupportedRepositoryOperationException (javax.jcr.UnsupportedRepositoryOperationException)4 VersionIterator (javax.jcr.version.VersionIterator)4 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 NodeDefinitionTemplate (javax.jcr.nodetype.NodeDefinitionTemplate)3 InvalidItemStateException (javax.jcr.InvalidItemStateException)2