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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalapp.controller.core;
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;
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
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;
69 import com.fasterxml.jackson.databind.DeserializationFeature;
70 import com.fasterxml.jackson.databind.JsonNode;
71 import com.fasterxml.jackson.databind.ObjectMapper;
75 public class PostSearchController extends RestrictedBaseController {
77 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PostSearchController.class);
79 @SuppressWarnings("rawtypes")
80 private static List sortByList = null;
83 private PostSearchService postSearchService;
86 private LdapService ldapService;
89 private ProfileService profileService;
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>();
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.getMessage());
107 return new ModelAndView(getViewName(), model);
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>();
115 ObjectMapper mapper = new ObjectMapper();
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());
131 @SuppressWarnings({ "unchecked", "rawtypes" })
132 private HashMap getExistingUsers() throws Exception {
133 HashMap existingUsers = new HashMap();
135 // get the list of user ids in the system
136 List<Profile> list = profileService.findAll();
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);
148 return existingUsers;
151 @RequestMapping(value = { "/post_search/search" }, method = RequestMethod.POST)
152 public ModelAndView search(HttpServletRequest request, HttpServletResponse response) throws Exception {
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);
160 postSearchBean.setSearchResult(loadSearchResultData(request, postSearchBean));
162 response.setCharacterEncoding("UTF-8");
163 response.setContentType("application / json");
164 request.setCharacterEncoding("UTF-8");
166 PrintWriter out = response.getWriter();
167 String responseString = mapper.writeValueAsString(postSearchBean);
168 JSONObject j = new JSONObject("{postSearchBean: " + responseString + "}");
170 out.write(j.toString());
171 } catch (Exception ex) {
172 logger.error(EELFLoggerDelegate.errorLogger, "search: failed to send search result" + ex.getMessage());
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"));
193 private SearchResult loadSearchResultData(HttpServletRequest request, PostSearchBean searchCriteria)
195 return ldapService.searchPost(searchCriteria.getUser(), searchCriteria.getSortBy1(),
196 searchCriteria.getSortBy2(), searchCriteria.getSortBy3(), searchCriteria.getPageNo(),
197 searchCriteria.getNewDataSize(), UserUtils.getUserSession(request).getId().intValue());
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 = "{}";
209 postSearchService.process(request, postSearch);
210 postSearch.setSearchResult(loadSearchResultData(request, postSearch));
212 errorMsg=e.getMessage();
213 logger.error(EELFLoggerDelegate.errorLogger,"Exception occurred while performing PostSearchController.process. Details:", e);
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");
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+"}");
225 out.write(j.toString());