Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / controller / DictionaryController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PAP-REST
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
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.openecomp.policy.pap.xacml.rest.controller;
22
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;
28 import java.util.Map;
29
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32
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;
52
53 import com.fasterxml.jackson.databind.DeserializationFeature;
54 import com.fasterxml.jackson.databind.JsonNode;
55 import com.fasterxml.jackson.databind.ObjectMapper;
56
57 @Controller
58 public class DictionaryController {
59         
60         private static final Log logger = LogFactory.getLog(DictionaryController.class);
61
62         @Autowired
63         AttributeDao attributeDao;
64
65         @Autowired
66         EcompNameDao ecompNameDao;
67         
68         @Autowired
69         UserInfoDao userInfoDao;
70         
71         @Autowired
72         CategoryDao categoryDao;
73         
74         
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")) {
79                                 return value;
80                         }
81                 }
82                 return null;    
83         }
84         
85         
86         public UserInfo getUserInfo(String loginId){
87                 UserInfo name = userInfoDao.getUserInfoByLoginId(loginId);
88                 return name;    
89         }
90
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){
93                 try{
94                         System.out.println();
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());
101                 }
102                 catch (Exception e){
103                         e.printStackTrace();
104                 }
105         }
106         
107         //Attribute Dictionary
108         @RequestMapping(value="/get_AttributeData", method= RequestMethod.GET , produces=MediaType.APPLICATION_JSON_VALUE)
109         public void getAttributeDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
110                 try{
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());
118                 }
119                 catch (Exception e){
120                         e.printStackTrace();
121                 }
122         }
123         
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{
126                 try {
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 = "";
135                         int counter = 0;
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();
140                                                 if(counter>0){
141                                                         userValue = userValue + ",";
142                                                 }
143                                                 userValue = userValue + key ;
144                                                 counter ++;
145                                         }
146                                 }
147                         }
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")){
153                                         a.setId(26);    
154                                 }else if(datatype.equalsIgnoreCase("integer")){
155                                         a.setId(12);    
156                                 }else if(datatype.equalsIgnoreCase("boolean")){
157                                         a.setId(18);    
158                                 }else if(datatype.equalsIgnoreCase("double")){
159                                         a.setId(25);    
160                                 }else if(datatype.equalsIgnoreCase("user")){
161                                         a.setId(29);    
162                                 }
163                                 attributeData.setDatatypeBean(a);
164                         }
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;
170                                 }else{
171                                         attributeData.setCategoryBean(this.getCategory());
172                                         attributeData.setUserCreatedBy(this.getUserInfo(userId));
173                                         attributeData.setUserModifiedBy(this.getUserInfo(userId));
174                                         attributeDao.Save(attributeData);
175                                 }
176                         }else{
177                                 attributeData.setUserModifiedBy(this.getUserInfo(userId));
178                                 attributeDao.update(attributeData); 
179                         } 
180                         response.setCharacterEncoding("UTF-8");
181                         response.setContentType("application / json");
182                         request.setCharacterEncoding("UTF-8");
183
184                         PrintWriter out = response.getWriter();
185                         String responseString = "";
186                         if(duplicateflag){
187                                 responseString = "Duplicate";
188                         }else{
189                                 responseString = mapper.writeValueAsString(this.attributeDao.getData());
190                         }
191                         JSONObject j = new JSONObject("{attributeDictionaryDatas: " + responseString + "}");
192
193                         out.write(j.toString());
194
195                         return null;
196                 }
197                 catch (Exception e){
198                         response.setCharacterEncoding("UTF-8");
199                         request.setCharacterEncoding("UTF-8");
200                         PrintWriter out = response.getWriter();
201                         out.write(e.getMessage());
202                 }
203                 return null;
204         }
205
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 {
208                 try{
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");
217
218                         PrintWriter out = response.getWriter();
219
220                         String responseString = mapper.writeValueAsString(this.attributeDao.getData());
221                         JSONObject j = new JSONObject("{attributeDictionaryDatas: " + responseString + "}");
222                         out.write(j.toString());
223
224                         return null;
225                 }
226                 catch (Exception e){
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());
232                 }
233                 return null;
234         }
235         
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");
240                 try{
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());
247                 }
248                 catch (Exception e){
249                         e.printStackTrace();
250                 }
251         }
252         
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");
256                 try{
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());
263                 }
264                 catch (Exception e){
265                         e.printStackTrace();
266                         logger.error("ERROR While callinge DAO: " + e.getMessage());
267                 }
268         }
269
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{
272                 try {
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;
287                                 }else{
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);
292                                 }
293                         }else{
294                                 ecompData.setUserModifiedBy(this.getUserInfo(userId));
295                                 ecompNameDao.update(ecompData); 
296                         } 
297                         response.setCharacterEncoding("UTF-8");
298                         response.setContentType("application / json");
299                         request.setCharacterEncoding("UTF-8");
300
301                         PrintWriter out = response.getWriter();
302                         String responseString = "";
303                         if(duplicateflag){
304                                 responseString = "Duplicate";
305                         }else{
306                                 responseString = mapper.writeValueAsString(this.ecompNameDao.getEcompName());
307                         }
308                         JSONObject j = new JSONObject("{ecompNameDictionaryDatas: " + responseString + "}");
309
310                         out.write(j.toString());
311
312                         return null;
313                 }
314                 catch (Exception e){
315                         response.setCharacterEncoding("UTF-8");
316                         request.setCharacterEncoding("UTF-8");
317                         PrintWriter out = response.getWriter();
318                         out.write(e.getMessage());
319                 }
320                 return null;
321         }
322
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 {
325                 try{
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");
334
335                         PrintWriter out = response.getWriter();
336
337                         String responseString = mapper.writeValueAsString(this.ecompNameDao.getEcompName());
338                         JSONObject j = new JSONObject("{ecompNameDictionaryDatas: " + responseString + "}");
339                         out.write(j.toString());
340
341                         return null;
342                 }
343                 catch (Exception e){
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());
349                 }
350                 return null;
351         }
352
353 }
354
355 class AttributeValues{
356         private ArrayList<Object> userDataTypeValues;
357
358         public ArrayList<Object> getUserDataTypeValues() {
359                 return userDataTypeValues;
360         }
361
362         public void setUserDataTypeValues(ArrayList<Object> userDataTypeValues) {
363                 this.userDataTypeValues = userDataTypeValues;
364         }
365 }
366