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.transport.UserWithNameSurnameTitle;
46 import org.onap.portalapp.portal.utils.EcompPortalUtils;
47 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.context.annotation.EnableAspectJAutoProxy;
50 import org.springframework.stereotype.Service;
51 import org.springframework.transaction.annotation.Transactional;
53 import com.fasterxml.jackson.core.JsonProcessingException;
54 import com.fasterxml.jackson.databind.ObjectMapper;
56 @Service("searchService")
58 @org.springframework.context.annotation.Configuration
59 @EnableAspectJAutoProxy
61 public class SearchServiceImpl implements SearchService {
62 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SearchServiceImpl.class);
64 // TODO: the values below should be defined in other place
65 private static final int maxSizeOfSearchResult = 100;
69 EPLdapService ldapService;
72 public String searchUsersInPhoneBook(String searchString) {
73 String orgUserId = null;
74 List<String> tokens = EcompPortalUtils.parsingByRegularExpression(searchString, " ");
75 for (int i = 0; i < tokens.size(); i++) { // find orgUserId if possible and remove it from tokens
76 if (tokens.get(i).matches(".*\\d+.*")) {
77 orgUserId = tokens.get(i);
81 while (tokens.size() > 2) { // we use no more then first 2 tokens (orgUserId is removed, see above)
82 tokens.remove(tokens.size() - 1);
84 EPUser attrUser = new EPUser();
85 attrUser.setOrgUserId(orgUserId);
86 List<UserWithNameSurnameTitle> resultOfSearch = new ArrayList<UserWithNameSurnameTitle>(), resultOfAdditionalSearch = null;
87 if (tokens.size() == 2) {
88 attrUser.setFirstName(tokens.get(0));
89 attrUser.setLastName(tokens.get(1));
90 resultOfSearch = this.searchUsersByAttrs(attrUser);
91 resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
92 resultOfSearch = this.removeWrongLastNames(resultOfSearch, tokens.get(1));
93 if (resultOfSearch.size() < maxSizeOfSearchResult) {
94 attrUser.setFirstName(tokens.get(1));
95 attrUser.setLastName(tokens.get(0));
96 resultOfAdditionalSearch = this.searchUsersByAttrs(attrUser);
97 resultOfAdditionalSearch = this.removeWrongFirstNames(resultOfAdditionalSearch, tokens.get(1));
98 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
100 } else if (tokens.size() == 1) {
101 attrUser.setFirstName(tokens.get(0));
102 resultOfSearch = this.searchUsersByAttrs(attrUser);
103 resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
104 if (resultOfSearch.size() < maxSizeOfSearchResult) {
105 attrUser.setFirstName(null);
106 attrUser.setLastName(tokens.get(0));
107 resultOfAdditionalSearch = this.searchUsersByAttrs(attrUser);
108 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
110 } else if (orgUserId != null) {
111 resultOfSearch = this.searchUsersByAttrs(attrUser);
113 if (resultOfAdditionalSearch != null) {
114 resultOfSearch.addAll(resultOfAdditionalSearch);
116 resultOfSearch = this.cutSearchResultToMaximumSize(resultOfSearch);
117 ObjectMapper mapper = new ObjectMapper();
118 String result = "[]";
120 result = mapper.writeValueAsString(resultOfSearch);
121 } catch (JsonProcessingException e) {
122 logger.error(EELFLoggerDelegate.errorLogger, "searchUsersInPhoneBook failed", e);
127 private List<UserWithNameSurnameTitle> searchUsersByAttrs(EPUser attrUser) {
128 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
130 org.onap.portalsdk.core.command.support.SearchResult searchResult = this.ldapService.searchPost(attrUser, null, null, null, 0, 0, -1);
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, "searchUsersByAttrs failed", 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("unchecked")
173 public EPUser searchUserByUserId(String orgUserId) {
175 EPUser searchedUser = new EPUser();
176 searchedUser.setOrgUserId(orgUserId);
178 List<Object> searchResult = ldapService.searchPost(searchedUser, "", null, null, 0, -1, 1);
179 for (Object obj : searchResult) {
180 if (obj instanceof EPUser) {
182 // This assignment should be checked later!
183 user.setLoginId(orgUserId);
187 } catch (Exception e) {
188 logger.error(EELFLoggerDelegate.errorLogger, "searchUserByUserId failed", e);