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 () {
103 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyContactToController - CALLED!"); //NOI18N
105 // Validate user instance
106 if (this.getContact() == null) {
108 throw new NullPointerException("this.contact is null"); //NOI18N
109 } else if (this.getContact().getContactId() == null) {
111 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
112 } else if (this.getContact().getContactId() < 1) {
114 throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
117 // Set all phone instances
118 this.setPhoneInstances(this.getContact());
120 // Set all fields: user
121 this.adminContactController.copyContactToController(this.getContact());
124 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyContactToController - EXIT!"); //NOI18N
128 public void copyCustomerToController () {
130 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyCustomerToController - CALLED!"); //NOI18N
132 // Validate user instance
133 if (this.getCustomer() == null) {
135 throw new NullPointerException("this.customer is null"); //NOI18N
136 } else if (this.getCustomer().getCustomerId() == null) {
138 throw new NullPointerException("this.customer.customerId is null"); //NOI18N
139 } else if (this.getCustomer().getCustomerId() < 1) {
141 throw new IllegalStateException(MessageFormat.format("this.customer.customerId={0} is not valid.", this.getCustomer().getCustomerId())); //NOI18N
144 // Set all fields: user
145 this.adminCustomerController.copyCustomerToController(this.getCustomer());
148 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyCustomerToController - EXIT!"); //NOI18N
152 public void copyUserToController () {
154 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyUserToController - CALLED!"); //NOI18N
156 // Validate user instance
157 if (this.getUser() == null) {
159 throw new NullPointerException("this.user is null"); //NOI18N
160 } else if (this.getUser().getUserId() == null) {
162 throw new NullPointerException("this.user.userId is null"); //NOI18N
163 } else if (this.getUser().getUserId() < 1) {
165 throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
169 Contact userContact = this.getUser().getUserContact();
171 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
172 this.setContact(userContact);
174 // Set all phone instances
175 this.setPhoneInstances(userContact);
177 // Set all fields: user
178 this.userController.setUserName(this.getUser().getUserName());
181 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyUserToController - EXIT!"); //NOI18N
185 public Contact getContact () {
190 public void setContact (final Contact contact) {
191 this.contact = contact;
195 public String getContactUsageMessageKey (final Contact contact) {
196 // The contact must be valid
197 if (null == contact) {
199 throw new NullPointerException("contact is null"); //NOI18N
200 } else if (contact.getContactId() == null) {
202 throw new NullPointerException("contact.contactId is null"); //NOI18N
203 } else if (contact.getContactId() < 1) {
205 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
208 // Default key is "unused"
209 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
211 // Check user/customer
212 boolean isUserContact = this.userController.isContactFound(contact);
213 boolean isCustomerContact = this.customerController.isContactFound(contact);
216 if (isUserContact && isCustomerContact) {
218 messageKey = "CONTACT_IS_USER_USER"; //NOI18N
219 } else if (isUserContact) {
221 messageKey = "CONTACT_IS_USER"; //NOI18N
222 } else if (isCustomerContact) {
224 messageKey = "CONTACT_IS_USER"; //NOI18N
227 // Return message key
232 public Customer getCustomer () {
233 return this.customer;
237 public void setCustomer (final Customer customer) {
238 this.customer = customer;
242 public User getUser () {
247 public void setUser (final User user) {
252 * Set's all given contact's phone instances: land-line, cellphone and fax
254 * @param contact Contact to set phone instances for
256 private void setPhoneInstances (final Contact contact) {
257 // The contact must be valid
258 if (null == contact) {
260 throw new NullPointerException("contact is null"); //NOI18N
261 } else if (contact.getContactId() == null) {
263 throw new NullPointerException("contact.contactId is null"); //NOI18N
264 } else if (contact.getContactId() < 1) {
266 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
270 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
271 // Yes, then set it in admin controller
272 this.adminPhoneController.setMobileNumber(contact.getContactMobileNumber());
276 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
277 // Yes, then set it in admin controller
278 this.adminPhoneController.setLandLineNumber(contact.getContactLandLineNumber());
282 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
283 // Yes, then set it in admin controller
284 this.adminPhoneController.setFaxNumber(contact.getContactFaxNumber());