b0a14fd61d5634e057d97f6330553e0dd246db51
[sdc/sdc-workflow-designer.git] /
1 package org.onap.sdc.workflow.services.impl;
2
3 import java.util.Collection;
4 import java.util.List;
5 import java.util.Objects;
6 import org.onap.sdc.workflow.services.WorkflowVersionManager;
7 import org.onap.sdc.workflow.services.exceptions.VersionNotFoundException;
8 import org.openecomp.sdc.versioning.VersioningManager;
9 import org.openecomp.sdc.versioning.dao.types.Version;
10 import org.openecomp.sdc.versioning.types.VersionCreationMethod;
11 import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.stereotype.Service;
13
14 @Service("workflowVersionManager")
15 public class WorkflowVersionManagerImpl  implements WorkflowVersionManager {
16
17     private final VersioningManager versioningManager;
18
19     @Autowired
20     public WorkflowVersionManagerImpl(VersioningManager versioningManager) {
21         this.versioningManager = versioningManager;
22     }
23
24     @Override
25     public Collection<Version> list(String id) {
26         return versioningManager.list(id);
27     }
28
29     @Override
30     public Version get(String id,Version version) {
31
32         try {
33             return versioningManager.get(id, version);
34         } catch (Exception e){
35             throw new VersionNotFoundException(id,version.getId());
36         }
37     }
38
39     @Override
40     public Version create(String id, Version version) {
41         if (Objects.nonNull(getLatestVersion(id)))
42             version.setBaseId(getLatestVersion(id).getId());
43         return versioningManager.create(id,version, VersionCreationMethod.major);
44     }
45
46     @Override
47     public void update(String id,Version version) {
48
49         versioningManager.updateVersion(id,version);
50     }
51
52     protected Version getLatestVersion(String itemId) {
53         List<Version> list = versioningManager.list(itemId);
54         return list.stream().max(Version::compareTo).orElse(null);
55     }
56
57 }