2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.List;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
31 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
32 import org.onap.policy.rest.dao.CommonClassDao;
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.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.bind.annotation.RequestMethod;
41 import org.springframework.web.servlet.ModelAndView;
43 import com.fasterxml.jackson.databind.DeserializationFeature;
44 import com.fasterxml.jackson.databind.JsonNode;
45 import com.fasterxml.jackson.databind.ObjectMapper;
48 public class DecisionPolicyDictionaryController {
50 private static CommonClassDao commonClassDao;
51 private static String xacmlId = "xacmlId";
52 private static String bbID = "bbid";
53 private static String operation = "operation";
54 private static String duplicateResponseString = "Duplicate";
55 private static String settingDatas = "settingsDictionaryDatas";
56 private static String rainDayDatas = "rainyDayDictionaryDatas";
57 private static String dictionaryFields ="dictionaryFields";
60 public DecisionPolicyDictionaryController(CommonClassDao commonClassDao){
61 DecisionPolicyDictionaryController.commonClassDao = commonClassDao;
64 public DecisionPolicyDictionaryController(){
68 private DictionaryUtils getDictionaryUtilsInstance(){
69 return DictionaryUtils.getDictionaryUtils();
72 @RequestMapping(value={"/get_SettingsDictionaryDataByName"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
73 public void getSettingsDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response){
74 DictionaryUtils utils = getDictionaryUtilsInstance();
75 utils.getDataByEntity(response, settingDatas, xacmlId, DecisionSettings.class);
79 @RequestMapping(value={"/get_SettingsDictionaryData"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
80 public void getSettingsDictionaryEntityData(HttpServletResponse response){
81 DictionaryUtils utils = getDictionaryUtilsInstance();
82 utils.getData(response, settingDatas, DecisionSettings.class);
85 @RequestMapping(value={"/decision_dictionary/save_Settings"}, method={RequestMethod.POST})
86 public ModelAndView saveSettingsDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
87 DictionaryUtils utils = getDictionaryUtilsInstance();
89 boolean fromAPI = utils.isRequestFromAPI(request);
90 ObjectMapper mapper = new ObjectMapper();
91 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
92 JsonNode root = mapper.readTree(request.getReader());
93 DecisionSettings decisionSettings;
97 decisionSettings = mapper.readValue(root.get(dictionaryFields).toString(), DecisionSettings.class);
100 decisionSettings = mapper.readValue(root.get("settingsDictionaryData").toString(), DecisionSettings.class);
101 userId = root.get("userid").textValue();
103 UserInfo userInfo = utils.getUserInfo(userId);
104 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(decisionSettings.getXacmlId(), xacmlId, DecisionSettings.class);
105 boolean duplicateflag = false;
106 if(!duplicateData.isEmpty()){
107 DecisionSettings data = (DecisionSettings) duplicateData.get(0);
108 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
109 decisionSettings.setId(data.getId());
110 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) ||
111 (request.getParameter(operation) == null && (data.getId() != decisionSettings.getId()))){
112 duplicateflag = true;
115 if(decisionSettings.getDatatypeBean().getShortName() != null){
116 String datatype = decisionSettings.getDatatypeBean().getShortName();
117 decisionSettings.setDatatypeBean(utils.getDataType(datatype));
119 String responseString = null;
121 decisionSettings.setUserModifiedBy(userInfo);
122 if(decisionSettings.getId() == 0){
123 decisionSettings.setUserCreatedBy(userInfo);
124 commonClassDao.save(decisionSettings);
126 decisionSettings.setModifiedDate(new Date());
127 commonClassDao.update(decisionSettings);
129 responseString = mapper.writeValueAsString(commonClassDao.getData(DecisionSettings.class));
131 responseString = duplicateResponseString;
134 return utils.getResultForApi(responseString);
136 utils.setResponseData(response, settingDatas, responseString);
138 }catch (Exception e){
139 utils.setErrorResponseData(response, e);
144 @RequestMapping(value={"/settings_dictionary/remove_settings"}, method={RequestMethod.POST})
145 public void removeSettingsDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
146 DictionaryUtils utils = getDictionaryUtilsInstance();
147 utils.removeData(request, response, settingDatas, DecisionSettings.class);
150 @RequestMapping(value={"/get_RainyDayDictionaryDataByName"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
151 public void getRainyDayDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response){
152 DictionaryUtils utils = getDictionaryUtilsInstance();
153 utils.getDataByEntity(response, rainDayDatas, bbID, RainyDayTreatments.class);
156 @RequestMapping(value={"/get_RainyDayDictionaryData"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
157 public void getRainyDayDictionaryEntityData(HttpServletResponse response){
158 DictionaryUtils utils = getDictionaryUtilsInstance();
159 utils.getData(response, rainDayDatas, RainyDayTreatments.class);
162 @RequestMapping(value={"/decision_dictionary/save_RainyDay"}, method={RequestMethod.POST})
163 public ModelAndView saveRainyDayDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
164 DictionaryUtils utils = getDictionaryUtilsInstance();
166 boolean fromAPI = utils.isRequestFromAPI(request);
167 ObjectMapper mapper = new ObjectMapper();
168 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
169 JsonNode root = mapper.readTree(request.getReader());
170 RainyDayTreatments decisionRainyDay;
171 TreatmentValues treatmentsData = null;
173 decisionRainyDay = mapper.readValue(root.get(dictionaryFields).toString(), RainyDayTreatments.class);
174 treatmentsData = mapper.readValue(root.get(dictionaryFields).toString(), TreatmentValues.class);
176 decisionRainyDay = mapper.readValue(root.get("rainyDayDictionaryData").toString(), RainyDayTreatments.class);
177 treatmentsData = mapper.readValue(root.get("rainyDayDictionaryData").toString(), TreatmentValues.class);
179 decisionRainyDay.setTreatments(utils.appendKey(treatmentsData.getUserDataTypeValues(), "treatment", ","));
181 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(decisionRainyDay.getBbid()+":"+decisionRainyDay.getWorkstep(), "bbid:workstep", RainyDayTreatments.class);
182 boolean duplicateflag = false;
183 if(!duplicateData.isEmpty()){
184 RainyDayTreatments data = (RainyDayTreatments) duplicateData.get(0);
185 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
186 decisionRainyDay.setId(data.getId());
187 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) ||
188 (request.getParameter(operation) == null && (data.getId() != decisionRainyDay.getId()))){
189 duplicateflag = true;
192 String responseString = null;
194 if(decisionRainyDay.getId() == 0){
195 commonClassDao.save(decisionRainyDay);
197 commonClassDao.update(decisionRainyDay);
199 responseString = mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class));
201 responseString = duplicateResponseString;
204 return utils.getResultForApi(responseString);
206 utils.setResponseData(response, rainDayDatas, responseString);
208 }catch (Exception e){
209 utils.setErrorResponseData(response, e);
214 @RequestMapping(value={"/decision_dictionary/remove_rainyDay"}, method={RequestMethod.POST})
215 public void removeRainyDayDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
216 DictionaryUtils utils = getDictionaryUtilsInstance();
217 utils.removeData(request, response, rainDayDatas, RainyDayTreatments.class);
222 class TreatmentValues {
223 private List<Object> userDataTypeValues = new ArrayList<>();
225 public List<Object> getUserDataTypeValues() {
226 return userDataTypeValues;
229 public void setUserDataTypeValues(List<Object> userDataTypeValues) {
230 this.userDataTypeValues = userDataTypeValues;