]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/helper/AddressbookWebRequestHelper.java
Please cherry-pick:
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / helper / AddressbookWebRequestHelper.java
1 /*
2  * Copyright (C) 2016 Roland Häder GmbH
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (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 Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.addressbook.beans.helper;
18
19 import java.text.MessageFormat;
20 import javax.annotation.PostConstruct;
21 import javax.enterprise.context.RequestScoped;
22 import javax.inject.Inject;
23 import javax.inject.Named;
24 import org.mxchange.addressbook.beans.contact.AddressbookAdminContactWebRequestController;
25 import org.mxchange.addressbook.beans.phone.AddressbookAdminPhoneWebRequestController;
26 import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController;
27 import org.mxchange.jcontacts.contact.Contact;
28 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
29 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
30 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
31 import org.mxchange.jusercore.model.user.User;
32
33 /**
34  * A general helper for beans
35  * <p>
36  * @author Roland Häder<roland@mxchange.org>
37  */
38 @Named ("adminHelper")
39 @RequestScoped
40 public class AddressbookWebRequestHelper implements AddressbookWebRequestController {
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 17_258_793_567_145_701L;
46
47         /**
48          * Administrative contact controller
49          */
50         @Inject
51         private AddressbookAdminContactWebRequestController adminContactController;
52
53         /**
54          * Administrative phone controller
55          */
56         @Inject
57         private AddressbookAdminPhoneWebRequestController adminPhoneController;
58
59         /**
60          * Contact instance
61          */
62         private Contact contact;
63
64         /**
65          * User instance
66          */
67         private User user;
68
69         /**
70          * Regular user controller
71          */
72         @Inject
73         private AddressbookUserWebSessionController userController;
74
75         /**
76          * Default constructor
77          */
78         public AddressbookWebRequestHelper () {
79         }
80
81         @Override
82         public void copyContactToController () {
83                 // Validate user instance
84                 if (this.getContact() == null) {
85                         // Throw NPE
86                         throw new NullPointerException("this.contact is null"); //NOI18N
87                 } else if (this.getContact().getContactId() == null) {
88                         // Throw NPE again
89                         throw new NullPointerException("this.contact.contactId is null"); //NOI18N
90                 } else if (this.getContact().getContactId() < 1) {
91                         // Not valid
92                         throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
93                 }
94
95                 // Set all phone instances
96                 this.setPhoneInstances(this.getContact());
97
98                 // Set all fields: user
99                 this.adminContactController.copyContactToController(this.getContact());
100         }
101
102         @Override
103         public void copyUserToController () {
104                 // Validate user instance
105                 if (this.getUser() == null) {
106                         // Throw NPE
107                         throw new NullPointerException("this.user is null"); //NOI18N
108                 } else if (this.getUser().getUserId() == null) {
109                         // Throw NPE again
110                         throw new NullPointerException("this.user.userId is null"); //NOI18N
111                 } else if (this.getUser().getUserId() < 1) {
112                         // Not valid
113                         throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
114                 }
115
116                 // Get contact
117                 Contact userContact = this.getUser().getUserContact();
118
119                 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
120                 this.setContact(userContact);
121
122                 // Set all phone instances
123                 this.setPhoneInstances(userContact);
124
125                 // Set all fields: user
126                 this.userController.setUserName(this.getUser().getUserName());
127         }
128
129         @Override
130         public Contact getContact () {
131                 return this.contact;
132         }
133
134         @Override
135         public void setContact (final Contact contact) {
136                 this.contact = contact;
137         }
138
139         @Override
140         public String getContactUsageMessageKey (final Contact contact) {
141                 // The contact must be valid
142                 if (null == contact) {
143                         // Throw NPE
144                         throw new NullPointerException("contact is null"); //NOI18N
145                 } else if (contact.getContactId() == null) {
146                         // Throw again ...
147                         throw new NullPointerException("contact.contactId is null"); //NOI18N
148                 } else if (contact.getContactId() < 1) {
149                         // Not valid
150                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
151                 }
152
153                 // Default key is "unused"
154                 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
155
156                 // Check user
157                 boolean isUserContact = this.userController.isContactFound(contact);
158
159                 // Check user first
160                 if (isUserContact) {
161                         // Only user
162                         messageKey = "CONTACT_IS_USER"; //NOI18N
163                 }
164
165                 // Return message key
166                 return messageKey;
167         }
168
169         @Override
170         public User getUser () {
171                 return this.user;
172         }
173
174         @Override
175         public void setUser (final User user) {
176                 this.user = user;
177         }
178
179         /**
180          * Post-construction method
181          */
182         @PostConstruct
183         public void init () {
184         }
185
186         /**
187          * Set's all given contact's phone instances: land-line, mobile and fax
188          * <p>
189          * @param contact Contact to set phone instances for
190          */
191         private void setPhoneInstances (final Contact contact) {
192                 // The contact must be valid
193                 if (null == contact) {
194                         // Throw NPE
195                         throw new NullPointerException("contact is null"); //NOI18N
196                 } else if (contact.getContactId() == null) {
197                         // Throw again ...
198                         throw new NullPointerException("contact.contactId is null"); //NOI18N
199                 } else if (contact.getContactId() < 1) {
200                         // Not valid
201                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
202                 }
203
204                 // Is cellphone set?
205                 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
206                         // Yes, then set it in admin controller
207                         this.adminPhoneController.setMobileNumber(contact.getContactMobileNumber());
208                 }
209
210                 // Is land-line set?
211                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
212                         // Yes, then set it in admin controller
213                         this.adminPhoneController.setLandLine(contact.getContactLandLineNumber());
214                 }
215
216                 // Is fax set?
217                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
218                         // Yes, then set it in admin controller
219                         this.adminPhoneController.setFax(contact.getContactFaxNumber());
220                 }
221         }
222
223 }