Consolidate PolicyRestAdapter setup
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / controller / DescriptiveDictionaryController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest.controller;
22
23 import com.fasterxml.jackson.databind.DeserializationFeature;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26
27 import java.io.IOException;
28 import java.util.Date;
29 import java.util.List;
30
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 import org.onap.policy.pap.xacml.rest.adapters.GridData;
35 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
36 import org.onap.policy.rest.dao.CommonClassDao;
37 import org.onap.policy.rest.jpa.DescriptiveScope;
38 import org.onap.policy.rest.jpa.UserInfo;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.http.MediaType;
41 import org.springframework.stereotype.Controller;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.bind.annotation.RequestMethod;
44 import org.springframework.web.servlet.ModelAndView;
45
46 @Controller
47 public class DescriptiveDictionaryController {
48
49     private static CommonClassDao commonClassDao;
50     private static String operation = "operation";
51     private static String dScopeName = "descriptiveScopeName";
52     private static String descriptiveDatas = "descriptiveScopeDictionaryDatas";
53
54     @Autowired
55     public DescriptiveDictionaryController(CommonClassDao commonClassDao) {
56         DescriptiveDictionaryController.commonClassDao = commonClassDao;
57     }
58
59     public void setCommonClassDao(CommonClassDao commonClassDao) {
60         DescriptiveDictionaryController.commonClassDao = commonClassDao;
61     }
62
63     public DescriptiveDictionaryController() {
64         super();
65     }
66
67     private DictionaryUtils getDictionaryUtilsInstance() {
68         return DictionaryUtils.getDictionaryUtils();
69     }
70
71     @RequestMapping(
72             value = {"/get_DescriptiveScopeByName"},
73             method = {RequestMethod.GET},
74             produces = MediaType.APPLICATION_JSON_VALUE)
75     public void getDescriptiveDictionaryByNameEntityData(HttpServletResponse response) {
76         DictionaryUtils utils = getDictionaryUtilsInstance();
77         utils.getDataByEntity(response, descriptiveDatas, dScopeName, DescriptiveScope.class);
78     }
79
80     @RequestMapping(
81             value = {"/get_DescriptiveScope"},
82             method = {RequestMethod.GET},
83             produces = MediaType.APPLICATION_JSON_VALUE)
84     public void getDescriptiveDictionaryEntityData(HttpServletResponse response) {
85         DictionaryUtils utils = getDictionaryUtilsInstance();
86         utils.getData(response, descriptiveDatas, DescriptiveScope.class);
87     }
88
89     @RequestMapping(value = {"/descriptive_dictionary/save_descriptive"}, method = {RequestMethod.POST})
90     public ModelAndView saveDescriptiveDictionary(HttpServletRequest request, HttpServletResponse response)
91             throws IOException {
92         DictionaryUtils utils = getDictionaryUtilsInstance();
93         try {
94             boolean fromAPI = utils.isRequestFromAPI(request);
95             ObjectMapper mapper = new ObjectMapper();
96             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
97             JsonNode root = mapper.readTree(request.getReader());
98             DescriptiveScope descriptiveScope;
99             GridData data;
100             String userId = null;
101             if (fromAPI) {
102                 descriptiveScope = mapper.readValue(root.get("dictionaryFields").toString(), DescriptiveScope.class);
103                 data = mapper.readValue(root.get("dictionaryFields").toString(), GridData.class);
104                 userId = "API";
105             } else {
106                 descriptiveScope =
107                         mapper.readValue(root.get("descriptiveScopeDictionaryData").toString(), DescriptiveScope.class);
108                 data = mapper.readValue(root.get("descriptiveScopeDictionaryData").toString(), GridData.class);
109                 userId = root.get("userid").textValue();
110             }
111             descriptiveScope.setSearch(utils.appendKeyValue(data.getAttributes(), "AND", ":"));
112             UserInfo userInfo = utils.getUserInfo(userId);
113             List<Object> duplicateData = commonClassDao.checkDuplicateEntry(descriptiveScope.getScopeName(), dScopeName,
114                     DescriptiveScope.class);
115             boolean duplicateflag = false;
116             if (!duplicateData.isEmpty()) {
117                 DescriptiveScope data1 = (DescriptiveScope) duplicateData.get(0);
118                 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
119                     descriptiveScope.setId(data1.getId());
120                 } else if ((request.getParameter(operation) != null
121                         && !"update".equals(request.getParameter(operation)))
122                         || (request.getParameter(operation) == null && (data1.getId() != descriptiveScope.getId()))) {
123                     duplicateflag = true;
124                 }
125             }
126             String responseString = null;
127             if (!duplicateflag) {
128                 descriptiveScope.setUserModifiedBy(userInfo);
129                 if (descriptiveScope.getId() == 0) {
130                     descriptiveScope.setUserCreatedBy(userInfo);
131                     commonClassDao.save(descriptiveScope);
132                 } else {
133                     descriptiveScope.setModifiedDate(new Date());
134                     commonClassDao.update(descriptiveScope);
135                 }
136                 responseString = mapper.writeValueAsString(commonClassDao.getData(DescriptiveScope.class));
137             } else {
138                 responseString = "Duplicate";
139             }
140             if (fromAPI) {
141                 return utils.getResultForApi(responseString);
142             } else {
143                 utils.setResponseData(response, descriptiveDatas, responseString);
144             }
145         } catch (Exception e) {
146             utils.setErrorResponseData(response, e);
147         }
148         return null;
149     }
150
151     @RequestMapping(value = {"/descriptive_dictionary/remove_descriptiveScope"}, method = {RequestMethod.POST})
152     public void removeDescriptiveDictionary(HttpServletRequest request, HttpServletResponse response)
153             throws IOException {
154         DictionaryUtils utils = getDictionaryUtilsInstance();
155         utils.removeData(request, response, descriptiveDatas, DescriptiveScope.class);
156     }
157 }