2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.jjobs.beans.helper;
19 import java.text.MessageFormat;
20 import javax.enterprise.context.RequestScoped;
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import org.mxchange.jcontacts.contact.Contact;
24 import org.mxchange.jjobs.beans.contact.JobsAdminContactWebRequestController;
25 import org.mxchange.jjobs.beans.phone.JobsAdminPhoneWebRequestController;
26 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
27 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
28 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
29 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
30 import org.mxchange.jusercore.model.user.User;
33 * A general helper for beans
35 * @author Roland Haeder<roland@mxchange.org>
37 @Named ("adminHelper")
39 public class JobsWebRequestHelper implements JobsWebRequestController {
44 private static final long serialVersionUID = 17_258_793_567_145_701L;
47 * Administrative contact controller
50 private JobsAdminContactWebRequestController adminContactController;
53 * Administrative phone controller
56 private JobsAdminPhoneWebRequestController adminPhoneController;
61 private DialableCellphoneNumber cellPhoneNumber;
66 private Contact contact;
71 private DialableFaxNumber fax;
76 private DialableLandLineNumber landLine;
84 * Regular user controller
87 private JobsUserWebSessionController userController;
92 public JobsWebRequestHelper () {
96 public void copyCellphoneNumberToController () {
97 // Validate cellphone instance
98 if (this.getCellPhoneNumber() == null) {
100 throw new NullPointerException("this.cellPhoneNumber is null");
101 } else if (this.getCellPhoneNumber().getPhoneId() == null) {
103 throw new NullPointerException("this.cellPhoneNumber.phoneId is null");
104 } else if (this.getCellPhoneNumber().getPhoneId() < 1) {
106 throw new IllegalArgumentException(MessageFormat.format("this.cellPhoneNumber.phoneId={0} is not valid", this.getCellPhoneNumber().getPhoneId()));
107 } else if (this.getCellPhoneNumber().getCellphoneProvider() == null) {
109 throw new NullPointerException("this.cellPhoneNumber.cellphoneProvider is null");
110 } else if (this.getCellPhoneNumber().getCellphoneProvider().getProviderId() == null) {
112 throw new NullPointerException("this.cellPhoneNumber.cellphoneProvider.providerId is null");
113 } else if (this.getCellPhoneNumber().getCellphoneProvider().getProviderId() < 1) {
115 throw new IllegalArgumentException(MessageFormat.format("this.cellPhoneNumber.cellphoneProvider.providerId={0} is invalid", this.getCellPhoneNumber().getCellphoneProvider().getProviderId()));
116 } else if (this.getCellPhoneNumber().getPhoneNumber() == null) {
117 // Throw NPE again ...
118 throw new NullPointerException("this.cellPhoneNumber.phoneNumber is null");
119 } else if (this.getCellPhoneNumber().getPhoneNumber() < 1) {
121 throw new IllegalArgumentException(MessageFormat.format("this.cellPhoneNumber.phoneNumber={0} is not valid", this.getCellPhoneNumber().getPhoneNumber()));
124 // Copy all (changeable) data fields to admin controller
125 this.adminPhoneController.setCellphoneProvider(this.getCellPhoneNumber().getCellphoneProvider());
126 this.adminPhoneController.setPhoneNumber(this.getCellPhoneNumber().getPhoneNumber());
130 public void copyContactToController () {
131 // Validate contact instance
132 if (this.getContact() == null) {
134 throw new NullPointerException("this.contact is null"); //NOI18N
135 } else if (this.getContact().getContactId() == null) {
137 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
138 } else if (this.getContact().getContactId() < 1) {
140 throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
143 // Set all phone instances
144 this.setPhoneInstances(this.getContact());
146 // Set all fields: user
147 this.adminContactController.copyContactToController(this.getContact());
150 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyContactToController - EXIT!"); //NOI18N
154 public void copyUserToController () {
156 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyUserToController - CALLED!"); //NOI18N
158 // Validate user instance
159 if (this.getUser() == null) {
161 throw new NullPointerException("this.user is null");
162 } else if (this.getUser().getUserId() == null) {
164 throw new NullPointerException("this.user.userId is null");
165 } else if (this.getUser().getUserId() < 1) {
167 throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId()));
171 Contact userContact = this.getUser().getUserContact();
173 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
174 this.setContact(userContact);
176 // Set all phone instances
177 this.setPhoneInstances(userContact);
179 // Set all fields: user
180 this.userController.setUserName(this.getUser().getUserName());
183 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyUserToController - EXIT!"); //NOI18N
187 public DialableCellphoneNumber getCellPhoneNumber () {
188 return this.cellPhoneNumber;
192 public void setCellPhoneNumber (final DialableCellphoneNumber cellPhoneNumber) {
193 this.cellPhoneNumber = cellPhoneNumber;
197 public Contact getContact () {
202 public void setContact (final Contact contact) {
203 this.contact = contact;
207 public String getContactUsageMessageKey (final Contact contact) {
208 // The contact must be valid
209 if (null == contact) {
211 throw new NullPointerException("contact is null"); //NOI18N
212 } else if (contact.getContactId() == null) {
214 throw new NullPointerException("contact.contactId is null"); //NOI18N
215 } else if (contact.getContactId() < 1) {
217 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
220 // Default key is "unused"
221 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
224 boolean isUserContact = this.userController.isContactFound(contact);
229 messageKey = "CONTACT_IS_USER"; //NOI18N
232 // Return message key
237 public DialableFaxNumber getFax () {
242 public void setFax (final DialableFaxNumber fax) {
247 public DialableLandLineNumber getLandLine () {
248 return this.landLine;
252 public void setLandLine (final DialableLandLineNumber landLine) {
253 this.landLine = landLine;
257 public User getUser () {
262 public void setUser (final User user) {
267 * Set's all given contact's phone instances: land-line, cellphone and fax
269 * @param contact Contact to set phone instances for
271 private void setPhoneInstances (final Contact contact) {
272 // The contact must be valid
273 if (null == contact) {
275 throw new NullPointerException("contact is null"); //NOI18N
276 } else if (contact.getContactId() == null) {
278 throw new NullPointerException("contact.contactId is null"); //NOI18N
279 } else if (contact.getContactId() < 1) {
281 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
285 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
286 // Yes, then set it in admin controller
287 this.setCellPhoneNumber(contact.getContactCellphoneNumber());
291 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
292 // Yes, then set it in admin controller
293 this.setLandLine(contact.getContactLandLineNumber());
297 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
298 // Yes, then set it in admin controller
299 this.setFax(contact.getContactFaxNumber());