2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018 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.stereotype.Service;
35 import javax.ws.rs.ForbiddenException;
37 import java.util.stream.Collectors;
41 public class CategoryParameterServiceImpl implements CategoryParameterService {
43 public static final String OPTION_ALREADY_EXIST_FOR_CATEGORY = "Option %s already exist for category %s";
45 private DataAccessService dataAccessService;
47 private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(CategoryParameterServiceImpl.class);
49 public static class UnfoundedCategoryException extends RuntimeException {
51 public UnfoundedCategoryException(String message) {
56 public static class UnfoundedCategoryOptionException extends RuntimeException {
58 public UnfoundedCategoryOptionException(String message) {
63 public static class AlreadyExistOptionNameException extends RuntimeException {
65 public AlreadyExistOptionNameException(String message) {
71 public CategoryParametersResponse getCategoryParameters(Family familyName) {
72 List<CategoryParameter> categoryParameters = dataAccessService.getList(CategoryParameter.class, String.format(" where family = '%s' ",familyName), null, null);
73 return convertToCategoryParametersResponse(categoryParameters);
76 private CategoryParametersResponse convertToCategoryParametersResponse(List<CategoryParameter> categoryParameters) {
77 Comparator<CategoryParameterOptionRep> comparator = Comparator.comparing(CategoryParameterOptionRep::getName, String.CASE_INSENSITIVE_ORDER);
78 Map<String, List<CategoryParameterOptionRep>> categoryParametersMap = categoryParameters.stream().collect(Collectors.toMap(
79 CategoryParameter::getName,
80 x -> x.getOptions().stream().map(opt -> new CategoryParameterOptionRep(opt.getAppId(), opt.getName())).sorted(comparator).collect(Collectors.toList())));
81 return new CategoryParametersResponse(categoryParametersMap);
85 public AddCategoryOptionResponse createCategoryParameterOptions(String categoryName, AddCategoryOptionsRequest optionsRequest) {
87 AddCategoryOptionResponse response = new AddCategoryOptionResponse(new ArrayList<>());
88 CategoryParameter categoryParameter = getCategoryParameter(categoryName);
89 Set<String> categoryOptions = categoryParameter.getOptions().stream().map(CategoryParameterOption::getName).collect(Collectors.toSet());
90 for (String optionName : optionsRequest.options) {
91 if (categoryOptions.contains(optionName)) {
92 response.getErrors().add(String.format(OPTION_ALREADY_EXIST_FOR_CATEGORY, optionName, categoryName));
95 String appId = categoryParameter.isIdSupported() ? UUID.randomUUID().toString() : optionName;
96 CategoryParameterOption categoryParameterOption = new CategoryParameterOption(appId, optionName, categoryParameter);
97 dataAccessService.saveDomainObject(categoryParameterOption, null);
103 private CategoryParameter getCategoryParameter( String categoryName) {
104 List<CategoryParameter> categoryParameters = dataAccessService.getList(CategoryParameter.class, String.format(" where name = '%s' ", categoryName), null, null);
105 if (categoryParameters.size() != 1) {
106 String msg = "There is no category parameter with name " + categoryName;
108 throw new UnfoundedCategoryException(msg);
112 return categoryParameters.get(0);
116 public AddCategoryOptionResponse updateCategoryParameterOption(String categoryName, CategoryParameterOptionRep option) {
117 AddCategoryOptionResponse response = new AddCategoryOptionResponse(new ArrayList<>());
118 CategoryParameter categoryParameter = getCategoryParameter(categoryName);
119 if (!categoryParameter.isIdSupported()) {
120 String msg = "Updating option name for category: " + categoryName + ", is not allowed";
122 throw new ForbiddenException(msg);
124 Optional<CategoryParameterOption> categoryParameterOptionOptional = categoryParameter.getOptions().stream().filter(x->x.getAppId().equals(option.getId())).findFirst();
125 if (!categoryParameterOptionOptional.isPresent()) {
126 String msg = "There is no option with id "+option.getId() + " for category " + categoryName;
128 throw new UnfoundedCategoryOptionException(msg);
130 CategoryParameterOption categoryParameterOption = categoryParameterOptionOptional.get();
131 Optional<CategoryParameterOption> alreadyExistOptionWithName = categoryParameter.getOptions().stream().filter(x->x.getName().equals(option.getName())).findFirst();
132 if (alreadyExistOptionWithName.isPresent() && !alreadyExistOptionWithName.get().getAppId().equals(categoryParameterOption.getAppId())) {
133 String msg = "Option with name "+option.getName() + " already exist for category " + categoryName;
135 throw new AlreadyExistOptionNameException(msg);
138 categoryParameterOption.setName(option.getName());
139 dataAccessService.saveDomainObject(categoryParameterOption, null);
145 public void deleteCategoryOption(String categoryName, CategoryParameterOption option) {
146 List<CategoryParameter> categoryParameters = dataAccessService.getList(CategoryParameter.class, String.format(" where name = '%s'", categoryName), null, null);
147 if (categoryParameters.size() != 1) {
148 String msg = "There is no category parameter with name " + categoryName;
150 throw new UnfoundedCategoryException(msg);
152 CategoryParameter categoryParameter = categoryParameters.get(0);
153 Set<CategoryParameterOption> categoryOptions = categoryParameter.getOptions();
154 for (CategoryParameterOption categoryOption: categoryOptions) {
155 if(categoryOption.getName().equals(option.getName()))
157 dataAccessService.deleteDomainObject(categoryOption, null);