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