HealthCheckController up
[portal.git] / portal-BE / src / main / java / org / onap / portal / service / SearchService.java
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *
10  * Unless otherwise specified, all software contained herein is licensed
11  * under the Apache License, Version 2.0 (the "License");
12  * you may not use this software except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Unless otherwise specified, all documentation contained herein is licensed
24  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25  * you may not use this documentation except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
30  * Unless required by applicable law or agreed to in writing, documentation
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  *
36  * ============LICENSE_END============================================
37  *
38  *
39  */
40
41 package org.onap.portal.service;
42
43 import com.fasterxml.jackson.core.JsonProcessingException;
44 import com.fasterxml.jackson.databind.ObjectMapper;
45 import java.util.ArrayList;
46 import java.util.List;
47 import java.util.stream.Collectors;
48 import org.onap.portal.domain.db.fn.FnUser;
49 import org.onap.portal.domain.dto.transport.UserWithNameSurnameTitle;
50 import org.onap.portal.service.user.FnUserService;
51 import org.onap.portal.utils.EcompPortalUtils;
52 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.stereotype.Service;
55 import org.springframework.transaction.annotation.Transactional;
56
57 @Service
58 @Transactional
59 public class SearchService {
60
61     EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SearchService.class);
62
63     private static final int maxSizeOfSearchResult = 100;
64
65     private final FnUserService userService;
66
67     @Autowired
68     public SearchService(FnUserService userService) {
69         this.userService = userService;
70     }
71
72     public String searchUsersInPhoneBook(final String searchString) {
73         List<String> tokens = EcompPortalUtils.parsingByRegularExpression(searchString, " ");
74         while (tokens.size() > 2) { // we use no more then first 2 tokens (userId is removed, see above)
75             tokens.remove(tokens.size() - 1);
76         }
77         FnUser attrUser = new FnUser();
78         List<UserWithNameSurnameTitle> resultOfSearch = new ArrayList<UserWithNameSurnameTitle>(), resultOfAdditionalSearch = null,
79             resultOfSearchUserId = new ArrayList<UserWithNameSurnameTitle>();
80         if (tokens.size() == 2) {
81             attrUser.setFirstName(tokens.get(0));
82             attrUser.setLastName(tokens.get(1));
83             resultOfSearch = this.searchUsersByName(attrUser);
84             resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
85             resultOfSearch = this.removeWrongLastNames(resultOfSearch, tokens.get(1));
86             if (resultOfSearch.size() < maxSizeOfSearchResult) {
87                 attrUser.setFirstName(tokens.get(1));
88                 attrUser.setLastName(tokens.get(0));
89                 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
90                 resultOfAdditionalSearch = this.removeWrongFirstNames(resultOfAdditionalSearch, tokens.get(1));
91                 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
92             }
93         } else if (tokens.size() == 1) {
94             attrUser.setFirstName(tokens.get(0));
95             attrUser.setOrgUserId(tokens.get(0));
96             resultOfSearch = this.searchUsersByName(attrUser);
97             resultOfSearchUserId = this.searchUsersByUserId(attrUser);
98             resultOfSearch = this.removeWrongFirstNames(resultOfSearch, tokens.get(0));
99             if (resultOfSearch.size() < maxSizeOfSearchResult) {
100                 attrUser.setFirstName(null);
101                 attrUser.setLastName(tokens.get(0));
102                 resultOfAdditionalSearch = this.searchUsersByName(attrUser);
103                 resultOfAdditionalSearch = this.removeWrongLastNames(resultOfAdditionalSearch, tokens.get(0));
104             }
105         }
106         if (resultOfAdditionalSearch != null) {
107             resultOfSearch.addAll(resultOfAdditionalSearch);
108         }
109         resultOfSearch.addAll(resultOfSearchUserId);
110         resultOfSearch.stream().distinct().collect(Collectors.toList());
111         resultOfSearch = this.cutSearchResultToMaximumSize(resultOfSearch);
112         ObjectMapper mapper = new ObjectMapper();
113         String result = "[]";
114         try {
115             result = mapper.writeValueAsString(resultOfSearch);
116         } catch (JsonProcessingException e) {
117             logger.error(EELFLoggerDelegate.errorLogger, "searchUsersInFnTable failed", e);
118         }
119         return result;
120     }
121
122     public List<UserWithNameSurnameTitle> searchUsersByUserId(FnUser attrUser) {
123         List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
124         try {
125             List<FnUser> searchResult = this.userService.getUserByUserId(attrUser.getOrgUserId());
126             for (FnUser user : searchResult) {
127                 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
128                 foundUsers.add(foundUser);
129             }
130         } catch (Exception e) {
131             logger.error(EELFLoggerDelegate.errorLogger, "searchUsersByUserId failed", e);
132         }
133         return foundUsers;
134     }
135
136     public List<UserWithNameSurnameTitle> searchUsersByName(FnUser attrUser) {
137         List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
138         try {
139             List<FnUser> searchResult = this.userService.getUserByFirstLastName(attrUser.getFirstName(),attrUser.getLastName());
140             for (Object obj : searchResult) {
141                 FnUser user = (FnUser) obj;
142                 UserWithNameSurnameTitle foundUser = new UserWithNameSurnameTitle(user.getOrgUserId(), user.getFirstName(), user.getLastName(), user.getJobTitle());
143                 foundUsers.add(foundUser);
144             }
145         } catch (Exception e) {
146             logger.error(EELFLoggerDelegate.errorLogger, "searchUsersByName failed", e);
147         }
148         return foundUsers;
149     }
150
151     private List<UserWithNameSurnameTitle> removeWrongFirstNames(List<UserWithNameSurnameTitle> resultOfSearch, String firstName) {
152         firstName = firstName.toUpperCase();
153         for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
154             UserWithNameSurnameTitle user = resultOfSearch.get(i);
155             if ((user.getFirstName() == null) || !user.getFirstName().toUpperCase().startsWith(firstName)) {
156                 resultOfSearch.remove(i);
157             }
158         }
159         return resultOfSearch;
160     }
161
162     private List<UserWithNameSurnameTitle> removeWrongLastNames(List<UserWithNameSurnameTitle> resultOfSearch, String lastName) {
163         lastName = lastName.toUpperCase();
164         for (int i = resultOfSearch.size() - 1; i >= 0; i--) {
165             UserWithNameSurnameTitle user = resultOfSearch.get(i);
166             if ((user.getLastName() == null) || !user.getLastName().toUpperCase().startsWith(lastName)) {
167                 resultOfSearch.remove(i);
168             }
169         }
170         return resultOfSearch;
171     }
172
173     private List<UserWithNameSurnameTitle> cutSearchResultToMaximumSize(List<UserWithNameSurnameTitle> resultOfSearch) {
174         if (resultOfSearch.size() > maxSizeOfSearchResult) {
175             resultOfSearch.subList(maxSizeOfSearchResult, resultOfSearch.size()).clear();
176         }
177         return resultOfSearch;
178     }
179 }