nexus site path corrected
[portal.git] / ecomp-portal-BE / 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         
53         @Override
54         public String searchUsersInFnTable(String searchString) {
55                 String orgUserId = null;
56                 List<String> tokens = EcompPortalUtils.parsingByRegularExpression(searchString, " ");
57                 for (int i = 0; i < tokens.size(); i++) { // find userid if possible and remove it from tokens
58                         if (tokens.get(i).matches(".*\\d+.*")) {
59                                 orgUserId = tokens.get(i);
60                                 tokens.remove(i);
61                         }
62                 }
63                 while (tokens.size() > 2) { // we use no more then first 2 tokens (userId is removed, see above)
64                         tokens.remove(tokens.size() - 1);
65                 }
66                 EPUser attrUser = new EPUser();
67                 attrUser.setOrgUserId(orgUserId);
68                 List<UserWithNameSurnameTitle> resultOfSearch = new ArrayList<UserWithNameSurnameTitle>(), resultOfAdditionalSearch = null;
69                 if (tokens.size() == 2) {
70                         attrUser.setFirstName(tokens.get(0));
71                         attrUser.setLastName(tokens.get(1));
72                         resultOfSearch = this.searchUsersByName(attrUser);
73                         resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
74                         resultOfSearch = this.removeWrongLastNames(resultOfSearch, tokens.get(1));
75                         if (resultOfSearch.size() < maxSizeOfSearchResult) {
76                                 attrUser.setFirstName(tokens.get(1));
77                                 attrUser.setLastName(tokens.get(0));
78                                 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
79                                 resultOfAdditionalSearch = this.removeWrongFirstNames(resultOfAdditionalSearch, tokens.get(1));
80                                 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
81                         }
82                 } else if (tokens.size() == 1) {
83                         attrUser.setFirstName(tokens.get(0));
84                         resultOfSearch = this.searchUsersByName(attrUser);
85                         resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
86                         if (resultOfSearch.size() < maxSizeOfSearchResult) {
87                                 attrUser.setFirstName(null);
88                                 attrUser.setLastName(tokens.get(0));
89                                 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
90                                 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
91                         }
92                 } else if (orgUserId != null) {
93                         resultOfSearch = this.searchUsersByUserId(attrUser);
94                 }
95                 if (resultOfAdditionalSearch != null) {
96                         resultOfSearch.addAll(resultOfAdditionalSearch);
97                 }
98                 resultOfSearch = this.cutSearchResultToMaximumSize(resultOfSearch);
99                 ObjectMapper mapper = new ObjectMapper();
100                 String result = "[]";
101                 try {
102                         result = mapper.writeValueAsString(resultOfSearch);
103                 } catch (JsonProcessingException e) {
104                         logger.error(EELFLoggerDelegate.errorLogger, "searchUsersInPhoneBook Exception = " + EcompPortalUtils.getStackTrace(e));
105                 }
106                 return result;
107         }
108
109         
110         @SuppressWarnings("rawtypes")
111         public List<UserWithNameSurnameTitle> searchUsersByUserId(EPUser attrUser) {
112                 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
113                 try {
114                         List searchResult = this.userService.getUserByUserId(attrUser.getOrgUserId());
115                         for (Object obj : searchResult) {
116                                 EPUser user = (EPUser) obj;
117                                 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
118                                 foundUsers.add(foundUser);
119                         }
120                 } catch (Exception e) {
121                         logger.error(EELFLoggerDelegate.errorLogger, "searchInPhoneBookWithToken Exception = " + EcompPortalUtils.getStackTrace(e));
122                 }
123                 return foundUsers;
124         }
125         
126         @SuppressWarnings("rawtypes")
127         public List<UserWithNameSurnameTitle> searchUsersByName(EPUser attrUser) {
128                 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
129                 try {
130                         List searchResult = this.userService.getUserByFirstLastName(attrUser.getFirstName(),attrUser.getLastName());
131                         for (Object obj : searchResult) {
132                                 EPUser user = (EPUser) obj;
133                                 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
134                                 foundUsers.add(foundUser);
135                         }
136                 } catch (Exception e) {
137                         logger.error(EELFLoggerDelegate.errorLogger, "searchInPhoneBookWithToken Exception = " + EcompPortalUtils.getStackTrace(e));
138                 }
139                 return foundUsers;
140         }
141
142         private List<UserWithNameSurnameTitle> removeWrongFirstNames(List<UserWithNameSurnameTitle> resultOfSearch, String firstName) {
143                 firstName = firstName.toUpperCase();
144                 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
145                         UserWithNameSurnameTitle user = resultOfSearch.get(i);
146                         if ((user.firstName == null) || !user.firstName.toUpperCase().startsWith(firstName)) {
147                                 resultOfSearch.remove(i);
148                         }
149                 }
150                 return resultOfSearch;
151         }
152
153         private List<UserWithNameSurnameTitle> removeWrongLastNames(List<UserWithNameSurnameTitle> resultOfSearch, String lastName) {
154                 lastName = lastName.toUpperCase();
155                 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
156                         UserWithNameSurnameTitle user = resultOfSearch.get(i);
157                         if ((user.lastName == null) || !user.lastName.toUpperCase().startsWith(lastName)) {
158                                 resultOfSearch.remove(i);
159                         }
160                 }
161                 return resultOfSearch;
162         }
163
164         private List<UserWithNameSurnameTitle> cutSearchResultToMaximumSize(List<UserWithNameSurnameTitle> resultOfSearch) {
165                 for (int i = resultOfSearch.size() - 1; i >= maxSizeOfSearchResult; i--) {
166                         resultOfSearch.remove(i);
167                 }
168                 return resultOfSearch;
169         }
170
171
172         @SuppressWarnings("rawtypes")
173         @Override
174         public EPUser searchUserByUserId(String orgUserId) {
175                 List<EPUser> foundUsers = new ArrayList<EPUser>();
176                 try {
177                         List searchResult = this.userService.getUserByUserId(orgUserId);
178                         for (Object obj : searchResult) {
179                                 EPUser user = (EPUser) obj;
180                                 foundUsers.add(user);
181                         }
182                 } catch (Exception e) {
183                         logger.error(EELFLoggerDelegate.errorLogger, "searchInPhoneBookWithToken Exception = " + EcompPortalUtils.getStackTrace(e));
184                 }
185                 return foundUsers.get(0);
186         }
187
188 }