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.openecomp.portalapp.portal.service;
40 import java.util.ArrayList;
41 import java.util.List;
43 import org.openecomp.portalapp.portal.domain.EPUser;
44 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
45 import org.openecomp.portalapp.portal.transport.UserWithNameSurnameTitle;
46 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
47 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.context.annotation.EnableAspectJAutoProxy;
50 import org.springframework.stereotype.Service;
51 import org.springframework.transaction.annotation.Transactional;
53 import com.fasterxml.jackson.core.JsonProcessingException;
54 import com.fasterxml.jackson.databind.ObjectMapper;
56 @Service("searchService")
58 @org.springframework.context.annotation.Configuration
59 @EnableAspectJAutoProxy
61 public class SearchServiceImpl implements SearchService {
62 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SearchServiceImpl.class);
64 // TODO: the values below should be defined in other place
65 private static final int maxSizeOfSearchResult = 100;
68 UserService userService;
71 public String searchUsersInPhoneBook(String searchString) {
72 return searchUsersInFnTable(searchString);
76 public String searchUsersInFnTable(String searchString) {
77 String orgUserId = null;
78 List<String> tokens = EcompPortalUtils.parsingByRegularExpression(searchString, " ");
79 for (int i = 0; i < tokens.size(); i++) { // find userid if possible and remove it from tokens
80 if (tokens.get(i).matches(".*\\d+.*")) {
81 orgUserId = tokens.get(i);
85 while (tokens.size() > 2) { // we use no more then first 2 tokens (userId is removed, see above)
86 tokens.remove(tokens.size() - 1);
88 EPUser attrUser = new EPUser();
89 attrUser.setOrgUserId(orgUserId);
90 List<UserWithNameSurnameTitle> resultOfSearch = new ArrayList<UserWithNameSurnameTitle>(), resultOfAdditionalSearch = null;
91 if (tokens.size() == 2) {
92 attrUser.setFirstName(tokens.get(0));
93 attrUser.setLastName(tokens.get(1));
94 resultOfSearch = this.searchUsersByName(attrUser);
95 resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
96 resultOfSearch = this.removeWrongLastNames(resultOfSearch, tokens.get(1));
97 if (resultOfSearch.size() < maxSizeOfSearchResult) {
98 attrUser.setFirstName(tokens.get(1));
99 attrUser.setLastName(tokens.get(0));
100 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
101 resultOfAdditionalSearch = this.removeWrongFirstNames(resultOfAdditionalSearch, tokens.get(1));
102 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
104 } else if (tokens.size() == 1) {
105 attrUser.setFirstName(tokens.get(0));
106 resultOfSearch = this.searchUsersByName(attrUser);
107 resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
108 if (resultOfSearch.size() < maxSizeOfSearchResult) {
109 attrUser.setFirstName(null);
110 attrUser.setLastName(tokens.get(0));
111 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
112 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
114 } else if (orgUserId != null) {
115 resultOfSearch = this.searchUsersByUserId(attrUser);
117 if (resultOfAdditionalSearch != null) {
118 resultOfSearch.addAll(resultOfAdditionalSearch);
120 resultOfSearch = this.cutSearchResultToMaximumSize(resultOfSearch);
121 ObjectMapper mapper = new ObjectMapper();
122 String result = "[]";
124 result = mapper.writeValueAsString(resultOfSearch);
125 } catch (JsonProcessingException e) {
126 logger.error(EELFLoggerDelegate.errorLogger, "searchUsersInFnTable failed", e);
132 @SuppressWarnings("rawtypes")
133 public List<UserWithNameSurnameTitle> searchUsersByUserId(EPUser attrUser) {
134 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
136 List searchResult = this.userService.getUserByUserId(attrUser.getOrgUserId());
137 for (Object obj : searchResult) {
138 EPUser user = (EPUser) obj;
139 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
140 foundUsers.add(foundUser);
142 } catch (Exception e) {
143 logger.error(EELFLoggerDelegate.errorLogger, "searchUsersByUserId failed", e);
148 @SuppressWarnings("rawtypes")
149 public List<UserWithNameSurnameTitle> searchUsersByName(EPUser attrUser) {
150 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
152 List searchResult = this.userService.getUserByFirstLastName(attrUser.getFirstName(),attrUser.getLastName());
153 for (Object obj : searchResult) {
154 EPUser user = (EPUser) obj;
155 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
156 foundUsers.add(foundUser);
158 } catch (Exception e) {
159 logger.error(EELFLoggerDelegate.errorLogger, "searchUsersByName failed", e);
164 private List<UserWithNameSurnameTitle> removeWrongFirstNames(List<UserWithNameSurnameTitle> resultOfSearch, String firstName) {
165 firstName = firstName.toUpperCase();
166 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
167 UserWithNameSurnameTitle user = resultOfSearch.get(i);
168 if ((user.firstName == null) || !user.firstName.toUpperCase().startsWith(firstName)) {
169 resultOfSearch.remove(i);
172 return resultOfSearch;
175 private List<UserWithNameSurnameTitle> removeWrongLastNames(List<UserWithNameSurnameTitle> resultOfSearch, String lastName) {
176 lastName = lastName.toUpperCase();
177 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
178 UserWithNameSurnameTitle user = resultOfSearch.get(i);
179 if ((user.lastName == null) || !user.lastName.toUpperCase().startsWith(lastName)) {
180 resultOfSearch.remove(i);
183 return resultOfSearch;
186 private List<UserWithNameSurnameTitle> cutSearchResultToMaximumSize(List<UserWithNameSurnameTitle> resultOfSearch) {
187 for (int i = resultOfSearch.size() - 1; i >= maxSizeOfSearchResult; i--) {
188 resultOfSearch.remove(i);
190 return resultOfSearch;
194 @SuppressWarnings("rawtypes")
196 public EPUser searchUserByUserId(String orgUserId) {
197 List<EPUser> foundUsers = new ArrayList<EPUser>();
199 List searchResult = this.userService.getUserByUserId(orgUserId);
200 for (Object obj : searchResult) {
201 EPUser user = (EPUser) obj;
202 foundUsers.add(user);
204 } catch (Exception e) {
205 logger.error(EELFLoggerDelegate.errorLogger, "searchUserByUserId failed", e);
208 return foundUsers.get(0);