use of javax.jcr.version.VersionIterator in project jackrabbit by apache.
the class VersionHistoryTest method testInitiallyGetAllLinearVersionsContainsTheRootAndTheBaseVersion.
/**
* Test if the iterator returned by {@link javax.jcr.version.VersionHistory#getAllLinearVersions()}
* contains both the root and the base version upon creation of the version history.
* @since JCR 2.0
*/
public void testInitiallyGetAllLinearVersionsContainsTheRootAndTheBaseVersion() throws RepositoryException {
VersionManager vm = versionableNode.getSession().getWorkspace().getVersionManager();
List<String> lvh = new ArrayList<String>();
for (VersionIterator it = vHistory.getAllLinearVersions(); it.hasNext(); ) {
lvh.add(it.nextVersion().getName());
}
String rootVersion = vm.getVersionHistory(versionableNode.getPath()).getRootVersion().getName();
String baseVersion = vm.getBaseVersion(versionableNode.getPath()).getName();
assertTrue("root version " + rootVersion + " must be part of the linear version history: " + lvh, lvh.contains(rootVersion));
assertTrue("base version " + baseVersion + " must be part of the linear version history: " + lvh, lvh.contains(baseVersion));
}
use of javax.jcr.version.VersionIterator in project jackrabbit by apache.
the class RestoreTest method testRestoreInvalidVersion2.
/**
* VersionException expected on Node.restore(String, boolean) if the specified version is not part of this node's version history.
*
* @throws RepositoryException
*/
@SuppressWarnings("deprecation")
public void testRestoreInvalidVersion2() throws RepositoryException {
String invalidName;
do {
invalidName = createRandomString(3);
for (VersionIterator it = versionableNode.getVersionHistory().getAllVersions(); it.hasNext(); ) {
Version v = it.nextVersion();
if (invalidName.equals(v.getName())) {
invalidName = null;
break;
}
}
} while (invalidName == null);
try {
versionableNode.restore(invalidName, true);
fail("VersionException expected on Node.restore(String, boolean) if the specified version is not part of this node's version history.");
} catch (VersionException e) {
// ok
}
}
use of javax.jcr.version.VersionIterator in project jackrabbit by apache.
the class VersionHistoryTest method testGetAllVersions.
/**
* Test that {@link VersionHistory#getAllVersions()} returns an iterator
* containing the root version and all versions that have been created by
* Node.checkin().
*
* @see javax.jcr.version.VersionHistory#getAllVersions()
*/
@SuppressWarnings("deprecation")
public void testGetAllVersions() throws RepositoryException {
int cnt = 5;
Map<String, Version> versions = new HashMap<String, Version>();
Version v = vHistory.getRootVersion();
versions.put(v.getUUID(), v);
for (int i = 0; i < cnt; i++) {
v = versionableNode.checkin();
versions.put(v.getUUID(), v);
versionableNode.checkout();
}
VersionIterator it = vHistory.getAllVersions();
while (it.hasNext()) {
v = it.nextVersion();
if (!versions.containsKey(v.getUUID())) {
fail("VersionHistory.getAllVersions() must only contain the root version and versions, that have been created by a Node.checkin() call.");
}
versions.remove(v.getUUID());
}
assertTrue("VersionHistory.getAllVersions() must contain the root version and all versions that have been created with a Node.checkin() call.", versions.isEmpty());
}
use of javax.jcr.version.VersionIterator in project pentaho-platform by pentaho.
the class JcrCmsOutputHandler method getFileOutputContentItem.
@Override
public IContentItem getFileOutputContentItem() {
String contentName = getContentRef();
try {
Repository repository = getRepository();
if (repository == null) {
Logger.error(JcrCmsOutputHandler.class.getName(), Messages.getInstance().getString(// $NON-NLS-1$
"JcrCmsOutputHandler.ERROR_0001_GETTING_CMSREPO"));
return null;
}
Session jcrSession = getJcrSession(repository);
if (jcrSession == null) {
Logger.error(JcrCmsOutputHandler.class.getName(), Messages.getInstance().getString(// $NON-NLS-1$
"JcrCmsOutputHandler.ERROR_0002_GETTING_SESSION"));
return null;
}
// Use the root node as a starting point
Node root = jcrSession.getRootNode();
if (root == null) {
Logger.error(JcrCmsOutputHandler.class.getName(), Messages.getInstance().getString(// $NON-NLS-1$
"JcrCmsOutputHandler.ERROR_0003_GETTING_ROOT"));
return null;
}
Node node = root;
// parse the path
// $NON-NLS-1$
StringTokenizer tokenizer = new StringTokenizer(contentName, "/");
int levels = tokenizer.countTokens();
for (int idx = 0; idx < levels - 1; idx++) {
String folder = tokenizer.nextToken();
if (!node.hasNode(folder)) {
// Create an unstructured node under which to import the XML
// $NON-NLS-1$
node = node.addNode(folder, "nt:folder");
} else {
node = node.getNodes(folder).nextNode();
}
}
// we should be at the right level now
String fileName = tokenizer.nextToken();
Node fileNode = null;
Node contentNode = null;
Version version = null;
if (node.hasNode(fileName)) {
fileNode = node.getNode(fileName);
// $NON-NLS-1$
contentNode = fileNode.getNode("jcr:content");
if (contentNode.isLocked()) {
JcrCmsOutputHandler.logger.warn(Messages.getInstance().getString("JcrCmsOutputHandler.ERROR_0004_NODE_LOCKED", // $NON-NLS-1$
contentName));
return null;
}
if (contentNode.isCheckedOut()) {
JcrCmsOutputHandler.logger.warn(Messages.getInstance().getString("JcrCmsOutputHandler.ERROR_0005_NODE_CHECKED_OUT", // $NON-NLS-1$
contentName));
return null;
}
contentNode.checkout();
VersionHistory history = contentNode.getVersionHistory();
VersionIterator iterator = history.getAllVersions();
while (iterator.hasNext()) {
version = iterator.nextVersion();
JcrCmsOutputHandler.logger.trace(version.getPath() + "," + version.getName() + "," + version.getIndex() + "," + // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
version.getCreated().toString());
}
} else {
// $NON-NLS-1$
fileNode = node.addNode(fileName, "nt:file");
// $NON-NLS-1$
fileNode.addMixin("mix:versionable");
// create the mandatory child node - jcr:content
// $NON-NLS-1$ //$NON-NLS-2$
contentNode = fileNode.addNode("jcr:content", "nt:resource");
// $NON-NLS-1$
contentNode.addMixin("mix:versionable");
// $NON-NLS-1$
contentNode.addMixin("mix:filename");
// $NON-NLS-1$
contentNode.setProperty("jcr:mimeType", getMimeType());
// $NON-NLS-1$
contentNode.setProperty("jcr:name", fileName);
// $NON-NLS-1$
contentNode.setProperty("jcr:encoding", LocaleHelper.getSystemEncoding());
}
CmsContentListener listener = new CmsContentListener(contentNode, jcrSession);
BufferedContentItem contentItem = new BufferedContentItem(listener);
listener.setContentItem(contentItem);
if (false) {
// Disable faked search for now
// $NON-NLS-1$
search("test", jcrSession);
}
return contentItem;
} catch (LockException le) {
Logger.error(JcrCmsOutputHandler.class.getName(), Messages.getInstance().getString("JcrCmsOutputHandler.ERROR_0006_GETTING_OUTPUTHANDLER") + contentName, // $NON-NLS-1$
le);
} catch (NestableRuntimeException nre) {
Logger.error(JcrCmsOutputHandler.class.getName(), Messages.getInstance().getString("JcrCmsOutputHandler.ERROR_0006_GETTING_OUTPUTHANDLER") + contentName, // $NON-NLS-1$
nre);
} catch (RepositoryException re) {
Logger.error(JcrCmsOutputHandler.class.getName(), Messages.getInstance().getString("JcrCmsOutputHandler.ERROR_0006_GETTING_OUTPUTHANDLER") + contentName, // $NON-NLS-1$
re);
}
return null;
}
use of javax.jcr.version.VersionIterator in project pentaho-platform by pentaho.
the class DefaultDeleteHelper method purgeHistory.
private void purgeHistory(Node fileNode, Session session, PentahoJcrConstants pentahoJcrConstants) throws RepositoryException {
// Delete all previous versions of this node
VersionManager versionManager = session.getWorkspace().getVersionManager();
if (JcrRepositoryFileUtils.isPentahoFolder(pentahoJcrConstants, fileNode)) {
// go down to children
NodeIterator nodes = fileNode.getNodes();
while (nodes.hasNext()) {
Node next = (Node) nodes.next();
purgeHistory(next, session, pentahoJcrConstants);
}
} else if (JcrRepositoryFileUtils.isPentahoFile(pentahoJcrConstants, fileNode) && fileNode.isNodeType(pentahoJcrConstants.getPHO_MIX_VERSIONABLE())) {
VersionHistory versionHistory = versionManager.getVersionHistory(fileNode.getPath());
VersionIterator allVersions = versionHistory.getAllVersions();
while (allVersions.hasNext()) {
Version next = (Version) allVersions.next();
String name = next.getName();
// Root version cannot be deleted, the remove below will take care of that.
if (!JCR_ROOT_VERSION.equals(name)) {
versionHistory.removeVersion(name);
}
}
}
}
Aggregations