51c8a1d919c7e46b30720d481771dc1b92079e64
[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 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;
42
43 @Controller
44 public class DescriptiveDictionaryController {
45
46     private static CommonClassDao commonClassDao;
47     private static String operation = "operation";
48     private static String dScopeName = "descriptiveScopeName";
49     private static String descriptiveDatas = "descriptiveScopeDictionaryDatas";
50
51     @Autowired
52     public DescriptiveDictionaryController(CommonClassDao commonClassDao) {
53         DescriptiveDictionaryController.commonClassDao = commonClassDao;
54     }
55
56     public void setCommonClassDao(CommonClassDao commonClassDao) {
57         DescriptiveDictionaryController.commonClassDao = commonClassDao;
58     }
59
60     public DescriptiveDictionaryController() {
61         super();
62     }
63
64     private DictionaryUtils getDictionaryUtilsInstance() {
65         return DictionaryUtils.getDictionaryUtils();
66     }
67
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);
73     }
74
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);
80     }
81
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();
87         try {
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;
93             GridData data;
94             String userId = null;
95             if (fromAPI) {
96                 descriptiveScope = mapper.readValue(root.get("dictionaryFields").toString(),
97                         DescriptiveScope.class);
98                 data = mapper.readValue(root.get("dictionaryFields").toString(), GridData.class);
99                 userId = "API";
100             } else {
101                 descriptiveScope =
102                         mapper.readValue(root.get("descriptiveScopeDictionaryData").toString(),
103                                 DescriptiveScope.class);
104                 data = mapper.readValue(root.get("descriptiveScopeDictionaryData").toString(),
105                         GridData.class);
106                 userId = root.get("userid").textValue();
107             }
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;
123                 }
124             }
125             String responseString = null;
126             if (!duplicateflag) {
127                 descriptiveScope.setUserModifiedBy(userInfo);
128                 if (descriptiveScope.getId() == 0) {
129                     descriptiveScope.setUserCreatedBy(userInfo);
130                     commonClassDao.save(descriptiveScope);
131                 } else {
132                     descriptiveScope.setModifiedDate(new Date());
133                     commonClassDao.update(descriptiveScope);
134                 }
135                 responseString =
136                         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"},
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);
157     }
158 }