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.PrintWriter;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.HashMap;
27 import java.util.LinkedHashMap;
28 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.json.JSONObject;
37 import org.onap.policy.pap.xacml.rest.util.JsonMessage;
38 import org.onap.policy.rest.dao.CommonClassDao;
39 import org.onap.policy.rest.jpa.Attribute;
40 import org.onap.policy.rest.jpa.Category;
41 import org.onap.policy.rest.jpa.Datatype;
42 import org.onap.policy.rest.jpa.OnapName;
43 import org.onap.policy.rest.jpa.UserInfo;
44 import org.onap.policy.xacml.api.XACMLErrorConstants;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.http.MediaType;
47 import org.springframework.stereotype.Controller;
48 import org.springframework.web.bind.annotation.RequestMapping;
49 import org.springframework.web.bind.annotation.RequestMethod;
50 import org.springframework.web.servlet.ModelAndView;
52 import com.fasterxml.jackson.databind.DeserializationFeature;
53 import com.fasterxml.jackson.databind.JsonNode;
54 import com.fasterxml.jackson.databind.ObjectMapper;
57 public class DictionaryController {
59 private static final Log LOGGER = LogFactory.getLog(DictionaryController.class);
61 private static CommonClassDao commonClassDao;
64 public DictionaryController(CommonClassDao commonClassDao){
65 DictionaryController.commonClassDao = commonClassDao;
68 public DictionaryController(){}
70 public UserInfo getUserInfo(String loginId){
71 UserInfo name = (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", loginId);
76 public Category getCategory(){
77 List<Object> list = commonClassDao.getData(Category.class);
78 for (int i = 0; i < list.size() ; i++) {
79 Category value = (Category) list.get(i);
80 if (value.getShortName().equals("resource")) {
87 @RequestMapping(value={"/get_AttributeDatabyAttributeName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
88 public void getAttributeDictionaryEntityDatabyAttributeName(HttpServletRequest request, HttpServletResponse response){
91 Map<String, Object> model = new HashMap<>();
92 ObjectMapper mapper = new ObjectMapper();
93 model.put("attributeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(Attribute.class, "xacmlId")));
94 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
95 JSONObject j = new JSONObject(msg);
96 response.getWriter().write(j.toString());
99 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
103 //Attribute Dictionary
104 @RequestMapping(value="/get_AttributeData", method= RequestMethod.GET , produces=MediaType.APPLICATION_JSON_VALUE)
105 public void getAttributeDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
107 System.out.println();
108 Map<String, Object> model = new HashMap<>();
109 ObjectMapper mapper = new ObjectMapper();
110 model.put("attributeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(Attribute.class)));
111 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
112 JSONObject j = new JSONObject(msg);
113 response.addHeader("successMapKey", "success");
114 response.addHeader("operation", "getDictionary");
115 response.getWriter().write(j.toString());
118 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
119 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
120 response.addHeader("error", "dictionaryDBQuery");
124 @RequestMapping(value={"/attribute_dictionary/save_attribute"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
125 public ModelAndView saveAttributeDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{
127 boolean duplicateflag = false;
128 boolean isFakeUpdate = false;
129 boolean fromAPI = false;
130 if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
133 ObjectMapper mapper = new ObjectMapper();
134 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
135 JsonNode root = mapper.readTree(request.getReader());
136 Attribute attributeData = null;
137 AttributeValues attributeValueData = null;
138 String userId = null;
140 //JsonNode json = root.get("dictionaryFields");
141 attributeData = (Attribute)mapper.readValue(root.get("dictionaryFields").toString(), Attribute.class);
142 attributeValueData = (AttributeValues)mapper.readValue(root.get("dictionaryFields").toString(), AttributeValues.class);
145 //check if update operation or create, get id for data to be updated and update attributeData
146 if (request.getParameter("operation").equals("update")) {
147 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(attributeData.getXacmlId(), "xacmlId", Attribute.class);
149 Attribute data = (Attribute) duplicateData.get(0);
153 attributeData.setId(1);
155 attributeData.setId(id);
157 attributeData.setUserCreatedBy(this.getUserInfo(userId));
160 attributeData = (Attribute)mapper.readValue(root.get("attributeDictionaryData").toString(), Attribute.class);
161 attributeValueData = (AttributeValues)mapper.readValue(root.get("attributeDictionaryData").toString(), AttributeValues.class);
162 userId = root.get("userid").textValue();
164 String userValue = "";
166 if(attributeValueData.getUserDataTypeValues().size() > 0){
167 for(Object attribute : attributeValueData.getUserDataTypeValues()){
168 if(attribute instanceof LinkedHashMap<?, ?>){
169 String key = ((LinkedHashMap<?, ?>) attribute).get("attributeValues").toString();
171 userValue = userValue + ",";
173 userValue = userValue + key ;
178 attributeData.setAttributeValue(userValue);
179 if(attributeData.getDatatypeBean().getShortName() != null){
180 String datatype = attributeData.getDatatypeBean().getShortName();
181 Datatype a = new Datatype();
182 if(datatype.equalsIgnoreCase("string")){
184 }else if(datatype.equalsIgnoreCase("integer")){
186 }else if(datatype.equalsIgnoreCase("boolean")){
188 }else if(datatype.equalsIgnoreCase("double")){
190 }else if(datatype.equalsIgnoreCase("user")){
193 attributeData.setDatatypeBean(a);
195 if(attributeData.getId() == 0){
196 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(attributeData.getXacmlId(), "xacmlId", Attribute.class);
197 if(!duplicateData.isEmpty()){
198 duplicateflag = true;
200 attributeData.setCategoryBean(this.getCategory());
201 attributeData.setUserCreatedBy(this.getUserInfo(userId));
202 attributeData.setUserModifiedBy(this.getUserInfo(userId));
203 commonClassDao.save(attributeData);
207 attributeData.setUserModifiedBy(this.getUserInfo(userId));
208 attributeData.setModifiedDate(new Date());
209 commonClassDao.update(attributeData);
212 String responseString = null;
214 responseString = "Duplicate";
216 responseString = mapper.writeValueAsString(commonClassDao.getData(Attribute.class));
220 if (responseString!=null && !responseString.equals("Duplicate")) {
222 responseString = "Exists";
224 responseString = "Success";
227 ModelAndView result = new ModelAndView();
228 result.setViewName(responseString);
231 response.setCharacterEncoding("UTF-8");
232 response.setContentType("application / json");
233 request.setCharacterEncoding("UTF-8");
235 PrintWriter out = response.getWriter();
236 JSONObject j = new JSONObject("{attributeDictionaryDatas: " + responseString + "}");
237 out.write(j.toString());
240 }catch (Exception e){
241 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
242 response.setCharacterEncoding("UTF-8");
243 request.setCharacterEncoding("UTF-8");
244 PrintWriter out = response.getWriter();
245 out.write(e.getMessage());
250 @RequestMapping(value={"/attribute_dictionary/remove_attribute"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
251 public ModelAndView removeAttributeDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception {
253 ObjectMapper mapper = new ObjectMapper();
254 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
255 JsonNode root = mapper.readTree(request.getReader());
256 Attribute attributeData = (Attribute)mapper.readValue(root.get("data").toString(), Attribute.class);
257 commonClassDao.delete(attributeData);
258 response.setCharacterEncoding("UTF-8");
259 response.setContentType("application / json");
260 request.setCharacterEncoding("UTF-8");
262 PrintWriter out = response.getWriter();
263 String responseString = mapper.writeValueAsString(commonClassDao.getData(Attribute.class));
264 JSONObject j = new JSONObject("{attributeDictionaryDatas: " + responseString + "}");
265 out.write(j.toString());
269 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
270 response.setCharacterEncoding("UTF-8");
271 request.setCharacterEncoding("UTF-8");
272 PrintWriter out = response.getWriter();
273 out.write(e.getMessage());
278 //OnapName Dictionary
279 @RequestMapping(value={"/get_OnapNameDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
280 public void getOnapNameDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response){
281 LOGGER.info("get_OnapNameDataByName is called");
283 Map<String, Object> model = new HashMap<>();
284 ObjectMapper mapper = new ObjectMapper();
285 model.put("onapNameDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(OnapName.class, "onapName")));
286 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
287 JSONObject j = new JSONObject(msg);
288 response.getWriter().write(j.toString());
291 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
295 @RequestMapping(value={"/get_OnapNameData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
296 public void getOnapNameDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
298 Map<String, Object> model = new HashMap<>();
299 ObjectMapper mapper = new ObjectMapper();
300 model.put("onapNameDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(OnapName.class)));
301 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
302 JSONObject j = new JSONObject(msg);
303 response.addHeader("successMapKey", "success");
304 response.addHeader("operation", "getDictionary");
305 response.getWriter().write(j.toString());
308 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
309 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
310 response.addHeader("error", "dictionaryDBQuery");
314 @RequestMapping(value={"/onap_dictionary/save_onapName"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
315 public ModelAndView saveOnapDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{
317 boolean duplicateflag = false;
318 boolean isFakeUpdate = false;
319 boolean fromAPI = false;
320 if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
323 ObjectMapper mapper = new ObjectMapper();
324 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
325 JsonNode root = mapper.readTree(request.getReader());
327 String userId = null;
329 onapData = (OnapName)mapper.readValue(root.get("dictionaryFields").toString(), OnapName.class);
332 //check if update operation or create, get id for data to be updated
333 if (request.getParameter("operation").equals("update")) {
334 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(onapData.getOnapName(), "onapName", OnapName.class);
336 OnapName data = (OnapName) duplicateData.get(0);
344 onapData.setUserCreatedBy(this.getUserInfo(userId));
347 onapData = (OnapName)mapper.readValue(root.get("onapNameDictionaryData").toString(), OnapName.class);
348 userId = root.get("userid").textValue();
350 if(onapData.getId() == 0){
351 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(onapData.getOnapName(), "onapName", OnapName.class);
352 if(!duplicateData.isEmpty()){
353 duplicateflag = true;
355 onapData.setUserCreatedBy(getUserInfo(userId));
356 onapData.setUserModifiedBy(getUserInfo(userId));
357 commonClassDao.save(onapData);
361 onapData.setUserModifiedBy(this.getUserInfo(userId));
362 onapData.setModifiedDate(new Date());
363 commonClassDao.update(onapData);
366 String responseString = null;
368 responseString = "Duplicate";
370 responseString = mapper.writeValueAsString(commonClassDao.getData(OnapName.class));
373 if (responseString!=null && !responseString.equals("Duplicate")) {
375 responseString = "Exists";
377 responseString = "Success";
381 ModelAndView result = new ModelAndView();
382 result.setViewName(responseString);
385 response.setCharacterEncoding("UTF-8");
386 response.setContentType("application / json");
387 request.setCharacterEncoding("UTF-8");
389 PrintWriter out = response.getWriter();
390 JSONObject j = new JSONObject("{onapNameDictionaryDatas: " + responseString + "}");
391 out.write(j.toString());
394 }catch (Exception e){
395 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
396 response.setCharacterEncoding("UTF-8");
397 request.setCharacterEncoding("UTF-8");
398 PrintWriter out = response.getWriter();
399 out.write(e.getMessage());
404 @RequestMapping(value={"/onap_dictionary/remove_onap"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
405 public ModelAndView removeOnapDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception {
407 ObjectMapper mapper = new ObjectMapper();
408 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
409 JsonNode root = mapper.readTree(request.getReader());
410 OnapName onapData = (OnapName)mapper.readValue(root.get("data").toString(), OnapName.class);
411 commonClassDao.delete(onapData);
412 response.setCharacterEncoding("UTF-8");
413 response.setContentType("application / json");
414 request.setCharacterEncoding("UTF-8");
416 PrintWriter out = response.getWriter();
418 String responseString = mapper.writeValueAsString(commonClassDao.getData(OnapName.class));
419 JSONObject j = new JSONObject("{onapNameDictionaryDatas: " + responseString + "}");
420 out.write(j.toString());
425 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
426 response.setCharacterEncoding("UTF-8");
427 request.setCharacterEncoding("UTF-8");
428 PrintWriter out = response.getWriter();
429 out.write(e.getMessage());
436 class AttributeValues{
437 private ArrayList<Object> userDataTypeValues;
439 public ArrayList<Object> getUserDataTypeValues() {
440 return userDataTypeValues;
443 public void setUserDataTypeValues(ArrayList<Object> userDataTypeValues) {
444 this.userDataTypeValues = userDataTypeValues;