2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 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.onap.portalapp.portal.service;
40 import java.util.ArrayList;
41 import java.util.List;
43 import org.onap.portalapp.portal.domain.EPUser;
44 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
45 import org.onap.portalapp.portal.service.SearchService;
46 import org.onap.portalapp.portal.service.SearchServiceImpl;
47 import org.onap.portalapp.portal.service.UserService;
48 import org.onap.portalapp.portal.transport.UserWithNameSurnameTitle;
49 import org.onap.portalapp.portal.utils.EcompPortalUtils;
50 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.context.annotation.EnableAspectJAutoProxy;
53 import org.springframework.stereotype.Service;
54 import org.springframework.transaction.annotation.Transactional;
56 import com.fasterxml.jackson.core.JsonProcessingException;
57 import com.fasterxml.jackson.databind.ObjectMapper;
59 @Service("searchService")
61 @org.springframework.context.annotation.Configuration
62 @EnableAspectJAutoProxy
64 public class SearchServiceImpl implements SearchService {
65 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SearchServiceImpl.class);
67 // TODO: the values below should be defined in other place
68 private static final int maxSizeOfSearchResult = 100;
71 UserService userService;
74 public String searchUsersInPhoneBook(String searchString) {
75 return searchUsersInFnTable(searchString);
79 public String searchUsersInFnTable(String searchString) {
80 String orgUserId = null;
81 List<String> tokens = EcompPortalUtils.parsingByRegularExpression(searchString, " ");
82 for (int i = 0; i < tokens.size(); i++) { // find userid if possible and remove it from tokens
83 if (tokens.get(i).matches(".*\\d+.*")) {
84 orgUserId = tokens.get(i);
88 while (tokens.size() > 2) { // we use no more then first 2 tokens (userId is removed, see above)
89 tokens.remove(tokens.size() - 1);
91 EPUser attrUser = new EPUser();
92 attrUser.setOrgUserId(orgUserId);
93 List<UserWithNameSurnameTitle> resultOfSearch = new ArrayList<UserWithNameSurnameTitle>(), resultOfAdditionalSearch = null;
94 if (tokens.size() == 2) {
95 attrUser.setFirstName(tokens.get(0));
96 attrUser.setLastName(tokens.get(1));
97 resultOfSearch = this.searchUsersByName(attrUser);
98 resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
99 resultOfSearch = this.removeWrongLastNames(resultOfSearch, tokens.get(1));
100 if (resultOfSearch.size() < maxSizeOfSearchResult) {
101 attrUser.setFirstName(tokens.get(1));
102 attrUser.setLastName(tokens.get(0));
103 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
104 resultOfAdditionalSearch = this.removeWrongFirstNames(resultOfAdditionalSearch, tokens.get(1));
105 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
107 } else if (tokens.size() == 1) {
108 attrUser.setFirstName(tokens.get(0));
109 resultOfSearch = this.searchUsersByName(attrUser);
110 resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
111 if (resultOfSearch.size() < maxSizeOfSearchResult) {
112 attrUser.setFirstName(null);
113 attrUser.setLastName(tokens.get(0));
114 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
115 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
117 } else if (orgUserId != null) {
118 resultOfSearch = this.searchUsersByUserId(attrUser);
120 if (resultOfAdditionalSearch != null) {
121 resultOfSearch.addAll(resultOfAdditionalSearch);
123 resultOfSearch = this.cutSearchResultToMaximumSize(resultOfSearch);
124 ObjectMapper mapper = new ObjectMapper();
125 String result = "[]";
127 result = mapper.writeValueAsString(resultOfSearch);
128 } catch (JsonProcessingException e) {
129 logger.error(EELFLoggerDelegate.errorLogger, "searchUsersInFnTable failed", e);
135 @SuppressWarnings("rawtypes")
136 public List<UserWithNameSurnameTitle> searchUsersByUserId(EPUser attrUser) {
137 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
139 List searchResult = this.userService.getUserByUserId(attrUser.getOrgUserId());
140 for (Object obj : searchResult) {
141 EPUser user = (EPUser) obj;
142 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
143 foundUsers.add(foundUser);
145 } catch (Exception e) {
146 logger.error(EELFLoggerDelegate.errorLogger, "searchUsersByUserId failed", e);
151 @SuppressWarnings("rawtypes")
152 public List<UserWithNameSurnameTitle> searchUsersByName(EPUser attrUser) {
153 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
155 List searchResult = this.userService.getUserByFirstLastName(attrUser.getFirstName(),attrUser.getLastName());
156 for (Object obj : searchResult) {
157 EPUser user = (EPUser) obj;
158 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
159 foundUsers.add(foundUser);
161 } catch (Exception e) {
162 logger.error(EELFLoggerDelegate.errorLogger, "searchUsersByName failed", e);
167 private List<UserWithNameSurnameTitle> removeWrongFirstNames(List<UserWithNameSurnameTitle> resultOfSearch, String firstName) {
168 firstName = firstName.toUpperCase();
169 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
170 UserWithNameSurnameTitle user = resultOfSearch.get(i);
171 if ((user.firstName == null) || !user.firstName.toUpperCase().startsWith(firstName)) {
172 resultOfSearch.remove(i);
175 return resultOfSearch;
178 private List<UserWithNameSurnameTitle> removeWrongLastNames(List<UserWithNameSurnameTitle> resultOfSearch, String lastName) {
179 lastName = lastName.toUpperCase();
180 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
181 UserWithNameSurnameTitle user = resultOfSearch.get(i);
182 if ((user.lastName == null) || !user.lastName.toUpperCase().startsWith(lastName)) {
183 resultOfSearch.remove(i);
186 return resultOfSearch;
189 private List<UserWithNameSurnameTitle> cutSearchResultToMaximumSize(List<UserWithNameSurnameTitle> resultOfSearch) {
190 for (int i = resultOfSearch.size() - 1; i >= maxSizeOfSearchResult; i--) {
191 resultOfSearch.remove(i);
193 return resultOfSearch;
197 @SuppressWarnings("rawtypes")
199 public EPUser searchUserByUserId(String orgUserId) {
200 List<EPUser> foundUsers = new ArrayList<EPUser>();
202 List searchResult = this.userService.getUserByUserId(orgUserId);
203 for (Object obj : searchResult) {
204 EPUser user = (EPUser) obj;
205 foundUsers.add(user);
207 } catch (Exception e) {
208 logger.error(EELFLoggerDelegate.errorLogger, "searchUserByUserId failed", e);
211 return foundUsers.get(0);