search user changes
[portal.git] / ecomp-portal-BE-os / 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.stream.Collectors;
43
44 import org.onap.portalapp.portal.domain.EPUser;
45 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
46 import org.onap.portalapp.portal.transport.UserWithNameSurnameTitle;
47 import org.onap.portalapp.portal.utils.EcompPortalUtils;
48 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.context.annotation.EnableAspectJAutoProxy;
51 import org.springframework.stereotype.Service;
52 import org.springframework.transaction.annotation.Transactional;
53
54 import com.fasterxml.jackson.core.JsonProcessingException;
55 import com.fasterxml.jackson.databind.ObjectMapper;
56
57 @Service("searchService")
58 @Transactional
59 @org.springframework.context.annotation.Configuration
60 @EnableAspectJAutoProxy
61 @EPMetricsLog
62 public class SearchServiceImpl implements SearchService {
63         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SearchServiceImpl.class);
64         
65         // TODO: the values below should be defined in other place
66         private static final int maxSizeOfSearchResult = 100;
67
68         @Autowired
69         UserService userService;
70
71         @Override
72         public String searchUsersInPhoneBook(String searchString) {
73                 return searchUsersInFnTable(searchString);
74         }
75         
76         
77         @Override
78         public String searchUsersInFnTable(String searchString) {
79                 List<String> tokens = EcompPortalUtils.parsingByRegularExpression(searchString, " ");
80                 while (tokens.size() > 2) { // we use no more then first 2 tokens (userId is removed, see above)
81                         tokens.remove(tokens.size() - 1);
82                 }
83                 EPUser attrUser = new EPUser();
84                 List<UserWithNameSurnameTitle> resultOfSearch = new ArrayList<UserWithNameSurnameTitle>(), resultOfAdditionalSearch = null,
85                                 resultOfSearchUserId = new ArrayList<UserWithNameSurnameTitle>();
86                 if (tokens.size() == 2) {
87                         attrUser.setFirstName(tokens.get(0));
88                         attrUser.setLastName(tokens.get(1));
89                         resultOfSearch = this.searchUsersByName(attrUser);
90                         resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
91                         resultOfSearch = this.removeWrongLastNames(resultOfSearch, tokens.get(1));
92                         if (resultOfSearch.size() < maxSizeOfSearchResult) {
93                                 attrUser.setFirstName(tokens.get(1));
94                                 attrUser.setLastName(tokens.get(0));
95                                 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
96                                 resultOfAdditionalSearch = this.removeWrongFirstNames(resultOfAdditionalSearch, tokens.get(1));
97                                 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
98                         }
99                 } else if (tokens.size() == 1) {
100                         attrUser.setFirstName(tokens.get(0));
101                         attrUser.setOrgUserId(tokens.get(0));
102                         resultOfSearch = this.searchUsersByName(attrUser);
103                         resultOfSearchUserId = this.searchUsersByUserId(attrUser);
104                         resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
105                         if (resultOfSearch.size() < maxSizeOfSearchResult) {
106                                 attrUser.setFirstName(null);
107                                 attrUser.setLastName(tokens.get(0));
108                                 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
109                                 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
110                         }
111                 }
112                 if (resultOfAdditionalSearch != null) {
113                         resultOfSearch.addAll(resultOfAdditionalSearch);
114                 }
115                 resultOfSearch.addAll(resultOfSearchUserId);
116                 resultOfSearch.stream().distinct().collect(Collectors.toList());
117                 resultOfSearch = this.cutSearchResultToMaximumSize(resultOfSearch);
118                 ObjectMapper mapper = new ObjectMapper();
119                 String result = "[]";
120                 try {
121                         result = mapper.writeValueAsString(resultOfSearch);
122                 } catch (JsonProcessingException e) {
123                         logger.error(EELFLoggerDelegate.errorLogger, "searchUsersInFnTable failed", e);
124                 }
125                 return result;
126         }
127
128         @SuppressWarnings("rawtypes")
129         public List<UserWithNameSurnameTitle> searchUsersByUserId(EPUser attrUser) {
130                 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
131                 try {
132                         List searchResult = this.userService.getUserByUserId(attrUser.getOrgUserId());
133                         for (Object obj : searchResult) {
134                                 EPUser user = (EPUser) obj;
135                                 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
136                                 foundUsers.add(foundUser);
137                         }
138                 } catch (Exception e) {
139                         logger.error(EELFLoggerDelegate.errorLogger, "searchUsersByUserId failed", e);
140                 }
141                 return foundUsers;
142         }
143         
144         @SuppressWarnings("rawtypes")
145         public List<UserWithNameSurnameTitle> searchUsersByName(EPUser attrUser) {
146                 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
147                 try {
148                         List searchResult = this.userService.getUserByFirstLastName(attrUser.getFirstName(),attrUser.getLastName());
149                         for (Object obj : searchResult) {
150                                 EPUser user = (EPUser) obj;
151                                 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
152                                 foundUsers.add(foundUser);
153                         }
154                 } catch (Exception e) {
155                         logger.error(EELFLoggerDelegate.errorLogger, "searchUsersByName failed", e);
156                 }
157                 return foundUsers;
158         }
159
160         private List<UserWithNameSurnameTitle> removeWrongFirstNames(List<UserWithNameSurnameTitle> resultOfSearch, String firstName) {
161                 firstName = firstName.toUpperCase();
162                 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
163                         UserWithNameSurnameTitle user = resultOfSearch.get(i);
164                         if ((user.firstName == null) || !user.firstName.toUpperCase().startsWith(firstName)) {
165                                 resultOfSearch.remove(i);
166                         }
167                 }
168                 return resultOfSearch;
169         }
170
171         private List<UserWithNameSurnameTitle> removeWrongLastNames(List<UserWithNameSurnameTitle> resultOfSearch, String lastName) {
172                 lastName = lastName.toUpperCase();
173                 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
174                         UserWithNameSurnameTitle user = resultOfSearch.get(i);
175                         if ((user.lastName == null) || !user.lastName.toUpperCase().startsWith(lastName)) {
176                                 resultOfSearch.remove(i);
177                         }
178                 }
179                 return resultOfSearch;
180         }
181
182         private List<UserWithNameSurnameTitle> cutSearchResultToMaximumSize(List<UserWithNameSurnameTitle> resultOfSearch) {
183                 for (int i = resultOfSearch.size() - 1; i >= maxSizeOfSearchResult; i--) {
184                         resultOfSearch.remove(i);
185                 }
186                 return resultOfSearch;
187         }
188
189
190         @SuppressWarnings("rawtypes")
191         @Override
192         public EPUser searchUserByUserId(String orgUserId) {
193                 List<EPUser> foundUsers = new ArrayList<EPUser>();
194                 try {
195                         List searchResult = this.userService.getUserByUserId(orgUserId);
196                         for (Object obj : searchResult) {
197                                 EPUser user = (EPUser) obj;
198                                 foundUsers.add(user);
199                         }
200                 } catch (Exception e) {
201                         logger.error(EELFLoggerDelegate.errorLogger, "searchUserByUserId failed", e);
202                         return null;
203                 }
204                 return foundUsers.get(0);
205         }
206
207 }