Search in sources :

Example 81 with VersionHistory

use of javax.jcr.version.VersionHistory 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 82 with VersionHistory

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

the class DavResourceFactoryImpl method createResourceForItem.

/**
 * Tries to retrieve the repository item defined by the locator's resource
 * path and build the corresponding WebDAV resource. The following distinction
 * is made between items: Version nodes, VersionHistory nodes, root node,
 * unspecified nodes and finally property items.
 *
 * @param locator
 * @param sessionImpl
 * @return DavResource representing a repository item.
 * @throws RepositoryException if {@link javax.jcr.Session#getItem(String)} fails.
 */
private DavResource createResourceForItem(DavResourceLocator locator, JcrDavSession sessionImpl) throws RepositoryException, DavException {
    DavResource resource;
    Item item = getItem(sessionImpl, locator);
    if (item.isNode()) {
        // create special resources for Version and VersionHistory
        if (item instanceof Version) {
            resource = new VersionItemCollection(locator, sessionImpl, this, item);
        } else if (item instanceof VersionHistory) {
            resource = new VersionHistoryItemCollection(locator, sessionImpl, this, item);
        } else {
            resource = new VersionControlledItemCollection(locator, sessionImpl, this, item);
        }
    } else {
        resource = new DefaultItemResource(locator, sessionImpl, this, item);
    }
    return resource;
}
Also used : Item(javax.jcr.Item) VersionHistoryItemCollection(org.apache.jackrabbit.webdav.jcr.version.VersionHistoryItemCollection) DavResource(org.apache.jackrabbit.webdav.DavResource) Version(javax.jcr.version.Version) VersionItemCollection(org.apache.jackrabbit.webdav.jcr.version.VersionItemCollection) VersionHistory(javax.jcr.version.VersionHistory)

Example 83 with VersionHistory

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

the class AbstractRepositoryTest method createVersionable.

protected Node createVersionable(Node parent) throws RepositoryException {
    Node versionable = parent.addNode("versionable", "nt:myversionable");
    versionable.setProperty("foo", "A");
    parent.save();
    VersionHistory history = versionable.getVersionHistory();
    Version versionA = versionable.checkin();
    history.addVersionLabel(versionA.getName(), "labelA", false);
    versionable.checkout();
    versionable.setProperty("foo", "B");
    parent.save();
    Version versionB = versionable.checkin();
    history.addVersionLabel(versionB.getName(), "labelB", false);
    return versionable;
}
Also used : Version(javax.jcr.version.Version) Node(javax.jcr.Node) VersionHistory(javax.jcr.version.VersionHistory)

Example 84 with VersionHistory

use of javax.jcr.version.VersionHistory 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 85 with VersionHistory

use of javax.jcr.version.VersionHistory 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

VersionHistory (javax.jcr.version.VersionHistory)89 Node (javax.jcr.Node)59 Version (javax.jcr.version.Version)43 Test (org.junit.Test)30 VersionManager (javax.jcr.version.VersionManager)26 RepositoryException (javax.jcr.RepositoryException)25 VersionIterator (javax.jcr.version.VersionIterator)23 Session (javax.jcr.Session)19 Property (javax.jcr.Property)11 ArrayList (java.util.ArrayList)8 DavException (org.apache.jackrabbit.webdav.DavException)7 DavResourceLocator (org.apache.jackrabbit.webdav.DavResourceLocator)7 JcrDavException (org.apache.jackrabbit.webdav.jcr.JcrDavException)7 Workspace (javax.jcr.Workspace)6 DavResource (org.apache.jackrabbit.webdav.DavResource)6 ItemNotFoundException (javax.jcr.ItemNotFoundException)5 Value (javax.jcr.Value)5 NodeIterator (javax.jcr.NodeIterator)4 PathNotFoundException (javax.jcr.PathNotFoundException)4 NodeType (javax.jcr.nodetype.NodeType)4