Modify the Ui
[clamp.git] / src / main / java / org / onap / clamp / tosca / DictionaryService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.tosca;
25
26 import com.google.common.collect.Sets;
27 import java.util.List;
28 import java.util.Set;
29 import javax.persistence.EntityNotFoundException;
30 import org.onap.clamp.clds.service.SecureServiceBase;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Service;
33
34 @Service
35 public class DictionaryService extends SecureServiceBase {
36
37     private final DictionaryRepository dictionaryRepository;
38     private final DictionaryElementsRepository dictionaryElementsRepository;
39
40     /**
41      * Constructor.
42      */
43     @Autowired
44     public DictionaryService(DictionaryRepository dictionaryRepository,
45         DictionaryElementsRepository dictionaryElementsRepository) {
46         this.dictionaryRepository = dictionaryRepository;
47         this.dictionaryElementsRepository = dictionaryElementsRepository;
48
49     }
50
51     public Dictionary saveOrUpdateDictionary(Dictionary dictionary) {
52         return dictionaryRepository.save(dictionary);
53     }
54
55     /**
56      * Creates or Updates Dictionary Element.
57      *
58      * @param dictionaryName The Dictionary name
59      * @param dictionary The Dictionary object with dictionary elements
60      * @return updated Dictionary object with all dictionary elements
61      */
62     public Dictionary saveOrUpdateDictionaryElement(String dictionaryName, Dictionary dictionary) {
63         Dictionary dict = getDictionary(dictionaryName);
64
65         Set<DictionaryElement> newDictionaryElements = dictionary.getDictionaryElements();
66
67         for (DictionaryElement dictionaryElement : newDictionaryElements) {
68             if (dict.getDictionaryElements().contains(dictionaryElement)) {
69                 // Update the Dictionary Element
70                 getAndUpdateDictionaryElement(dict, dictionaryElement);
71             } else {
72                 // Create the Dictionary Element
73                 dict.addDictionaryElements(getAndUpdateDictionaryElement(dict, dictionaryElement));
74                 dictionaryRepository.save(dict);
75             }
76         }
77
78         // Fetch again to get Dictionary with most recent updates.
79         return dictionaryRepository.findById(dictionaryName).orElseThrow(
80             () -> new EntityNotFoundException("Couldn't find Dictionary named: " + dictionaryName));
81
82     }
83
84     private DictionaryElement getAndUpdateDictionaryElement(Dictionary dictionary,
85         DictionaryElement element) {
86         return dictionaryElementsRepository
87             .save(dictionaryElementsRepository.findById(element.getShortName())
88                 .map(p -> updateDictionaryElement(p, element, dictionary))
89                 .orElse(new DictionaryElement(element.getName(), element.getShortName(),
90                     element.getDescription(), element.getType(), element.getSubDictionary(),
91                     Sets.newHashSet(dictionary))));
92     }
93
94     public void deleteDictionary(Dictionary dictionary) {
95         dictionaryRepository.delete(dictionary);
96     }
97
98     public void deleteDictionary(String dictionaryName) {
99         dictionaryRepository.deleteById(dictionaryName);
100     }
101
102     public List<Dictionary> getAllDictionaries() {
103         return dictionaryRepository.findAll();
104     }
105
106     public List<String> getAllSecondaryLevelDictionaryNames() {
107         return dictionaryRepository.getAllSecondaryLevelDictionaryNames();
108     }
109
110     public Dictionary getDictionary(String dictionaryName) {
111         return dictionaryRepository.findById(dictionaryName).orElseThrow(
112             () -> new EntityNotFoundException("Couldn't find Dictionary named: " + dictionaryName));
113     }
114
115     /**
116      * Deletes a dictionary element from Dictionary by shortName.
117      *
118      * @param dictionaryName The dictionary name
119      * @param dictionaryElementShortName the dictionary Element Short name
120      */
121     public void deleteDictionaryElement(String dictionaryName, String dictionaryElementShortName) {
122         if (dictionaryRepository.existsById(dictionaryName)) {
123             DictionaryElement element =
124                 dictionaryElementsRepository.findById(dictionaryElementShortName).orElse(null);
125             if (element != null) {
126                 Dictionary dict = getDictionary(dictionaryName);
127                 dict.removeDictionaryElement(element);
128                 dictionaryRepository.save(dict);
129             }
130         }
131     }
132
133     private DictionaryElement updateDictionaryElement(DictionaryElement oldDictionaryElement,
134         DictionaryElement newDictionaryElement, Dictionary dictionary) {
135         oldDictionaryElement.setName(newDictionaryElement.getName());
136         oldDictionaryElement.setDescription(newDictionaryElement.getDescription());
137         oldDictionaryElement.setType(newDictionaryElement.getType());
138         oldDictionaryElement.setSubDictionary(newDictionaryElement.getSubDictionary());
139         oldDictionaryElement.getUsedByDictionaries().add(dictionary);
140         return oldDictionaryElement;
141     }
142 }