Search in sources :

Example 1 with VersionIterator

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

the class CopyTest method testCopyVersionableNodeClearsVersions.

@Test
public void testCopyVersionableNodeClearsVersions() throws Exception {
    Session session = getAdminSession();
    Node toCopy = session.getNode(TEST_PATH + "/source/node");
    toCopy.addMixin(JcrConstants.MIX_VERSIONABLE);
    session.save();
    Version v1 = toCopy.checkin();
    toCopy.checkout();
    Version v2 = toCopy.checkin();
    toCopy.checkout();
    session.getWorkspace().copy(TEST_PATH + "/source/node", TEST_PATH + "/target/copied");
    Node copy = testNode.getNode("target/copied");
    VersionHistory vh = copy.getVersionHistory();
    Version rootV = vh.getRootVersion();
    assertEquals(0, rootV.getSuccessors().length);
    VersionIterator vItr = vh.getAllVersions();
    while (vItr.hasNext()) {
        if (!rootV.isSame(vItr.nextVersion())) {
            fail("Unexpected version in version history of copied node.");
        }
    }
    try {
        vh.getVersion(v1.getName());
        fail("Unexpected version in version history of copied node.");
    } catch (VersionException e) {
    // success
    }
    try {
        vh.getVersion(v2.getName());
        fail("Unexpected version in version history of copied node.");
    } catch (VersionException e) {
    // success
    }
}
Also used : Version(javax.jcr.version.Version) Node(javax.jcr.Node) VersionIterator(javax.jcr.version.VersionIterator) VersionHistory(javax.jcr.version.VersionHistory) Session(javax.jcr.Session) VersionException(javax.jcr.version.VersionException) Test(org.junit.Test)

Example 2 with VersionIterator

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

the class VersionCopyTestUtils method assertLabeledVersions.

public static void assertLabeledVersions(VersionHistory history) throws RepositoryException {
    final VersionIterator versions = history.getAllVersions();
    // root
    assertFalse(versions.nextVersion().getFrozenNode().hasProperty("version"));
    for (final String label : LABELS) {
        assertEquals(label, versions.nextVersion().getFrozenNode().getProperty("version").getString());
    }
}
Also used : VersionIterator(javax.jcr.version.VersionIterator)

Example 3 with VersionIterator

use of javax.jcr.version.VersionIterator in project kylo by Teradata.

the class FeedVersioningRemovalUpgradeAction method upgradeTo.

