]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebBean.java
seperated user init and method invocation
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / addressbook / AddressbookWebBean.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.addressbook.beans.addressbook;
18
19 import java.text.MessageFormat;
20 import java.util.ArrayList;
21 import java.util.Calendar;
22 import java.util.Collections;
23 import java.util.GregorianCalendar;
24 import java.util.List;
25 import java.util.Objects;
26 import java.util.concurrent.ConcurrentHashMap;
27 import java.util.concurrent.ConcurrentMap;
28 import javax.annotation.PostConstruct;
29 import javax.enterprise.context.SessionScoped;
30 import javax.enterprise.event.Observes;
31 import javax.faces.view.facelets.FaceletException;
32 import javax.inject.Inject;
33 import javax.inject.Named;
34 import javax.naming.Context;
35 import javax.naming.InitialContext;
36 import javax.naming.NamingException;
37 import org.mxchange.addressbook.beans.login.UserLoginWebController;
38 import org.mxchange.addressbook.events.addressbook.AddressbookLoadedEvent;
39 import org.mxchange.addressbook.exceptions.AddressbookNameAlreadyUsedException;
40 import org.mxchange.addressbook.model.addressbook.Addressbook;
41 import org.mxchange.addressbook.model.addressbook.AddressbookSessionBeanRemote;
42 import org.mxchange.addressbook.model.addressbook.UserAddressbook;
43 import org.mxchange.addressbook.model.addressbook.entry.AddressbookEntry;
44 import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook;
45 import org.mxchange.addressbook.model.addressbook.status.AddressbokStatus;
46 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
47 import org.mxchange.jusercore.model.user.User;
48
49 /**
50  * An address book bean (controller)
51  * <p>
52  * @author Roland Haeder<roland@mxchange.org>
53  */
54 @Named ("addressbookController")
55 @SessionScoped
56 public class AddressbookWebBean implements AddressbookWebController {
57
58         /**
59          * Map for count of user's shared addresses
60          */
61         private static ConcurrentMap<User, Integer> countSharesList;
62
63         /**
64          * Serial number
65          */
66         private static final long serialVersionUID = 185_781_756_712_969L;
67
68         /**
69          * Address book instance
70          */
71         private Addressbook addressbook;
72
73         /**
74          * Remote address book bean
75          */
76         private AddressbookSessionBeanRemote addressbookBean;
77
78         /**
79          * When this address book has been created
80          */
81         private Calendar addressbookCreated;
82
83         /**
84          * Address book id number (from URL for example)
85          */
86         private Long addressbookId;
87
88         /**
89          * Name of the address book
90          */
91         private String addressbookName;
92
93         /**
94          * Who owns this address book
95          */
96         private User addressbookUser;
97
98         /**
99          * Login controller
100          */
101         @Inject
102         private UserLoginWebController loginController;
103
104         /**
105          * A list of all user's shared (with others) address books
106          */
107         private List<ShareableAddressbook> sharedAddressbooks;
108
109         /**
110          * A list of all user's address books
111          */
112         private List<Addressbook> usersAddressbooks;
113
114         /**
115          * Default constructor
116          */
117         public AddressbookWebBean () {
118                 // Try it
119                 try {
120                         // Get initial context
121                         Context context = new InitialContext();
122
123                         // Try to lookup
124                         this.addressbookBean = (AddressbookSessionBeanRemote) context.lookup("ejb/stateless-addressbook"); //NOI18N
125                 } catch (final NamingException e) {
126                         // Throw again
127                         throw new FaceletException(e);
128                 }
129
130                 // Init list
131                 AddressbookWebBean.countSharesList = new ConcurrentHashMap<>(0);
132         }
133
134         @Override
135         public String addAddressbook () {
136                 // Is this name already used?
137                 if (!this.loginController.isUserLoggedIn()) {
138                         // Not logged in
139                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
140                 } else if (this.getAddressbookName() == null) {
141                         // Address book name is null
142                         throw new NullPointerException("addressbookName is null"); //NOI18N
143                 } else if (this.getAddressbookName().isEmpty()) {
144                         // Address book name is empty
145                         throw new IllegalStateException("addressbookName is empty."); //NOI18N
146                 } else if (this.isAddressbookNameUsed(this.getAddressbookName())) {
147                         // Already used by this user
148                         throw new FaceletException(MessageFormat.format("Address book name {0} already used.", this.getAddressbookName())); //NOI18N
149                 }
150
151                 // Create address book instance with name
152                 Addressbook book = new UserAddressbook(this.getAddressbookName());
153
154                 // Set default status to UNLOCKED and owner
155                 book.setAddressbookStatus(AddressbokStatus.UNLOCKED);
156                 book.setAddressbookUser(this.loginController.getLoggedInUser());
157                 book.setAddressbookCreated(new GregorianCalendar());
158
159                 try {
160                         // Register this address book
161                         Addressbook updatedAddressbook = this.addressbookBean.createAddressbook(book);
162
163                         // Remove name
164                         this.setAddressbookName(null);
165
166                         // Add address book entry to list
167                         this.usersAddressbooks.add(updatedAddressbook);
168
169                         // All fine
170                         return "login_own_addressbooks"; //NOI18N
171                 } catch (final AddressbookNameAlreadyUsedException ex) {
172                         // Throw again as cause
173                         throw new FaceletException(ex);
174                 }
175         }
176
177         @Override
178         public void afterAddressbookLoadedEvent (final @Observes AddressbookLoadedEvent event) {
179                 // event should not be null
180                 if (null == event) {
181                         // Throw NPE
182                         throw new NullPointerException("event is null"); //NOI18N
183                 } else if (event.getAddressbook() == null) {
184                         // Throw NPE again
185                         throw new NullPointerException("event.addressbook is null"); //NOI18N
186                 } else if (event.getAddressbook().getAddressbookId() == null) {
187                         // And again a NPE
188                         throw new NullPointerException("event.addressbook.addressbookId is null"); //NOI18N
189                 } else if (event.getAddressbook().getAddressbookId() < 1) {
190                         // Invalid id number
191                         throw new IllegalArgumentException(MessageFormat.format("Address book instance {0} has invalid id number: {1}", event.getAddressbook(), event.getAddressbook().getAddressbookId())); //NOI18N
192                 } else if (event.getAddressbook().getAddressbookUser() == null) {
193                         // One more NPE ...
194                         throw new NullPointerException("event.addressbook.addressbookUser is null"); //NOI18N
195                 }
196
197                 // Get address book instance
198                 Addressbook book = event.getAddressbook();
199
200                 // Set address book data
201                 this.setAddressbookId(book.getAddressbookId());
202                 this.setAddressbookName(book.getAddressbookName());
203                 this.setAddressbookUser(book.getAddressbookUser());
204                 this.setAddressbookCreated(book.getAddressbookCreated());
205
206                 // And instance ...
207                 this.setAddressbook(book);
208         }
209
210         @Override
211         public void afterLoginEvent (final @Observes UserLoggedInEvent event) {
212                 // Is the user logged in?
213                 if (null == event) {
214                         // Is null
215                         throw new NullPointerException("event is null"); //NOI18N
216                 } else if (event.getUser() == null) {
217                         // user is null
218                         throw new NullPointerException("event.user is null"); //NOI18N
219                 } else if (!event.getUser().equals(this.loginController.getLoggedInUser())) {
220                         // Not matching
221                         throw new IllegalStateException("event.user and loginController.loggedInUser don't match."); //NOI18N
222                 } else if (!this.loginController.isUserLoggedIn()) {
223                         // Not logged in
224                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
225                 }
226
227                 // Init user's address book list
228                 this.initAddressbookList();
229         }
230
231         @Override
232         public List<Addressbook> allAddressbooks () {
233                 // Is the user logged in?
234                 if (!this.loginController.isUserLoggedIn()) {
235                         // Not logged in
236                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
237                 }
238
239                 return Collections.unmodifiableList(this.usersAddressbooks);
240         }
241
242         @Override
243         public List<AddressbookEntry> allEntries (final Addressbook addressbook) {
244                 // Is the user logged in?
245                 if (!this.loginController.isUserLoggedIn()) {
246                         // Not logged in
247                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
248                 }
249
250                 // Ask the bean
251                 return this.addressbookBean.allEntries(addressbook);
252         }
253
254         @Override
255         public int allEntriesSize (final Addressbook addressbook) {
256                 // Ask the bean
257                 return this.allEntries(addressbook).size();
258         }
259
260         @Override
261         public List<ShareableAddressbook> allShares () {
262                 // Is the user logged in?
263                 if (!this.loginController.isUserLoggedIn()) {
264                         // Not logged in
265                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
266                 }
267
268                 return Collections.unmodifiableList(this.sharedAddressbooks);
269         }
270
271         @Override
272         public List<User> allUsersNotSharing () {
273                 // Is the user logged in?
274                 if (!this.loginController.isUserLoggedIn()) {
275                         // Not logged in
276                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
277                 }
278
279                 // Call EJB
280                 return this.addressbookBean.allUsersNotSharing(this.loginController.getLoggedInUser(), this.getAddressbook());
281         }
282
283         @Override
284         public Integer countAllUserSharedAddressbooks (final User user) {
285                 // Is there cache?
286                 if (AddressbookWebBean.countSharesList.containsKey(user)) {
287                         // Return it instead
288                         return AddressbookWebBean.countSharesList.get(user);
289                 }
290
291                 // Call EJB ("expensive")
292                 Integer count = this.addressbookBean.countAllUserSharedAddressbooks(user);
293
294                 // Add to list
295                 AddressbookWebBean.countSharesList.put(user, count);
296
297                 // Return it
298                 return count;
299         }
300
301         @Override
302         public Addressbook getAddressbook () {
303                 return this.addressbook;
304         }
305
306         @Override
307         public void setAddressbook (final Addressbook addressbook) {
308                 this.addressbook = addressbook;
309         }
310
311         @Override
312         public Calendar getAddressbookCreated () {
313                 return this.addressbookCreated;
314         }
315
316         @Override
317         public void setAddressbookCreated (final Calendar addressbookCreated) {
318                 this.addressbookCreated = addressbookCreated;
319         }
320
321         @Override
322         public Long getAddressbookId () {
323                 return this.addressbookId;
324         }
325
326         @Override
327         public void setAddressbookId (final Long addressbookId) {
328                 this.addressbookId = addressbookId;
329         }
330
331         @Override
332         public String getAddressbookName () {
333                 return this.addressbookName;
334         }
335
336         @Override
337         public void setAddressbookName (final String addressbookName) {
338                 this.addressbookName = addressbookName;
339         }
340
341         @Override
342         public User getAddressbookUser () {
343                 return this.addressbookUser;
344         }
345
346         @Override
347         public void setAddressbookUser (final User addressbookUser) {
348                 this.addressbookUser = addressbookUser;
349         }
350
351         @Override
352         public boolean hasCreatedAddressbooks () {
353                 // Is the user logged in?
354                 if (!this.loginController.isUserLoggedIn()) {
355                         // Not logged in
356                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
357                 }
358
359                 // Check if the list is filled
360                 return (!this.usersAddressbooks.isEmpty());
361         }
362
363         @PostConstruct
364         public void init () {
365                 // Init list
366                 this.usersAddressbooks = new ArrayList<>(0);
367
368                 // Is the user logged-in?
369                 if (this.loginController.isUserLoggedIn()) {
370                         // Initialize list
371                         this.initAddressbookList();
372                 }
373
374                 // TODO Initialize list from bean with just one call
375                 //this.addressbookBean.getUserCountMap()
376         }
377
378         @Override
379         public boolean isAddressbookNameUsed (final String addressbookName) {
380                 // Is it zero size?
381                 if (null == addressbookName) {
382                         // Is null
383                         throw new NullPointerException("addressbookName is null"); //NOI18N
384                 } else if (this.usersAddressbooks.isEmpty()) {
385                         // Not found!
386                         return false;
387                 }
388
389                 // Default is not found
390                 boolean isFound = false;
391
392                 // Check all entries
393                 for (final Addressbook book : this.usersAddressbooks) {
394                         // Is the name same?
395                         if (book.getAddressbookName().equals(addressbookName)) {
396                                 // Found a match
397                                 isFound = true;
398                                 break;
399                         }
400                 }
401
402                 // Return status
403                 return isFound;
404         }
405
406         @Override
407         public boolean isOtherAddressbook () {
408                 // Just call the other method and invert it
409                 return (!this.isOwnAddressbook());
410         }
411
412         @Override
413         public boolean isOwnAddressbook () {
414                 // Is the user logged in?
415                 if (!this.loginController.isUserLoggedIn()) {
416                         // No, then no own address book
417                         return false;
418                 }
419
420                 // Is same user?
421                 return Objects.equals(this.getAddressbookUser(), this.loginController.getLoggedInUser());
422         }
423
424         @Override
425         public String startSharing () {
426                 // Check conditions
427                 if (!this.loginController.isUserLoggedIn()) {
428                         // No, then throw exception
429                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
430                 } else if (this.getAddressbookUser() == null) {
431                         // Throw NPE
432                         throw new NullPointerException("this.addressbookUser is null"); //NOI18N
433                 } else if (this.getAddressbookUser().getUserId() == null) {
434                         // Throw NPE again
435                         throw new NullPointerException("this.addressbookUser.userId is null"); //NOI18N
436                 } else if (this.getAddressbookUser().getUserId() < 1) {
437                         // Invalid id number
438                         throw new IllegalStateException(MessageFormat.format("this.addressbookUser.userId={0} is invalid", this.getAddressbookUser().getUserId())); //NOI18N
439                 } else if (Objects.equals(this.getAddressbookUser(), this.loginController.getLoggedInUser())) {
440                         // Sharing with yourself!
441                         throw new IllegalStateException("User tries to share with himself."); //NOI18N
442                 } else if (this.getAddressbook() == null) {
443                         // Throw NPE again
444                         throw new NullPointerException("this.addressbook is null"); //NOI18N
445                 } else if (this.getAddressbook().getAddressbookId() == null) {
446                         // Throw NPE again
447                         throw new NullPointerException("this.addressbook.addressbookId is null"); //NOI18N
448                 } else if (this.getAddressbook().getAddressbookId() < 1) {
449                         // Invalid id number
450                         throw new IllegalArgumentException(MessageFormat.format("this.addressbook.addressbookId={0} is invalid.", this.getAddressbook().getAddressbookId())); //NOI18N
451                 }
452
453                 // TODO Unfinished
454                 return null;
455         }
456
457         /**
458          * Initializes the user user's address book list
459          */
460         private void initAddressbookList () {
461                 // Get user instance
462                 User user = this.loginController.getLoggedInUser();
463
464                 // Fill list with entries
465                 this.usersAddressbooks = this.addressbookBean.getUsersAddressbookList(user);
466         }
467 }