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.util.DictionaryUtils;
35 import org.onap.policy.rest.dao.CommonClassDao;
36 import org.onap.policy.rest.jpa.RiskType;
37 import org.onap.policy.rest.jpa.SafePolicyWarning;
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 SafePolicyController {
49 private static CommonClassDao commonClassDao;
50 private static String duplicateResponseString = "Duplicate";
51 private static String operation = "operation";
52 private static String riskTypeDatas = "riskTypeDictionaryDatas";
53 private static String safePolicyWarningDatas = "safePolicyWarningDatas";
56 public SafePolicyController(CommonClassDao commonClassDao) {
57 SafePolicyController.commonClassDao = commonClassDao;
60 public void setCommonClassDao(CommonClassDao commonClassDao) {
61 SafePolicyController.commonClassDao = commonClassDao;
64 public SafePolicyController() {
68 private DictionaryUtils getDictionaryUtilsInstance() {
69 return DictionaryUtils.getDictionaryUtils();
73 value = {"/get_RiskTypeDataByName"},
74 method = {RequestMethod.GET},
75 produces = MediaType.APPLICATION_JSON_VALUE)
76 public void getRiskTypeDictionaryByNameEntityData(HttpServletResponse response) {
77 DictionaryUtils utils = getDictionaryUtilsInstance();
78 utils.getDataByEntity(response, riskTypeDatas, "name", RiskType.class);
82 value = {"/get_RiskTypeData"},
83 method = {RequestMethod.GET},
84 produces = MediaType.APPLICATION_JSON_VALUE)
85 public void getRiskTypeDictionaryEntityData(HttpServletResponse response) {
86 DictionaryUtils utils = getDictionaryUtilsInstance();
87 utils.getData(response, riskTypeDatas, RiskType.class);
90 @RequestMapping(value = {"/sp_dictionary/save_riskType"}, method = {RequestMethod.POST})
91 public ModelAndView saveRiskTypeDictionary(HttpServletRequest request, HttpServletResponse response)
93 DictionaryUtils utils = getDictionaryUtilsInstance();
95 boolean fromAPI = utils.isRequestFromAPI(request);
96 ObjectMapper mapper = new ObjectMapper();
97 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
98 JsonNode root = mapper.readTree(request.getReader());
99 RiskType riskTypeData;
100 String userId = null;
102 riskTypeData = mapper.readValue(root.get("dictionaryFields").toString(), RiskType.class);
105 riskTypeData = mapper.readValue(root.get("riskTypeDictionaryData").toString(), RiskType.class);
106 userId = root.get("userid").textValue();
108 UserInfo userInfo = utils.getUserInfo(userId);
109 List<Object> duplicateData =
110 commonClassDao.checkDuplicateEntry(riskTypeData.getRiskName(), "name", RiskType.class);
111 boolean duplicateflag = false;
112 if (!duplicateData.isEmpty()) {
113 RiskType data = (RiskType) duplicateData.get(0);
114 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
115 riskTypeData.setId(data.getId());
116 } else if ((request.getParameter(operation) != null
117 && !"update".equals(request.getParameter(operation)))
118 || (request.getParameter(operation) == null && (data.getId() != riskTypeData.getId()))) {
119 duplicateflag = true;
122 String responseString = null;
123 if (!duplicateflag) {
124 riskTypeData.setUserModifiedBy(userInfo);
125 if (riskTypeData.getId() == 0) {
126 riskTypeData.setUserCreatedBy(userInfo);
127 commonClassDao.save(riskTypeData);
129 riskTypeData.setModifiedDate(new Date());
130 commonClassDao.update(riskTypeData);
132 responseString = mapper.writeValueAsString(commonClassDao.getData(RiskType.class));
134 responseString = duplicateResponseString;
137 return utils.getResultForApi(responseString);
139 utils.setResponseData(response, riskTypeDatas, responseString);
141 } catch (Exception e) {
142 utils.setErrorResponseData(response, e);
147 @RequestMapping(value = {"/sp_dictionary/remove_riskType"}, method = {RequestMethod.POST})
148 public void removeRiskTypeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
149 DictionaryUtils utils = getDictionaryUtilsInstance();
150 utils.removeData(request, response, riskTypeDatas, RiskType.class);
154 value = {"/get_SafePolicyWarningDataByName"},
155 method = {RequestMethod.GET},
156 produces = MediaType.APPLICATION_JSON_VALUE)
157 public void getSafePolicyWarningEntityDataByName(HttpServletResponse response) {
158 DictionaryUtils utils = getDictionaryUtilsInstance();
159 utils.getDataByEntity(response, safePolicyWarningDatas, "name", SafePolicyWarning.class);
163 value = {"/get_SafePolicyWarningData"},
164 method = {RequestMethod.GET},
165 produces = MediaType.APPLICATION_JSON_VALUE)
166 public void getSafePolicyWarningeEntityData(HttpServletResponse response) {
167 DictionaryUtils utils = getDictionaryUtilsInstance();
168 utils.getData(response, safePolicyWarningDatas, SafePolicyWarning.class);
171 @RequestMapping(value = {"/sp_dictionary/save_safePolicyWarning"}, method = {RequestMethod.POST})
172 public ModelAndView saveSafePolicyWarningDictionary(HttpServletRequest request, HttpServletResponse response)
174 DictionaryUtils utils = getDictionaryUtilsInstance();
176 boolean fromAPI = utils.isRequestFromAPI(request);
177 ObjectMapper mapper = new ObjectMapper();
178 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
179 JsonNode root = mapper.readTree(request.getReader());
180 SafePolicyWarning safePolicyWarning;
182 safePolicyWarning = mapper.readValue(root.get("dictionaryFields").toString(), SafePolicyWarning.class);
185 mapper.readValue(root.get("safePolicyWarningData").toString(), SafePolicyWarning.class);
188 List<Object> duplicateData =
189 commonClassDao.checkDuplicateEntry(safePolicyWarning.getName(), "name", SafePolicyWarning.class);
190 boolean duplicateflag = false;
191 if (!duplicateData.isEmpty()) {
192 SafePolicyWarning data = (SafePolicyWarning) duplicateData.get(0);
193 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
194 safePolicyWarning.setId(data.getId());
195 } else if ((request.getParameter(operation) != null
196 && !"update".equals(request.getParameter(operation)))
197 || (request.getParameter(operation) == null && (data.getId() != safePolicyWarning.getId()))) {
198 duplicateflag = true;
201 String responseString = null;
202 if (!duplicateflag) {
203 if (safePolicyWarning.getId() == 0) {
204 commonClassDao.save(safePolicyWarning);
206 commonClassDao.update(safePolicyWarning);
208 responseString = mapper.writeValueAsString(commonClassDao.getData(SafePolicyWarning.class));
210 responseString = duplicateResponseString;
213 return utils.getResultForApi(responseString);
215 utils.setResponseData(response, safePolicyWarningDatas, responseString);
217 } catch (Exception e) {
218 utils.setErrorResponseData(response, e);
223 @RequestMapping(value = {"/sp_dictionary/remove_SafePolicyWarning"}, method = {RequestMethod.POST})
224 public void removeSafePolicyWarningDictionary(HttpServletRequest request, HttpServletResponse response)
226 DictionaryUtils utils = getDictionaryUtilsInstance();
227 utils.removeData(request, response, safePolicyWarningDatas, SafePolicyWarning.class);