@Override
public void upgradeTo(KyloVersion startingVersion) {
    log.info("Removing feed versioning: {}", startingVersion);
    Session session = JcrMetadataAccess.getActiveSession();
    try {
        Node feedsNode = session.getRootNode().getNode("metadata/feeds");
        NodeTypeManager typeMgr = (NodeTypeManager) session.getWorkspace().getNodeTypeManager();
        NodeType currentFeedType = typeMgr.getNodeType("tba:feed");
        List<String> currentSupertypes = Arrays.asList(currentFeedType.getDeclaredSupertypeNames());
        if (currentSupertypes.contains("mix:versionable")) {
            log.info("Removing versionable feed type {} ", currentFeedType);
            // Remove feed version history
            for (Node catNode : JcrUtil.getNodesOfType(feedsNode, "tba:category")) {
                for (Node feedNode : JcrUtil.getNodesOfType(catNode, "tba:feed")) {
                    log.debug("Removing prior versions of feed: {}.{}", catNode.getName(), feedNode.getName());
                    if (JcrVersionUtil.isVersionable(feedNode)) {
                        VersionManager versionManager = session.getWorkspace().getVersionManager();
                        VersionHistory versionHistory = versionManager.getVersionHistory(feedNode.getPath());
                        VersionIterator vIt = versionHistory.getAllVersions();
                        int count = 0;
                        String last = "";
                        while (vIt.hasNext()) {
                            Version version = vIt.nextVersion();
                            String versionName = version.getName();
                            String baseVersion = "";
                            if (!"jcr:rootVersion".equals(versionName)) {
                                // baseVersion requires actual versionable node to get the base version name
                                baseVersion = JcrVersionUtil.getBaseVersion(feedNode).getName();
                            }
                            if (!"jcr:rootVersion".equals(versionName) && !versionName.equalsIgnoreCase(baseVersion)) {
                                last = version.getName();
                                // removeVersion writes directly to workspace, no session.save is necessary
                                versionHistory.removeVersion(version.getName());
                                count++;
                            }
                        }
                        if (count > 0) {
                            log.info("Removed {} versions through {} of feed {}", count, last, feedNode.getName());
                        } else {
                            log.debug("Feed {} had no versions", feedNode.getName());
                        }
                    }
                }
            }
            // Redefine the NodeType of tba:feed to remove versionable but retain the versionable properties with weaker constraints
            // Retaining the properties seems to override some residual properties on feed nodes that causes a failure later.
            // In particular, jcr:predecessors was accessed later but redefining all mix:versionable properties to be safe.
            NodeTypeTemplate template = typeMgr.createNodeTypeTemplate(currentFeedType);
            List<String> newSupertypes = currentSupertypes.stream().filter(type -> !type.equals("mix:versionable")).collect(Collectors.toList());
            template.setDeclaredSuperTypeNames(newSupertypes.toArray(new String[newSupertypes.size()]));
            @SuppressWarnings("unchecked") List<PropertyDefinitionTemplate> propTemplates = template.getPropertyDefinitionTemplates();
            PropertyDefinitionTemplate prop = typeMgr.createPropertyDefinitionTemplate();
            prop.setName("jcr:versionHistory");
            prop.setRequiredType(PropertyType.WEAKREFERENCE);
            propTemplates.add(prop);
            prop = typeMgr.createPropertyDefinitionTemplate();
            prop.setName("jcr:baseVersion");
            prop.setRequiredType(PropertyType.WEAKREFERENCE);
            propTemplates.add(prop);
            prop = typeMgr.createPropertyDefinitionTemplate();
            prop.setName("jcr:predecessors");
            prop.setRequiredType(PropertyType.WEAKREFERENCE);
            prop.setMultiple(true);
            propTemplates.add(prop);
            prop = typeMgr.createPropertyDefinitionTemplate();
            prop.setName("jcr:mergeFailed");
            prop.setRequiredType(PropertyType.WEAKREFERENCE);
            propTemplates.add(prop);
            prop = typeMgr.createPropertyDefinitionTemplate();
            prop.setName("jcr:activity");
            prop.setRequiredType(PropertyType.WEAKREFERENCE);
            propTemplates.add(prop);
            prop = typeMgr.createPropertyDefinitionTemplate();
            prop.setName("jcr:configuration");
            prop.setRequiredType(PropertyType.WEAKREFERENCE);
            propTemplates.add(prop);
            log.info("Replacing the versionable feed type '{}' with a non-versionable type", currentFeedType);
            NodeType newType = typeMgr.registerNodeType(template, true);
            log.info("Replaced with new feed type '{}' with a non-versionable type", newType);
            // This step may not be necessary.
            for (Node catNode : JcrUtil.getNodesOfType(feedsNode, "tba:category")) {
                for (Node feedNode : JcrUtil.getNodesOfType(catNode, "tba:feed")) {
                    feedNode.setPrimaryType(newType.getName());
                    if (feedNode.hasProperty("jcr:predecessors")) {
                        feedNode.getProperty("jcr:predecessors").setValue(new Value[0]);
                        ;
                        feedNode.getProperty("jcr:predecessors").remove();
                    }
                }
            }
        }
    } catch (RepositoryException e) {
        log.error("Failure while attempting to remove versioning from feeds", e);
        throw new UpgradeException("Failure while attempting to remove versioning from feeds", e);
    }
}
Also used : Arrays(java.util.Arrays) VersionHistory(javax.jcr.version.VersionHistory) NodeTypeManager(org.modeshape.jcr.api.nodetype.NodeTypeManager) Version(javax.jcr.version.Version) VersionIterator(javax.jcr.version.VersionIterator) LoggerFactory(org.slf4j.LoggerFactory) RepositoryException(javax.jcr.RepositoryException) JcrVersionUtil(com.thinkbiganalytics.metadata.modeshape.support.JcrVersionUtil) Node(javax.jcr.Node) UpgradeException(com.thinkbiganalytics.server.upgrade.UpgradeException) VersionManager(javax.jcr.version.VersionManager) UpgradeState(com.thinkbiganalytics.server.upgrade.UpgradeState) Logger(org.slf4j.Logger) Session(javax.jcr.Session) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) PropertyType(javax.jcr.PropertyType) Collectors(java.util.stream.Collectors) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) Profile(org.springframework.context.annotation.Profile) List(java.util.List) Component(org.springframework.stereotype.Component) Value(javax.jcr.Value) NodeType(javax.jcr.nodetype.NodeType) JcrUtil(com.thinkbiganalytics.metadata.modeshape.support.JcrUtil) KyloVersion(com.thinkbiganalytics.KyloVersion) JcrMetadataAccess(com.thinkbiganalytics.metadata.modeshape.JcrMetadataAccess) KyloUpgrader(com.thinkbiganalytics.server.upgrade.KyloUpgrader) NodeTypeManager(org.modeshape.jcr.api.nodetype.NodeTypeManager) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) VersionManager(javax.jcr.version.VersionManager) VersionHistory(javax.jcr.version.VersionHistory) UpgradeException(com.thinkbiganalytics.server.upgrade.UpgradeException) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) Version(javax.jcr.version.Version) KyloVersion(com.thinkbiganalytics.KyloVersion) NodeType(javax.jcr.nodetype.NodeType) VersionIterator(javax.jcr.version.VersionIterator) Session(javax.jcr.Session)

