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