2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018 - 2019 Nokia. All rights 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
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=========================================================
21 package org.onap.vid.services;
23 import org.onap.vid.category.AddCategoryOptionResponse;
24 import org.onap.vid.category.AddCategoryOptionsRequest;
25 import org.onap.vid.category.CategoryParameterOptionRep;
26 import org.onap.vid.category.CategoryParametersResponse;
27 import org.onap.vid.model.CategoryParameter;
28 import org.onap.vid.model.CategoryParameter.Family;
29 import org.onap.vid.model.CategoryParameterOption;
30 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
31 import org.onap.portalsdk.core.service.DataAccessService;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.context.annotation.Primary;
34 import org.springframework.stereotype.Service;
36 import javax.ws.rs.ForbiddenException;
38 import java.util.stream.Collectors;
43 public class CategoryParameterServiceImpl implements CategoryParameterService {
45 public static final String OPTION_ALREADY_EXIST_FOR_CATEGORY = "Option %s already exist for category %s";
47 private DataAccessService dataAccessService;
49 private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(CategoryParameterServiceImpl.class);
51 public static class UnfoundedCategoryException extends RuntimeException {
53 public UnfoundedCategoryException(String message) {
58 public static class UnfoundedCategoryOptionException extends RuntimeException {
60 public UnfoundedCategoryOptionException(String message) {
65 public static class AlreadyExistOptionNameException extends RuntimeException {
67 public AlreadyExistOptionNameException(String message) {
73 public CategoryParametersResponse getCategoryParameters(Family familyName) {
74 List<CategoryParameter> categoryParameters = dataAccessService.getList(CategoryParameter.class, String.format(" where family = '%s' ",familyName), null, null);
75 return convertToCategoryParametersResponse(categoryParameters);
78 private CategoryParametersResponse convertToCategoryParametersResponse(List<CategoryParameter> categoryParameters) {
79 Comparator<CategoryParameterOptionRep> comparator = Comparator.comparing(CategoryParameterOptionRep::getName, String.CASE_INSENSITIVE_ORDER);
80 Map<String, List<CategoryParameterOptionRep>> categoryParametersMap = categoryParameters.stream().collect(Collectors.toMap(
81 CategoryParameter::getName,
82 x -> x.getOptions().stream().map(opt -> new CategoryParameterOptionRep(opt.getAppId(), opt.getName())).sorted(comparator).collect(Collectors.toList())));
83 return new CategoryParametersResponse(categoryParametersMap);
87 public AddCategoryOptionResponse createCategoryParameterOptions(String categoryName, AddCategoryOptionsRequest optionsRequest) {
89 AddCategoryOptionResponse response = new AddCategoryOptionResponse(new ArrayList<>());
90 CategoryParameter categoryParameter = getCategoryParameter(categoryName);
91 Set<String> categoryOptions = categoryParameter.getOptions().stream().map(CategoryParameterOption::getName).collect(Collectors.toSet());
92 for (String optionName : optionsRequest.options) {
93 if (categoryOptions.contains(optionName)) {
94 response.getErrors().add(String.format(OPTION_ALREADY_EXIST_FOR_CATEGORY, optionName, categoryName));
97 String appId = categoryParameter.isIdSupported() ? UUID.randomUUID().toString() : optionName;
98 CategoryParameterOption categoryParameterOption = new CategoryParameterOption(appId, optionName, categoryParameter);
99 dataAccessService.saveDomainObject(categoryParameterOption, null);
105 private CategoryParameter getCategoryParameter( String categoryName) {
106 List<CategoryParameter> categoryParameters = dataAccessService.getList(CategoryParameter.class, String.format(" where name = '%s' ", categoryName), null, null);
107 if (categoryParameters.size() != 1) {
108 String msg = "There is no category parameter with name " + categoryName;
110 throw new UnfoundedCategoryException(msg);
114 return categoryParameters.get(0);
118 public AddCategoryOptionResponse updateCategoryParameterOption(String categoryName, CategoryParameterOptionRep option) {
119 AddCategoryOptionResponse response = new AddCategoryOptionResponse(new ArrayList<>());
120 CategoryParameter categoryParameter = getCategoryParameter(categoryName);
121 if (!categoryParameter.isIdSupported()) {
122 String msg = "Updating option name for category: " + categoryName + ", is not allowed";
124 throw new ForbiddenException(msg);
126 Optional<CategoryParameterOption> categoryParameterOptionOptional = categoryParameter.getOptions().stream().filter(x->x.getAppId().equals(option.getId())).findFirst();
127 if (!categoryParameterOptionOptional.isPresent()) {
128 String msg = "There is no option with id "+option.getId() + " for category " + categoryName;
130 throw new UnfoundedCategoryOptionException(msg);
132 CategoryParameterOption categoryParameterOption = categoryParameterOptionOptional.get();
133 Optional<CategoryParameterOption> alreadyExistOptionWithName = categoryParameter.getOptions().stream().filter(x->x.getName().equals(option.getName())).findFirst();
134 if (alreadyExistOptionWithName.isPresent() && !alreadyExistOptionWithName.get().getAppId().equals(categoryParameterOption.getAppId())) {
135 String msg = "Option with name "+option.getName() + " already exist for category " + categoryName;
137 throw new AlreadyExistOptionNameException(msg);
140 categoryParameterOption.setName(option.getName());
141 dataAccessService.saveDomainObject(categoryParameterOption, null);
147 public void deleteCategoryOption(String categoryName, CategoryParameterOption option) {
148 List<CategoryParameter> categoryParameters = dataAccessService.getList(CategoryParameter.class, String.format(" where name = '%s'", categoryName), null, null);
149 if (categoryParameters.size() != 1) {
150 String msg = "There is no category parameter with name " + categoryName;
152 throw new UnfoundedCategoryException(msg);
154 CategoryParameter categoryParameter = categoryParameters.get(0);
155 Set<CategoryParameterOption> categoryOptions = categoryParameter.getOptions();
156 for (CategoryParameterOption categoryOption: categoryOptions) {
157 if(categoryOption.getName().equals(option.getName()))
159 dataAccessService.deleteDomainObject(categoryOption, null);