From ad451b4b965bdc956b24f1b39084cd5ed8eecf58 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 8 Aug 2016 17:43:11 +0200 Subject: [PATCH] Continued with splitting/cleanup: (please cherry-pick) - removed all boolean hasFoos() methods as isEmpty() works the same way - got rid of many Collections.unmodifiableList() calls as they may confuse the developer, please watch out! - splitted contact's special phone stuff out to own controller (bean) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../contact/PizzaContactWebSessionBean.java | 72 +----- .../PizzaContactWebSessionController.java | 14 -- .../PizzaContactPhoneWebSessionBean.java | 213 ++++++++++++++++++ ...PizzaContactPhoneWebSessionController.java | 69 ++++++ .../PizzaCountryWebApplicationBean.java | 8 +- .../PizzaCountryWebApplicationController.java | 7 - .../beans/user/PizzaUserWebSessionBean.java | 12 +- .../user/PizzaUserWebSessionController.java | 7 - .../cellphone/admin_cellphone_show.xhtml | 2 +- web/admin/contact/admin_contact_export.xhtml | 2 +- web/admin/contact/admin_contact_list.xhtml | 2 +- web/admin/country/admin_country_list.xhtml | 2 +- web/admin/user/admin_user_export.xhtml | 2 +- web/admin/user/admin_user_list.xhtml | 2 +- 14 files changed, 295 insertions(+), 119 deletions(-) create mode 100644 src/java/org/mxchange/pizzaapplication/beans/contact/phone/PizzaContactPhoneWebSessionBean.java create mode 100644 src/java/org/mxchange/pizzaapplication/beans/contact/phone/PizzaContactPhoneWebSessionController.java diff --git a/src/java/org/mxchange/pizzaapplication/beans/contact/PizzaContactWebSessionBean.java b/src/java/org/mxchange/pizzaapplication/beans/contact/PizzaContactWebSessionBean.java index dfbb9c9a..d337c840 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/contact/PizzaContactWebSessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/contact/PizzaContactWebSessionBean.java @@ -19,11 +19,9 @@ package org.mxchange.pizzaapplication.beans.contact; import java.text.MessageFormat; import java.util.Collections; import java.util.Date; -import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; -import java.util.Map; import java.util.Objects; import javax.annotation.PostConstruct; import javax.enterprise.context.SessionScoped; @@ -50,7 +48,6 @@ import org.mxchange.jphone.phonenumbers.fax.FaxNumber; import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; import org.mxchange.jphone.phonenumbers.landline.LandLineNumber; import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider; -import org.mxchange.jphone.phonenumbers.phone.AdminPhoneSessionBeanRemote; import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent; import org.mxchange.jusercore.events.login.UserLoggedInEvent; import org.mxchange.jusercore.events.registration.UserRegisteredEvent; @@ -60,7 +57,6 @@ import org.mxchange.jusercore.exceptions.UserPasswordMismatchException; import org.mxchange.jusercore.model.user.User; import org.mxchange.pizzaapplication.beans.BasePizzaController; import org.mxchange.pizzaapplication.beans.login.PizzaUserLoginWebSessionController; -import org.mxchange.pizzaapplication.beans.phone.PizzaAdminPhoneWebRequestController; import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController; /** @@ -77,17 +73,6 @@ public class PizzaContactWebSessionBean extends BasePizzaController implements P */ private static final long serialVersionUID = 542_145_347_916L; - /** - * Remote EJB for phone number (administrative) - */ - private AdminPhoneSessionBeanRemote adminPhoneBean; - - /** - * Administrative phone controller - */ - @Inject - private PizzaAdminPhoneWebRequestController adminPhoneController; - /** * Birth day */ @@ -103,11 +88,6 @@ public class PizzaContactWebSessionBean extends BasePizzaController implements P */ private Long cellphoneNumber; - /** - * All cell phone numbers - */ - private final List cellphoneNumbers; - /** * City */ @@ -128,12 +108,6 @@ public class PizzaContactWebSessionBean extends BasePizzaController implements P */ private final List contactList; - /** - * "Cache" for contact lists, mostly only one is assigned. So this cache - * shouldn't grow beyond control. - */ - private final Map> contacts; - /** * Country instance */ @@ -267,17 +241,12 @@ public class PizzaContactWebSessionBean extends BasePizzaController implements P // Try to lookup this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N - - // Try to lookup the beans - this.adminPhoneBean = (AdminPhoneSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/adminphone!org.mxchange.jphone.phonenumbers.phone.AdminPhoneSessionBeanRemote"); //NOI18N } catch (final NamingException e) { // Throw again throw new FaceletException(e); } // Init lists/maps - this.cellphoneNumbers = new LinkedList<>(); - this.contacts = new HashMap<>(10); this.contactList = new LinkedList<>(); this.emailAddressList = new LinkedList<>(); } @@ -460,42 +429,13 @@ public class PizzaContactWebSessionBean extends BasePizzaController implements P } @Override - public List allCellphoneContacts () { - // Get id - Long phoneId = this.adminPhoneController.getCellPhone().getPhoneId(); - - // Is cache there? - if (this.contacts.containsKey(phoneId)) { - // Return cached version - return this.contacts.get(phoneId); - } else { - // Ask bean - List list = new LinkedList<>(); - - // "Walk" through all contacts - for (final Contact contact : this.contactList) { - // Is cellphone instance the same? - if (Objects.equals(contact.getContactCellphoneNumber(), this.adminPhoneController.getCellPhone())) { - // Found one - list.add(contact); - } - } - - // Store result in cache - this.contacts.put(phoneId, list); - - // Return now-cached list - return list; - } - } - - @Override + @SuppressWarnings ("ReturnOfCollectionOrArrayField") public List allContacts () { // Debug message //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactController.allContacts: contactList.size()={0} - EXIT!", this.contactList.size())); // Return un-modified list - return Collections.unmodifiableList(this.contactList); + return this.contactList; } @Override @@ -876,11 +816,6 @@ public class PizzaContactWebSessionBean extends BasePizzaController implements P this.zipCode = zipCode; } - @Override - public boolean hasContacts () { - return (!this.contactList.isEmpty()); - } - /** * Post-initialization of this class */ @@ -925,9 +860,6 @@ public class PizzaContactWebSessionBean extends BasePizzaController implements P // Set contact list this.selectableContacts = allContacts; - - // All phone numbers - this.cellphoneNumbers.addAll(this.adminPhoneBean.allCellphoneNumbers()); } @Override diff --git a/src/java/org/mxchange/pizzaapplication/beans/contact/PizzaContactWebSessionController.java b/src/java/org/mxchange/pizzaapplication/beans/contact/PizzaContactWebSessionController.java index 79d1ac25..6d4414e1 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/contact/PizzaContactWebSessionController.java +++ b/src/java/org/mxchange/pizzaapplication/beans/contact/PizzaContactWebSessionController.java @@ -53,20 +53,6 @@ public interface PizzaContactWebSessionController extends Serializable { */ List allContacts (); - /** - * Getter for all contacts having current cellphone instance linked - *

