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