jackrabbit-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Tomasz Dabrowski" <Tomasz.Dabrow...@cognifide.com>
Subject RE: Index incorrect after mulitple cloning
Date Wed, 23 Nov 2005 12:57:24 GMT
ups... I thought that I'm able to attach the file but it doesn't work so
I past cod here

package prototyping;

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;

import org.apache.jackrabbit.core.RepositoryImpl;
import org.apache.jackrabbit.core.WorkspaceImpl;
import org.apache.jackrabbit.core.config.RepositoryConfig;

public class CloningTest {
    private static final org.apache.commons.logging.Log log =
org.apache.commons.logging.LogFactory.getLog(CloningTest.class);

    public static String REPOSITORY_HOME = "d:/repo_embeded/";

    public static String REPOSITORY_CONFIG = REPOSITORY_HOME +
"repository.xml";

    public static String WORKSPACE_0 = "workspace_0";

    public static String WORKSPACE_1 = "workspace_1";

    public static String WORKSPACE_2 = "workspace_2";

    public static String CHILD_NAME = "child";

    public static void main(String[] args) throws RepositoryException {
        System.setProperty("java.security.auth.login.config",
"jaas_jcr.config");

        RepositoryConfig config =
RepositoryConfig.create(REPOSITORY_CONFIG, REPOSITORY_HOME);
        Repository repository = RepositoryImpl.create(config);

        Session session0 = repository.login(new
SimpleCredentials("admin", "admin".toCharArray()));

        createWorkspace(session0, WORKSPACE_0);
        createWorkspace(session0, WORKSPACE_1);
        createWorkspace(session0, WORKSPACE_2);

        session0 = repository.login(new SimpleCredentials("admin",
"admin".toCharArray()), WORKSPACE_0);
        Session session1 = repository.login(new
SimpleCredentials("admin", "admin".toCharArray()), WORKSPACE_1);
        Session session2 = repository.login(new
SimpleCredentials("admin", "admin".toCharArray()), WORKSPACE_2);

        /*
         * prepare data - build some simple hierarchy as presented
below:
         * 
         * node 
         * | 
         * +--child 
         * | 
         * +--child[2] 
         * | 
         * +--child[3]
         */
        Node node0 = prepareData(session0);

        // clone this hierarchy into session1 (session to WORKSPACE_1)
        Node node1 = cloneSubtree(session1, node0);

        // clone this hierarchy into session2 (session to WORKSPACE_2)
        Node node2 = cloneSubtree(session2, node0);

        // add one more "child" to node1 (this is node from session1)
        Node node1_child = addChild(node1);

        // display tree hierarchy for node1 (session1)
        log.info("Hierarchy in " + WORKSPACE_1);
        printChildren(node1, "");

        // display tree hierarchy for node2 (session2)
        log.info("Hierarchy in " + WORKSPACE_2);
        printChildren(node2, "");

        // clone one more node into session2
        cloneSubtree(session2, node1_child);

        // display tree hierarchy for node2 (session2)
        log.info("Hierarchy in " + WORKSPACE_2 + " aftert one more
clonning");
        printChildren(node2, "");

        session0.logout();
        session1.logout();
        session2.logout();

        ((RepositoryImpl) repository).shutdown();

        log.info("Done.");
    }

    private static Node addChild(Node node) throws RepositoryException {
        node.checkout();

        Node child = node.addNode(CHILD_NAME);
        child.addMixin("mix:versionable");

        node.save();
        child.checkin();
        node.checkin();

        log.info("Child added.");

        return child;
    }

    private static void printChildren(Node node, String extra) throws
RepositoryException {
        log.info(extra + "Node name = [" + node.getName() + "]; node
path = [" + node.getPath() + "]");

        for (NodeIterator iter = node.getNodes(); iter.hasNext();) {
            Node child = iter.nextNode();
            printChildren(child, "\t");
        }
    }

    private static Node prepareData(Session session) throws
RepositoryException {
        Node root = session.getRootNode();

        Node node = root.addNode("node");
        node.addMixin("mix:versionable");

        log.info("node.getDefinition().allowsSameNameSiblings() = [" +
node.getDefinition().allowsSameNameSiblings() + "]");

        Node child0 = node.addNode(CHILD_NAME);
        child0.addMixin("mix:versionable");

        Node child1 = node.addNode(CHILD_NAME);
        child1.addMixin("mix:versionable");

        Node child2 = node.addNode(CHILD_NAME);
        child2.addMixin("mix:versionable");

        root.save();
        child0.checkin();
        child1.checkin();
        child2.checkin();

        node.checkin();

        return node;
    }

    private static void createWorkspace(Session session, String
workspace) throws RepositoryException {
        ((WorkspaceImpl)
session.getWorkspace()).createWorkspace(workspace);
    }

    private static Node cloneSubtree(Session session, Node node) throws
RepositoryException {
        Node root = session.getRootNode();
        Node parent = node.getParent();
        String path = "";
        Node targetParent = null;
        String rootName = "/";

        if (!parent.isSame(root)) {
            path = parent.getPath();
            targetParent = session.getNodeByUUID(parent.getUUID());
            targetParent.checkout();
        }

        path += "/" + node.getName();
        String workspace = node.getSession().getWorkspace().getName();

        session.getWorkspace().clone(workspace, node.getPath(), path,
false);

        if (targetParent != null) {
            targetParent.checkin();
            rootName = targetParent.getName();
        }

        log.info("Subtree cloned; root node = [" + rootName + "]");

        return session.getNodeByUUID(node.getUUID());

    }
} 


--
Tomasz Dabrowski
email: tomasz.dabrowski@cognifide.com
skype: tomekdab
www: www.cognifide.com

Mime
View raw message