Update css file name in conf.py
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / controller / DecisionPolicyDictionaryController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest.controller;
22
23 import com.fasterxml.jackson.databind.DeserializationFeature;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.Date;
30 import java.util.List;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
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;
46
47 @Controller
48 public class DecisionPolicyDictionaryController {
49
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";
58
59     @Autowired
60     public DecisionPolicyDictionaryController(CommonClassDao commonClassDao) {
61         DecisionPolicyDictionaryController.commonClassDao = commonClassDao;
62     }
63
64     public DecisionPolicyDictionaryController() {
65         super();
66     }
67
68     private DictionaryUtils getDictionaryUtilsInstance() {
69         return DictionaryUtils.getDictionaryUtils();
70     }
71
72     @RequestMapping(
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);
79     }
80
81     @RequestMapping(
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);
88     }
89
90     @RequestMapping(value = {"/decision_dictionary/save_Settings"}, method = {RequestMethod.POST})
91     public ModelAndView saveSettingsDictionary(HttpServletRequest request, HttpServletResponse response)
92             throws IOException {
93         DictionaryUtils utils = getDictionaryUtilsInstance();
94         try {
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;
101
102             if (fromAPI) {
103                 decisionSettings = mapper.readValue(root.get(dictionaryFields).toString(), DecisionSettings.class);
104                 userId = "API";
105             } else {
106                 decisionSettings =
107                         mapper.readValue(root.get("settingsDictionaryData").toString(), DecisionSettings.class);
108                 userId = root.get("userid").textValue();
109             }
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;
122                 }
123             }
124             if (decisionSettings.getDatatypeBean().getShortName() != null) {
125                 String datatype = decisionSettings.getDatatypeBean().getShortName();
126                 decisionSettings.setDatatypeBean(utils.getDataType(datatype));
127             }
128             String responseString = null;
129             if (!duplicateflag) {
130                 decisionSettings.setUserModifiedBy(userInfo);
131                 if (decisionSettings.getId() == 0) {
132                     decisionSettings.setUserCreatedBy(userInfo);
133                     commonClassDao.save(decisionSettings);
134                 } else {
135                     decisionSettings.setModifiedDate(new Date());
136                     commonClassDao.update(decisionSettings);
137                 }
138                 responseString = mapper.writeValueAsString(commonClassDao.getData(DecisionSettings.class));
139             } else {
140                 responseString = duplicateResponseString;
141             }
142             if (fromAPI) {
143                 return utils.getResultForApi(responseString);
144             } else {
145                 utils.setResponseData(response, settingDatas, responseString);
146             }
147         } catch (Exception e) {
148             utils.setErrorResponseData(response, e);
149         }
150         return null;
151     }
152
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);
157     }
158
159     @RequestMapping(
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);
166     }
167
168     @RequestMapping(
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);
175     }
176
177     @RequestMapping(value = {"/decision_dictionary/save_RainyDay"}, method = {RequestMethod.POST})
178     public ModelAndView saveRainyDayDictionary(HttpServletRequest request, HttpServletResponse response)
179             throws IOException {
180         DictionaryUtils utils = getDictionaryUtilsInstance();
181         try {
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;
188             if (fromAPI) {
189                 decisionRainyDay = mapper.readValue(root.get(dictionaryFields).toString(), RainyDayTreatments.class);
190                 treatmentsData = mapper.readValue(root.get(dictionaryFields).toString(), TreatmentValues.class);
191             } else {
192                 decisionRainyDay =
193                         mapper.readValue(root.get("rainyDayDictionaryData").toString(), RainyDayTreatments.class);
194                 treatmentsData = mapper.readValue(root.get("rainyDayDictionaryData").toString(), TreatmentValues.class);
195             }
196             decisionRainyDay.setTreatments(utils.appendKey(treatmentsData.getUserDataTypeValues(), "treatment", ","));
197
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;
210                 }
211             }
212             String responseString = null;
213             if (!duplicateflag) {
214                 if (decisionRainyDay.getId() == 0) {
215                     commonClassDao.save(decisionRainyDay);
216                 } else {
217                     commonClassDao.update(decisionRainyDay);
218                 }
219                 responseString = mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class));
220             } else {
221                 responseString = duplicateResponseString;
222             }
223             if (fromAPI) {
224                 return utils.getResultForApi(responseString);
225             } else {
226                 utils.setResponseData(response, rainDayDatas, responseString);
227             }
228         } catch (Exception e) {
229             utils.setErrorResponseData(response, e);
230         }
231         return null;
232     }
233
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);
238     }
239
240 }
241
242
243 class TreatmentValues {
244     private List<Object> userDataTypeValues = new ArrayList<>();
245
246     public List<Object> getUserDataTypeValues() {
247         return userDataTypeValues;
248     }
249
250     public void setUserDataTypeValues(List<Object> userDataTypeValues) {
251         this.userDataTypeValues = userDataTypeValues;
252     }
253 }