2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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 java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Date;
29 import java.util.List;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
33 import org.onap.policy.rest.dao.CommonClassDao;
34 import org.onap.policy.rest.jpa.DecisionSettings;
35 import org.onap.policy.rest.jpa.RainyDayTreatments;
36 import org.onap.policy.rest.jpa.UserInfo;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.http.MediaType;
39 import org.springframework.stereotype.Controller;
40 import org.springframework.web.bind.annotation.RequestMapping;
41 import org.springframework.web.bind.annotation.RequestMethod;
42 import org.springframework.web.servlet.ModelAndView;
45 public class DecisionPolicyDictionaryController {
47 private static CommonClassDao commonClassDao;
48 private static String xacmlId = "xacmlId";
49 private static String bbID = "bbid";
50 private static String operation = "operation";
51 private static String duplicateResponseString = "Duplicate";
52 private static String settingDatas = "settingsDictionaryDatas";
53 private static String rainDayDatas = "rainyDayDictionaryDatas";
54 private static String dictionaryFields = "dictionaryFields";
57 public DecisionPolicyDictionaryController(CommonClassDao commonClassDao) {
58 DecisionPolicyDictionaryController.commonClassDao = commonClassDao;
61 public DecisionPolicyDictionaryController() {
65 private DictionaryUtils getDictionaryUtilsInstance() {
66 return DictionaryUtils.getDictionaryUtils();
69 @RequestMapping(value = {"/get_SettingsDictionaryDataByName"}, method = {RequestMethod.GET},
70 produces = MediaType.APPLICATION_JSON_VALUE)
71 public void getSettingsDictionaryByNameEntityData(HttpServletRequest request,
72 HttpServletResponse response) {
73 DictionaryUtils utils = getDictionaryUtilsInstance();
74 utils.getDataByEntity(response, settingDatas, xacmlId, DecisionSettings.class);
78 @RequestMapping(value = {"/get_SettingsDictionaryData"}, method = {RequestMethod.GET},
79 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,
87 HttpServletResponse response) throws IOException {
88 DictionaryUtils utils = getDictionaryUtilsInstance();
90 boolean fromAPI = utils.isRequestFromAPI(request);
91 ObjectMapper mapper = new ObjectMapper();
92 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
93 JsonNode root = mapper.readTree(request.getReader());
94 DecisionSettings decisionSettings;
98 decisionSettings = mapper.readValue(root.get(dictionaryFields).toString(),
99 DecisionSettings.class);
102 decisionSettings = mapper.readValue(root.get("settingsDictionaryData").toString(),
103 DecisionSettings.class);
104 userId = root.get("userid").textValue();
106 UserInfo userInfo = utils.getUserInfo(userId);
107 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(
108 decisionSettings.getXacmlId(), xacmlId, DecisionSettings.class);
109 boolean duplicateflag = false;
110 if (!duplicateData.isEmpty()) {
111 DecisionSettings data = (DecisionSettings) duplicateData.get(0);
112 if (request.getParameter(operation) != null
113 && "update".equals(request.getParameter(operation))) {
114 decisionSettings.setId(data.getId());
115 } else if ((request.getParameter(operation) != null
116 && !"update".equals(request.getParameter(operation)))
117 || (request.getParameter(operation) == null
118 && (data.getId() != decisionSettings.getId()))) {
119 duplicateflag = true;
122 if (decisionSettings.getDatatypeBean().getShortName() != null) {
123 String datatype = decisionSettings.getDatatypeBean().getShortName();
124 decisionSettings.setDatatypeBean(utils.getDataType(datatype));
126 String responseString = null;
127 if (!duplicateflag) {
128 decisionSettings.setUserModifiedBy(userInfo);
129 if (decisionSettings.getId() == 0) {
130 decisionSettings.setUserCreatedBy(userInfo);
131 commonClassDao.save(decisionSettings);
133 decisionSettings.setModifiedDate(new Date());
134 commonClassDao.update(decisionSettings);
137 mapper.writeValueAsString(commonClassDao.getData(DecisionSettings.class));
139 responseString = duplicateResponseString;
142 return utils.getResultForApi(responseString);
144 utils.setResponseData(response, settingDatas, responseString);
146 } catch (Exception e) {
147 utils.setErrorResponseData(response, e);
152 @RequestMapping(value = {"/settings_dictionary/remove_settings"}, method = {RequestMethod.POST})
153 public void removeSettingsDictionary(HttpServletRequest request, HttpServletResponse response)
155 DictionaryUtils utils = getDictionaryUtilsInstance();
156 utils.removeData(request, response, settingDatas, DecisionSettings.class);
159 @RequestMapping(value = {"/get_RainyDayDictionaryDataByName"}, method = {RequestMethod.GET},
160 produces = MediaType.APPLICATION_JSON_VALUE)
161 public void getRainyDayDictionaryByNameEntityData(HttpServletRequest request,
162 HttpServletResponse response) {
163 DictionaryUtils utils = getDictionaryUtilsInstance();
164 utils.getDataByEntity(response, rainDayDatas, bbID, RainyDayTreatments.class);
167 @RequestMapping(value = {"/get_RainyDayDictionaryData"}, method = {RequestMethod.GET},
168 produces = MediaType.APPLICATION_JSON_VALUE)
169 public void getRainyDayDictionaryEntityData(HttpServletResponse response) {
170 DictionaryUtils utils = getDictionaryUtilsInstance();
171 utils.getData(response, rainDayDatas, RainyDayTreatments.class);
174 @RequestMapping(value = {"/decision_dictionary/save_RainyDay"}, method = {RequestMethod.POST})
175 public ModelAndView saveRainyDayDictionary(HttpServletRequest request,
176 HttpServletResponse response) throws IOException {
177 DictionaryUtils utils = getDictionaryUtilsInstance();
179 boolean fromAPI = utils.isRequestFromAPI(request);
180 ObjectMapper mapper = new ObjectMapper();
181 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
182 JsonNode root = mapper.readTree(request.getReader());
183 RainyDayTreatments decisionRainyDay;
184 TreatmentValues treatmentsData = null;
186 decisionRainyDay = mapper.readValue(root.get(dictionaryFields).toString(),
187 RainyDayTreatments.class);
188 treatmentsData = mapper.readValue(root.get(dictionaryFields).toString(),
189 TreatmentValues.class);
191 decisionRainyDay = mapper.readValue(root.get("rainyDayDictionaryData").toString(),
192 RainyDayTreatments.class);
193 treatmentsData = mapper.readValue(root.get("rainyDayDictionaryData").toString(),
194 TreatmentValues.class);
196 decisionRainyDay.setTreatments(
197 utils.appendKey(treatmentsData.getUserDataTypeValues(), "treatment", ","));
199 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(
200 decisionRainyDay.getBbid() + ":" + decisionRainyDay.getWorkstep(),
201 "bbid:workstep", RainyDayTreatments.class);
202 boolean duplicateflag = false;
203 if (!duplicateData.isEmpty()) {
204 RainyDayTreatments data = (RainyDayTreatments) duplicateData.get(0);
205 if (request.getParameter(operation) != null
206 && "update".equals(request.getParameter(operation))) {
207 decisionRainyDay.setId(data.getId());
208 } else if ((request.getParameter(operation) != null
209 && !"update".equals(request.getParameter(operation)))
210 || (request.getParameter(operation) == null
211 && (data.getId() != decisionRainyDay.getId()))) {
212 duplicateflag = true;
215 String responseString = null;
216 if (!duplicateflag) {
217 if (decisionRainyDay.getId() == 0) {
218 commonClassDao.save(decisionRainyDay);
220 commonClassDao.update(decisionRainyDay);
223 mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class));
225 responseString = duplicateResponseString;
228 return utils.getResultForApi(responseString);
230 utils.setResponseData(response, rainDayDatas, responseString);
232 } catch (Exception e) {
233 utils.setErrorResponseData(response, e);
238 @RequestMapping(value = {"/decision_dictionary/remove_rainyDay"}, method = {RequestMethod.POST})
239 public void removeRainyDayDictionary(HttpServletRequest request, HttpServletResponse response)
241 DictionaryUtils utils = getDictionaryUtilsInstance();
242 utils.removeData(request, response, rainDayDatas, RainyDayTreatments.class);
248 class TreatmentValues {
249 private List<Object> userDataTypeValues = new ArrayList<>();
251 public List<Object> getUserDataTypeValues() {
252 return userDataTypeValues;
255 public void setUserDataTypeValues(List<Object> userDataTypeValues) {
256 this.userDataTypeValues = userDataTypeValues;