Example 4 with VersionIterator

use of javax.jcr.version.VersionIterator in project pentaho-platform by pentaho.

the class DefaultDeleteHelperTest method testVersionHistoryDeleted.

@Test
public void testVersionHistoryDeleted() throws Exception {
    String fileID = "testFileID";
    final Node nodeToRemove = mock(Node.class);
    when(nodeToRemove.getPath()).thenReturn("nodePath");
    when(nodeToRemove.isNodeType(pentahoJcrConstants.getNT_FROZENNODE())).thenReturn(true);
    when(nodeToRemove.isNodeType(pentahoJcrConstants.getPHO_MIX_VERSIONABLE())).thenReturn(true);
    NodeType nt = mock(NodeType.class);
    when(nt.getName()).thenReturn(NameConstants.MIX_VERSIONABLE.getLocalName());
    when(nodeToRemove.getPrimaryNodeType()).thenReturn(nt);
    when(nodeToRemove.getMixinNodeTypes()).thenReturn(new NodeType[] {});
    Property prop1 = mock(Property.class);
    String pho_nt_pentahofile = pentahoJcrConstants.getPHO_NT_PENTAHOFILE();
    when(prop1.toString()).thenReturn(pho_nt_pentahofile);
    when(prop1.getString()).thenReturn(pho_nt_pentahofile);
    when(nodeToRemove.getProperty(pentahoJcrConstants.getJCR_FROZENPRIMARYTYPE())).thenReturn(prop1);
    final PropertyIterator referencesPropertyIterator = mock(PropertyIterator.class);
    when(referencesPropertyIterator.hasNext()).thenReturn(false);
    when(nodeToRemove.getReferences()).thenReturn(referencesPropertyIterator);
    when(session.getNodeByIdentifier(fileID)).thenReturn(nodeToRemove);
    when(session.getNamespacePrefix(anyString())).thenReturn("prefix:");
    Workspace workspace = mock(Workspace.class);
    when(session.getWorkspace()).thenReturn(workspace);
    VersionManager versionManager = mock(VersionManager.class);
    when(workspace.getVersionManager()).thenReturn(versionManager);
    VersionHistory history = mock(VersionHistory.class);
    when(versionManager.getVersionHistory("nodePath")).thenReturn(history);
    VersionIterator versions = mock(VersionIterator.class);
    when(history.getAllVersions()).thenReturn(versions);
    when(versions.hasNext()).thenReturn(true, false);
    Version version = mock(Version.class);
    when(versions.next()).thenReturn(version);
    String value = "Omega Man";
    when(version.getName()).thenReturn(value);
    try {
        defaultDeleteHelper.permanentlyDeleteFile(session, pentahoJcrConstants, fileID);
        verify(versionManager).getVersionHistory(nodeToRemove.getPath());
        verify(nodeToRemove).remove();
        verify(history).removeVersion(value);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : Version(javax.jcr.version.Version) Node(javax.jcr.Node) NodeType(javax.jcr.nodetype.NodeType) PropertyIterator(javax.jcr.PropertyIterator) VersionIterator(javax.jcr.version.VersionIterator) Matchers.anyString(org.mockito.Matchers.anyString) VersionManager(javax.jcr.version.VersionManager) VersionHistory(javax.jcr.version.VersionHistory) Property(javax.jcr.Property) RepositoryFileDaoReferentialIntegrityException(org.pentaho.platform.repository2.unified.exception.RepositoryFileDaoReferentialIntegrityException) RepositoryFileDaoFileExistsException(org.pentaho.platform.repository2.unified.exception.RepositoryFileDaoFileExistsException) RepositoryException(javax.jcr.RepositoryException) Workspace(javax.jcr.Workspace) Test(org.junit.Test)

Example 5 with VersionIterator

use of javax.jcr.version.VersionIterator in project pentaho-platform by pentaho.

the class DefaultDeleteHelperTest method testVersionHistoryDeletedWithParent.

@Test
public void testVersionHistoryDeletedWithParent() throws Exception {
    String fileID = "testFileID";
    final Node nodeToRemove = mock(Node.class);
    when(nodeToRemove.getPath()).thenReturn("nodePath");
    when(nodeToRemove.isNodeType(pentahoJcrConstants.getNT_FROZENNODE())).thenReturn(true);
    NodeType nt = mock(NodeType.class);
    when(nt.getName()).thenReturn(NameConstants.MIX_VERSIONABLE.getLocalName());
    when(nodeToRemove.getPrimaryNodeType()).thenReturn(nt);
    when(nodeToRemove.getMixinNodeTypes()).thenReturn(new NodeType[] {});
    when(nodeToRemove.isNodeType(pentahoJcrConstants.getNT_FROZENNODE())).thenReturn(true);
    when(nodeToRemove.isNodeType(pentahoJcrConstants.getPHO_MIX_VERSIONABLE())).thenReturn(true);
    Property prop1 = mock(Property.class);
    String pho_nt_pentahofile = pentahoJcrConstants.getPHO_NT_PENTAHOFILE();
    when(prop1.toString()).thenReturn(pho_nt_pentahofile);
    when(prop1.getString()).thenReturn(pho_nt_pentahofile);
    when(nodeToRemove.getProperty(pentahoJcrConstants.getJCR_FROZENPRIMARYTYPE())).thenReturn(prop1);
    final PropertyIterator referencesPropertyIterator = mock(PropertyIterator.class);
    when(referencesPropertyIterator.hasNext()).thenReturn(false);
    when(nodeToRemove.getReferences()).thenReturn(referencesPropertyIterator);
    when(session.getNodeByIdentifier(fileID)).thenReturn(nodeToRemove);
    when(session.getNamespacePrefix(anyString())).thenReturn("prefix:");
    Workspace workspace = mock(Workspace.class);
    when(session.getWorkspace()).thenReturn(workspace);
    VersionManager versionManager = mock(VersionManager.class);
    when(workspace.getVersionManager()).thenReturn(versionManager);
    VersionHistory history = mock(VersionHistory.class);
    when(versionManager.getVersionHistory("nodePath")).thenReturn(history);
    VersionIterator versions = mock(VersionIterator.class);
    when(history.getAllVersions()).thenReturn(versions);
    when(versions.hasNext()).thenReturn(true, false);
    Version version = mock(Version.class);
    when(versions.next()).thenReturn(version);
    String value = "Omega Man";
    when(version.getName()).thenReturn(value);
    String parentID = "parent";
    final Node parent = mock(Node.class);
    when(session.getNodeByIdentifier(parentID)).thenReturn(parent);
    when(parent.getPath()).thenReturn("parentNodePath");
    when(parent.isNodeType(pentahoJcrConstants.getNT_FROZENNODE())).thenReturn(true);
    Property prop = mock(Property.class);
    String pho_nt_pentahofolder = pentahoJcrConstants.getPHO_NT_PENTAHOFOLDER();
    when(prop.toString()).thenReturn(pho_nt_pentahofolder);
    when(prop.getString()).thenReturn(pho_nt_pentahofolder);
    when(parent.getProperty(pentahoJcrConstants.getJCR_FROZENPRIMARYTYPE())).thenReturn(prop);
    when(parent.getReferences()).thenReturn(referencesPropertyIterator);
    NodeIterator nodeIterator = mock(NodeIterator.class);
    when(nodeIterator.hasNext()).thenReturn(true, false);
    when(nodeIterator.next()).thenReturn(nodeToRemove);
    when(parent.getNodes()).thenReturn(nodeIterator);
    try {
        defaultDeleteHelper.permanentlyDeleteFile(session, pentahoJcrConstants, parentID);
        verify(versionManager).getVersionHistory(nodeToRemove.getPath());
        verify(parent).remove();
        verify(history).removeVersion(value);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Version(javax.jcr.version.Version) Node(javax.jcr.Node) NodeType(javax.jcr.nodetype.NodeType) PropertyIterator(javax.jcr.PropertyIterator) VersionIterator(javax.jcr.version.VersionIterator) Matchers.anyString(org.mockito.Matchers.anyString) VersionManager(javax.jcr.version.VersionManager) VersionHistory(javax.jcr.version.VersionHistory) Property(javax.jcr.Property) RepositoryFileDaoReferentialIntegrityException(org.pentaho.platform.repository2.unified.exception.RepositoryFileDaoReferentialIntegrityException) RepositoryFileDaoFileExistsException(org.pentaho.platform.repository2.unified.exception.RepositoryFileDaoFileExistsException) RepositoryException(javax.jcr.RepositoryException) Workspace(javax.jcr.Workspace) Test(org.junit.Test)

Aggregations

VersionIterator (javax.jcr.version.VersionIterator)45 Version (javax.jcr.version.Version)31 VersionHistory (javax.jcr.version.VersionHistory)23 Node (javax.jcr.Node)18 RepositoryException (javax.jcr.RepositoryException)12 VersionManager (javax.jcr.version.VersionManager)12 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)10 Session (javax.jcr.Session)8 Resource (org.apache.sling.api.resource.Resource)7 VersionException (javax.jcr.version.VersionException)5 Iterator (java.util.Iterator)4 NodeType (javax.jcr.nodetype.NodeType)4 List (java.util.List)3 NodeIterator (javax.jcr.NodeIterator)3 Workspace (javax.jcr.Workspace)3 DavResourceLocator (org.apache.jackrabbit.webdav.DavResourceLocator)3 JcrDavException (org.apache.jackrabbit.webdav.jcr.JcrDavException)3 KyloVersion (com.thinkbiganalytics.KyloVersion)2 JcrMetadataAccess (com.thinkbiganalytics.metadata.modeshape.JcrMetadataAccess)2