entry: versionsByLabel.entrySet())
{
+ String key = entry.getKey();
if (EqualsHelper.nullSafeEquals(this.versionHistory.get(key), versionLabel))
{
- result.add(getVersion(key));
+ result.add(entry.getValue());
}
}
}
}
-
- return result;
+
+ return sortDescending(result);
}
/**
- * Gets a version with a specified version label. The version label is guarenteed
+ * Gets a version with a specified version label. The version label is guaranteed
* unique within the version history.
*
* @param versionLabel the version label
* @return the version object
- * @throws VersionDoesNotExistException indicates requested version does not exisit
+ * @throws VersionDoesNotExistException indicates requested version does not exist
*/
public Version getVersion(String versionLabel)
{
@@ -211,7 +219,8 @@ public class VersionHistoryImpl implements VersionHistory
}
/**
- * Add a version to the version history.
+ * Add a version to the version history, in the order they were
+ * created.
*
* Used internally to build the version history tree.
*
@@ -230,75 +239,7 @@ public class VersionHistoryImpl implements VersionHistory
}
}
- /**
- * Version Comparator
- *
- * Note: Descending (last modified) date order
- */
- public static class VersionComparatorDesc implements Comparator, Serializable
- {
- private static final long serialVersionUID = 6227528170880231770L;
-
- public int compare(Version v1, Version v2)
- {
- int result = 0;
-
- if ((null != v1) && (null != v2))
- {
- Serializable dbIdV1 = (null != v1.getVersionProperties()) ? (v1.getVersionProperties().get(ContentModel.PROP_NODE_DBID.getLocalName())) : (null);
- Serializable dbIdV2 = (null != v2.getVersionProperties()) ? (v2.getVersionProperties().get(ContentModel.PROP_NODE_DBID.getLocalName())) : (null);
-
- if ((null != dbIdV1) && (null != dbIdV2))
- {
- Long id1 = (dbIdV1 instanceof Integer) ? ((Integer) dbIdV1) : ((Long) dbIdV1);
- Long id2 = (dbIdV2 instanceof Integer) ? ((Integer) dbIdV2) : ((Long) dbIdV2);
- result = (id2).compareTo(id1);
- }
- else
- {
- logger.warn("DB Id of versioned node is missing!");
- }
- }
-
- return result;
- }
- }
-
- /**
- * Version Comparator
- *
- * Note: Ascending (last modified) date order
- */
- public static class VersionComparatorAsc implements Comparator, Serializable
- {
- private static final long serialVersionUID = 6227528170880231770L;
-
- public int compare(Version v1, Version v2)
- {
- int result = 0;
-
- if ((null != v1) && (null != v2))
- {
- Serializable dbIdV1 = (null != v1.getVersionProperties()) ? (v1.getVersionProperties().get(ContentModel.PROP_NODE_DBID.getLocalName())) : (null);
- Serializable dbIdV2 = (null != v2.getVersionProperties()) ? (v2.getVersionProperties().get(ContentModel.PROP_NODE_DBID.getLocalName())) : (null);
-
- if ((null != dbIdV1) && (null != dbIdV2))
- {
- Long id1 = (dbIdV1 instanceof Integer) ? ((Integer) dbIdV1) : ((Long) dbIdV1);
- Long id2 = (dbIdV2 instanceof Integer) ? ((Integer) dbIdV2) : ((Long) dbIdV2);
- result = (id1).compareTo(id2);
- }
- else
- {
- logger.warn("DB Id of versioned node is missing!");
- }
- }
-
- return result;
- }
- }
-
- @SuppressWarnings("unchecked")
+ @SuppressWarnings({ "unchecked", "deprecation" })
private void readObject(ObjectInputStream is) throws ClassNotFoundException, IOException
{
GetField fields = is.readFields();
@@ -306,21 +247,30 @@ public class VersionHistoryImpl implements VersionHistory
{
// This is a V2.2 class
// The old 'rootVersion' maps to the current 'rootVersion'
- this.rootVersion = (Version) fields.get("rootVersion", null);;
- // The old 'versions' maps to the current 'versionsByLabel'
this.versionsByLabel = (HashMap) fields.get("versions", new HashMap());
// The old 'versionHistory' maps to the current 'versionHistory'
this.versionHistory = (HashMap) fields.get("versionHistory", new HashMap());
+ // Need this comparator as versionsByLabel is not a LinkedHashMap in this version
+ this.versionComparatorDesc = new VersionLabelComparator();
}
- else
+ else if (fields.defaulted("versionComparatorDesc"))
{
- // This is a V3.1.0 to ... class
+ // This is a V3.1.0 class
// The old 'rootVersion' maps to the current 'rootVersion'
- this.rootVersion = (Version) fields.get("rootVersion", null);
- // The old 'versionsByLabel' maps to the current 'versionsByLabel'
this.versionsByLabel = (HashMap) fields.get("versionsByLabel", new HashMap());
// The old 'versionHistory' maps to the current 'versionHistory'
this.versionHistory = (HashMap) fields.get("versionHistory", new HashMap());
+ // Need this comparator as versionsByLabel is not a LinkedHashMap in this version
+ this.versionComparatorDesc = new VersionLabelComparator();
+ }
+ else
+ {
+ // This is a V4.1.3 (and 4.0.2 HF) class
+ // The old 'rootVersion' maps to the current 'rootVersion'
+ this.versionsByLabel = (Map) fields.get("versionsByLabel", new LinkedHashMap());
+ // The old 'versionHistory' maps to the current 'versionHistory'
+ this.versionHistory = (HashMap) fields.get("versionHistory", new HashMap());
+ this.versionComparatorDesc = (Comparator) fields.get("versionComparatorDesc", null);
}
}
}
diff --git a/source/java/org/alfresco/repo/version/common/VersionHistoryImplTest.java b/source/java/org/alfresco/repo/version/common/VersionHistoryImplTest.java
index 9079d88b92..030239fe55 100644
--- a/source/java/org/alfresco/repo/version/common/VersionHistoryImplTest.java
+++ b/source/java/org/alfresco/repo/version/common/VersionHistoryImplTest.java
@@ -24,9 +24,13 @@ import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
+import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
import junit.framework.TestCase;
@@ -50,6 +54,7 @@ public class VersionHistoryImplTest extends TestCase
/**
* Data used in the tests
*/
+ private NodeRef nodeRef;
private Version rootVersion = null;
private Version childVersion1 = null;
private Version childVersion2 = null;
@@ -62,27 +67,22 @@ public class VersionHistoryImplTest extends TestCase
super.setUp();
// Create dummy node ref
- NodeRef nodeRef = new NodeRef(new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "test"), "test");
+ nodeRef = new NodeRef(new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "test"), "test");
- HashMap versionProperties1 = new HashMap();
- versionProperties1.put(VersionModel.PROP_VERSION_LABEL, "1");
- versionProperties1.put(VersionModel.PROP_CREATED_DATE, new Date());
- versionProperties1.put("testProperty", "testValue");
- this.rootVersion = new VersionImpl(versionProperties1, nodeRef);
-
- HashMap versionProperties2 = new HashMap();
- versionProperties2.put(VersionModel.PROP_VERSION_LABEL, "2");
- versionProperties2.put(VersionModel.PROP_CREATED_DATE, new Date());
- versionProperties2.put("testProperty", "testValue");
- this.childVersion1 = new VersionImpl(versionProperties2, nodeRef);
-
- HashMap versionProperties3 = new HashMap();
- versionProperties3.put(VersionModel.PROP_VERSION_LABEL, "3");
- versionProperties3.put(VersionModel.PROP_CREATED_DATE, new Date());
- versionProperties3.put("testProperty", "testValue");
- this.childVersion2 = new VersionImpl(versionProperties3, nodeRef);
+ this.rootVersion = newVersion(nodeRef, "1");
+ this.childVersion1 = newVersion(nodeRef, "2");
+ this.childVersion2 = newVersion(nodeRef, "3");
}
+ private VersionImpl newVersion(NodeRef nodeRef, String label)
+ {
+ HashMap versionProperties1 = new HashMap();
+ versionProperties1.put(VersionModel.PROP_VERSION_LABEL, label);
+ versionProperties1.put(VersionModel.PROP_CREATED_DATE, new Date());
+ versionProperties1.put("testProperty", "testValue");
+ return new VersionImpl(versionProperties1, nodeRef);
+ }
+
/**
* Test constructor
*/
@@ -98,7 +98,7 @@ public class VersionHistoryImplTest extends TestCase
*/
private VersionHistoryImpl testContructorImpl()
{
- VersionHistoryImpl vh = new VersionHistoryImpl(this.rootVersion);
+ VersionHistoryImpl vh = new VersionHistoryImpl(this.rootVersion, null);
assertNotNull(vh);
return vh;
@@ -112,7 +112,7 @@ public class VersionHistoryImplTest extends TestCase
{
try
{
- new VersionHistoryImpl(null);
+ new VersionHistoryImpl(null, null);
fail();
}
catch(VersionServiceException exception)
@@ -146,6 +146,42 @@ public class VersionHistoryImplTest extends TestCase
assertEquals(3, allVersions.size());
}
+ /**
+ * Test getAllVersions using a comparator to resort versions which are in the
+ * wrong order.
+ */
+ public void testGetAllVersionsComparator()
+ {
+ String[] labels = new String[] { "1.0", "1.1", "1.2", "2.0", "2.1" };
+ List versions = new ArrayList(labels.length);
+ for (String label: labels)
+ {
+ versions.add(newVersion(nodeRef, label));
+ }
+ Collections.shuffle(versions);
+
+ Iterator itr = versions.iterator();
+ Version version = itr.next();
+ Version predecessor;
+ VersionHistoryImpl vh = new VersionHistoryImpl(version,
+ Collections.reverseOrder(new VersionLabelComparator()));
+ while (itr.hasNext())
+ {
+ predecessor = version;
+ version = itr.next();
+ vh.addVersion(version, predecessor);
+ }
+
+ Collection allVersions = vh.getAllVersions();
+ assertNotNull(allVersions);
+ assertEquals(labels.length, allVersions.size());
+ itr = allVersions.iterator();
+ for (String label: labels)
+ {
+ assertEquals(label, itr.next().getVersionLabel());
+ }
+ }
+
/**
* Test addVersion
*
@@ -210,7 +246,6 @@ public class VersionHistoryImplTest extends TestCase
/**
* Test getSuccessors
*/
- @SuppressWarnings("unchecked")
public void testGetSuccessors()
{
VersionHistoryImpl vh = testAddVersionImpl();
@@ -237,6 +272,40 @@ public class VersionHistoryImplTest extends TestCase
assertTrue(versions3.isEmpty());
}
+ /**
+ * Test getSuccessors using a comparator to resort versions which are in the
+ * wrong order.
+ */
+ public void testGetSuccessorsComparator()
+ {
+ rootVersion = newVersion(nodeRef, "1.0");
+ String[] labels = new String[] { "1.1", "1.2", "2.0", "2.1" };
+ List versions = new ArrayList(labels.length);
+ for (String label: labels)
+ {
+ versions.add(newVersion(nodeRef, label));
+ }
+ Collections.shuffle(versions);
+
+ Iterator itr = versions.iterator();
+ Version version = rootVersion;
+ VersionHistoryImpl vh = new VersionHistoryImpl(version,
+ Collections.reverseOrder(new VersionLabelComparator()));
+ while (itr.hasNext())
+ {
+ vh.addVersion(itr.next(), rootVersion);
+ }
+
+ Collection allVersions = vh.getSuccessors(rootVersion);
+ assertNotNull(allVersions);
+ assertEquals(labels.length, allVersions.size());
+ itr = allVersions.iterator();
+ for (String label: labels)
+ {
+ assertEquals(label, itr.next().getVersionLabel());
+ }
+ }
+
/**
* Test getVersion
*/
diff --git a/source/java/org/alfresco/repo/version/common/VersionLabelComparator.java b/source/java/org/alfresco/repo/version/common/VersionLabelComparator.java
index 03368956b1..7ed1b14aa2 100644
--- a/source/java/org/alfresco/repo/version/common/VersionLabelComparator.java
+++ b/source/java/org/alfresco/repo/version/common/VersionLabelComparator.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2005-2010 Alfresco Software Limited.
+ * Copyright (C) 2005-2012 Alfresco Software Limited.
*
* This file is part of Alfresco
*
@@ -28,7 +28,7 @@ import org.alfresco.util.VersionNumber;
*
* @author Yanick Pignot
*
- * @deprecated see VersionHistory (getAllVersions, VersionComparatorAsc, VersionComparatorDesc)
+ * @deprecated See {@link VersionHistory}
*/
public class VersionLabelComparator implements Comparator
{