2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ===================================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
28 * https://creativecommons.org/licenses/by/4.0/
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.
36 * ============LICENSE_END============================================
41 package org.onap.portal.service;
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;
59 public class SearchService {
61 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SearchService.class);
63 private static final int maxSizeOfSearchResult = 100;
65 private final FnUserService userService;
68 public SearchService(FnUserService userService) {
69 this.userService = userService;
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);
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));
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));
106 if (resultOfAdditionalSearch != null) {
107 resultOfSearch.addAll(resultOfAdditionalSearch);
109 resultOfSearch.addAll(resultOfSearchUserId);
110 resultOfSearch.stream().distinct().collect(Collectors.toList());
111 resultOfSearch = this.cutSearchResultToMaximumSize(resultOfSearch);
112 ObjectMapper mapper = new ObjectMapper();
113 String result = "[]";
115 result = mapper.writeValueAsString(resultOfSearch);
116 } catch (JsonProcessingException e) {
117 logger.error(EELFLoggerDelegate.errorLogger, "searchUsersInFnTable failed", e);
122 public List<UserWithNameSurnameTitle> searchUsersByUserId(FnUser attrUser) {
123 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
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);
130 } catch (Exception e) {
131 logger.error(EELFLoggerDelegate.errorLogger, "searchUsersByUserId failed", e);
136 public List<UserWithNameSurnameTitle> searchUsersByName(FnUser attrUser) {
137 List<UserWithNameSurnameTitle> foundUsers = new ArrayList<UserWithNameSurnameTitle>();
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);
145 } catch (Exception e) {
146 logger.error(EELFLoggerDelegate.errorLogger, "searchUsersByName failed", e);
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);
159 return resultOfSearch;
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);
170 return resultOfSearch;
173 private List<UserWithNameSurnameTitle> cutSearchResultToMaximumSize(List<UserWithNameSurnameTitle> resultOfSearch) {
174 if (resultOfSearch.size() > maxSizeOfSearchResult) {
175 resultOfSearch.subList(maxSizeOfSearchResult, resultOfSearch.size()).clear();
177 return resultOfSearch;