- * @return List of all linked contacts - */ - List allCellphoneContacts (); - - /** - * Checks whether there are contacts. - *

- * @return Whether contacts are there - */ - boolean hasContacts (); - /** * Event observer for newly added users by adminstrator *

diff --git a/src/java/org/mxchange/pizzaapplication/beans/contact/phone/PizzaContactPhoneWebSessionBean.java b/src/java/org/mxchange/pizzaapplication/beans/contact/phone/PizzaContactPhoneWebSessionBean.java new file mode 100644 index 00000000..0411a6ba --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/beans/contact/phone/PizzaContactPhoneWebSessionBean.java @@ -0,0 +1,213 @@ +/* + * Copyright (C) 2016 Cho-Time GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.pizzaapplication.beans.contact.phone; + +import java.text.MessageFormat; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import javax.annotation.PostConstruct; +import javax.enterprise.context.SessionScoped; +import javax.enterprise.event.Observes; +import javax.faces.view.facelets.FaceletException; +import javax.inject.Inject; +import javax.inject.Named; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import org.mxchange.jcontacts.contact.Contact; +import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent; +import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent; +import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber; +import org.mxchange.jphone.phonenumbers.phone.AdminPhoneSessionBeanRemote; +import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent; +import org.mxchange.pizzaapplication.beans.BasePizzaController; +import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController; +import org.mxchange.pizzaapplication.beans.phone.PizzaAdminPhoneWebRequestController; + +/** + * A general contact bean (controller) + *

+ * @author Roland Haeder + */ +@Named ("contactPhoneController") +@SessionScoped +public class PizzaContactPhoneWebSessionBean extends BasePizzaController implements PizzaContactPhoneWebSessionController { + + /** + * Serial number + */ + private static final long serialVersionUID = 542_145_347_916L; + + /** + * Remote EJB for phone number (administrative) + */ + private AdminPhoneSessionBeanRemote adminPhoneBean; + + /** + * Administrative phone controller + */ + @Inject + private PizzaAdminPhoneWebRequestController adminPhoneController; + + /** + * All cell phone numbers + */ + private final List cellphoneNumbers; + + /** + * General contact controller + */ + @Inject + private PizzaContactWebSessionController contactController; + + /** + * "Cache" for contact lists, mostly only one is assigned. So this cache + * shouldn't grow beyond control. + */ + private final Map> contacts; + + /** + * Default constructor + */ + public PizzaContactPhoneWebSessionBean () { + // Try it + try { + // Get initial context + Context context = new InitialContext(); + + // Try to lookup the beans + this.adminPhoneBean = (AdminPhoneSessionBeanRemote) context.lookup("java:global/jlandingpage-ejb/adminphone!org.mxchange.jphone.phonenumbers.phone.AdminPhoneSessionBeanRemote"); //NOI18N + } catch (final NamingException e) { + // Throw again + throw new FaceletException(e); + } + + // Init lists/maps + this.cellphoneNumbers = new LinkedList<>(); + this.contacts = new HashMap<>(10); + } + + @Override + public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) { + // The event must be valid + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getAddedContact() == null) { + // Throw again ... + throw new NullPointerException("event.addedContact is null"); //NOI18N + } else if (event.getAddedContact().getContactId() == null) { + // ... and again + throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N + } else if (event.getAddedContact().getContactId() < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N + } + + // Clear this bean + this.clear(); + } + + @Override + public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getAddedUser() == null) { + // Throw NPE again + throw new NullPointerException("event.addedUser is null"); //NOI18N + } else if (event.getAddedUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.addedUser.userId is null"); //NOI18N + } else if (event.getAddedUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N + } + + // Clear all data + this.clear(); + } + + @Override + public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getUpdatedContact() == null) { + // Throw NPE again + throw new NullPointerException("event.updatedContact is null"); //NOI18N + } else if (event.getUpdatedContact().getContactId() == null) { + // userId is null + throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N + } else if (event.getUpdatedContact().getContactId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N + } + } + + @Override + public List allCellphoneContacts () { + // Get id + Long phoneId = this.adminPhoneController.getCellPhone().getPhoneId(); + + // Is cache there? + if (this.contacts.containsKey(phoneId)) { + // Return cached version + return this.contacts.get(phoneId); + } else { + // Ask bean + List list = new LinkedList<>(); + + // "Walk" through all contacts + for (final Contact contact : this.contactController.allContacts()) { + // Is cellphone instance the same? + if (Objects.equals(contact.getContactCellphoneNumber(), this.adminPhoneController.getCellPhone())) { + // Found one + list.add(contact); + } + } + + // Store result in cache + this.contacts.put(phoneId, list); + + // Return now-cached list + return list; + } + } + + /** + * Post-initialization of this class + */ + @PostConstruct + public void init () { + // All phone numbers + this.cellphoneNumbers.addAll(this.adminPhoneBean.allCellphoneNumbers()); + } + + /** + * Clears this bean + */ + private void clear () { + // Clear all data + } + +} diff --git a/src/java/org/mxchange/pizzaapplication/beans/contact/phone/PizzaContactPhoneWebSessionController.java b/src/java/org/mxchange/pizzaapplication/beans/contact/phone/PizzaContactPhoneWebSessionController.java new file mode 100644 index 00000000..becb2a97 --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/beans/contact/phone/PizzaContactPhoneWebSessionController.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2016 Cho-Time GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.pizzaapplication.beans.contact.phone; + +import java.io.Serializable; +import java.util.List; +import javax.ejb.Local; +import org.mxchange.jcontacts.contact.Contact; +import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent; +import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent; +import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent; + +/** + * An interface for user beans + *

+ * @author Roland Haeder + */ +@Local +public interface PizzaContactPhoneWebSessionController extends Serializable { + + /** + * Minimum password length + */ + public static final Integer MINIMUM_PASSWORD_LENGTH = 5; + + /** + * Getter for all contacts having current cellphone instance linked + *

+ * @return List of all linked contacts + */ + List allCellphoneContacts (); + + /** + * Event observer for newly added users by adminstrator + *

+ * @param event Event being fired + */ + void afterAdminAddedUserEvent (final AdminAddedUserEvent event); + + /** + * Observes events being fired when an administrator has added a new + * contact. + *

+ * @param event Event being fired + */ + void afterAdminAddedContact (final AdminAddedContactEvent event); + + /** + * Event observer for updated contact data by administrators + *

+ * @param event Updated contact data event + */ + void afterAdminUpdatedContactDataEvent (final AdminUpdatedContactEvent event); + +} diff --git a/src/java/org/mxchange/pizzaapplication/beans/country/PizzaCountryWebApplicationBean.java b/src/java/org/mxchange/pizzaapplication/beans/country/PizzaCountryWebApplicationBean.java index 02707613..752b97e1 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/country/PizzaCountryWebApplicationBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/country/PizzaCountryWebApplicationBean.java @@ -17,7 +17,6 @@ package org.mxchange.pizzaapplication.beans.country; import java.text.MessageFormat; -import java.util.Collections; import java.util.List; import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; @@ -97,12 +96,7 @@ public class PizzaCountryWebApplicationBean extends BasePizzaController implemen @Override public List allCountries () { // Return "cached" version - return Collections.unmodifiableList(this.countryList); - } - - @Override - public boolean hasCountries () { - return (!this.countryList.isEmpty()); + return this.countryList; } /** diff --git a/src/java/org/mxchange/pizzaapplication/beans/country/PizzaCountryWebApplicationController.java b/src/java/org/mxchange/pizzaapplication/beans/country/PizzaCountryWebApplicationController.java index ef787e6f..2ae92eea 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/country/PizzaCountryWebApplicationController.java +++ b/src/java/org/mxchange/pizzaapplication/beans/country/PizzaCountryWebApplicationController.java @@ -35,13 +35,6 @@ public interface PizzaCountryWebApplicationController extends Serializable { */ List allCountries (); - /** - * Checks whether countries has been registered - *

- * @return Whether countries has been registered - */ - boolean hasCountries (); - /** * Observing method when the event is fired that an administrator added a * new country diff --git a/src/java/org/mxchange/pizzaapplication/beans/user/PizzaUserWebSessionBean.java b/src/java/org/mxchange/pizzaapplication/beans/user/PizzaUserWebSessionBean.java index c09ea390..18925b20 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/user/PizzaUserWebSessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/user/PizzaUserWebSessionBean.java @@ -17,7 +17,6 @@ package org.mxchange.pizzaapplication.beans.user; import java.text.MessageFormat; -import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Objects; @@ -389,15 +388,17 @@ public class PizzaUserWebSessionBean extends BasePizzaController implements Pizz } @Override + @SuppressWarnings ("ReturnOfCollectionOrArrayField") public List allUsers () { // Return it - return Collections.unmodifiableList(this.userList); + return this.userList; } @Override + @SuppressWarnings ("ReturnOfCollectionOrArrayField") public List allVisibleUsers () { // Return it - return Collections.unmodifiableList(this.visibleUserList); + return this.visibleUserList; } @Override @@ -573,11 +574,6 @@ public class PizzaUserWebSessionBean extends BasePizzaController implements Pizz this.userProfileMode = userProfileMode; } - @Override - public boolean hasUsers () { - return (!this.allUsers().isEmpty()); - } - /** * Post-initialization of this class */ diff --git a/src/java/org/mxchange/pizzaapplication/beans/user/PizzaUserWebSessionController.java b/src/java/org/mxchange/pizzaapplication/beans/user/PizzaUserWebSessionController.java index 30995458..2847064e 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/user/PizzaUserWebSessionController.java +++ b/src/java/org/mxchange/pizzaapplication/beans/user/PizzaUserWebSessionController.java @@ -107,13 +107,6 @@ public interface PizzaUserWebSessionController extends Serializable { */ List allVisibleUsers (); - /** - * Checks whether users are registered - *

- * @return Whether users are registered - */ - boolean hasUsers (); - /** * Checks whether the given contact is a user *

diff --git a/web/admin/cellphone/admin_cellphone_show.xhtml b/web/admin/cellphone/admin_cellphone_show.xhtml index e59ccd73..569b0624 100644 --- a/web/admin/cellphone/admin_cellphone_show.xhtml +++ b/web/admin/cellphone/admin_cellphone_show.xhtml @@ -23,7 +23,7 @@ - + diff --git a/web/admin/contact/admin_contact_export.xhtml b/web/admin/contact/admin_contact_export.xhtml index 4cb937cc..502d57a3 100644 --- a/web/admin/contact/admin_contact_export.xhtml +++ b/web/admin/contact/admin_contact_export.xhtml @@ -17,7 +17,7 @@ - + diff --git a/web/admin/contact/admin_contact_list.xhtml b/web/admin/contact/admin_contact_list.xhtml index b01ffd2e..5b081f69 100644 --- a/web/admin/contact/admin_contact_list.xhtml +++ b/web/admin/contact/admin_contact_list.xhtml @@ -15,7 +15,7 @@ - + diff --git a/web/admin/country/admin_country_list.xhtml b/web/admin/country/admin_country_list.xhtml index 6e6b3e7e..2462fdd7 100644 --- a/web/admin/country/admin_country_list.xhtml +++ b/web/admin/country/admin_country_list.xhtml @@ -15,7 +15,7 @@ - + diff --git a/web/admin/user/admin_user_export.xhtml b/web/admin/user/admin_user_export.xhtml index 1063e002..151ea73d 100644 --- a/web/admin/user/admin_user_export.xhtml +++ b/web/admin/user/admin_user_export.xhtml @@ -17,7 +17,7 @@ - + diff --git a/web/admin/user/admin_user_list.xhtml b/web/admin/user/admin_user_list.xhtml index 89f6c70d..f82cb0c4 100644 --- a/web/admin/user/admin_user_list.xhtml +++ b/web/admin/user/admin_user_list.xhtml @@ -15,7 +15,7 @@ - + -- 2.39.5