2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.controller.core;
40 import java.io.IOException;
41 import java.io.PrintWriter;
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.Iterator;
45 import java.util.List;
48 import javax.naming.NamingException;
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
52 import org.json.JSONObject;
53 import org.onap.portalsdk.core.command.PostSearchBean;
54 import org.onap.portalsdk.core.command.support.SearchResult;
55 import org.onap.portalsdk.core.controller.RestrictedBaseController;
56 import org.onap.portalsdk.core.domain.Lookup;
57 import org.onap.portalsdk.core.domain.Profile;
58 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
59 import org.onap.portalsdk.core.service.LdapService;
60 import org.onap.portalsdk.core.service.PostSearchService;
61 import org.onap.portalsdk.core.service.ProfileService;
62 import org.onap.portalsdk.core.web.support.JsonMessage;
63 import org.onap.portalsdk.core.web.support.UserUtils;
64 import org.springframework.beans.factory.annotation.Autowired;
65 import org.springframework.stereotype.Controller;
66 import org.springframework.web.bind.annotation.ModelAttribute;
67 import org.springframework.web.bind.annotation.RequestMapping;
68 import org.springframework.web.bind.annotation.RequestMethod;
69 import org.springframework.web.servlet.ModelAndView;
71 import com.fasterxml.jackson.databind.DeserializationFeature;
72 import com.fasterxml.jackson.databind.JsonNode;
73 import com.fasterxml.jackson.databind.ObjectMapper;
77 public class PostSearchController extends RestrictedBaseController {
79 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PostSearchController.class);
81 @SuppressWarnings("rawtypes")
82 private static List sortByList = null;
85 private PostSearchService postSearchService;
88 private LdapService ldapService;
91 private ProfileService profileService;
93 @RequestMapping(value = { "/post_search" }, method = RequestMethod.GET)
94 public ModelAndView welcome(@ModelAttribute("postSearchBean") PostSearchBean postSearchBean) {
95 Map<String, Object> model = new HashMap<>();
96 ObjectMapper mapper = new ObjectMapper();
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);
107 return new ModelAndView(getViewName(), model);
110 @RequestMapping(value = { "/post_search_sample" }, method = RequestMethod.GET)
111 public void getPostSearchProfile(HttpServletResponse response,
112 @ModelAttribute("postSearchBean") PostSearchBean postSearchBean) {
113 Map<String, Object> model = new HashMap<>();
114 ObjectMapper mapper = new ObjectMapper();
116 postSearchBean = new PostSearchBean();
117 model.put("profileList", mapper.writeValueAsString(postSearchBean.getSearchResult()));
118 model.put("postSearchBean", mapper.writeValueAsString(postSearchBean));
119 model.put("existingUsers", mapper.writeValueAsString(getExistingUsers()));
120 model.put("sortByList", mapper.writeValueAsString(getSortByList()));
121 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
122 JSONObject j = new JSONObject(msg);
123 response.getWriter().write(j.toString());
124 } catch (Exception ex) {
125 logger.error(EELFLoggerDelegate.errorLogger, "getPostSearchProfile: failed to write JSON", ex);
129 @SuppressWarnings({ "unchecked", "rawtypes" })
130 private HashMap getExistingUsers() throws IOException {
131 HashMap existingUsers = new HashMap();
133 // get the list of user ids in the system
134 List<Profile> list = profileService.findAll();
137 Iterator<Profile> i = list.iterator();
138 while (i.hasNext()) {
139 Profile user = i.next();
140 String orgUserId = user.getOrgUserId(); // userid scalar
141 Long id = user.getId(); // id scalar
142 if (orgUserId != null)
143 existingUsers.put(orgUserId, id);
146 return existingUsers;
149 @RequestMapping(value = { "/post_search/search" }, method = RequestMethod.POST)
150 public ModelAndView search(HttpServletRequest request, HttpServletResponse response) {
152 ObjectMapper mapper = new ObjectMapper();
153 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
154 JsonNode root = mapper.readTree(request.getReader());
155 PostSearchBean postSearchBean = mapper.readValue(root.get("postSearchBean").toString(),
156 PostSearchBean.class);
158 postSearchBean.setSearchResult(loadSearchResultData(request, postSearchBean));
160 response.setCharacterEncoding("UTF-8");
161 response.setContentType("application / json");
162 request.setCharacterEncoding("UTF-8");
164 PrintWriter out = response.getWriter();
165 String responseString = mapper.writeValueAsString(postSearchBean);
166 JSONObject j = new JSONObject("{postSearchBean: " + responseString + "}");
168 out.write(j.toString());
169 } catch (Exception ex) {
170 logger.error(EELFLoggerDelegate.errorLogger, "search: failed to send search result", ex);
176 @SuppressWarnings({ "unchecked", "rawtypes" })
177 public static List getSortByList() {
178 if (sortByList == null) {
179 sortByList = new ArrayList();
180 sortByList.add(new Lookup("Last Name", "last_name"));
181 sortByList.add(new Lookup("First Name", "first_name"));
182 sortByList.add(new Lookup("HRID", "hrid"));
183 sortByList.add(new Lookup("SBCID", "sbcid"));
184 sortByList.add(new Lookup("Organization", "org_code"));
185 sortByList.add(new Lookup("Email", "email"));
191 private SearchResult loadSearchResultData(HttpServletRequest request, PostSearchBean searchCriteria)
192 throws NamingException {
193 return ldapService.searchPost(searchCriteria.getUser(), searchCriteria.getSortBy1(),
194 searchCriteria.getSortBy2(), searchCriteria.getSortBy3(), searchCriteria.getPageNo(),
195 searchCriteria.getNewDataSize(), UserUtils.getUserSession(request).getId().intValue());
198 @RequestMapping(value = { "/post_search/process" }, method = RequestMethod.POST)
199 public ModelAndView process(HttpServletRequest request, HttpServletResponse response) throws Exception {
200 ObjectMapper mapper = new ObjectMapper();
201 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
202 mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
203 JsonNode root = mapper.readTree(request.getReader());
204 PostSearchBean postSearch = mapper.readValue(root.get("postSearchBean").toString(), PostSearchBean.class);
205 String errorMsg = "{}";
207 postSearchService.process(request, postSearch);
208 postSearch.setSearchResult(loadSearchResultData(request, postSearch));
209 } catch (Exception e) {
210 errorMsg = e.getMessage();
211 logger.error(EELFLoggerDelegate.errorLogger,
212 "Exception occurred while performing PostSearchController.process. Details:", e);
214 logger.info(EELFLoggerDelegate.auditLogger, "Import new user from webphone ");
215 response.setCharacterEncoding("UTF-8");
216 response.setContentType("application / json");
217 request.setCharacterEncoding("UTF-8");
219 PrintWriter out = response.getWriter();
220 String postSearchString = mapper.writeValueAsString(postSearch);
221 JSONObject j = new JSONObject("{postSearchBean: " + postSearchString + ",existingUsers: "
222 + mapper.writeValueAsString(getExistingUsers()) + ",errorMsg:" + errorMsg + "}");
224 out.write(j.toString());