2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.io.PrintWriter;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.LinkedHashMap;
29 import java.util.List;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.json.JSONObject;
38 import org.onap.policy.pap.xacml.rest.util.JsonMessage;
39 import org.onap.policy.rest.dao.CommonClassDao;
40 import org.onap.policy.rest.jpa.Attribute;
41 import org.onap.policy.rest.jpa.Category;
42 import org.onap.policy.rest.jpa.Datatype;
43 import org.onap.policy.rest.jpa.OnapName;
44 import org.onap.policy.rest.jpa.UserInfo;
45 import org.onap.policy.xacml.api.XACMLErrorConstants;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.http.MediaType;
48 import org.springframework.stereotype.Controller;
49 import org.springframework.web.bind.annotation.RequestMapping;
50 import org.springframework.web.bind.annotation.RequestMethod;
51 import org.springframework.web.servlet.ModelAndView;
53 import com.fasterxml.jackson.databind.DeserializationFeature;
54 import com.fasterxml.jackson.databind.JsonNode;
55 import com.fasterxml.jackson.databind.ObjectMapper;
58 public class DictionaryController {
60 private static final Log LOGGER = LogFactory.getLog(DictionaryController.class);
62 private static CommonClassDao commonClassDao;
63 private static String xacmlId = "xacmlId";
64 private static String apiflag = "apiflag";
65 private static String operation = "operation";
66 private static String dictionaryFields ="dictionaryFields";
67 private static String duplicateResponseString = "Duplicate";
68 private static String utf8 = "UTF-8";
69 private static String applicationJsonContentType = "application / json";
70 private static String onapName = "onapName";
72 public DictionaryController(CommonClassDao commonClassDao){
73 DictionaryController.commonClassDao = commonClassDao;
76 * This is an empty constructor
78 public DictionaryController(){}
80 public UserInfo getUserInfo(String loginId){
81 return (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", loginId);
85 public Category getCategory(){
86 List<Object> list = commonClassDao.getData(Category.class);
87 for (int i = 0; i < list.size() ; i++) {
88 Category value = (Category) list.get(i);
89 if (("resource").equals(value.getShortName())) {
96 @RequestMapping(value={"/get_AttributeDatabyAttributeName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
97 public void getAttributeDictionaryEntityDatabyAttributeName(HttpServletResponse response){
99 Map<String, Object> model = new HashMap<>();
100 ObjectMapper mapper = new ObjectMapper();
101 model.put("attributeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(Attribute.class, xacmlId)));
102 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
103 JSONObject j = new JSONObject(msg);
104 response.getWriter().write(j.toString());
107 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
111 //Attribute Dictionary
112 @RequestMapping(value="/get_AttributeData", method= RequestMethod.GET , produces=MediaType.APPLICATION_JSON_VALUE)
113 public void getAttributeDictionaryEntityData(HttpServletResponse response){
115 Map<String, Object> model = new HashMap<>();
116 ObjectMapper mapper = new ObjectMapper();
117 model.put("attributeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(Attribute.class)));
118 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
119 JSONObject j = new JSONObject(msg);
120 response.addHeader("successMapKey", "success");
121 response.addHeader(operation, "getDictionary");
122 response.getWriter().write(j.toString());
125 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
126 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
127 response.addHeader("error", "dictionaryDBQuery");
131 @RequestMapping(value={"/attribute_dictionary/save_attribute"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
132 public ModelAndView saveAttributeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
134 boolean duplicateflag = false;
135 boolean isFakeUpdate = false;
136 boolean fromAPI = false;
137 if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
140 ObjectMapper mapper = new ObjectMapper();
141 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
142 JsonNode root = mapper.readTree(request.getReader());
143 Attribute attributeData = null;
144 AttributeValues attributeValueData = null;
145 String userId = null;
147 attributeData = (Attribute)mapper.readValue(root.get(dictionaryFields).toString(), Attribute.class);
148 attributeValueData = (AttributeValues)mapper.readValue(root.get(dictionaryFields).toString(), AttributeValues.class);
151 //check if update operation or create, get id for data to be updated and update attributeData
152 if (("update").equals(request.getParameter(operation))) {
153 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(attributeData.getXacmlId(), xacmlId, Attribute.class);
154 Attribute data = (Attribute) duplicateData.get(0);
155 int id = data.getId();
158 attributeData.setId(1);
160 attributeData.setId(id);
162 attributeData.setUserCreatedBy(this.getUserInfo(userId));
165 attributeData = (Attribute)mapper.readValue(root.get("attributeDictionaryData").toString(), Attribute.class);
166 attributeValueData = (AttributeValues)mapper.readValue(root.get("attributeDictionaryData").toString(), AttributeValues.class);
167 userId = root.get("userid").textValue();
169 String userValue = "";
171 if(!attributeValueData.getUserDataTypeValues().isEmpty()){
172 for(Object attribute : attributeValueData.getUserDataTypeValues()){
173 if(attribute instanceof LinkedHashMap<?, ?>){
174 String key = ((LinkedHashMap<?, ?>) attribute).get("attributeValues").toString();
176 userValue = userValue + ",";
178 userValue = userValue + key ;
183 attributeData.setAttributeValue(userValue);
184 if(attributeData.getDatatypeBean().getShortName() != null){
185 String datatype = attributeData.getDatatypeBean().getShortName();
186 Datatype a = new Datatype();
187 if(("string").equalsIgnoreCase(datatype)){
189 }else if(("integer").equalsIgnoreCase(datatype)){
191 }else if(("boolean").equalsIgnoreCase(datatype)){
193 }else if(("double").equalsIgnoreCase(datatype)){
195 }else if(("user").equalsIgnoreCase(datatype)){
198 attributeData.setDatatypeBean(a);
200 if(attributeData.getId() == 0){
201 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(attributeData.getXacmlId(), xacmlId, Attribute.class);
202 if(!duplicateData.isEmpty()){
203 duplicateflag = true;
205 attributeData.setCategoryBean(this.getCategory());
206 attributeData.setUserCreatedBy(this.getUserInfo(userId));
207 attributeData.setUserModifiedBy(this.getUserInfo(userId));
208 commonClassDao.save(attributeData);
212 attributeData.setUserModifiedBy(this.getUserInfo(userId));
213 attributeData.setModifiedDate(new Date());
214 commonClassDao.update(attributeData);
217 String responseString = null;
219 responseString = duplicateResponseString;
221 responseString = mapper.writeValueAsString(commonClassDao.getData(Attribute.class));
225 if (responseString!=null && !(duplicateResponseString).equals(responseString)) {
227 responseString = "Exists";
229 responseString = "Success";
232 ModelAndView result = new ModelAndView();
233 result.setViewName(responseString);
236 response.setCharacterEncoding(utf8);
237 response.setContentType(applicationJsonContentType);
238 request.setCharacterEncoding(utf8);
240 PrintWriter out = response.getWriter();
241 JSONObject j = new JSONObject("{attributeDictionaryDatas: " + responseString + "}");
242 out.write(j.toString());
245 }catch (Exception e){
246 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
247 response.setCharacterEncoding(utf8);
248 request.setCharacterEncoding(utf8);
249 PrintWriter out = response.getWriter();
250 out.write(e.getMessage());
255 @RequestMapping(value={"/attribute_dictionary/remove_attribute"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
256 public ModelAndView removeAttributeDictionary(HttpServletRequest request, HttpServletResponse response)throws IOException {
258 ObjectMapper mapper = new ObjectMapper();
259 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
260 JsonNode root = mapper.readTree(request.getReader());
261 Attribute attributeData = (Attribute)mapper.readValue(root.get("data").toString(), Attribute.class);
262 commonClassDao.delete(attributeData);
263 response.setCharacterEncoding(utf8);
264 response.setContentType(applicationJsonContentType);
265 request.setCharacterEncoding(utf8);
267 PrintWriter out = response.getWriter();
268 String responseString = mapper.writeValueAsString(commonClassDao.getData(Attribute.class));
269 JSONObject j = new JSONObject("{attributeDictionaryDatas: " + responseString + "}");
270 out.write(j.toString());
274 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
275 response.setCharacterEncoding(utf8);
276 request.setCharacterEncoding(utf8);
277 PrintWriter out = response.getWriter();
278 out.write(e.getMessage());
283 //OnapName Dictionary
284 @RequestMapping(value={"/get_OnapNameDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
285 public void getOnapNameDictionaryByNameEntityData(HttpServletResponse response){
286 LOGGER.info("get_OnapNameDataByName is called");
288 Map<String, Object> model = new HashMap<>();
289 ObjectMapper mapper = new ObjectMapper();
290 model.put("onapNameDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(OnapName.class, onapName)));
291 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
292 JSONObject j = new JSONObject(msg);
293 response.getWriter().write(j.toString());
296 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
300 @RequestMapping(value={"/get_OnapNameData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
301 public void getOnapNameDictionaryEntityData(HttpServletResponse response){
303 Map<String, Object> model = new HashMap<>();
304 ObjectMapper mapper = new ObjectMapper();
305 model.put("onapNameDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(OnapName.class)));
306 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
307 JSONObject j = new JSONObject(msg);
308 response.addHeader("successMapKey", "success");
309 response.addHeader(operation, "getDictionary");
310 response.getWriter().write(j.toString());
313 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
314 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
315 response.addHeader("error", "dictionaryDBQuery");
319 @RequestMapping(value={"/onap_dictionary/save_onapName"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
320 public ModelAndView saveOnapDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
322 boolean duplicateflag = false;
323 boolean isFakeUpdate = false;
324 boolean fromAPI = false;
325 if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
328 ObjectMapper mapper = new ObjectMapper();
329 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
330 JsonNode root = mapper.readTree(request.getReader());
332 String userId = null;
334 onapData = (OnapName)mapper.readValue(root.get(dictionaryFields).toString(), OnapName.class);
337 //check if update operation or create, get id for data to be updated
338 if (("update").equals(request.getParameter(operation))) {
339 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(onapData.getOnapName(), onapName, OnapName.class);
340 OnapName data = (OnapName) duplicateData.get(0);
341 int id = data.getId();
348 onapData.setUserCreatedBy(this.getUserInfo(userId));
351 onapData = (OnapName)mapper.readValue(root.get("onapNameDictionaryData").toString(), OnapName.class);
352 userId = root.get("userid").textValue();
354 if(onapData.getId() == 0){
355 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(onapData.getOnapName(), onapName, OnapName.class);
356 if(!duplicateData.isEmpty()){
357 duplicateflag = true;
359 onapData.setUserCreatedBy(getUserInfo(userId));
360 onapData.setUserModifiedBy(getUserInfo(userId));
361 commonClassDao.save(onapData);
365 onapData.setUserModifiedBy(this.getUserInfo(userId));
366 onapData.setModifiedDate(new Date());
367 commonClassDao.update(onapData);
370 String responseString = null;
372 responseString = duplicateResponseString;
374 responseString = mapper.writeValueAsString(commonClassDao.getData(OnapName.class));
377 if (responseString!=null && !(duplicateResponseString).equals(responseString)) {
379 responseString = "Exists";
381 responseString = "Success";
385 ModelAndView result = new ModelAndView();
386 result.setViewName(responseString);
389 response.setCharacterEncoding(utf8);
390 response.setContentType(applicationJsonContentType);
391 request.setCharacterEncoding(utf8);
393 PrintWriter out = response.getWriter();
394 JSONObject j = new JSONObject("{onapNameDictionaryDatas: " + responseString + "}");
395 out.write(j.toString());
398 }catch (Exception e){
399 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
400 response.setCharacterEncoding(utf8);
401 request.setCharacterEncoding(utf8);
402 PrintWriter out = response.getWriter();
403 out.write(e.getMessage());
408 @RequestMapping(value={"/onap_dictionary/remove_onap"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
409 public ModelAndView removeOnapDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
411 ObjectMapper mapper = new ObjectMapper();
412 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
413 JsonNode root = mapper.readTree(request.getReader());
414 OnapName onapData = (OnapName)mapper.readValue(root.get("data").toString(), OnapName.class);
415 commonClassDao.delete(onapData);
416 response.setCharacterEncoding(utf8);
417 response.setContentType(applicationJsonContentType);
418 request.setCharacterEncoding(utf8);
420 PrintWriter out = response.getWriter();
422 String responseString = mapper.writeValueAsString(commonClassDao.getData(OnapName.class));
423 JSONObject j = new JSONObject("{onapNameDictionaryDatas: " + responseString + "}");
424 out.write(j.toString());
429 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
430 response.setCharacterEncoding(utf8);
431 request.setCharacterEncoding(utf8);
432 PrintWriter out = response.getWriter();
433 out.write(e.getMessage());
440 class AttributeValues{
441 private List<Object> userDataTypeValues;
443 public List<Object> getUserDataTypeValues() {
444 return userDataTypeValues;
447 public void setUserDataTypeValues(List<Object> userDataTypeValues) {
448 this.userDataTypeValues = userDataTypeValues;