org.onap migration
[vid.git] / vid-app-common / src / main / java / org / onap / vid / services / CategoryParameterServiceImpl.java
1 package org.onap.vid.services;
2
3 import java.io.IOException;
4 import java.util.*;
5 import java.util.stream.Collectors;
6
7 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
8 import org.openecomp.portalsdk.core.service.DataAccessService;
9 import org.onap.vid.category.AddCategoryOptionResponse;
10 import org.onap.vid.category.CategoryParameterOptionRep;
11 import org.onap.vid.model.CategoryParameter;
12 import org.onap.vid.model.CategoryParameterOption;
13 import org.onap.vid.category.AddCategoryOptionsRequest;
14 import org.onap.vid.category.CategoryParametersResponse;
15 import org.springframework.beans.factory.annotation.Autowired;
16 import org.springframework.stereotype.Service;
17 import org.onap.vid.model.CategoryParameter.Family;
18
19 import javax.ws.rs.ForbiddenException;
20
21
22 @Service
23 public class CategoryParameterServiceImpl implements CategoryParameterService {
24
25     @Autowired
26     private DataAccessService dataAccessService;
27
28     private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(CategoryParameterServiceImpl.class);
29
30     public static class UnfoundedCategoryException extends RuntimeException {
31
32         public UnfoundedCategoryException(String message) {
33             super(message);
34         }
35     }
36
37     public static class UnfoundedCategoryOptionException extends RuntimeException {
38
39         public UnfoundedCategoryOptionException(String message) {
40             super(message);
41         }
42     }
43
44     public static class AlreadyExistOptionNameException extends RuntimeException {
45
46         public AlreadyExistOptionNameException(String message) {
47             super(message);
48         }
49     }
50
51     @Override
52     public CategoryParametersResponse getCategoryParameters(Family familyName) throws IOException {
53         List<CategoryParameter> categoryParameters = dataAccessService.getList(CategoryParameter.class, String.format(" where family = '%s' ",familyName), null, null);
54         return convertToCategoryParametersResponse(categoryParameters);
55     }
56
57     private CategoryParametersResponse convertToCategoryParametersResponse(List<CategoryParameter> categoryParameters) {
58         Comparator<CategoryParameterOptionRep> comparator = Comparator.comparing(CategoryParameterOptionRep::getName, String.CASE_INSENSITIVE_ORDER);
59         Map<String, List<CategoryParameterOptionRep>> categoryParametersMap = categoryParameters.stream().collect(Collectors.toMap(
60                 CategoryParameter::getName,
61                 x ->  x.getOptions().stream().map(opt -> new CategoryParameterOptionRep(opt.getAppId(), opt.getName())).sorted(comparator).collect(Collectors.toList())));
62         return new CategoryParametersResponse(categoryParametersMap);
63     }
64
65     @Override
66     public AddCategoryOptionResponse createCategoryParameterOptions(String categoryName, AddCategoryOptionsRequest optionsRequest) throws IOException, UnfoundedCategoryException {
67
68         AddCategoryOptionResponse response = new AddCategoryOptionResponse(new ArrayList<>());
69         CategoryParameter categoryParameter = getCategoryParameter(categoryName);
70         Set<String> categoryOptions = categoryParameter.getOptions().stream().map(CategoryParameterOption::getName).collect(Collectors.toSet());
71         for (String optionName : optionsRequest.options) {
72             if (categoryOptions.contains(optionName)) {
73                 response.getErrors().add(String.format("Option %s already exist for category %s", optionName, categoryName));
74                 continue;
75             }
76             String appId = categoryParameter.isIdSupported() ? UUID.randomUUID().toString() : optionName;
77             CategoryParameterOption categoryParameterOption = new CategoryParameterOption(appId, optionName, categoryParameter);
78             dataAccessService.saveDomainObject(categoryParameterOption, null);
79         }
80
81         return response;
82     }
83
84     private CategoryParameter getCategoryParameter( String categoryName) {
85         List<CategoryParameter> categoryParameters = dataAccessService.getList(CategoryParameter.class, String.format(" where name = '%s' ", categoryName), null, null);
86         if (categoryParameters.size() != 1) {
87             String msg = "There is no category parameter with name " + categoryName;
88             LOG.debug(msg);
89             throw new UnfoundedCategoryException(msg);
90         }
91
92
93         return categoryParameters.get(0);
94     }
95
96     @Override
97     public AddCategoryOptionResponse updateCategoryParameterOption(String categoryName, CategoryParameterOptionRep option) {
98         AddCategoryOptionResponse response = new AddCategoryOptionResponse(new ArrayList<>());
99         CategoryParameter categoryParameter = getCategoryParameter(categoryName);
100         if (!categoryParameter.isIdSupported()) {
101             String msg = "Updating option name for category: " + categoryName + ", is not allowed";
102             LOG.debug(msg);
103             throw new ForbiddenException(msg);
104         }
105         Optional<CategoryParameterOption> categoryParameterOptionOptional = categoryParameter.getOptions().stream().filter(x->x.getAppId().equals(option.getId())).findFirst();
106         if (!categoryParameterOptionOptional.isPresent()) {
107             String msg = "There is no option with id "+option.getId() + " for category " + categoryName;
108             LOG.debug(msg);
109             throw new UnfoundedCategoryOptionException(msg);
110         }
111         CategoryParameterOption categoryParameterOption = categoryParameterOptionOptional.get();
112         Optional<CategoryParameterOption> alreadyExistOptionWithName = categoryParameter.getOptions().stream().filter(x->x.getName().equals(option.getName())).findFirst();
113         if (alreadyExistOptionWithName.isPresent() && !alreadyExistOptionWithName.get().getAppId().equals(categoryParameterOption.getAppId())) {
114             String msg = "Option with name "+option.getName() + " already exist for category " + categoryName;
115             LOG.debug(msg);
116             throw new AlreadyExistOptionNameException(msg);
117         }
118
119         categoryParameterOption.setName(option.getName());
120         dataAccessService.saveDomainObject(categoryParameterOption, null);
121
122         return response;
123     }
124
125     @Override
126     public void deleteCategoryOption(String categoryName, CategoryParameterOption option) throws IOException {
127         List<CategoryParameter> categoryParameters = dataAccessService.getList(CategoryParameter.class, String.format(" where name = '%s'", categoryName), null, null);
128         if (categoryParameters.size() != 1) {
129             String msg = "There is no category parameter with name " + categoryName;
130             LOG.debug(msg);
131             throw new UnfoundedCategoryException(msg);
132         }
133         CategoryParameter categoryParameter = categoryParameters.get(0);
134         Set<CategoryParameterOption> categoryOptions = categoryParameter.getOptions();
135         for (CategoryParameterOption categoryOption: categoryOptions) {
136             if(categoryOption.getName().equals(option.getName()))
137             {
138                 dataAccessService.deleteDomainObject(categoryOption, null);
139             }
140         }
141     }
142
143 }
144