[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-os / src / main / java / org / openecomp / portalapp / portal / service / SearchServiceImpl.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.service;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.openecomp.portalapp.portal.domain.EPUser;
26 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
27 import org.openecomp.portalapp.portal.transport.UserWithNameSurnameTitle;
28 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
29 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.context.annotation.EnableAspectJAutoProxy;
32 import org.springframework.stereotype.Service;
33 import org.springframework.transaction.annotation.Transactional;
34
35 import com.fasterxml.jackson.core.JsonProcessingException;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37
38 @Service("searchService")
39 @Transactional
40 @org.springframework.context.annotation.Configuration
41 @EnableAspectJAutoProxy
42 @EPMetricsLog
43 public class SearchServiceImpl implements SearchService {
44         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SearchServiceImpl.class);
45         
46         // TODO: the values below should be defined in other place
47         private static final int maxSizeOfSearchResult = 100;
48
49         @Autowired
50         UserService userService;
51
52         @Override
53         public String searchUsersInPhoneBook(String searchString) {
54                 return searchUsersInFnTable(searchString);
55         }
56         
57         @Override
58         public String searchUsersInFnTable(String searchString) {
59                 String orgUserId = null;
60                 List<String> tokens = EcompPortalUtils.parsingByRegularExpression(searchString, " ");
61                 for (int i = 0; i < tokens.size(); i++) { // find userid if possible and remove it from tokens
62                         if (tokens.get(i).matches(".*\\d+.*")) {
63                                 orgUserId = tokens.get(i);
64                                 tokens.remove(i);
65                         }
66                 }
67                 while (tokens.size() > 2) { // we use no more then first 2 tokens (userId is removed, see above)
68                         tokens.remove(tokens.size() - 1);
69                 }
70                 EPUser attrUser = new EPUser();
71                 attrUser.setOrgUserId(orgUserId);
72                 List<UserWithNameSurnameTitle> resultOfSearch = new ArrayList<UserWithNameSurnameTitle>(), resultOfAdditionalSearch = null;
73                 if (tokens.size() == 2) {
74                         attrUser.setFirstName(tokens.get(0));
75                         attrUser.setLastName(tokens.get(1));
76                         resultOfSearch = this.searchUsersByName(attrUser);
77                         resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
78                         resultOfSearch = this.removeWrongLastNames(resultOfSearch, tokens.get(1));
79                         if (resultOfSearch.size() < maxSizeOfSearchResult) {
80                                 attrUser.setFirstName(tokens.get(1));
81                                 attrUser.setLastName(tokens.get(0));
82                                 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
83                                 resultOfAdditionalSearch = this.removeWrongFirstNames(resultOfAdditionalSearch, tokens.get(1));
84                                 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
85                         }
86                 } else if (tokens.size() == 1) {
87                         attrUser.setFirstName(tokens.get(0));
88                         resultOfSearch = this.searchUsersByName(attrUser);
89                         resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
90                         if (resultOfSearch.size() < maxSizeOfSearchResult) {
91                                 attrUser.setFirstName(null);
92                                 attrUser.setLastName(tokens.get(0));
93                                 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
94                                 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
95                         }
96                 } else if (orgUserId != null) {
97                         resultOfSearch = this.searchUsersByUserId(attrUser);
98                 }
99                 if (resultOfAdditionalSearch != null) {
100                         resultOfSearch.addAll(resultOfAdditionalSearch);
101                 }
102                 resultOfSearch = this.cutSearchResultToMaximumSize(resultOfSearch);
103                 ObjectMapper mapper = new ObjectMapper();
104                 String result = "[]";
105                 try {
106                         result = mapper.writeValueAsString(resultOfSearch);
107                 } catch (JsonProcessingException e) {
108                         logger.error(EELFLoggerDelegate.errorLogger, "searchUsersInPhoneBook Exception = " + EcompPortalUtils.getStackTrace(e));
109                 }
110                 return result;
111         }
112
113         
114         @SuppressWarnings("rawtypes")
115         public List<UserWithNameSurnameTitle> searchUsersByUserId(EPUser attrUser) {
116                 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
117                 try {
118                         List searchResult = this.userService.getUserByUserId(attrUser.getOrgUserId());
119                         for (Object obj : searchResult) {
120                                 EPUser user = (EPUser) obj;
121                                 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
122                                 foundUsers.add(foundUser);
123                         }
124                 } catch (Exception e) {
125                         logger.error(EELFLoggerDelegate.errorLogger, "searchInPhoneBookWithToken Exception = " + EcompPortalUtils.getStackTrace(e));
126                 }
127                 return foundUsers;
128         }
129         
130         @SuppressWarnings("rawtypes")
131         public List<UserWithNameSurnameTitle> searchUsersByName(EPUser attrUser) {
132                 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
133                 try {
134                         List searchResult = this.userService.getUserByFirstLastName(attrUser.getFirstName(),attrUser.getLastName());
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, "searchInPhoneBookWithToken Exception = " + EcompPortalUtils.getStackTrace(e));
142                 }
143                 return foundUsers;
144         }
145
146         private List<UserWithNameSurnameTitle> removeWrongFirstNames(List<UserWithNameSurnameTitle> resultOfSearch, String firstName) {
147                 firstName = firstName.toUpperCase();
148                 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
149                         UserWithNameSurnameTitle user = resultOfSearch.get(i);
150                         if ((user.firstName == null) || !user.firstName.toUpperCase().startsWith(firstName)) {
151                                 resultOfSearch.remove(i);
152                         }
153                 }
154                 return resultOfSearch;
155         }
156
157         private List<UserWithNameSurnameTitle> removeWrongLastNames(List<UserWithNameSurnameTitle> resultOfSearch, String lastName) {
158                 lastName = lastName.toUpperCase();
159                 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
160                         UserWithNameSurnameTitle user = resultOfSearch.get(i);
161                         if ((user.lastName == null) || !user.lastName.toUpperCase().startsWith(lastName)) {
162                                 resultOfSearch.remove(i);
163                         }
164                 }
165                 return resultOfSearch;
166         }
167
168         private List<UserWithNameSurnameTitle> cutSearchResultToMaximumSize(List<UserWithNameSurnameTitle> resultOfSearch) {
169                 for (int i = resultOfSearch.size() - 1; i >= maxSizeOfSearchResult; i--) {
170                         resultOfSearch.remove(i);
171                 }
172                 return resultOfSearch;
173         }
174
175
176         @SuppressWarnings("rawtypes")
177         @Override
178         public EPUser searchUserByUserId(String orgUserId) {
179                 List<EPUser> foundUsers = new ArrayList<EPUser>();
180                 try {
181                         List searchResult = this.userService.getUserByUserId(orgUserId);
182                         for (Object obj : searchResult) {
183                                 EPUser user = (EPUser) obj;
184                                 foundUsers.add(user);
185                         }
186                 } catch (Exception e) {
187                         logger.error(EELFLoggerDelegate.errorLogger, "searchInPhoneBookWithToken Exception = " + EcompPortalUtils.getStackTrace(e));
188                 }
189                 return foundUsers.get(0);
190         }
191
192 }