Update license; improve coverage; add docs dir
[portal.git] / ecomp-portal-BE-os / 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         @Autowired
68         UserService userService;
69
70         @Override
71         public String searchUsersInPhoneBook(String searchString) {
72                 return searchUsersInFnTable(searchString);
73         }
74         
75         @Override
76         public String searchUsersInFnTable(String searchString) {
77                 String orgUserId = null;
78                 List<String> tokens = EcompPortalUtils.parsingByRegularExpression(searchString, " ");
79                 for (int i = 0; i < tokens.size(); i++) { // find userid if possible and remove it from tokens
80                         if (tokens.get(i).matches(".*\\d+.*")) {
81                                 orgUserId = tokens.get(i);
82                                 tokens.remove(i);
83                         }
84                 }
85                 while (tokens.size() > 2) { // we use no more then first 2 tokens (userId 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.searchUsersByName(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.searchUsersByName(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.searchUsersByName(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.searchUsersByName(attrUser);
112                                 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
113                         }
114                 } else if (orgUserId != null) {
115                         resultOfSearch = this.searchUsersByUserId(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, "searchUsersInFnTable failed", e);
127                 }
128                 return result;
129         }
130
131         
132         @SuppressWarnings("rawtypes")
133         public List<UserWithNameSurnameTitle> searchUsersByUserId(EPUser attrUser) {
134                 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
135                 try {
136                         List searchResult = this.userService.getUserByUserId(attrUser.getOrgUserId());
137                         for (Object obj : searchResult) {
138                                 EPUser user = (EPUser) obj;
139                                 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
140                                 foundUsers.add(foundUser);
141                         }
142                 } catch (Exception e) {
143                         logger.error(EELFLoggerDelegate.errorLogger, "searchUsersByUserId failed", e);
144                 }
145                 return foundUsers;
146         }
147         
148         @SuppressWarnings("rawtypes")
149         public List<UserWithNameSurnameTitle> searchUsersByName(EPUser attrUser) {
150                 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
151                 try {
152                         List searchResult = this.userService.getUserByFirstLastName(attrUser.getFirstName(),attrUser.getLastName());
153                         for (Object obj : searchResult) {
154                                 EPUser user = (EPUser) obj;
155                                 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
156                                 foundUsers.add(foundUser);
157                         }
158                 } catch (Exception e) {
159                         logger.error(EELFLoggerDelegate.errorLogger, "searchUsersByName failed", e);
160                 }
161                 return foundUsers;
162         }
163
164         private List<UserWithNameSurnameTitle> removeWrongFirstNames(List<UserWithNameSurnameTitle> resultOfSearch, String firstName) {
165                 firstName = firstName.toUpperCase();
166                 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
167                         UserWithNameSurnameTitle user = resultOfSearch.get(i);
168                         if ((user.firstName == null) || !user.firstName.toUpperCase().startsWith(firstName)) {
169                                 resultOfSearch.remove(i);
170                         }
171                 }
172                 return resultOfSearch;
173         }
174
175         private List<UserWithNameSurnameTitle> removeWrongLastNames(List<UserWithNameSurnameTitle> resultOfSearch, String lastName) {
176                 lastName = lastName.toUpperCase();
177                 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
178                         UserWithNameSurnameTitle user = resultOfSearch.get(i);
179                         if ((user.lastName == null) || !user.lastName.toUpperCase().startsWith(lastName)) {
180                                 resultOfSearch.remove(i);
181                         }
182                 }
183                 return resultOfSearch;
184         }
185
186         private List<UserWithNameSurnameTitle> cutSearchResultToMaximumSize(List<UserWithNameSurnameTitle> resultOfSearch) {
187                 for (int i = resultOfSearch.size() - 1; i >= maxSizeOfSearchResult; i--) {
188                         resultOfSearch.remove(i);
189                 }
190                 return resultOfSearch;
191         }
192
193
194         @SuppressWarnings("rawtypes")
195         @Override
196         public EPUser searchUserByUserId(String orgUserId) {
197                 List<EPUser> foundUsers = new ArrayList<EPUser>();
198                 try {
199                         List searchResult = this.userService.getUserByUserId(orgUserId);
200                         for (Object obj : searchResult) {
201                                 EPUser user = (EPUser) obj;
202                                 foundUsers.add(user);
203                         }
204                 } catch (Exception e) {
205                         logger.error(EELFLoggerDelegate.errorLogger, "searchUserByUserId failed", e);
206                         return null;
207                 }
208                 return foundUsers.get(0);
209         }
210
211 }