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.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
37 import org.onap.policy.rest.dao.CommonClassDao;
38 import org.onap.policy.rest.jpa.Attribute;
39 import org.onap.policy.rest.jpa.OnapName;
40 import org.onap.policy.rest.jpa.UserInfo;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.http.MediaType;
43 import org.springframework.stereotype.Controller;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RequestMethod;
46 import org.springframework.web.servlet.ModelAndView;
49 public class DictionaryController {
51 private static final Log LOGGER = LogFactory.getLog(DictionaryController.class);
53 private static CommonClassDao commonClassDao;
54 private static String xacmlId = "xacmlId";
55 private static String operation = "operation";
56 private static String dictionaryFields = "dictionaryFields";
57 private static String duplicateResponseString = "Duplicate";
58 private static String onapName = "onapName";
59 private static String attributeDatas = "attributeDictionaryDatas";
60 private static String onapNameDatas = "onapNameDictionaryDatas";
63 public DictionaryController(CommonClassDao commonClassDao) {
64 DictionaryController.commonClassDao = commonClassDao;
67 public DictionaryController() {
71 private DictionaryUtils getDictionaryUtilsInstance() {
72 return DictionaryUtils.getDictionaryUtils();
76 value = {"/get_AttributeDatabyAttributeName"},
77 method = {RequestMethod.GET},
78 produces = MediaType.APPLICATION_JSON_VALUE)
79 public void getAttributeDictionaryEntityDatabyAttributeName(HttpServletResponse response) {
80 DictionaryUtils utils = getDictionaryUtilsInstance();
81 utils.getDataByEntity(response, attributeDatas, xacmlId, Attribute.class);
84 // Attribute Dictionary
86 value = "/get_AttributeData",
87 method = RequestMethod.GET,
88 produces = MediaType.APPLICATION_JSON_VALUE)
89 public void getAttributeDictionaryEntityData(HttpServletResponse response) {
90 DictionaryUtils utils = getDictionaryUtilsInstance();
91 utils.getData(response, attributeDatas, Attribute.class);
94 @RequestMapping(value = {"/attribute_dictionary/save_attribute"}, method = {RequestMethod.POST})
95 public ModelAndView saveAttributeDictionary(HttpServletRequest request, HttpServletResponse response)
97 DictionaryUtils utils = getDictionaryUtilsInstance();
99 boolean fromAPI = utils.isRequestFromAPI(request);
100 ObjectMapper mapper = new ObjectMapper();
101 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
102 JsonNode root = mapper.readTree(request.getReader());
103 Attribute attributeData = null;
104 AttributeValues attributeValueData = null;
105 String userId = null;
107 attributeData = mapper.readValue(root.get(dictionaryFields).toString(), Attribute.class);
108 attributeValueData = mapper.readValue(root.get(dictionaryFields).toString(), AttributeValues.class);
111 attributeData = mapper.readValue(root.get("attributeDictionaryData").toString(), Attribute.class);
113 mapper.readValue(root.get("attributeDictionaryData").toString(), AttributeValues.class);
114 userId = root.get("userid").textValue();
116 UserInfo userInfo = utils.getUserInfo(userId);
117 List<Object> duplicateData =
118 commonClassDao.checkDuplicateEntry(attributeData.getXacmlId(), xacmlId, Attribute.class);
119 boolean duplicateflag = false;
120 if (!duplicateData.isEmpty()) {
121 Attribute data = (Attribute) duplicateData.get(0);
122 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
123 attributeData.setId(data.getId());
124 } else if ((request.getParameter(operation) != null
125 && !"update".equals(request.getParameter(operation)))
126 || (request.getParameter(operation) == null && (data.getId() != attributeData.getId()))) {
127 duplicateflag = true;
130 if (attributeValueData.getUserDataTypeValues() != null
131 && !attributeValueData.getUserDataTypeValues().isEmpty()) {
132 attributeData.setAttributeValue(
133 utils.appendKey(attributeValueData.getUserDataTypeValues(), "attributeValues", ","));
136 if (attributeData.getDatatypeBean().getShortName() != null) {
137 String datatype = attributeData.getDatatypeBean().getShortName();
138 attributeData.setDatatypeBean(utils.getDataType(datatype));
141 String responseString = null;
142 if (!duplicateflag) {
143 attributeData.setUserModifiedBy(userInfo);
144 if (attributeData.getId() == 0) {
145 attributeData.setCategoryBean(utils.getCategory());
146 attributeData.setUserCreatedBy(userInfo);
147 commonClassDao.save(attributeData);
149 attributeData.setModifiedDate(new Date());
150 commonClassDao.update(attributeData);
152 responseString = mapper.writeValueAsString(commonClassDao.getData(Attribute.class));
154 responseString = duplicateResponseString;
157 return utils.getResultForApi(responseString);
159 utils.setResponseData(response, attributeDatas, responseString);
161 } catch (Exception e) {
162 utils.setErrorResponseData(response, e);
167 @RequestMapping(value = {"/attribute_dictionary/remove_attribute"}, method = {RequestMethod.POST})
168 public void removeAttributeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
169 DictionaryUtils utils = getDictionaryUtilsInstance();
170 utils.removeData(request, response, attributeDatas, Attribute.class);
173 // OnapName Dictionary
175 value = {"/get_OnapNameDataByName"},
176 method = {RequestMethod.GET},
177 produces = MediaType.APPLICATION_JSON_VALUE)
178 public void getOnapNameDictionaryByNameEntityData(HttpServletResponse response) {
179 LOGGER.info("get_OnapNameDataByName is called");
180 DictionaryUtils utils = getDictionaryUtilsInstance();
181 utils.getDataByEntity(response, onapNameDatas, onapName, OnapName.class);
185 value = {"/get_OnapNameData"},
186 method = {RequestMethod.GET},
187 produces = MediaType.APPLICATION_JSON_VALUE)
188 public void getOnapNameDictionaryEntityData(HttpServletResponse response) {
189 DictionaryUtils utils = getDictionaryUtilsInstance();
190 utils.getData(response, onapNameDatas, OnapName.class);
193 @RequestMapping(value = {"/onap_dictionary/save_onapName"}, method = {RequestMethod.POST})
194 public ModelAndView saveOnapDictionary(HttpServletRequest request, HttpServletResponse response)
196 DictionaryUtils utils = getDictionaryUtilsInstance();
198 boolean fromAPI = utils.isRequestFromAPI(request);
199 ObjectMapper mapper = new ObjectMapper();
200 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
201 JsonNode root = mapper.readTree(request.getReader());
203 String userId = null;
205 onapData = mapper.readValue(root.get(dictionaryFields).toString(), OnapName.class);
208 onapData = mapper.readValue(root.get("onapNameDictionaryData").toString(), OnapName.class);
209 userId = root.get("userid").textValue();
211 UserInfo userInfo = utils.getUserInfo(userId);
213 List<Object> duplicateData =
214 commonClassDao.checkDuplicateEntry(onapData.getOnapName(), onapName, OnapName.class);
215 boolean duplicateflag = false;
216 if (!duplicateData.isEmpty()) {
217 OnapName data = (OnapName) duplicateData.get(0);
218 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
219 onapData.setId(data.getId());
220 } else if ((request.getParameter(operation) != null
221 && !"update".equals(request.getParameter(operation)))
222 || (request.getParameter(operation) == null && (data.getId() != onapData.getId()))) {
223 duplicateflag = true;
226 String responseString = null;
227 if (!duplicateflag) {
228 onapData.setUserModifiedBy(userInfo);
229 if (onapData.getId() == 0) {
230 onapData.setUserCreatedBy(userInfo);
231 commonClassDao.save(onapData);
233 onapData.setModifiedDate(new Date());
234 commonClassDao.update(onapData);
236 responseString = mapper.writeValueAsString(commonClassDao.getData(OnapName.class));
238 responseString = duplicateResponseString;
241 return utils.getResultForApi(responseString);
243 utils.setResponseData(response, onapNameDatas, responseString);
245 } catch (Exception e) {
246 utils.setErrorResponseData(response, e);
251 @RequestMapping(value = {"/onap_dictionary/remove_onap"}, method = {RequestMethod.POST})
252 public void removeOnapDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
253 DictionaryUtils utils = getDictionaryUtilsInstance();
254 utils.removeData(request, response, onapNameDatas, OnapName.class);
259 class AttributeValues {
260 private List<Object> userDataTypeValues;
262 public List<Object> getUserDataTypeValues() {
263 return userDataTypeValues;
266 public void setUserDataTypeValues(List<Object> userDataTypeValues) {
267 this.userDataTypeValues = userDataTypeValues;