1 package org.onap.vid.services;
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;
15 import javax.ws.rs.ForbiddenException;
17 import java.util.stream.Collectors;
21 public class CategoryParameterServiceImpl implements CategoryParameterService {
24 private DataAccessService dataAccessService;
26 private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(CategoryParameterServiceImpl.class);
28 public static class UnfoundedCategoryException extends RuntimeException {
30 public UnfoundedCategoryException(String message) {
35 public static class UnfoundedCategoryOptionException extends RuntimeException {
37 public UnfoundedCategoryOptionException(String message) {
42 public static class AlreadyExistOptionNameException extends RuntimeException {
44 public AlreadyExistOptionNameException(String message) {
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);
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);
64 public AddCategoryOptionResponse createCategoryParameterOptions(String categoryName, AddCategoryOptionsRequest optionsRequest) {
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));
74 String appId = categoryParameter.isIdSupported() ? UUID.randomUUID().toString() : optionName;
75 CategoryParameterOption categoryParameterOption = new CategoryParameterOption(appId, optionName, categoryParameter);
76 dataAccessService.saveDomainObject(categoryParameterOption, null);
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;
87 throw new UnfoundedCategoryException(msg);
91 return categoryParameters.get(0);
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";
101 throw new ForbiddenException(msg);
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;
107 throw new UnfoundedCategoryOptionException(msg);
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;
114 throw new AlreadyExistOptionNameException(msg);
117 categoryParameterOption.setName(option.getName());
118 dataAccessService.saveDomainObject(categoryParameterOption, null);
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;
129 throw new UnfoundedCategoryException(msg);
131 CategoryParameter categoryParameter = categoryParameters.get(0);
132 Set<CategoryParameterOption> categoryOptions = categoryParameter.getOptions();
133 for (CategoryParameterOption categoryOption: categoryOptions) {
134 if(categoryOption.getName().equals(option.getName()))
136 dataAccessService.deleteDomainObject(categoryOption, null);