2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 java.io.IOException;
24 import java.io.PrintWriter;
25 import java.util.Date;
26 import java.util.HashMap;
27 import java.util.List;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
33 import org.json.JSONObject;
34 import org.onap.policy.common.logging.flexlogger.FlexLogger;
35 import org.onap.policy.common.logging.flexlogger.Logger;
36 import org.onap.policy.pap.xacml.rest.util.JsonMessage;
37 import org.onap.policy.rest.dao.CommonClassDao;
38 import org.onap.policy.rest.jpa.RiskType;
39 import org.onap.policy.rest.jpa.SafePolicyWarning;
40 import org.onap.policy.rest.jpa.UserInfo;
41 import org.onap.policy.utils.PolicyUtils;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.http.MediaType;
44 import org.springframework.stereotype.Controller;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.RequestMethod;
47 import org.springframework.web.servlet.ModelAndView;
49 import com.fasterxml.jackson.databind.DeserializationFeature;
50 import com.fasterxml.jackson.databind.JsonNode;
51 import com.fasterxml.jackson.databind.ObjectMapper;
54 public class SafePolicyController {
56 private static final Logger LOGGER = FlexLogger.getLogger(SafePolicyController.class);
58 private static CommonClassDao commonClassDao;
59 private static String duplicateResponseString = "Duplicate";
60 private static String operation = "operation";
61 private static String apiflag = "apiflag";
62 private static String utf8 = "UTF-8";
63 private static String applicationJsonContentType = "application / json";
66 public SafePolicyController(CommonClassDao commonClassDao){
67 SafePolicyController.commonClassDao = commonClassDao;
70 public void setCommonClassDao(CommonClassDao commonClassDao){
71 SafePolicyController.commonClassDao = commonClassDao;
74 public SafePolicyController(){
78 public UserInfo getUserInfo(String loginId){
79 return (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", loginId);
82 @RequestMapping(value = { "/get_RiskTypeDataByName" }, method = {RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
83 public void getRiskTypeDictionaryByNameEntityData(HttpServletResponse response) {
85 Map<String, Object> model = new HashMap<>();
86 ObjectMapper mapper = new ObjectMapper();
87 model.put("riskTypeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(RiskType.class, "name")));
88 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
89 JSONObject j = new JSONObject(msg);
90 response.getWriter().write(j.toString());
91 } catch (Exception e) {
92 LOGGER.error("Exception Occured"+e);
96 @RequestMapping(value = { "/get_RiskTypeData" }, method = {RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
97 public void getRiskTypeDictionaryEntityData(HttpServletResponse response) {
99 Map<String, Object> model = new HashMap<>();
100 ObjectMapper mapper = new ObjectMapper();
101 model.put("riskTypeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(RiskType.class)));
102 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
103 JSONObject j = new JSONObject(msg);
104 response.addHeader("successMapKey", "success");
105 response.addHeader(operation, "getDictionary");
106 response.getWriter().write(j.toString());
107 } catch (Exception e) {
109 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
110 response.addHeader("error", "dictionaryDBQuery");
114 @RequestMapping(value = { "/sp_dictionary/save_riskType" }, method = {RequestMethod.POST })
115 public ModelAndView saveRiskTypeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
117 boolean duplicateflag = false;
118 boolean isFakeUpdate = false;
119 boolean fromAPI = false;
120 if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
123 ObjectMapper mapper = new ObjectMapper();
124 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
125 JsonNode root = mapper.readTree(request.getReader());
126 RiskType riskTypeData;
127 String userId = null;
129 riskTypeData = mapper.readValue(root.get("dictionaryFields").toString(),
133 //check if update operation or create, get id for data to be updated and update attributeData
134 if ("update".equalsIgnoreCase(request.getParameter(operation))){
135 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(riskTypeData.getRiskName(), "name", RiskType.class);
136 RiskType data = (RiskType) duplicateData.get(0);
137 int id = data.getId();
141 riskTypeData.setId(1);
143 riskTypeData.setId(id);
146 riskTypeData.setUserCreatedBy(this.getUserInfo(userId));
149 riskTypeData = mapper.readValue(root.get("riskTypeDictionaryData").toString(), RiskType.class);
150 userId = root.get("userid").textValue();
153 if (riskTypeData.getId() == 0) {
154 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(riskTypeData.getRiskName(), "name", RiskType.class);
155 if(!duplicateData.isEmpty()){
156 duplicateflag = true;
158 riskTypeData.setUserCreatedBy(getUserInfo(userId));
159 riskTypeData.setUserModifiedBy(getUserInfo(userId));
160 commonClassDao.save(riskTypeData);
164 riskTypeData.setUserModifiedBy(this.getUserInfo(userId));
165 riskTypeData.setModifiedDate(new Date());
166 commonClassDao.update(riskTypeData);
169 String responseString = "";
171 responseString = duplicateResponseString;
173 responseString = mapper.writeValueAsString(commonClassDao.getData(RiskType.class));
177 if (responseString!=null && !responseString.equals(duplicateResponseString)) {
179 responseString = "Exists";
181 responseString = "Success";
184 ModelAndView result = new ModelAndView();
185 result.setViewName(responseString);
188 response.setCharacterEncoding(utf8);
189 response.setContentType(applicationJsonContentType);
190 request.setCharacterEncoding(utf8);
192 PrintWriter out = response.getWriter();
193 JSONObject j = new JSONObject("{riskTypeDictionaryDatas: " + responseString + "}");
194 out.write(j.toString());
197 }catch (Exception e) {
199 response.setCharacterEncoding(utf8);
200 request.setCharacterEncoding(utf8);
201 PrintWriter out = response.getWriter();
202 out.write(PolicyUtils.CATCH_EXCEPTION);
207 @RequestMapping(value = { "/sp_dictionary/remove_riskType" }, method = {RequestMethod.POST })
208 public ModelAndView removeRiskTypeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
210 ObjectMapper mapper = new ObjectMapper();
211 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
212 JsonNode root = mapper.readTree(request.getReader());
213 RiskType onapData = mapper.readValue(root.get("data").toString(), RiskType.class);
214 commonClassDao.delete(onapData);
215 response.setCharacterEncoding(utf8);
216 response.setContentType(applicationJsonContentType);
217 request.setCharacterEncoding(utf8);
219 PrintWriter out = response.getWriter();
221 String responseString = mapper.writeValueAsString(commonClassDao.getData(RiskType.class));
222 JSONObject j = new JSONObject("{riskTypeDictionaryDatas: " + responseString + "}");
223 out.write(j.toString());
226 } catch (Exception e) {
228 response.setCharacterEncoding(utf8);
229 request.setCharacterEncoding(utf8);
230 PrintWriter out = response.getWriter();
231 out.write(PolicyUtils.CATCH_EXCEPTION);
236 @RequestMapping(value = { "/get_SafePolicyWarningDataByName" }, method = {RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
237 public void getSafePolicyWarningEntityDataByName(HttpServletResponse response) {
239 Map<String, Object> model = new HashMap<>();
240 ObjectMapper mapper = new ObjectMapper();
241 model.put("safePolicyWarningDatas",
242 mapper.writeValueAsString(commonClassDao.getDataByColumn(SafePolicyWarning.class, "name")));
243 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
244 JSONObject j = new JSONObject(msg);
245 response.getWriter().write(j.toString());
246 } catch (Exception e) {
247 LOGGER.error("Exception Occured"+e);
251 @RequestMapping(value = { "/get_SafePolicyWarningData" }, method = {RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
252 public void getSafePolicyWarningeEntityData(HttpServletResponse response) {
254 Map<String, Object> model = new HashMap<>();
255 ObjectMapper mapper = new ObjectMapper();
256 model.put("safePolicyWarningDatas",
257 mapper.writeValueAsString(commonClassDao.getData(SafePolicyWarning.class)));
258 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
259 JSONObject j = new JSONObject(msg);
260 response.addHeader("successMapKey", "success");
261 response.addHeader(operation, "getDictionary");
262 response.getWriter().write(j.toString());
263 } catch (Exception e) {
265 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
266 response.addHeader("error", "dictionaryDBQuery");
270 @RequestMapping(value = { "/sp_dictionary/save_safePolicyWarning" }, method = {RequestMethod.POST })
271 public ModelAndView saveSafePolicyWarningDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
273 boolean duplicateflag = false;
274 boolean isFakeUpdate = false;
275 boolean fromAPI = false;
276 if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
279 ObjectMapper mapper = new ObjectMapper();
280 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
281 JsonNode root = mapper.readTree(request.getReader());
282 SafePolicyWarning safePolicyWarning;
284 safePolicyWarning = mapper.readValue(root.get("dictionaryFields").toString(), SafePolicyWarning.class);
286 //check if update operation or create, get id for data to be updated and update attributeData
287 if (("update").equals(request.getParameter(operation))) {
288 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(safePolicyWarning.getName(), "name", SafePolicyWarning.class);
289 SafePolicyWarning data = (SafePolicyWarning) duplicateData.get(0);
290 int id = data.getId();
294 safePolicyWarning.setId(1);
296 safePolicyWarning.setId(id);
300 safePolicyWarning = mapper.readValue(root.get("safePolicyWarningData").toString(), SafePolicyWarning.class);
303 if (safePolicyWarning.getId() == 0) {
304 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(safePolicyWarning.getName(), "name", SafePolicyWarning.class);
305 if(!duplicateData.isEmpty()){
306 duplicateflag = true;
308 commonClassDao.save(safePolicyWarning);
312 commonClassDao.update(safePolicyWarning);
315 String responseString = "";
317 responseString = duplicateResponseString;
319 responseString = mapper.writeValueAsString(commonClassDao.getData(SafePolicyWarning.class));
323 if (responseString!=null && !responseString.equals(duplicateResponseString)) {
325 responseString = "Exists";
327 responseString = "Success";
330 ModelAndView result = new ModelAndView();
331 result.setViewName(responseString);
334 response.setCharacterEncoding(utf8);
335 response.setContentType(applicationJsonContentType);
336 request.setCharacterEncoding(utf8);
338 PrintWriter out = response.getWriter();
339 JSONObject j = new JSONObject("{safePolicyWarningDatas: " + responseString + "}");
340 out.write(j.toString());
344 }catch (Exception e) {
346 response.setCharacterEncoding(utf8);
347 request.setCharacterEncoding(utf8);
348 PrintWriter out = response.getWriter();
349 out.write(PolicyUtils.CATCH_EXCEPTION);
354 @RequestMapping(value = { "/sp_dictionary/remove_SafePolicyWarning" }, method = {RequestMethod.POST })
355 public void removeSafePolicyWarningDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
357 ObjectMapper mapper = new ObjectMapper();
358 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
359 JsonNode root = mapper.readTree(request.getReader());
360 SafePolicyWarning safePolicyWarningData = mapper.readValue(root.get("data").toString(),
361 SafePolicyWarning.class);
362 commonClassDao.delete(safePolicyWarningData);
363 response.setCharacterEncoding(utf8);
364 response.setContentType(applicationJsonContentType);
365 request.setCharacterEncoding(utf8);
367 PrintWriter out = response.getWriter();
369 String responseString = mapper.writeValueAsString(commonClassDao.getData(SafePolicyWarning.class));
370 JSONObject j = new JSONObject("{safePolicyWarningDatas: " + responseString + "}");
371 out.write(j.toString());
372 } catch (Exception e) {
374 response.setCharacterEncoding(utf8);
375 request.setCharacterEncoding(utf8);
376 PrintWriter out = response.getWriter();
377 out.write(PolicyUtils.CATCH_EXCEPTION);