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;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.Date;
30 import java.util.List;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
35 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
36 import org.onap.policy.rest.dao.CommonClassDao;
37 import org.onap.policy.rest.jpa.DecisionSettings;
38 import org.onap.policy.rest.jpa.RainyDayTreatments;
39 import org.onap.policy.rest.jpa.UserInfo;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.MediaType;
42 import org.springframework.stereotype.Controller;
43 import org.springframework.web.bind.annotation.RequestMapping;
44 import org.springframework.web.bind.annotation.RequestMethod;
45 import org.springframework.web.servlet.ModelAndView;
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();
73 value = {"/get_SettingsDictionaryDataByName"},
74 method = {RequestMethod.GET},
75 produces = MediaType.APPLICATION_JSON_VALUE)
76 public void getSettingsDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response) {
77 DictionaryUtils utils = getDictionaryUtilsInstance();
78 utils.getDataByEntity(response, settingDatas, xacmlId, DecisionSettings.class);
82 value = {"/get_SettingsDictionaryData"},
83 method = {RequestMethod.GET},
84 produces = MediaType.APPLICATION_JSON_VALUE)
85 public void getSettingsDictionaryEntityData(HttpServletResponse response) {
86 DictionaryUtils utils = getDictionaryUtilsInstance();
87 utils.getData(response, settingDatas, DecisionSettings.class);
90 @RequestMapping(value = {"/decision_dictionary/save_Settings"}, method = {RequestMethod.POST})
91 public ModelAndView saveSettingsDictionary(HttpServletRequest request, HttpServletResponse response)
93 DictionaryUtils utils = getDictionaryUtilsInstance();
95 boolean fromAPI = utils.isRequestFromAPI(request);
96 ObjectMapper mapper = new ObjectMapper();
97 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
98 JsonNode root = mapper.readTree(request.getReader());
99 DecisionSettings decisionSettings;
100 String userId = null;
103 decisionSettings = mapper.readValue(root.get(dictionaryFields).toString(), DecisionSettings.class);
107 mapper.readValue(root.get("settingsDictionaryData").toString(), DecisionSettings.class);
108 userId = root.get("userid").textValue();
110 UserInfo userInfo = utils.getUserInfo(userId);
111 List<Object> duplicateData =
112 commonClassDao.checkDuplicateEntry(decisionSettings.getXacmlId(), xacmlId, DecisionSettings.class);
113 boolean duplicateflag = false;
114 if (!duplicateData.isEmpty()) {
115 DecisionSettings data = (DecisionSettings) duplicateData.get(0);
116 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
117 decisionSettings.setId(data.getId());
118 } else if ((request.getParameter(operation) != null
119 && !"update".equals(request.getParameter(operation)))
120 || (request.getParameter(operation) == null && (data.getId() != decisionSettings.getId()))) {
121 duplicateflag = true;
124 if (decisionSettings.getDatatypeBean().getShortName() != null) {
125 String datatype = decisionSettings.getDatatypeBean().getShortName();
126 decisionSettings.setDatatypeBean(utils.getDataType(datatype));
128 String responseString = null;
129 if (!duplicateflag) {
130 decisionSettings.setUserModifiedBy(userInfo);
131 if (decisionSettings.getId() == 0) {
132 decisionSettings.setUserCreatedBy(userInfo);
133 commonClassDao.save(decisionSettings);
135 decisionSettings.setModifiedDate(new Date());
136 commonClassDao.update(decisionSettings);
138 responseString = mapper.writeValueAsString(commonClassDao.getData(DecisionSettings.class));
140 responseString = duplicateResponseString;
143 return utils.getResultForApi(responseString);
145 utils.setResponseData(response, settingDatas, responseString);
147 } catch (Exception e) {
148 utils.setErrorResponseData(response, e);
153 @RequestMapping(value = {"/settings_dictionary/remove_settings"}, method = {RequestMethod.POST})
154 public void removeSettingsDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
155 DictionaryUtils utils = getDictionaryUtilsInstance();
156 utils.removeData(request, response, settingDatas, DecisionSettings.class);
160 value = {"/get_RainyDayDictionaryDataByName"},
161 method = {RequestMethod.GET},
162 produces = MediaType.APPLICATION_JSON_VALUE)
163 public void getRainyDayDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response) {
164 DictionaryUtils utils = getDictionaryUtilsInstance();
165 utils.getDataByEntity(response, rainDayDatas, bbID, RainyDayTreatments.class);
169 value = {"/get_RainyDayDictionaryData"},
170 method = {RequestMethod.GET},
171 produces = MediaType.APPLICATION_JSON_VALUE)
172 public void getRainyDayDictionaryEntityData(HttpServletResponse response) {
173 DictionaryUtils utils = getDictionaryUtilsInstance();
174 utils.getData(response, rainDayDatas, RainyDayTreatments.class);
177 @RequestMapping(value = {"/decision_dictionary/save_RainyDay"}, method = {RequestMethod.POST})
178 public ModelAndView saveRainyDayDictionary(HttpServletRequest request, HttpServletResponse response)
180 DictionaryUtils utils = getDictionaryUtilsInstance();
182 boolean fromAPI = utils.isRequestFromAPI(request);
183 ObjectMapper mapper = new ObjectMapper();
184 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
185 JsonNode root = mapper.readTree(request.getReader());
186 RainyDayTreatments decisionRainyDay;
187 TreatmentValues treatmentsData = null;
189 decisionRainyDay = mapper.readValue(root.get(dictionaryFields).toString(), RainyDayTreatments.class);
190 treatmentsData = mapper.readValue(root.get(dictionaryFields).toString(), TreatmentValues.class);
193 mapper.readValue(root.get("rainyDayDictionaryData").toString(), RainyDayTreatments.class);
194 treatmentsData = mapper.readValue(root.get("rainyDayDictionaryData").toString(), TreatmentValues.class);
196 decisionRainyDay.setTreatments(utils.appendKey(treatmentsData.getUserDataTypeValues(), "treatment", ","));
198 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(
199 decisionRainyDay.getBbid() + ":" + decisionRainyDay.getWorkstep(), "bbid:workstep",
200 RainyDayTreatments.class);
201 boolean duplicateflag = false;
202 if (!duplicateData.isEmpty()) {
203 RainyDayTreatments data = (RainyDayTreatments) duplicateData.get(0);
204 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
205 decisionRainyDay.setId(data.getId());
206 } else if ((request.getParameter(operation) != null
207 && !"update".equals(request.getParameter(operation)))
208 || (request.getParameter(operation) == null && (data.getId() != decisionRainyDay.getId()))) {
209 duplicateflag = true;
212 String responseString = null;
213 if (!duplicateflag) {
214 if (decisionRainyDay.getId() == 0) {
215 commonClassDao.save(decisionRainyDay);
217 commonClassDao.update(decisionRainyDay);
219 responseString = mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class));
221 responseString = duplicateResponseString;
224 return utils.getResultForApi(responseString);
226 utils.setResponseData(response, rainDayDatas, responseString);
228 } catch (Exception e) {
229 utils.setErrorResponseData(response, e);
234 @RequestMapping(value = {"/decision_dictionary/remove_rainyDay"}, method = {RequestMethod.POST})
235 public void removeRainyDayDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
236 DictionaryUtils utils = getDictionaryUtilsInstance();
237 utils.removeData(request, response, rainDayDatas, RainyDayTreatments.class);
243 class TreatmentValues {
244 private List<Object> userDataTypeValues = new ArrayList<>();
246 public List<Object> getUserDataTypeValues() {
247 return userDataTypeValues;
250 public void setUserDataTypeValues(List<Object> userDataTypeValues) {
251 this.userDataTypeValues = userDataTypeValues;