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;
26 import java.io.IOException;
27 import java.util.Date;
28 import java.util.List;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
34 import org.onap.policy.rest.dao.CommonClassDao;
35 import org.onap.policy.rest.jpa.Attribute;
36 import org.onap.policy.rest.jpa.OnapName;
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 DictionaryController {
48 private static final Log LOGGER = LogFactory.getLog(DictionaryController.class);
50 private static CommonClassDao commonClassDao;
51 private static String xacmlId = "xacmlId";
52 private static String operation = "operation";
53 private static String dictionaryFields = "dictionaryFields";
54 private static String duplicateResponseString = "Duplicate";
55 private static String onapName = "onapName";
56 private static String attributeDatas = "attributeDictionaryDatas";
57 private static String onapNameDatas = "onapNameDictionaryDatas";
60 public DictionaryController(CommonClassDao commonClassDao) {
61 DictionaryController.commonClassDao = commonClassDao;
64 public DictionaryController() {
68 private DictionaryUtils getDictionaryUtilsInstance() {
69 return DictionaryUtils.getDictionaryUtils();
72 @RequestMapping(value = {"/get_AttributeDatabyAttributeName"}, method = {RequestMethod.GET},
73 produces = MediaType.APPLICATION_JSON_VALUE)
74 public void getAttributeDictionaryEntityDatabyAttributeName(HttpServletResponse response) {
75 DictionaryUtils utils = getDictionaryUtilsInstance();
76 utils.getDataByEntity(response, attributeDatas, xacmlId, Attribute.class);
79 // Attribute Dictionary
80 @RequestMapping(value = "/get_AttributeData", method = RequestMethod.GET,
81 produces = MediaType.APPLICATION_JSON_VALUE)
82 public void getAttributeDictionaryEntityData(HttpServletResponse response) {
83 DictionaryUtils utils = getDictionaryUtilsInstance();
84 utils.getData(response, attributeDatas, Attribute.class);
87 @RequestMapping(value = {"/attribute_dictionary/save_attribute"}, method = {RequestMethod.POST})
88 public ModelAndView saveAttributeDictionary(HttpServletRequest request,
89 HttpServletResponse response) throws IOException {
90 DictionaryUtils utils = getDictionaryUtilsInstance();
92 boolean fromAPI = utils.isRequestFromAPI(request);
93 ObjectMapper mapper = new ObjectMapper();
94 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
95 JsonNode root = mapper.readTree(request.getReader());
96 Attribute attributeData = null;
97 AttributeValues attributeValueData = null;
101 mapper.readValue(root.get(dictionaryFields).toString(), Attribute.class);
102 attributeValueData = mapper.readValue(root.get(dictionaryFields).toString(),
103 AttributeValues.class);
106 attributeData = mapper.readValue(root.get("attributeDictionaryData").toString(),
108 attributeValueData = mapper.readValue(
109 root.get("attributeDictionaryData").toString(), AttributeValues.class);
110 userId = root.get("userid").textValue();
112 UserInfo userInfo = utils.getUserInfo(userId);
113 List<Object> duplicateData = commonClassDao
114 .checkDuplicateEntry(attributeData.getXacmlId(), xacmlId, Attribute.class);
115 boolean duplicateflag = false;
116 if (!duplicateData.isEmpty()) {
117 Attribute data = (Attribute) duplicateData.get(0);
118 if (request.getParameter(operation) != null
119 && "update".equals(request.getParameter(operation))) {
120 attributeData.setId(data.getId());
121 } else if ((request.getParameter(operation) != null
122 && !"update".equals(request.getParameter(operation)))
123 || (request.getParameter(operation) == null
124 && (data.getId() != attributeData.getId()))) {
125 duplicateflag = true;
128 if (attributeValueData.getUserDataTypeValues() != null
129 && !attributeValueData.getUserDataTypeValues().isEmpty()) {
130 attributeData.setAttributeValue(utils.appendKey(
131 attributeValueData.getUserDataTypeValues(), "attributeValues", ","));
134 if (attributeData.getDatatypeBean().getShortName() != null) {
135 String datatype = attributeData.getDatatypeBean().getShortName();
136 attributeData.setDatatypeBean(utils.getDataType(datatype));
139 String responseString = null;
140 if (!duplicateflag) {
141 attributeData.setUserModifiedBy(userInfo);
142 if (attributeData.getId() == 0) {
143 attributeData.setCategoryBean(utils.getCategory());
144 attributeData.setUserCreatedBy(userInfo);
145 commonClassDao.save(attributeData);
147 attributeData.setModifiedDate(new Date());
148 commonClassDao.update(attributeData);
150 responseString = mapper.writeValueAsString(commonClassDao.getData(Attribute.class));
152 responseString = duplicateResponseString;
155 return utils.getResultForApi(responseString);
157 utils.setResponseData(response, attributeDatas, responseString);
159 } catch (Exception e) {
160 utils.setErrorResponseData(response, e);
165 @RequestMapping(value = {"/attribute_dictionary/remove_attribute"},
166 method = {RequestMethod.POST})
167 public void removeAttributeDictionary(HttpServletRequest request, HttpServletResponse response)
169 DictionaryUtils utils = getDictionaryUtilsInstance();
170 utils.removeData(request, response, attributeDatas, Attribute.class);
173 // OnapName Dictionary
174 @RequestMapping(value = {"/get_OnapNameDataByName"}, method = {RequestMethod.GET},
175 produces = MediaType.APPLICATION_JSON_VALUE)
176 public void getOnapNameDictionaryByNameEntityData(HttpServletResponse response) {
177 LOGGER.info("get_OnapNameDataByName is called");
178 DictionaryUtils utils = getDictionaryUtilsInstance();
179 utils.getDataByEntity(response, onapNameDatas, onapName, OnapName.class);
182 @RequestMapping(value = {"/get_OnapNameData"}, method = {RequestMethod.GET},
183 produces = MediaType.APPLICATION_JSON_VALUE)
184 public void getOnapNameDictionaryEntityData(HttpServletResponse response) {
185 DictionaryUtils utils = getDictionaryUtilsInstance();
186 utils.getData(response, onapNameDatas, OnapName.class);
189 @RequestMapping(value = {"/onap_dictionary/save_onapName"}, method = {RequestMethod.POST})
190 public ModelAndView saveOnapDictionary(HttpServletRequest request, HttpServletResponse response)
192 DictionaryUtils utils = getDictionaryUtilsInstance();
194 boolean fromAPI = utils.isRequestFromAPI(request);
195 ObjectMapper mapper = new ObjectMapper();
196 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
197 JsonNode root = mapper.readTree(request.getReader());
199 String userId = null;
201 onapData = mapper.readValue(root.get(dictionaryFields).toString(), OnapName.class);
204 onapData = mapper.readValue(root.get("onapNameDictionaryData").toString(),
206 userId = root.get("userid").textValue();
208 UserInfo userInfo = utils.getUserInfo(userId);
210 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(onapData.getOnapName(),
211 onapName, OnapName.class);
212 boolean duplicateflag = false;
213 if (!duplicateData.isEmpty()) {
214 OnapName data = (OnapName) duplicateData.get(0);
215 if (request.getParameter(operation) != null
216 && "update".equals(request.getParameter(operation))) {
217 onapData.setId(data.getId());
218 } else if ((request.getParameter(operation) != null
219 && !"update".equals(request.getParameter(operation)))
220 || (request.getParameter(operation) == null
221 && (data.getId() != onapData.getId()))) {
222 duplicateflag = true;
225 String responseString = null;
226 if (!duplicateflag) {
227 onapData.setUserModifiedBy(userInfo);
228 if (onapData.getId() == 0) {
229 onapData.setUserCreatedBy(userInfo);
230 commonClassDao.save(onapData);
232 onapData.setModifiedDate(new Date());
233 commonClassDao.update(onapData);
235 responseString = mapper.writeValueAsString(commonClassDao.getData(OnapName.class));
237 responseString = duplicateResponseString;
240 return utils.getResultForApi(responseString);
242 utils.setResponseData(response, onapNameDatas, responseString);
244 } catch (Exception e) {
245 utils.setErrorResponseData(response, e);
250 @RequestMapping(value = {"/onap_dictionary/remove_onap"}, method = {RequestMethod.POST})
251 public void removeOnapDictionary(HttpServletRequest request, HttpServletResponse response)
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;