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.ArrayList;
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.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.json.JSONObject;
36 import org.openecomp.policy.pap.xacml.rest.util.JsonMessage;
37 import org.openecomp.policy.rest.dao.AttributeDao;
38 import org.openecomp.policy.rest.dao.CategoryDao;
39 import org.openecomp.policy.rest.dao.EcompNameDao;
40 import org.openecomp.policy.rest.dao.UserInfoDao;
41 import org.openecomp.policy.rest.jpa.Attribute;
42 import org.openecomp.policy.rest.jpa.Category;
43 import org.openecomp.policy.rest.jpa.Datatype;
44 import org.openecomp.policy.rest.jpa.EcompName;
45 import org.openecomp.policy.rest.jpa.UserInfo;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.http.MediaType;
48 import org.springframework.stereotype.Controller;
49 import org.springframework.web.bind.annotation.RequestMapping;
50 import org.springframework.web.bind.annotation.RequestMethod;
51 import org.springframework.web.servlet.ModelAndView;
53 import com.fasterxml.jackson.databind.DeserializationFeature;
54 import com.fasterxml.jackson.databind.JsonNode;
55 import com.fasterxml.jackson.databind.ObjectMapper;
58 public class DictionaryController {
60 private static final Log logger = LogFactory.getLog(DictionaryController.class);
63 AttributeDao attributeDao;
66 EcompNameDao ecompNameDao;
69 UserInfoDao userInfoDao;
72 CategoryDao categoryDao;
75 public Category getCategory(){
76 for (int i = 0; i < categoryDao.getCategoryListData().size() ; i++) {
77 Category value = categoryDao.getCategoryListData().get(i);
78 if (value.getShortName().equals("resource")) {
86 public UserInfo getUserInfo(String loginId){
87 UserInfo name = userInfoDao.getUserInfoByLoginId(loginId);
91 @RequestMapping(value={"/get_AttributeDatabyAttributeName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
92 public void getAttributeDictionaryEntityDatabyAttributeName(HttpServletRequest request, HttpServletResponse response){
95 Map<String, Object> model = new HashMap<String, Object>();
96 ObjectMapper mapper = new ObjectMapper();
97 model.put("attributeDictionaryDatas", mapper.writeValueAsString(attributeDao.getAttributeData()));
98 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
99 JSONObject j = new JSONObject(msg);
100 response.getWriter().write(j.toString());
107 //Attribute Dictionary
108 @RequestMapping(value="/get_AttributeData", method= RequestMethod.GET , produces=MediaType.APPLICATION_JSON_VALUE)
109 public void getAttributeDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
111 System.out.println();
112 Map<String, Object> model = new HashMap<String, Object>();
113 ObjectMapper mapper = new ObjectMapper();
114 model.put("attributeDictionaryDatas", mapper.writeValueAsString(attributeDao.getData()));
115 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
116 JSONObject j = new JSONObject(msg);
117 response.getWriter().write(j.toString());
124 @RequestMapping(value={"/attribute_dictionary/save_attribute.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
125 public ModelAndView saveAttributeDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{
127 boolean duplicateflag = false;
128 ObjectMapper mapper = new ObjectMapper();
129 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
130 JsonNode root = mapper.readTree(request.getReader());
131 Attribute attributeData = (Attribute)mapper.readValue(root.get("attributeDictionaryData").toString(), Attribute.class);
132 AttributeValues attributeValueData = (AttributeValues)mapper.readValue(root.get("attributeDictionaryData").toString(), AttributeValues.class);
133 String userId = root.get("loginId").textValue();
134 String userValue = "";
136 if(attributeValueData.getUserDataTypeValues().size() > 0){
137 for(Object attribute : attributeValueData.getUserDataTypeValues()){
138 if(attribute instanceof LinkedHashMap<?, ?>){
139 String key = ((LinkedHashMap<?, ?>) attribute).get("attributeValues").toString();
141 userValue = userValue + ",";
143 userValue = userValue + key ;
148 attributeData.setAttributeValue(userValue);
149 if(attributeData.getDatatypeBean().getShortName() != null){
150 String datatype = attributeData.getDatatypeBean().getShortName();
151 Datatype a = new Datatype();
152 if(datatype.equalsIgnoreCase("string")){
154 }else if(datatype.equalsIgnoreCase("integer")){
156 }else if(datatype.equalsIgnoreCase("boolean")){
158 }else if(datatype.equalsIgnoreCase("double")){
160 }else if(datatype.equalsIgnoreCase("user")){
163 attributeData.setDatatypeBean(a);
165 if(attributeData.getId() == 0){
166 CheckDictionaryDuplicateEntries entry = new CheckDictionaryDuplicateEntries();
167 List<Object> duplicateData = entry.CheckDuplicateEntry(attributeData.getXacmlId(), "xacmlId", Attribute.class);
168 if(!duplicateData.isEmpty()){
169 duplicateflag = true;
171 attributeData.setCategoryBean(this.getCategory());
172 attributeData.setUserCreatedBy(this.getUserInfo(userId));
173 attributeData.setUserModifiedBy(this.getUserInfo(userId));
174 attributeDao.Save(attributeData);
177 attributeData.setUserModifiedBy(this.getUserInfo(userId));
178 attributeDao.update(attributeData);
180 response.setCharacterEncoding("UTF-8");
181 response.setContentType("application / json");
182 request.setCharacterEncoding("UTF-8");
184 PrintWriter out = response.getWriter();
185 String responseString = "";
187 responseString = "Duplicate";
189 responseString = mapper.writeValueAsString(this.attributeDao.getData());
191 JSONObject j = new JSONObject("{attributeDictionaryDatas: " + responseString + "}");
193 out.write(j.toString());
198 response.setCharacterEncoding("UTF-8");
199 request.setCharacterEncoding("UTF-8");
200 PrintWriter out = response.getWriter();
201 out.write(e.getMessage());
206 @RequestMapping(value={"/attribute_dictionary/remove_attribute.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
207 public ModelAndView removeAttributeDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception {
209 ObjectMapper mapper = new ObjectMapper();
210 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
211 JsonNode root = mapper.readTree(request.getReader());
212 Attribute attributeData = (Attribute)mapper.readValue(root.get("data").toString(), Attribute.class);
213 attributeDao.delete(attributeData);
214 response.setCharacterEncoding("UTF-8");
215 response.setContentType("application / json");
216 request.setCharacterEncoding("UTF-8");
218 PrintWriter out = response.getWriter();
220 String responseString = mapper.writeValueAsString(this.attributeDao.getData());
221 JSONObject j = new JSONObject("{attributeDictionaryDatas: " + responseString + "}");
222 out.write(j.toString());
227 System.out.println(e);
228 response.setCharacterEncoding("UTF-8");
229 request.setCharacterEncoding("UTF-8");
230 PrintWriter out = response.getWriter();
231 out.write(e.getMessage());
236 //EcompName Dictionary
237 @RequestMapping(value={"/get_EcompNameDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
238 public void getEcompNameDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response){
239 logger.info("get_EcompNameDataByName is called");
241 Map<String, Object> model = new HashMap<String, Object>();
242 ObjectMapper mapper = new ObjectMapper();
243 model.put("ecompNameDictionaryDatas", mapper.writeValueAsString(ecompNameDao.getEcompNameDataByName()));
244 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
245 JSONObject j = new JSONObject(msg);
246 response.getWriter().write(j.toString());
253 @RequestMapping(value={"/get_EcompNameData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
254 public void getEcompNameDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
255 logger.info("get_EcompNameData is called");
257 Map<String, Object> model = new HashMap<String, Object>();
258 ObjectMapper mapper = new ObjectMapper();
259 model.put("ecompNameDictionaryDatas", mapper.writeValueAsString(ecompNameDao.getEcompName()));
260 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
261 JSONObject j = new JSONObject(msg);
262 response.getWriter().write(j.toString());
266 logger.error("ERROR While callinge DAO: " + e.getMessage());
270 @RequestMapping(value={"/ecomp_dictionary/save_ecompName.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
271 public ModelAndView saveEcompDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{
273 boolean duplicateflag = false;
274 System.out.println("DictionaryController: saveEcompDictionary() is called");
275 logger.debug("DictionaryController: saveEcompDictionary() is called");
276 ObjectMapper mapper = new ObjectMapper();
277 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
278 JsonNode root = mapper.readTree(request.getReader());
279 EcompName ecompData = (EcompName)mapper.readValue(root.get("ecompNameDictionaryData").toString(), EcompName.class);
280 String userId = root.get("loginId").textValue();
281 System.out.println("the userId from the ecomp portal is: " + userId);
282 if(ecompData.getId() == 0){
283 CheckDictionaryDuplicateEntries entry = new CheckDictionaryDuplicateEntries();
284 List<Object> duplicateData = entry.CheckDuplicateEntry(ecompData.getEcompName(), "ecompName", EcompName.class);
285 if(!duplicateData.isEmpty()){
286 duplicateflag = true;
288 ecompData.setUserCreatedBy(getUserInfo(userId));
289 ecompData.setUserModifiedBy(getUserInfo(userId));
290 System.out.println("DictionaryController: got the user info now about to call Save() method on ecompNamedao");
291 ecompNameDao.Save(ecompData);
294 ecompData.setUserModifiedBy(this.getUserInfo(userId));
295 ecompNameDao.update(ecompData);
297 response.setCharacterEncoding("UTF-8");
298 response.setContentType("application / json");
299 request.setCharacterEncoding("UTF-8");
301 PrintWriter out = response.getWriter();
302 String responseString = "";
304 responseString = "Duplicate";
306 responseString = mapper.writeValueAsString(this.ecompNameDao.getEcompName());
308 JSONObject j = new JSONObject("{ecompNameDictionaryDatas: " + responseString + "}");
310 out.write(j.toString());
315 response.setCharacterEncoding("UTF-8");
316 request.setCharacterEncoding("UTF-8");
317 PrintWriter out = response.getWriter();
318 out.write(e.getMessage());
323 @RequestMapping(value={"/ecomp_dictionary/remove_ecomp.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
324 public ModelAndView removeEcompDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception {
326 ObjectMapper mapper = new ObjectMapper();
327 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
328 JsonNode root = mapper.readTree(request.getReader());
329 EcompName ecompData = (EcompName)mapper.readValue(root.get("data").toString(), EcompName.class);
330 ecompNameDao.delete(ecompData);
331 response.setCharacterEncoding("UTF-8");
332 response.setContentType("application / json");
333 request.setCharacterEncoding("UTF-8");
335 PrintWriter out = response.getWriter();
337 String responseString = mapper.writeValueAsString(this.ecompNameDao.getEcompName());
338 JSONObject j = new JSONObject("{ecompNameDictionaryDatas: " + responseString + "}");
339 out.write(j.toString());
344 System.out.println(e);
345 response.setCharacterEncoding("UTF-8");
346 request.setCharacterEncoding("UTF-8");
347 PrintWriter out = response.getWriter();
348 out.write(e.getMessage());
355 class AttributeValues{
356 private ArrayList<Object> userDataTypeValues;
358 public ArrayList<Object> getUserDataTypeValues() {
359 return userDataTypeValues;
362 public void setUserDataTypeValues(ArrayList<Object> userDataTypeValues) {
363 this.userDataTypeValues = userDataTypeValues;