Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / controller / SafePolicyController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PAP-REST
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.policy.pap.xacml.rest.controller;
22
23 import java.io.PrintWriter;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
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;
48
49 import com.fasterxml.jackson.databind.DeserializationFeature;
50 import com.fasterxml.jackson.databind.JsonNode;
51 import com.fasterxml.jackson.databind.ObjectMapper;
52
53 @Controller
54 public class SafePolicyController {
55
56         private static final Log logger = LogFactory.getLog(SafePolicyController.class);
57
58         @Autowired
59         SafePolicyWarningDao safePolicyWarningDao;
60
61         @Autowired
62         RiskTypeDao riskTypeDao;
63
64         @Autowired
65         UserInfoDao userInfoDao;
66
67         @Autowired
68         CategoryDao categoryDao;
69         
70
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")) {
75                                 return value;
76                         }
77                 }
78                 return null;
79         }
80
81         public UserInfo getUserInfo(String loginId) {
82                 UserInfo name = userInfoDao.getUserInfoByLoginId(loginId);
83                 return name;
84         }
85
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");
91                 try {
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) {
99                         e.printStackTrace();
100                 }
101         }
102
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");
107                 try {
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) {
115                         e.printStackTrace();
116                         logger.error("ERROR While callinge DAO: " + e.getMessage());
117                 }
118         }
119
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)
123                         throws Exception {
124                 try {
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(),
132                                         RiskType.class);
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;
140                                 }else{
141                                         riskTypeData.setUserCreatedBy(getUserInfo(userId));
142                                         riskTypeData.setUserModifiedBy(getUserInfo(userId));
143                                         System.out.println(
144                                                         "SafePolicyController:  got the user info now about to call Save() method on riskTypedao");
145                                         riskTypeDao.Save(riskTypeData);
146                                 }
147                         } else {
148                                 riskTypeData.setUserModifiedBy(this.getUserInfo(userId));
149                                 riskTypeDao.update(riskTypeData);
150                         }
151                         response.setCharacterEncoding("UTF-8");
152                         response.setContentType("application / json");
153                         request.setCharacterEncoding("UTF-8");
154
155                         PrintWriter out = response.getWriter();
156                         String responseString = "";
157                         if(duplicateflag){
158                                 responseString = "Duplicate";
159                         }else{
160                                 responseString = mapper.writeValueAsString(this.riskTypeDao.getRiskName());
161                         }
162                         JSONObject j = new JSONObject("{riskTypeDictionaryDatas: " + responseString + "}");
163
164                         out.write(j.toString());
165
166                         return null;
167                 } catch (Exception e) {
168                         response.setCharacterEncoding("UTF-8");
169                         request.setCharacterEncoding("UTF-8");
170                         PrintWriter out = response.getWriter();
171                         out.write(e.getMessage());
172                 }
173                 return null;
174         }
175
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)
179                         throws Exception {
180                 try {
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");
189
190                         PrintWriter out = response.getWriter();
191
192                         String responseString = mapper.writeValueAsString(this.riskTypeDao.getRiskName());
193                         JSONObject j = new JSONObject("{riskTypeDictionaryDatas: " + responseString + "}");
194                         out.write(j.toString());
195
196                         return null;
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());
203                 }
204                 return null;
205         }
206
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) {
210                 try {
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) {
219                         e.printStackTrace();
220                 }
221         }
222
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) {
226                 try {
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) {
235                         e.printStackTrace();
236                 }
237         }
238
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)
242                         throws Exception {
243                 try {
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);
250
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;
256                                 }else{
257                                         safePolicyWarningDao.Save(safePolicyWarning);
258                                 }
259                         } else {
260                                 safePolicyWarningDao.update(safePolicyWarning);
261                         }
262                         response.setCharacterEncoding("UTF-8");
263                         response.setContentType("application / json");
264                         request.setCharacterEncoding("UTF-8");
265
266                         PrintWriter out = response.getWriter();
267                         String responseString = "";
268                         if(duplicateflag){
269                                 responseString = "Duplicate";
270                         }else{
271                                 responseString = mapper.writeValueAsString(this.safePolicyWarningDao.getSafePolicyWarningData());
272                         }
273                         JSONObject j = new JSONObject("{safePolicyWarningDatas: " + responseString + "}");
274
275                         out.write(j.toString());
276
277                         return null;
278                 } catch (Exception e) {
279                         response.setCharacterEncoding("UTF-8");
280                         request.setCharacterEncoding("UTF-8");
281                         PrintWriter out = response.getWriter();
282                         out.write(e.getMessage());
283                 }
284                 return null;
285         }
286
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)
290                         throws Exception {
291                 try {
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");
301
302                         PrintWriter out = response.getWriter();
303
304                         String responseString = mapper.writeValueAsString(this.safePolicyWarningDao.getSafePolicyWarningData());
305                         JSONObject j = new JSONObject("{groupPolicyScopeListDatas: " + responseString + "}");
306                         out.write(j.toString());
307
308                         return null;
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());
315                 }
316                 return null;
317         }
318
319 }