2da1e6a6a189df32df1e4724b8d6e5e22de879bc
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * ECOMP Portal SDK
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
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.controller.core;
21
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;
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.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;
50
51 import com.fasterxml.jackson.databind.DeserializationFeature;
52 import com.fasterxml.jackson.databind.JsonNode;
53 import com.fasterxml.jackson.databind.ObjectMapper;
54
55 @Controller
56 @RequestMapping("/")
57 public class PostSearchController extends RestrictedBaseController {
58
59         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PostSearchController.class);
60
61         @SuppressWarnings("rawtypes")
62         private static List sortByList = null;
63
64         @Autowired
65         private PostSearchService postSearchService;
66
67         @Autowired
68         private LdapService ldapService;
69
70         @Autowired
71         private ProfileService profileService;
72
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>();
77
78                 ObjectMapper mapper = new ObjectMapper();
79                 try {
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());
87                 }
88
89                 return new ModelAndView(getViewName(), model);
90         }
91
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>();
96
97                 ObjectMapper mapper = new ObjectMapper();
98                 try {
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());
109                 }
110
111         }
112
113         @SuppressWarnings({ "unchecked", "rawtypes" })
114         private HashMap getExistingUsers() {
115                 HashMap existingUsers = new HashMap();
116
117                 // get the list of user ids in the system
118                 List<Profile> list = profileService.findAll();
119
120                 if (list != null) {
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);
128                         }
129                 }
130                 return existingUsers;
131         }
132
133         @RequestMapping(value = { "/post_search/search" }, method = RequestMethod.POST)
134         public ModelAndView search(HttpServletRequest request, HttpServletResponse response) throws Exception {
135                 try {
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);
141
142                         postSearchBean.setSearchResult(loadSearchResultData(request, postSearchBean));
143
144                         response.setCharacterEncoding("UTF-8");
145                         response.setContentType("application / json");
146                         request.setCharacterEncoding("UTF-8");
147
148                         PrintWriter out = response.getWriter();
149                         String responseString = mapper.writeValueAsString(postSearchBean);
150                         JSONObject j = new JSONObject("{postSearchBean: " + responseString + "}");
151
152                         out.write(j.toString());
153                 } catch (Exception ex) {
154                         logger.error(EELFLoggerDelegate.errorLogger, "search: failed to send search result" + ex.getMessage());
155                 }
156
157                 return null;
158         }
159
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"));
170                 } // if
171
172                 return sortByList;
173         } // getSortByList
174
175         private SearchResult loadSearchResultData(HttpServletRequest request, PostSearchBean searchCriteria)
176                         throws Exception {
177                 return ldapService.searchPost(searchCriteria.getUser(), searchCriteria.getSortBy1(),
178                                 searchCriteria.getSortBy2(), searchCriteria.getSortBy3(), searchCriteria.getPageNo(),
179                                 searchCriteria.getNewDataSize(), UserUtils.getUserSession(request).getId().intValue());
180         }
181
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 = "{}";
190                 try{
191                         postSearchService.process(request, postSearch);
192                         postSearch.setSearchResult(loadSearchResultData(request, postSearch));
193                 }catch(Exception e){
194                         errorMsg=e.getMessage();
195                         logger.error(EELFLoggerDelegate.errorLogger,"Exception occurred while performing PostSearchController.process. Details:", e);
196                 }       
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");
201
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+"}");
206
207                 out.write(j.toString());
208
209                 return null;
210         }
211 }