1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveboard.business; |
16 | |
17 | import java.util.HashMap; |
18 | import java.util.List; |
19 | import java.util.Map; |
20 | |
21 | import net.sourceforge.hiveboard.Account; |
22 | import net.sourceforge.hiveboard.dao.AccountDAO; |
23 | |
24 | /** |
25 | * Implementation of the Account Repository service. |
26 | * The repository is initalized from the database at construction time. |
27 | * |
28 | * @author Jean-Francois Poilpret |
29 | */ |
30 | public class AccountRepositoryImpl implements AccountRepository |
31 | { |
32 | public AccountRepositoryImpl(AccountDAO dao) |
33 | { |
34 | // Read all Accounts |
35 | List<Account> accounts = dao.selectActiveAccounts(); |
36 | for (Account account: accounts) |
37 | { |
38 | _accountsById.put(account.getId(), account); |
39 | _accountsByVisa.put(account.getVisa(), account); |
40 | } |
41 | _maxDiscriminator = dao.selectMaxDiscriminator(); |
42 | } |
43 | |
44 | synchronized public Account getAccount(int id, boolean clone) |
45 | { |
46 | Account account = _accountsById.get(id); |
47 | if (account == null) |
48 | { |
49 | return null; |
50 | } |
51 | if (clone) |
52 | { |
53 | return account.copy(); |
54 | } |
55 | else |
56 | { |
57 | return account; |
58 | } |
59 | } |
60 | |
61 | synchronized public Account getAccount(String visa, boolean clone) |
62 | { |
63 | Account account = _accountsByVisa.get(visa); |
64 | if (account == null) |
65 | { |
66 | return null; |
67 | } |
68 | if (clone) |
69 | { |
70 | return account.copy(); |
71 | } |
72 | else |
73 | { |
74 | return account; |
75 | } |
76 | } |
77 | |
78 | synchronized public void addAccount(Account account) |
79 | { |
80 | if (account == null) |
81 | { |
82 | return; |
83 | } |
84 | _accountsById.put(account.getId(), account); |
85 | _accountsByVisa.put(account.getVisa(), account); |
86 | } |
87 | |
88 | synchronized public void changeAccount(Account account) |
89 | { |
90 | if (account == null) |
91 | { |
92 | return; |
93 | } |
94 | int id = account.getId(); |
95 | Account old = _accountsById.get(id); |
96 | if (old == null) |
97 | { |
98 | return; |
99 | } |
100 | String password = account.getPassword(); |
101 | account.setPassword(old.getPassword()); |
102 | _accountsById.put(id, account); |
103 | _accountsByVisa.remove(old.getVisa()); |
104 | _accountsByVisa.put(account.getVisa(), account); |
105 | account.setPassword(password); |
106 | } |
107 | |
108 | synchronized public int removeAccount(int id) |
109 | { |
110 | Account account = _accountsById.remove(id); |
111 | if (account == null) |
112 | { |
113 | return -1; |
114 | } |
115 | _accountsByVisa.remove(account.getVisa()); |
116 | _maxDiscriminator++; |
117 | return _maxDiscriminator; |
118 | } |
119 | |
120 | synchronized public Account[] getAccounts(boolean clone) |
121 | { |
122 | Account[] accounts = _accountsById.values().toArray( |
123 | new Account[_accountsById.size()]); |
124 | if (clone) |
125 | { |
126 | for (int i = 0; i < accounts.length; i++) |
127 | { |
128 | accounts[i] = accounts[i].copy(); |
129 | accounts[i].setPassword(null); |
130 | } |
131 | } |
132 | return accounts; |
133 | } |
134 | |
135 | private final Map<Integer, Account> _accountsById = new HashMap<Integer, Account>(); |
136 | private final Map<String, Account> _accountsByVisa = new HashMap<String, Account>(); |
137 | private int _maxDiscriminator; |
138 | } |