Merge "Documentation: ControllerLogging, MDC Filters"
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / controller / SafePolicyController.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 import java.io.IOException;
27 import java.util.Date;
28 import java.util.List;
29 import javax.servlet.http.HttpServletRequest;
30 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.RiskType;
34 import org.onap.policy.rest.jpa.SafePolicyWarning;
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 @Controller
44 public class SafePolicyController {
45
46     private static CommonClassDao commonClassDao;
47     private static String duplicateResponseString = "Duplicate";
48     private static String operation = "operation";
49     private static String riskTypeDatas = "riskTypeDictionaryDatas";
50     private static String safePolicyWarningDatas = "safePolicyWarningDatas";
51
52     @Autowired
53     public SafePolicyController(CommonClassDao commonClassDao) {
54         SafePolicyController.commonClassDao = commonClassDao;
55     }
56
57     public void setCommonClassDao(CommonClassDao commonClassDao) {
58         SafePolicyController.commonClassDao = commonClassDao;
59     }
60
61     public SafePolicyController() {
62         super();
63     }
64
65     private DictionaryUtils getDictionaryUtilsInstance() {
66         return DictionaryUtils.getDictionaryUtils();
67     }
68
69     @RequestMapping(value = {"/get_RiskTypeDataByName"}, method = {RequestMethod.GET},
70             produces = MediaType.APPLICATION_JSON_VALUE)
71     public void getRiskTypeDictionaryByNameEntityData(HttpServletResponse response) {
72         DictionaryUtils utils = getDictionaryUtilsInstance();
73         utils.getDataByEntity(response, riskTypeDatas, "name", RiskType.class);
74     }
75
76     @RequestMapping(value = {"/get_RiskTypeData"}, method = {RequestMethod.GET},
77             produces = MediaType.APPLICATION_JSON_VALUE)
78     public void getRiskTypeDictionaryEntityData(HttpServletResponse response) {
79         DictionaryUtils utils = getDictionaryUtilsInstance();
80         utils.getData(response, riskTypeDatas, RiskType.class);
81     }
82
83     @RequestMapping(value = {"/sp_dictionary/save_riskType"}, method = {RequestMethod.POST})
84     public ModelAndView saveRiskTypeDictionary(HttpServletRequest request,
85             HttpServletResponse response) throws IOException {
86         DictionaryUtils utils = getDictionaryUtilsInstance();
87         try {
88             boolean fromAPI = utils.isRequestFromAPI(request);
89             ObjectMapper mapper = new ObjectMapper();
90             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
91             JsonNode root = mapper.readTree(request.getReader());
92             RiskType riskTypeData;
93             String userId = null;
94             if (fromAPI) {
95                 riskTypeData =
96                         mapper.readValue(root.get("dictionaryFields").toString(), RiskType.class);
97                 userId = "API";
98             } else {
99                 riskTypeData = mapper.readValue(root.get("riskTypeDictionaryData").toString(),
100                         RiskType.class);
101                 userId = root.get("userid").textValue();
102             }
103             UserInfo userInfo = utils.getUserInfo(userId);
104             List<Object> duplicateData = commonClassDao
105                     .checkDuplicateEntry(riskTypeData.getRiskName(), "name", RiskType.class);
106             boolean duplicateflag = false;
107             if (!duplicateData.isEmpty()) {
108                 RiskType data = (RiskType) duplicateData.get(0);
109                 if (request.getParameter(operation) != null
110                         && "update".equals(request.getParameter(operation))) {
111                     riskTypeData.setId(data.getId());
112                 } else if ((request.getParameter(operation) != null
113                         && !"update".equals(request.getParameter(operation)))
114                         || (request.getParameter(operation) == null
115                                 && (data.getId() != riskTypeData.getId()))) {
116                     duplicateflag = true;
117                 }
118             }
119             String responseString = null;
120             if (!duplicateflag) {
121                 riskTypeData.setUserModifiedBy(userInfo);
122                 if (riskTypeData.getId() == 0) {
123                     riskTypeData.setUserCreatedBy(userInfo);
124                     commonClassDao.save(riskTypeData);
125                 } else {
126                     riskTypeData.setModifiedDate(new Date());
127                     commonClassDao.update(riskTypeData);
128                 }
129                 responseString = mapper.writeValueAsString(commonClassDao.getData(RiskType.class));
130             } else {
131                 responseString = duplicateResponseString;
132             }
133             if (fromAPI) {
134                 return utils.getResultForApi(responseString);
135             } else {
136                 utils.setResponseData(response, riskTypeDatas, responseString);
137             }
138         } catch (Exception e) {
139             utils.setErrorResponseData(response, e);
140         }
141         return null;
142     }
143
144     @RequestMapping(value = {"/sp_dictionary/remove_riskType"}, method = {RequestMethod.POST})
145     public void removeRiskTypeDictionary(HttpServletRequest request, HttpServletResponse response)
146             throws IOException {
147         DictionaryUtils utils = getDictionaryUtilsInstance();
148         utils.removeData(request, response, riskTypeDatas, RiskType.class);
149     }
150
151     @RequestMapping(value = {"/get_SafePolicyWarningDataByName"}, method = {RequestMethod.GET},
152             produces = MediaType.APPLICATION_JSON_VALUE)
153     public void getSafePolicyWarningEntityDataByName(HttpServletResponse response) {
154         DictionaryUtils utils = getDictionaryUtilsInstance();
155         utils.getDataByEntity(response, safePolicyWarningDatas, "name", SafePolicyWarning.class);
156     }
157
158     @RequestMapping(value = {"/get_SafePolicyWarningData"}, method = {RequestMethod.GET},
159             produces = MediaType.APPLICATION_JSON_VALUE)
160     public void getSafePolicyWarningeEntityData(HttpServletResponse response) {
161         DictionaryUtils utils = getDictionaryUtilsInstance();
162         utils.getData(response, safePolicyWarningDatas, SafePolicyWarning.class);
163     }
164
165     @RequestMapping(value = {"/sp_dictionary/save_safePolicyWarning"},
166             method = {RequestMethod.POST})
167     public ModelAndView saveSafePolicyWarningDictionary(HttpServletRequest request,
168             HttpServletResponse response) throws IOException {
169         DictionaryUtils utils = getDictionaryUtilsInstance();
170         try {
171             boolean fromAPI = utils.isRequestFromAPI(request);
172             ObjectMapper mapper = new ObjectMapper();
173             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
174             JsonNode root = mapper.readTree(request.getReader());
175             SafePolicyWarning safePolicyWarning;
176             if (fromAPI) {
177                 safePolicyWarning = mapper.readValue(root.get("dictionaryFields").toString(),
178                         SafePolicyWarning.class);
179             } else {
180                 safePolicyWarning = mapper.readValue(root.get("safePolicyWarningData").toString(),
181                         SafePolicyWarning.class);
182             }
183
184             List<Object> duplicateData = commonClassDao.checkDuplicateEntry(
185                     safePolicyWarning.getName(), "name", SafePolicyWarning.class);
186             boolean duplicateflag = false;
187             if (!duplicateData.isEmpty()) {
188                 SafePolicyWarning data = (SafePolicyWarning) duplicateData.get(0);
189                 if (request.getParameter(operation) != null
190                         && "update".equals(request.getParameter(operation))) {
191                     safePolicyWarning.setId(data.getId());
192                 } else if ((request.getParameter(operation) != null
193                         && !"update".equals(request.getParameter(operation)))
194                         || (request.getParameter(operation) == null
195                                 && (data.getId() != safePolicyWarning.getId()))) {
196                     duplicateflag = true;
197                 }
198             }
199             String responseString = null;
200             if (!duplicateflag) {
201                 if (safePolicyWarning.getId() == 0) {
202                     commonClassDao.save(safePolicyWarning);
203                 } else {
204                     commonClassDao.update(safePolicyWarning);
205                 }
206                 responseString =
207                         mapper.writeValueAsString(commonClassDao.getData(SafePolicyWarning.class));
208             } else {
209                 responseString = duplicateResponseString;
210             }
211             if (fromAPI) {
212                 return utils.getResultForApi(responseString);
213             } else {
214                 utils.setResponseData(response, safePolicyWarningDatas, responseString);
215             }
216         } catch (Exception e) {
217             utils.setErrorResponseData(response, e);
218         }
219         return null;
220     }
221
222     @RequestMapping(value = {"/sp_dictionary/remove_SafePolicyWarning"},
223             method = {RequestMethod.POST})
224     public void removeSafePolicyWarningDictionary(HttpServletRequest request,
225             HttpServletResponse response) throws IOException {
226         DictionaryUtils utils = getDictionaryUtilsInstance();
227         utils.removeData(request, response, safePolicyWarningDatas, SafePolicyWarning.class);
228     }
229
230 }