jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tri...@apache.org
Subject svn commit: r157553 - in incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core: NodeImpl.java WorkspaceImpl.java version/InternalFrozenNode.java version/VersionImpl.java version/persistence/InternalFrozenNodeImpl.java
Date Tue, 15 Mar 2005 15:45:49 GMT
Author: tripod
Date: Tue Mar 15 07:45:46 2005
New Revision: 157553

URL: http://svn.apache.org/viewcvs?view=rev&rev=157553
Log:
- fixed restore bugs
- implemented Workspace.restore() [not tested yet]

Modified:
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/NodeImpl.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/WorkspaceImpl.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/version/InternalFrozenNode.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/version/VersionImpl.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/version/persistence/InternalFrozenNodeImpl.java

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/NodeImpl.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/NodeImpl.java?view=diff&r1=157552&r2=157553
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/NodeImpl.java (original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/NodeImpl.java Tue Mar 15
07:45:46 2005
@@ -2977,8 +2977,7 @@
         checkLock();
 
         // check if 'own' version
-        // TODO: change if Version.getContainingVersionHistory() is introduced
-        if (!version.getParent().isSame(getVersionHistory())) {
+        if (!((VersionImpl) version).getContainingVersionHistory().isSame(getVersionHistory()))
{
             throw new VersionException("Unable to restore version. Not same version history.");
         }
 
@@ -3010,8 +3009,32 @@
         if (hasNode(relPath)) {
             getNode(relPath).restore(version, removeExisting);
         } else {
+            NodeImpl node;
+            try {
+                // check if versionable node exists
+                InternalFrozenNode fn = ((VersionImpl) version).getFrozenNode();
+                node = (NodeImpl) session.getNodeByUUID(fn.getFrozenUUID());
+                if (removeExisting) {
+                    try {
+                        Path dstPath = Path.create(getPrimaryPath(), relPath, session.getNamespaceResolver(),
true);
+                        // move to respective location
+                        session.move(node.getPath(), dstPath.toJCRPath(session.getNamespaceResolver()));
+                        // need to refetch ?
+                        node = (NodeImpl) session.getNodeByUUID(fn.getFrozenUUID());
+                    } catch (MalformedPathException e) {
+                        throw new RepositoryException(e);
+                    } catch (NoPrefixDeclaredException e) {
+                        throw new RepositoryException("InternalError.", e);
+                    }
+                } else {
+                    throw new ItemExistsException("Unable to restore version. Versionable
node already exists.");
+                }
+            } catch (ItemNotFoundException e) {
+                // not found, create new one
+                node = addNode(relPath, ((VersionImpl) version).getFrozenNode());
+            }
+
             // recreate node from frozen state
-            NodeImpl node = addNode(relPath, ((VersionImpl) version).getFrozenNode());
             node.internalRestore(version, new GenericVersionSelector(version.getCreated()),
removeExisting);
             // session.save/revert is done in internal restore
         }
@@ -3042,7 +3065,7 @@
             throw new VersionException("No version for label " + versionLabel + " found.");
         }
         internalRestore(v, new GenericVersionSelector(versionLabel), removeExisting);
-        save();
+        // session.save/revert is done in internal restore
     }
 
     /**
@@ -3484,7 +3507,7 @@
      * @param removeExisting
      * @throws RepositoryException
      */
-    private void internalRestore(InternalVersion version, VersionSelector vsel,
+    protected InternalVersion[] internalRestore(InternalVersion version, VersionSelector
vsel,
                                  boolean removeExisting)
             throws RepositoryException {
 
@@ -3494,7 +3517,9 @@
         // 1. The child node and properties of N will be changed, removed or
         //    added to, depending on their corresponding copies in V and their
         //    own OnParentVersion attributes (see 7.2.8, below, for details).
-        restoreFrozenState(version.getFrozenNode(), vsel, removeExisting);
+        HashSet restored = new HashSet();
+        restoreFrozenState(version.getFrozenNode(), vsel, restored, removeExisting);
+        restored.add(version);
 
         // 2. N's jcr:baseVersion property will be changed to point to V.
         internalSetProperty(JCR_BASEVERSION, InternalValue.create(new UUID(version.getId())));
@@ -3504,6 +3529,8 @@
 
         // 3. N's jcr:isCheckedOut property is set to false.
         internalSetProperty(JCR_ISCHECKEDOUT, InternalValue.create(false));
+
+        return (InternalVersion[]) restored.toArray(new InternalVersion[restored.size()]);
     }
 
     /**
@@ -3514,7 +3541,7 @@
      * @param removeExisting
      * @throws RepositoryException
      */
-    void restoreFrozenState(InternalFrozenNode freeze, VersionSelector vsel, boolean removeExisting)
+    void restoreFrozenState(InternalFrozenNode freeze, VersionSelector vsel, Set restored,
boolean removeExisting)
             throws RepositoryException {
 
         // check uuid
@@ -3578,19 +3605,17 @@
             }
         }
 
-        // restore the frozen nodes
-        InternalFreeze[] frozenNodes = freeze.getFrozenChildNodes();
-
-        // first delete all non frozen version histories, ie. all OPV!=Version
+        // first delete all non frozen version histories
         NodeIterator iter = getNodes();
         while (iter.hasNext()) {
             NodeImpl n = (NodeImpl) iter.nextNode();
-            if (n.getDefinition().getOnParentVersion() == OnParentVersionAction.COPY) {
-                n.remove();
+            if (!freeze.hasFrozenHistory(n.internalGetUUID())) {
+                n.internalRemove(true);
             }
         }
 
-        // now restore the frozen ones
+        // restore the frozen nodes
+        InternalFreeze[] frozenNodes = freeze.getFrozenChildNodes();
         for (int i = 0; i < frozenNodes.length; i++) {
             InternalFreeze child = frozenNodes[i];
             if (child instanceof InternalFrozenNode) {
@@ -3599,6 +3624,7 @@
                 if (f.getFrozenUUID() != null) {
                     try {
                         NodeImpl existing = (NodeImpl) session.getNodeByUUID(f.getFrozenUUID());
+                        // check if one of this restoretrees node
                         if (removeExisting) {
                             existing.remove();
                         } else {
@@ -3611,27 +3637,33 @@
                     }
                 }
                 NodeImpl n = addNode(f.getName(), f);
-                n.restoreFrozenState(f, vsel, removeExisting);
+                n.restoreFrozenState(f, vsel, restored, removeExisting);
 
             } else if (child instanceof InternalFrozenVersionHistory) {
                 InternalFrozenVersionHistory f = (InternalFrozenVersionHistory) child;
                 VersionHistoryImpl history = (VersionHistoryImpl) session.getNodeByUUID(f.getVersionHistoryId());
                 String nodeId = history.getVersionableUUID();
 
-                // check if representing vh already exists somewhere
+                // check if representing versionable already exists somewhere
                 if (itemMgr.itemExists(new NodeId(nodeId))) {
                     NodeImpl n = (NodeImpl) session.getNodeByUUID(nodeId);
-                    if (hasNode(n.getQName())) {
+                    if (n.getParent().isSame(this)) {
                         // so order at end
                         // orderBefore(n.getName(), "");
-                    } else {
+                    } else if (removeExisting) {
                         session.move(n.getPath(), getPath() + "/" + n.getName());
+                    } else {
+                        // since we delete the OPV=Copy children beforehand, all
+                        // found nodes must be outside of this tree
+                        throw new ItemExistsException("Unable to restore node, item already
exists outside of restored tree: " + n.safeGetJCRPath());
                     }
                 } else {
                     // get desired version from version selector
                     InternalVersion v = ((VersionImpl) vsel.select(history)).getInternalVersion();
                     NodeImpl node = addNode(child.getName(), v.getFrozenNode());
                     node.internalRestore(v, vsel, removeExisting);
+                    // add this version to set
+                    restored.add(v);
                 }
             }
         }

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/WorkspaceImpl.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/WorkspaceImpl.java?view=diff&r1=157552&r2=157553
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/WorkspaceImpl.java (original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/WorkspaceImpl.java Tue
Mar 15 07:45:46 2005
@@ -37,6 +37,10 @@
 import org.apache.jackrabbit.core.state.TransactionalItemStateManager;
 import org.apache.jackrabbit.core.util.uuid.UUID;
 import org.apache.jackrabbit.core.xml.ImportHandler;
+import org.apache.jackrabbit.core.version.VersionSelector;
+import org.apache.jackrabbit.core.version.VersionImpl;
+import org.apache.jackrabbit.core.version.GenericVersionSelector;
+import org.apache.jackrabbit.core.version.InternalVersion;
 import org.apache.log4j.Logger;
 import org.xml.sax.Attributes;
 import org.xml.sax.ContentHandler;
@@ -65,12 +69,14 @@
 import javax.jcr.query.QueryManager;
 import javax.jcr.version.Version;
 import javax.jcr.version.VersionException;
+import javax.jcr.version.VersionHistory;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.PrintStream;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.HashMap;
 
 /**
  * A <code>WorkspaceImpl</code> ...
@@ -1050,11 +1056,83 @@
             VersionException, LockException, InvalidItemStateException,
             RepositoryException {
 
+        // todo: perform restore operations direct on the node states
+
         // check state of this instance
         sanityCheck();
 
-        // @todo implement Workspace#restore
-        throw new UnsupportedRepositoryOperationException();
+        // add all versions to map of versions to restore
+        final HashMap toRestore = new HashMap();
+        for (int i=0; i<versions.length; i++) {
+            VersionImpl v = (VersionImpl) versions[i];
+            VersionHistory vh = v.getContainingVersionHistory();
+            // check for collision
+            if (toRestore.containsKey(vh.getUUID())) {
+                throw new VersionException("Unable to restore. Two ore more versions have
same version history.");
+            }
+            toRestore.put(vh.getUUID(), v);
+        }
+
+        // create a version selector to the set of versions
+        VersionSelector vsel = new VersionSelector() {
+            public Version select(VersionHistory versionHistory) throws RepositoryException
{
+                // try to select version as specified
+                Version v = (Version) toRestore.get(versionHistory.getUUID());
+                if (v == null) {
+                    // select latest one
+                    v = GenericVersionSelector.selectByDate(versionHistory, null);
+                }
+                return v;
+            }
+        };
+
+        // check for pending changes
+        if (session.hasPendingChanges()) {
+            String msg = "Unable to restore version. Session has pending changes.";
+            log.debug(msg);
+            throw new InvalidItemStateException(msg);
+        }
+
+        try {
+            // now restore all versions that have a node in the ws
+            int numRestored = 0;
+            while (toRestore.size()>0) {
+                InternalVersion[] restored = null;
+                Iterator iter = toRestore.values().iterator();
+                while (iter.hasNext()) {
+                    VersionImpl v = (VersionImpl) iter.next();
+                    try {
+                        NodeImpl node = (NodeImpl) session.getNodeByUUID(v.getFrozenNode().getFrozenUUID());
+                        restored = node.internalRestore(v.getInternalVersion(), vsel, removeExisting);
+                        // remove restored versions from set
+                        for (int i=0; i<restored.length; i++) {
+                            toRestore.remove(restored[i].getVersionHistory().getId());
+                        }
+                        numRestored += restored.length;
+                        break;
+                    } catch (ItemNotFoundException e) {
+                        // ignore
+                    }
+                }
+                if (restored == null) {
+                    if (numRestored == 0) {
+                        throw new VersionException("Unable to restore. At least one version
needs existing versionable node in workspace.");
+                    } else {
+                        throw new VersionException("Unable to restore. All versions with
non existing versionable nodes need parent.");
+                    }
+                }
+            }
+        } catch (RepositoryException e) {
+            // revert session
+            try {
+                log.error("reverting changes applied during restore...");
+                session.refresh(false);
+            } catch (RepositoryException e1) {
+                // ignore this
+            }
+            throw e;
+        }
+        session.save();
     }
 
     /**

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/version/InternalFrozenNode.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/version/InternalFrozenNode.java?view=diff&r1=157552&r2=157553
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/version/InternalFrozenNode.java
(original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/version/InternalFrozenNode.java
Tue Mar 15 07:45:46 2005
@@ -65,4 +65,11 @@
      */
     public QName[] getFrozenMixinTypes();
 
+    /**
+     * Checks if this frozen node has the frozen version history
+     * @param uuid
+     * @return
+     */
+    public boolean hasFrozenHistory(String uuid);
+
 }

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/version/VersionImpl.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/version/VersionImpl.java?view=diff&r1=157552&r2=157553
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/version/VersionImpl.java
(original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/version/VersionImpl.java
Tue Mar 15 07:45:46 2005
@@ -32,6 +32,7 @@
 import javax.jcr.PropertyIterator;
 import javax.jcr.nodetype.NodeDef;
 import javax.jcr.version.Version;
+import javax.jcr.version.VersionHistory;
 import java.util.Calendar;
 import java.util.List;
 
@@ -106,6 +107,15 @@
     }
 
     /**
+     * Returns the version history this version is contained in.
+     * @return this versions history
+     * @throws RepositoryException
+     */
+    public VersionHistory getContainingVersionHistory() throws RepositoryException {
+        return (VersionHistory) getParent();
+    }
+
+    /**
      * Returns the internal version
      *
      * @return
@@ -145,4 +155,5 @@
     public PropertyIterator getReferences() throws RepositoryException {
         return getReferences(true);
     }
+
 }

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/version/persistence/InternalFrozenNodeImpl.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/version/persistence/InternalFrozenNodeImpl.java?view=diff&r1=157552&r2=157553
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/version/persistence/InternalFrozenNodeImpl.java
(original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/version/persistence/InternalFrozenNodeImpl.java
Tue Mar 15 07:45:46 2005
@@ -29,6 +29,7 @@
 import org.apache.jackrabbit.core.version.InternalFrozenNode;
 import org.apache.jackrabbit.core.version.InternalVersionItem;
 import org.apache.jackrabbit.core.version.PersistentVersionManager;
+import org.apache.jackrabbit.core.version.InternalFrozenVersionHistory;
 
 import javax.jcr.NodeIterator;
 import javax.jcr.PropertyIterator;
@@ -189,6 +190,21 @@
         } catch (RepositoryException e) {
             throw new VersionException("Unable to retrieve frozen child nodes", e);
         }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean hasFrozenHistory(String uuid) {
+        try {
+            List entries = node.getState().getChildNodeEntries(uuid);
+            if (entries.size()>0) {
+                return getVersionManager().getItemByInternal(uuid) instanceof InternalFrozenVersionHistory;
+            }
+        } catch (RepositoryException e) {
+            // ignore
+        }
+        return false;
     }
 
     /**



Mime
View raw message