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;
26 import java.io.IOException;
27 import java.util.Date;
28 import java.util.List;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
32 import org.onap.policy.rest.dao.CommonClassDao;
33 import org.onap.policy.rest.jpa.RiskType;
34 import org.onap.policy.rest.jpa.SafePolicyWarning;
35 import org.onap.policy.rest.jpa.UserInfo;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.http.MediaType;
38 import org.springframework.stereotype.Controller;
39 import org.springframework.web.bind.annotation.RequestMapping;
40 import org.springframework.web.bind.annotation.RequestMethod;
41 import org.springframework.web.servlet.ModelAndView;
44 public class SafePolicyController {
46 private static CommonClassDao commonClassDao;
47 private static String duplicateResponseString = "Duplicate";
48 private static String operation = "operation";
49 private static String riskTypeDatas = "riskTypeDictionaryDatas";
50 private static String safePolicyWarningDatas = "safePolicyWarningDatas";
53 public SafePolicyController(CommonClassDao commonClassDao) {
54 SafePolicyController.commonClassDao = commonClassDao;
57 public void setCommonClassDao(CommonClassDao commonClassDao) {
58 SafePolicyController.commonClassDao = commonClassDao;
61 public SafePolicyController() {
65 private DictionaryUtils getDictionaryUtilsInstance() {
66 return DictionaryUtils.getDictionaryUtils();
69 @RequestMapping(value = {"/get_RiskTypeDataByName"}, method = {RequestMethod.GET},
70 produces = MediaType.APPLICATION_JSON_VALUE)
71 public void getRiskTypeDictionaryByNameEntityData(HttpServletResponse response) {
72 DictionaryUtils utils = getDictionaryUtilsInstance();
73 utils.getDataByEntity(response, riskTypeDatas, "name", RiskType.class);
76 @RequestMapping(value = {"/get_RiskTypeData"}, method = {RequestMethod.GET},
77 produces = MediaType.APPLICATION_JSON_VALUE)
78 public void getRiskTypeDictionaryEntityData(HttpServletResponse response) {
79 DictionaryUtils utils = getDictionaryUtilsInstance();
80 utils.getData(response, riskTypeDatas, RiskType.class);
83 @RequestMapping(value = {"/sp_dictionary/save_riskType"}, method = {RequestMethod.POST})
84 public ModelAndView saveRiskTypeDictionary(HttpServletRequest request,
85 HttpServletResponse response) throws IOException {
86 DictionaryUtils utils = getDictionaryUtilsInstance();
88 boolean fromAPI = utils.isRequestFromAPI(request);
89 ObjectMapper mapper = new ObjectMapper();
90 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
91 JsonNode root = mapper.readTree(request.getReader());
92 RiskType riskTypeData;
96 mapper.readValue(root.get("dictionaryFields").toString(), RiskType.class);
99 riskTypeData = mapper.readValue(root.get("riskTypeDictionaryData").toString(),
101 userId = root.get("userid").textValue();
103 UserInfo userInfo = utils.getUserInfo(userId);
104 List<Object> duplicateData = commonClassDao
105 .checkDuplicateEntry(riskTypeData.getRiskName(), "name", RiskType.class);
106 boolean duplicateflag = false;
107 if (!duplicateData.isEmpty()) {
108 RiskType data = (RiskType) duplicateData.get(0);
109 if (request.getParameter(operation) != null
110 && "update".equals(request.getParameter(operation))) {
111 riskTypeData.setId(data.getId());
112 } else if ((request.getParameter(operation) != null
113 && !"update".equals(request.getParameter(operation)))
114 || (request.getParameter(operation) == null
115 && (data.getId() != riskTypeData.getId()))) {
116 duplicateflag = true;
119 String responseString = null;
120 if (!duplicateflag) {
121 riskTypeData.setUserModifiedBy(userInfo);
122 if (riskTypeData.getId() == 0) {
123 riskTypeData.setUserCreatedBy(userInfo);
124 commonClassDao.save(riskTypeData);
126 riskTypeData.setModifiedDate(new Date());
127 commonClassDao.update(riskTypeData);
129 responseString = mapper.writeValueAsString(commonClassDao.getData(RiskType.class));
131 responseString = duplicateResponseString;
134 return utils.getResultForApi(responseString);
136 utils.setResponseData(response, riskTypeDatas, responseString);
138 } catch (Exception e) {
139 utils.setErrorResponseData(response, e);
144 @RequestMapping(value = {"/sp_dictionary/remove_riskType"}, method = {RequestMethod.POST})
145 public void removeRiskTypeDictionary(HttpServletRequest request, HttpServletResponse response)
147 DictionaryUtils utils = getDictionaryUtilsInstance();
148 utils.removeData(request, response, riskTypeDatas, RiskType.class);
151 @RequestMapping(value = {"/get_SafePolicyWarningDataByName"}, method = {RequestMethod.GET},
152 produces = MediaType.APPLICATION_JSON_VALUE)
153 public void getSafePolicyWarningEntityDataByName(HttpServletResponse response) {
154 DictionaryUtils utils = getDictionaryUtilsInstance();
155 utils.getDataByEntity(response, safePolicyWarningDatas, "name", SafePolicyWarning.class);
158 @RequestMapping(value = {"/get_SafePolicyWarningData"}, method = {RequestMethod.GET},
159 produces = MediaType.APPLICATION_JSON_VALUE)
160 public void getSafePolicyWarningeEntityData(HttpServletResponse response) {
161 DictionaryUtils utils = getDictionaryUtilsInstance();
162 utils.getData(response, safePolicyWarningDatas, SafePolicyWarning.class);
165 @RequestMapping(value = {"/sp_dictionary/save_safePolicyWarning"},
166 method = {RequestMethod.POST})
167 public ModelAndView saveSafePolicyWarningDictionary(HttpServletRequest request,
168 HttpServletResponse response) throws IOException {
169 DictionaryUtils utils = getDictionaryUtilsInstance();
171 boolean fromAPI = utils.isRequestFromAPI(request);
172 ObjectMapper mapper = new ObjectMapper();
173 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
174 JsonNode root = mapper.readTree(request.getReader());
175 SafePolicyWarning safePolicyWarning;
177 safePolicyWarning = mapper.readValue(root.get("dictionaryFields").toString(),
178 SafePolicyWarning.class);
180 safePolicyWarning = mapper.readValue(root.get("safePolicyWarningData").toString(),
181 SafePolicyWarning.class);
184 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(
185 safePolicyWarning.getName(), "name", SafePolicyWarning.class);
186 boolean duplicateflag = false;
187 if (!duplicateData.isEmpty()) {
188 SafePolicyWarning data = (SafePolicyWarning) duplicateData.get(0);
189 if (request.getParameter(operation) != null
190 && "update".equals(request.getParameter(operation))) {
191 safePolicyWarning.setId(data.getId());
192 } else if ((request.getParameter(operation) != null
193 && !"update".equals(request.getParameter(operation)))
194 || (request.getParameter(operation) == null
195 && (data.getId() != safePolicyWarning.getId()))) {
196 duplicateflag = true;
199 String responseString = null;
200 if (!duplicateflag) {
201 if (safePolicyWarning.getId() == 0) {
202 commonClassDao.save(safePolicyWarning);
204 commonClassDao.update(safePolicyWarning);
207 mapper.writeValueAsString(commonClassDao.getData(SafePolicyWarning.class));
209 responseString = duplicateResponseString;
212 return utils.getResultForApi(responseString);
214 utils.setResponseData(response, safePolicyWarningDatas, responseString);
216 } catch (Exception e) {
217 utils.setErrorResponseData(response, e);
222 @RequestMapping(value = {"/sp_dictionary/remove_SafePolicyWarning"},
223 method = {RequestMethod.POST})
224 public void removeSafePolicyWarningDictionary(HttpServletRequest request,
225 HttpServletResponse response) throws IOException {
226 DictionaryUtils utils = getDictionaryUtilsInstance();
227 utils.removeData(request, response, safePolicyWarningDatas, SafePolicyWarning.class);