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.onap.policy.pap.xacml.rest.adapters.GridData;
31 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
32 import org.onap.policy.rest.dao.CommonClassDao;
33 import org.onap.policy.rest.jpa.DescriptiveScope;
34 import org.onap.policy.rest.jpa.UserInfo;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.http.MediaType;
37 import org.springframework.stereotype.Controller;
38 import org.springframework.web.bind.annotation.RequestMapping;
39 import org.springframework.web.bind.annotation.RequestMethod;
40 import org.springframework.web.servlet.ModelAndView;
42 import com.fasterxml.jackson.databind.DeserializationFeature;
43 import com.fasterxml.jackson.databind.JsonNode;
44 import com.fasterxml.jackson.databind.ObjectMapper;
47 public class DescriptiveDictionaryController {
49 private static CommonClassDao commonClassDao;
50 private static String operation = "operation";
51 private static String dScopeName = "descriptiveScopeName";
52 private static String descriptiveDatas = "descriptiveScopeDictionaryDatas";
55 public DescriptiveDictionaryController(CommonClassDao commonClassDao){
56 DescriptiveDictionaryController.commonClassDao = commonClassDao;
59 public void setCommonClassDao(CommonClassDao commonClassDao){
60 DescriptiveDictionaryController.commonClassDao = commonClassDao;
63 public DescriptiveDictionaryController(){
67 private DictionaryUtils getDictionaryUtilsInstance(){
68 return DictionaryUtils.getDictionaryUtils();
71 @RequestMapping(value={"/get_DescriptiveScopeByName"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
72 public void getDescriptiveDictionaryByNameEntityData(HttpServletResponse response){
73 DictionaryUtils utils = getDictionaryUtilsInstance();
74 utils.getDataByEntity(response, descriptiveDatas, dScopeName, DescriptiveScope.class);
77 @RequestMapping(value={"/get_DescriptiveScope"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
78 public void getDescriptiveDictionaryEntityData(HttpServletResponse response){
79 DictionaryUtils utils = getDictionaryUtilsInstance();
80 utils.getData(response, descriptiveDatas, DescriptiveScope.class);
83 @RequestMapping(value={"/descriptive_dictionary/save_descriptive"}, method={RequestMethod.POST})
84 public ModelAndView saveDescriptiveDictionary(HttpServletRequest request, HttpServletResponse response)throws IOException{
85 DictionaryUtils utils = getDictionaryUtilsInstance();
87 boolean fromAPI = utils.isRequestFromAPI(request);
88 ObjectMapper mapper = new ObjectMapper();
89 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
90 JsonNode root = mapper.readTree(request.getReader());
91 DescriptiveScope descriptiveScope;
95 descriptiveScope = mapper.readValue(root.get("dictionaryFields").toString(), DescriptiveScope.class);
96 data = mapper.readValue(root.get("dictionaryFields").toString(), GridData.class);
99 descriptiveScope = mapper.readValue(root.get("descriptiveScopeDictionaryData").toString(), DescriptiveScope.class);
100 data = mapper.readValue(root.get("descriptiveScopeDictionaryData").toString(), GridData.class);
101 userId = root.get("userid").textValue();
103 descriptiveScope.setSearch(utils.appendKeyValue(data.getAttributes(), "AND", ":"));
104 UserInfo userInfo = utils.getUserInfo(userId);
105 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(descriptiveScope.getScopeName(), dScopeName, DescriptiveScope.class);
106 boolean duplicateflag = false;
107 if(!duplicateData.isEmpty()){
108 DescriptiveScope data1 = (DescriptiveScope) duplicateData.get(0);
109 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
110 descriptiveScope.setId(data1.getId());
111 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) ||
112 (request.getParameter(operation) == null && (data1.getId() != descriptiveScope.getId()))){
113 duplicateflag = true;
116 String responseString = null;
118 descriptiveScope.setUserModifiedBy(userInfo);
119 if(descriptiveScope.getId() == 0){
120 descriptiveScope.setUserCreatedBy(userInfo);
121 commonClassDao.save(descriptiveScope);
123 descriptiveScope.setModifiedDate(new Date());
124 commonClassDao.update(descriptiveScope);
126 responseString = mapper.writeValueAsString(commonClassDao.getData(DescriptiveScope.class));
128 responseString = "Duplicate";
131 return utils.getResultForApi(responseString);
133 utils.setResponseData(response, descriptiveDatas, responseString);
135 }catch (Exception e){
136 utils.setErrorResponseData(response, e);
141 @RequestMapping(value={"/descriptive_dictionary/remove_descriptiveScope"}, method={RequestMethod.POST})
142 public void removeDescriptiveDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
143 DictionaryUtils utils = getDictionaryUtilsInstance();
144 utils.removeData(request, response, descriptiveDatas, DescriptiveScope.class);