Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-sdc-versioning-lib / openecomp-sdc-versioning-core / src / main / java / org / openecomp / sdc / versioning / impl / MajorVersionCalculatorImpl.java
1 package org.openecomp.sdc.versioning.impl;
2
3 import org.openecomp.core.utilities.CommonMethods;
4 import org.openecomp.sdc.versioning.VersionCalculator;
5 import org.openecomp.sdc.versioning.dao.types.Version;
6 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
7 import org.openecomp.sdc.versioning.types.VersionCreationMethod;
8
9 import java.util.HashSet;
10 import java.util.Set;
11
12 public class MajorVersionCalculatorImpl implements VersionCalculator {
13   private static final String INITIAL_VERSION = "1.0";
14   private static final String VERSION_STRING_VIOLATION_MSG =
15       "Version string must be in the format of: {integer}.{integer}";
16
17   @Override
18   public String calculate(String baseVersion, VersionCreationMethod creationMethod) {
19
20     if (baseVersion == null) {
21       return INITIAL_VERSION;
22     }
23
24     String[] versionLevels = baseVersion.split("\\.");
25     if (versionLevels.length != 2) {
26       throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
27     }
28
29     int index = Integer.parseInt(versionLevels[0]);
30     index++;
31     versionLevels[0] = Integer.toString(index);
32     versionLevels[1] = "0";
33
34     return CommonMethods.arrayToSeparatedString(versionLevels, '.');
35   }
36
37   @Override
38   public void injectAdditionalInfo(Version version, Set<String> existingVersions) {
39     String optionalVersion;
40     Set<VersionCreationMethod> optionalCreationMethods = new HashSet<>();
41     if(version.getStatus().equals(VersionStatus.Certified)) {
42       try {
43         optionalVersion = calculate(version.getName(), VersionCreationMethod.major);
44         if (!existingVersions.contains(optionalVersion)) {
45           optionalCreationMethods.add(VersionCreationMethod.major);
46         }
47       } catch (IllegalArgumentException iae) {
48         //not a valid creation method.
49       }
50     }
51     version.getAdditionalInfo().put("OptionalCreationMethods", optionalCreationMethods);
52   }
53 }