2 * ============LICENSE_START=======================================================
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
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 com.fasterxml.jackson.databind.DeserializationFeature;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
27 import java.io.IOException;
28 import java.util.Date;
29 import java.util.List;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
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.ActionPolicyDict;
37 import org.onap.policy.rest.jpa.UserInfo;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.http.MediaType;
40 import org.springframework.stereotype.Controller;
41 import org.springframework.web.bind.annotation.RequestMapping;
42 import org.springframework.web.bind.annotation.RequestMethod;
43 import org.springframework.web.servlet.ModelAndView;
46 public class ActionPolicyDictionaryController {
48 private static CommonClassDao commonClassDao;
49 private static String operation = "operation";
50 private static String attributeName = "attributeName";
51 private static String actionDatas = "actionPolicyDictionaryDatas";
54 public ActionPolicyDictionaryController(CommonClassDao commonClassDao) {
55 ActionPolicyDictionaryController.commonClassDao = commonClassDao;
58 public void setCommonClassDao(CommonClassDao commonClassDao) {
59 ActionPolicyDictionaryController.commonClassDao = commonClassDao;
62 public ActionPolicyDictionaryController() {
66 private DictionaryUtils getDictionaryUtilsInstance() {
67 return DictionaryUtils.getDictionaryUtils();
71 value = {"/get_ActionPolicyDictDataByName"},
72 method = {RequestMethod.GET},
73 produces = MediaType.APPLICATION_JSON_VALUE)
74 public void getActionEntitybyName(HttpServletResponse response) {
75 DictionaryUtils utils = getDictionaryUtilsInstance();
76 utils.getDataByEntity(response, actionDatas, attributeName, ActionPolicyDict.class);
80 value = {"/get_ActionPolicyDictData"},
81 method = {RequestMethod.GET},
82 produces = MediaType.APPLICATION_JSON_VALUE)
83 public void getActionPolicyDictionaryEntityData(HttpServletResponse response) {
84 DictionaryUtils utils = getDictionaryUtilsInstance();
85 utils.getData(response, actionDatas, ActionPolicyDict.class);
88 @RequestMapping(value = {"/action_dictionary/save_ActionDict"}, method = {RequestMethod.POST})
89 public ModelAndView saveActionPolicyDictionary(HttpServletRequest request, HttpServletResponse response)
91 DictionaryUtils utils = getDictionaryUtilsInstance();
93 boolean fromAPI = utils.isRequestFromAPI(request);
94 ObjectMapper mapper = new ObjectMapper();
95 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
96 JsonNode root = mapper.readTree(request.getReader());
97 ActionPolicyDict actionPolicyDict = null;
98 ActionAdapter adapter = null;
101 actionPolicyDict = mapper.readValue(root.get("dictionaryFields").toString(), ActionPolicyDict.class);
102 adapter = mapper.readValue(root.get("dictionaryFields").toString(), ActionAdapter.class);
106 mapper.readValue(root.get("actionPolicyDictionaryData").toString(), ActionPolicyDict.class);
107 adapter = mapper.readValue(root.get("actionPolicyDictionaryData").toString(), ActionAdapter.class);
108 userId = root.get("userid").textValue();
110 UserInfo userInfo = utils.getUserInfo(userId);
112 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(actionPolicyDict.getAttributeName(),
113 attributeName, ActionPolicyDict.class);
114 boolean duplicateflag = false;
115 if (!duplicateData.isEmpty()) {
116 ActionPolicyDict data = (ActionPolicyDict) duplicateData.get(0);
117 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
118 actionPolicyDict.setId(data.getId());
119 } else if ((request.getParameter(operation) != null
120 && !"update".equals(request.getParameter(operation)))
121 || (request.getParameter(operation) == null && (data.getId() != actionPolicyDict.getId()))) {
122 duplicateflag = true;
125 actionPolicyDict.setHeader(utils.appendKeyValue(adapter.getHeaders(), ":", "="));
127 String responseString = null;
128 if (!duplicateflag) {
129 actionPolicyDict.setUserModifiedBy(userInfo);
130 if (actionPolicyDict.getId() == 0) {
131 actionPolicyDict.setUserCreatedBy(userInfo);
132 commonClassDao.save(actionPolicyDict);
134 actionPolicyDict.setModifiedDate(new Date());
135 commonClassDao.update(actionPolicyDict);
137 responseString = mapper.writeValueAsString(commonClassDao.getData(ActionPolicyDict.class));
139 responseString = "Duplicate";
142 return utils.getResultForApi(responseString);
144 utils.setResponseData(response, actionDatas, responseString);
146 } catch (Exception e) {
147 utils.setErrorResponseData(response, e);
152 @RequestMapping(value = {"/action_dictionary/remove_actionPolicyDict"}, method = {RequestMethod.POST})
153 public void removeActionPolicyDictionary(HttpServletRequest request, HttpServletResponse response)
155 DictionaryUtils utils = getDictionaryUtilsInstance();
156 utils.removeData(request, response, actionDatas, ActionPolicyDict.class);
161 class ActionAdapter {
162 private List<Object> headers;
164 public List<Object> getHeaders() {
168 public void setHeaders(List<Object> headers) {
169 this.headers = headers;