2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalapp.controller.core;
22 import java.io.PrintWriter;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
32 import org.json.JSONObject;
33 import org.openecomp.portalsdk.core.command.PostSearchBean;
34 import org.openecomp.portalsdk.core.command.support.SearchResult;
35 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
36 import org.openecomp.portalsdk.core.domain.Lookup;
37 import org.openecomp.portalsdk.core.domain.Profile;
38 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
39 import org.openecomp.portalsdk.core.service.LdapService;
40 import org.openecomp.portalsdk.core.service.PostSearchService;
41 import org.openecomp.portalsdk.core.service.ProfileService;
42 import org.openecomp.portalsdk.core.web.support.JsonMessage;
43 import org.openecomp.portalsdk.core.web.support.UserUtils;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.stereotype.Controller;
46 import org.springframework.web.bind.annotation.ModelAttribute;
47 import org.springframework.web.bind.annotation.RequestMapping;
48 import org.springframework.web.bind.annotation.RequestMethod;
49 import org.springframework.web.servlet.ModelAndView;
51 import com.fasterxml.jackson.databind.DeserializationFeature;
52 import com.fasterxml.jackson.databind.JsonNode;
53 import com.fasterxml.jackson.databind.ObjectMapper;
57 public class PostSearchController extends RestrictedBaseController {
59 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PostSearchController.class);
61 @SuppressWarnings("rawtypes")
62 private static List sortByList = null;
65 private PostSearchService postSearchService;
68 private LdapService ldapService;
71 private ProfileService profileService;
73 @RequestMapping(value = { "/post_search" }, method = RequestMethod.GET)
74 public ModelAndView welcome(HttpServletRequest request,
75 @ModelAttribute("postSearchBean") PostSearchBean postSearchBean) {
76 Map<String, Object> model = new HashMap<String, Object>();
78 ObjectMapper mapper = new ObjectMapper();
80 postSearchBean = new PostSearchBean();
81 model.put("profileList", mapper.writeValueAsString(postSearchBean.getSearchResult()));
82 model.put("postSearchBean", mapper.writeValueAsString(postSearchBean));
83 model.put("existingUsers", mapper.writeValueAsString(getExistingUsers()));
84 model.put("sortByList", mapper.writeValueAsString(getSortByList()));
85 } catch (Exception ex) {
86 logger.error(EELFLoggerDelegate.errorLogger, "welcome: failed to write JSON" + ex.getMessage());
89 return new ModelAndView(getViewName(), model);
92 @RequestMapping(value = { "/post_search_sample" }, method = RequestMethod.GET)
93 public void getPostSearchProfile(HttpServletRequest request, HttpServletResponse response,
94 @ModelAttribute("postSearchBean") PostSearchBean postSearchBean) {
95 Map<String, Object> model = new HashMap<String, Object>();
97 ObjectMapper mapper = new ObjectMapper();
99 postSearchBean = new PostSearchBean();
100 model.put("profileList", mapper.writeValueAsString(postSearchBean.getSearchResult()));
101 model.put("postSearchBean", mapper.writeValueAsString(postSearchBean));
102 model.put("existingUsers", mapper.writeValueAsString(getExistingUsers()));
103 model.put("sortByList", mapper.writeValueAsString(getSortByList()));
104 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
105 JSONObject j = new JSONObject(msg);
106 response.getWriter().write(j.toString());
107 } catch (Exception ex) {
108 logger.error(EELFLoggerDelegate.errorLogger, "getPostSearchProfile: failed to write JSON" + ex.getMessage());
113 @SuppressWarnings({ "unchecked", "rawtypes" })
114 private HashMap getExistingUsers() throws Exception {
115 HashMap existingUsers = new HashMap();
117 // get the list of user ids in the system
118 List<Profile> list = profileService.findAll();
121 Iterator<Profile> i = list.iterator();
122 while (i.hasNext()) {
123 Profile user = i.next();
124 String orgUserId = user.getOrgUserId(); // userid scalar
125 Long id = user.getId(); // id scalar
126 if (orgUserId != null)
127 existingUsers.put(orgUserId, id);
130 return existingUsers;
133 @RequestMapping(value = { "/post_search/search" }, method = RequestMethod.POST)
134 public ModelAndView search(HttpServletRequest request, HttpServletResponse response) throws Exception {
136 ObjectMapper mapper = new ObjectMapper();
137 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
138 JsonNode root = mapper.readTree(request.getReader());
139 PostSearchBean postSearchBean = mapper.readValue(root.get("postSearchBean").toString(),
140 PostSearchBean.class);
142 postSearchBean.setSearchResult(loadSearchResultData(request, postSearchBean));
144 response.setCharacterEncoding("UTF-8");
145 response.setContentType("application / json");
146 request.setCharacterEncoding("UTF-8");
148 PrintWriter out = response.getWriter();
149 String responseString = mapper.writeValueAsString(postSearchBean);
150 JSONObject j = new JSONObject("{postSearchBean: " + responseString + "}");
152 out.write(j.toString());
153 } catch (Exception ex) {
154 logger.error(EELFLoggerDelegate.errorLogger, "search: failed to send search result" + ex.getMessage());
160 @SuppressWarnings({ "unchecked", "rawtypes" })
161 public static List getSortByList() {
162 if (sortByList == null) {
163 sortByList = new ArrayList();
164 sortByList.add(new Lookup("Last Name", "last_name"));
165 sortByList.add(new Lookup("First Name", "first_name"));
166 sortByList.add(new Lookup("HRID", "hrid"));
167 sortByList.add(new Lookup("SBCID", "sbcid"));
168 sortByList.add(new Lookup("Organization", "org_code"));
169 sortByList.add(new Lookup("Email", "email"));
175 private SearchResult loadSearchResultData(HttpServletRequest request, PostSearchBean searchCriteria)
177 return ldapService.searchPost(searchCriteria.getUser(), searchCriteria.getSortBy1(),
178 searchCriteria.getSortBy2(), searchCriteria.getSortBy3(), searchCriteria.getPageNo(),
179 searchCriteria.getNewDataSize(), UserUtils.getUserSession(request).getId().intValue());
182 @RequestMapping(value = { "/post_search/process" }, method = RequestMethod.POST)
183 public ModelAndView process(HttpServletRequest request, HttpServletResponse response) throws Exception {
184 ObjectMapper mapper = new ObjectMapper();
185 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
186 mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
187 JsonNode root = mapper.readTree(request.getReader());
188 PostSearchBean postSearch = mapper.readValue(root.get("postSearchBean").toString(), PostSearchBean.class);
189 String errorMsg = "{}";
191 postSearchService.process(request, postSearch);
192 postSearch.setSearchResult(loadSearchResultData(request, postSearch));
194 errorMsg=e.getMessage();
195 logger.error(EELFLoggerDelegate.errorLogger,"Exception occurred while performing PostSearchController.process. Details:", e);
197 logger.info(EELFLoggerDelegate.auditLogger, "Import new user from webphone ");
198 response.setCharacterEncoding("UTF-8");
199 response.setContentType("application / json");
200 request.setCharacterEncoding("UTF-8");
202 PrintWriter out = response.getWriter();
203 String postSearchString = mapper.writeValueAsString(postSearch);
204 JSONObject j = new JSONObject("{postSearchBean: " + postSearchString + ",existingUsers: "
205 + mapper.writeValueAsString(getExistingUsers()) + ",errorMsg:"+errorMsg+"}");
207 out.write(j.toString());