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.io.UnsupportedEncodingException;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.List;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
34 import org.json.JSONObject;
35 import org.onap.policy.common.logging.flexlogger.FlexLogger;
36 import org.onap.policy.common.logging.flexlogger.Logger;
37 import org.onap.policy.pap.xacml.rest.util.JsonMessage;
38 import org.onap.policy.rest.dao.CommonClassDao;
39 import org.onap.policy.rest.jpa.RiskType;
40 import org.onap.policy.rest.jpa.SafePolicyWarning;
41 import org.onap.policy.rest.jpa.UserInfo;
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;
60 public SafePolicyController(CommonClassDao commonClassDao){
61 SafePolicyController.commonClassDao = commonClassDao;
64 public SafePolicyController(){}
66 public UserInfo getUserInfo(String loginId){
67 UserInfo name = (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", loginId);
71 private static String DUPLICATE = "Duplicate";
73 @RequestMapping(value = { "/get_RiskTypeDataByName" }, method = {
74 org.springframework.web.bind.annotation.RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
75 public void getRiskTypeDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response) {
77 Map<String, Object> model = new HashMap<>();
78 ObjectMapper mapper = new ObjectMapper();
79 model.put("riskTypeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(RiskType.class, "name")));
80 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
81 JSONObject j = new JSONObject(msg);
82 response.getWriter().write(j.toString());
83 } catch (Exception e) {
84 LOGGER.error("Exception Occured"+e);
88 @RequestMapping(value = { "/get_RiskTypeData" }, method = {
89 org.springframework.web.bind.annotation.RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
90 public void getOnapNameDictionaryEntityData(HttpServletRequest request, HttpServletResponse response) {
92 Map<String, Object> model = new HashMap<>();
93 ObjectMapper mapper = new ObjectMapper();
94 model.put("riskTypeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(RiskType.class)));
95 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
96 JSONObject j = new JSONObject(msg);
97 response.addHeader("successMapKey", "success");
98 response.addHeader("operation", "getDictionary");
99 response.getWriter().write(j.toString());
100 } catch (Exception e) {
102 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
103 response.addHeader("error", "dictionaryDBQuery");
107 @RequestMapping(value = { "/sp_dictionary/save_riskType" }, method = {
108 org.springframework.web.bind.annotation.RequestMethod.POST })
109 public ModelAndView saveRiskTypeDictionary(HttpServletRequest request, HttpServletResponse response)
110 throws UnsupportedEncodingException, IOException {
112 boolean duplicateflag = false;
113 boolean isFakeUpdate = false;
114 boolean fromAPI = false;
115 if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
118 ObjectMapper mapper = new ObjectMapper();
119 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
120 JsonNode root = mapper.readTree(request.getReader());
121 RiskType riskTypeData;
122 String userId = null;
124 riskTypeData = (RiskType) mapper.readValue(root.get("dictionaryFields").toString(),
128 //check if update operation or create, get id for data to be updated and update attributeData
129 if ("update".equalsIgnoreCase(request.getParameter("operation"))){
130 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(riskTypeData.getRiskName(), "name", RiskType.class);
132 RiskType data = (RiskType) duplicateData.get(0);
137 riskTypeData.setId(1);
139 riskTypeData.setId(id);
142 riskTypeData.setUserCreatedBy(this.getUserInfo(userId));
145 riskTypeData = (RiskType) mapper.readValue(root.get("riskTypeDictionaryData").toString(), RiskType.class);
146 userId = root.get("userid").textValue();
149 if (riskTypeData.getId() == 0) {
150 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(riskTypeData.getRiskName(), "name", RiskType.class);
151 if(!duplicateData.isEmpty()){
152 duplicateflag = true;
154 riskTypeData.setUserCreatedBy(getUserInfo(userId));
155 riskTypeData.setUserModifiedBy(getUserInfo(userId));
156 commonClassDao.save(riskTypeData);
160 riskTypeData.setUserModifiedBy(this.getUserInfo(userId));
161 riskTypeData.setModifiedDate(new Date());
162 commonClassDao.update(riskTypeData);
165 String responseString = "";
167 responseString = DUPLICATE;
169 responseString = mapper.writeValueAsString(commonClassDao.getData(RiskType.class));
173 if (responseString!=null && !responseString.equals(DUPLICATE)) {
175 responseString = "Exists";
177 responseString = "Success";
180 ModelAndView result = new ModelAndView();
181 result.setViewName(responseString);
184 response.setCharacterEncoding("UTF-8");
185 response.setContentType("application / json");
186 request.setCharacterEncoding("UTF-8");
188 PrintWriter out = response.getWriter();
189 JSONObject j = new JSONObject("{riskTypeDictionaryDatas: " + responseString + "}");
190 out.write(j.toString());
193 }catch (Exception e) {
195 response.setCharacterEncoding("UTF-8");
196 request.setCharacterEncoding("UTF-8");
197 PrintWriter out = response.getWriter();
198 out.write(e.getMessage());
203 @RequestMapping(value = { "/sp_dictionary/remove_riskType" }, method = {
204 org.springframework.web.bind.annotation.RequestMethod.POST })
205 public ModelAndView removeOnapDictionary(HttpServletRequest request, HttpServletResponse response)
206 throws UnsupportedEncodingException, IOException{
208 ObjectMapper mapper = new ObjectMapper();
209 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
210 JsonNode root = mapper.readTree(request.getReader());
211 RiskType onapData = (RiskType) mapper.readValue(root.get("data").toString(), RiskType.class);
212 commonClassDao.delete(onapData);
213 response.setCharacterEncoding("UTF-8");
214 response.setContentType("application / json");
215 request.setCharacterEncoding("UTF-8");
217 PrintWriter out = response.getWriter();
219 String responseString = mapper.writeValueAsString(commonClassDao.getData(RiskType.class));
220 JSONObject j = new JSONObject("{riskTypeDictionaryDatas: " + responseString + "}");
221 out.write(j.toString());
224 } catch (Exception e) {
226 response.setCharacterEncoding("UTF-8");
227 request.setCharacterEncoding("UTF-8");
228 PrintWriter out = response.getWriter();
229 out.write(e.getMessage());
234 @RequestMapping(value = { "/get_SafePolicyWarningDataByName" }, method = {
235 org.springframework.web.bind.annotation.RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
236 public void getSafePolicyWarningEntityDataByName(HttpServletRequest request, HttpServletResponse response) {
238 Map<String, Object> model = new HashMap<>();
239 ObjectMapper mapper = new ObjectMapper();
240 model.put("safePolicyWarningDatas",
241 mapper.writeValueAsString(commonClassDao.getDataByColumn(SafePolicyWarning.class, "name")));
242 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
243 JSONObject j = new JSONObject(msg);
244 response.getWriter().write(j.toString());
245 } catch (Exception e) {
246 LOGGER.error("Exception Occured"+e);
250 @RequestMapping(value = { "/get_SafePolicyWarningData" }, method = {
251 org.springframework.web.bind.annotation.RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
252 public void getSafePolicyWarningeEntityData(HttpServletRequest request, 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 = {
271 org.springframework.web.bind.annotation.RequestMethod.POST })
272 public ModelAndView saveSafePolicyWarningDictionary(HttpServletRequest request, HttpServletResponse response)
273 throws UnsupportedEncodingException, IOException {
275 boolean duplicateflag = false;
276 boolean isFakeUpdate = false;
277 boolean fromAPI = false;
278 if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
281 ObjectMapper mapper = new ObjectMapper();
282 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
283 JsonNode root = mapper.readTree(request.getReader());
284 SafePolicyWarning safePolicyWarning;
286 safePolicyWarning = (SafePolicyWarning) mapper
287 .readValue(root.get("dictionaryFields").toString(), SafePolicyWarning.class);
289 //check if update operation or create, get id for data to be updated and update attributeData
290 if (request.getParameter("operation").equals("update")) {
291 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(safePolicyWarning.getName(), "name", SafePolicyWarning.class);
293 SafePolicyWarning data = (SafePolicyWarning) duplicateData.get(0);
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 = DUPLICATE;
323 responseString = mapper.writeValueAsString(commonClassDao.getData(SafePolicyWarning.class));
327 if (responseString!=null && !responseString.equals(DUPLICATE)) {
329 responseString = "Exists";
331 responseString = "Success";
334 ModelAndView result = new ModelAndView();
335 result.setViewName(responseString);
338 response.setCharacterEncoding("UTF-8");
339 response.setContentType("application / json");
340 request.setCharacterEncoding("UTF-8");
342 PrintWriter out = response.getWriter();
343 JSONObject j = new JSONObject("{safePolicyWarningDatas: " + responseString + "}");
344 out.write(j.toString());
348 }catch (Exception e) {
350 response.setCharacterEncoding("UTF-8");
351 request.setCharacterEncoding("UTF-8");
352 PrintWriter out = response.getWriter();
353 out.write(e.getMessage());
358 @RequestMapping(value = { "/sp_dictionary/remove_SafePolicyWarning" }, method = {
359 org.springframework.web.bind.annotation.RequestMethod.POST })
360 public ModelAndView removeSafePolicyWarningDictionary(HttpServletRequest request, HttpServletResponse response)
361 throws UnsupportedEncodingException, IOException {
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("UTF-8");
370 response.setContentType("application / json");
371 request.setCharacterEncoding("UTF-8");
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) {
381 System.out.println(e);
382 response.setCharacterEncoding("UTF-8");
383 request.setCharacterEncoding("UTF-8");
384 PrintWriter out = response.getWriter();
385 out.write(e.getMessage());