]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebBean.java
Continued:
[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.Collections;
22 import java.util.GregorianCalendar;
23 import java.util.List;
24 import javax.annotation.PostConstruct;
25 import javax.enterprise.context.SessionScoped;
26 import javax.faces.view.facelets.FaceletException;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
32 import org.mxchange.addressbook.beans.login.UserLoginWebController;
33 import org.mxchange.addressbook.exceptions.AddressbookNameAlreadyUsedException;
34 import org.mxchange.addressbook.model.addressbook.Addressbook;
35 import org.mxchange.addressbook.model.addressbook.AddressbookSessionBeanRemote;
36 import org.mxchange.addressbook.model.addressbook.UserAddressbook;
37 import org.mxchange.addressbook.model.addressbook.status.AddressbokStatus;
38
39 /**
40  * A user bean (controller)
41  * <p>
42  * @author Roland Haeder<roland@mxchange.org>
43  */
44 @Named ("addressbookController")
45 @SessionScoped
46 public class AddressbookWebBean implements AddressbookWebController {
47
48         /**
49          * Serial number
50          */
51         private static final long serialVersionUID = 185_781_756_712_969L;
52
53         /**
54          * Remote address book bean
55          */
56         private AddressbookSessionBeanRemote addressbookBean;
57
58         /////////////////////// Properties /////////////////////
59         /**
60          * A list of all user's address books
61          */
62         private List<Addressbook> addressbookList;
63
64         /**
65          * Name of the address book
66          */
67         private String addressbookName;
68
69         /**
70          * Login controller
71          */
72         @Inject
73         private UserLoginWebController loginController;
74
75         /**
76          * Default constructor
77          */
78         public AddressbookWebBean () {
79                 // Try it
80                 try {
81                         // Get initial context
82                         Context context = new InitialContext();
83
84                         // Try to lookup
85                         this.addressbookBean = (AddressbookSessionBeanRemote) context.lookup("ejb/stateless-addressbook"); //NOI18N
86                 } catch (final NamingException e) {
87                         // Throw again
88                         throw new FaceletException(e);
89                 }
90         }
91
92         @Override
93         public void addAddressbook () {
94                 // Is this name already used?
95                 if (!this.loginController.isUserLoggedIn()) {
96                         // Not logged in
97                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
98                 } else if (this.getAddressbookName() == null) {
99                         // Address book name is null
100                         throw new NullPointerException("addressbookName is null");
101                 } else if (this.getAddressbookName().isEmpty()) {
102                         // Address book name is empty
103                         throw new IllegalStateException("addressbookName is empty.");
104                 } else if (this.isAddressbookNameUsed(this.getAddressbookName())) {
105                         // Already used by this user
106                         throw new FaceletException(MessageFormat.format("Address book name {0} already used.", this.getAddressbookName())); //NOI18N
107                 }
108
109                 // Create address book instance with name
110                 Addressbook addressbook = new UserAddressbook(this.getAddressbookName());
111
112                 // Set default status to UNLOCKED and owner
113                 addressbook.setAddressbookStatus(AddressbokStatus.UNLOCKED);
114                 addressbook.setAddressbookUser(this.loginController.getLoggedInUser());
115                 addressbook.setAddressbookCreated(new GregorianCalendar());
116
117                 try {
118                         // Register this address book
119                         Addressbook updatedAddressbook = this.addressbookBean.createAddressbook(addressbook);
120
121                         // Remove name
122                         this.setAddressbookName(null);
123
124                         // Add address book entry to list
125                         this.addressbookList.add(updatedAddressbook);
126                 } catch (final AddressbookNameAlreadyUsedException ex) {
127                         // Throw again as cause
128                         throw new FaceletException(ex);
129                 }
130         }
131
132         @Override
133         public List<Addressbook> allAddressbooks () {
134                 return Collections.unmodifiableList(this.addressbookList);
135         }
136
137         @Override
138         public String getAddressbookName () {
139                 return this.addressbookName;
140         }
141
142         @Override
143         public void setAddressbookName (final String addressbookName) {
144                 this.addressbookName = addressbookName;
145         }
146
147         @Override
148         public boolean hasCreatedAddressbooks () {
149                 // Is the user logged in?
150                 if (!this.loginController.isUserLoggedIn()) {
151                         // Not logged in
152                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
153                 }
154
155                 // Check if the list is filled
156                 return (!this.addressbookList.isEmpty());
157         }
158
159         @PostConstruct
160         public void init () {
161                 // Init list
162                 this.addressbookList = new ArrayList<>(0);
163
164                 // Is the user logged-in?
165                 if (this.loginController.isUserLoggedIn()) {
166                         // Fill list with entries
167                         this.addressbookList = this.addressbookBean.getUsersList(this.loginController.getLoggedInUser());
168                 }
169         }
170
171         @Override
172         public boolean isAddressbookNameUsed (final String addressbookName) {
173                 // Is it zero size?
174                 if (null == addressbookName) {
175                         // Is null
176                         throw new NullPointerException("addressbookName is null"); //NOI18N
177                 } else if (this.addressbookList.isEmpty()) {
178                         // Not found!
179                         return false;
180                 }
181
182                 // Default is not found
183                 boolean isFound = false;
184
185                 // Check all entries
186                 for (final Addressbook addressbook : this.addressbookList) {
187                         // Is the name same?
188                         if (addressbook.getAddressbookName().equals(addressbookName)) {
189                                 // Found a match
190                                 isFound = true;
191                                 break;
192                         }
193                 }
194
195                 // Return status
196                 return isFound;
197         }
198 }