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.HashMap;
25 import java.util.LinkedHashMap;
26 import java.util.List;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
32 import org.json.JSONObject;
33 import org.openecomp.policy.pap.xacml.rest.adapters.GridData;
34 import org.openecomp.policy.pap.xacml.rest.util.JsonMessage;
35 import org.openecomp.policy.rest.dao.DescriptiveScopeDao;
36 import org.openecomp.policy.rest.dao.UserInfoDao;
37 import org.openecomp.policy.rest.jpa.DescriptiveScope;
38 import org.openecomp.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.servlet.ModelAndView;
45 import com.fasterxml.jackson.databind.DeserializationFeature;
46 import com.fasterxml.jackson.databind.JsonNode;
47 import com.fasterxml.jackson.databind.ObjectMapper;
50 public class DescriptiveDictionaryController {
53 DescriptiveScopeDao descriptiveScopeDao;
56 UserInfoDao userInfoDao;
58 public UserInfo getUserInfo(String loginId){
59 UserInfo name = userInfoDao.getUserInfoByLoginId(loginId);
63 @RequestMapping(value={"/get_DescriptiveScopeByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
64 public void getDescriptiveDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response){
66 Map<String, Object> model = new HashMap<String, Object>();
67 ObjectMapper mapper = new ObjectMapper();
68 model.put("descriptiveScopeDictionaryDatas", mapper.writeValueAsString(descriptiveScopeDao.getDescriptiveScopeDataByName()));
69 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
70 JSONObject j = new JSONObject(msg);
71 response.getWriter().write(j.toString());
78 @RequestMapping(value={"/get_DescriptiveScope"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
79 public void getDescriptiveDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
81 Map<String, Object> model = new HashMap<String, Object>();
82 ObjectMapper mapper = new ObjectMapper();
83 model.put("descriptiveScopeDictionaryDatas", mapper.writeValueAsString(descriptiveScopeDao.getDescriptiveScope()));
84 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
85 JSONObject j = new JSONObject(msg);
86 response.getWriter().write(j.toString());
93 @RequestMapping(value={"/descriptive_dictionary/save_descriptive.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
94 public ModelAndView saveDescriptiveDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{
96 boolean duplicateflag = false;
97 ObjectMapper mapper = new ObjectMapper();
98 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
99 JsonNode root = mapper.readTree(request.getReader());
100 DescriptiveScope descriptiveScope = (DescriptiveScope)mapper.readValue(root.get("descriptiveScopeDictionaryData").toString(), DescriptiveScope.class);
101 GridData data = (GridData)mapper.readValue(root.get("descriptiveScopeDictionaryData").toString(), GridData.class);
102 String userId = root.get("loginId").textValue();
105 if(data.getAttributes().size() > 0){
106 for(Object attribute : data.getAttributes()){
107 if(attribute instanceof LinkedHashMap<?, ?>){
108 String key = ((LinkedHashMap<?, ?>) attribute).get("option").toString();
109 String value = ((LinkedHashMap<?, ?>) attribute).get("number").toString();
111 header = header + "AND";
113 header = header + key + ":";
114 header = header + value;
119 descriptiveScope.setSearch(header);
120 if(descriptiveScope.getId() == 0){
121 CheckDictionaryDuplicateEntries entry = new CheckDictionaryDuplicateEntries();
122 List<Object> duplicateData = entry.CheckDuplicateEntry(descriptiveScope.getScopeName(), "descriptiveScopeName", DescriptiveScope.class);
123 if(!duplicateData.isEmpty()){
124 duplicateflag = true;
126 descriptiveScope.setUserCreatedBy(this.getUserInfo(userId));
127 descriptiveScope.setUserModifiedBy(this.getUserInfo(userId));
128 descriptiveScopeDao.Save(descriptiveScope);
131 descriptiveScope.setUserModifiedBy(this.getUserInfo(userId));
132 descriptiveScopeDao.update(descriptiveScope);
134 response.setCharacterEncoding("UTF-8");
135 response.setContentType("application / json");
136 request.setCharacterEncoding("UTF-8");
138 PrintWriter out = response.getWriter();
139 String responseString = "";
141 responseString = "Duplicate";
143 responseString = mapper.writeValueAsString(this.descriptiveScopeDao.getDescriptiveScope());
145 JSONObject j = new JSONObject("{descriptiveScopeDictionaryDatas: " + responseString + "}");
147 out.write(j.toString());
152 response.setCharacterEncoding("UTF-8");
153 request.setCharacterEncoding("UTF-8");
154 PrintWriter out = response.getWriter();
155 out.write(e.getMessage());
160 @RequestMapping(value={"/descriptive_dictionary/remove_descriptiveScope.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
161 public ModelAndView removeDescriptiveDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception {
163 ObjectMapper mapper = new ObjectMapper();
164 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
165 JsonNode root = mapper.readTree(request.getReader());
166 DescriptiveScope descriptiveScope = (DescriptiveScope)mapper.readValue(root.get("data").toString(), DescriptiveScope.class);
167 descriptiveScopeDao.delete(descriptiveScope);
168 response.setCharacterEncoding("UTF-8");
169 response.setContentType("application / json");
170 request.setCharacterEncoding("UTF-8");
172 PrintWriter out = response.getWriter();
174 String responseString = mapper.writeValueAsString(this.descriptiveScopeDao.getDescriptiveScope());
175 JSONObject j = new JSONObject("{descriptiveScopeDictionaryDatas: " + responseString + "}");
176 out.write(j.toString());
181 System.out.println(e);
182 response.setCharacterEncoding("UTF-8");
183 request.setCharacterEncoding("UTF-8");
184 PrintWriter out = response.getWriter();
185 out.write(e.getMessage());