From 13a7635c2c9530244b7178f8b1cfc8bab1f28600 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/JobsContactWebSessionBean.java | 74 +----- .../JobsContactWebSessionController.java | 14 -- .../phone/JobsContactPhoneWebSessionBean.java | 213 ++++++++++++++++++ .../JobsContactPhoneWebSessionController.java | 69 ++++++ .../JobsCountryWebApplicationBean.java | 8 +- .../JobsCountryWebApplicationController.java | 7 - .../beans/user/JobsUserWebSessionBean.java | 12 +- .../user/JobsUserWebSessionController.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(+), 121 deletions(-) create mode 100644 src/java/org/mxchange/jjobs/beans/contact/phone/JobsContactPhoneWebSessionBean.java create mode 100644 src/java/org/mxchange/jjobs/beans/contact/phone/JobsContactPhoneWebSessionController.java diff --git a/src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java b/src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java index 80ee15f5..7ccff238 100644 --- a/src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java +++ b/src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java @@ -19,11 +19,9 @@ package org.mxchange.jjobs.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; @@ -45,7 +43,6 @@ import org.mxchange.jcontacts.exceptions.ContactNotFoundException; import org.mxchange.jcountry.data.Country; import org.mxchange.jjobs.beans.BaseJobsController; import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController; -import org.mxchange.jjobs.beans.phone.JobsAdminPhoneWebRequestController; import org.mxchange.jjobs.beans.user.JobsUserWebSessionController; import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber; import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber; @@ -54,7 +51,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; @@ -77,17 +73,6 @@ public class JobsContactWebSessionBean extends BaseJobsController implements Job */ private static final long serialVersionUID = 542_145_347_916L; - /** - * Remote EJB for phone number (administrative) - */ - private AdminPhoneSessionBeanRemote adminPhoneBean; - - /** - * Administrative phone controller - */ - @Inject - private JobsAdminPhoneWebRequestController adminPhoneController; - /** * Birth day */ @@ -103,11 +88,6 @@ public class JobsContactWebSessionBean extends BaseJobsController implements Job */ private Long cellphoneNumber; - /** - * All cell phone numbers - */ - private final List cellphoneNumbers; - /** * City */ @@ -128,12 +108,6 @@ public class JobsContactWebSessionBean extends BaseJobsController implements Job */ 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 JobsContactWebSessionBean extends BaseJobsController implements Job // Try to lookup this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N - - // Try to lookup the beans - this.adminPhoneBean = (AdminPhoneSessionBeanRemote) context.lookup("java:global/jjobs-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<>(); } @@ -457,42 +426,13 @@ public class JobsContactWebSessionBean extends BaseJobsController implements Job } @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 @@ -525,7 +465,6 @@ public class JobsContactWebSessionBean extends BaseJobsController implements Job // Debug message //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress())); - // Don't set null or wrong references if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) { // Now the number must be given @@ -585,7 +524,6 @@ public class JobsContactWebSessionBean extends BaseJobsController implements Job // Trace message //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); - // Return it return contact; } @@ -873,11 +811,6 @@ public class JobsContactWebSessionBean extends BaseJobsController implements Job this.zipCode = zipCode; } - @Override - public boolean hasContacts () { - return (!this.contactList.isEmpty()); - } - /** * Post-initialization of this class */ @@ -922,9 +855,6 @@ public class JobsContactWebSessionBean extends BaseJobsController implements Job // Set contact list this.selectableContacts = allContacts; - - // All phone numbers - this.cellphoneNumbers.addAll(this.adminPhoneBean.allCellphoneNumbers()); } @Override diff --git a/src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionController.java b/src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionController.java index 8dd604f9..2640d420 100644 --- a/src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionController.java +++ b/src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionController.java @@ -53,20 +53,6 @@ public interface JobsContactWebSessionController 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/jjobs/beans/contact/phone/JobsContactPhoneWebSessionBean.java b/src/java/org/mxchange/jjobs/beans/contact/phone/JobsContactPhoneWebSessionBean.java new file mode 100644 index 00000000..f3292cc1 --- /dev/null +++ b/src/java/org/mxchange/jjobs/beans/contact/phone/JobsContactPhoneWebSessionBean.java @@ -0,0 +1,213 @@ +/* + * Copyright (C) 2016 Roland Haeder + * + * 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.jjobs.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.jjobs.beans.BaseJobsController; +import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController; +import org.mxchange.jjobs.beans.phone.JobsAdminPhoneWebRequestController; +import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber; +import org.mxchange.jphone.phonenumbers.phone.AdminPhoneSessionBeanRemote; +import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent; + +/** + * A general contact bean (controller) + *

