]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebBean.java
b50a418923c0b1f12380aa0bbdf3a02d341ada08
[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.entry.AddressbookEntry;
38 import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook;
39 import org.mxchange.addressbook.model.addressbook.status.AddressbokStatus;
40
41 /**
42  * A user bean (controller)
43  * <p>
44  * @author Roland Haeder<roland@mxchange.org>
45  */
46 @Named ("addressbookController")
47 @SessionScoped
48 public class AddressbookWebBean implements AddressbookWebController {
49
50         /**
51          * Serial number
52          */
53         private static final long serialVersionUID = 185_781_756_712_969L;
54
55         /**
56          * Remote address book bean
57          */
58         private AddressbookSessionBeanRemote addressbookBean;
59
60         /**
61          * Address book id number (from URL for example)
62          */
63         private Long addressbookId;
64
65
66         /**
67          * Name of the address book
68          */
69         private String addressbookName;
70
71
72         /**
73          * Login controller
74          */
75         @Inject
76         private UserLoginWebController loginController;
77
78         /**
79          * A list of all user's shared (with others) address books
80          */
81         private List<ShareableAddressbook> sharedAddressbooks;
82
83         /////////////////////// Properties /////////////////////
84         /**
85          * A list of all user's address books
86          */
87         private List<Addressbook> usersAddressbooks;
88
89         /**
90          * Default constructor
91          */
92         public AddressbookWebBean () {
93                 // Try it
94                 try {
95                         // Get initial context
96                         Context context = new InitialContext();
97
98                         // Try to lookup
99                         this.addressbookBean = (AddressbookSessionBeanRemote) context.lookup("ejb/stateless-addressbook"); //NOI18N
100                 } catch (final NamingException e) {
101                         // Throw again
102                         throw new FaceletException(e);
103                 }
104         }
105
106         @Override
107         public String addAddressbook () {
108                 // Is this name already used?
109                 if (!this.loginController.isUserLoggedIn()) {
110                         // Not logged in
111                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
112                 } else if (this.getAddressbookName() == null) {
113                         // Address book name is null
114                         throw new NullPointerException("addressbookName is null"); //NOI18N
115                 } else if (this.getAddressbookName().isEmpty()) {
116                         // Address book name is empty
117                         throw new IllegalStateException("addressbookName is empty."); //NOI18N
118                 } else if (this.isAddressbookNameUsed(this.getAddressbookName())) {
119                         // Already used by this user
120                         throw new FaceletException(MessageFormat.format("Address book name {0} already used.", this.getAddressbookName())); //NOI18N
121                 }
122
123                 // Create address book instance with name
124                 Addressbook addressbook = new UserAddressbook(this.getAddressbookName());
125
126                 // Set default status to UNLOCKED and owner
127                 addressbook.setAddressbookStatus(AddressbokStatus.UNLOCKED);
128                 addressbook.setAddressbookUser(this.loginController.getLoggedInUser());
129                 addressbook.setAddressbookCreated(new GregorianCalendar());
130
131                 try {
132                         // Register this address book
133                         Addressbook updatedAddressbook = this.addressbookBean.createAddressbook(addressbook);
134
135                         // Remove name
136                         this.setAddressbookName(null);
137
138                         // Add address book entry to list
139                         this.usersAddressbooks.add(updatedAddressbook);
140
141                         // All fine
142                         return "login_own_addressbooks"; //NOI18N
143                 } catch (final AddressbookNameAlreadyUsedException ex) {
144                         // Throw again as cause
145                         throw new FaceletException(ex);
146                 }
147         }
148
149         @Override
150         public List<Addressbook> allAddressbooks () {
151                 // Is the user logged in?
152                 if (!this.loginController.isUserLoggedIn()) {
153                         // Not logged in
154                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
155                 }
156
157                 return Collections.unmodifiableList(this.usersAddressbooks);
158         }
159
160         @Override
161         public List<AddressbookEntry> allEntries (final Addressbook addressbook) {
162                 // Is the user logged in?
163                 if (!this.loginController.isUserLoggedIn()) {
164                         // Not logged in
165                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
166                 }
167
168                 // Ask the bean
169                 return this.addressbookBean.allEntries(addressbook);
170         }
171
172         @Override
173         public int allEntriesSize (final Addressbook addressbook) {
174                 // Ask the bean
175                 return this.allEntries(addressbook).size();
176         }
177
178         @Override
179         public List<ShareableAddressbook> allShares () {
180                 // Is the user logged in?
181                 if (!this.loginController.isUserLoggedIn()) {
182                         // Not logged in
183                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
184                 }
185                 
186                 return Collections.unmodifiableList(this.sharedAddressbooks);
187         }
188
189         @Override
190         public Long getAddressbookId () {
191                 return this.addressbookId;
192         }
193
194         @Override
195         public void setAddressbookId (final Long addressbookId) {
196                 this.addressbookId = addressbookId;
197         }
198
199         @Override
200         public String getAddressbookName () {
201                 return this.addressbookName;
202         }
203
204         @Override
205         public void setAddressbookName (final String addressbookName) {
206                 this.addressbookName = addressbookName;
207         }
208
209         @Override
210         public boolean hasCreatedAddressbooks () {
211                 // Is the user logged in?
212                 if (!this.loginController.isUserLoggedIn()) {
213                         // Not logged in
214                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
215                 }
216
217                 // Check if the list is filled
218                 return (!this.usersAddressbooks.isEmpty());
219         }
220
221         @PostConstruct
222         public void init () {
223                 // Init list
224                 this.usersAddressbooks = new ArrayList<>(0);
225
226                 // Is the user logged-in?
227                 if (this.loginController.isUserLoggedIn()) {
228                         // Fill list with entries
229                         this.usersAddressbooks = this.addressbookBean.getUsersList(this.loginController.getLoggedInUser());
230                 }
231         }
232
233         @Override
234         public boolean isAddressbookNameUsed (final String addressbookName) {
235                 // Is it zero size?
236                 if (null == addressbookName) {
237                         // Is null
238                         throw new NullPointerException("addressbookName is null"); //NOI18N
239                 } else if (this.usersAddressbooks.isEmpty()) {
240                         // Not found!
241                         return false;
242                 }
243
244                 // Default is not found
245                 boolean isFound = false;
246
247                 // Check all entries
248                 for (final Addressbook addressbook : this.usersAddressbooks) {
249                         // Is the name same?
250                         if (addressbook.getAddressbookName().equals(addressbookName)) {
251                                 // Found a match
252                                 isFound = true;
253                                 break;
254                         }
255                 }
256
257                 // Return status
258                 return isFound;
259         }
260 }