e0934713940843823f9d2450538e462c66de3611
[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
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                 while (tokens.size() > 2) { // we use no more then first 2 tokens (userId is removed, see above)
83                         tokens.remove(tokens.size() - 1);
84                 }
85                 EPUser attrUser = new EPUser();
86                 attrUser.setOrgUserId(orgUserId);
87                 List<UserWithNameSurnameTitle> resultOfSearch = new ArrayList<UserWithNameSurnameTitle>(), resultOfAdditionalSearch = null,
88                                 resultOfSearchUserId = new ArrayList<UserWithNameSurnameTitle>();
89                 if (tokens.size() == 2) {
90                         attrUser.setFirstName(tokens.get(0));
91                         attrUser.setLastName(tokens.get(1));
92                         resultOfSearch = this.searchUsersByName(attrUser);
93                         resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
94                         resultOfSearch = this.removeWrongLastNames(resultOfSearch, tokens.get(1));
95                         if (resultOfSearch.size() < maxSizeOfSearchResult) {
96                                 attrUser.setFirstName(tokens.get(1));
97                                 attrUser.setLastName(tokens.get(0));
98                                 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
99                                 resultOfAdditionalSearch = this.removeWrongFirstNames(resultOfAdditionalSearch, tokens.get(1));
100                                 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
101                         }
102                 } else if (tokens.size() == 1) {
103                         attrUser.setFirstName(tokens.get(0));
104                         resultOfSearch = this.searchUsersByName(attrUser);
105                         resultOfSearchUserId = this.searchUsersByUserId(attrUser);
106                         resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
107                         if (resultOfSearch.size() < maxSizeOfSearchResult) {
108                                 attrUser.setFirstName(null);
109                                 attrUser.setLastName(tokens.get(0));
110                                 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
111                                 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
112                         }
113                 }
114                 if (resultOfAdditionalSearch != null) {
115                         resultOfSearch.addAll(resultOfAdditionalSearch);
116                 }
117                 resultOfSearch.addAll(resultOfSearchUserId);
118                 resultOfSearch = this.cutSearchResultToMaximumSize(resultOfSearch);
119                 ObjectMapper mapper = new ObjectMapper();
120                 String result = "[]";
121                 try {
122                         result = mapper.writeValueAsString(resultOfSearch);
123                 } catch (JsonProcessingException e) {
124                         logger.error(EELFLoggerDelegate.errorLogger, "searchUsersInFnTable failed", e);
125                 }
126                 return result;
127         }
128
129         
130         @SuppressWarnings("rawtypes")
131         public List<UserWithNameSurnameTitle> searchUsersByUserId(EPUser attrUser) {
132                 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
133                 try {
134                         List searchResult = this.userService.getUserByUserId(attrUser.getOrgUserId());
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, "searchUsersByUserId failed", e);
142                 }
143                 return foundUsers;
144         }
145         
146         @SuppressWarnings("rawtypes")
147         public List<UserWithNameSurnameTitle> searchUsersByName(EPUser attrUser) {
148                 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
149                 try {
150                         List searchResult = this.userService.getUserByFirstLastName(attrUser.getFirstName(),attrUser.getLastName());
151                         for (Object obj : searchResult) {
152                                 EPUser user = (EPUser) obj;
153                                 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
154                                 foundUsers.add(foundUser);
155                         }
156                 } catch (Exception e) {
157                         logger.error(EELFLoggerDelegate.errorLogger, "searchUsersByName failed", e);
158                 }
159                 return foundUsers;
160         }
161
162         private List<UserWithNameSurnameTitle> removeWrongFirstNames(List<UserWithNameSurnameTitle> resultOfSearch, String firstName) {
163                 firstName = firstName.toUpperCase();
164                 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
165                         UserWithNameSurnameTitle user = resultOfSearch.get(i);
166                         if ((user.firstName == null) || !user.firstName.toUpperCase().startsWith(firstName)) {
167                                 resultOfSearch.remove(i);
168                         }
169                 }
170                 return resultOfSearch;
171         }
172
173         private List<UserWithNameSurnameTitle> removeWrongLastNames(List<UserWithNameSurnameTitle> resultOfSearch, String lastName) {
174                 lastName = lastName.toUpperCase();
175                 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
176                         UserWithNameSurnameTitle user = resultOfSearch.get(i);
177                         if ((user.lastName == null) || !user.lastName.toUpperCase().startsWith(lastName)) {
178                                 resultOfSearch.remove(i);
179                         }
180                 }
181                 return resultOfSearch;
182         }
183
184         private List<UserWithNameSurnameTitle> cutSearchResultToMaximumSize(List<UserWithNameSurnameTitle> resultOfSearch) {
185                 for (int i = resultOfSearch.size() - 1; i >= maxSizeOfSearchResult; i--) {
186                         resultOfSearch.remove(i);
187                 }
188                 return resultOfSearch;
189         }
190
191
192         @SuppressWarnings("rawtypes")
193         @Override
194         public EPUser searchUserByUserId(String orgUserId) {
195                 List<EPUser> foundUsers = new ArrayList<EPUser>();
196                 try {
197                         List searchResult = this.userService.getUserByUserId(orgUserId);
198                         for (Object obj : searchResult) {
199                                 EPUser user = (EPUser) obj;
200                                 foundUsers.add(user);
201                         }
202                 } catch (Exception e) {
203                         logger.error(EELFLoggerDelegate.errorLogger, "searchUserByUserId failed", e);
204                         return null;
205                 }
206                 return foundUsers.get(0);
207         }
208
209 }