1 package org.onap.vid.services;
3 import java.io.IOException;
5 import java.util.stream.Collectors;
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;
19 import javax.ws.rs.ForbiddenException;
23 public class CategoryParameterServiceImpl implements CategoryParameterService {
26 private DataAccessService dataAccessService;
28 private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(CategoryParameterServiceImpl.class);
30 public static class UnfoundedCategoryException extends RuntimeException {
32 public UnfoundedCategoryException(String message) {
37 public static class UnfoundedCategoryOptionException extends RuntimeException {
39 public UnfoundedCategoryOptionException(String message) {
44 public static class AlreadyExistOptionNameException extends RuntimeException {
46 public AlreadyExistOptionNameException(String message) {
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);
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);
66 public AddCategoryOptionResponse createCategoryParameterOptions(String categoryName, AddCategoryOptionsRequest optionsRequest) throws IOException, UnfoundedCategoryException {
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));
76 String appId = categoryParameter.isIdSupported() ? UUID.randomUUID().toString() : optionName;
77 CategoryParameterOption categoryParameterOption = new CategoryParameterOption(appId, optionName, categoryParameter);
78 dataAccessService.saveDomainObject(categoryParameterOption, null);
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;
89 throw new UnfoundedCategoryException(msg);
93 return categoryParameters.get(0);
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";
103 throw new ForbiddenException(msg);
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;
109 throw new UnfoundedCategoryOptionException(msg);
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;
116 throw new AlreadyExistOptionNameException(msg);
119 categoryParameterOption.setName(option.getName());
120 dataAccessService.saveDomainObject(categoryParameterOption, null);
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;
131 throw new UnfoundedCategoryException(msg);
133 CategoryParameter categoryParameter = categoryParameters.get(0);
134 Set<CategoryParameterOption> categoryOptions = categoryParameter.getOptions();
135 for (CategoryParameterOption categoryOption: categoryOptions) {
136 if(categoryOption.getName().equals(option.getName()))
138 dataAccessService.deleteDomainObject(categoryOption, null);