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.openecomp.policy.pap.xacml.rest.controller;
23 import java.io.PrintWriter;
24 import java.util.HashMap;
25 import java.util.List;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.json.JSONObject;
34 import org.openecomp.policy.pap.xacml.rest.util.JsonMessage;
35 import org.openecomp.policy.rest.dao.CategoryDao;
36 import org.openecomp.policy.rest.dao.RiskTypeDao;
37 import org.openecomp.policy.rest.dao.SafePolicyWarningDao;
38 import org.openecomp.policy.rest.dao.UserInfoDao;
39 import org.openecomp.policy.rest.jpa.Category;
40 import org.openecomp.policy.rest.jpa.RiskType;
41 import org.openecomp.policy.rest.jpa.SafePolicyWarning;
42 import org.openecomp.policy.rest.jpa.UserInfo;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.http.MediaType;
45 import org.springframework.stereotype.Controller;
46 import org.springframework.web.bind.annotation.RequestMapping;
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 Log logger = LogFactory.getLog(SafePolicyController.class);
59 SafePolicyWarningDao safePolicyWarningDao;
62 RiskTypeDao riskTypeDao;
65 UserInfoDao userInfoDao;
68 CategoryDao categoryDao;
71 public Category getCategory() {
72 for (int i = 0; i < categoryDao.getCategoryListData().size(); i++) {
73 Category value = categoryDao.getCategoryListData().get(i);
74 if (value.getShortName().equals("resource")) {
81 public UserInfo getUserInfo(String loginId) {
82 UserInfo name = userInfoDao.getUserInfoByLoginId(loginId);
86 // EcompName Dictionary
87 @RequestMapping(value = { "/get_RiskTypeDataByName" }, method = {
88 org.springframework.web.bind.annotation.RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
89 public void getRiskTypeDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response) {
90 logger.info("get_RiskTypeDataByName is called");
92 Map<String, Object> model = new HashMap<String, Object>();
93 ObjectMapper mapper = new ObjectMapper();
94 model.put("riskTypeDictionaryDatas", mapper.writeValueAsString(riskTypeDao.getRiskTypeDataByName()));
95 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
96 JSONObject j = new JSONObject(msg);
97 response.getWriter().write(j.toString());
98 } catch (Exception e) {
103 @RequestMapping(value = { "/get_RiskTypeData" }, method = {
104 org.springframework.web.bind.annotation.RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
105 public void getEcompNameDictionaryEntityData(HttpServletRequest request, HttpServletResponse response) {
106 logger.info("get_RiskTypeData is called");
108 Map<String, Object> model = new HashMap<String, Object>();
109 ObjectMapper mapper = new ObjectMapper();
110 model.put("riskTypeDictionaryDatas", mapper.writeValueAsString(riskTypeDao.getRiskName()));
111 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
112 JSONObject j = new JSONObject(msg);
113 response.getWriter().write(j.toString());
114 } catch (Exception e) {
116 logger.error("ERROR While callinge DAO: " + e.getMessage());
120 @RequestMapping(value = { "/sp_dictionary/save_riskType.htm" }, method = {
121 org.springframework.web.bind.annotation.RequestMethod.POST })
122 public ModelAndView saveRiskTypeDictionary(HttpServletRequest request, HttpServletResponse response)
125 boolean duplicateflag = false;
126 System.out.println("SafePolicyController: saveRiskTypeDictionary() is called");
127 logger.debug("SafePolicyController: saveRiskTypeDictionary() is called");
128 ObjectMapper mapper = new ObjectMapper();
129 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
130 JsonNode root = mapper.readTree(request.getReader());
131 RiskType riskTypeData = (RiskType) mapper.readValue(root.get("riskTypeDictionaryData").toString(),
133 String userId = root.get("loginId").textValue();
134 System.out.println("the userId from the ecomp portal is: " + userId);
135 if (riskTypeData.getId() == 0) {
136 CheckDictionaryDuplicateEntries entry = new CheckDictionaryDuplicateEntries();
137 List<Object> duplicateData = entry.CheckDuplicateEntry(riskTypeData.getRiskName(), "name", RiskType.class);
138 if(!duplicateData.isEmpty()){
139 duplicateflag = true;
141 riskTypeData.setUserCreatedBy(getUserInfo(userId));
142 riskTypeData.setUserModifiedBy(getUserInfo(userId));
144 "SafePolicyController: got the user info now about to call Save() method on riskTypedao");
145 riskTypeDao.Save(riskTypeData);
148 riskTypeData.setUserModifiedBy(this.getUserInfo(userId));
149 riskTypeDao.update(riskTypeData);
151 response.setCharacterEncoding("UTF-8");
152 response.setContentType("application / json");
153 request.setCharacterEncoding("UTF-8");
155 PrintWriter out = response.getWriter();
156 String responseString = "";
158 responseString = "Duplicate";
160 responseString = mapper.writeValueAsString(this.riskTypeDao.getRiskName());
162 JSONObject j = new JSONObject("{riskTypeDictionaryDatas: " + responseString + "}");
164 out.write(j.toString());
167 } catch (Exception e) {
168 response.setCharacterEncoding("UTF-8");
169 request.setCharacterEncoding("UTF-8");
170 PrintWriter out = response.getWriter();
171 out.write(e.getMessage());
176 @RequestMapping(value = { "/sp_dictionary/remove_riskType.htm" }, method = {
177 org.springframework.web.bind.annotation.RequestMethod.POST })
178 public ModelAndView removeEcompDictionary(HttpServletRequest request, HttpServletResponse response)
181 ObjectMapper mapper = new ObjectMapper();
182 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
183 JsonNode root = mapper.readTree(request.getReader());
184 RiskType ecompData = (RiskType) mapper.readValue(root.get("data").toString(), RiskType.class);
185 riskTypeDao.delete(ecompData);
186 response.setCharacterEncoding("UTF-8");
187 response.setContentType("application / json");
188 request.setCharacterEncoding("UTF-8");
190 PrintWriter out = response.getWriter();
192 String responseString = mapper.writeValueAsString(this.riskTypeDao.getRiskName());
193 JSONObject j = new JSONObject("{riskTypeDictionaryDatas: " + responseString + "}");
194 out.write(j.toString());
197 } catch (Exception e) {
198 System.out.println(e);
199 response.setCharacterEncoding("UTF-8");
200 request.setCharacterEncoding("UTF-8");
201 PrintWriter out = response.getWriter();
202 out.write(e.getMessage());
207 @RequestMapping(value = { "/get_SafePolicyWarningDataByName" }, method = {
208 org.springframework.web.bind.annotation.RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
209 public void getSafePolicyWarningEntityDataByName(HttpServletRequest request, HttpServletResponse response) {
211 Map<String, Object> model = new HashMap<String, Object>();
212 ObjectMapper mapper = new ObjectMapper();
213 model.put("safePolicyWarningDatas",
214 mapper.writeValueAsString(safePolicyWarningDao.getSafePolicyWarningDataByName()));
215 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
216 JSONObject j = new JSONObject(msg);
217 response.getWriter().write(j.toString());
218 } catch (Exception e) {
223 @RequestMapping(value = { "/get_SafePolicyWarningData" }, method = {
224 org.springframework.web.bind.annotation.RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
225 public void getSafePolicyWarningeEntityData(HttpServletRequest request, HttpServletResponse response) {
227 Map<String, Object> model = new HashMap<String, Object>();
228 ObjectMapper mapper = new ObjectMapper();
229 model.put("safePolicyWarningDatas",
230 mapper.writeValueAsString(safePolicyWarningDao.getSafePolicyWarningData()));
231 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
232 JSONObject j = new JSONObject(msg);
233 response.getWriter().write(j.toString());
234 } catch (Exception e) {
239 @RequestMapping(value = { "/sp_dictionary/save_safePolicyWarning.htm" }, method = {
240 org.springframework.web.bind.annotation.RequestMethod.POST })
241 public ModelAndView saveSafePolicyWarningDictionary(HttpServletRequest request, HttpServletResponse response)
244 boolean duplicateflag = false;
245 ObjectMapper mapper = new ObjectMapper();
246 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
247 JsonNode root = mapper.readTree(request.getReader());
248 SafePolicyWarning safePolicyWarning = (SafePolicyWarning) mapper
249 .readValue(root.get("safePolicyWarningData").toString(), SafePolicyWarning.class);
251 if (safePolicyWarning.getId() == 0) {
252 CheckDictionaryDuplicateEntries entry = new CheckDictionaryDuplicateEntries();
253 List<Object> duplicateData = entry.CheckDuplicateEntry(safePolicyWarning.getName(), "name", SafePolicyWarning.class);
254 if(!duplicateData.isEmpty()){
255 duplicateflag = true;
257 safePolicyWarningDao.Save(safePolicyWarning);
260 safePolicyWarningDao.update(safePolicyWarning);
262 response.setCharacterEncoding("UTF-8");
263 response.setContentType("application / json");
264 request.setCharacterEncoding("UTF-8");
266 PrintWriter out = response.getWriter();
267 String responseString = "";
269 responseString = "Duplicate";
271 responseString = mapper.writeValueAsString(this.safePolicyWarningDao.getSafePolicyWarningData());
273 JSONObject j = new JSONObject("{safePolicyWarningDatas: " + responseString + "}");
275 out.write(j.toString());
278 } catch (Exception e) {
279 response.setCharacterEncoding("UTF-8");
280 request.setCharacterEncoding("UTF-8");
281 PrintWriter out = response.getWriter();
282 out.write(e.getMessage());
287 @RequestMapping(value = { "/sp_dictionary/remove_SafePolicyWarning.htm" }, method = {
288 org.springframework.web.bind.annotation.RequestMethod.POST })
289 public ModelAndView removeSafePolicyWarningDictionary(HttpServletRequest request, HttpServletResponse response)
292 ObjectMapper mapper = new ObjectMapper();
293 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
294 JsonNode root = mapper.readTree(request.getReader());
295 SafePolicyWarning safePolicyWarningData = (SafePolicyWarning) mapper.readValue(root.get("data").toString(),
296 SafePolicyWarning.class);
297 safePolicyWarningDao.delete(safePolicyWarningData);
298 response.setCharacterEncoding("UTF-8");
299 response.setContentType("application / json");
300 request.setCharacterEncoding("UTF-8");
302 PrintWriter out = response.getWriter();
304 String responseString = mapper.writeValueAsString(this.safePolicyWarningDao.getSafePolicyWarningData());
305 JSONObject j = new JSONObject("{groupPolicyScopeListDatas: " + responseString + "}");
306 out.write(j.toString());
309 } catch (Exception e) {
310 System.out.println(e);
311 response.setCharacterEncoding("UTF-8");
312 request.setCharacterEncoding("UTF-8");
313 PrintWriter out = response.getWriter();
314 out.write(e.getMessage());