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.portal.service;
22 import java.util.ArrayList;
23 import java.util.List;
25 import org.openecomp.portalapp.portal.domain.EPUser;
26 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
27 import org.openecomp.portalapp.portal.transport.UserWithNameSurnameTitle;
28 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
29 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.context.annotation.EnableAspectJAutoProxy;
32 import org.springframework.stereotype.Service;
33 import org.springframework.transaction.annotation.Transactional;
35 import com.fasterxml.jackson.core.JsonProcessingException;
36 import com.fasterxml.jackson.databind.ObjectMapper;
38 @Service("searchService")
40 @org.springframework.context.annotation.Configuration
41 @EnableAspectJAutoProxy
43 public class SearchServiceImpl implements SearchService {
44 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SearchServiceImpl.class);
46 // TODO: the values below should be defined in other place
47 private static final int maxSizeOfSearchResult = 100;
50 UserService userService;
53 public String searchUsersInPhoneBook(String searchString) {
54 return searchUsersInFnTable(searchString);
58 public String searchUsersInFnTable(String searchString) {
59 String orgUserId = null;
60 List<String> tokens = EcompPortalUtils.parsingByRegularExpression(searchString, " ");
61 for (int i = 0; i < tokens.size(); i++) { // find userid if possible and remove it from tokens
62 if (tokens.get(i).matches(".*\\d+.*")) {
63 orgUserId = tokens.get(i);
67 while (tokens.size() > 2) { // we use no more then first 2 tokens (userId is removed, see above)
68 tokens.remove(tokens.size() - 1);
70 EPUser attrUser = new EPUser();
71 attrUser.setOrgUserId(orgUserId);
72 List<UserWithNameSurnameTitle> resultOfSearch = new ArrayList<UserWithNameSurnameTitle>(), resultOfAdditionalSearch = null;
73 if (tokens.size() == 2) {
74 attrUser.setFirstName(tokens.get(0));
75 attrUser.setLastName(tokens.get(1));
76 resultOfSearch = this.searchUsersByName(attrUser);
77 resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
78 resultOfSearch = this.removeWrongLastNames(resultOfSearch, tokens.get(1));
79 if (resultOfSearch.size() < maxSizeOfSearchResult) {
80 attrUser.setFirstName(tokens.get(1));
81 attrUser.setLastName(tokens.get(0));
82 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
83 resultOfAdditionalSearch = this.removeWrongFirstNames(resultOfAdditionalSearch, tokens.get(1));
84 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
86 } else if (tokens.size() == 1) {
87 attrUser.setFirstName(tokens.get(0));
88 resultOfSearch = this.searchUsersByName(attrUser);
89 resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
90 if (resultOfSearch.size() < maxSizeOfSearchResult) {
91 attrUser.setFirstName(null);
92 attrUser.setLastName(tokens.get(0));
93 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
94 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
96 } else if (orgUserId != null) {
97 resultOfSearch = this.searchUsersByUserId(attrUser);
99 if (resultOfAdditionalSearch != null) {
100 resultOfSearch.addAll(resultOfAdditionalSearch);
102 resultOfSearch = this.cutSearchResultToMaximumSize(resultOfSearch);
103 ObjectMapper mapper = new ObjectMapper();
104 String result = "[]";
106 result = mapper.writeValueAsString(resultOfSearch);
107 } catch (JsonProcessingException e) {
108 logger.error(EELFLoggerDelegate.errorLogger, "searchUsersInPhoneBook Exception = " + EcompPortalUtils.getStackTrace(e));
114 @SuppressWarnings("rawtypes")
115 public List<UserWithNameSurnameTitle> searchUsersByUserId(EPUser attrUser) {
116 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
118 List searchResult = this.userService.getUserByUserId(attrUser.getOrgUserId());
119 for (Object obj : searchResult) {
120 EPUser user = (EPUser) obj;
121 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
122 foundUsers.add(foundUser);
124 } catch (Exception e) {
125 logger.error(EELFLoggerDelegate.errorLogger, "searchInPhoneBookWithToken Exception = " + EcompPortalUtils.getStackTrace(e));
130 @SuppressWarnings("rawtypes")
131 public List<UserWithNameSurnameTitle> searchUsersByName(EPUser attrUser) {
132 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
134 List searchResult = this.userService.getUserByFirstLastName(attrUser.getFirstName(),attrUser.getLastName());
135 for (Object obj : searchResult) {
136 EPUser user = (EPUser) obj;
137 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
138 foundUsers.add(foundUser);
140 } catch (Exception e) {
141 logger.error(EELFLoggerDelegate.errorLogger, "searchInPhoneBookWithToken Exception = " + EcompPortalUtils.getStackTrace(e));
146 private List<UserWithNameSurnameTitle> removeWrongFirstNames(List<UserWithNameSurnameTitle> resultOfSearch, String firstName) {
147 firstName = firstName.toUpperCase();
148 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
149 UserWithNameSurnameTitle user = resultOfSearch.get(i);
150 if ((user.firstName == null) || !user.firstName.toUpperCase().startsWith(firstName)) {
151 resultOfSearch.remove(i);
154 return resultOfSearch;
157 private List<UserWithNameSurnameTitle> removeWrongLastNames(List<UserWithNameSurnameTitle> resultOfSearch, String lastName) {
158 lastName = lastName.toUpperCase();
159 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
160 UserWithNameSurnameTitle user = resultOfSearch.get(i);
161 if ((user.lastName == null) || !user.lastName.toUpperCase().startsWith(lastName)) {
162 resultOfSearch.remove(i);
165 return resultOfSearch;
168 private List<UserWithNameSurnameTitle> cutSearchResultToMaximumSize(List<UserWithNameSurnameTitle> resultOfSearch) {
169 for (int i = resultOfSearch.size() - 1; i >= maxSizeOfSearchResult; i--) {
170 resultOfSearch.remove(i);
172 return resultOfSearch;
176 @SuppressWarnings("rawtypes")
178 public EPUser searchUserByUserId(String orgUserId) {
179 List<EPUser> foundUsers = new ArrayList<EPUser>();
181 List searchResult = this.userService.getUserByUserId(orgUserId);
182 for (Object obj : searchResult) {
183 EPUser user = (EPUser) obj;
184 foundUsers.add(user);
186 } catch (Exception e) {
187 logger.error(EELFLoggerDelegate.errorLogger, "searchInPhoneBookWithToken Exception = " + EcompPortalUtils.getStackTrace(e));
190 return foundUsers.get(0);