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
}
}
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());
}
}
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);
}
}
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();
}
}
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();
}
}
Aggregations