2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.xacml.rest.controller;
23 import com.fasterxml.jackson.databind.DeserializationFeature;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
27 import java.io.IOException;
28 import java.util.Date;
29 import java.util.List;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
34 import org.onap.policy.pap.xacml.rest.adapters.GridData;
35 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
36 import org.onap.policy.rest.dao.CommonClassDao;
37 import org.onap.policy.rest.jpa.DescriptiveScope;
38 import org.onap.policy.rest.jpa.UserInfo;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.http.MediaType;
41 import org.springframework.stereotype.Controller;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.bind.annotation.RequestMethod;
44 import org.springframework.web.servlet.ModelAndView;
47 public class DescriptiveDictionaryController {
49 private static CommonClassDao commonClassDao;
50 private static String operation = "operation";
51 private static String dScopeName = "descriptiveScopeName";
52 private static String descriptiveDatas = "descriptiveScopeDictionaryDatas";
55 public DescriptiveDictionaryController(CommonClassDao commonClassDao) {
56 DescriptiveDictionaryController.commonClassDao = commonClassDao;
59 public void setCommonClassDao(CommonClassDao commonClassDao) {
60 DescriptiveDictionaryController.commonClassDao = commonClassDao;
63 public DescriptiveDictionaryController() {
67 private DictionaryUtils getDictionaryUtilsInstance() {
68 return DictionaryUtils.getDictionaryUtils();
72 value = {"/get_DescriptiveScopeByName"},
73 method = {RequestMethod.GET},
74 produces = MediaType.APPLICATION_JSON_VALUE)
75 public void getDescriptiveDictionaryByNameEntityData(HttpServletResponse response) {
76 DictionaryUtils utils = getDictionaryUtilsInstance();
77 utils.getDataByEntity(response, descriptiveDatas, dScopeName, DescriptiveScope.class);
81 value = {"/get_DescriptiveScope"},
82 method = {RequestMethod.GET},
83 produces = MediaType.APPLICATION_JSON_VALUE)
84 public void getDescriptiveDictionaryEntityData(HttpServletResponse response) {
85 DictionaryUtils utils = getDictionaryUtilsInstance();
86 utils.getData(response, descriptiveDatas, DescriptiveScope.class);
89 @RequestMapping(value = {"/descriptive_dictionary/save_descriptive"}, method = {RequestMethod.POST})
90 public ModelAndView saveDescriptiveDictionary(HttpServletRequest request, HttpServletResponse response)
92 DictionaryUtils utils = getDictionaryUtilsInstance();
94 boolean fromAPI = utils.isRequestFromAPI(request);
95 ObjectMapper mapper = new ObjectMapper();
96 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
97 JsonNode root = mapper.readTree(request.getReader());
98 DescriptiveScope descriptiveScope;
100 String userId = null;
102 descriptiveScope = mapper.readValue(root.get("dictionaryFields").toString(), DescriptiveScope.class);
103 data = mapper.readValue(root.get("dictionaryFields").toString(), GridData.class);
107 mapper.readValue(root.get("descriptiveScopeDictionaryData").toString(), DescriptiveScope.class);
108 data = mapper.readValue(root.get("descriptiveScopeDictionaryData").toString(), GridData.class);
109 userId = root.get("userid").textValue();
111 descriptiveScope.setSearch(utils.appendKeyValue(data.getAttributes(), "AND", ":"));
112 UserInfo userInfo = utils.getUserInfo(userId);
113 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(descriptiveScope.getScopeName(), dScopeName,
114 DescriptiveScope.class);
115 boolean duplicateflag = false;
116 if (!duplicateData.isEmpty()) {
117 DescriptiveScope data1 = (DescriptiveScope) duplicateData.get(0);
118 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
119 descriptiveScope.setId(data1.getId());
120 } else if ((request.getParameter(operation) != null
121 && !"update".equals(request.getParameter(operation)))
122 || (request.getParameter(operation) == null && (data1.getId() != descriptiveScope.getId()))) {
123 duplicateflag = true;
126 String responseString = null;
127 if (!duplicateflag) {
128 descriptiveScope.setUserModifiedBy(userInfo);
129 if (descriptiveScope.getId() == 0) {
130 descriptiveScope.setUserCreatedBy(userInfo);
131 commonClassDao.save(descriptiveScope);
133 descriptiveScope.setModifiedDate(new Date());
134 commonClassDao.update(descriptiveScope);
136 responseString = mapper.writeValueAsString(commonClassDao.getData(DescriptiveScope.class));
138 responseString = "Duplicate";
141 return utils.getResultForApi(responseString);
143 utils.setResponseData(response, descriptiveDatas, responseString);
145 } catch (Exception e) {
146 utils.setErrorResponseData(response, e);
151 @RequestMapping(value = {"/descriptive_dictionary/remove_descriptiveScope"}, method = {RequestMethod.POST})
152 public void removeDescriptiveDictionary(HttpServletRequest request, HttpServletResponse response)
154 DictionaryUtils utils = getDictionaryUtilsInstance();
155 utils.removeData(request, response, descriptiveDatas, DescriptiveScope.class);