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