3b7b018678362b04df9d472534b91e9b501ce64e
[sdc.git] /
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 java.util.Collection;
20 import java.util.function.Predicate;
21 import java.util.stream.Collectors;
22 import org.openecomp.sdc.common.errors.CoreException;
23 import org.openecomp.sdc.common.errors.ErrorCategory;
24 import org.openecomp.sdc.common.errors.ErrorCode;
25 import org.openecomp.sdc.versioning.ItemManager;
26 import org.openecomp.sdc.versioning.dao.ItemDao;
27 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
28 import org.openecomp.sdc.versioning.types.Item;
29 import org.openecomp.sdc.versioning.types.ItemStatus;
30
31 public class ItemManagerImpl implements ItemManager {
32
33   private ItemDao itemDao;
34
35
36   public ItemManagerImpl(ItemDao itemDao) {
37     this.itemDao = itemDao;
38
39   }
40
41   @Override
42   public Collection<Item> list(Predicate<Item> predicate) {
43     return itemDao.list().stream().filter(predicate).collect(Collectors.toList());
44   }
45
46   @Override
47   public Item get(String itemId) {
48     Item item = new Item();
49     item.setId(itemId);
50     return itemDao.get(item);
51   }
52
53   @Override
54   public Item create(Item item) {
55     return itemDao.create(item);
56   }
57
58   @Override
59   public void updateVersionStatus(String itemId, VersionStatus addedVersionStatus,
60                                   VersionStatus removedVersionStatus) {
61     Item item = get(itemId);
62     if (item == null) {
63       return;
64     }
65
66     item.addVersionStatus(addedVersionStatus);
67     if (removedVersionStatus != null) {
68       item.removeVersionStatus(removedVersionStatus);
69     }
70     itemDao.update(item);
71   }
72
73   @Override
74   public void delete(Item item) {
75     itemDao.delete(item);
76   }
77
78   @Override
79   public void archive(Item item) {
80
81     if (item.getStatus() == ItemStatus.ARCHIVED) {
82       throw new CoreException(new ErrorCode.ErrorCodeBuilder()
83           .withCategory(ErrorCategory.APPLICATION)
84           .withMessage(String.format("Archive item failed, item %s is already Archived", item.getId()))
85           .build());
86     }
87
88     item.setStatus(ItemStatus.ARCHIVED);
89     itemDao.update(item);
90   }
91
92   @Override
93   public void restore(Item item) {
94
95       if (item.getStatus() == ItemStatus.ACTIVE) {
96           throw new CoreException(new ErrorCode.ErrorCodeBuilder()
97                   .withCategory(ErrorCategory.APPLICATION)
98                   .withMessage(String.format("Restore item failed, item %s is already Active", item.getId()))
99                   .build());
100       }
101
102       item.setStatus(ItemStatus.ACTIVE);
103       itemDao.update(item);
104     }
105
106   @Override
107   public void update(Item item) {
108     itemDao.update(item);
109   }
110 }