Merge "Increase tests coverage in backend"
[vid.git] / vid-app-common / src / main / java / org / onap / vid / services / CategoryParameterServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
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 package org.onap.vid.services;
22
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;
34
35 import javax.ws.rs.ForbiddenException;
36 import java.util.*;
37 import java.util.stream.Collectors;
38
39
40 @Service
41 public class CategoryParameterServiceImpl implements CategoryParameterService {
42
43     public static final String OPTION_ALREADY_EXIST_FOR_CATEGORY = "Option %s already exist for category %s";
44     @Autowired
45     private DataAccessService dataAccessService;
46
47     private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(CategoryParameterServiceImpl.class);
48
49     public static class UnfoundedCategoryException extends RuntimeException {
50
51         public UnfoundedCategoryException(String message) {
52             super(message);
53         }
54     }
55
56     public static class UnfoundedCategoryOptionException extends RuntimeException {
57
58         public UnfoundedCategoryOptionException(String message) {
59             super(message);
60         }
61     }
62
63     public static class AlreadyExistOptionNameException extends RuntimeException {
64
65         public AlreadyExistOptionNameException(String message) {
66             super(message);
67         }
68     }
69
70     @Override
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);
74     }
75
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);
82     }
83
84     @Override
85     public AddCategoryOptionResponse createCategoryParameterOptions(String categoryName, AddCategoryOptionsRequest optionsRequest) {
86
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));
93                 continue;
94             }
95             String appId = categoryParameter.isIdSupported() ? UUID.randomUUID().toString() : optionName;
96             CategoryParameterOption categoryParameterOption = new CategoryParameterOption(appId, optionName, categoryParameter);
97             dataAccessService.saveDomainObject(categoryParameterOption, null);
98         }
99
100         return response;
101     }
102
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;
107             LOG.debug(msg);
108             throw new UnfoundedCategoryException(msg);
109         }
110
111
112         return categoryParameters.get(0);
113     }
114
115     @Override
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";
121             LOG.debug(msg);
122             throw new ForbiddenException(msg);
123         }
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;
127             LOG.debug(msg);
128             throw new UnfoundedCategoryOptionException(msg);
129         }
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;
134             LOG.debug(msg);
135             throw new AlreadyExistOptionNameException(msg);
136         }
137
138         categoryParameterOption.setName(option.getName());
139         dataAccessService.saveDomainObject(categoryParameterOption, null);
140
141         return response;
142     }
143
144     @Override
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;
149             LOG.debug(msg);
150             throw new UnfoundedCategoryException(msg);
151         }
152         CategoryParameter categoryParameter = categoryParameters.get(0);
153         Set<CategoryParameterOption> categoryOptions = categoryParameter.getOptions();
154         for (CategoryParameterOption categoryOption: categoryOptions) {
155             if(categoryOption.getName().equals(option.getName()))
156             {
157                 dataAccessService.deleteDomainObject(categoryOption, null);
158             }
159         }
160     }
161
162 }
163