1 17 package org.alfresco.repo.version.common; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 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 33 public class VersionHistoryImpl implements VersionHistory 34 { 35 38 private static final long serialVersionUID = 3257001051558326840L; 39 40 43 private static final String ERR_MSG = "The root version must be specified when creating a version history object."; 44 45 48 private String rootVersionLabel = null; 49 50 53 private HashMap <String , String > versionHistory = null; 54 55 58 private HashMap <String , Version> versions = null; 59 60 private Version rootVersion; 61 62 67 public VersionHistoryImpl(Version rootVersion) 68 { 69 if (rootVersion == null) 70 { 71 throw new VersionServiceException(VersionHistoryImpl.ERR_MSG); 74 } 75 76 this.versionHistory = new HashMap <String , String >(); 77 this.versions = new HashMap <String , Version>(); 78 79 this.rootVersion = rootVersion; 80 this.rootVersionLabel = rootVersion.getVersionLabel(); 81 addVersion(rootVersion, null); 82 } 83 84 89 public Version getRootVersion() 90 { 91 return this.rootVersion; 92 } 93 94 102 public Collection <Version> getAllVersions() 103 { 104 return this.versions.values(); 105 } 106 107 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 129 public Collection <Version> getSuccessors(Version version) 130 { 131 ArrayList <Version> result = new ArrayList <Version>(); 132 133 if (version != null) 134 { 135 String versionLabel = version.getVersionLabel(); 136 137 if (this.versionHistory.containsValue(versionLabel) == true) 138 { 139 for (String 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 160 public Version getVersion(String 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 new VersionDoesNotExistException(versionLabel); 171 } 172 } 173 return result; 174 } 175 176 184 public void addVersion(Version version, Version predecessor) 185 { 186 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 |