Update license; improve coverage; add docs dir
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / SearchServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.portal.service;
39
40 import java.util.ArrayList;
41 import java.util.List;
42
43 import org.openecomp.portalapp.portal.domain.EPUser;
44 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
45 import org.openecomp.portalapp.portal.transport.UserWithNameSurnameTitle;
46 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
47 import org.openecomp.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;
52
53 import com.fasterxml.jackson.core.JsonProcessingException;
54 import com.fasterxml.jackson.databind.ObjectMapper;
55
56 @Service("searchService")
57 @Transactional
58 @org.springframework.context.annotation.Configuration
59 @EnableAspectJAutoProxy
60 @EPMetricsLog
61 public class SearchServiceImpl implements SearchService {
62         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SearchServiceImpl.class);
63         
64         // TODO: the values below should be defined in other place
65         private static final int maxSizeOfSearchResult = 100;
66
67
68         @Autowired
69         EPLdapService ldapService;
70
71         @Override
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);
78                                 tokens.remove(i);
79                         }
80                 }
81                 while (tokens.size() > 2) { // we use no more then first 2 tokens (orgUserId is removed, see above)
82                         tokens.remove(tokens.size() - 1);
83                 }
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));
99                         }
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));
109                         }
110                 } else if (orgUserId != null) {
111                         resultOfSearch = this.searchUsersByAttrs(attrUser);
112                 }
113                 if (resultOfAdditionalSearch != null) {
114                         resultOfSearch.addAll(resultOfAdditionalSearch);
115                 }
116                 resultOfSearch = this.cutSearchResultToMaximumSize(resultOfSearch);
117                 ObjectMapper mapper = new ObjectMapper();
118                 String result = "[]";
119                 try {
120                         result = mapper.writeValueAsString(resultOfSearch);
121                 } catch (JsonProcessingException e) {
122                         logger.error(EELFLoggerDelegate.errorLogger, "searchUsersInPhoneBook failed", e);
123                 }
124                 return result;
125         }
126
127         private List<UserWithNameSurnameTitle> searchUsersByAttrs(EPUser attrUser) {
128                 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
129                 try {
130                         org.openecomp.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);
135                         }
136                 } catch (Exception e) {
137                         logger.error(EELFLoggerDelegate.errorLogger, "searchUsersByAttrs failed", e);
138                 }
139                 return foundUsers;
140         }
141
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);
148                         }
149                 }
150                 return resultOfSearch;
151         }
152
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);
159                         }
160                 }
161                 return resultOfSearch;
162         }
163
164         private List<UserWithNameSurnameTitle> cutSearchResultToMaximumSize(List<UserWithNameSurnameTitle> resultOfSearch) {
165                 for (int i = resultOfSearch.size() - 1; i >= maxSizeOfSearchResult; i--) {
166                         resultOfSearch.remove(i);
167                 }
168                 return resultOfSearch;
169         }
170
171         @Override
172         @SuppressWarnings("unchecked")
173         public EPUser searchUserByUserId(String orgUserId) {
174                 EPUser user = null;
175                 EPUser searchedUser = new EPUser();
176                 searchedUser.setOrgUserId(orgUserId);
177                 try {
178                         List<Object> searchResult = ldapService.searchPost(searchedUser, "", null, null, 0, -1, 1);
179                         for (Object obj : searchResult) {
180                                 if (obj instanceof EPUser) {
181                                         user = (EPUser) obj;
182                                         // This assignment should be checked later!
183                                         user.setLoginId(orgUserId);
184                                         break;
185                                 }
186                         }
187                 } catch (Exception e) {
188                         logger.error(EELFLoggerDelegate.errorLogger, "searchUserByUserId failed", e);
189                 }
190                 return user;
191         }
192
193 }