2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.xacml.rest.controller;
23 import java.io.IOException;
24 import java.util.Date;
25 import java.util.List;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
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;
42 import com.fasterxml.jackson.databind.DeserializationFeature;
43 import com.fasterxml.jackson.databind.JsonNode;
44 import com.fasterxml.jackson.databind.ObjectMapper;
47 public class SafePolicyController {
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";
56 public SafePolicyController(CommonClassDao commonClassDao){
57 SafePolicyController.commonClassDao = commonClassDao;
60 public void setCommonClassDao(CommonClassDao commonClassDao){
61 SafePolicyController.commonClassDao = commonClassDao;
64 public SafePolicyController(){
68 private DictionaryUtils getDictionaryUtilsInstance(){
69 return DictionaryUtils.getDictionaryUtils();
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);
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);
84 @RequestMapping(value = { "/sp_dictionary/save_riskType" }, method = {RequestMethod.POST })
85 public ModelAndView saveRiskTypeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
86 DictionaryUtils utils = getDictionaryUtilsInstance();
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;
95 riskTypeData = mapper.readValue(root.get("dictionaryFields").toString(), RiskType.class);
98 riskTypeData = mapper.readValue(root.get("riskTypeDictionaryData").toString(), RiskType.class);
99 userId = root.get("userid").textValue();
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;
113 String responseString = null;
115 riskTypeData.setUserModifiedBy(userInfo);
116 if(riskTypeData.getId() == 0){
117 riskTypeData.setUserCreatedBy(userInfo);
118 commonClassDao.save(riskTypeData);
120 riskTypeData.setModifiedDate(new Date());
121 commonClassDao.update(riskTypeData);
123 responseString = mapper.writeValueAsString(commonClassDao.getData(RiskType.class));
125 responseString = duplicateResponseString;
128 return utils.getResultForApi(responseString);
130 utils.setResponseData(response, riskTypeDatas, responseString);
132 }catch (Exception e) {
133 utils.setErrorResponseData(response, e);
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);
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);
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);
156 @RequestMapping(value = { "/sp_dictionary/save_safePolicyWarning" }, method = {RequestMethod.POST })
157 public ModelAndView saveSafePolicyWarningDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
158 DictionaryUtils utils = getDictionaryUtilsInstance();
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;
166 safePolicyWarning = mapper.readValue(root.get("dictionaryFields").toString(), SafePolicyWarning.class);
168 safePolicyWarning = mapper.readValue(root.get("safePolicyWarningData").toString(), SafePolicyWarning.class);
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;
182 String responseString = null;
184 if(safePolicyWarning.getId() == 0){
185 commonClassDao.save(safePolicyWarning);
187 commonClassDao.update(safePolicyWarning);
189 responseString = mapper.writeValueAsString(commonClassDao.getData(SafePolicyWarning.class));
191 responseString = duplicateResponseString;
194 return utils.getResultForApi(responseString);
196 utils.setResponseData(response, safePolicyWarningDatas, responseString);
198 }catch (Exception e) {
199 utils.setErrorResponseData(response, e);
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);