232b0038efa1ce92681e23820f3d1b702f68b78c
[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 updateName(String itemId, String name) {
80     Item item = get(itemId);
81     if (item == null) {
82       return;
83     }
84
85     item.setName(name);
86     itemDao.update(item);
87   }
88
89   @Override
90   public void archive(Item item) {
91
92     if (item.getStatus() == ItemStatus.ARCHIVED) {
93       throw new CoreException(new ErrorCode.ErrorCodeBuilder()
94           .withCategory(ErrorCategory.APPLICATION)
95           .withMessage(String.format("Archive item failed, item %s is already Archived", item.getId()))
96           .build());
97     }
98
99     item.setStatus(ItemStatus.ARCHIVED);
100     itemDao.update(item);
101   }
102
103   @Override
104   public void restore(Item item) {
105
106       if (item.getStatus() == ItemStatus.ACTIVE) {
107           throw new CoreException(new ErrorCode.ErrorCodeBuilder()
108                   .withCategory(ErrorCategory.APPLICATION)
109                   .withMessage(String.format("Restore item failed, item %s is already Active", item.getId()))
110                   .build());
111       }
112
113       item.setStatus(ItemStatus.ACTIVE);
114       itemDao.update(item);
115     }
116
117   @Override
118   public void update(Item item) {
119     itemDao.update(item);
120   }
121 }