X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ECOMP-PAP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fpolicy%2Fpap%2Fxacml%2Frest%2Fcontroller%2FDecisionPolicyDictionaryController.java;h=fc525bbe36363d0e81aae50490cc7b9d363e1e7b;hb=eb654437ee2815028602daa943d0a939a5bee3cd;hp=3308a990f82a01db3f016e8464ea03f768474572;hpb=91d04c64771832a0b8815ffbe1f0f9920320d94d;p=policy%2Fengine.git diff --git a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/controller/DecisionPolicyDictionaryController.java b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/controller/DecisionPolicyDictionaryController.java index 3308a990f..fc525bbe3 100644 --- a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/controller/DecisionPolicyDictionaryController.java +++ b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/controller/DecisionPolicyDictionaryController.java @@ -21,7 +21,9 @@ package org.openecomp.policy.pap.xacml.rest.controller; import java.io.PrintWriter; +import java.util.ArrayList; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -29,12 +31,15 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONObject; +import org.openecomp.policy.common.logging.flexlogger.FlexLogger; +import org.openecomp.policy.common.logging.flexlogger.Logger; import org.openecomp.policy.pap.xacml.rest.util.JsonMessage; -import org.openecomp.policy.rest.dao.DecisionPolicyDao; -import org.openecomp.policy.rest.dao.UserInfoDao; +import org.openecomp.policy.rest.dao.CommonClassDao; import org.openecomp.policy.rest.jpa.Datatype; import org.openecomp.policy.rest.jpa.DecisionSettings; +import org.openecomp.policy.rest.jpa.RainyDayTreatments; import org.openecomp.policy.rest.jpa.UserInfo; +import org.openecomp.policy.xacml.api.XACMLErrorConstants; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; @@ -48,29 +53,34 @@ import com.fasterxml.jackson.databind.ObjectMapper; @Controller public class DecisionPolicyDictionaryController { - @Autowired - DecisionPolicyDao decisionPolicyDao; + private static final Logger LOGGER = FlexLogger.getLogger(DecisionPolicyDictionaryController.class); + + private static CommonClassDao commonClassDao; @Autowired - UserInfoDao userInfoDao; + public DecisionPolicyDictionaryController(CommonClassDao commonClassDao){ + DecisionPolicyDictionaryController.commonClassDao = commonClassDao; + } + + public DecisionPolicyDictionaryController(){} public UserInfo getUserInfo(String loginId){ - UserInfo name = userInfoDao.getUserInfoByLoginId(loginId); + UserInfo name = (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", loginId); return name; } @RequestMapping(value={"/get_SettingsDictionaryDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE) public void getSettingsDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response){ try{ - Map model = new HashMap(); + Map model = new HashMap<>(); ObjectMapper mapper = new ObjectMapper(); - model.put("settingsDictionaryDatas", mapper.writeValueAsString(decisionPolicyDao.getDecisionDataByName())); + model.put("settingsDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(DecisionSettings.class, "xacmlId"))); JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); JSONObject j = new JSONObject(msg); response.getWriter().write(j.toString()); } catch (Exception e){ - e.printStackTrace(); + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e); } } @@ -78,27 +88,59 @@ public class DecisionPolicyDictionaryController { @RequestMapping(value={"/get_SettingsDictionaryData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE) public void getSettingsDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){ try{ - Map model = new HashMap(); + Map model = new HashMap<>(); ObjectMapper mapper = new ObjectMapper(); - model.put("settingsDictionaryDatas", mapper.writeValueAsString(decisionPolicyDao.getDecisionSettingsData())); + model.put("settingsDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(DecisionSettings.class))); JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); JSONObject j = new JSONObject(msg); + response.addHeader("successMapKey", "success"); + response.addHeader("operation", "getDictionary"); response.getWriter().write(j.toString()); } catch (Exception e){ - e.printStackTrace(); + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e); + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + response.addHeader("error", "dictionaryDBQuery"); } } - @RequestMapping(value={"/decision_dictionary/save_Settings.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST}) + @RequestMapping(value={"/decision_dictionary/save_Settings"}, method={org.springframework.web.bind.annotation.RequestMethod.POST}) public ModelAndView saveSettingsDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{ try { boolean duplicateflag = false; + boolean isFakeUpdate = false; + boolean fromAPI = false; + if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) { + fromAPI = true; + } ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); - DecisionSettings decisionSettings = (DecisionSettings)mapper.readValue(root.get("settingsDictionaryData").toString(), DecisionSettings.class); - String userId = root.get("loginId").textValue(); + DecisionSettings decisionSettings; + String userId = null; + + if (fromAPI) { + decisionSettings = (DecisionSettings)mapper.readValue(root.get("dictionaryFields").toString(), DecisionSettings.class); + userId = "API"; + + //check if update operation or create, get id for data to be updated and update attributeData + if (request.getParameter("operation").equals("update")) { + List duplicateData = commonClassDao.checkDuplicateEntry(decisionSettings.getXacmlId(), "xacmlId", DecisionSettings.class); + int id = 0; + DecisionSettings data = (DecisionSettings) duplicateData.get(0); + id = data.getId(); + if(id==0){ + isFakeUpdate=true; + decisionSettings.setId(1); + } else { + decisionSettings.setId(id); + } + decisionSettings.setUserCreatedBy(this.getUserInfo(userId)); + } + } else { + decisionSettings = (DecisionSettings)mapper.readValue(root.get("settingsDictionaryData").toString(), DecisionSettings.class); + userId = root.get("userid").textValue(); + } if(decisionSettings.getDatatypeBean().getShortName() != null){ String datatype = decisionSettings.getDatatypeBean().getShortName(); Datatype a = new Datatype(); @@ -116,37 +158,81 @@ public class DecisionPolicyDictionaryController { decisionSettings.setDatatypeBean(a); } if(decisionSettings.getId() == 0){ - CheckDictionaryDuplicateEntries entry = new CheckDictionaryDuplicateEntries(); - List duplicateData = entry.CheckDuplicateEntry(decisionSettings.getXacmlId(), "xacmlId", DecisionSettings.class); + List duplicateData = commonClassDao.checkDuplicateEntry(decisionSettings.getXacmlId(), "xacmlId", DecisionSettings.class); if(!duplicateData.isEmpty()){ duplicateflag = true; }else{ decisionSettings.setUserCreatedBy(this.getUserInfo(userId)); decisionSettings.setUserModifiedBy(this.getUserInfo(userId)); - decisionPolicyDao.Save(decisionSettings); + commonClassDao.save(decisionSettings); } }else{ - decisionSettings.setUserModifiedBy(this.getUserInfo(userId)); - decisionPolicyDao.update(decisionSettings); - } + if(!isFakeUpdate) { + decisionSettings.setUserModifiedBy(this.getUserInfo(userId)); + commonClassDao.update(decisionSettings); + } + } + String responseString = ""; + if(duplicateflag){ + responseString = "Duplicate"; + }else{ + responseString = mapper.writeValueAsString(commonClassDao.getData(DecisionSettings.class)); + } + + if (fromAPI) { + if (responseString!=null && !responseString.equals("Duplicate")) { + if(isFakeUpdate){ + responseString = "Exists"; + } else { + responseString = "Success"; + } + } + ModelAndView result = new ModelAndView(); + result.setViewName(responseString); + return result; + } else { + response.setCharacterEncoding("UTF-8"); + response.setContentType("application / json"); + request.setCharacterEncoding("UTF-8"); + + PrintWriter out = response.getWriter(); + JSONObject j = new JSONObject("{settingsDictionaryDatas: " + responseString + "}"); + out.write(j.toString()); + return null; + } + + }catch (Exception e){ + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e); + response.setCharacterEncoding("UTF-8"); + request.setCharacterEncoding("UTF-8"); + PrintWriter out = response.getWriter(); + out.write(e.getMessage()); + } + return null; + } + + @RequestMapping(value={"/settings_dictionary/remove_settings"}, method={org.springframework.web.bind.annotation.RequestMethod.POST}) + public ModelAndView removeSettingsDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception { + try{ + ObjectMapper mapper = new ObjectMapper(); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + JsonNode root = mapper.readTree(request.getReader()); + DecisionSettings decisionSettings = (DecisionSettings)mapper.readValue(root.get("data").toString(), DecisionSettings.class); + commonClassDao.delete(decisionSettings); response.setCharacterEncoding("UTF-8"); response.setContentType("application / json"); request.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); - String responseString = ""; - if(duplicateflag){ - responseString = "Duplicate"; - }else{ - responseString = mapper.writeValueAsString(this.decisionPolicyDao.getDecisionSettingsData()); - } - JSONObject j = new JSONObject("{settingsDictionaryDatas: " + responseString + "}"); + String responseString = mapper.writeValueAsString(commonClassDao.getData(DecisionSettings.class)); + JSONObject j = new JSONObject("{settingsDictionaryDatas: " + responseString + "}"); out.write(j.toString()); return null; } catch (Exception e){ + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e); response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); @@ -154,29 +240,167 @@ public class DecisionPolicyDictionaryController { } return null; } + + + + @RequestMapping(value={"/get_RainyDayDictionaryDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE) + public void getRainyDayDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response){ + try{ + Map model = new HashMap<>(); + ObjectMapper mapper = new ObjectMapper(); + model.put("rainyDayDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(RainyDayTreatments.class, "bbID"))); + JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); + JSONObject j = new JSONObject(msg); + response.getWriter().write(j.toString()); + } + catch (Exception e){ + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e); + } + } - @RequestMapping(value={"/settings_dictionary/remove_settings.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST}) - public ModelAndView removeSettingsDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception { + + @RequestMapping(value={"/get_RainyDayDictionaryData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE) + public void getRainyDayDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){ try{ + Map model = new HashMap<>(); + ObjectMapper mapper = new ObjectMapper(); + model.put("rainyDayDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class))); + JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); + JSONObject j = new JSONObject(msg); + response.addHeader("successMapKey", "success"); + response.addHeader("operation", "getDictionary"); + response.getWriter().write(j.toString()); + } + catch (Exception e){ + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e); + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + response.addHeader("error", "dictionaryDBQuery"); + } + } + + @RequestMapping(value={"/decision_dictionary/save_RainyDay"}, method={org.springframework.web.bind.annotation.RequestMethod.POST}) + public ModelAndView saveRainyDayDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{ + try { + boolean duplicateflag = false; + boolean isFakeUpdate = false; + boolean fromAPI = false; + if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) { + fromAPI = true; + } ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); - DecisionSettings decisionSettings = (DecisionSettings)mapper.readValue(root.get("data").toString(), DecisionSettings.class); - decisionPolicyDao.delete(decisionSettings); + RainyDayTreatments decisionRainyDay; + TreatmentValues treatmentsData = null; + if (fromAPI) { + decisionRainyDay = (RainyDayTreatments)mapper.readValue(root.get("dictionaryFields").toString(), RainyDayTreatments.class); + treatmentsData = (TreatmentValues)mapper.readValue(root.get("dictionaryFields").toString(), TreatmentValues.class); + //check if update operation or create, get id for data to be updated and update attributeData + if (request.getParameter("operation").equals("update")) { + List duplicateData = commonClassDao.checkDuplicateEntry(decisionRainyDay.getBbid()+":"+decisionRainyDay.getWorkstep(), "bbid:workstep", RainyDayTreatments.class); + int id = 0; + RainyDayTreatments data = (RainyDayTreatments) duplicateData.get(0); + id = data.getId(); + if(id==0){ + isFakeUpdate=true; + } else { + decisionRainyDay.setId(id); + } + } + } else { + decisionRainyDay = (RainyDayTreatments)mapper.readValue(root.get("rainyDayDictionaryData").toString(), RainyDayTreatments.class); + treatmentsData = (TreatmentValues)mapper.readValue(root.get("rainyDayDictionaryData").toString(), TreatmentValues.class); + } + + String userValue = ""; + int counter = 0; + if(treatmentsData.getUserDataTypeValues().size() > 0){ + for(Object treatment : treatmentsData.getUserDataTypeValues()){ + if(treatment instanceof LinkedHashMap){ + String key = ((LinkedHashMap) treatment).get("treatment").toString(); + if(counter>0){ + userValue = userValue + ","; + } + userValue = userValue + key ; + counter ++; + } + } + } + decisionRainyDay.setTreatments(userValue); + + if(decisionRainyDay.getId() == 0){ + List duplicateData = commonClassDao.checkDuplicateEntry(decisionRainyDay.getBbid()+":"+decisionRainyDay.getWorkstep(), "bbid:workstep", RainyDayTreatments.class); + if(!duplicateData.isEmpty()){ + duplicateflag = true; + }else{ + commonClassDao.save(decisionRainyDay); + } + }else{ + if(!isFakeUpdate) { + commonClassDao.update(decisionRainyDay); + } + } + String responseString = ""; + if(duplicateflag){ + responseString = "Duplicate"; + }else{ + responseString = mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class)); + } + + if (fromAPI) { + if (responseString!=null && !responseString.equals("Duplicate")) { + if(isFakeUpdate){ + responseString = "Exists"; + } else { + responseString = "Success"; + } + } + ModelAndView result = new ModelAndView(); + result.setViewName(responseString); + return result; + } else { + response.setCharacterEncoding("UTF-8"); + response.setContentType("application / json"); + request.setCharacterEncoding("UTF-8"); + + PrintWriter out = response.getWriter(); + JSONObject j = new JSONObject("{rainyDayDictionaryDatas: " + responseString + "}"); + out.write(j.toString()); + return null; + } + + }catch (Exception e){ + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e); + response.setCharacterEncoding("UTF-8"); + request.setCharacterEncoding("UTF-8"); + PrintWriter out = response.getWriter(); + out.write(e.getMessage()); + } + return null; + } + + @RequestMapping(value={"/decision_dictionary/remove_rainyDay"}, method={org.springframework.web.bind.annotation.RequestMethod.POST}) + public ModelAndView removeRainyDayDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception { + try{ + ObjectMapper mapper = new ObjectMapper(); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + JsonNode root = mapper.readTree(request.getReader()); + RainyDayTreatments decisionRainyDay = (RainyDayTreatments)mapper.readValue(root.get("data").toString(), RainyDayTreatments.class); + commonClassDao.delete(decisionRainyDay); response.setCharacterEncoding("UTF-8"); response.setContentType("application / json"); request.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); - String responseString = mapper.writeValueAsString(this.decisionPolicyDao.getDecisionSettingsData()); - JSONObject j = new JSONObject("{settingsDictionaryDatas: " + responseString + "}"); + String responseString = mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class)); + JSONObject j = new JSONObject("{rainyDayDictionaryDatas: " + responseString + "}"); out.write(j.toString()); return null; } catch (Exception e){ - System.out.println(e); + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e); response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); @@ -186,3 +410,15 @@ public class DecisionPolicyDictionaryController { } } + +class TreatmentValues { + private ArrayList userDataTypeValues; + + public ArrayList getUserDataTypeValues() { + return userDataTypeValues; + } + + public void setUserDataTypeValues(ArrayList userDataTypeValues) { + this.userDataTypeValues = userDataTypeValues; + } +}