Search in sources :

Example 41 with VersionIterator

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));
}
Also used : ArrayList(java.util.ArrayList) VersionIterator(javax.jcr.version.VersionIterator) VersionManager(javax.jcr.version.VersionManager)

Example 42 with VersionIterator

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
    }
}
Also used : Version(javax.jcr.version.Version) VersionIterator(javax.jcr.version.VersionIterator) VersionException(javax.jcr.version.VersionException)

Example 43 with VersionIterator

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());
}
Also used : Version(javax.jcr.version.Version) HashMap(java.util.HashMap) VersionIterator(javax.jcr.version.VersionIterator)

Example 44 with VersionIterator

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;
}
Also used : NestableRuntimeException(org.apache.commons.lang.exception.NestableRuntimeException) Node(javax.jcr.Node) BufferedContentItem(org.pentaho.platform.engine.core.output.BufferedContentItem) RepositoryException(javax.jcr.RepositoryException) VersionHistory(javax.jcr.version.VersionHistory) Repository(javax.jcr.Repository) StringTokenizer(java.util.StringTokenizer) Version(javax.jcr.version.Version) LockException(javax.jcr.lock.LockException) VersionIterator(javax.jcr.version.VersionIterator) Session(javax.jcr.Session)

Example 45 with VersionIterator

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);
            }
        }
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Version(javax.jcr.version.Version) Node(javax.jcr.Node) VersionIterator(javax.jcr.version.VersionIterator) VersionManager(javax.jcr.version.VersionManager) VersionHistory(javax.jcr.version.VersionHistory)

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