Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-sdc-versioning-lib / openecomp-sdc-versioning-api / src / main / java / org / openecomp / sdc / versioning / dao / types / Version.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.versioning.dao.types;
22
23 import com.datastax.driver.mapping.annotations.Transient;
24 import com.datastax.driver.mapping.annotations.UDT;
25
26 import java.util.Date;
27 import java.util.Map;
28
29 @UDT(name = "version", keyspace = "dox")
30 public class Version {
31   public static final String VERSION_STRING_VIOLATION_MSG =
32       "Version string must be in the format of: {integer}.{integer}";
33
34   @Transient
35   private String id;
36   private int major; // TODO: 6/7/2017 remove!
37   private int minor; // TODO: 6/7/2017 remove!
38   @Transient
39   private String name;
40   @Transient
41   private String description;
42   @Transient
43   private String baseId;
44   @Transient
45   private Date creationTime;
46   @Transient
47   private Date modificationTime;
48   @Transient
49   private VersionStatus status = VersionStatus.Draft;
50   @Transient
51   private VersionState state;
52   @Transient
53   private Map<String, Object> additionalInfo;
54
55   public Version() {
56   }
57
58   public Version(String id) {
59     this.id = id;
60   }
61
62   @Deprecated
63   public Version(int major, int minor) {
64     this.major = major;
65     this.minor = minor;
66   }
67
68   /**
69    * Value of version.
70    *
71    * @param versionString the version string
72    * @return the version
73    */
74   public static Version valueOf(String versionString) {
75     if (versionString == null) {
76       return null;
77     }
78     String[] versionLevels = versionString.split("\\.");
79     Version version;
80     if (versionLevels.length != 2) {
81       throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
82     }
83     try {
84       version = new Version(Integer.parseInt(versionLevels[0]), Integer.parseInt(versionLevels[1]));
85     } catch (Exception ex) {
86       throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
87     }
88
89     return version;
90   }
91
92   public int getMajor() {
93     return major;
94   }
95
96   public void setMajor(int major) {
97     this.major = major;
98   }
99
100   public int getMinor() {
101     return minor;
102   }
103
104   public void setMinor(int minor) {
105     this.minor = minor;
106   }
107
108   public String getId() {
109     return id;
110   }
111
112   public void setId(String id) {
113     this.id = id;
114   }
115
116   public String getName() {
117     return name;
118   }
119
120   public void setName(String name) {
121     this.name = name;
122   }
123
124   public String getDescription() {
125     return description;
126   }
127
128   public void setDescription(String description) {
129     this.description = description;
130   }
131
132   public String getBaseId() {
133     return baseId;
134   }
135
136   public void setBaseId(String baseId) {
137     this.baseId = baseId;
138   }
139
140   public Date getCreationTime() {
141     return creationTime;
142   }
143
144   public void setCreationTime(Date creationTime) {
145     this.creationTime = creationTime;
146   }
147
148   public Date getModificationTime() {
149     return modificationTime;
150   }
151
152   public void setModificationTime(Date modificationTime) {
153     this.modificationTime = modificationTime;
154   }
155
156   public VersionStatus getStatus() {
157     return status;
158   }
159
160   public void setStatus(VersionStatus status) {
161     this.status = status;
162   }
163
164   public VersionState getState() {
165     return state;
166   }
167
168   public void setState(VersionState state) {
169     this.state = state;
170   }
171
172   public Version calculateNextCandidate() {
173     return new Version(major, minor + 1);
174   }
175
176   public Version calculateNextFinal() {
177     return new Version(major + 1, 0);
178   }
179
180   public boolean isFinal() {
181     return major != 0 && minor == 0;
182   }
183
184   public Map<String, Object> getAdditionalInfo() {
185     return additionalInfo;
186   }
187
188   public void setAdditionalInfo(Map<String, Object> additionalInfo) {
189     this.additionalInfo = additionalInfo;
190   }
191
192   @Override
193   public int hashCode() {
194     int result = major;
195     result = 31 * result + minor;
196     return result;
197   }
198
199   @Override
200   public boolean equals(Object obj) {
201     if (this == obj) {
202       return true;
203     }
204     if (obj == null || getClass() != obj.getClass()) {
205       return false;
206     }
207
208     Version version = (Version) obj;
209     return major == version.major && minor == version.minor;
210   }
211
212   public int compateTo(Version other){
213     if (this.major>other.major) {
214       return 1;
215     } else if(this.major<other.major){
216       return -1;
217     } else if(this.major == other.major){
218       return Integer.compare(this.minor,other.minor);
219     }
220     return 0;
221   }
222
223   @Override
224   public String toString() {
225     return name != null ? name : major + "." + minor;
226   }
227
228   @Override
229   public Version clone() {
230     Version version = new Version();
231     version.setStatus(this.getStatus());
232     version.setCreationTime(this.getCreationTime());
233     version.setName(this.getName());
234     version.setBaseId(this.getBaseId());
235     version.setMajor(this.major);
236     version.setMinor(this.minor);
237     version.setModificationTime(this.getModificationTime());
238     version.setDescription(this.description);
239     version.setId(this.getId());
240     return version;
241   }
242 }