login and Certman AAF Integration changes
[portal.git] / ecomp-portal-BE-common / 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.service.SearchService;
44 import org.onap.portalapp.portal.service.SearchServiceImpl;
45 import org.onap.portalapp.portal.domain.EPUser;
46 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
47 import org.onap.portalapp.portal.transport.UserWithNameSurnameTitle;
48 import org.onap.portalapp.portal.utils.EcompPortalUtils;
49 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.context.annotation.EnableAspectJAutoProxy;
52 import org.springframework.stereotype.Service;
53 import org.springframework.transaction.annotation.Transactional;
54
55 import com.fasterxml.jackson.core.JsonProcessingException;
56 import com.fasterxml.jackson.databind.ObjectMapper;
57
58 @Service("searchService")
59 @Transactional
60 @org.springframework.context.annotation.Configuration
61 @EnableAspectJAutoProxy
62 @EPMetricsLog
63 public class SearchServiceImpl implements SearchService {
64         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SearchServiceImpl.class);
65         
66         // TODO: the values below should be defined in other place
67         private static final int maxSizeOfSearchResult = 100;
68
69
70         @Autowired
71         EPLdapService ldapService;
72
73         @Override
74         public String searchUsersInPhoneBook(String searchString) {
75                 String orgUserId = null;
76                 List<String> tokens = EcompPortalUtils.parsingByRegularExpression(searchString, " ");
77                 for (int i = 0; i < tokens.size(); i++) { // find orgUserId if possible and remove it from tokens
78                         if (tokens.get(i).matches(".*\\d+.*")) {
79                                 orgUserId = tokens.get(i);
80                                 tokens.remove(i);
81                         }
82                 }
83                 while (tokens.size() > 2) { // we use no more then first 2 tokens (orgUserId is removed, see above)
84                         tokens.remove(tokens.size() - 1);
85                 }
86                 EPUser attrUser = new EPUser();
87                 attrUser.setOrgUserId(orgUserId);
88                 List<UserWithNameSurnameTitle> resultOfSearch = new ArrayList<UserWithNameSurnameTitle>(), resultOfAdditionalSearch = null;
89                 if (tokens.size() == 2) {
90                         attrUser.setFirstName(tokens.get(0));
91                         attrUser.setLastName(tokens.get(1));
92                         resultOfSearch = this.searchUsersByAttrs(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.searchUsersByAttrs(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.searchUsersByAttrs(attrUser);
105                         resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
106                         if (resultOfSearch.size() < maxSizeOfSearchResult) {
107                                 attrUser.setFirstName(null);
108                                 attrUser.setLastName(tokens.get(0));
109                                 resultOfAdditionalSearch = this.searchUsersByAttrs(attrUser);
110                                 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
111                         }
112                 } else if (orgUserId != null) {
113                         resultOfSearch = this.searchUsersByAttrs(attrUser);
114                 }
115                 if (resultOfAdditionalSearch != null) {
116                         resultOfSearch.addAll(resultOfAdditionalSearch);
117                 }
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, "searchUsersInPhoneBook failed", e);
125                 }
126                 return result;
127         }
128
129         private List<UserWithNameSurnameTitle> searchUsersByAttrs(EPUser attrUser) {
130                 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
131                 try {
132                         org.onap.portalsdk.core.command.support.SearchResult searchResult = this.ldapService.searchPost(attrUser, null, null, null, 0, 0, -1);
133                         for (Object obj : searchResult) {
134                                 EPUser user = (EPUser) obj;
135                                 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
136                                 foundUsers.add(foundUser);
137                         }
138                 } catch (Exception e) {
139                         logger.error(EELFLoggerDelegate.errorLogger, "searchUsersByAttrs failed", e);
140                 }
141                 return foundUsers;
142         }
143
144         private List<UserWithNameSurnameTitle> removeWrongFirstNames(List<UserWithNameSurnameTitle> resultOfSearch, String firstName) {
145                 firstName = firstName.toUpperCase();
146                 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
147                         UserWithNameSurnameTitle user = resultOfSearch.get(i);
148                         if ((user.firstName == null) || !user.firstName.toUpperCase().startsWith(firstName)) {
149                                 resultOfSearch.remove(i);
150                         }
151                 }
152                 return resultOfSearch;
153         }
154
155         private List<UserWithNameSurnameTitle> removeWrongLastNames(List<UserWithNameSurnameTitle> resultOfSearch, String lastName) {
156                 lastName = lastName.toUpperCase();
157                 for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
158                         UserWithNameSurnameTitle user = resultOfSearch.get(i);
159                         if ((user.lastName == null) || !user.lastName.toUpperCase().startsWith(lastName)) {
160                                 resultOfSearch.remove(i);
161                         }
162                 }
163                 return resultOfSearch;
164         }
165
166         private List<UserWithNameSurnameTitle> cutSearchResultToMaximumSize(List<UserWithNameSurnameTitle> resultOfSearch) {
167                 for (int i = resultOfSearch.size() - 1; i >= maxSizeOfSearchResult; i--) {
168                         resultOfSearch.remove(i);
169                 }
170                 return resultOfSearch;
171         }
172
173         @Override
174         @SuppressWarnings("unchecked")
175         public EPUser searchUserByUserId(String orgUserId) {
176                 EPUser user = null;
177                 EPUser searchedUser = new EPUser();
178                 searchedUser.setOrgUserId(orgUserId);
179                 try {
180                         List<Object> searchResult = ldapService.searchPost(searchedUser, "", null, null, 0, -1, 1);
181                         for (Object obj : searchResult) {
182                                 if (obj instanceof EPUser) {
183                                         user = (EPUser) obj;
184                                         // This assignment should be checked later!
185                                         user.setLoginId(orgUserId);
186                                         break;
187                                 }
188                         }
189                 } catch (Exception e) {
190                         logger.error(EELFLoggerDelegate.errorLogger, "searchUserByUserId failed", e);
191                 }
192                 return user;
193         }
194
195 }