Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / controller / DescriptiveDictionaryController.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.HashMap;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
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;
44
45 import com.fasterxml.jackson.databind.DeserializationFeature;
46 import com.fasterxml.jackson.databind.JsonNode;
47 import com.fasterxml.jackson.databind.ObjectMapper;
48
49 @Controller
50 public class DescriptiveDictionaryController {
51
52         @Autowired
53         DescriptiveScopeDao descriptiveScopeDao;
54         
55         @Autowired
56         UserInfoDao userInfoDao;
57         
58         public UserInfo getUserInfo(String loginId){
59                 UserInfo name = userInfoDao.getUserInfoByLoginId(loginId);
60                 return name;    
61         }
62
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){
65                 try{
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());
72                 }
73                 catch (Exception e){
74                         e.printStackTrace();
75                 }
76         }
77         
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){
80                 try{
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());
87                 }
88                 catch (Exception e){
89                         e.printStackTrace();
90                 }
91         }
92         
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{
95                 try {
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();
103                         String header = "";
104                         int counter = 0;
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();
110                                                 if(counter>0){
111                                                         header = header + "AND";
112                                                 }
113                                                 header = header + key + ":";
114                                                 header = header + value;
115                                                 counter ++;
116                                         }
117                                 }
118                         }
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;
125                                 }else{
126                                         descriptiveScope.setUserCreatedBy(this.getUserInfo(userId));
127                                         descriptiveScope.setUserModifiedBy(this.getUserInfo(userId));
128                                         descriptiveScopeDao.Save(descriptiveScope);
129                                 }
130                         }else{
131                                 descriptiveScope.setUserModifiedBy(this.getUserInfo(userId));
132                                 descriptiveScopeDao.update(descriptiveScope); 
133                         } 
134                         response.setCharacterEncoding("UTF-8");
135                         response.setContentType("application / json");
136                         request.setCharacterEncoding("UTF-8");
137
138                         PrintWriter out = response.getWriter();
139                         String responseString = "";
140                         if(duplicateflag){
141                                 responseString = "Duplicate";
142                         }else{
143                                 responseString =  mapper.writeValueAsString(this.descriptiveScopeDao.getDescriptiveScope());
144                         }
145                         JSONObject j = new JSONObject("{descriptiveScopeDictionaryDatas: " + responseString + "}");
146
147                         out.write(j.toString());
148
149                         return null;
150                 }
151                 catch (Exception e){
152                         response.setCharacterEncoding("UTF-8");
153                         request.setCharacterEncoding("UTF-8");
154                         PrintWriter out = response.getWriter();
155                         out.write(e.getMessage());
156                 }
157                 return null;
158         }
159
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 {
162                 try{
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");
171
172                         PrintWriter out = response.getWriter();
173
174                         String responseString = mapper.writeValueAsString(this.descriptiveScopeDao.getDescriptiveScope());
175                         JSONObject j = new JSONObject("{descriptiveScopeDictionaryDatas: " + responseString + "}");
176                         out.write(j.toString());
177
178                         return null;
179                 }
180                 catch (Exception e){
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());
186                 }
187                 return null;
188         }
189 }
190