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.regex.Matcher;
43 import java.util.regex.Pattern;
45 import org.onap.portalapp.portal.domain.EPUser;
46 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
47 import org.onap.portalapp.portal.transport.UserWithNameSurnameTitle;
48 import org.onap.portalapp.portal.utils.EcompPortalUtils;
49 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.context.annotation.EnableAspectJAutoProxy;
52 import org.springframework.stereotype.Service;
53 import org.springframework.transaction.annotation.Transactional;
55 import com.fasterxml.jackson.core.JsonProcessingException;
56 import com.fasterxml.jackson.databind.ObjectMapper;
58 @Service("searchService")
60 @org.springframework.context.annotation.Configuration
61 @EnableAspectJAutoProxy
63 public class SearchServiceImpl implements SearchService {
64 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SearchServiceImpl.class);
66 // TODO: the values below should be defined in other place
67 private static final int maxSizeOfSearchResult = 100;
71 EPLdapService ldapService;
74 public String searchUsersInPhoneBook(String searchString) {
75 String orgUserId = null;
76 List<String> tokens = EcompPortalUtils.parsingByRegularExpression(searchString, " ");
77 Pattern pattern = Pattern.compile(EcompPortalUtils.SEARCH_USERS_REGEX);
78 for (int i = 0; i < tokens.size(); i++) { // find orgUserId if possible and remove it from tokens
79 Matcher matcher = pattern.matcher(tokens.get(i));
80 if (matcher.matches()) {
81 orgUserId = tokens.get(i);
85 while (tokens.size() > 2) { // we use no more then first 2 tokens (orgUserId 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.searchUsersByAttrs(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.searchUsersByAttrs(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.searchUsersByAttrs(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.searchUsersByAttrs(attrUser);
112 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
114 } else if (orgUserId != null) {
115 resultOfSearch = this.searchUsersByAttrs(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, "searchUsersInPhoneBook failed", e);
131 private List<UserWithNameSurnameTitle> searchUsersByAttrs(EPUser attrUser) {
132 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
134 org.onap.portalsdk.core.command.support.SearchResult searchResult = this.ldapService.searchPost(attrUser, null, null, null, 0, 0, -1);
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, "searchUsersByAttrs failed", e);
146 private List<UserWithNameSurnameTitle> removeWrongFirstNames(List<UserWithNameSurnameTitle> resultOfSearch, String firstName) {
147 firstName = firstName.toUpperCase();
148 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
149 UserWithNameSurnameTitle user = resultOfSearch.get(i);
150 if ((user.firstName == null) || !user.firstName.toUpperCase().startsWith(firstName)) {
151 resultOfSearch.remove(i);
154 return resultOfSearch;
157 private List<UserWithNameSurnameTitle> removeWrongLastNames(List<UserWithNameSurnameTitle> resultOfSearch, String lastName) {
158 lastName = lastName.toUpperCase();
159 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
160 UserWithNameSurnameTitle user = resultOfSearch.get(i);
161 if ((user.lastName == null) || !user.lastName.toUpperCase().startsWith(lastName)) {
162 resultOfSearch.remove(i);
165 return resultOfSearch;
168 private List<UserWithNameSurnameTitle> cutSearchResultToMaximumSize(List<UserWithNameSurnameTitle> resultOfSearch) {
169 for (int i = resultOfSearch.size() - 1; i >= maxSizeOfSearchResult; i--) {
170 resultOfSearch.remove(i);
172 return resultOfSearch;
176 @SuppressWarnings("unchecked")
177 public EPUser searchUserByUserId(String orgUserId) {
179 EPUser searchedUser = new EPUser();
180 searchedUser.setOrgUserId(orgUserId);
182 List<Object> searchResult = ldapService.searchPost(searchedUser, "", null, null, 0, -1, 1);
183 for (Object obj : searchResult) {
184 if (obj instanceof EPUser) {
186 // This assignment should be checked later!
187 user.setLoginId(orgUserId);
191 } catch (Exception e) {
192 logger.error(EELFLoggerDelegate.errorLogger, "searchUserByUserId failed", e);