d1ced9b5e2431026452e99026e93967d551a9bfe
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the “License”);
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.controller.core;
39
40 import java.io.PrintWriter;
41 import java.util.ArrayList;
42 import java.util.HashMap;
43 import java.util.Iterator;
44 import java.util.List;
45 import java.util.Map;
46
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
49
50 import org.json.JSONObject;
51 import org.onap.portalsdk.core.command.PostSearchBean;
52 import org.onap.portalsdk.core.command.support.SearchResult;
53 import org.onap.portalsdk.core.controller.RestrictedBaseController;
54 import org.onap.portalsdk.core.domain.Lookup;
55 import org.onap.portalsdk.core.domain.Profile;
56 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
57 import org.onap.portalsdk.core.service.LdapService;
58 import org.onap.portalsdk.core.service.PostSearchService;
59 import org.onap.portalsdk.core.service.ProfileService;
60 import org.onap.portalsdk.core.web.support.JsonMessage;
61 import org.onap.portalsdk.core.web.support.UserUtils;
62 import org.springframework.beans.factory.annotation.Autowired;
63 import org.springframework.stereotype.Controller;
64 import org.springframework.web.bind.annotation.ModelAttribute;
65 import org.springframework.web.bind.annotation.RequestMapping;
66 import org.springframework.web.bind.annotation.RequestMethod;
67 import org.springframework.web.servlet.ModelAndView;
68
69 import com.fasterxml.jackson.databind.DeserializationFeature;
70 import com.fasterxml.jackson.databind.JsonNode;
71 import com.fasterxml.jackson.databind.ObjectMapper;
72
73 @Controller
74 @RequestMapping("/")
75 public class PostSearchController extends RestrictedBaseController {
76
77         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PostSearchController.class);
78
79         @SuppressWarnings("rawtypes")
80         private static List sortByList = null;
81
82         @Autowired
83         private PostSearchService postSearchService;
84
85         @Autowired
86         private LdapService ldapService;
87
88         @Autowired
89         private ProfileService profileService;
90
91         @RequestMapping(value = { "/post_search" }, method = RequestMethod.GET)
92         public ModelAndView welcome(HttpServletRequest request,
93                         @ModelAttribute("postSearchBean") PostSearchBean postSearchBean) {
94                 Map<String, Object> model = new HashMap<String, Object>();
95
96                 ObjectMapper mapper = new ObjectMapper();
97                 try {
98                         postSearchBean = new PostSearchBean();
99                         model.put("profileList", mapper.writeValueAsString(postSearchBean.getSearchResult()));
100                         model.put("postSearchBean", mapper.writeValueAsString(postSearchBean));
101                         model.put("existingUsers", mapper.writeValueAsString(getExistingUsers()));
102                         model.put("sortByList", mapper.writeValueAsString(getSortByList()));
103                 } catch (Exception ex) {
104                         logger.error(EELFLoggerDelegate.errorLogger, "welcome: failed to write JSON" + ex.getMessage());
105                 }
106
107                 return new ModelAndView(getViewName(), model);
108         }
109
110         @RequestMapping(value = { "/post_search_sample" }, method = RequestMethod.GET)
111         public void getPostSearchProfile(HttpServletRequest request, HttpServletResponse response,
112                         @ModelAttribute("postSearchBean") PostSearchBean postSearchBean) {
113                 Map<String, Object> model = new HashMap<String, Object>();
114
115                 ObjectMapper mapper = new ObjectMapper();
116                 try {
117                         postSearchBean = new PostSearchBean();
118                         model.put("profileList", mapper.writeValueAsString(postSearchBean.getSearchResult()));
119                         model.put("postSearchBean", mapper.writeValueAsString(postSearchBean));
120                         model.put("existingUsers", mapper.writeValueAsString(getExistingUsers()));
121                         model.put("sortByList", mapper.writeValueAsString(getSortByList()));
122                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
123                         JSONObject j = new JSONObject(msg);
124                         response.getWriter().write(j.toString());
125                 } catch (Exception ex) {
126                         logger.error(EELFLoggerDelegate.errorLogger, "getPostSearchProfile: failed to write JSON" + ex.getMessage());
127                 }
128
129         }
130
131         @SuppressWarnings({ "unchecked", "rawtypes" })
132         private HashMap getExistingUsers() throws Exception {
133                 HashMap existingUsers = new HashMap();
134
135                 // get the list of user ids in the system
136                 List<Profile> list = profileService.findAll();
137
138                 if (list != null) {
139                         Iterator<Profile> i = list.iterator();
140                         while (i.hasNext()) {
141                                 Profile user = i.next();
142                                 String orgUserId = user.getOrgUserId(); // userid scalar
143                                 Long id = user.getId(); // id scalar
144                                 if (orgUserId != null)
145                                         existingUsers.put(orgUserId, id);
146                         }
147                 }
148                 return existingUsers;
149         }
150
151         @RequestMapping(value = { "/post_search/search" }, method = RequestMethod.POST)
152         public ModelAndView search(HttpServletRequest request, HttpServletResponse response) throws Exception {
153                 try {
154                         ObjectMapper mapper = new ObjectMapper();
155                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
156                         JsonNode root = mapper.readTree(request.getReader());
157                         PostSearchBean postSearchBean = mapper.readValue(root.get("postSearchBean").toString(),
158                                         PostSearchBean.class);
159
160                         postSearchBean.setSearchResult(loadSearchResultData(request, postSearchBean));
161
162                         response.setCharacterEncoding("UTF-8");
163                         response.setContentType("application / json");
164                         request.setCharacterEncoding("UTF-8");
165
166                         PrintWriter out = response.getWriter();
167                         String responseString = mapper.writeValueAsString(postSearchBean);
168                         JSONObject j = new JSONObject("{postSearchBean: " + responseString + "}");
169
170                         out.write(j.toString());
171                 } catch (Exception ex) {
172                         logger.error(EELFLoggerDelegate.errorLogger, "search: failed to send search result" + ex.getMessage());
173                 }
174
175                 return null;
176         }
177
178         @SuppressWarnings({ "unchecked", "rawtypes" })
179         public static List getSortByList() {
180                 if (sortByList == null) {
181                         sortByList = new ArrayList();
182                         sortByList.add(new Lookup("Last Name", "last_name"));
183                         sortByList.add(new Lookup("First Name", "first_name"));
184                         sortByList.add(new Lookup("HRID", "hrid"));
185                         sortByList.add(new Lookup("SBCID", "sbcid"));
186                         sortByList.add(new Lookup("Organization", "org_code"));
187                         sortByList.add(new Lookup("Email", "email"));
188                 } // if
189
190                 return sortByList;
191         } // getSortByList
192
193         private SearchResult loadSearchResultData(HttpServletRequest request, PostSearchBean searchCriteria)
194                         throws Exception {
195                 return ldapService.searchPost(searchCriteria.getUser(), searchCriteria.getSortBy1(),
196                                 searchCriteria.getSortBy2(), searchCriteria.getSortBy3(), searchCriteria.getPageNo(),
197                                 searchCriteria.getNewDataSize(), UserUtils.getUserSession(request).getId().intValue());
198         }
199
200         @RequestMapping(value = { "/post_search/process" }, method = RequestMethod.POST)
201         public ModelAndView process(HttpServletRequest request, HttpServletResponse response) throws Exception {
202                 ObjectMapper mapper = new ObjectMapper();
203                 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
204                 mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
205                 JsonNode root = mapper.readTree(request.getReader());
206                 PostSearchBean postSearch = mapper.readValue(root.get("postSearchBean").toString(), PostSearchBean.class);
207                 String errorMsg = "{}";
208                 try{
209                         postSearchService.process(request, postSearch);
210                         postSearch.setSearchResult(loadSearchResultData(request, postSearch));
211                 }catch(Exception e){
212                         errorMsg=e.getMessage();
213                         logger.error(EELFLoggerDelegate.errorLogger,"Exception occurred while performing PostSearchController.process. Details:", e);
214                 }       
215                 logger.info(EELFLoggerDelegate.auditLogger, "Import new user from webphone ");
216                 response.setCharacterEncoding("UTF-8");
217                 response.setContentType("application / json");
218                 request.setCharacterEncoding("UTF-8");
219
220                 PrintWriter out = response.getWriter();
221                 String postSearchString = mapper.writeValueAsString(postSearch);
222                 JSONObject j = new JSONObject("{postSearchBean: " + postSearchString + ",existingUsers: "
223                                 + mapper.writeValueAsString(getExistingUsers()) + ",errorMsg:"+errorMsg+"}");
224
225                 out.write(j.toString());
226
227                 return null;
228         }
229 }