Merge "JUnit additions for ONAP-PAP-REST"
[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-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.Date;
25 import java.util.List;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
31 import org.onap.policy.rest.dao.CommonClassDao;
32 import org.onap.policy.rest.jpa.RiskType;
33 import org.onap.policy.rest.jpa.SafePolicyWarning;
34 import org.onap.policy.rest.jpa.UserInfo;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.http.MediaType;
37 import org.springframework.stereotype.Controller;
38 import org.springframework.web.bind.annotation.RequestMapping;
39 import org.springframework.web.bind.annotation.RequestMethod;
40 import org.springframework.web.servlet.ModelAndView;
41
42 import com.fasterxml.jackson.databind.DeserializationFeature;
43 import com.fasterxml.jackson.databind.JsonNode;
44 import com.fasterxml.jackson.databind.ObjectMapper;
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.dictionaryUtils != null ? DictionaryUtils.getDictionaryUtils() : new DictionaryUtils();
70         }
71         
72         @RequestMapping(value = { "/get_RiskTypeDataByName" }, method = {RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
73         public void getRiskTypeDictionaryByNameEntityData(HttpServletResponse response) {
74                 DictionaryUtils utils = getDictionaryUtilsInstance();
75                 utils.getDataByEntity(response, riskTypeDatas, "name", RiskType.class);
76         }
77
78         @RequestMapping(value = { "/get_RiskTypeData" }, method = {RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
79         public void getRiskTypeDictionaryEntityData(HttpServletResponse response) {
80                 DictionaryUtils utils = getDictionaryUtilsInstance();
81                 utils.getData(response, riskTypeDatas, RiskType.class);
82         }
83
84         @RequestMapping(value = { "/sp_dictionary/save_riskType" }, method = {RequestMethod.POST })
85         public ModelAndView saveRiskTypeDictionary(HttpServletRequest request, 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 = mapper.readValue(root.get("dictionaryFields").toString(), RiskType.class);
96                 userId = "API";
97             } else {
98                 riskTypeData = mapper.readValue(root.get("riskTypeDictionaryData").toString(), RiskType.class);
99                 userId = root.get("userid").textValue();
100             }
101             UserInfo userInfo = utils.getUserInfo(userId);
102             List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(riskTypeData.getRiskName(), "name", RiskType.class);
103                         boolean duplicateflag = false;
104             if(!duplicateData.isEmpty()){
105                 RiskType data = (RiskType) duplicateData.get(0);
106                                 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
107                                         riskTypeData.setId(data.getId());
108                                 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) || 
109                                                 (request.getParameter(operation) == null && (data.getId() != riskTypeData.getId()))){
110                                         duplicateflag = true;
111                                 }
112                         }
113             String responseString = null;
114                         if(!duplicateflag){
115                                 riskTypeData.setUserModifiedBy(userInfo);
116                                 if(riskTypeData.getId() == 0){
117                                         riskTypeData.setUserCreatedBy(userInfo);
118                                         commonClassDao.save(riskTypeData);
119                                 }else{
120                                         riskTypeData.setModifiedDate(new Date());
121                                         commonClassDao.update(riskTypeData); 
122                                 } 
123                                 responseString = mapper.writeValueAsString(commonClassDao.getData(RiskType.class));
124                         }else{
125                                 responseString = duplicateResponseString;
126                         }
127                         if(fromAPI){
128                                 return utils.getResultForApi(responseString);
129                         }else{
130                                 utils.setResponseData(response, riskTypeDatas, responseString);
131                         }
132         }catch (Exception e) {
133                 utils.setErrorResponseData(response, e);
134                 }
135                 return null;
136         }
137
138         @RequestMapping(value = { "/sp_dictionary/remove_riskType" }, method = {RequestMethod.POST })
139         public void removeRiskTypeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
140                 DictionaryUtils utils = getDictionaryUtilsInstance();
141                 utils.removeData(request, response, riskTypeDatas, RiskType.class);
142         }
143         
144         @RequestMapping(value = { "/get_SafePolicyWarningDataByName" }, method = {RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
145         public void getSafePolicyWarningEntityDataByName(HttpServletResponse response) {
146                 DictionaryUtils utils = getDictionaryUtilsInstance();
147                 utils.getDataByEntity(response, safePolicyWarningDatas, "name", SafePolicyWarning.class);
148         }
149
150         @RequestMapping(value = { "/get_SafePolicyWarningData" }, method = {RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
151         public void getSafePolicyWarningeEntityData(HttpServletResponse response) {
152                 DictionaryUtils utils = getDictionaryUtilsInstance();
153                 utils.getData(response, safePolicyWarningDatas, SafePolicyWarning.class);
154         }
155
156         @RequestMapping(value = { "/sp_dictionary/save_safePolicyWarning" }, method = {RequestMethod.POST })
157         public ModelAndView saveSafePolicyWarningDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
158                 DictionaryUtils utils = getDictionaryUtilsInstance();
159                 try {
160                         boolean fromAPI = utils.isRequestFromAPI(request);
161                         ObjectMapper mapper = new ObjectMapper();
162                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
163                         JsonNode root = mapper.readTree(request.getReader());
164                         SafePolicyWarning safePolicyWarning;
165             if (fromAPI) {
166                 safePolicyWarning = mapper.readValue(root.get("dictionaryFields").toString(), SafePolicyWarning.class);
167             } else {
168                 safePolicyWarning = mapper.readValue(root.get("safePolicyWarningData").toString(), SafePolicyWarning.class);
169             }
170
171             List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(safePolicyWarning.getName(), "name", SafePolicyWarning.class);
172                         boolean duplicateflag = false;
173                         if(!duplicateData.isEmpty()){
174                                 SafePolicyWarning data = (SafePolicyWarning) duplicateData.get(0);
175                                 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
176                                         safePolicyWarning.setId(data.getId());
177                                 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) || 
178                                                 (request.getParameter(operation) == null && (data.getId() != safePolicyWarning.getId()))){
179                                         duplicateflag = true;
180                                 }
181                         }
182                         String responseString = null;
183                         if(!duplicateflag){
184                                 if(safePolicyWarning.getId() == 0){
185                                         commonClassDao.save(safePolicyWarning);
186                                 }else{
187                                         commonClassDao.update(safePolicyWarning); 
188                                 } 
189                                 responseString = mapper.writeValueAsString(commonClassDao.getData(SafePolicyWarning.class));
190                         }else{
191                                 responseString = duplicateResponseString;
192                         }
193                         if(fromAPI){
194                                 return utils.getResultForApi(responseString);
195                         }else{
196                                 utils.setResponseData(response, safePolicyWarningDatas, responseString);
197                         }
198         }catch (Exception e) {
199                 utils.setErrorResponseData(response, e);
200                 }
201                 return null;
202         }
203
204         @RequestMapping(value = { "/sp_dictionary/remove_SafePolicyWarning" }, method = {RequestMethod.POST })
205         public void removeSafePolicyWarningDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
206                 DictionaryUtils utils = getDictionaryUtilsInstance();
207                 utils.removeData(request, response, safePolicyWarningDatas, SafePolicyWarning.class);
208         }
209
210 }