Fix for Penetration test _ Session and cookie management
[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 - 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
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.context.annotation.Primary;
34 import org.springframework.stereotype.Service;
35
36 import javax.ws.rs.ForbiddenException;
37 import java.util.*;
38 import java.util.stream.Collectors;
39
40
41 @Service
42 @Primary
43 public class CategoryParameterServiceImpl implements CategoryParameterService {
44
45     public static final String OPTION_ALREADY_EXIST_FOR_CATEGORY = "Option %s already exist for category %s";
46     @Autowired
47     private DataAccessService dataAccessService;
48
49     private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(CategoryParameterServiceImpl.class);
50
51     public static class UnfoundedCategoryException extends RuntimeException {
52
53         public UnfoundedCategoryException(String message) {
54             super(message);
55         }
56     }
57
58     public static class UnfoundedCategoryOptionException extends RuntimeException {
59
60         public UnfoundedCategoryOptionException(String message) {
61             super(message);
62         }
63     }
64
65     public static class AlreadyExistOptionNameException extends RuntimeException {
66
67         public AlreadyExistOptionNameException(String message) {
68             super(message);
69         }
70     }
71
72     @Override
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);
76     }
77
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);
84     }
85
86     @Override
87     public AddCategoryOptionResponse createCategoryParameterOptions(String categoryName, AddCategoryOptionsRequest optionsRequest) {
88
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));
95                 continue;
96             }
97             String appId = categoryParameter.isIdSupported() ? UUID.randomUUID().toString() : optionName;
98             CategoryParameterOption categoryParameterOption = new CategoryParameterOption(appId, optionName, categoryParameter);
99             dataAccessService.saveDomainObject(categoryParameterOption, null);
100         }
101
102         return response;
103     }
104
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;
109             LOG.debug(msg);
110             throw new UnfoundedCategoryException(msg);
111         }
112
113
114         return categoryParameters.get(0);
115     }
116
117     @Override
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";
123             LOG.debug(msg);
124             throw new ForbiddenException(msg);
125         }
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;
129             LOG.debug(msg);
130             throw new UnfoundedCategoryOptionException(msg);
131         }
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;
136             LOG.debug(msg);
137             throw new AlreadyExistOptionNameException(msg);
138         }
139
140         categoryParameterOption.setName(option.getName());
141         dataAccessService.saveDomainObject(categoryParameterOption, null);
142
143         return response;
144     }
145
146     @Override
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;
151             LOG.debug(msg);
152             throw new UnfoundedCategoryException(msg);
153         }
154         CategoryParameter categoryParameter = categoryParameters.get(0);
155         Set<CategoryParameterOption> categoryOptions = categoryParameter.getOptions();
156         for (CategoryParameterOption categoryOption: categoryOptions) {
157             if(categoryOption.getName().equals(option.getName()))
158             {
159                 dataAccessService.deleteDomainObject(categoryOption, null);
160             }
161         }
162     }
163
164 }
165