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;
54 public String searchUsersInFnTable(String searchString) {
55 String orgUserId = null;
56 List<String> tokens = EcompPortalUtils.parsingByRegularExpression(searchString, " ");
57 for (int i = 0; i < tokens.size(); i++) { // find userid if possible and remove it from tokens
58 if (tokens.get(i).matches(".*\\d+.*")) {
59 orgUserId = tokens.get(i);
63 while (tokens.size() > 2) { // we use no more then first 2 tokens (userId is removed, see above)
64 tokens.remove(tokens.size() - 1);
66 EPUser attrUser = new EPUser();
67 attrUser.setOrgUserId(orgUserId);
68 List<UserWithNameSurnameTitle> resultOfSearch = new ArrayList<UserWithNameSurnameTitle>(), resultOfAdditionalSearch = null;
69 if (tokens.size() == 2) {
70 attrUser.setFirstName(tokens.get(0));
71 attrUser.setLastName(tokens.get(1));
72 resultOfSearch = this.searchUsersByName(attrUser);
73 resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
74 resultOfSearch = this.removeWrongLastNames(resultOfSearch, tokens.get(1));
75 if (resultOfSearch.size() < maxSizeOfSearchResult) {
76 attrUser.setFirstName(tokens.get(1));
77 attrUser.setLastName(tokens.get(0));
78 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
79 resultOfAdditionalSearch = this.removeWrongFirstNames(resultOfAdditionalSearch, tokens.get(1));
80 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
82 } else if (tokens.size() == 1) {
83 attrUser.setFirstName(tokens.get(0));
84 resultOfSearch = this.searchUsersByName(attrUser);
85 resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
86 if (resultOfSearch.size() < maxSizeOfSearchResult) {
87 attrUser.setFirstName(null);
88 attrUser.setLastName(tokens.get(0));
89 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
90 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
92 } else if (orgUserId != null) {
93 resultOfSearch = this.searchUsersByUserId(attrUser);
95 if (resultOfAdditionalSearch != null) {
96 resultOfSearch.addAll(resultOfAdditionalSearch);
98 resultOfSearch = this.cutSearchResultToMaximumSize(resultOfSearch);
99 ObjectMapper mapper = new ObjectMapper();
100 String result = "[]";
102 result = mapper.writeValueAsString(resultOfSearch);
103 } catch (JsonProcessingException e) {
104 logger.error(EELFLoggerDelegate.errorLogger, "searchUsersInPhoneBook Exception = " + EcompPortalUtils.getStackTrace(e));
110 @SuppressWarnings("rawtypes")
111 public List<UserWithNameSurnameTitle> searchUsersByUserId(EPUser attrUser) {
112 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
114 List searchResult = this.userService.getUserByUserId(attrUser.getOrgUserId());
115 for (Object obj : searchResult) {
116 EPUser user = (EPUser) obj;
117 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
118 foundUsers.add(foundUser);
120 } catch (Exception e) {
121 logger.error(EELFLoggerDelegate.errorLogger, "searchInPhoneBookWithToken Exception = " + EcompPortalUtils.getStackTrace(e));
126 @SuppressWarnings("rawtypes")
127 public List<UserWithNameSurnameTitle> searchUsersByName(EPUser attrUser) {
128 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
130 List searchResult = this.userService.getUserByFirstLastName(attrUser.getFirstName(),attrUser.getLastName());
131 for (Object obj : searchResult) {
132 EPUser user = (EPUser) obj;
133 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
134 foundUsers.add(foundUser);
136 } catch (Exception e) {
137 logger.error(EELFLoggerDelegate.errorLogger, "searchInPhoneBookWithToken Exception = " + EcompPortalUtils.getStackTrace(e));
142 private List<UserWithNameSurnameTitle> removeWrongFirstNames(List<UserWithNameSurnameTitle> resultOfSearch, String firstName) {
143 firstName = firstName.toUpperCase();
144 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
145 UserWithNameSurnameTitle user = resultOfSearch.get(i);
146 if ((user.firstName == null) || !user.firstName.toUpperCase().startsWith(firstName)) {
147 resultOfSearch.remove(i);
150 return resultOfSearch;
153 private List<UserWithNameSurnameTitle> removeWrongLastNames(List<UserWithNameSurnameTitle> resultOfSearch, String lastName) {
154 lastName = lastName.toUpperCase();
155 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
156 UserWithNameSurnameTitle user = resultOfSearch.get(i);
157 if ((user.lastName == null) || !user.lastName.toUpperCase().startsWith(lastName)) {
158 resultOfSearch.remove(i);
161 return resultOfSearch;
164 private List<UserWithNameSurnameTitle> cutSearchResultToMaximumSize(List<UserWithNameSurnameTitle> resultOfSearch) {
165 for (int i = resultOfSearch.size() - 1; i >= maxSizeOfSearchResult; i--) {
166 resultOfSearch.remove(i);
168 return resultOfSearch;
172 @SuppressWarnings("rawtypes")
174 public EPUser searchUserByUserId(String orgUserId) {
175 List<EPUser> foundUsers = new ArrayList<EPUser>();
177 List searchResult = this.userService.getUserByUserId(orgUserId);
178 for (Object obj : searchResult) {
179 EPUser user = (EPUser) obj;
180 foundUsers.add(user);
182 } catch (Exception e) {
183 logger.error(EELFLoggerDelegate.errorLogger, "searchInPhoneBookWithToken Exception = " + EcompPortalUtils.getStackTrace(e));
185 return foundUsers.get(0);