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.openecomp.policy.pap.xacml.rest.controller;
23 import java.io.PrintWriter;
24 import java.util.Date;
25 import java.util.HashMap;
26 import java.util.LinkedHashMap;
27 import java.util.List;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
33 import org.json.JSONObject;
34 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
35 import org.openecomp.policy.common.logging.flexlogger.Logger;
36 import org.openecomp.policy.pap.xacml.rest.adapters.GridData;
37 import org.openecomp.policy.pap.xacml.rest.util.JsonMessage;
38 import org.openecomp.policy.rest.dao.CommonClassDao;
39 import org.openecomp.policy.rest.jpa.DescriptiveScope;
40 import org.openecomp.policy.rest.jpa.UserInfo;
41 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.http.MediaType;
44 import org.springframework.stereotype.Controller;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.servlet.ModelAndView;
48 import com.fasterxml.jackson.databind.DeserializationFeature;
49 import com.fasterxml.jackson.databind.JsonNode;
50 import com.fasterxml.jackson.databind.ObjectMapper;
53 public class DescriptiveDictionaryController {
55 private static final Logger LOGGER = FlexLogger.getLogger(DescriptiveDictionaryController.class);
57 private static CommonClassDao commonClassDao;
60 public DescriptiveDictionaryController(CommonClassDao commonClassDao){
61 DescriptiveDictionaryController.commonClassDao = commonClassDao;
64 public DescriptiveDictionaryController(){}
66 public UserInfo getUserInfo(String loginId){
67 UserInfo name = (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", loginId);
71 @RequestMapping(value={"/get_DescriptiveScopeByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
72 public void getDescriptiveDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response){
74 Map<String, Object> model = new HashMap<>();
75 ObjectMapper mapper = new ObjectMapper();
76 model.put("descriptiveScopeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(DescriptiveScope.class, "descriptiveScopeName")));
77 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
78 JSONObject j = new JSONObject(msg);
79 response.getWriter().write(j.toString());
82 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
86 @RequestMapping(value={"/get_DescriptiveScope"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
87 public void getDescriptiveDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
89 Map<String, Object> model = new HashMap<>();
90 ObjectMapper mapper = new ObjectMapper();
91 model.put("descriptiveScopeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(DescriptiveScope.class)));
92 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
93 JSONObject j = new JSONObject(msg);
94 response.addHeader("successMapKey", "success");
95 response.addHeader("operation", "getDictionary");
96 response.getWriter().write(j.toString());
99 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
100 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
101 response.addHeader("error", "dictionaryDBQuery");
105 @RequestMapping(value={"/descriptive_dictionary/save_descriptive"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
106 public ModelAndView saveDescriptiveDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{
108 boolean duplicateflag = false;
109 boolean isFakeUpdate = false;
110 boolean fromAPI = false;
111 if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
114 ObjectMapper mapper = new ObjectMapper();
115 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
116 JsonNode root = mapper.readTree(request.getReader());
117 DescriptiveScope descriptiveScope;
119 String userId = null;
121 descriptiveScope = (DescriptiveScope)mapper.readValue(root.get("dictionaryFields").toString(), DescriptiveScope.class);
122 data = (GridData)mapper.readValue(root.get("dictionaryFields").toString(), GridData.class);
125 //check if update operation or create, get id for data to be updated and update attributeData
126 if (request.getParameter("operation").equals("update")) {
127 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(descriptiveScope.getScopeName(), "descriptiveScopeName", DescriptiveScope.class);
129 DescriptiveScope dbdata = (DescriptiveScope) duplicateData.get(0);
133 descriptiveScope.setId(1);
135 descriptiveScope.setId(id);
137 descriptiveScope.setUserCreatedBy(this.getUserInfo(userId));
140 descriptiveScope = (DescriptiveScope)mapper.readValue(root.get("descriptiveScopeDictionaryData").toString(), DescriptiveScope.class);
141 data = (GridData)mapper.readValue(root.get("descriptiveScopeDictionaryData").toString(), GridData.class);
142 userId = root.get("userid").textValue();
146 if(data.getAttributes().size() > 0){
147 for(Object attribute : data.getAttributes()){
148 if(attribute instanceof LinkedHashMap<?, ?>){
149 String key = ((LinkedHashMap<?, ?>) attribute).get("option").toString();
150 String value = ((LinkedHashMap<?, ?>) attribute).get("number").toString();
152 header = header + "AND";
154 header = header + key + ":";
155 header = header + value;
160 descriptiveScope.setSearch(header);
161 if(descriptiveScope.getId() == 0){
162 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(descriptiveScope.getScopeName(), "descriptiveScopeName", DescriptiveScope.class);
163 if(!duplicateData.isEmpty()){
164 duplicateflag = true;
166 descriptiveScope.setUserCreatedBy(this.getUserInfo(userId));
167 descriptiveScope.setUserModifiedBy(this.getUserInfo(userId));
168 commonClassDao.save(descriptiveScope);
172 descriptiveScope.setUserModifiedBy(this.getUserInfo(userId));
173 descriptiveScope.setModifiedDate(new Date());
174 commonClassDao.update(descriptiveScope);
177 String responseString = "";
179 responseString = "Duplicate";
181 responseString = mapper.writeValueAsString(commonClassDao.getData(DescriptiveScope.class));
185 if (responseString!=null && !responseString.equals("Duplicate")) {
187 responseString = "Exists";
189 responseString = "Success";
192 ModelAndView result = new ModelAndView();
193 result.setViewName(responseString);
196 response.setCharacterEncoding("UTF-8");
197 response.setContentType("application / json");
198 request.setCharacterEncoding("UTF-8");
200 PrintWriter out = response.getWriter();
201 JSONObject j = new JSONObject("{descriptiveScopeDictionaryDatas: " + responseString + "}");
202 out.write(j.toString());
206 }catch (Exception e){
207 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
208 response.setCharacterEncoding("UTF-8");
209 request.setCharacterEncoding("UTF-8");
210 PrintWriter out = response.getWriter();
211 out.write(e.getMessage());
216 @RequestMapping(value={"/descriptive_dictionary/remove_descriptiveScope"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
217 public ModelAndView removeDescriptiveDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception {
219 ObjectMapper mapper = new ObjectMapper();
220 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
221 JsonNode root = mapper.readTree(request.getReader());
222 DescriptiveScope descriptiveScope = (DescriptiveScope)mapper.readValue(root.get("data").toString(), DescriptiveScope.class);
223 commonClassDao.delete(descriptiveScope);
224 response.setCharacterEncoding("UTF-8");
225 response.setContentType("application / json");
226 request.setCharacterEncoding("UTF-8");
228 PrintWriter out = response.getWriter();
230 String responseString = mapper.writeValueAsString(commonClassDao.getData(DescriptiveScope.class));
231 JSONObject j = new JSONObject("{descriptiveScopeDictionaryDatas: " + responseString + "}");
232 out.write(j.toString());
237 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
238 response.setCharacterEncoding("UTF-8");
239 request.setCharacterEncoding("UTF-8");
240 PrintWriter out = response.getWriter();
241 out.write(e.getMessage());