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.fax.DialableFaxNumber;
28 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
29 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
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 Contact contact;
66 private DialableFaxNumber faxNumber;
71 private DialableLandLineNumber landLineNumber;
76 private DialableMobileNumber mobileNumber;
84 * Regular user controller
87 private JobsUserWebSessionController userController;
92 public JobsWebRequestHelper () {
96 public void copyContactToController () {
97 // Validate contact instance
98 if (this.getContact() == null) {
100 throw new NullPointerException("this.contact is null"); //NOI18N
101 } else if (this.getContact().getContactId() == null) {
103 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
104 } else if (this.getContact().getContactId() < 1) {
106 throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
109 // Set all phone instances
110 this.setPhoneInstances(this.getContact());
112 // Set all fields: user
113 this.adminContactController.copyContactToController(this.getContact());
117 public void copyMobileNumberToController () {
118 // Validate mobile instance
119 if (this.getMobileNumber() == null) {
121 throw new NullPointerException("this.mobileNumber is null");
122 } else if (this.getMobileNumber().getPhoneId() == null) {
124 throw new NullPointerException("this.mobileNumber.phoneId is null");
125 } else if (this.getMobileNumber().getPhoneId() < 1) {
127 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId()));
128 } else if (this.getMobileNumber().getMobileProvider() == null) {
130 throw new NullPointerException("this.mobileNumber.mobileProvider is null");
131 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
133 throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null");
134 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
136 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.mobileProvider.providerId={0} is invalid", this.getMobileNumber().getMobileProvider().getProviderId()));
137 } else if (this.getMobileNumber().getPhoneNumber() == null) {
138 // Throw NPE again ...
139 throw new NullPointerException("this.mobileNumber.phoneNumber is null");
140 } else if (this.getMobileNumber().getPhoneNumber() < 1) {
142 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber()));
145 // Copy all (changeable) data fields to admin controller
146 this.adminPhoneController.setMobileProvider(this.getMobileNumber().getMobileProvider());
147 this.adminPhoneController.setPhoneNumber(this.getMobileNumber().getPhoneNumber());
151 public void copyUserToController () {
153 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyUserToController - CALLED!"); //NOI18N
155 // Validate user instance
156 if (this.getUser() == null) {
158 throw new NullPointerException("this.user is null");
159 } else if (this.getUser().getUserId() == null) {
161 throw new NullPointerException("this.user.userId is null");
162 } else if (this.getUser().getUserId() < 1) {
164 throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId()));
168 Contact userContact = this.getUser().getUserContact();
170 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
171 this.setContact(userContact);
173 // Set all phone instances
174 this.setPhoneInstances(userContact);
176 // Set all fields: user
177 this.userController.setUserName(this.getUser().getUserName());
181 public Contact getContact () {
186 public void setContact (final Contact contact) {
187 this.contact = contact;
191 public String getContactUsageMessageKey (final Contact contact) {
192 // The contact must be valid
193 if (null == contact) {
195 throw new NullPointerException("contact is null"); //NOI18N
196 } else if (contact.getContactId() == null) {
198 throw new NullPointerException("contact.contactId is null"); //NOI18N
199 } else if (contact.getContactId() < 1) {
201 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
204 // Default key is "unused"
205 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
208 boolean isUserContact = this.userController.isContactFound(contact);
213 messageKey = "CONTACT_IS_USER"; //NOI18N
216 // Return message key
221 public DialableFaxNumber getFaxNumber () {
222 return this.faxNumber;
226 public void setFaxNumber (final DialableFaxNumber faxNumber) {
227 this.faxNumber = faxNumber;
231 public DialableLandLineNumber getLandLineNumber () {
232 return this.landLineNumber;
236 public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
237 this.landLineNumber = landLineNumber;
241 public DialableMobileNumber getMobileNumber () {
242 return this.mobileNumber;
246 public void setMobileNumber (final DialableMobileNumber mobileNumber) {
247 this.mobileNumber = mobileNumber;
251 public User getUser () {
256 public void setUser (final User user) {
261 * Set's all given contact's phone instances: land-line, mobile and
264 * @param contact Contact to set phone instances for
266 private void setPhoneInstances (final Contact contact) {
267 // The contact must be valid
268 if (null == contact) {
270 throw new NullPointerException("contact is null"); //NOI18N
271 } else if (contact.getContactId() == null) {
273 throw new NullPointerException("contact.contactId is null"); //NOI18N
274 } else if (contact.getContactId() < 1) {
276 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
280 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
281 // Yes, then set it in admin controller
282 this.setMobileNumber(contact.getContactMobileNumber());
286 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
287 // Yes, then set it in admin controller
288 this.setLandLineNumber(contact.getContactLandLineNumber());
292 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
293 // Yes, then set it in admin controller
294 this.setFaxNumber(contact.getContactFaxNumber());