2 * Copyright (C) 2015 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.addressbook.beans.addressbook;
19 import java.text.MessageFormat;
20 import java.util.ArrayList;
21 import java.util.GregorianCalendar;
22 import java.util.List;
23 import javax.annotation.PostConstruct;
24 import javax.enterprise.context.SessionScoped;
25 import javax.faces.view.facelets.FaceletException;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import org.mxchange.addressbook.beans.login.UserLoginWebController;
32 import org.mxchange.addressbook.exceptions.AddressbookNameAlreadyUsedException;
33 import org.mxchange.addressbook.model.addressbook.Addressbook;
34 import org.mxchange.addressbook.model.addressbook.AddressbookSessionBeanRemote;
35 import org.mxchange.addressbook.model.addressbook.UserAddressbook;
36 import org.mxchange.addressbook.model.addressbook.status.AddressbokStatus;
39 * A user bean (controller)
41 * @author Roland Haeder<roland@mxchange.org>
43 @Named ("addressbookController")
45 public class AddressbookWebBean implements AddressbookWebController {
50 private static final long serialVersionUID = 185_781_756_712_969L;
52 /////////////////////// Properties /////////////////////
54 * A list of all user's address books
56 private List<Addressbook> addressbookList;
59 * Name of the address book
61 private String addressbookName;
64 * Remote address book bean
66 private final AddressbookSessionBeanRemote addressbookBean;
72 private UserLoginWebController loginController;
77 public AddressbookWebBean () {
80 // Get initial context
81 Context context = new InitialContext();
84 this.addressbookBean = (AddressbookSessionBeanRemote) context.lookup("ejb/stateless-addressbook"); //NOI18N
85 } catch (final NamingException e) {
87 throw new FaceletException(e);
94 this.addressbookList = new ArrayList<>(0);
96 // Is the user logged-in?
97 if (this.loginController.isUserLoggedIn()) {
98 // Fill list with entries
99 this.addressbookList = this.addressbookBean.getUsersList(this.loginController.getLoggedInUser());
104 public boolean hasCreatedAddressbooks () {
105 // Is the user logged in?
106 if (!this.loginController.isUserLoggedIn()) {
108 throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
111 // Check if the list is filled
112 return (!this.addressbookList.isEmpty());
116 public void addAddressbook () {
117 // Is this name already used?
118 if (!this.loginController.isUserLoggedIn()) {
120 throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
121 } else if (this.getAddressbookName() == null) {
122 // Address book name is null
123 throw new NullPointerException("addressbookName is null");
124 } else if (this.getAddressbookName().isEmpty()) {
125 // Address book name is empty
126 throw new IllegalStateException("addressbookName is empty.");
127 } else if (this.isAddressbookNameUsed(this.getAddressbookName())) {
128 // Already used by this user
129 throw new FaceletException(MessageFormat.format("Address book name {0} already used.", this.getAddressbookName())); //NOI18N
132 // Create address book instance with name
133 Addressbook addressbook = new UserAddressbook(this.getAddressbookName());
135 // Set default status to UNLOCKED and owner
136 addressbook.setAddressbookStatus(AddressbokStatus.UNLOCKED);
137 addressbook.setAddressbookUser(this.loginController.getLoggedInUser());
138 addressbook.setAddressbookCreated(new GregorianCalendar());
141 // Register this address book
142 Addressbook updatedAddressbook = this.addressbookBean.createAddressbook(addressbook);
145 this.setAddressbookName(null);
147 // Add address book entry to list
148 this.addressbookList.add(updatedAddressbook);
149 } catch (final AddressbookNameAlreadyUsedException ex) {
150 // Throw again as cause
151 throw new FaceletException(ex);
156 public String getAddressbookName () {
157 return this.addressbookName;
161 public void setAddressbookName (final String addressbookName) {
162 this.addressbookName = addressbookName;
166 public boolean isAddressbookNameUsed (final String addressbookName) {
168 if (null == addressbookName) {
170 throw new NullPointerException("addressbookName is null"); //NOI18N
171 } else if (this.addressbookList.isEmpty()) {
176 // Default is not found
177 boolean isFound = false;
180 for (final Addressbook addressbook : this.addressbookList) {
182 if (addressbook.getAddressbookName().equals(addressbookName)) {