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.ArrayList;
25 import java.util.HashMap;
26 import java.util.LinkedHashMap;
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.openecomp.policy.common.logging.flexlogger.FlexLogger;
35 import org.openecomp.policy.common.logging.flexlogger.Logger;
36 import org.openecomp.policy.pap.xacml.rest.util.JsonMessage;
37 import org.openecomp.policy.rest.dao.CommonClassDao;
38 import org.openecomp.policy.rest.jpa.Datatype;
39 import org.openecomp.policy.rest.jpa.DecisionSettings;
40 import org.openecomp.policy.rest.jpa.RainyDayTreatments;
41 import org.openecomp.policy.rest.jpa.UserInfo;
42 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
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 DecisionPolicyDictionaryController {
56 private static final Logger LOGGER = FlexLogger.getLogger(DecisionPolicyDictionaryController.class);
58 private static CommonClassDao commonClassDao;
61 public DecisionPolicyDictionaryController(CommonClassDao commonClassDao){
62 DecisionPolicyDictionaryController.commonClassDao = commonClassDao;
65 public DecisionPolicyDictionaryController(){}
67 public UserInfo getUserInfo(String loginId){
68 UserInfo name = (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", loginId);
72 @RequestMapping(value={"/get_SettingsDictionaryDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
73 public void getSettingsDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response){
75 Map<String, Object> model = new HashMap<>();
76 ObjectMapper mapper = new ObjectMapper();
77 model.put("settingsDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(DecisionSettings.class, "xacmlId")));
78 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
79 JSONObject j = new JSONObject(msg);
80 response.getWriter().write(j.toString());
83 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
88 @RequestMapping(value={"/get_SettingsDictionaryData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
89 public void getSettingsDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
91 Map<String, Object> model = new HashMap<>();
92 ObjectMapper mapper = new ObjectMapper();
93 model.put("settingsDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(DecisionSettings.class)));
94 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
95 JSONObject j = new JSONObject(msg);
96 response.addHeader("successMapKey", "success");
97 response.addHeader("operation", "getDictionary");
98 response.getWriter().write(j.toString());
101 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
102 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
103 response.addHeader("error", "dictionaryDBQuery");
107 @RequestMapping(value={"/decision_dictionary/save_Settings"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
108 public ModelAndView saveSettingsDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{
110 boolean duplicateflag = false;
111 boolean isFakeUpdate = false;
112 boolean fromAPI = false;
113 if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
116 ObjectMapper mapper = new ObjectMapper();
117 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
118 JsonNode root = mapper.readTree(request.getReader());
119 DecisionSettings decisionSettings;
120 String userId = null;
123 decisionSettings = (DecisionSettings)mapper.readValue(root.get("dictionaryFields").toString(), DecisionSettings.class);
126 //check if update operation or create, get id for data to be updated and update attributeData
127 if (request.getParameter("operation").equals("update")) {
128 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(decisionSettings.getXacmlId(), "xacmlId", DecisionSettings.class);
130 DecisionSettings data = (DecisionSettings) duplicateData.get(0);
134 decisionSettings.setId(1);
136 decisionSettings.setId(id);
138 decisionSettings.setUserCreatedBy(this.getUserInfo(userId));
141 decisionSettings = (DecisionSettings)mapper.readValue(root.get("settingsDictionaryData").toString(), DecisionSettings.class);
142 userId = root.get("userid").textValue();
144 if(decisionSettings.getDatatypeBean().getShortName() != null){
145 String datatype = decisionSettings.getDatatypeBean().getShortName();
146 Datatype a = new Datatype();
147 if(datatype.equalsIgnoreCase("string")){
149 }else if(datatype.equalsIgnoreCase("integer")){
151 }else if(datatype.equalsIgnoreCase("boolean")){
153 }else if(datatype.equalsIgnoreCase("double")){
155 }else if(datatype.equalsIgnoreCase("user")){
158 decisionSettings.setDatatypeBean(a);
160 if(decisionSettings.getId() == 0){
161 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(decisionSettings.getXacmlId(), "xacmlId", DecisionSettings.class);
162 if(!duplicateData.isEmpty()){
163 duplicateflag = true;
165 decisionSettings.setUserCreatedBy(this.getUserInfo(userId));
166 decisionSettings.setUserModifiedBy(this.getUserInfo(userId));
167 commonClassDao.save(decisionSettings);
171 decisionSettings.setUserModifiedBy(this.getUserInfo(userId));
172 commonClassDao.update(decisionSettings);
175 String responseString = "";
177 responseString = "Duplicate";
179 responseString = mapper.writeValueAsString(commonClassDao.getData(DecisionSettings.class));
183 if (responseString!=null && !responseString.equals("Duplicate")) {
185 responseString = "Exists";
187 responseString = "Success";
190 ModelAndView result = new ModelAndView();
191 result.setViewName(responseString);
194 response.setCharacterEncoding("UTF-8");
195 response.setContentType("application / json");
196 request.setCharacterEncoding("UTF-8");
198 PrintWriter out = response.getWriter();
199 JSONObject j = new JSONObject("{settingsDictionaryDatas: " + responseString + "}");
200 out.write(j.toString());
204 }catch (Exception e){
205 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
206 response.setCharacterEncoding("UTF-8");
207 request.setCharacterEncoding("UTF-8");
208 PrintWriter out = response.getWriter();
209 out.write(e.getMessage());
214 @RequestMapping(value={"/settings_dictionary/remove_settings"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
215 public ModelAndView removeSettingsDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception {
217 ObjectMapper mapper = new ObjectMapper();
218 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
219 JsonNode root = mapper.readTree(request.getReader());
220 DecisionSettings decisionSettings = (DecisionSettings)mapper.readValue(root.get("data").toString(), DecisionSettings.class);
221 commonClassDao.delete(decisionSettings);
222 response.setCharacterEncoding("UTF-8");
223 response.setContentType("application / json");
224 request.setCharacterEncoding("UTF-8");
226 PrintWriter out = response.getWriter();
228 String responseString = mapper.writeValueAsString(commonClassDao.getData(DecisionSettings.class));
229 JSONObject j = new JSONObject("{settingsDictionaryDatas: " + responseString + "}");
230 out.write(j.toString());
235 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
236 response.setCharacterEncoding("UTF-8");
237 request.setCharacterEncoding("UTF-8");
238 PrintWriter out = response.getWriter();
239 out.write(e.getMessage());
246 @RequestMapping(value={"/get_RainyDayDictionaryDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
247 public void getRainyDayDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response){
249 Map<String, Object> model = new HashMap<>();
250 ObjectMapper mapper = new ObjectMapper();
251 model.put("rainyDayDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(RainyDayTreatments.class, "bbID")));
252 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
253 JSONObject j = new JSONObject(msg);
254 response.getWriter().write(j.toString());
257 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
262 @RequestMapping(value={"/get_RainyDayDictionaryData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
263 public void getRainyDayDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
265 Map<String, Object> model = new HashMap<>();
266 ObjectMapper mapper = new ObjectMapper();
267 model.put("rainyDayDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class)));
268 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
269 JSONObject j = new JSONObject(msg);
270 response.addHeader("successMapKey", "success");
271 response.addHeader("operation", "getDictionary");
272 response.getWriter().write(j.toString());
275 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
276 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
277 response.addHeader("error", "dictionaryDBQuery");
281 @RequestMapping(value={"/decision_dictionary/save_RainyDay"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
282 public ModelAndView saveRainyDayDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{
284 boolean duplicateflag = false;
285 boolean isFakeUpdate = false;
286 boolean fromAPI = false;
287 if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
290 ObjectMapper mapper = new ObjectMapper();
291 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
292 JsonNode root = mapper.readTree(request.getReader());
293 RainyDayTreatments decisionRainyDay;
294 TreatmentValues treatmentsData = null;
296 decisionRainyDay = (RainyDayTreatments)mapper.readValue(root.get("dictionaryFields").toString(), RainyDayTreatments.class);
297 treatmentsData = (TreatmentValues)mapper.readValue(root.get("dictionaryFields").toString(), TreatmentValues.class);
298 //check if update operation or create, get id for data to be updated and update attributeData
299 if (request.getParameter("operation").equals("update")) {
300 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(decisionRainyDay.getBbid()+":"+decisionRainyDay.getWorkstep(), "bbid:workstep", RainyDayTreatments.class);
302 RainyDayTreatments data = (RainyDayTreatments) duplicateData.get(0);
307 decisionRainyDay.setId(id);
311 decisionRainyDay = (RainyDayTreatments)mapper.readValue(root.get("rainyDayDictionaryData").toString(), RainyDayTreatments.class);
312 treatmentsData = (TreatmentValues)mapper.readValue(root.get("rainyDayDictionaryData").toString(), TreatmentValues.class);
315 String userValue = "";
317 if(treatmentsData.getUserDataTypeValues().size() > 0){
318 for(Object treatment : treatmentsData.getUserDataTypeValues()){
319 if(treatment instanceof LinkedHashMap<?, ?>){
320 String key = ((LinkedHashMap<?, ?>) treatment).get("treatment").toString();
322 userValue = userValue + ",";
324 userValue = userValue + key ;
329 decisionRainyDay.setTreatments(userValue);
331 if(decisionRainyDay.getId() == 0){
332 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(decisionRainyDay.getBbid()+":"+decisionRainyDay.getWorkstep(), "bbid:workstep", RainyDayTreatments.class);
333 if(!duplicateData.isEmpty()){
334 duplicateflag = true;
336 commonClassDao.save(decisionRainyDay);
340 commonClassDao.update(decisionRainyDay);
343 String responseString = "";
345 responseString = "Duplicate";
347 responseString = mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class));
351 if (responseString!=null && !responseString.equals("Duplicate")) {
353 responseString = "Exists";
355 responseString = "Success";
358 ModelAndView result = new ModelAndView();
359 result.setViewName(responseString);
362 response.setCharacterEncoding("UTF-8");
363 response.setContentType("application / json");
364 request.setCharacterEncoding("UTF-8");
366 PrintWriter out = response.getWriter();
367 JSONObject j = new JSONObject("{rainyDayDictionaryDatas: " + responseString + "}");
368 out.write(j.toString());
372 }catch (Exception e){
373 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
374 response.setCharacterEncoding("UTF-8");
375 request.setCharacterEncoding("UTF-8");
376 PrintWriter out = response.getWriter();
377 out.write(e.getMessage());
382 @RequestMapping(value={"/decision_dictionary/remove_rainyDay"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
383 public ModelAndView removeRainyDayDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception {
385 ObjectMapper mapper = new ObjectMapper();
386 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
387 JsonNode root = mapper.readTree(request.getReader());
388 RainyDayTreatments decisionRainyDay = (RainyDayTreatments)mapper.readValue(root.get("data").toString(), RainyDayTreatments.class);
389 commonClassDao.delete(decisionRainyDay);
390 response.setCharacterEncoding("UTF-8");
391 response.setContentType("application / json");
392 request.setCharacterEncoding("UTF-8");
394 PrintWriter out = response.getWriter();
396 String responseString = mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class));
397 JSONObject j = new JSONObject("{rainyDayDictionaryDatas: " + responseString + "}");
398 out.write(j.toString());
403 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
404 response.setCharacterEncoding("UTF-8");
405 request.setCharacterEncoding("UTF-8");
406 PrintWriter out = response.getWriter();
407 out.write(e.getMessage());
414 class TreatmentValues {
415 private ArrayList<Object> userDataTypeValues;
417 public ArrayList<Object> getUserDataTypeValues() {
418 return userDataTypeValues;
421 public void setUserDataTypeValues(ArrayList<Object> userDataTypeValues) {
422 this.userDataTypeValues = userDataTypeValues;