AccountServiceDefault.java
2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package org.legrog.web.account;
import org.legrog.entities.*;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
@Stateless
public class AccountServiceDefault implements AccountService {
AccountRepository accountRepository;
AccountSearchRepository accountSearchRepository;
/**
* Le service s'appuie concrètement sur un ensemble de dépôts et sur le service auxiliaire SharedService.
*
* @param accountRepository
* @param accountSearchRepository
*/
@Inject
public AccountServiceDefault(AccountRepository accountRepository,
AccountSearchRepository accountSearchRepository) {
this.accountRepository = accountRepository;
this.accountSearchRepository = accountSearchRepository;
}
AccountServiceDefault() {
//no args constructor to make it proxyable
}
public void addUser(Account account) {
accountRepository.save(account);
}
public List<Account> getAllUsers() {
return accountRepository.findAll();
}
public Account findUserById(int id) {
return accountRepository.findOne(new Integer(id));
}
public void updateUser(Account account) {
accountRepository.save(account);
}
@Override
public List<Account> search(@NotNull String string) throws SearchingException {
return convertIndexedAccountsIntoAccounts(accountSearchRepository.search(string));
}
@Override
public List<Account> convertIndexedAccountsIntoAccounts(List<IndexedAccount> indexedAccounts) {
List<Integer> integers = new ArrayList<>(indexedAccounts.size());
indexedAccounts.forEach(indexedAccount -> integers.add(indexedAccount.getUserId()));
if (!integers.isEmpty()) {
return accountRepository.findByUserIdIn(integers);
}
return new ArrayList<>();
}
protected List<IndexedAccount> convertAccountsIntoIndexedAccounts(List<Account> accounts) {
List<IndexedAccount> indexedAccounts = new ArrayList<>(accounts.size());
accounts.forEach(account -> indexedAccounts.add(new IndexedAccount(account)));
return indexedAccounts;
}
@Override
public int reindexAllAccounts() throws IndexingException {
List<Account> accounts = accountRepository.findByPresentationIsNotNull();
List<IndexedAccount> indexedAccounts = convertAccountsIntoIndexedAccounts(accounts);
accountSearchRepository.reindex(indexedAccounts);
return indexedAccounts.size();
}
}