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.onap.policy.pap.xacml.rest.adapters.GridData;
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.DescriptiveScope;
35 import org.onap.policy.rest.jpa.UserInfo;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.http.MediaType;
38 import org.springframework.stereotype.Controller;
39 import org.springframework.web.bind.annotation.RequestMapping;
40 import org.springframework.web.bind.annotation.RequestMethod;
41 import org.springframework.web.servlet.ModelAndView;
44 public class DescriptiveDictionaryController {
46 private static CommonClassDao commonClassDao;
47 private static String operation = "operation";
48 private static String dScopeName = "descriptiveScopeName";
49 private static String descriptiveDatas = "descriptiveScopeDictionaryDatas";
52 public DescriptiveDictionaryController(CommonClassDao commonClassDao) {
53 DescriptiveDictionaryController.commonClassDao = commonClassDao;
56 public void setCommonClassDao(CommonClassDao commonClassDao) {
57 DescriptiveDictionaryController.commonClassDao = commonClassDao;
60 public DescriptiveDictionaryController() {
64 private DictionaryUtils getDictionaryUtilsInstance() {
65 return DictionaryUtils.getDictionaryUtils();
68 @RequestMapping(value = {"/get_DescriptiveScopeByName"}, method = {RequestMethod.GET},
69 produces = MediaType.APPLICATION_JSON_VALUE)
70 public void getDescriptiveDictionaryByNameEntityData(HttpServletResponse response) {
71 DictionaryUtils utils = getDictionaryUtilsInstance();
72 utils.getDataByEntity(response, descriptiveDatas, dScopeName, DescriptiveScope.class);
75 @RequestMapping(value = {"/get_DescriptiveScope"}, method = {RequestMethod.GET},
76 produces = MediaType.APPLICATION_JSON_VALUE)
77 public void getDescriptiveDictionaryEntityData(HttpServletResponse response) {
78 DictionaryUtils utils = getDictionaryUtilsInstance();
79 utils.getData(response, descriptiveDatas, DescriptiveScope.class);
82 @RequestMapping(value = {"/descriptive_dictionary/save_descriptive"},
83 method = {RequestMethod.POST})
84 public ModelAndView saveDescriptiveDictionary(HttpServletRequest request,
85 HttpServletResponse response) throws IOException {
86 DictionaryUtils utils = getDictionaryUtilsInstance();
88 boolean fromAPI = utils.isRequestFromAPI(request);
89 ObjectMapper mapper = new ObjectMapper();
90 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
91 JsonNode root = mapper.readTree(request.getReader());
92 DescriptiveScope descriptiveScope;
96 descriptiveScope = mapper.readValue(root.get("dictionaryFields").toString(),
97 DescriptiveScope.class);
98 data = mapper.readValue(root.get("dictionaryFields").toString(), GridData.class);
102 mapper.readValue(root.get("descriptiveScopeDictionaryData").toString(),
103 DescriptiveScope.class);
104 data = mapper.readValue(root.get("descriptiveScopeDictionaryData").toString(),
106 userId = root.get("userid").textValue();
108 descriptiveScope.setSearch(utils.appendKeyValue(data.getAttributes(), "AND", ":"));
109 UserInfo userInfo = utils.getUserInfo(userId);
110 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(
111 descriptiveScope.getScopeName(), dScopeName, DescriptiveScope.class);
112 boolean duplicateflag = false;
113 if (!duplicateData.isEmpty()) {
114 DescriptiveScope data1 = (DescriptiveScope) duplicateData.get(0);
115 if (request.getParameter(operation) != null
116 && "update".equals(request.getParameter(operation))) {
117 descriptiveScope.setId(data1.getId());
118 } else if ((request.getParameter(operation) != null
119 && !"update".equals(request.getParameter(operation)))
120 || (request.getParameter(operation) == null
121 && (data1.getId() != descriptiveScope.getId()))) {
122 duplicateflag = true;
125 String responseString = null;
126 if (!duplicateflag) {
127 descriptiveScope.setUserModifiedBy(userInfo);
128 if (descriptiveScope.getId() == 0) {
129 descriptiveScope.setUserCreatedBy(userInfo);
130 commonClassDao.save(descriptiveScope);
132 descriptiveScope.setModifiedDate(new Date());
133 commonClassDao.update(descriptiveScope);
136 mapper.writeValueAsString(commonClassDao.getData(DescriptiveScope.class));
138 responseString = "Duplicate";
141 return utils.getResultForApi(responseString);
143 utils.setResponseData(response, descriptiveDatas, responseString);
145 } catch (Exception e) {
146 utils.setErrorResponseData(response, e);
151 @RequestMapping(value = {"/descriptive_dictionary/remove_descriptiveScope"},
152 method = {RequestMethod.POST})
153 public void removeDescriptiveDictionary(HttpServletRequest request,
154 HttpServletResponse response) throws IOException {
155 DictionaryUtils utils = getDictionaryUtilsInstance();
156 utils.removeData(request, response, descriptiveDatas, DescriptiveScope.class);