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