2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017-2018 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============================================
38 package org.onap.portalapp.portal.service;
40 import java.util.ArrayList;
41 import java.util.List;
42 import java.util.stream.Collectors;
44 import org.onap.portalapp.portal.domain.EPUser;
45 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
46 import org.onap.portalapp.portal.transport.UserWithNameSurnameTitle;
47 import org.onap.portalapp.portal.utils.EcompPortalUtils;
48 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.context.annotation.EnableAspectJAutoProxy;
51 import org.springframework.stereotype.Service;
52 import org.springframework.transaction.annotation.Transactional;
54 import com.fasterxml.jackson.core.JsonProcessingException;
55 import com.fasterxml.jackson.databind.ObjectMapper;
57 @Service("searchService")
59 @org.springframework.context.annotation.Configuration
60 @EnableAspectJAutoProxy
62 public class SearchServiceImpl implements SearchService {
63 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SearchServiceImpl.class);
65 // TODO: the values below should be defined in other place
66 private static final int maxSizeOfSearchResult = 100;
69 UserService userService;
72 public String searchUsersInPhoneBook(String searchString) {
73 return searchUsersInFnTable(searchString);
78 public String searchUsersInFnTable(String searchString) {
79 List<String> tokens = EcompPortalUtils.parsingByRegularExpression(searchString, " ");
80 while (tokens.size() > 2) { // we use no more then first 2 tokens (userId is removed, see above)
81 tokens.remove(tokens.size() - 1);
83 EPUser attrUser = new EPUser();
84 List<UserWithNameSurnameTitle> resultOfSearch = new ArrayList<UserWithNameSurnameTitle>(), resultOfAdditionalSearch = null,
85 resultOfSearchUserId = new ArrayList<UserWithNameSurnameTitle>();
86 if (tokens.size() == 2) {
87 attrUser.setFirstName(tokens.get(0));
88 attrUser.setLastName(tokens.get(1));
89 resultOfSearch = this.searchUsersByName(attrUser);
90 resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
91 resultOfSearch = this.removeWrongLastNames(resultOfSearch, tokens.get(1));
92 if (resultOfSearch.size() < maxSizeOfSearchResult) {
93 attrUser.setFirstName(tokens.get(1));
94 attrUser.setLastName(tokens.get(0));
95 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
96 resultOfAdditionalSearch = this.removeWrongFirstNames(resultOfAdditionalSearch, tokens.get(1));
97 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
99 } else if (tokens.size() == 1) {
100 attrUser.setFirstName(tokens.get(0));
101 attrUser.setOrgUserId(tokens.get(0));
102 resultOfSearch = this.searchUsersByName(attrUser);
103 resultOfSearchUserId = this.searchUsersByUserId(attrUser);
104 resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
105 if (resultOfSearch.size() < maxSizeOfSearchResult) {
106 attrUser.setFirstName(null);
107 attrUser.setLastName(tokens.get(0));
108 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
109 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
112 if (resultOfAdditionalSearch != null) {
113 resultOfSearch.addAll(resultOfAdditionalSearch);
115 resultOfSearch.addAll(resultOfSearchUserId);
116 resultOfSearch = resultOfSearch.stream().distinct().collect(Collectors.toList());
117 resultOfSearch = this.cutSearchResultToMaximumSize(resultOfSearch);
118 ObjectMapper mapper = new ObjectMapper();
119 String result = "[]";
121 result = mapper.writeValueAsString(resultOfSearch);
122 } catch (JsonProcessingException e) {
123 logger.error(EELFLoggerDelegate.errorLogger, "searchUsersInFnTable failed", e);
128 @SuppressWarnings("rawtypes")
129 public List<UserWithNameSurnameTitle> searchUsersByUserId(EPUser attrUser) {
130 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
132 List searchResult = this.userService.getUserByUserId(attrUser.getOrgUserId());
133 for (Object obj : searchResult) {
134 EPUser user = (EPUser) obj;
135 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
136 foundUsers.add(foundUser);
138 } catch (Exception e) {
139 logger.error(EELFLoggerDelegate.errorLogger, "searchUsersByUserId failed", e);
144 @SuppressWarnings("rawtypes")
145 public List<UserWithNameSurnameTitle> searchUsersByName(EPUser attrUser) {
146 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
148 List searchResult = this.userService.getUserByFirstLastName(attrUser.getFirstName(),attrUser.getLastName());
149 for (Object obj : searchResult) {
150 EPUser user = (EPUser) obj;
151 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
152 foundUsers.add(foundUser);
154 } catch (Exception e) {
155 logger.error(EELFLoggerDelegate.errorLogger, "searchUsersByName failed", e);
160 private List<UserWithNameSurnameTitle> removeWrongFirstNames(List<UserWithNameSurnameTitle> resultOfSearch, String firstName) {
161 firstName = firstName.toUpperCase();
162 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
163 UserWithNameSurnameTitle user = resultOfSearch.get(i);
164 if ((user.firstName == null) || !user.firstName.toUpperCase().startsWith(firstName)) {
165 resultOfSearch.remove(i);
168 return resultOfSearch;
171 private List<UserWithNameSurnameTitle> removeWrongLastNames(List<UserWithNameSurnameTitle> resultOfSearch, String lastName) {
172 lastName = lastName.toUpperCase();
173 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
174 UserWithNameSurnameTitle user = resultOfSearch.get(i);
175 if ((user.lastName == null) || !user.lastName.toUpperCase().startsWith(lastName)) {
176 resultOfSearch.remove(i);
179 return resultOfSearch;
182 private List<UserWithNameSurnameTitle> cutSearchResultToMaximumSize(List<UserWithNameSurnameTitle> resultOfSearch) {
183 for (int i = resultOfSearch.size() - 1; i >= maxSizeOfSearchResult; i--) {
184 resultOfSearch.remove(i);
186 return resultOfSearch;
190 @SuppressWarnings("rawtypes")
192 public EPUser searchUserByUserId(String orgUserId) {
193 List<EPUser> foundUsers = new ArrayList<EPUser>();
195 List searchResult = this.userService.getUserByUserId(orgUserId);
196 for (Object obj : searchResult) {
197 EPUser user = (EPUser) obj;
198 foundUsers.add(user);
200 } catch (Exception e) {
201 logger.error(EELFLoggerDelegate.errorLogger, "searchUserByUserId failed", e);
204 return foundUsers.get(0);