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.service.SearchService;
44 import org.onap.portalapp.portal.service.SearchServiceImpl;
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 for (int i = 0; i < tokens.size(); i++) { // find orgUserId if possible and remove it from tokens
78 if (tokens.get(i).matches(".*\\d+.*")) {
79 orgUserId = tokens.get(i);
83 while (tokens.size() > 2) { // we use no more then first 2 tokens (orgUserId is removed, see above)
84 tokens.remove(tokens.size() - 1);
86 EPUser attrUser = new EPUser();
87 attrUser.setOrgUserId(orgUserId);
88 List<UserWithNameSurnameTitle> resultOfSearch = new ArrayList<UserWithNameSurnameTitle>(), resultOfAdditionalSearch = null;
89 if (tokens.size() == 2) {
90 attrUser.setFirstName(tokens.get(0));
91 attrUser.setLastName(tokens.get(1));
92 resultOfSearch = this.searchUsersByAttrs(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.searchUsersByAttrs(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.searchUsersByAttrs(attrUser);
105 resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
106 if (resultOfSearch.size() < maxSizeOfSearchResult) {
107 attrUser.setFirstName(null);
108 attrUser.setLastName(tokens.get(0));
109 resultOfAdditionalSearch = this.searchUsersByAttrs(attrUser);
110 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
112 } else if (orgUserId != null) {
113 resultOfSearch = this.searchUsersByAttrs(attrUser);
115 if (resultOfAdditionalSearch != null) {
116 resultOfSearch.addAll(resultOfAdditionalSearch);
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, "searchUsersInPhoneBook failed", e);
129 private List<UserWithNameSurnameTitle> searchUsersByAttrs(EPUser attrUser) {
130 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
132 org.onap.portalsdk.core.command.support.SearchResult searchResult = this.ldapService.searchPost(attrUser, null, null, null, 0, 0, -1);
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, "searchUsersByAttrs failed", e);
144 private List<UserWithNameSurnameTitle> removeWrongFirstNames(List<UserWithNameSurnameTitle> resultOfSearch, String firstName) {
145 firstName = firstName.toUpperCase();
146 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
147 UserWithNameSurnameTitle user = resultOfSearch.get(i);
148 if ((user.firstName == null) || !user.firstName.toUpperCase().startsWith(firstName)) {
149 resultOfSearch.remove(i);
152 return resultOfSearch;
155 private List<UserWithNameSurnameTitle> removeWrongLastNames(List<UserWithNameSurnameTitle> resultOfSearch, String lastName) {
156 lastName = lastName.toUpperCase();
157 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
158 UserWithNameSurnameTitle user = resultOfSearch.get(i);
159 if ((user.lastName == null) || !user.lastName.toUpperCase().startsWith(lastName)) {
160 resultOfSearch.remove(i);
163 return resultOfSearch;
166 private List<UserWithNameSurnameTitle> cutSearchResultToMaximumSize(List<UserWithNameSurnameTitle> resultOfSearch) {
167 for (int i = resultOfSearch.size() - 1; i >= maxSizeOfSearchResult; i--) {
168 resultOfSearch.remove(i);
170 return resultOfSearch;
174 @SuppressWarnings("unchecked")
175 public EPUser searchUserByUserId(String orgUserId) {
177 EPUser searchedUser = new EPUser();
178 searchedUser.setOrgUserId(orgUserId);
180 List<Object> searchResult = ldapService.searchPost(searchedUser, "", null, null, 0, -1, 1);
181 for (Object obj : searchResult) {
182 if (obj instanceof EPUser) {
184 // This assignment should be checked later!
185 user.setLoginId(orgUserId);
189 } catch (Exception e) {
190 logger.error(EELFLoggerDelegate.errorLogger, "searchUserByUserId failed", e);