]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/shares/SharesWebBean.java
Moved allShares() to proper bean
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / shares / SharesWebBean.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.shares;
18
19 import java.text.MessageFormat;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Objects;
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.UserAlreadySharingAddressbookException;
32 import org.mxchange.addressbook.model.addressbook.Addressbook;
33 import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook;
34 import org.mxchange.addressbook.model.shared.SharedAddressbooksSessionBeanRemote;
35 import org.mxchange.jusercore.model.user.User;
36
37 /**
38  * A bean for sharing address books with other users
39  * <p>
40  * @author Roland Haeder
41  */
42 @Named (value = "shareController")
43 @SessionScoped
44 public class SharesWebBean implements SharesWebController {
45
46         /**
47          * Serial number
48          */
49         private static final long serialVersionUID = 19_868_976_871_976_780L;
50
51         /**
52          * Cached flag whether the user is sharing address books
53          */
54         private Boolean isUserSharing = null;
55
56         /**
57          * Login controller injection
58          */
59         @Inject
60         private UserLoginWebController loginController;
61
62         /**
63          * Share instance
64          */
65         private ShareableAddressbook share;
66
67         /**
68          * A list of all user's shared (with others) address books
69          */
70         private List<ShareableAddressbook> sharedAddressbooks;
71
72         /**
73          * Remote bean for sharing address books
74          */
75         private SharedAddressbooksSessionBeanRemote shareBean;
76
77         /**
78          * User id of sharee
79          */
80         private Long shareeUserId;
81
82         /**
83          * Default constructor
84          */
85         public SharesWebBean () {
86                 // Try it
87                 try {
88                         // Get initial context
89                         Context context = new InitialContext();
90
91                         // Look up bean
92                         this.shareBean = (SharedAddressbooksSessionBeanRemote) context.lookup("ejb/stateless-share"); //NOI18N
93                 } catch (final NamingException ex) {
94                         // Continue to throw
95                         throw new FaceletException(ex);
96                 }
97         }
98
99         @Override
100         public Long getShareeUserId () {
101                 return this.shareeUserId;
102         }
103
104         @Override
105         public void setShareeUserId (final Long shareeUserId) {
106                 this.shareeUserId = shareeUserId;
107         }
108
109         @Override
110         public boolean isShareeUserIdSet () {
111                 return ((this.getShareeUserId() instanceof Long) && (this.getShareeUserId() > 0));
112         }
113
114         @Override
115         public boolean isShareeUserIdEmpty () {
116                 return (!this.isShareeUserIdSet());
117         }
118
119         @Override
120         public boolean isSharingAddressbooks () {
121                 // Only to be called for logged-in users
122                 if (!this.loginController.isUserLoggedIn()) {
123                         // Not logged in
124                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
125                 } else if (this.isUserSharing instanceof Boolean) {
126                         // Return cached value
127                         return this.isUserSharing;
128                 }
129
130                 // Call the proper bean
131                 this.isUserSharing = this.shareBean.isUserSharingAddressbooks(this.loginController.getLoggedInUser());
132
133                 // Return it
134                 return this.isUserSharing;
135         }
136
137         @Override
138         public String startSharing (final User user, final Addressbook addressbook) {
139                 // Check conditions
140                 if (!this.loginController.isUserLoggedIn()) {
141                         // No, then throw exception
142                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
143                 } else if (null == user) {
144                         // Throw NPE
145                         throw new NullPointerException("user is null"); //NOI18N
146                 } else if (user.getUserId() == null) {
147                         // Throw NPE again
148                         throw new NullPointerException("user.userId is null"); //NOI18N
149                 } else if (user.getUserId() < 1) {
150                         // Invalid id number
151                         throw new IllegalStateException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
152                 } else if (Objects.equals(user, this.loginController.getLoggedInUser())) {
153                         // Sharing with yourself!
154                         throw new IllegalStateException("User tries to share with himself."); //NOI18N
155                 } else if (null == addressbook) {
156                         // Throw NPE again
157                         throw new NullPointerException("addressbook is null"); //NOI18N
158                 } else if (addressbook.getAddressbookId() == null) {
159                         // Throw NPE again
160                         throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
161                 } else if (addressbook.getAddressbookId() < 1) {
162                         // Invalid id number
163                         throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
164                 } else if (!Objects.equals(addressbook.getAddressbookUser(), this.loginController.getLoggedInUser())) {
165                         // Not the same user!
166                         throw new IllegalStateException(MessageFormat.format("Address book id {0} owner id {1} mismatching logged-in user id {2}", addressbook.getAddressbookId(), addressbook.getAddressbookUser().getUserId(), this.loginController.getLoggedInUser().getUserId()));
167                 }
168
169                 try {
170                         // Init sharing
171                         ShareableAddressbook shared = this.shareBean.startSharing(user, addressbook);
172
173                         // TODO Set it here
174                         this.setShare(shared);
175                 } catch (final UserAlreadySharingAddressbookException ex) {
176                         // Throw again
177                         throw new FaceletException(ex);
178                 }
179
180                 // TODO Unfinished
181                 return null;
182         }
183
184         @Override
185         public List<ShareableAddressbook> allShares () {
186                 // Is the user logged in?
187                 if (!this.loginController.isUserLoggedIn()) {
188                         // Not logged in
189                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
190                 }
191
192                 return Collections.unmodifiableList(this.sharedAddressbooks);
193         }
194
195         @Override
196         public ShareableAddressbook getShare () {
197                 return this.share;
198         }
199
200         @Override
201         public void setShare (final ShareableAddressbook share) {
202                 this.share = share;
203         }
204 }