KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > version > common > VersionHistoryImpl


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.version.common;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22
23 import org.alfresco.service.cmr.version.Version;
24 import org.alfresco.service.cmr.version.VersionDoesNotExistException;
25 import org.alfresco.service.cmr.version.VersionHistory;
26 import org.alfresco.service.cmr.version.VersionServiceException;
27
28 /**
29  * Version History implementation.
30  *
31  * @author Roy Wetherall
32  */

33 public class VersionHistoryImpl implements VersionHistory
34 {
35     /*
36      * Serial version UID
37      */

38     private static final long serialVersionUID = 3257001051558326840L;
39
40     /*
41      * Error message(s)
42      */

43     private static final String JavaDoc ERR_MSG = "The root version must be specified when creating a version history object.";
44     
45     /*
46      * The root version label
47      */

48     private String JavaDoc rootVersionLabel = null;
49     
50     /*
51      * Version history tree structure map
52      */

53     private HashMap JavaDoc<String JavaDoc, String JavaDoc> versionHistory = null;
54     
55     /*
56      * Label to version object map
57      */

58     private HashMap JavaDoc<String JavaDoc, Version> versions = null;
59     
60     private Version rootVersion;
61     
62     /**
63      * Constructor, ensures the root version is set.
64      *
65      * @param rootVersion the root version, can not be null.
66      */

67     public VersionHistoryImpl(Version rootVersion)
68     {
69         if (rootVersion == null)
70         {
71             // Exception - a version history can not be created unless
72
// a root version is specified
73
throw new VersionServiceException(VersionHistoryImpl.ERR_MSG);
74         }
75         
76         this.versionHistory = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
77         this.versions = new HashMap JavaDoc<String JavaDoc, Version>();
78         
79         this.rootVersion = rootVersion;
80         this.rootVersionLabel = rootVersion.getVersionLabel();
81         addVersion(rootVersion, null);
82     }
83     
84     /**
85      * Gets the root (or initial) version of the version history.
86      *
87      * @return the root version
88      */

89     public Version getRootVersion()
90     {
91         return this.rootVersion;
92     }
93     
94     /**
95      * Gets a collection containing all the versions within the
96      * version history.
97      * <p>
98      * The order of the versions is not guarenteed.
99      *
100      * @return collection containing all the versions
101      */

102     public Collection JavaDoc<Version> getAllVersions()
103     {
104         return this.versions.values();
105     }
106     
107     /**
108      * Gets the predecessor of a specified version
109      *
110      * @param version the version object
111      * @return the predeceeding version, null if root version
112      */

113     public Version getPredecessor(Version version)
114     {
115         Version result = null;
116         if (version != null)
117         {
118             result = getVersion(this.versionHistory.get(version.getVersionLabel()));
119         }
120         return result;
121     }
122
123     /**
124      * Gets the succeeding versions of a specified version.
125      *
126      * @param version the version object
127      * @return a collection containing the succeeding version, empty is none
128      */

129     public Collection JavaDoc<Version> getSuccessors(Version version)
130     {
131         ArrayList JavaDoc<Version> result = new ArrayList JavaDoc<Version>();
132         
133         if (version != null)
134         {
135             String JavaDoc versionLabel = version.getVersionLabel();
136             
137             if (this.versionHistory.containsValue(versionLabel) == true)
138             {
139                 for (String JavaDoc key : this.versionHistory.keySet())
140                 {
141                     if (this.versionHistory.get(key) == versionLabel)
142                     {
143                         result.add(getVersion(key));
144                     }
145                 }
146             }
147         }
148         
149         return result;
150     }
151     
152     /**
153      * Gets a version with a specified version label. The version label is guarenteed
154      * unique within the version history.
155      *
156      * @param versionLabel the version label
157      * @return the version object
158      * @throws VersionDoesNotExistException indicates requested version does not exisit
159      */

160     public Version getVersion(String JavaDoc versionLabel)
161     {
162         Version result = null;
163         if (versionLabel != null)
164         {
165             result = this.versions.get(versionLabel);
166             
167             if (result == null)
168             {
169                 // Throw exception indicating that the version does not exit
170
throw new VersionDoesNotExistException(versionLabel);
171             }
172         }
173         return result;
174     }
175     
176     /**
177      * Add a version to the version history.
178      * <p>
179      * Used internally to build the version history tree.
180      *
181      * @param version the version object
182      * @param predecessor the preceeding version
183      */

184     public void addVersion(Version version, Version predecessor)
185     {
186         // TODO cope with exception case where duplicate version labels have been specified
187

188         this.versions.put(version.getVersionLabel(), version);
189         
190         if (predecessor != null)
191         {
192             this.versionHistory.put(version.getVersionLabel(), predecessor.getVersionLabel());
193         }
194     }
195 }
196
Popular Tags