Search code examples
javajakarta-eenodesversioningjackrabbit

How to properly add versionable nodes in jackrabbit


im using Jackrabbit 2.0 inside an j2ee application on tomcat wtih java 1.7. I want to version at least under the rootnode a usernode and under that a versionable lessonnode and under that a versionable filecardnode. Each, the lesson- and filecardnode holding a serialized object as property. I am able to restore each single one, but i expected under a given version of a lessonnode to receive a frozennode that holds all filecardnodes to that time....but if i search for subnodes on that there are no subnodes. Any suggestions what im doin wrong, or i mabey understand something wrong in specs.

This is how i save the nodes :

    public Node insertNewLessonNode(String userID, LessonEntity lessonEntity,
        Session session) throws PathNotFoundException, RepositoryException,
        IOException {
    //that adds a usernode if not there already 
    Node userNode = handleLessonParentNodes(lessonEntity, userID, session);

    Node lessonNode = userNode.addNode(LESSON
            + lessonEntity.getID(), "nt:unstructured");
    lessonNode.addMixin("mix:versionable");
    lessonNode.setProperty("id", lessonEntity.getID());
    lessonNode.setProperty("data", JRUtils.serializObject(lessonEntity));
    session.save();
    session.getWorkspace().getVersionManager()
            .checkin(lessonNode.getPath());

    System.out.println("Lesson "
            + session.getWorkspace().getVersionManager()
                    .getBaseVersion(lessonNode.getPath()));

    return lessonNode;
}

public Node insertNewFileCardNode(Node lessonNode,
        FileCardEntity fileCardEntity, String userID, Session session)
        throws UnsupportedRepositoryOperationException,
        RepositoryException, IOException {

    nodeCheckout(lessonNode, session);

    Node fileCardNode = null;
    fileCardNode = lessonNode.addNode(FILECARD + fileCardEntity.getID(),
            "nt:unstructured");
    fileCardNode.addMixin("mix:versionable");

    fileCardNode
            .setProperty("data", JRUtils.serializObject(fileCardEntity));
    session.save();
    session.getWorkspace()
            .getVersionManager()
            .checkin(
                    lessonNode.getPath() + DEL + FILECARD
                            + fileCardEntity.getID());

    System.out.println("FileCard "
            + session
                    .getWorkspace()
                    .getVersionManager()
                    .getBaseVersion(
                            lessonNode.getPath() + DEL + FILECARD
                                    + fileCardEntity.getID()));

    return fileCardNode;
}

And this is how i restore it:

     public void restoreLesson(String lessonId,
        String userID, String versionName) throws SQLException, VersionException, ItemExistsException, UnsupportedRepositoryOperationException, LockException, InvalidItemStateException, RepositoryException {
    Session session = JRRepository.getInstance().getSession();
    String userNodePath = session.getRootNode().getNode(USER + userID)
            .getPath();
    String absPath = userNodePath + DEL + LESSON+lessonId;
    VersionHistory versionHistory = session.getWorkspace()
            .getVersionManager().getVersionHistory(absPath);
    VersionIterator iterator = versionHistory.getAllVersions();




    LessonEntity lessonEntity = null;

    while (iterator.hasNext()) {
        Version version = (Version) iterator.next();
        if (version.getName().equals(versionName)) {
            Node frozenNode = version.getFrozenNode();
            Value value = frozenNode.getProperty("data").getValue();

            //here i expect a few of my filecardnodes...but theres nothin...
            //i tried already with differened searchpatterns
            for (NodeIterator iterator3 = frozenNode.getNodes("nt:unstructured"); iterator3.hasNext();) {
                Node type = (Node) iterator3.next();
                System.out.println(type.getName());
            }

            Object ob = JRUtils.deSerializeObject(value.getBinary());

            lessonEntity = (LessonEntity) ob;

        }
    }

    session.getWorkspace().getVersionManager()
            .restore(absPath, versionName, false);
    List<LessonEntity> updateList = new ArrayList<LessonEntity>();
    lessonEntity.setLastModified(new Date());
    updateList.add(lessonEntity);
    lessonDao.updateLessons(updateList, userID);

    session.logout();

}

Any help would be appreciated....thanks in advance!


Solution

  • If you want to be able to retrieve the children, of a versioned node, then you must only make the parent versionable.

    When you make the parent and the child versionable they are stored as separate versions so there is no link between them. If you do want to make them both versionable then you would have to add your own properties in order to link a particular version of filecardnode with a particular version of lessionnode.