2 * ================================================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ================================================================================
\r
20 package org.openecomp.portalapp.portal.service;
\r
22 import java.util.ArrayList;
\r
23 import java.util.List;
\r
25 import org.springframework.beans.factory.annotation.Autowired;
\r
26 import org.springframework.context.annotation.EnableAspectJAutoProxy;
\r
27 import org.springframework.stereotype.Service;
\r
28 import org.springframework.transaction.annotation.Transactional;
\r
30 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
\r
31 import org.openecomp.portalapp.portal.domain.EPUser;
\r
32 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
\r
33 import org.openecomp.portalapp.portal.transport.UserWithNameSurnameTitle;
\r
34 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
\r
35 import com.fasterxml.jackson.core.JsonProcessingException;
\r
36 import com.fasterxml.jackson.databind.ObjectMapper;
\r
38 @Service("searchService")
\r
40 @org.springframework.context.annotation.Configuration
\r
41 @EnableAspectJAutoProxy
\r
43 public class SearchServiceImpl implements SearchService {
\r
44 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SearchServiceImpl.class);
\r
46 // TODO: the values below should be defined in other place
\r
47 private static final int maxSizeOfSearchResult = 100;
\r
51 EPLdapService ldapService;
\r
54 public String searchUsersInPhoneBook(String searchString) {
\r
55 String orgUserId = null;
\r
56 List<String> tokens = EcompPortalUtils.parsingByRegularExpression(searchString, " ");
\r
57 for (int i = 0; i < tokens.size(); i++) { // find orgUserId if possible and remove it from tokens
\r
58 if (tokens.get(i).matches(".*\\d+.*")) {
\r
59 orgUserId = tokens.get(i);
\r
63 while (tokens.size() > 2) { // we use no more then first 2 tokens (orgUserId is removed, see above)
\r
64 tokens.remove(tokens.size() - 1);
\r
66 EPUser attrUser = new EPUser();
\r
67 attrUser.setOrgUserId(orgUserId);
\r
68 List<UserWithNameSurnameTitle> resultOfSearch = new ArrayList<UserWithNameSurnameTitle>(), resultOfAdditionalSearch = null;
\r
69 if (tokens.size() == 2) {
\r
70 attrUser.setFirstName(tokens.get(0));
\r
71 attrUser.setLastName(tokens.get(1));
\r
72 resultOfSearch = this.searchUsersByAttrs(attrUser);
\r
73 resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
\r
74 resultOfSearch = this.removeWrongLastNames(resultOfSearch, tokens.get(1));
\r
75 if (resultOfSearch.size() < maxSizeOfSearchResult) {
\r
76 attrUser.setFirstName(tokens.get(1));
\r
77 attrUser.setLastName(tokens.get(0));
\r
78 resultOfAdditionalSearch = this.searchUsersByAttrs(attrUser);
\r
79 resultOfAdditionalSearch = this.removeWrongFirstNames(resultOfAdditionalSearch, tokens.get(1));
\r
80 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
\r
82 } else if (tokens.size() == 1) {
\r
83 attrUser.setFirstName(tokens.get(0));
\r
84 resultOfSearch = this.searchUsersByAttrs(attrUser);
\r
85 resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
\r
86 if (resultOfSearch.size() < maxSizeOfSearchResult) {
\r
87 attrUser.setFirstName(null);
\r
88 attrUser.setLastName(tokens.get(0));
\r
89 resultOfAdditionalSearch = this.searchUsersByAttrs(attrUser);
\r
90 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
\r
92 } else if (orgUserId != null) {
\r
93 resultOfSearch = this.searchUsersByAttrs(attrUser);
\r
95 if (resultOfAdditionalSearch != null) {
\r
96 resultOfSearch.addAll(resultOfAdditionalSearch);
\r
98 resultOfSearch = this.cutSearchResultToMaximumSize(resultOfSearch);
\r
99 ObjectMapper mapper = new ObjectMapper();
\r
100 String result = "[]";
\r
102 result = mapper.writeValueAsString(resultOfSearch);
\r
103 } catch (JsonProcessingException e) {
\r
104 logger.error(EELFLoggerDelegate.errorLogger, "searchUsersInPhoneBook Exception = " + EcompPortalUtils.getStackTrace(e));
\r
109 private List<UserWithNameSurnameTitle> searchUsersByAttrs(EPUser attrUser) {
\r
110 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
\r
112 org.openecomp.portalsdk.core.command.support.SearchResult searchResult = this.ldapService.searchPost(attrUser, null, null, null, 0, 0, -1);
\r
113 for (Object obj : searchResult) {
\r
114 EPUser user = (EPUser) obj;
\r
115 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
\r
116 foundUsers.add(foundUser);
\r
118 } catch (Exception e) {
\r
119 logger.error(EELFLoggerDelegate.errorLogger, "searchInPhoneBookWithToken Exception = " + EcompPortalUtils.getStackTrace(e));
\r
124 private List<UserWithNameSurnameTitle> removeWrongFirstNames(List<UserWithNameSurnameTitle> resultOfSearch, String firstName) {
\r
125 firstName = firstName.toUpperCase();
\r
126 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
\r
127 UserWithNameSurnameTitle user = resultOfSearch.get(i);
\r
128 if ((user.firstName == null) || !user.firstName.toUpperCase().startsWith(firstName)) {
\r
129 resultOfSearch.remove(i);
\r
132 return resultOfSearch;
\r
135 private List<UserWithNameSurnameTitle> removeWrongLastNames(List<UserWithNameSurnameTitle> resultOfSearch, String lastName) {
\r
136 lastName = lastName.toUpperCase();
\r
137 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
\r
138 UserWithNameSurnameTitle user = resultOfSearch.get(i);
\r
139 if ((user.lastName == null) || !user.lastName.toUpperCase().startsWith(lastName)) {
\r
140 resultOfSearch.remove(i);
\r
143 return resultOfSearch;
\r
146 private List<UserWithNameSurnameTitle> cutSearchResultToMaximumSize(List<UserWithNameSurnameTitle> resultOfSearch) {
\r
147 for (int i = resultOfSearch.size() - 1; i >= maxSizeOfSearchResult; i--) {
\r
148 resultOfSearch.remove(i);
\r
150 return resultOfSearch;
\r
154 @SuppressWarnings("unchecked")
\r
155 public EPUser searchUserByUserId(String orgUserId) {
\r
156 EPUser user = null;
\r
157 EPUser searchedUser = new EPUser();
\r
158 searchedUser.setOrgUserId(orgUserId);
\r
160 List<Object> searchResult = ldapService.searchPost(searchedUser, "", null, null, 0, -1, 1);
\r
161 for (Object obj : searchResult) {
\r
162 if (obj instanceof EPUser) {
\r
163 user = (EPUser) obj;
\r
164 // This assignment should be checked later!
\r
165 user.setLoginId(orgUserId);
\r
169 } catch (Exception e) {
\r
170 logger.error(EELFLoggerDelegate.errorLogger, "searchUserBy orgUserId exception = " + EcompPortalUtils.getStackTrace(e));
\r