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.List;
22 import javax.annotation.PostConstruct;
23 import javax.enterprise.context.SessionScoped;
24 import javax.faces.view.facelets.FaceletException;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
30 import org.mxchange.addressbook.beans.login.UserLoginWebController;
31 import org.mxchange.addressbook.exceptions.AddressbookNameAlreadyUsedException;
32 import org.mxchange.addressbook.model.addressbook.Addressbook;
33 import org.mxchange.addressbook.model.addressbook.AddressbookSessionBeanRemote;
34 import org.mxchange.addressbook.model.addressbook.UserAddressbook;
35 import org.mxchange.addressbook.model.addressbook.status.AddressbokStatus;
38 * A user bean (controller)
40 * @author Roland Haeder<roland@mxchange.org>
42 @Named ("addressbookController")
44 public class AddressbookWebBean implements AddressbookWebController {
49 private static final long serialVersionUID = 185_781_756_712_969L;
51 /////////////////////// Properties /////////////////////
53 * A list of all user's address books
55 private List<Addressbook> addressbookList;
58 * Name of the address book
60 private String addressbookName;
63 * Remote address book bean
65 private final AddressbookSessionBeanRemote addressbookBean;
71 private UserLoginWebController loginController;
76 public AddressbookWebBean () {
79 // Get initial context
80 Context context = new InitialContext();
83 this.addressbookBean = (AddressbookSessionBeanRemote) context.lookup("ejb/stateless-addressbook"); //NOI18N
84 } catch (final NamingException e) {
86 throw new FaceletException(e);
93 this.addressbookList = new ArrayList<>(0);
95 // Is the user logged-in?
96 if (this.loginController.isUserLoggedIn()) {
97 // Fill list with entries
98 this.addressbookList = this.addressbookBean.getUsersList(this.loginController.getLoggedInUser());
103 public boolean hasCreatedAddressbooks () {
104 // Is the user logged in?
105 if (!this.loginController.isUserLoggedIn()) {
107 throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
110 // Check if the list is filled
111 return (!this.addressbookList.isEmpty());
115 public void addAddressbook () {
116 // Is this name already used?
117 if (!this.loginController.isUserLoggedIn()) {
119 throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
120 } else if (this.getAddressbookName() == null) {
121 // Address book name is null
122 throw new IllegalStateException("addressbookName is null");
123 } else if (this.getAddressbookName().isEmpty()) {
124 // Address book name is empty
125 throw new IllegalStateException("addressbookName is empty.");
126 } else if (!this.isAddressbookNameUsed(this.getAddressbookName())) {
127 // Already used by this user
128 throw new FaceletException(MessageFormat.format("Address book name {0} already used.", this.getAddressbookName())); //NOI18N
131 // Create address book instance with name
132 Addressbook addressbook = new UserAddressbook(this.getAddressbookName());
134 // Set default status to UNLOCKED and owner
135 addressbook.setAddressbookStatus(AddressbokStatus.UNLOCKED);
136 addressbook.setAddressbookUser(this.loginController.getLoggedInUser());
139 // Register this address book
140 Addressbook updatedAddressbook = this.addressbookBean.createAddressbook(addressbook);
143 this.setAddressbookName(null);
145 // Add address book entry to list
146 this.addressbookList.add(updatedAddressbook);
147 } catch (final AddressbookNameAlreadyUsedException ex) {
148 // Throw again as cause
149 throw new FaceletException(ex);
154 public String getAddressbookName () {
155 return this.addressbookName;
159 public void setAddressbookName (final String addressbookName) {
160 this.addressbookName = addressbookName;
164 public boolean isAddressbookNameUsed (final String addressbookName) {
166 if (null == addressbookName) {
168 throw new NullPointerException("addressbookName is null"); //NOI18N
169 } else if (this.addressbookList.isEmpty()) {
174 // Default is not found
175 boolean isFound = false;
178 for (final Addressbook addressbook : this.addressbookList) {
180 if (addressbook.getAddressbookName().equals(addressbookName)) {