2 * Copyright (C) 2016 Roland Häder GmbH
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.addressbook.beans.helper;
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;
34 * A general helper for beans
36 * @author Roland Häder<roland@mxchange.org>
38 @Named ("adminHelper")
40 public class AddressbookWebRequestHelper implements AddressbookWebRequestController {
45 private static final long serialVersionUID = 17_258_793_567_145_701L;
48 * Administrative contact controller
51 private AddressbookAdminContactWebRequestController adminContactController;
54 * Administrative phone controller
57 private AddressbookAdminPhoneWebRequestController adminPhoneController;
62 private Contact contact;
70 * Regular user controller
73 private AddressbookUserWebSessionController userController;
78 public AddressbookWebRequestHelper () {
82 public void copyContactToController () {
83 // Validate user instance
84 if (this.getContact() == null) {
86 throw new NullPointerException("this.contact is null"); //NOI18N
87 } else if (this.getContact().getContactId() == null) {
89 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
90 } else if (this.getContact().getContactId() < 1) {
92 throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
95 // Set all phone instances
96 this.setPhoneInstances(this.getContact());
98 // Set all fields: user
99 this.adminContactController.copyContactToController(this.getContact());
103 public void copyUserToController () {
104 // Validate user instance
105 if (this.getUser() == null) {
107 throw new NullPointerException("this.user is null"); //NOI18N
108 } else if (this.getUser().getUserId() == null) {
110 throw new NullPointerException("this.user.userId is null"); //NOI18N
111 } else if (this.getUser().getUserId() < 1) {
113 throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
117 Contact userContact = this.getUser().getUserContact();
119 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
120 this.setContact(userContact);
122 // Set all phone instances
123 this.setPhoneInstances(userContact);
125 // Set all fields: user
126 this.userController.setUserName(this.getUser().getUserName());
130 public Contact getContact () {
135 public void setContact (final Contact contact) {
136 this.contact = contact;
140 public String getContactUsageMessageKey (final Contact contact) {
141 // The contact must be valid
142 if (null == contact) {
144 throw new NullPointerException("contact is null"); //NOI18N
145 } else if (contact.getContactId() == null) {
147 throw new NullPointerException("contact.contactId is null"); //NOI18N
148 } else if (contact.getContactId() < 1) {
150 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
153 // Default key is "unused"
154 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
157 boolean isUserContact = this.userController.isContactFound(contact);
162 messageKey = "CONTACT_IS_USER"; //NOI18N
165 // Return message key
170 public User getUser () {
175 public void setUser (final User user) {
180 * Post-construction method
183 public void init () {
187 * Set's all given contact's phone instances: land-line, mobile and fax
189 * @param contact Contact to set phone instances for
191 private void setPhoneInstances (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
205 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
206 // Yes, then set it in admin controller
207 this.adminPhoneController.setMobileNumber(contact.getContactMobileNumber());
211 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
212 // Yes, then set it in admin controller
213 this.adminPhoneController.setLandLine(contact.getContactLandLineNumber());
217 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
218 // Yes, then set it in admin controller
219 this.adminPhoneController.setFax(contact.getContactFaxNumber());