2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.tosca;
26 import com.google.common.collect.Sets;
27 import java.util.List;
29 import java.util.stream.Collectors;
30 import javax.persistence.EntityNotFoundException;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Service;
35 public class DictionaryService {
37 private final DictionaryRepository dictionaryRepository;
38 private final DictionaryElementsRepository dictionaryElementsRepository;
44 public DictionaryService(DictionaryRepository dictionaryRepository,
45 DictionaryElementsRepository dictionaryElementsRepository) {
46 this.dictionaryRepository = dictionaryRepository;
47 this.dictionaryElementsRepository = dictionaryElementsRepository;
51 public Dictionary saveOrUpdateDictionary(Dictionary dictionary) {
52 return dictionaryRepository.save(dictionary);
56 * Creates or Updates Dictionary Element.
58 * @param dictionaryName The Dictionary name
59 * @param dictionary The Dictionary object with dictionary elements
60 * @return updated Dictionary object with all dictionary elements
62 public Dictionary saveOrUpdateDictionaryElement(String dictionaryName, Dictionary dictionary) {
63 Dictionary dict = getDictionary(dictionaryName);
65 Set<DictionaryElement> newDictionaryElements = dictionary.getDictionaryElements();
67 if (newDictionaryElements != null && !newDictionaryElements.isEmpty()) {
68 Set<DictionaryElement> updatedDictionaryElements = newDictionaryElements.stream()
69 .map(dictionaryElement -> getAndUpdateDictionaryElement(dict, dictionaryElement))
70 .collect(Collectors.toSet());
72 dict.getDictionaryElements().forEach(dictElement -> {
73 if (!updatedDictionaryElements.contains(dictElement)) {
74 updatedDictionaryElements.add(dictElement);
77 dict.setDictionaryElements(updatedDictionaryElements);
79 return dictionaryRepository.save(dict);
83 private DictionaryElement getAndUpdateDictionaryElement(Dictionary dictionary,
84 DictionaryElement element) {
85 return dictionaryElementsRepository
86 .save(dictionaryElementsRepository.findById(element.getShortName())
87 .map(p -> updateDictionaryElement(p, element, dictionary))
88 .orElse(new DictionaryElement(element.getName(), element.getShortName(),
89 element.getDescription(), element.getType(), element.getSubDictionary(),
90 Sets.newHashSet(dictionary))));
93 public void deleteDictionary(Dictionary dictionary) {
94 dictionaryRepository.delete(dictionary);
97 public void deleteDictionary(String dictionaryName) {
98 dictionaryRepository.deleteById(dictionaryName);
101 public List<Dictionary> getAllDictionaries() {
102 return dictionaryRepository.findAll();
105 public List<String> getAllSecondaryLevelDictionaryNames() {
106 return dictionaryRepository.getAllSecondaryLevelDictionaryNames();
109 public Dictionary getDictionary(String dictionaryName) {
110 return dictionaryRepository.findById(dictionaryName).orElseThrow(
111 () -> new EntityNotFoundException("Couldn't find Dictionary named: " + dictionaryName));
115 * Deletes a dictionary element from Dictionary by shortName.
117 * @param dictionaryName The dictionary name
118 * @param dictionaryElementShortName the dictionary Element Short name
120 public void deleteDictionaryElement(String dictionaryName, String dictionaryElementShortName) {
121 if (dictionaryRepository.existsById(dictionaryName)) {
122 DictionaryElement element =
123 dictionaryElementsRepository.findById(dictionaryElementShortName).orElse(null);
124 if (element != null) {
125 Dictionary dict = getDictionary(dictionaryName);
126 dict.removeDictionaryElement(element);
127 dictionaryRepository.save(dict);
132 private DictionaryElement updateDictionaryElement(DictionaryElement oldDictionaryElement,
133 DictionaryElement newDictionaryElement, Dictionary dictionary) {
134 oldDictionaryElement.setName(newDictionaryElement.getName());
135 oldDictionaryElement.setDescription(newDictionaryElement.getDescription());
136 oldDictionaryElement.setType(newDictionaryElement.getType());
137 oldDictionaryElement.setSubDictionary(newDictionaryElement.getSubDictionary());
138 oldDictionaryElement.getUsedByDictionaries().add(dictionary);
139 return oldDictionaryElement;