2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.servlet.ModelAndView;
48 import com.fasterxml.jackson.databind.DeserializationFeature;
49 import com.fasterxml.jackson.databind.JsonNode;
50 import com.fasterxml.jackson.databind.ObjectMapper;
53 public class SafePolicyController {
55 private static final Logger LOGGER = FlexLogger.getLogger(SafePolicyController.class);
57 private static CommonClassDao commonClassDao;
58 private static String duplicateResponseString = "Duplicate";
59 private static String operation = "operation";
60 private static String apiflag = "apiflag";
61 private static String utf8 = "UTF-8";
62 private static String applicationJsonContentType = "application / json";
65 public SafePolicyController(CommonClassDao commonClassDao){
66 SafePolicyController.commonClassDao = commonClassDao;
69 public SafePolicyController(){}
71 public UserInfo getUserInfo(String loginId){
72 return (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", loginId);
75 @RequestMapping(value = { "/get_RiskTypeDataByName" }, method = {
76 org.springframework.web.bind.annotation.RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
77 public void getRiskTypeDictionaryByNameEntityData(HttpServletResponse response) {
79 Map<String, Object> model = new HashMap<>();
80 ObjectMapper mapper = new ObjectMapper();
81 model.put("riskTypeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(RiskType.class, "name")));
82 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
83 JSONObject j = new JSONObject(msg);
84 response.getWriter().write(j.toString());
85 } catch (Exception e) {
86 LOGGER.error("Exception Occured"+e);
90 @RequestMapping(value = { "/get_RiskTypeData" }, method = {
91 org.springframework.web.bind.annotation.RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
92 public void getOnapNameDictionaryEntityData(HttpServletResponse response) {
94 Map<String, Object> model = new HashMap<>();
95 ObjectMapper mapper = new ObjectMapper();
96 model.put("riskTypeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(RiskType.class)));
97 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
98 JSONObject j = new JSONObject(msg);
99 response.addHeader("successMapKey", "success");
100 response.addHeader(operation, "getDictionary");
101 response.getWriter().write(j.toString());
102 } catch (Exception e) {
104 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
105 response.addHeader("error", "dictionaryDBQuery");
109 @RequestMapping(value = { "/sp_dictionary/save_riskType" }, method = {
110 org.springframework.web.bind.annotation.RequestMethod.POST })
111 public ModelAndView saveRiskTypeDictionary(HttpServletRequest request, HttpServletResponse response)
114 boolean duplicateflag = false;
115 boolean isFakeUpdate = false;
116 boolean fromAPI = false;
117 if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
120 ObjectMapper mapper = new ObjectMapper();
121 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
122 JsonNode root = mapper.readTree(request.getReader());
123 RiskType riskTypeData;
124 String userId = null;
126 riskTypeData = (RiskType) mapper.readValue(root.get("dictionaryFields").toString(),
130 //check if update operation or create, get id for data to be updated and update attributeData
131 if ("update".equalsIgnoreCase(request.getParameter(operation))){
132 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(riskTypeData.getRiskName(), "name", RiskType.class);
133 RiskType data = (RiskType) duplicateData.get(0);
134 int id = data.getId();
138 riskTypeData.setId(1);
140 riskTypeData.setId(id);
143 riskTypeData.setUserCreatedBy(this.getUserInfo(userId));
146 riskTypeData = (RiskType) mapper.readValue(root.get("riskTypeDictionaryData").toString(), RiskType.class);
147 userId = root.get("userid").textValue();
150 if (riskTypeData.getId() == 0) {
151 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(riskTypeData.getRiskName(), "name", RiskType.class);
152 if(!duplicateData.isEmpty()){
153 duplicateflag = true;
155 riskTypeData.setUserCreatedBy(getUserInfo(userId));
156 riskTypeData.setUserModifiedBy(getUserInfo(userId));
157 commonClassDao.save(riskTypeData);
161 riskTypeData.setUserModifiedBy(this.getUserInfo(userId));
162 riskTypeData.setModifiedDate(new Date());
163 commonClassDao.update(riskTypeData);
166 String responseString = "";
168 responseString = duplicateResponseString;
170 responseString = mapper.writeValueAsString(commonClassDao.getData(RiskType.class));
174 if (responseString!=null && !responseString.equals(duplicateResponseString)) {
176 responseString = "Exists";
178 responseString = "Success";
181 ModelAndView result = new ModelAndView();
182 result.setViewName(responseString);
185 response.setCharacterEncoding(utf8);
186 response.setContentType(applicationJsonContentType);
187 request.setCharacterEncoding(utf8);
189 PrintWriter out = response.getWriter();
190 JSONObject j = new JSONObject("{riskTypeDictionaryDatas: " + responseString + "}");
191 out.write(j.toString());
194 }catch (Exception e) {
196 response.setCharacterEncoding(utf8);
197 request.setCharacterEncoding(utf8);
198 PrintWriter out = response.getWriter();
199 out.write(PolicyUtils.CATCH_EXCEPTION);
204 @RequestMapping(value = { "/sp_dictionary/remove_riskType" }, method = {
205 org.springframework.web.bind.annotation.RequestMethod.POST })
206 public ModelAndView removeOnapDictionary(HttpServletRequest request, HttpServletResponse response)
209 ObjectMapper mapper = new ObjectMapper();
210 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
211 JsonNode root = mapper.readTree(request.getReader());
212 RiskType onapData = (RiskType) mapper.readValue(root.get("data").toString(), RiskType.class);
213 commonClassDao.delete(onapData);
214 response.setCharacterEncoding(utf8);
215 response.setContentType(applicationJsonContentType);
216 request.setCharacterEncoding(utf8);
218 PrintWriter out = response.getWriter();
220 String responseString = mapper.writeValueAsString(commonClassDao.getData(RiskType.class));
221 JSONObject j = new JSONObject("{riskTypeDictionaryDatas: " + responseString + "}");
222 out.write(j.toString());
225 } catch (Exception e) {
227 response.setCharacterEncoding(utf8);
228 request.setCharacterEncoding(utf8);
229 PrintWriter out = response.getWriter();
230 out.write(PolicyUtils.CATCH_EXCEPTION);
235 @RequestMapping(value = { "/get_SafePolicyWarningDataByName" }, method = {
236 org.springframework.web.bind.annotation.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 = {
252 org.springframework.web.bind.annotation.RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
253 public void getSafePolicyWarningeEntityData(HttpServletResponse response) {
255 Map<String, Object> model = new HashMap<>();
256 ObjectMapper mapper = new ObjectMapper();
257 model.put("safePolicyWarningDatas",
258 mapper.writeValueAsString(commonClassDao.getData(SafePolicyWarning.class)));
259 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
260 JSONObject j = new JSONObject(msg);
261 response.addHeader("successMapKey", "success");
262 response.addHeader(operation, "getDictionary");
263 response.getWriter().write(j.toString());
264 } catch (Exception e) {
266 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
267 response.addHeader("error", "dictionaryDBQuery");
271 @RequestMapping(value = { "/sp_dictionary/save_safePolicyWarning" }, method = {
272 org.springframework.web.bind.annotation.RequestMethod.POST })
273 public ModelAndView saveSafePolicyWarningDictionary(HttpServletRequest request, HttpServletResponse response)
276 boolean duplicateflag = false;
277 boolean isFakeUpdate = false;
278 boolean fromAPI = false;
279 if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
282 ObjectMapper mapper = new ObjectMapper();
283 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
284 JsonNode root = mapper.readTree(request.getReader());
285 SafePolicyWarning safePolicyWarning;
287 safePolicyWarning = (SafePolicyWarning) mapper
288 .readValue(root.get("dictionaryFields").toString(), SafePolicyWarning.class);
290 //check if update operation or create, get id for data to be updated and update attributeData
291 if (("update").equals(request.getParameter(operation))) {
292 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(safePolicyWarning.getName(), "name", SafePolicyWarning.class);
293 SafePolicyWarning data = (SafePolicyWarning) duplicateData.get(0);
294 int id = data.getId();
298 safePolicyWarning.setId(1);
300 safePolicyWarning.setId(id);
304 safePolicyWarning = (SafePolicyWarning) mapper.readValue(root.get("safePolicyWarningData").toString(), SafePolicyWarning.class);
307 if (safePolicyWarning.getId() == 0) {
308 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(safePolicyWarning.getName(), "name", SafePolicyWarning.class);
309 if(!duplicateData.isEmpty()){
310 duplicateflag = true;
312 commonClassDao.save(safePolicyWarning);
316 commonClassDao.update(safePolicyWarning);
319 String responseString = "";
321 responseString = duplicateResponseString;
323 responseString = mapper.writeValueAsString(commonClassDao.getData(SafePolicyWarning.class));
327 if (responseString!=null && !responseString.equals(duplicateResponseString)) {
329 responseString = "Exists";
331 responseString = "Success";
334 ModelAndView result = new ModelAndView();
335 result.setViewName(responseString);
338 response.setCharacterEncoding(utf8);
339 response.setContentType(applicationJsonContentType);
340 request.setCharacterEncoding(utf8);
342 PrintWriter out = response.getWriter();
343 JSONObject j = new JSONObject("{safePolicyWarningDatas: " + responseString + "}");
344 out.write(j.toString());
348 }catch (Exception e) {
350 response.setCharacterEncoding(utf8);
351 request.setCharacterEncoding(utf8);
352 PrintWriter out = response.getWriter();
353 out.write(PolicyUtils.CATCH_EXCEPTION);
358 @RequestMapping(value = { "/sp_dictionary/remove_SafePolicyWarning" }, method = {
359 org.springframework.web.bind.annotation.RequestMethod.POST })
360 public ModelAndView removeSafePolicyWarningDictionary(HttpServletRequest request, HttpServletResponse response)
363 ObjectMapper mapper = new ObjectMapper();
364 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
365 JsonNode root = mapper.readTree(request.getReader());
366 SafePolicyWarning safePolicyWarningData = (SafePolicyWarning) mapper.readValue(root.get("data").toString(),
367 SafePolicyWarning.class);
368 commonClassDao.delete(safePolicyWarningData);
369 response.setCharacterEncoding(utf8);
370 response.setContentType(applicationJsonContentType);
371 request.setCharacterEncoding(utf8);
373 PrintWriter out = response.getWriter();
375 String responseString = mapper.writeValueAsString(commonClassDao.getData(SafePolicyWarning.class));
376 JSONObject j = new JSONObject("{groupPolicyScopeListDatas: " + responseString + "}");
377 out.write(j.toString());
380 } catch (Exception e) {
382 response.setCharacterEncoding(utf8);
383 request.setCharacterEncoding(utf8);
384 PrintWriter out = response.getWriter();
385 out.write(PolicyUtils.CATCH_EXCEPTION);