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 com.fasterxml.jackson.databind.DeserializationFeature;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
27 import org.json.JSONObject;
28 import org.onap.policy.common.logging.flexlogger.FlexLogger;
29 import org.onap.policy.common.logging.flexlogger.Logger;
30 import org.onap.policy.pap.xacml.rest.util.JsonMessage;
31 import org.onap.policy.rest.dao.CommonClassDao;
32 import org.onap.policy.rest.jpa.Datatype;
33 import org.onap.policy.rest.jpa.DecisionSettings;
34 import org.onap.policy.rest.jpa.RainyDayTreatments;
35 import org.onap.policy.rest.jpa.UserInfo;
36 import org.onap.policy.utils.PolicyUtils;
37 import org.onap.policy.xacml.api.XACMLErrorConstants;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.http.MediaType;
40 import org.springframework.stereotype.Controller;
41 import org.springframework.web.bind.annotation.RequestMapping;
42 import org.springframework.web.servlet.ModelAndView;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
47 import java.io.IOException;
48 import java.io.PrintWriter;
49 import java.io.UnsupportedEncodingException;
50 import java.util.ArrayList;
51 import java.util.Date;
52 import java.util.HashMap;
53 import java.util.LinkedHashMap;
54 import java.util.List;
58 public class DecisionPolicyDictionaryController {
60 private static final Logger LOGGER = FlexLogger.getLogger(DecisionPolicyDictionaryController.class);
62 private static CommonClassDao commonClassDao;
65 public DecisionPolicyDictionaryController(CommonClassDao commonClassDao){
66 DecisionPolicyDictionaryController.commonClassDao = commonClassDao;
69 public DecisionPolicyDictionaryController(){}
71 public UserInfo getUserInfo(String loginId){
72 UserInfo name = (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", loginId);
76 @RequestMapping(value={"/get_SettingsDictionaryDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
77 public void getSettingsDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response){
79 Map<String, Object> model = new HashMap<>();
80 ObjectMapper mapper = new ObjectMapper();
81 model.put("settingsDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(DecisionSettings.class, "xacmlId")));
82 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
83 JSONObject j = new JSONObject(msg);
84 response.getWriter().write(j.toString());
87 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
92 @RequestMapping(value={"/get_SettingsDictionaryData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
93 public void getSettingsDictionaryEntityData(HttpServletResponse response){
95 Map<String, Object> model = new HashMap<>();
96 ObjectMapper mapper = new ObjectMapper();
97 model.put("settingsDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(DecisionSettings.class)));
98 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
99 JSONObject j = new JSONObject(msg);
100 response.addHeader("successMapKey", "success");
101 response.addHeader("operation", "getDictionary");
102 response.getWriter().write(j.toString());
105 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
106 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
107 response.addHeader("error", "dictionaryDBQuery");
111 @RequestMapping(value={"/decision_dictionary/save_Settings"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
112 public ModelAndView saveSettingsDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException{
114 boolean duplicateflag = false;
115 boolean isFakeUpdate = false;
116 boolean fromAPI = false;
117 if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
120 ObjectMapper mapper = new ObjectMapper();
121 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
122 JsonNode root = mapper.readTree(request.getReader());
123 DecisionSettings decisionSettings;
124 String userId = null;
127 decisionSettings = (DecisionSettings)mapper.readValue(root.get("dictionaryFields").toString(), DecisionSettings.class);
130 //check if update operation or create, get id for data to be updated and update attributeData
131 if (request.getParameter("operation").equals("update")) {
132 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(decisionSettings.getXacmlId(), "xacmlId", DecisionSettings.class);
134 DecisionSettings data = (DecisionSettings) duplicateData.get(0);
138 decisionSettings.setId(1);
140 decisionSettings.setId(id);
142 decisionSettings.setUserCreatedBy(this.getUserInfo(userId));
145 decisionSettings = (DecisionSettings)mapper.readValue(root.get("settingsDictionaryData").toString(), DecisionSettings.class);
146 userId = root.get("userid").textValue();
148 if(decisionSettings.getDatatypeBean().getShortName() != null){
149 String datatype = decisionSettings.getDatatypeBean().getShortName();
150 Datatype a = new Datatype();
151 if(datatype.equalsIgnoreCase("string")){
153 }else if(datatype.equalsIgnoreCase("integer")){
155 }else if(datatype.equalsIgnoreCase("boolean")){
157 }else if(datatype.equalsIgnoreCase("double")){
159 }else if(datatype.equalsIgnoreCase("user")){
162 decisionSettings.setDatatypeBean(a);
164 if(decisionSettings.getId() == 0){
165 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(decisionSettings.getXacmlId(), "xacmlId", DecisionSettings.class);
166 if(!duplicateData.isEmpty()){
167 duplicateflag = true;
169 decisionSettings.setUserCreatedBy(this.getUserInfo(userId));
170 decisionSettings.setUserModifiedBy(this.getUserInfo(userId));
171 commonClassDao.save(decisionSettings);
175 decisionSettings.setUserModifiedBy(this.getUserInfo(userId));
176 decisionSettings.setModifiedDate(new Date());
177 commonClassDao.update(decisionSettings);
180 String responseString = "";
182 responseString = "Duplicate";
184 responseString = mapper.writeValueAsString(commonClassDao.getData(DecisionSettings.class));
188 if (!"Duplicate".equals(responseString)) {
190 responseString = "Exists";
192 responseString = "Success";
195 ModelAndView result = new ModelAndView();
196 result.setViewName(responseString);
199 response.setCharacterEncoding("UTF-8");
200 response.setContentType("application / json");
201 request.setCharacterEncoding("UTF-8");
203 PrintWriter out = response.getWriter();
204 JSONObject j = new JSONObject("{settingsDictionaryDatas: " + responseString + "}");
205 out.write(j.toString());
209 }catch (Exception e){
210 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
211 response.setCharacterEncoding("UTF-8");
212 request.setCharacterEncoding("UTF-8");
213 PrintWriter out = response.getWriter();
214 out.write(PolicyUtils.CATCH_EXCEPTION);
219 @RequestMapping(value={"/settings_dictionary/remove_settings"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
220 public ModelAndView removeSettingsDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException {
222 ObjectMapper mapper = new ObjectMapper();
223 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
224 JsonNode root = mapper.readTree(request.getReader());
225 DecisionSettings decisionSettings = (DecisionSettings)mapper.readValue(root.get("data").toString(), DecisionSettings.class);
226 commonClassDao.delete(decisionSettings);
227 response.setCharacterEncoding("UTF-8");
228 response.setContentType("application / json");
229 request.setCharacterEncoding("UTF-8");
231 PrintWriter out = response.getWriter();
233 String responseString = mapper.writeValueAsString(commonClassDao.getData(DecisionSettings.class));
234 JSONObject j = new JSONObject("{settingsDictionaryDatas: " + responseString + "}");
235 out.write(j.toString());
240 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
241 response.setCharacterEncoding("UTF-8");
242 request.setCharacterEncoding("UTF-8");
243 PrintWriter out = response.getWriter();
244 out.write(PolicyUtils.CATCH_EXCEPTION);
251 @RequestMapping(value={"/get_RainyDayDictionaryDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
252 public void getRainyDayDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response){
254 Map<String, Object> model = new HashMap<>();
255 ObjectMapper mapper = new ObjectMapper();
256 model.put("rainyDayDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(RainyDayTreatments.class, "bbid")));
257 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
258 JSONObject j = new JSONObject(msg);
259 response.getWriter().write(j.toString());
262 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
267 @RequestMapping(value={"/get_RainyDayDictionaryData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
268 public void getRainyDayDictionaryEntityData(HttpServletResponse response){
270 Map<String, Object> model = new HashMap<>();
271 ObjectMapper mapper = new ObjectMapper();
272 model.put("rainyDayDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class)));
273 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
274 JSONObject j = new JSONObject(msg);
275 response.addHeader("successMapKey", "success");
276 response.addHeader("operation", "getDictionary");
277 response.getWriter().write(j.toString());
280 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
281 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
282 response.addHeader("error", "dictionaryDBQuery");
286 @RequestMapping(value={"/decision_dictionary/save_RainyDay"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
287 public ModelAndView saveRainyDayDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException{
289 boolean duplicateflag = false;
290 boolean isFakeUpdate = false;
291 boolean fromAPI = false;
292 if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
295 ObjectMapper mapper = new ObjectMapper();
296 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
297 JsonNode root = mapper.readTree(request.getReader());
298 RainyDayTreatments decisionRainyDay;
299 TreatmentValues treatmentsData = null;
301 decisionRainyDay = (RainyDayTreatments)mapper.readValue(root.get("dictionaryFields").toString(), RainyDayTreatments.class);
302 treatmentsData = (TreatmentValues)mapper.readValue(root.get("dictionaryFields").toString(), TreatmentValues.class);
303 //check if update operation or create, get id for data to be updated and update attributeData
304 if (request.getParameter("operation").equals("update")) {
305 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(decisionRainyDay.getBbid()+":"+decisionRainyDay.getWorkstep(), "bbid:workstep", RainyDayTreatments.class);
307 RainyDayTreatments data = (RainyDayTreatments) duplicateData.get(0);
312 decisionRainyDay.setId(id);
316 decisionRainyDay = (RainyDayTreatments)mapper.readValue(root.get("rainyDayDictionaryData").toString(), RainyDayTreatments.class);
317 treatmentsData = (TreatmentValues)mapper.readValue(root.get("rainyDayDictionaryData").toString(), TreatmentValues.class);
320 String userValue = "";
322 if(treatmentsData.getUserDataTypeValues().size() > 0){
323 for(Object treatment : treatmentsData.getUserDataTypeValues()){
324 if(treatment instanceof LinkedHashMap<?, ?>){
325 String key = ((LinkedHashMap<?, ?>) treatment).get("treatment").toString();
327 userValue = userValue + ",";
329 userValue = userValue + key ;
333 decisionRainyDay.setTreatments(userValue);
336 if(decisionRainyDay.getId() == 0){
337 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(decisionRainyDay.getBbid()+":"+decisionRainyDay.getWorkstep(), "bbid:workstep", RainyDayTreatments.class);
338 if(!duplicateData.isEmpty()){
339 duplicateflag = true;
341 commonClassDao.save(decisionRainyDay);
345 commonClassDao.update(decisionRainyDay);
348 String responseString = "";
350 responseString = "Duplicate";
352 responseString = mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class));
356 if (!"Duplicate".equals(responseString)) {
358 responseString = "Exists";
360 responseString = "Success";
363 ModelAndView result = new ModelAndView();
364 result.setViewName(responseString);
367 response.setCharacterEncoding("UTF-8");
368 response.setContentType("application / json");
369 request.setCharacterEncoding("UTF-8");
371 PrintWriter out = response.getWriter();
372 JSONObject j = new JSONObject("{rainyDayDictionaryDatas: " + responseString + "}");
373 out.write(j.toString());
377 }catch (Exception e){
378 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
379 response.setCharacterEncoding("UTF-8");
380 request.setCharacterEncoding("UTF-8");
381 PrintWriter out = response.getWriter();
382 out.write(PolicyUtils.CATCH_EXCEPTION);
387 @RequestMapping(value={"/decision_dictionary/remove_rainyDay"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
388 public ModelAndView removeRainyDayDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException {
390 ObjectMapper mapper = new ObjectMapper();
391 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
392 JsonNode root = mapper.readTree(request.getReader());
393 RainyDayTreatments decisionRainyDay = (RainyDayTreatments)mapper.readValue(root.get("data").toString(), RainyDayTreatments.class);
394 commonClassDao.delete(decisionRainyDay);
395 response.setCharacterEncoding("UTF-8");
396 response.setContentType("application / json");
397 request.setCharacterEncoding("UTF-8");
399 PrintWriter out = response.getWriter();
401 String responseString = mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class));
402 JSONObject j = new JSONObject("{rainyDayDictionaryDatas: " + responseString + "}");
403 out.write(j.toString());
408 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
409 response.setCharacterEncoding("UTF-8");
410 request.setCharacterEncoding("UTF-8");
411 PrintWriter out = response.getWriter();
412 out.write(PolicyUtils.CATCH_EXCEPTION);
419 class TreatmentValues {
420 private ArrayList<Object> userDataTypeValues = new ArrayList<>();
422 public ArrayList<Object> getUserDataTypeValues() {
423 return userDataTypeValues;
426 public void setUserDataTypeValues(ArrayList<Object> userDataTypeValues) {
427 this.userDataTypeValues = userDataTypeValues;