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