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;
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 while (tokens.size() > 2) { // we use no more then first 2 tokens (userId is removed, see above)
83 tokens.remove(tokens.size() - 1);
85 EPUser attrUser = new EPUser();
86 attrUser.setOrgUserId(orgUserId);
87 List<UserWithNameSurnameTitle> resultOfSearch = new ArrayList<UserWithNameSurnameTitle>(), resultOfAdditionalSearch = null,
88 resultOfSearchUserId = new ArrayList<UserWithNameSurnameTitle>();
89 if (tokens.size() == 2) {
90 attrUser.setFirstName(tokens.get(0));
91 attrUser.setLastName(tokens.get(1));
92 resultOfSearch = this.searchUsersByName(attrUser);
93 resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
94 resultOfSearch = this.removeWrongLastNames(resultOfSearch, tokens.get(1));
95 if (resultOfSearch.size() < maxSizeOfSearchResult) {
96 attrUser.setFirstName(tokens.get(1));
97 attrUser.setLastName(tokens.get(0));
98 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
99 resultOfAdditionalSearch = this.removeWrongFirstNames(resultOfAdditionalSearch, tokens.get(1));
100 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
102 } else if (tokens.size() == 1) {
103 attrUser.setFirstName(tokens.get(0));
104 resultOfSearch = this.searchUsersByName(attrUser);
105 resultOfSearchUserId = this.searchUsersByUserId(attrUser);
106 resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
107 if (resultOfSearch.size() < maxSizeOfSearchResult) {
108 attrUser.setFirstName(null);
109 attrUser.setLastName(tokens.get(0));
110 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
111 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
114 if (resultOfAdditionalSearch != null) {
115 resultOfSearch.addAll(resultOfAdditionalSearch);
117 resultOfSearch.addAll(resultOfSearchUserId);
118 resultOfSearch = this.cutSearchResultToMaximumSize(resultOfSearch);
119 ObjectMapper mapper = new ObjectMapper();
120 String result = "[]";
122 result = mapper.writeValueAsString(resultOfSearch);
123 } catch (JsonProcessingException e) {
124 logger.error(EELFLoggerDelegate.errorLogger, "searchUsersInFnTable failed", e);
130 @SuppressWarnings("rawtypes")
131 public List<UserWithNameSurnameTitle> searchUsersByUserId(EPUser attrUser) {
132 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
134 List searchResult = this.userService.getUserByUserId(attrUser.getOrgUserId());
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, "searchUsersByUserId failed", e);
146 @SuppressWarnings("rawtypes")
147 public List<UserWithNameSurnameTitle> searchUsersByName(EPUser attrUser) {
148 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
150 List searchResult = this.userService.getUserByFirstLastName(attrUser.getFirstName(),attrUser.getLastName());
151 for (Object obj : searchResult) {
152 EPUser user = (EPUser) obj;
153 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
154 foundUsers.add(foundUser);
156 } catch (Exception e) {
157 logger.error(EELFLoggerDelegate.errorLogger, "searchUsersByName failed", e);
162 private List<UserWithNameSurnameTitle> removeWrongFirstNames(List<UserWithNameSurnameTitle> resultOfSearch, String firstName) {
163 firstName = firstName.toUpperCase();
164 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
165 UserWithNameSurnameTitle user = resultOfSearch.get(i);
166 if ((user.firstName == null) || !user.firstName.toUpperCase().startsWith(firstName)) {
167 resultOfSearch.remove(i);
170 return resultOfSearch;
173 private List<UserWithNameSurnameTitle> removeWrongLastNames(List<UserWithNameSurnameTitle> resultOfSearch, String lastName) {
174 lastName = lastName.toUpperCase();
175 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
176 UserWithNameSurnameTitle user = resultOfSearch.get(i);
177 if ((user.lastName == null) || !user.lastName.toUpperCase().startsWith(lastName)) {
178 resultOfSearch.remove(i);
181 return resultOfSearch;
184 private List<UserWithNameSurnameTitle> cutSearchResultToMaximumSize(List<UserWithNameSurnameTitle> resultOfSearch) {
185 for (int i = resultOfSearch.size() - 1; i >= maxSizeOfSearchResult; i--) {
186 resultOfSearch.remove(i);
188 return resultOfSearch;
192 @SuppressWarnings("rawtypes")
194 public EPUser searchUserByUserId(String orgUserId) {
195 List<EPUser> foundUsers = new ArrayList<EPUser>();
197 List searchResult = this.userService.getUserByUserId(orgUserId);
198 for (Object obj : searchResult) {
199 EPUser user = (EPUser) obj;
200 foundUsers.add(user);
202 } catch (Exception e) {
203 logger.error(EELFLoggerDelegate.errorLogger, "searchUserByUserId failed", e);
206 return foundUsers.get(0);