738a808f6981845aa65364f7dd072449cf739710
[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-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
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 java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.List;
27
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
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;
42
43 import com.fasterxml.jackson.databind.DeserializationFeature;
44 import com.fasterxml.jackson.databind.JsonNode;
45 import com.fasterxml.jackson.databind.ObjectMapper;
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(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);
76     }
77
78
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);
83     }
84
85     @RequestMapping(value={"/decision_dictionary/save_Settings"}, method={RequestMethod.POST})
86     public ModelAndView saveSettingsDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
87         DictionaryUtils utils = getDictionaryUtilsInstance();
88         try {
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;
94             String userId = null;
95
96             if(fromAPI){
97                 decisionSettings = mapper.readValue(root.get(dictionaryFields).toString(), DecisionSettings.class);
98                 userId = "API";
99             }else{
100                 decisionSettings = mapper.readValue(root.get("settingsDictionaryData").toString(), DecisionSettings.class);
101                 userId = root.get("userid").textValue();
102             }
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;
113                 }
114             }
115             if(decisionSettings.getDatatypeBean().getShortName() != null){
116                 String datatype = decisionSettings.getDatatypeBean().getShortName();
117                 decisionSettings.setDatatypeBean(utils.getDataType(datatype));
118             }
119             String responseString = null;
120             if(!duplicateflag){
121                 decisionSettings.setUserModifiedBy(userInfo);
122                 if(decisionSettings.getId() == 0){
123                     decisionSettings.setUserCreatedBy(userInfo);
124                     commonClassDao.save(decisionSettings);
125                 }else{
126                     decisionSettings.setModifiedDate(new Date());
127                     commonClassDao.update(decisionSettings);
128                 }
129                 responseString = mapper.writeValueAsString(commonClassDao.getData(DecisionSettings.class));
130             }else{
131                 responseString = duplicateResponseString;
132             }
133             if(fromAPI){
134                 return utils.getResultForApi(responseString);
135             }else{
136                 utils.setResponseData(response, settingDatas, responseString);
137             }
138         }catch (Exception e){
139             utils.setErrorResponseData(response, e);
140         }
141         return null;
142     }
143
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);
148     }
149
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);
154     }
155
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);
160     }
161
162     @RequestMapping(value={"/decision_dictionary/save_RainyDay"}, method={RequestMethod.POST})
163     public ModelAndView saveRainyDayDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
164         DictionaryUtils utils = getDictionaryUtilsInstance();
165         try {
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;
172             if(fromAPI){
173                 decisionRainyDay = mapper.readValue(root.get(dictionaryFields).toString(), RainyDayTreatments.class);
174                 treatmentsData = mapper.readValue(root.get(dictionaryFields).toString(), TreatmentValues.class);
175             }else{
176                 decisionRainyDay = mapper.readValue(root.get("rainyDayDictionaryData").toString(), RainyDayTreatments.class);
177                 treatmentsData = mapper.readValue(root.get("rainyDayDictionaryData").toString(), TreatmentValues.class);
178             }
179             decisionRainyDay.setTreatments(utils.appendKey(treatmentsData.getUserDataTypeValues(), "treatment", ","));
180
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;
190                 }
191             }
192             String responseString = null;
193             if(!duplicateflag){
194                 if(decisionRainyDay.getId() == 0){
195                     commonClassDao.save(decisionRainyDay);
196                 }else{
197                     commonClassDao.update(decisionRainyDay);
198                 }
199                 responseString = mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class));
200             }else{
201                 responseString = duplicateResponseString;
202             }
203             if(fromAPI){
204                 return utils.getResultForApi(responseString);
205             }else{
206                 utils.setResponseData(response, rainDayDatas, responseString);
207             }
208         }catch (Exception e){
209             utils.setErrorResponseData(response, e);
210         }
211         return null;
212     }
213
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);
218     }
219
220 }
221
222 class TreatmentValues { 
223     private List<Object> userDataTypeValues = new ArrayList<>();
224
225     public List<Object> getUserDataTypeValues() {
226         return userDataTypeValues;
227     }
228
229     public void setUserDataTypeValues(List<Object> userDataTypeValues) {
230         this.userDataTypeValues = userDataTypeValues;
231     }
232 }