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;
26 import org.json.JSONObject;
27 import org.onap.policy.common.logging.flexlogger.FlexLogger;
28 import org.onap.policy.common.logging.flexlogger.Logger;
29 import org.onap.policy.pap.xacml.rest.util.JsonMessage;
30 import org.onap.policy.rest.dao.CommonClassDao;
31 import org.onap.policy.rest.jpa.Datatype;
32 import org.onap.policy.rest.jpa.DecisionSettings;
33 import org.onap.policy.rest.jpa.RainyDayTreatments;
34 import org.onap.policy.rest.jpa.UserInfo;
35 import org.onap.policy.xacml.api.XACMLErrorConstants;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.http.MediaType;
38 import org.springframework.stereotype.Controller;
39 import org.springframework.web.bind.annotation.RequestMapping;
40 import org.springframework.web.servlet.ModelAndView;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44 import java.io.IOException;
45 import java.io.PrintWriter;
46 import java.io.UnsupportedEncodingException;
47 import java.util.ArrayList;
48 import java.util.Date;
49 import java.util.HashMap;
50 import java.util.LinkedHashMap;
51 import java.util.List;
55 public class DecisionPolicyDictionaryController {
57 private static final Logger LOGGER = FlexLogger.getLogger(DecisionPolicyDictionaryController.class);
59 private static CommonClassDao commonClassDao;
62 public DecisionPolicyDictionaryController(CommonClassDao commonClassDao){
63 DecisionPolicyDictionaryController.commonClassDao = commonClassDao;
66 public DecisionPolicyDictionaryController(){}
68 public UserInfo getUserInfo(String loginId){
69 UserInfo name = (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", loginId);
73 @RequestMapping(value={"/get_SettingsDictionaryDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
74 public void getSettingsDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response){
76 Map<String, Object> model = new HashMap<>();
77 ObjectMapper mapper = new ObjectMapper();
78 model.put("settingsDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(DecisionSettings.class, "xacmlId")));
79 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
80 JSONObject j = new JSONObject(msg);
81 response.getWriter().write(j.toString());
84 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
89 @RequestMapping(value={"/get_SettingsDictionaryData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
90 public void getSettingsDictionaryEntityData(HttpServletResponse response){
92 Map<String, Object> model = new HashMap<>();
93 ObjectMapper mapper = new ObjectMapper();
94 model.put("settingsDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(DecisionSettings.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());
102 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
103 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
104 response.addHeader("error", "dictionaryDBQuery");
108 @RequestMapping(value={"/decision_dictionary/save_Settings"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
109 public ModelAndView saveSettingsDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException{
111 boolean duplicateflag = false;
112 boolean isFakeUpdate = false;
113 boolean fromAPI = false;
114 if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
117 ObjectMapper mapper = new ObjectMapper();
118 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
119 JsonNode root = mapper.readTree(request.getReader());
120 DecisionSettings decisionSettings;
121 String userId = null;
124 decisionSettings = (DecisionSettings)mapper.readValue(root.get("dictionaryFields").toString(), DecisionSettings.class);
127 //check if update operation or create, get id for data to be updated and update attributeData
128 if (request.getParameter("operation").equals("update")) {
129 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(decisionSettings.getXacmlId(), "xacmlId", DecisionSettings.class);
131 DecisionSettings data = (DecisionSettings) duplicateData.get(0);
135 decisionSettings.setId(1);
137 decisionSettings.setId(id);
139 decisionSettings.setUserCreatedBy(this.getUserInfo(userId));
142 decisionSettings = (DecisionSettings)mapper.readValue(root.get("settingsDictionaryData").toString(), DecisionSettings.class);
143 userId = root.get("userid").textValue();
145 if(decisionSettings.getDatatypeBean().getShortName() != null){
146 String datatype = decisionSettings.getDatatypeBean().getShortName();
147 Datatype a = new Datatype();
148 if(datatype.equalsIgnoreCase("string")){
150 }else if(datatype.equalsIgnoreCase("integer")){
152 }else if(datatype.equalsIgnoreCase("boolean")){
154 }else if(datatype.equalsIgnoreCase("double")){
156 }else if(datatype.equalsIgnoreCase("user")){
159 decisionSettings.setDatatypeBean(a);
161 if(decisionSettings.getId() == 0){
162 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(decisionSettings.getXacmlId(), "xacmlId", DecisionSettings.class);
163 if(!duplicateData.isEmpty()){
164 duplicateflag = true;
166 decisionSettings.setUserCreatedBy(this.getUserInfo(userId));
167 decisionSettings.setUserModifiedBy(this.getUserInfo(userId));
168 commonClassDao.save(decisionSettings);
172 decisionSettings.setUserModifiedBy(this.getUserInfo(userId));
173 decisionSettings.setModifiedDate(new Date());
174 commonClassDao.update(decisionSettings);
177 String responseString = "";
179 responseString = "Duplicate";
181 responseString = mapper.writeValueAsString(commonClassDao.getData(DecisionSettings.class));
185 if (!"Duplicate".equals(responseString)) {
187 responseString = "Exists";
189 responseString = "Success";
192 ModelAndView result = new ModelAndView();
193 result.setViewName(responseString);
196 response.setCharacterEncoding("UTF-8");
197 response.setContentType("application / json");
198 request.setCharacterEncoding("UTF-8");
200 PrintWriter out = response.getWriter();
201 JSONObject j = new JSONObject("{settingsDictionaryDatas: " + responseString + "}");
202 out.write(j.toString());
206 }catch (Exception e){
207 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
208 response.setCharacterEncoding("UTF-8");
209 request.setCharacterEncoding("UTF-8");
210 PrintWriter out = response.getWriter();
211 out.write(e.getMessage());
216 @RequestMapping(value={"/settings_dictionary/remove_settings"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
217 public ModelAndView removeSettingsDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException {
219 ObjectMapper mapper = new ObjectMapper();
220 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
221 JsonNode root = mapper.readTree(request.getReader());
222 DecisionSettings decisionSettings = (DecisionSettings)mapper.readValue(root.get("data").toString(), DecisionSettings.class);
223 commonClassDao.delete(decisionSettings);
224 response.setCharacterEncoding("UTF-8");
225 response.setContentType("application / json");
226 request.setCharacterEncoding("UTF-8");
228 PrintWriter out = response.getWriter();
230 String responseString = mapper.writeValueAsString(commonClassDao.getData(DecisionSettings.class));
231 JSONObject j = new JSONObject("{settingsDictionaryDatas: " + responseString + "}");
232 out.write(j.toString());
237 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
238 response.setCharacterEncoding("UTF-8");
239 request.setCharacterEncoding("UTF-8");
240 PrintWriter out = response.getWriter();
241 out.write(e.getMessage());
248 @RequestMapping(value={"/get_RainyDayDictionaryDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
249 public void getRainyDayDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response){
251 Map<String, Object> model = new HashMap<>();
252 ObjectMapper mapper = new ObjectMapper();
253 model.put("rainyDayDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(RainyDayTreatments.class, "bbid")));
254 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
255 JSONObject j = new JSONObject(msg);
256 response.getWriter().write(j.toString());
259 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
264 @RequestMapping(value={"/get_RainyDayDictionaryData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
265 public void getRainyDayDictionaryEntityData(HttpServletResponse response){
267 Map<String, Object> model = new HashMap<>();
268 ObjectMapper mapper = new ObjectMapper();
269 model.put("rainyDayDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class)));
270 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
271 JSONObject j = new JSONObject(msg);
272 response.addHeader("successMapKey", "success");
273 response.addHeader("operation", "getDictionary");
274 response.getWriter().write(j.toString());
277 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
278 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
279 response.addHeader("error", "dictionaryDBQuery");
283 @RequestMapping(value={"/decision_dictionary/save_RainyDay"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
284 public ModelAndView saveRainyDayDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException{
286 boolean duplicateflag = false;
287 boolean isFakeUpdate = false;
288 boolean fromAPI = false;
289 if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
292 ObjectMapper mapper = new ObjectMapper();
293 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
294 JsonNode root = mapper.readTree(request.getReader());
295 RainyDayTreatments decisionRainyDay;
296 TreatmentValues treatmentsData = null;
298 decisionRainyDay = (RainyDayTreatments)mapper.readValue(root.get("dictionaryFields").toString(), RainyDayTreatments.class);
299 treatmentsData = (TreatmentValues)mapper.readValue(root.get("dictionaryFields").toString(), TreatmentValues.class);
300 //check if update operation or create, get id for data to be updated and update attributeData
301 if (request.getParameter("operation").equals("update")) {
302 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(decisionRainyDay.getBbid()+":"+decisionRainyDay.getWorkstep(), "bbid:workstep", RainyDayTreatments.class);
304 RainyDayTreatments data = (RainyDayTreatments) duplicateData.get(0);
309 decisionRainyDay.setId(id);
313 decisionRainyDay = (RainyDayTreatments)mapper.readValue(root.get("rainyDayDictionaryData").toString(), RainyDayTreatments.class);
314 treatmentsData = (TreatmentValues)mapper.readValue(root.get("rainyDayDictionaryData").toString(), TreatmentValues.class);
317 String userValue = "";
319 if(treatmentsData.getUserDataTypeValues().size() > 0){
320 for(Object treatment : treatmentsData.getUserDataTypeValues()){
321 if(treatment instanceof LinkedHashMap<?, ?>){
322 String key = ((LinkedHashMap<?, ?>) treatment).get("treatment").toString();
324 userValue = userValue + ",";
326 userValue = userValue + key ;
330 decisionRainyDay.setTreatments(userValue);
333 if(decisionRainyDay.getId() == 0){
334 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(decisionRainyDay.getBbid()+":"+decisionRainyDay.getWorkstep(), "bbid:workstep", RainyDayTreatments.class);
335 if(!duplicateData.isEmpty()){
336 duplicateflag = true;
338 commonClassDao.save(decisionRainyDay);
342 commonClassDao.update(decisionRainyDay);
345 String responseString = "";
347 responseString = "Duplicate";
349 responseString = mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class));
353 if (!"Duplicate".equals(responseString)) {
355 responseString = "Exists";
357 responseString = "Success";
360 ModelAndView result = new ModelAndView();
361 result.setViewName(responseString);
364 response.setCharacterEncoding("UTF-8");
365 response.setContentType("application / json");
366 request.setCharacterEncoding("UTF-8");
368 PrintWriter out = response.getWriter();
369 JSONObject j = new JSONObject("{rainyDayDictionaryDatas: " + responseString + "}");
370 out.write(j.toString());
374 }catch (Exception e){
375 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
376 response.setCharacterEncoding("UTF-8");
377 request.setCharacterEncoding("UTF-8");
378 PrintWriter out = response.getWriter();
379 out.write(e.getMessage());
384 @RequestMapping(value={"/decision_dictionary/remove_rainyDay"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
385 public ModelAndView removeRainyDayDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException {
387 ObjectMapper mapper = new ObjectMapper();
388 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
389 JsonNode root = mapper.readTree(request.getReader());
390 RainyDayTreatments decisionRainyDay = (RainyDayTreatments)mapper.readValue(root.get("data").toString(), RainyDayTreatments.class);
391 commonClassDao.delete(decisionRainyDay);
392 response.setCharacterEncoding("UTF-8");
393 response.setContentType("application / json");
394 request.setCharacterEncoding("UTF-8");
396 PrintWriter out = response.getWriter();
398 String responseString = mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class));
399 JSONObject j = new JSONObject("{rainyDayDictionaryDatas: " + responseString + "}");
400 out.write(j.toString());
405 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
406 response.setCharacterEncoding("UTF-8");
407 request.setCharacterEncoding("UTF-8");
408 PrintWriter out = response.getWriter();
409 out.write(e.getMessage());
416 class TreatmentValues {
417 private ArrayList<Object> userDataTypeValues = new ArrayList<>();
419 public ArrayList<Object> getUserDataTypeValues() {
420 return userDataTypeValues;
423 public void setUserDataTypeValues(ArrayList<Object> userDataTypeValues) {
424 this.userDataTypeValues = userDataTypeValues;