re base code
[sdc.git] / openecomp-be / lib / openecomp-sdc-versioning-lib / openecomp-sdc-versioning-core / src / main / java / org / openecomp / sdc / versioning / impl / ItemManagerImpl.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.versioning.impl;
18
19 import org.openecomp.sdc.common.errors.CoreException;
20 import org.openecomp.sdc.common.errors.ErrorCategory;
21 import org.openecomp.sdc.common.errors.ErrorCode;
22 import org.openecomp.sdc.versioning.ItemManager;
23 import org.openecomp.sdc.versioning.dao.ItemDao;
24 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
25 import org.openecomp.sdc.versioning.types.Item;
26 import org.openecomp.sdc.versioning.types.ItemStatus;
27
28 import java.util.Collection;
29 import java.util.function.Predicate;
30 import java.util.stream.Collectors;
31
32 public class ItemManagerImpl implements ItemManager {
33
34   private ItemDao itemDao;
35
36
37   public ItemManagerImpl(ItemDao itemDao) {
38     this.itemDao = itemDao;
39
40   }
41
42   @Override
43   public Collection<Item> list(Predicate<Item> predicate) {
44     return itemDao.list().stream().filter(predicate).collect(Collectors.toList());
45   }
46
47   @Override
48   public Item get(String itemId) {
49     Item item = new Item();
50     item.setId(itemId);
51     return itemDao.get(item);
52   }
53
54   @Override
55   public Item create(Item item) {
56     return itemDao.create(item);
57   }
58
59   @Override
60   public void updateVersionStatus(String itemId, VersionStatus addedVersionStatus,
61                                   VersionStatus removedVersionStatus) {
62     Item item = get(itemId);
63     if (item == null) {
64       return;
65     }
66
67     item.addVersionStatus(addedVersionStatus);
68     if (removedVersionStatus != null) {
69       item.removeVersionStatus(removedVersionStatus);
70     }
71     itemDao.update(item);
72   }
73
74   @Override
75   public void delete(Item item) {
76     itemDao.delete(item);
77   }
78
79   @Override
80   public void updateName(String itemId, String name) {
81     Item item = get(itemId);
82     if (item == null) {
83       return;
84     }
85
86     item.setName(name);
87     itemDao.update(item);
88   }
89
90   @Override
91   public void archive(Item item) {
92
93     if (item.getStatus() == ItemStatus.ARCHIVED) {
94       throw new CoreException(new ErrorCode.ErrorCodeBuilder()
95           .withCategory(ErrorCategory.APPLICATION)
96           .withMessage(String.format("Archive item failed, item %s is already Archived", item.getId()))
97           .build());
98     }
99
100     item.setStatus(ItemStatus.ARCHIVED);
101     itemDao.update(item);
102   }
103
104   @Override
105   public void restore(Item item) {
106
107       if (item.getStatus() == ItemStatus.ACTIVE) {
108           throw new CoreException(new ErrorCode.ErrorCodeBuilder()
109                   .withCategory(ErrorCategory.APPLICATION)
110                   .withMessage(String.format("Restore item failed, item %s is already Active", item.getId()))
111                   .build());
112       }
113
114       item.setStatus(ItemStatus.ACTIVE);
115       itemDao.update(item);
116     }
117
118   @Override
119   public void update(Item item) {
120     itemDao.update(item);
121   }
122 }