a192ad9436becd4267a021045a1310e45347edfa
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / service / SearchServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.portal.service;
39
40 import java.util.ArrayList;
41 import java.util.List;
42 import java.util.regex.Matcher;
43 import java.util.regex.Pattern;
44
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;
54
55 import com.fasterxml.jackson.core.JsonProcessingException;
56 import com.fasterxml.jackson.databind.ObjectMapper;
57
58 @Service("searchService")
59 @Transactional
60 @org.springframework.context.annotation.Configuration
61 @EnableAspectJAutoProxy
62 @EPMetricsLog
63 public class SearchServiceImpl implements SearchService {
64         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SearchServiceImpl.class);
65         
66         // TODO: the values below should be defined in other place
67         private static final int maxSizeOfSearchResult = 100;
68
69
70         @Autowired
71         EPLdapService ldapService;
72
73         @Override
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);
82                                 tokens.remove(i);
83                         }
84                 }
85                 while (tokens.size() > 2) { // we use no more then first 2 tokens (orgUserId is removed, see above)
86                         tokens.remove(tokens.size() - 1);
87                 }
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));
103                         }
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));
113                         }
114                 } else if (orgUserId != null) {
115                         resultOfSearch = this.searchUsersByAttrs(attrUser);
116                 }
117                 if (resultOfAdditionalSearch != null) {
118                         resultOfSearch.addAll(resultOfAdditionalSearch);
119                 }
120                 resultOfSearch = this.cutSearchResultToMaximumSize(resultOfSearch);
121                 ObjectMapper mapper = new ObjectMapper();
122                 String result = "[]";
123                 try {
124                         result = mapper.writeValueAsString(resultOfSearch);
125                 } catch (JsonProcessingException e) {
126                         logger.error(EELFLoggerDelegate.errorLogger, "searchUsersInPhoneBook failed", e);
127                 }
128                 return result;
129         }
130
131         private List<UserWithNameSurnameTitle> searchUsersByAttrs(EPUser attrUser) {
132                 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
133                 try {
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);
139                         }
140                 } catch (Exception e) {
141                         logger.error(EELFLoggerDelegate.errorLogger, "searchUsersByAttrs failed", e);
142                 }
143                 return foundUsers;
144         }
145
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);
152                         }
153                 }
154                 return resultOfSearch;
155         }
156
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);
163                         }
164                 }
165                 return resultOfSearch;
166         }
167
168         private List<UserWithNameSurnameTitle> cutSearchResultToMaximumSize(List<UserWithNameSurnameTitle> resultOfSearch) {
169                 for (int i = resultOfSearch.size() - 1; i >= maxSizeOfSearchResult; i--) {
170                         resultOfSearch.remove(i);
171                 }
172                 return resultOfSearch;
173         }
174
175         @Override
176         @SuppressWarnings("unchecked")
177         public EPUser searchUserByUserId(String orgUserId) {
178                 EPUser user = null;
179                 EPUser searchedUser = new EPUser();
180                 searchedUser.setOrgUserId(orgUserId);
181                 try {
182                         List<Object> searchResult = ldapService.searchPost(searchedUser, "", null, null, 0, -1, 1);
183                         for (Object obj : searchResult) {
184                                 if (obj instanceof EPUser) {
185                                         user = (EPUser) obj;
186                                         // This assignment should be checked later!
187                                         user.setLoginId(orgUserId);
188                                         break;
189                                 }
190                         }
191                 } catch (Exception e) {
192                         logger.error(EELFLoggerDelegate.errorLogger, "searchUserByUserId failed", e);
193                 }
194                 return user;
195         }
196
197 }