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