2 * Copyright (C) 2016 Roland Häder
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.pizzaapplication.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.jcustomercore.model.customer.Customer;
25 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
26 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
27 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
28 import org.mxchange.jusercore.model.user.User;
29 import org.mxchange.pizzaapplication.beans.contact.PizzaAdminContactWebRequestController;
30 import org.mxchange.pizzaapplication.beans.customer.PizzaAdminCustomerWebRequestController;
31 import org.mxchange.pizzaapplication.beans.customer.PizzaCustomerWebSessionController;
32 import org.mxchange.pizzaapplication.beans.phone.PizzaAdminPhoneWebRequestController;
33 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
36 * A general helper for beans
38 * @author Roland Häder<roland@mxchange.org>
42 public class PizzaWebRequestHelper implements PizzaWebRequestController {
47 private static final long serialVersionUID = 17_258_793_567_145_701L;
50 * Administrative contact controller
53 private PizzaAdminContactWebRequestController adminContactController;
56 * Administrative user controller
59 private PizzaAdminCustomerWebRequestController adminCustomerController;
62 * Administrative phone controller
65 private PizzaAdminPhoneWebRequestController adminPhoneController;
70 private Contact contact;
75 private Customer customer;
78 * General user controller
81 private PizzaCustomerWebSessionController customerController;
89 * Regular user controller
92 private PizzaUserWebSessionController userController;
97 public PizzaWebRequestHelper () {
101 public void copyContactToController () {
102 // Validate user instance
103 if (this.getContact() == null) {
105 throw new NullPointerException("this.contact is null"); //NOI18N
106 } else if (this.getContact().getContactId() == null) {
108 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
109 } else if (this.getContact().getContactId() < 1) {
111 throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
114 // Set all phone instances
115 this.setPhoneInstances(this.getContact());
117 // Set all fields: user
118 this.adminContactController.copyContactToController(this.getContact());
122 public void copyCustomerToController () {
124 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyCustomerToController - CALLED!"); //NOI18N
126 // Validate user instance
127 if (this.getCustomer() == null) {
129 throw new NullPointerException("this.customer is null"); //NOI18N
130 } else if (this.getCustomer().getCustomerId() == null) {
132 throw new NullPointerException("this.customer.customerId is null"); //NOI18N
133 } else if (this.getCustomer().getCustomerId() < 1) {
135 throw new IllegalStateException(MessageFormat.format("this.customer.customerId={0} is not valid.", this.getCustomer().getCustomerId())); //NOI18N
138 // Set all fields: user
139 this.adminCustomerController.copyCustomerToController(this.getCustomer());
142 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyCustomerToController - EXIT!"); //NOI18N
146 public void copyUserToController () {
147 // Validate user instance
148 if (this.getUser() == null) {
150 throw new NullPointerException("this.user is null"); //NOI18N
151 } else if (this.getUser().getUserId() == null) {
153 throw new NullPointerException("this.user.userId is null"); //NOI18N
154 } else if (this.getUser().getUserId() < 1) {
156 throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
160 Contact userContact = this.getUser().getUserContact();
162 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
163 this.setContact(userContact);
165 // Set all phone instances
166 this.setPhoneInstances(userContact);
168 // Set all fields: user
169 this.userController.setUserName(this.getUser().getUserName());
173 public Contact getContact () {
178 public void setContact (final Contact contact) {
179 this.contact = contact;
183 public String getContactUsageMessageKey (final Contact contact) {
184 // The contact must be valid
185 if (null == contact) {
187 throw new NullPointerException("contact is null"); //NOI18N
188 } else if (contact.getContactId() == null) {
190 throw new NullPointerException("contact.contactId is null"); //NOI18N
191 } else if (contact.getContactId() < 1) {
193 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
196 // Default key is "unused"
197 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
199 // Check user/customer
200 boolean isUserContact = this.userController.isContactFound(contact);
201 boolean isCustomerContact = this.customerController.isContactFound(contact);
204 if (isUserContact && isCustomerContact) {
206 messageKey = "CONTACT_IS_USER_USER"; //NOI18N
207 } else if (isUserContact) {
209 messageKey = "CONTACT_IS_USER"; //NOI18N
210 } else if (isCustomerContact) {
212 messageKey = "CONTACT_IS_USER"; //NOI18N
215 // Return message key
220 public Customer getCustomer () {
221 return this.customer;
225 public void setCustomer (final Customer customer) {
226 this.customer = customer;
230 public User getUser () {
235 public void setUser (final User user) {
240 * Set's all given contact's phone instances: land-line, cellphone and fax
242 * @param contact Contact to set phone instances for
244 private void setPhoneInstances (final Contact contact) {
245 // The contact must be valid
246 if (null == contact) {
248 throw new NullPointerException("contact is null"); //NOI18N
249 } else if (contact.getContactId() == null) {
251 throw new NullPointerException("contact.contactId is null"); //NOI18N
252 } else if (contact.getContactId() < 1) {
254 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
258 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
259 // Yes, then set it in admin controller
260 this.adminPhoneController.setMobileNumber(contact.getContactMobileNumber());
264 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
265 // Yes, then set it in admin controller
266 this.adminPhoneController.setLandLineNumber(contact.getContactLandLineNumber());
270 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
271 // Yes, then set it in admin controller
272 this.adminPhoneController.setFaxNumber(contact.getContactFaxNumber());