+ * @author Roland Haeder + */ +@Named ("contactPhoneController") +@SessionScoped +public class JobsContactPhoneWebSessionBean extends BaseJobsController implements JobsContactPhoneWebSessionController { + + /** + * 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 JobsAdminPhoneWebRequestController adminPhoneController; + + /** + * All cell phone numbers + */ + private final List cellphoneNumbers; + + /** + * General contact controller + */ + @Inject + private JobsContactWebSessionController 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 JobsContactPhoneWebSessionBean () { + // 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/jjobs/beans/contact/phone/JobsContactPhoneWebSessionController.java b/src/java/org/mxchange/jjobs/beans/contact/phone/JobsContactPhoneWebSessionController.java new file mode 100644 index 00000000..fefb5c1a --- /dev/null +++ b/src/java/org/mxchange/jjobs/beans/contact/phone/JobsContactPhoneWebSessionController.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2016 Roland Haeder + * + * 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.jjobs.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 JobsContactPhoneWebSessionController 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/jjobs/beans/country/JobsCountryWebApplicationBean.java b/src/java/org/mxchange/jjobs/beans/country/JobsCountryWebApplicationBean.java index f2f15c02..ff02ee7a 100644 --- a/src/java/org/mxchange/jjobs/beans/country/JobsCountryWebApplicationBean.java +++ b/src/java/org/mxchange/jjobs/beans/country/JobsCountryWebApplicationBean.java @@ -17,7 +17,6 @@ package org.mxchange.jjobs.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 JobsCountryWebApplicationBean extends BaseJobsController implements @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/jjobs/beans/country/JobsCountryWebApplicationController.java b/src/java/org/mxchange/jjobs/beans/country/JobsCountryWebApplicationController.java index 1f661321..226fcc45 100644 --- a/src/java/org/mxchange/jjobs/beans/country/JobsCountryWebApplicationController.java +++ b/src/java/org/mxchange/jjobs/beans/country/JobsCountryWebApplicationController.java @@ -35,13 +35,6 @@ public interface JobsCountryWebApplicationController 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/jjobs/beans/user/JobsUserWebSessionBean.java b/src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionBean.java index 2040fcd4..378218cf 100644 --- a/src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionBean.java +++ b/src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionBean.java @@ -17,7 +17,6 @@ package org.mxchange.jjobs.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 JobsUserWebSessionBean extends BaseJobsController implements JobsUs } @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 JobsUserWebSessionBean extends BaseJobsController implements JobsUs this.userProfileMode = userProfileMode; } - @Override - public boolean hasUsers () { - return (!this.allUsers().isEmpty()); - } - /** * Post-initialization of this class */ diff --git a/src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionController.java b/src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionController.java index b64268aa..418a251b 100644 --- a/src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionController.java +++ b/src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionController.java @@ -107,13 +107,6 @@ public interface JobsUserWebSessionController 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 32f5ccdd..26e34fd1 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 d00e40f1..29f93641 100644 --- a/web/admin/user/admin_user_list.xhtml +++ b/web/admin/user/admin_user_list.xhtml @@ -15,7 +15,7 @@ - + -- 2.39.2