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.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
33 import org.onap.policy.rest.dao.CommonClassDao;
34 import org.onap.policy.rest.jpa.Attribute;
35 import org.onap.policy.rest.jpa.OnapName;
36 import org.onap.policy.rest.jpa.UserInfo;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.http.MediaType;
39 import org.springframework.stereotype.Controller;
40 import org.springframework.web.bind.annotation.RequestMapping;
41 import org.springframework.web.bind.annotation.RequestMethod;
42 import org.springframework.web.servlet.ModelAndView;
44 import com.fasterxml.jackson.databind.DeserializationFeature;
45 import com.fasterxml.jackson.databind.JsonNode;
46 import com.fasterxml.jackson.databind.ObjectMapper;
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.dictionaryUtils != null ? DictionaryUtils.getDictionaryUtils() : new DictionaryUtils();
75 @RequestMapping(value={"/get_AttributeDatabyAttributeName"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
76 public void getAttributeDictionaryEntityDatabyAttributeName(HttpServletResponse response){
77 DictionaryUtils utils = getDictionaryUtilsInstance();
78 utils.getDataByEntity(response, attributeDatas, xacmlId, Attribute.class);
81 //Attribute Dictionary
82 @RequestMapping(value="/get_AttributeData", method= RequestMethod.GET , produces=MediaType.APPLICATION_JSON_VALUE)
83 public void getAttributeDictionaryEntityData(HttpServletResponse response){
84 DictionaryUtils utils = getDictionaryUtilsInstance();
85 utils.getData(response, attributeDatas, Attribute.class);
88 @RequestMapping(value={"/attribute_dictionary/save_attribute"}, method={RequestMethod.POST})
89 public ModelAndView saveAttributeDictionary(HttpServletRequest request, 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;
100 attributeData = mapper.readValue(root.get(dictionaryFields).toString(), Attribute.class);
101 attributeValueData = mapper.readValue(root.get(dictionaryFields).toString(), AttributeValues.class);
104 attributeData = mapper.readValue(root.get("attributeDictionaryData").toString(), Attribute.class);
105 attributeValueData = mapper.readValue(root.get("attributeDictionaryData").toString(), AttributeValues.class);
106 userId = root.get("userid").textValue();
108 UserInfo userInfo = utils.getUserInfo(userId);
109 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(attributeData.getXacmlId(), xacmlId, Attribute.class);
110 boolean duplicateflag = false;
111 if(!duplicateData.isEmpty()){
112 Attribute data = (Attribute) duplicateData.get(0);
113 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
114 attributeData.setId(data.getId());
115 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) ||
116 (request.getParameter(operation) == null && (data.getId() != attributeData.getId()))){
117 duplicateflag = true;
120 if(attributeValueData.getUserDataTypeValues() != null && !attributeValueData.getUserDataTypeValues().isEmpty()){
121 attributeData.setAttributeValue(utils.appendKey(attributeValueData.getUserDataTypeValues(), "attributeValues", ","));
124 if(attributeData.getDatatypeBean().getShortName() != null){
125 String datatype = attributeData.getDatatypeBean().getShortName();
126 attributeData.setDatatypeBean(utils.getDataType(datatype));
129 String responseString = null;
131 attributeData.setUserModifiedBy(userInfo);
132 if(attributeData.getId() == 0){
133 attributeData.setCategoryBean(utils.getCategory());
134 attributeData.setUserCreatedBy(userInfo);
135 commonClassDao.save(attributeData);
137 attributeData.setModifiedDate(new Date());
138 commonClassDao.update(attributeData);
140 responseString = mapper.writeValueAsString(commonClassDao.getData(Attribute.class));
142 responseString = duplicateResponseString;
145 return utils.getResultForApi(responseString);
147 utils.setResponseData(response, attributeDatas, responseString);
149 }catch (Exception e){
150 utils.setErrorResponseData(response, e);
155 @RequestMapping(value={"/attribute_dictionary/remove_attribute"}, method={RequestMethod.POST})
156 public void removeAttributeDictionary(HttpServletRequest request, HttpServletResponse response)throws IOException {
157 DictionaryUtils utils = getDictionaryUtilsInstance();
158 utils.removeData(request, response, attributeDatas, Attribute.class);
161 //OnapName Dictionary
162 @RequestMapping(value={"/get_OnapNameDataByName"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
163 public void getOnapNameDictionaryByNameEntityData(HttpServletResponse response){
164 LOGGER.info("get_OnapNameDataByName is called");
165 DictionaryUtils utils = getDictionaryUtilsInstance();
166 utils.getDataByEntity(response, onapNameDatas, onapName, OnapName.class);
169 @RequestMapping(value={"/get_OnapNameData"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
170 public void getOnapNameDictionaryEntityData(HttpServletResponse response){
171 DictionaryUtils utils = getDictionaryUtilsInstance();
172 utils.getData(response, onapNameDatas, OnapName.class);
175 @RequestMapping(value={"/onap_dictionary/save_onapName"}, method={RequestMethod.POST})
176 public ModelAndView saveOnapDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
177 DictionaryUtils utils = getDictionaryUtilsInstance();
179 boolean fromAPI = utils.isRequestFromAPI(request);
180 ObjectMapper mapper = new ObjectMapper();
181 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
182 JsonNode root = mapper.readTree(request.getReader());
184 String userId = null;
186 onapData = mapper.readValue(root.get(dictionaryFields).toString(), OnapName.class);
189 onapData = mapper.readValue(root.get("onapNameDictionaryData").toString(), OnapName.class);
190 userId = root.get("userid").textValue();
192 UserInfo userInfo = utils.getUserInfo(userId);
194 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(onapData.getOnapName(), onapName, OnapName.class);
195 boolean duplicateflag = false;
196 if(!duplicateData.isEmpty()){
197 OnapName data = (OnapName) duplicateData.get(0);
198 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
199 onapData.setId(data.getId());
200 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) ||
201 (request.getParameter(operation) == null && (data.getId() != onapData.getId()))){
202 duplicateflag = true;
205 String responseString = null;
207 onapData.setUserModifiedBy(userInfo);
208 if(onapData.getId() == 0){
209 onapData.setUserCreatedBy(userInfo);
210 commonClassDao.save(onapData);
212 onapData.setModifiedDate(new Date());
213 commonClassDao.update(onapData);
215 responseString = mapper.writeValueAsString(commonClassDao.getData(OnapName.class));
217 responseString = duplicateResponseString;
220 return utils.getResultForApi(responseString);
222 utils.setResponseData(response, onapNameDatas, responseString);
224 }catch (Exception e){
225 utils.setErrorResponseData(response, e);
230 @RequestMapping(value={"/onap_dictionary/remove_onap"}, method={RequestMethod.POST})
231 public void removeOnapDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
232 DictionaryUtils utils = getDictionaryUtilsInstance();
233 utils.removeData(request, response, onapNameDatas, OnapName.class);
237 class AttributeValues{
238 private List<Object> userDataTypeValues;
240 public List<Object> getUserDataTypeValues() {
241 return userDataTypeValues;
244 public void setUserDataTypeValues(List<Object> userDataTypeValues) {
245 this.userDataTypeValues = userDataTypeValues;