如何在jackrabbit 2.1存储库中恢复“版本化节点”?

时间:2010-07-20 17:37:09

标签: jackrabbit jcr

删除节点后,如何找到它以便使用jackrabbit或jcr API恢复它?

1 个答案:

答案 0 :(得分:3)

我不是Jackrabbit版本的专家,但据我所知,除非你知道它的一些数据,否则没有简单的方法来定位这样的节点。如果您知道,那么您可以使用查询并导航到作为javax.jcr.version.Version实例的下一个父级,并将其还原。如果您不知道,则需要迭代版本存储并打印所有数据。您可以过滤掉未删除的节点,但是否则它是手动作业,因为节点的路径未存储在版本存储中(除非您添加包含该路径的属性)。以下是如何列出版本存储中的所有节点的示例。它将恢复它找到的最后一个javax.jcr.version.Version:

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.version.Version;
import javax.jcr.version.VersionManager;
import org.apache.jackrabbit.core.TransientRepository;
public class TestRestoreDeleted {
    public static void main(String... args) throws Exception {
        TransientRepository rep = new TransientRepository();
        Session s = rep.login(
                new SimpleCredentials("", new char[0]));
        try {
            // clear the repository first
            if (s.getRootNode().hasNode("test")) {
                s.getRootNode().getNode("test").remove();
                s.save();
            }
            // add test/t1 and check in the change
            Node test = s.getRootNode().addNode("test");
            Node t1 = test.addNode("t1");
            t1.addMixin("mix:versionable");
            s.save();
            VersionManager vm = s.getWorkspace().
                    getVersionManager();
            for(int i=0; i<3; i++) {
                vm.checkout("/test/t1");
                t1.setProperty("data", "Hello" + i);
                s.save();
                vm.checkin("/test/t1");
            }
            // remove the node
            t1.remove();
            s.save();
            // list all versions of all nodes in the repository
            Node vs = s.getRootNode().
                    getNode("jcr:system").
                    getNode("jcr:versionStorage");
            Version v = traverseVersionStorage(vs, 0);
            // restore a version
            vm.restore("/test/t1", v, false);
            // get the node and print the data
            t1 = s.getRootNode().
                    getNode("test").getNode("t1");
            System.out.println("Restored: " +
                    t1.getProperty("data").getString());
        } finally {
            s.logout();
        }
    }

    private static Version traverseVersionStorage(
            Node n, int level) throws Exception {
        Version v = null;
        for (NodeIterator it = n.getNodes(); it.hasNext();) {
            Node n2 = it.nextNode();
            if (n2 instanceof Version
                    && !n2.getName().startsWith("jcr:")) {
                v = (Version) n2;
                System.out.println("version " + n2.getName() +
                        " of node " + n2.getParent().getName() + ":");
                Node n3 = n2.getNode("jcr:frozenNode");
                for (PropertyIterator pt =
                            n3.getProperties(); pt.hasNext();) {
                    Property p = pt.nextProperty();
                    if (!p.getName().startsWith("jcr:")) {
                        System.out.println("  " + p.getName() + "="
                                + (p.isMultiple() ? p.getValues().toString()
                                        : p.getValue().getString()));
                    }
                }
                System.out.println();
            }
            Version v2 = traverseVersionStorage(n2, level + 1);
            v = v == null ? v2 : v;
        }
        return v;
    }

}