From: Roland Häder Date: Sat, 2 Sep 2017 19:52:41 +0000 (+0200) Subject: Please rename/cherry-pick: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=8e0df0cf281bf63f6683aea90c28427ef04d5af5;p=addressbook-war.git Please rename/cherry-pick: - having to much session-scoped beans was a bad design idea anyway - on each new session, "tons" of EJB (JNDI lookup + business method invocation) will occur which will overrun the EJB container soon or later - also having JNDI lookup in PostConstruct-annotated methods was bad, too because it "confuses" the web container - while it is building proxy objects arround every managed class (like also now converters, JSF 2.3.x only) especially backing beans, there should be no JNDI lookup hapening - but initializing some lists for performance reasons is the right thing to do - maybe later some other annotation will be used instead, but for now it is fine - so let's make this painless and rename all session-scoped backing beans and re-annotate them with RequestScoped for now as this is also reduces memory foot-print Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/addressbook/beans/business/basicdata/AddressbookBusinessDataWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/business/basicdata/AddressbookBusinessDataWebRequestBean.java new file mode 100644 index 00000000..47c6a218 --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/business/basicdata/AddressbookBusinessDataWebRequestBean.java @@ -0,0 +1,386 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * 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.addressbook.beans.business.basicdata; + +import java.text.MessageFormat; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import javax.annotation.PostConstruct; +import javax.ejb.EJB; +import javax.enterprise.context.RequestScoped; +import javax.enterprise.event.Observes; +import javax.inject.Inject; +import javax.inject.Named; +import org.mxchange.addressbook.beans.BaseAddressbookController; +import org.mxchange.addressbook.beans.user.login.AddressbookUserLoginWebSessionController; +import org.mxchange.jcontactsbusiness.events.basicdata.added.ObservableAdminAddedBusinessBasicDataEvent; +import org.mxchange.jcontactsbusiness.model.basicdata.AdminBusinessDataSessionBeanRemote; +import org.mxchange.jcontactsbusiness.model.basicdata.BusinessBasicData; +import org.mxchange.jcontactsbusiness.model.basicdata.BusinessDataSessionBeanRemote; +import org.mxchange.jcountry.model.data.Country; + +/** + * A business contact bean (controller) + *

+ * @author Roland Häder + */ +@Named ("companyDataController") +@RequestScoped +public class AddressbookBusinessDataWebRequestBean extends BaseAddressbookController implements AddressbookBusinessDataWebRequestController { + + /** + * Serial number + */ + private static final long serialVersionUID = 56_189_028_928_371L; + + /** + * EJB for administrative basic business data purposes + */ + @EJB (lookup = "java:global/addressbook-ejb/adminBusinessData!org.mxchange.jcontactsbusiness.basicdata.AdminBusinessDataSessionBeanRemote", description = "A stateless session bean for administrative purposes.") + private AdminBusinessDataSessionBeanRemote adminBusinessDataBean; + + /** + * A list of all registered companies (globally) + */ + @Inject + @Cached (cacheName = "basicDataCache") + private transient Cache basicDataCache; + + /** + * EJB for general basic business data purposes + */ + @EJB (lookup = "java:global/addressbook-ejb/businessData!org.mxchange.jcontactsbusiness.basicdata.BusinessDataSessionBeanRemote", description = "A stateless session bean for general purposes.") + private BusinessDataSessionBeanRemote businessDataBean; + + /** + * Comments for this company + */ + private String companyComments; + + /** + * Companies (main) email address (example: info@company.example) + */ + private String companyEmailAddress; + + /** + * Company cacheName + */ + private String companyName; + + /** + * Area code for fax number + */ + private Integer faxAreaCode; + + /** + * Country for fax number + */ + private Country faxCountry; + + /** + * Dial number for fax number + */ + private Long faxNumber; + + /** + * Area code for land-line number + */ + private Integer landLineAreaCode; + + /** + * Country for land-line number + */ + private Country landLineCountry; + + /** + * Dial number for land-line number + */ + private Long landLineNumber; + + /** + * User instance + */ + @Inject + private AddressbookUserLoginWebSessionController userLoginController; + + /** + * Constructor + */ + public AddressbookBusinessDataWebRequestBean () { + // Call super constructor + super(); + } + + /** + * Observers events being fired when an administrator has added company + * basic data. + *

+ * @param event Event being fired + */ + public void afterAdminAddedBasicCompanyDataEvent (@Observes final ObservableAdminAddedBusinessBasicDataEvent event) { + // Is the parameter valid? + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); + } else if (event.getBasicData() == null) { + // Throw NPE again + throw new NullPointerException("event.basicData is null"); + } else if (event.getBasicData().getBasicDataId() == null) { + // Throw NPE again + throw new NullPointerException("event.basicData.companyDataId is null"); + } else if (event.getBasicData().getBasicDataId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("event.basicData.companyDataId={0} is invalid", event.getBasicData().getBasicDataId())); + } else if (event.getBasicData().getCompanyName() == null) { + // Throw NPE again + throw new NullPointerException("event.basicData.companyName is null"); + } else if (event.getBasicData().getCompanyName().isEmpty()) { + // Throw IAE again + throw new IllegalArgumentException("event.basicData.companyName is empty"); + } + + // Add it to list + this.basicDataCache.put(event.getBasicData().getBasicDataId(), event.getBasicData()); + } + + /** + * Returns a list of all business contacts + *

+ * @return A list of all business contacts + */ + @SuppressWarnings ("ReturnOfCollectionOrArrayField") + public List allCompanyBasicData () { + // Init list + List list = new LinkedList<>(); + + // Get iterator + Iterator> iterator = this.basicDataCache.iterator(); + + // Loop over all + while (iterator.hasNext()) { + // Get next entry + final Cache.Entry next = iterator.next(); + + // Add value to list + list.add(next.getValue()); + } + + // Return it + return list; + } + + /** + * Getter for comments + *

+ * @return Comments + */ + public String getCompanyComments () { + return this.companyComments; + } + + /** + * Setter for comments + *

+ * @param companyComments Comments + */ + public void setCompanyComments (final String companyComments) { + this.companyComments = companyComments; + } + + /** + * Getter for company's (main) email address + *

+ * @return Company's (main) email address + */ + public String getCompanyEmailAddress () { + return this.companyEmailAddress; + } + + /** + * Setter for company's (main) email address + *

+ * @param companyEmailAddress Company's (main) email address + */ + public void setCompanyEmailAddress (final String companyEmailAddress) { + this.companyEmailAddress = companyEmailAddress; + } + + /** + * Getter for company cacheName + *

+ * @return Company cacheName + */ + public String getCompanyName () { + return this.companyName; + } + + /** + * Setter for company cacheName + *

+ * @param companyName Company cacheName + */ + public void setCompanyName (final String companyName) { + this.companyName = companyName; + } + + /** + * Getter for fax number's area code + *

+ * @return Fax number's area code + */ + public Integer getFaxAreaCode () { + return this.faxAreaCode; + } + + /** + * Setter for fax number's area code + *

+ * @param faxAreaCode Fax number's area code + */ + public void setFaxAreaCode (final Integer faxAreaCode) { + this.faxAreaCode = faxAreaCode; + } + + /** + * Getter for fax's country instance + *

+ * @return Fax' country instance + */ + public Country getFaxCountry () { + return this.faxCountry; + } + + /** + * Setter for fax's country instance + *

+ * @param faxCountry Fax' country instance + */ + public void setFaxCountry (final Country faxCountry) { + this.faxCountry = faxCountry; + } + + /** + * Getter for fax number + *

+ * @return Fax number + */ + public Long getFaxNumber () { + return this.faxNumber; + } + + /** + * Setter for fax number + *

+ * @param faxNumber Fax number + */ + public void setFaxNumber (final Long faxNumber) { + this.faxNumber = faxNumber; + } + + /** + * Getter for land-line number's area code + *

+ * @return Land-line number's area code + */ + public Integer getLandLineAreaCode () { + return this.landLineAreaCode; + } + + /** + * Setter for land-line number's area code + *

+ * @param landLineAreaCode Land-line number's area code + */ + public void setLandLineAreaCode (final Integer landLineAreaCode) { + this.landLineAreaCode = landLineAreaCode; + } + + /** + * Getter for land-line number's country instance + *

+ * @return Land-line number's country instance + */ + public Country getLandLineCountry () { + return this.landLineCountry; + } + + /** + * Setter for land-line number's country instance + *

+ * @param landLineCountry Land-line number's country instance + */ + public void setLandLineCountry (final Country landLineCountry) { + this.landLineCountry = landLineCountry; + } + + /** + * Getter for land-line number + *

+ * @return Land-line number + */ + public Long getLandLineNumber () { + return this.landLineNumber; + } + + /** + * Setter for land-line number + *

+ * @param landLineNumber Land-line number + */ + public void setLandLineNumber (final Long landLineNumber) { + this.landLineNumber = landLineNumber; + } + + /** + * Initializer method + */ + @PostConstruct + public void initializeList () { + // Is cache there? + if (!this.basicDataCache.iterator().hasNext()) { + // Get whole list + List list = this.businessDataBean.allCompanyBasicData(); + + // Add all + for (final Iterator iterator = list.iterator(); iterator.hasNext();) { + // Get next element + final BusinessBasicData next = iterator.next(); + + // Add it to cache + this.basicDataCache.put(next.getBasicDataId(), next); + } + } + } + + /** + * Clears this bean + */ + private void clear () { + // Clear all data: + this.setCompanyComments(null); + this.setCompanyEmailAddress(null); + this.setCompanyName(null); + this.setFaxAreaCode(null); + this.setFaxCountry(null); + this.setFaxNumber(null); + this.setLandLineAreaCode(null); + this.setLandLineCountry(null); + this.setLandLineNumber(null); + } + +} diff --git a/src/java/org/mxchange/addressbook/beans/business/basicdata/AddressbookBusinessDataWebRequestController.java b/src/java/org/mxchange/addressbook/beans/business/basicdata/AddressbookBusinessDataWebRequestController.java new file mode 100644 index 00000000..44dd6a82 --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/business/basicdata/AddressbookBusinessDataWebRequestController.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2017 Roland Häder + * + * 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.addressbook.beans.business.basicdata; + +import java.io.Serializable; + +/** + * An interface for session-scoped financial controller + *

+ * @author Roland Häder + */ +public interface AddressbookBusinessDataWebRequestController extends Serializable { + +} diff --git a/src/java/org/mxchange/addressbook/beans/business/basicdata/AddressbookBusinessDataWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/business/basicdata/AddressbookBusinessDataWebSessionBean.java deleted file mode 100644 index 4be303ac..00000000 --- a/src/java/org/mxchange/addressbook/beans/business/basicdata/AddressbookBusinessDataWebSessionBean.java +++ /dev/null @@ -1,343 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * 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.addressbook.beans.business.basicdata; - -import java.text.MessageFormat; -import java.util.LinkedList; -import java.util.List; -import javax.annotation.PostConstruct; -import javax.ejb.EJB; -import javax.enterprise.context.SessionScoped; -import javax.enterprise.event.Observes; -import javax.inject.Inject; -import javax.inject.Named; -import org.mxchange.addressbook.beans.BaseAddressbookController; -import org.mxchange.addressbook.beans.user.login.AddressbookUserLoginWebSessionController; -import org.mxchange.jcontactsbusiness.events.basicdata.added.ObservableAdminAddedBusinessBasicDataEvent; -import org.mxchange.jcontactsbusiness.model.basicdata.AdminBusinessDataSessionBeanRemote; -import org.mxchange.jcontactsbusiness.model.basicdata.BusinessBasicData; -import org.mxchange.jcontactsbusiness.model.basicdata.BusinessDataSessionBeanRemote; -import org.mxchange.jcountry.model.data.Country; - -/** - * A business contact bean (controller) - *

- * @author Roland Häder - */ -@Named ("companyDataController") -@SessionScoped -public class AddressbookBusinessDataWebSessionBean extends BaseAddressbookController implements AddressbookBusinessDataWebSessionController { - - /** - * Serial number - */ - private static final long serialVersionUID = 56_189_028_928_371L; - - /** - * EJB for administrative basic business data purposes - */ - @EJB (lookup = "java:global/addressbook-ejb/adminBusinessData!org.mxchange.jcontactsbusiness.basicdata.AdminBusinessDataSessionBeanRemote", description = "A stateless session bean for administrative purposes.") - private AdminBusinessDataSessionBeanRemote adminBusinessDataBean; - - /** - * A list of all registered companies (globally) - */ - private final List businessContacts; - - /** - * EJB for general basic business data purposes - */ - @EJB (lookup = "java:global/addressbook-ejb/businessData!org.mxchange.jcontactsbusiness.basicdata.BusinessDataSessionBeanRemote", description = "A stateless session bean for general purposes.") - private BusinessDataSessionBeanRemote businessDataBean; - - /** - * Comments for this company - */ - private String companyComments; - - /** - * Companies (main) email address (example: info@company.example) - */ - private String companyEmailAddress; - - /** - * Company name - */ - private String companyName; - - /** - * Area code for fax number - */ - private Integer faxAreaCode; - - /** - * Country for fax number - */ - private Country faxCountry; - - /** - * Dial number for fax number - */ - private Long faxNumber; - - /** - * Area code for land-line number - */ - private Integer landLineAreaCode; - - /** - * Country for land-line number - */ - private Country landLineCountry; - - /** - * Dial number for land-line number - */ - private Long landLineNumber; - - /** - * User instance - */ - @Inject - private AddressbookUserLoginWebSessionController userLoginController; - - /** - * Constructor - */ - public AddressbookBusinessDataWebSessionBean () { - // Call super constructor - super(); - - // Init list - this.businessContacts = new LinkedList<>(); - } - - /** - * Observers events being fired when an administrator has added company - * basic data. - *

- * @param event Event being fired - */ - public void afterAdminAddedBasicCompanyDataEvent (@Observes final ObservableAdminAddedBusinessBasicDataEvent event) { - // Is the parameter valid? - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); - } else if (event.getBasicData() == null) { - // Throw NPE again - throw new NullPointerException("event.basicData is null"); - } else if (event.getBasicData().getBasicDataId() == null) { - // Throw NPE again - throw new NullPointerException("event.basicData.basicDataId is null"); - } else if (event.getBasicData().getBasicDataId() < 1) { - // Throw IAE - throw new IllegalArgumentException(MessageFormat.format("event.basicData.basicDataId={0} is invalid", event.getBasicData().getBasicDataId())); - } else if (event.getBasicData().getCompanyName() == null) { - // Throw NPE again - throw new NullPointerException("event.basicData.companyName is null"); - } else if (event.getBasicData().getCompanyName().isEmpty()) { - // Throw IAE again - throw new IllegalArgumentException("event.basicData.companyName is empty"); - } - - // Add it to list - this.businessContacts.add(event.getBasicData()); - } - - /** - * Returns a list of all business contacts - *

- * @return A list of all business contacts - */ - @SuppressWarnings ("ReturnOfCollectionOrArrayField") - public List allCompanyBasicData () { - // Return it - return this.businessContacts; - } - - /** - * Getter for comments - *

- * @return Comments - */ - public String getCompanyComments () { - return this.companyComments; - } - - /** - * Setter for comments - *

- * @param companyComments Comments - */ - public void setCompanyComments (final String companyComments) { - this.companyComments = companyComments; - } - - /** - * Getter for company's (main) email address - *

- * @return Company's (main) email address - */ - public String getCompanyEmailAddress () { - return this.companyEmailAddress; - } - - /** - * Setter for company's (main) email address - *

- * @param companyEmailAddress Company's (main) email address - */ - public void setCompanyEmailAddress (final String companyEmailAddress) { - this.companyEmailAddress = companyEmailAddress; - } - - /** - * Getter for company name - *

- * @return Company name - */ - public String getCompanyName () { - return this.companyName; - } - - /** - * Setter for company name - *

- * @param companyName Company name - */ - public void setCompanyName (final String companyName) { - this.companyName = companyName; - } - - /** - * Getter for fax number's area code - *

- * @return Fax number's area code - */ - public Integer getFaxAreaCode () { - return this.faxAreaCode; - } - - /** - * Setter for fax number's area code - *

- * @param faxAreaCode Fax number's area code - */ - public void setFaxAreaCode (final Integer faxAreaCode) { - this.faxAreaCode = faxAreaCode; - } - - /** - * Getter for fax's country instance - *

- * @return Fax' country instance - */ - public Country getFaxCountry () { - return this.faxCountry; - } - - /** - * Setter for fax's country instance - *

- * @param faxCountry Fax' country instance - */ - public void setFaxCountry (final Country faxCountry) { - this.faxCountry = faxCountry; - } - - /** - * Getter for fax number - *

- * @return Fax number - */ - public Long getFaxNumber () { - return this.faxNumber; - } - - /** - * Setter for fax number - *

- * @param faxNumber Fax number - */ - public void setFaxNumber (final Long faxNumber) { - this.faxNumber = faxNumber; - } - - /** - * Getter for land-line number's area code - *

- * @return Land-line number's area code - */ - public Integer getLandLineAreaCode () { - return this.landLineAreaCode; - } - - /** - * Setter for land-line number's area code - *

- * @param landLineAreaCode Land-line number's area code - */ - public void setLandLineAreaCode (final Integer landLineAreaCode) { - this.landLineAreaCode = landLineAreaCode; - } - - /** - * Getter for land-line number's country instance - *

- * @return Land-line number's country instance - */ - public Country getLandLineCountry () { - return this.landLineCountry; - } - - /** - * Setter for land-line number's country instance - *

- * @param landLineCountry Land-line number's country instance - */ - public void setLandLineCountry (final Country landLineCountry) { - this.landLineCountry = landLineCountry; - } - - /** - * Getter for land-line number - *

- * @return Land-line number - */ - public Long getLandLineNumber () { - return this.landLineNumber; - } - - /** - * Setter for land-line number - *

- * @param landLineNumber Land-line number - */ - public void setLandLineNumber (final Long landLineNumber) { - this.landLineNumber = landLineNumber; - } - - /** - * Initializer method - */ - @PostConstruct - public void initializeList () { - // Init user's contact list - this.businessContacts.addAll(this.adminBusinessDataBean.allCompanyBasicData()); - } - -} diff --git a/src/java/org/mxchange/addressbook/beans/business/basicdata/AddressbookBusinessDataWebSessionController.java b/src/java/org/mxchange/addressbook/beans/business/basicdata/AddressbookBusinessDataWebSessionController.java deleted file mode 100644 index ca46bea6..00000000 --- a/src/java/org/mxchange/addressbook/beans/business/basicdata/AddressbookBusinessDataWebSessionController.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) 2017 Roland Häder - * - * 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.addressbook.beans.business.basicdata; - -import java.io.Serializable; - -/** - * An interface for session-scoped financial controller - *

- * @author Roland Häder - */ -public interface AddressbookBusinessDataWebSessionController extends Serializable { - -} diff --git a/src/java/org/mxchange/addressbook/beans/business/employee/AddressbookCompanyEmployeeWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/business/employee/AddressbookCompanyEmployeeWebRequestBean.java index af67cbf0..c33698d2 100644 --- a/src/java/org/mxchange/addressbook/beans/business/employee/AddressbookCompanyEmployeeWebRequestBean.java +++ b/src/java/org/mxchange/addressbook/beans/business/employee/AddressbookCompanyEmployeeWebRequestBean.java @@ -16,15 +16,18 @@ */ package org.mxchange.addressbook.beans.business.employee; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; import javax.annotation.PostConstruct; +import javax.cache.Cache; import javax.ejb.EJB; -import javax.enterprise.context.SessionScoped; +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; import javax.inject.Named; -import org.mxchange.addressbook.beans.BaseAddressbookController; import org.mxchange.jcontactsbusiness.model.employee.CompanyEmployeeSessionBeanRemote; import org.mxchange.jcontactsbusiness.model.employee.Employee; +import org.mxchange.addressbook.beans.BaseAddressbookController; /** * A request-scoped bean for general purposes for company employees. @@ -32,8 +35,8 @@ import org.mxchange.jcontactsbusiness.model.employee.Employee; * @author Roland Häder */ @Named ("companyEmployeeController") -@SessionScoped -public class AddressbookCompanyEmployeeWebRequestBean extends BaseAddressbookController implements AddressbookCompanyEmployeeWebSessionController { +@RequestScoped +public class AddressbookCompanyEmployeeWebRequestBean extends BaseAddressbookController implements AddressbookCompanyEmployeeWebRequestController { /** * Serial number @@ -49,7 +52,9 @@ public class AddressbookCompanyEmployeeWebRequestBean extends BaseAddressbookCon /** * List of all company employees */ - private final List companyEmployees; + @Inject + @Cached(cacheName = "companyEmployeeCache") + private transient Cache companyEmployeeCache; /** * Default constructor @@ -57,9 +62,6 @@ public class AddressbookCompanyEmployeeWebRequestBean extends BaseAddressbookCon public AddressbookCompanyEmployeeWebRequestBean () { // Call super constructor super(); - - // Init list instance - this.companyEmployees = new LinkedList<>(); } /** @@ -69,7 +71,23 @@ public class AddressbookCompanyEmployeeWebRequestBean extends BaseAddressbookCon */ @SuppressWarnings ("ReturnOfCollectionOrArrayField") public List allCompanyEmployees () { - return this.companyEmployees; + // Init list + List list = new LinkedList<>(); + + // Get iterator + Iterator> iterator = this.companyEmployeeCache.iterator(); + + // Loop over all + while (iterator.hasNext()) { + // Get next entry + final Cache.Entry next = iterator.next(); + + // Add value to list + list.add(next.getValue()); + } + + // Return it + return list; } /** @@ -77,11 +95,20 @@ public class AddressbookCompanyEmployeeWebRequestBean extends BaseAddressbookCon */ @PostConstruct public void init () { - // Get all entries from remote bean - List employees = this.companyEmployeeBean.allCompanyEmployees(); + // Is cache there? + if (!this.companyEmployeeCache.iterator().hasNext()) { + // Get whole list + List list = this.companyEmployeeBean.allCompanyEmployees(); + + // Add all + for (final Iterator iterator = list.iterator(); iterator.hasNext();) { + // Get next element + final Employee next = iterator.next(); - // Copy it to main list - this.companyEmployees.addAll(employees); + // Add it to cache + this.companyEmployeeCache.put(next.getEmployeeId(), next); + } + } } } diff --git a/src/java/org/mxchange/addressbook/beans/business/employee/AddressbookCompanyEmployeeWebRequestController.java b/src/java/org/mxchange/addressbook/beans/business/employee/AddressbookCompanyEmployeeWebRequestController.java new file mode 100644 index 00000000..9d7708fb --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/business/employee/AddressbookCompanyEmployeeWebRequestController.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2017 Roland Häder + * + * 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.addressbook.beans.business.employee; + +import java.io.Serializable; + +/** + * An interface for request-scoped general company employee beans + *

+ * @author Roland Häder + */ +public interface AddressbookCompanyEmployeeWebRequestController extends Serializable { + +} diff --git a/src/java/org/mxchange/addressbook/beans/business/employee/AddressbookCompanyEmployeeWebSessionController.java b/src/java/org/mxchange/addressbook/beans/business/employee/AddressbookCompanyEmployeeWebSessionController.java deleted file mode 100644 index 25606668..00000000 --- a/src/java/org/mxchange/addressbook/beans/business/employee/AddressbookCompanyEmployeeWebSessionController.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) 2017 Roland Häder - * - * 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.addressbook.beans.business.employee; - -import java.io.Serializable; - -/** - * An interface for request-scoped general company employee beans - *

- * @author Roland Häder - */ -public interface AddressbookCompanyEmployeeWebSessionController extends Serializable { - -} diff --git a/src/java/org/mxchange/addressbook/beans/contact/AddressbookAdminContactWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/contact/AddressbookAdminContactWebRequestBean.java index 24b6c4ab..ebd57ab7 100644 --- a/src/java/org/mxchange/addressbook/beans/contact/AddressbookAdminContactWebRequestBean.java +++ b/src/java/org/mxchange/addressbook/beans/contact/AddressbookAdminContactWebRequestBean.java @@ -107,7 +107,7 @@ public class AddressbookAdminContactWebRequestBean extends BaseAddressbookContro * General contact controller */ @Inject - private AddressbookContactWebSessionController contactController; + private AddressbookContactWebRequestController contactController; /** * Country instance diff --git a/src/java/org/mxchange/addressbook/beans/contact/AddressbookContactWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/contact/AddressbookContactWebRequestBean.java new file mode 100644 index 00000000..59598088 --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/contact/AddressbookContactWebRequestBean.java @@ -0,0 +1,1327 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * 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.addressbook.beans.contact; + +import java.text.MessageFormat; +import java.util.Collections; +import java.util.Date; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; +import javax.annotation.PostConstruct; +import javax.ejb.EJB; +import javax.enterprise.context.RequestScoped; +import javax.enterprise.event.Observes; +import javax.faces.view.facelets.FaceletException; +import javax.inject.Inject; +import javax.inject.Named; +import org.mxchange.addressbook.beans.BaseAddressbookController; +import org.mxchange.addressbook.beans.user.AddressbookUserWebRequestController; +import org.mxchange.addressbook.beans.user.login.AddressbookUserLoginWebSessionController; +import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent; +import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent; +import org.mxchange.jcontacts.model.contact.Contact; +import org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote; +import org.mxchange.jcontacts.model.contact.ContactUtils; +import org.mxchange.jcontacts.model.contact.UserContact; +import org.mxchange.jcontacts.model.contact.title.PersonalTitle; +import org.mxchange.jcountry.model.data.Country; +import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber; +import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber; +import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber; +import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber; +import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber; +import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumber; +import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider; +import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent; +import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent; +import org.mxchange.jusercore.model.user.User; +import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent; +import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent; +import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent; +import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException; + +/** + * A general contact bean (controller) + *

+ * @author Roland Häder + */ +@Named ("contactController") +@RequestScoped +public class AddressbookContactWebRequestBean extends BaseAddressbookController implements AddressbookContactWebRequestController { + + /** + * Serial number + */ + private static final long serialVersionUID = 542_145_347_916L; + + /** + * Academic academicTitle + */ + private String academicTitle; + + /** + * Birth day + */ + private Date birthday; + + /** + * City + */ + private String city; + + /** + * Optional comments + */ + private String comment; + + /** + * EJB for general contact purposes + */ + @EJB (lookup = "java:global/addressbook-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote") + private ContactSessionBeanRemote contactBean; + + /** + * Contact list + */ + @Inject + @Cached (cacheName = "contactsCache") + private transient Cache contactsCache; + + /** + * Country instance + */ + private Country country; + + /** + * Email address + */ + private String emailAddress; + + /** + * Email address list + */ + @Inject + @Cached (cacheName = "emailAddressCache") + private transient Cache emailAddressCache; + + /** + * Email address repeated + */ + private String emailAddressRepeat; + + /** + * Family name + */ + private String familyName; + + /** + * Fax number's area code + */ + private Integer faxAreaCode; + + /** + * Country instance for fax number + */ + private Country faxCountry; + + /** + * Fax number + */ + private Long faxNumber; + + /** + * First name + */ + private String firstName; + + /** + * House number + */ + private Short houseNumber; + + /** + * House number extension + */ + private String houseNumberExtension; + + /** + * Whether a fax entry has been unlinked + */ + private boolean isFaxUnlinked; + + /** + * Whether a land-line number has been unlinked + */ + private boolean isLandLineUnlinked; + + /** + * Whether a mobile entry has been unlinked + */ + private boolean isMobileUnlinked; + + /** + * Phone number area code + */ + private Integer landLineAreaCode; + + /** + * Country instance for phone number + */ + private Country landLineCountry; + + /** + * Phone number + */ + private Long landLineNumber; + + /** + * Mobile number + */ + private Long mobileNumber; + + /** + * Mobile number's carrier + */ + private MobileProvider mobileProvider; + + /** + * Personal academicTitle + */ + private PersonalTitle personalTitle; + + /** + * A list of all selectable contacts + */ + private List selectableContacts; + + /** + * Street + */ + private String street; + + /** + * Regular user controller + */ + @Inject + private AddressbookUserWebRequestController userController; + + /** + * Login bean (controller) + */ + @Inject + private AddressbookUserLoginWebSessionController userLoginController; + + /** + * ZIP code + */ + private Integer zipCode; + + /** + * Default constructor + */ + public AddressbookContactWebRequestBean () { + // Call super constructor + super(); + } + + /** + * Observes events being fired when an administrator has added a new + * contact. + *

+ * @param event Event being fired + */ + public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent 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(); + + // Call other method + this.uniqueAddContact(event.getAddedContact()); + + // Add to selectable contacts + this.selectableContacts.add(event.getAddedContact()); + } + + /** + * Event observer for newly added users by administrator + *

+ * @param event Event being fired + */ + public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent 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(); + } + + /** + * Observes events being fired when an administrator has linked a new user + * with existing contact data. + *

+ * @param event Event being fired + */ + public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getLinkedUser() == null) { + // Throw NPE again + throw new NullPointerException("event.linkedUser is null"); //NOI18N + } else if (event.getLinkedUser().getUserContact() == null) { + // Throw NPE again + throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N + } else if (event.getLinkedUser().getUserContact().getContactId() == null) { + // userId is null + throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N + } else if (event.getLinkedUser().getUserContact().getContactId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N + } + + // Remove contact from list available contacts list + this.selectableContacts.remove(event.getLinkedUser().getUserContact()); + + // Clear all data + this.clear(); + } + + /** + * Event observer for updated contact data by administrators + *

+ * @param event Updated contact data event + */ + public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent 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("userId of user={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N + } + + // Add contact instance only once + this.uniqueAddContact(event.getUpdatedContact()); + + // Add email address to list + this.emailAddressCache.put(event.getUpdatedContact().getContactId(), event.getUpdatedContact().getContactEmailAddress()); + } + + /** + * Event observer when user confirmed account. + *

+ * @param event Event being fired + */ + public void afterUserConfirmedAccount (@Observes final ObservableUserConfirmedAccountEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getConfirmedUser() == null) { + // Throw NPE again + throw new NullPointerException("event.confirmedUser is null"); //NOI18N + } else if (event.getConfirmedUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N + } else if (event.getConfirmedUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N + } + + // Add contact instance only once + this.uniqueAddContact(event.getConfirmedUser().getUserContact()); + } + + /** + * Event observer for logged-in user + *

+ * @param event Event instance + */ + public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getLoggedInUser() == null) { + // Throw NPE again + throw new NullPointerException("event.loggedInUser is null"); //NOI18N + } else if (event.getLoggedInUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N + } else if (event.getLoggedInUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N + } + + // Copy all data to this bean + this.copyContact(event.getLoggedInUser().getUserContact()); + } + + /** + * Event observer for new user registrations + *

+ * @param event User registration event + */ + public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getRegisteredUser() == null) { + // Throw NPE again + throw new NullPointerException("event.registeredUser is null"); //NOI18N + } else if (event.getRegisteredUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N + } else if (event.getRegisteredUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N + } + + // Get user instance + Contact registeredContact = event.getRegisteredUser().getUserContact(); + + // Copy all data from registered->user + this.copyContact(registeredContact); + + // Add contact instance only once + this.uniqueAddContact(registeredContact); + + // Add user name and email address + this.addUserNameEmailAddress(registeredContact); + + // Clear all data + this.clear(); + } + + @Override + @SuppressWarnings ("ReturnOfCollectionOrArrayField") + public List allContacts () { + // Init list + List list = new LinkedList<>(); + + // Get iterator + Iterator> iterator = this.contactsCache.iterator(); + + // Loop over all + while (iterator.hasNext()) { + // Get next entry + final Cache.Entry next = iterator.next(); + + // Add value to list + list.add(next.getValue()); + } + + // Return it + return list; + } + + @Override + public void clearEmailAddresses () { + // Clear both + this.setEmailAddress(null); + this.setEmailAddressRepeat(null); + } + + @Override + public Contact createContactInstance () { + // Is all required data set? + if (!this.isRequiredPersonalDataSet()) { + // No, then abort here + throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N + } + + // Required personal data must be set + assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N + + // Generate phone number + DialableLandLineNumber phone = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber()); + DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber()); + DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber()); + + // Create new contact + Contact contact = new UserContact(this.getPersonalTitle(), this.getFirstName(), this.getFamilyName()); + contact.setContactStreet(this.getStreet()); + contact.setContactHouseNumber(this.getHouseNumber()); + contact.setContactHouseNumberExtension(this.getHouseNumberExtension()); + contact.setContactZipCode(this.getZipCode()); + contact.setContactCity(this.getCity()); + contact.setContactCountry(this.getCountry()); + contact.setContactEmailAddress(this.getEmailAddress()); + contact.setContactBirthday(this.getBirthday()); + contact.setContactComment(this.getComment()); + + // Don't set null or wrong references + if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) { + // Now the number must be given + if (phone.getPhoneAreaCode() == null) { + // Is null + throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N + } else if (phone.getPhoneAreaCode() < 1) { + // Abort here + throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N + } else if (phone.getPhoneNumber() == null) { + // Is null + throw new NullPointerException("phone.phoneNumber is null"); //NOI18N + } else if (phone.getPhoneNumber() < 1) { + // Abort here + throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N + } + + // Set phone number + contact.setContactLandLineNumber(phone); + } + + // Don't set null or wrong references + if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) { + // Now the number must be given + if (fax.getPhoneAreaCode() == null) { + // Is null + throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N + } else if (fax.getPhoneAreaCode() < 1) { + // Abort here + throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N + } else if (fax.getPhoneNumber() == null) { + // Is null + throw new NullPointerException("fax.phoneNumber is null"); //NOI18N + } else if (fax.getPhoneNumber() < 1) { + // Abort here + throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N + } + + // Set fax number + contact.setContactFaxNumber(fax); + } + + // Is the provider set? + if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) { + // Is the number set? + if (mobile.getPhoneNumber() == null) { + // Is null + throw new NullPointerException("mobile.phoneNumber is null"); //NOI18N + } else if (mobile.getPhoneNumber() < 1) { + // Abort here + throw new IllegalArgumentException("mobile.phoneNumber is zero or below."); //NOI18N + } + + // Set mobile number + contact.setContactMobileNumber(mobile); + } + + // Return it + return contact; + } + + @Override + public String doChangePersonalContactData () { + // This method shall only be called if the user is logged-in + if (!this.userLoginController.isUserLoggedIn()) { + // Not logged-in + throw new IllegalStateException("User is not logged-in"); //NOI18N + } else if (!this.isRequiredChangePersonalDataSet()) { + // Not all required fields are set + throw new FaceletException("Not all required fields are set."); //NOI18N + } else if (!this.userLoginController.ifCurrentPasswordMatches()) { + // Password not matching + this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N + return ""; //NOI18N + } + + // Get contact instance + Contact contact = this.userLoginController.getLoggedInUser().getUserContact(); + + // It should be there, so run some tests on it + assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N + assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N + assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N + + // Update all fields + contact.setContactPersonalTitle(this.getPersonalTitle()); + contact.setContactFirstName(this.getFirstName()); + contact.setContactFamilyName(this.getFamilyName()); + contact.setContactStreet(this.getStreet()); + contact.setContactHouseNumber(this.getHouseNumber()); + contact.setContactHouseNumberExtension(this.getHouseNumberExtension()); + contact.setContactZipCode(this.getZipCode()); + contact.setContactCity(this.getCity()); + contact.setContactCountry(this.getCountry()); + + // Update contact's mobile number + this.isMobileUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber()); + + // Update contact's land-line number + this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber()); + + // Update contact's fax number + this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber()); + + // Send it to the EJB + this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked); + + // All fine + return "contact_data_saved"; //NOI18N + } + + /** + * Getter for academic title + *

+ * @return Academic title + */ + public String getAcademicTitle () { + return this.academicTitle; + } + + /** + * Setter for academic title + *

+ * @param academicTitle Academic title + */ + public void setAcademicTitle (final String academicTitle) { + this.academicTitle = academicTitle; + } + + /** + * Getter for birth day + *

+ * @return Birth day + */ + @SuppressWarnings ("ReturnOfDateField") + public Date getBirthday () { + return this.birthday; + } + + /** + * Setter for birth day + *

+ * @param birthday Birth day + */ + @SuppressWarnings ("AssignmentToDateFieldFromParameter") + public void setBirthday (final Date birthday) { + this.birthday = birthday; + } + + /** + * Getter for city name + *

+ * @return City name + */ + public String getCity () { + return this.city; + } + + /** + * Setter for city name + *

+ * @param city City name + */ + public void setCity (final String city) { + this.city = city; + } + + /** + * Getter for comments + *

+ * @return Comments + */ + public String getComment () { + return this.comment; + } + + /** + * Setter for comment + *

+ * @param comment Comments + */ + public void setComment (final String comment) { + this.comment = comment; + } + + @Override + public String getControllerType () { + return "general"; //NOI18N + } + + @Override + @Deprecated + public void setControllerType (final String controllerType) { + throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N + } + + /** + * Getter for country instance + *

+ * @return Country instance + */ + public Country getCountry () { + return this.country; + } + + /** + * Setter for country instance + *

+ * @param country Country instance + */ + public void setCountry (final Country country) { + this.country = country; + } + + @Override + public String getEmailAddress () { + return this.emailAddress; + } + + /** + * Setter for email address + *

+ * @param emailAddress Email address + */ + public void setEmailAddress (final String emailAddress) { + this.emailAddress = emailAddress; + } + + /** + * Getter for email address, repeated + *

+ * @return the emailAddress, repeated + */ + public String getEmailAddressRepeat () { + return this.emailAddressRepeat; + } + + /** + * Setter for email address repeated + *

+ * @param emailAddressRepeat the emailAddress to set + */ + public void setEmailAddressRepeat (final String emailAddressRepeat) { + this.emailAddressRepeat = emailAddressRepeat; + } + + /** + * Family name + *

+ * @return the familyName + */ + public String getFamilyName () { + return this.familyName; + } + + /** + * Family name + *

+ * @param familyName the familyName to set + */ + public void setFamilyName (final String familyName) { + this.familyName = familyName; + } + + /** + * Getter for fax number's area code + *

+ * @return Fax number's area code + */ + public Integer getFaxAreaCode () { + return this.faxAreaCode; + } + + /** + * Setter for fax number's area code + *

+ * @param faxAreaCode Fax number's area code + */ + public void setFaxAreaCode (final Integer faxAreaCode) { + this.faxAreaCode = faxAreaCode; + } + + /** + * Getter for fax's country instance + *

+ * @return Fax' country instance + */ + public Country getFaxCountry () { + return this.faxCountry; + } + + /** + * Setter for fax's country instance + *

+ * @param faxCountry Fax' country instance + */ + public void setFaxCountry (final Country faxCountry) { + this.faxCountry = faxCountry; + } + + /** + * Getter for fax number + *

+ * @return Fax number + */ + public Long getFaxNumber () { + return this.faxNumber; + } + + /** + * Setter for fax number + *

+ * @param faxNumber Fax number + */ + public void setFaxNumber (final Long faxNumber) { + this.faxNumber = faxNumber; + } + + /** + * First name + *

+ * @return the first name + */ + public String getFirstName () { + return this.firstName; + } + + /** + * First name + *

+ * @param firstName the first name to set + */ + public void setFirstName (final String firstName) { + this.firstName = firstName; + } + + /** + * House number + *

+ * @return the houseNumber + */ + public Short getHouseNumber () { + return this.houseNumber; + } + + /** + * House number + *

+ * @param houseNumber the houseNumber to set + */ + public void setHouseNumber (final Short houseNumber) { + this.houseNumber = houseNumber; + } + + /** + * Getter for house number extension, example: 123a 'a' is then the + * extension and 123 is the house number. + *

+ * @return House number extension + */ + public String getHouseNumberExtension () { + return this.houseNumberExtension; + } + + /** + * Setter for house number extension + *

+ * @param houseNumberExtension House number extension + */ + public void setHouseNumberExtension (final String houseNumberExtension) { + this.houseNumberExtension = houseNumberExtension; + } + + /** + * Getter for land-line number's area code + *

+ * @return Land-line number's area code + */ + public Integer getLandLineAreaCode () { + return this.landLineAreaCode; + } + + /** + * Setter for land-line number's area code + *

+ * @param landLineAreaCode Land-line number's area code + */ + public void setLandLineAreaCode (final Integer landLineAreaCode) { + this.landLineAreaCode = landLineAreaCode; + } + + /** + * Getter for land-line number's country instance + *

+ * @return Land-line number's country instance + */ + public Country getLandLineCountry () { + return this.landLineCountry; + } + + /** + * Setter for land-line number's country instance + *

+ * @param landLineCountry Land-line number's country instance + */ + public void setLandLineCountry (final Country landLineCountry) { + this.landLineCountry = landLineCountry; + } + + /** + * Getter for land-line number + *

+ * @return Land-line number + */ + public Long getLandLineNumber () { + return this.landLineNumber; + } + + /** + * Setter for land-line number + *

+ * @param landLineNumber Land-line number + */ + public void setLandLineNumber (final Long landLineNumber) { + this.landLineNumber = landLineNumber; + } + + /** + * Getter for mobile number + *

+ * @return Mobile number + */ + public Long getMobileNumber () { + return this.mobileNumber; + } + + /** + * Setter for mobile number + *

+ * @param mobileNumber Mobile number + */ + public void setMobileNumber (final Long mobileNumber) { + this.mobileNumber = mobileNumber; + } + + /** + * Getter for mobile number's carrier + *

+ * @return Mobile number's carrier + */ + public MobileProvider getMobileProvider () { + return this.mobileProvider; + } + + /** + * Setter for mobile number's provider + *

+ * @param mobileProvider Mobile number's provider + */ + public void setMobileProvider (final MobileProvider mobileProvider) { + this.mobileProvider = mobileProvider; + } + + /** + * Getter for personal title + *

+ * @return Personal title + */ + public PersonalTitle getPersonalTitle () { + return this.personalTitle; + } + + /** + * Setter for personal title + *

+ * @param personalTitle Personal title + */ + public void setPersonalTitle (final PersonalTitle personalTitle) { + this.personalTitle = personalTitle; + } + + /** + * Getter for street + *

+ * @return Street + */ + public String getStreet () { + return this.street; + } + + /** + * Setter for street + *

+ * @param street Street + */ + public void setStreet (final String street) { + this.street = street; + } + + /** + * Getter for ZIP code + *

+ * @return ZIP code + */ + public Integer getZipCode () { + return this.zipCode; + } + + /** + * Setter for ZIP code + *

+ * @param zipCode ZIP code + */ + public void setZipCode (final Integer zipCode) { + this.zipCode = zipCode; + } + + /** + * Post-construction method + */ + @PostConstruct + public void init () { + // Get all contacts + final List selectable = new LinkedList<>(); + + // Is cache there? + if (!this.contactsCache.iterator().hasNext()) { + // Get whole list + List list = this.contactBean.allContacts(); + + // Add all + for (final Iterator iterator = list.iterator(); iterator.hasNext();) { + // Get next element + final Contact next = iterator.next(); + + // Add it to cache + this.contactsCache.put(next.getContactId(), next); + this.emailAddressCache.put(next.getContactId(), next.getContactEmailAddress()); + selectable.add(next); + } + } + + // Get all users + List allUsers = this.userController.allUsers(); + + // Get iterator + Iterator iterator = selectable.iterator(); + + // Loop through it + while (iterator.hasNext()) { + // Get next element + Contact next = iterator.next(); + + // Get iterator + Iterator userIterator = allUsers.iterator(); + + // Loop through all users + while (userIterator.hasNext()) { + // Get user instance + User nextUser = userIterator.next(); + + // Is contact same? + if (Objects.equals(next, nextUser.getUserContact())) { + // Found same + iterator.remove(); + break; + } + } + } + + // Set contact list + this.selectableContacts = selectable; + } + + @Override + public boolean isEmailAddressRegistered (final Contact contact) { + // Cherck parameter + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactEmailAddress() == null) { + // Throw again + throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N + } else if (contact.getContactEmailAddress().isEmpty()) { + // Is empty + throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N + } + + // Determine it + return ((this.emailAddressCache instanceof List) && (this.emailAddressCache.containsKey(contact.getContactId()))); + } + + @Override + public boolean isRequiredChangePersonalDataSet () { + return ((this.getPersonalTitle() != null) && + (this.getFirstName() != null) && + (this.getFamilyName() != null) && + (this.getStreet() != null) && + (this.getHouseNumber() != null) && + (this.getZipCode() != null) && + (this.getCity() != null)); + } + + @Override + public boolean isRequiredPersonalDataSet () { + return ((this.getPersonalTitle() != null) && + (this.getFirstName() != null) && + (this.getFamilyName() != null) && + (this.getStreet() != null) && + (this.getHouseNumber() != null) && + (this.getZipCode() != null) && + (this.getCity() != null) && + (this.getEmailAddress() != null) && + (this.getEmailAddressRepeat() != null)); + } + + @Override + public boolean isSameEmailAddressEntered () { + return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat())); + } + + /** + * Returns a list of all selectable contacts for user creation. Contacts + * from already existing users are excluded in this list. + *

+ * @return A list of all selectable contacts + */ + public List selectableContacts () { + return Collections.unmodifiableList(this.selectableContacts); + } + + @Override + public void updateContactDataFromController (final Contact contact) { + // Is the instance valid? + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // Throw NPE + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { + // Not valid id number + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N + } + + // Set all + this.copyContact(contact); + } + + /** + * Adds email address to bean's internal list. + *

+ * @param contact Contact instance + */ + private void addUserNameEmailAddress (final Contact contact) { + // Make sure the entry is not added yet + if (this.emailAddressCache.containsKey(contact.getContactId())) { + // Already added + throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N + } + + // Add email addres + this.emailAddressCache.put(contact.getContactId(), contact.getContactEmailAddress()); + } + + /** + * Clears this bean + */ + private void clear () { + // Clear all data + // - personal data + this.setPersonalTitle(null); + this.setAcademicTitle(null); + this.setFirstName(null); + this.setFamilyName(null); + this.setStreet(null); + this.setHouseNumber(null); + this.setHouseNumberExtension(null); + this.setZipCode(null); + this.setCity(null); + this.setCountry(null); + + // - contact data + this.clearEmailAddresses(); + this.setLandLineAreaCode(null); + this.setLandLineCountry(null); + this.setLandLineNumber(null); + this.setMobileProvider(null); + this.setMobileNumber(null); + this.setFaxAreaCode(null); + this.setFaxCountry(null); + this.setFaxNumber(null); + + // - other data + this.setBirthday(null); + this.setComment(null); + } + + /** + * Copies given contact into the controller + *

+ * @param contact Contact instance + */ + private void copyContact (final Contact contact) { + // Is the instance valid? + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // Throw NPE + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { + // Not valid id number + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N + } + + // Copy all fields: + // - base data + this.setPersonalTitle(contact.getContactPersonalTitle()); + this.setAcademicTitle(contact.getContactTitle()); + this.setFirstName(contact.getContactFirstName()); + this.setFamilyName(contact.getContactFamilyName()); + this.setStreet(contact.getContactStreet()); + this.setHouseNumber(contact.getContactHouseNumber()); + this.setHouseNumberExtension(contact.getContactHouseNumberExtension()); + this.setZipCode(contact.getContactZipCode()); + this.setCity(contact.getContactCity()); + this.setCountry(contact.getContactCountry()); + this.setEmailAddress(contact.getContactEmailAddress()); + this.setBirthday(contact.getContactBirthday()); + this.setComment(contact.getContactComment()); + + // Get mobile, phone and fax instance + DialableMobileNumber mobile = contact.getContactMobileNumber(); + DialableFaxNumber fax = contact.getContactFaxNumber(); + DialableLandLineNumber phone = contact.getContactLandLineNumber(); + + // - contact data + if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) { + this.setLandLineCountry(phone.getPhoneCountry()); + this.setLandLineAreaCode(phone.getPhoneAreaCode()); + this.setLandLineNumber(phone.getPhoneNumber()); + } + + if ((mobile instanceof DialableMobileNumber) && (mobile.getMobileProvider() instanceof MobileProvider)) { + this.setMobileProvider(mobile.getMobileProvider()); + this.setMobileNumber(mobile.getPhoneNumber()); + } + + if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) { + this.setFaxCountry(fax.getPhoneCountry()); + this.setFaxAreaCode(fax.getPhoneAreaCode()); + this.setFaxNumber(fax.getPhoneNumber()); + } + } + + /** + * Removes given contact from all lists + *

+ * @param contact Contact instance to remove + */ + private void removeContact (final Contact contact) { + // Is the instance valid? + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // Throw NPE + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { + // Not valid id number + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N + } + + // Remove from general list + if (!this.contactsCache.remove(contact.getContactId())) { + // Did not remove contact + throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N + } + + // Remove from other lists + this.emailAddressCache.remove(contact.getContactId()); + } + + /** + * Adds unique instance to contact list. First any existing instance is + * being removed, then the new instance is added. + *

+ * @param contact Contact instance to add uniquely + */ + private void uniqueAddContact (final Contact contact) { + // Is the instance valid? + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // Throw NPE + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { + // Not valid id number + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N + } + + // Get iterator from list + Iterator> iterator = this.contactsCache.iterator(); + + // "Walk" through all entries + while (iterator.hasNext()) { + // Get next element + Cache.Entry next = iterator.next(); + + // Is id number the same? + if (Objects.equals(contact.getContactId(), next.getKey())) { + // Found entry, so remove it and abort + this.removeContact(next.getValue()); + break; + } + } + + // Add contact to list + this.contactsCache.put(contact.getContactId(), contact); + } + +} diff --git a/src/java/org/mxchange/addressbook/beans/contact/AddressbookContactWebRequestController.java b/src/java/org/mxchange/addressbook/beans/contact/AddressbookContactWebRequestController.java new file mode 100644 index 00000000..69f54a5c --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/contact/AddressbookContactWebRequestController.java @@ -0,0 +1,123 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * 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.addressbook.beans.contact; + +import java.io.Serializable; +import java.util.List; +import org.mxchange.jcontacts.model.contact.Contact; + +/** + * An interface for user beans + *

+ * @author Roland Häder + */ +public interface AddressbookContactWebRequestController extends Serializable { + + /** + * Minimum password length + */ + public static final Integer MINIMUM_PASSWORD_LENGTH = 5; + + /** + * Getter for email address + *

+ * @return Email address + */ + String getEmailAddress (); + + /** + * Clears both email address field + */ + void clearEmailAddresses (); + + /** + * Returns a list of all found contacts + *

+ * @return A list of all contacts. + */ + List allContacts (); + + /** + * Updates all data from bean in given contact instance + *

+ * @param userContact Contact instance to update + */ + void updateContactDataFromController (final Contact userContact); + + /** + * Creates an instance from all properties + *

+ * @return A contact instance + */ + Contact createContactInstance (); + + /** + * Getter for controller type + *

+ * @return controller type + */ + String getControllerType (); + + /** + * Setter for controller type + *

+ * @param controllerType Controller type + *

+ * @deprecated Don't use this method. + */ + @Deprecated + void setControllerType (final String controllerType); + + /** + * Checks whether contact instance's email address is used + *

+ * @param contact Contact instance's email address to check + *

+ * @return Whether it is already used + */ + boolean isEmailAddressRegistered (final Contact contact); + + /** + * Checks whether all required personal data is set + *

+ * @return Whether the required personal data is set + */ + boolean isRequiredPersonalDataSet (); + + /** + * Checks whether all required personal data is set for changing them + *

+ * @return Whether the required personal data is set + */ + boolean isRequiredChangePersonalDataSet (); + + /** + * Checks whether same email addresses have been entered + *

+ * @return Whether same email addresses have been entered + */ + boolean isSameEmailAddressEntered (); + + /** + * Changes logged-in user's personal data if the current password matches + * and TAC + privacy statement has been accepted. + *

+ * @return New target page + */ + String doChangePersonalContactData (); + +} diff --git a/src/java/org/mxchange/addressbook/beans/contact/AddressbookContactWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/contact/AddressbookContactWebSessionBean.java deleted file mode 100644 index 234057b8..00000000 --- a/src/java/org/mxchange/addressbook/beans/contact/AddressbookContactWebSessionBean.java +++ /dev/null @@ -1,1301 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * 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.addressbook.beans.contact; - -import java.text.MessageFormat; -import java.util.Collections; -import java.util.Date; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Objects; -import javax.annotation.PostConstruct; -import javax.ejb.EJB; -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 org.mxchange.addressbook.beans.BaseAddressbookController; -import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController; -import org.mxchange.addressbook.beans.user.login.AddressbookUserLoginWebSessionController; -import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent; -import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent; -import org.mxchange.jcontacts.model.contact.Contact; -import org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote; -import org.mxchange.jcontacts.model.contact.ContactUtils; -import org.mxchange.jcontacts.model.contact.UserContact; -import org.mxchange.jcontacts.model.contact.title.PersonalTitle; -import org.mxchange.jcountry.model.data.Country; -import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber; -import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber; -import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber; -import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber; -import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber; -import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumber; -import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider; -import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent; -import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent; -import org.mxchange.jusercore.model.user.User; -import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent; -import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent; -import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent; -import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException; - -/** - * A general contact bean (controller) - *

- * @author Roland Häder - */ -@Named ("contactController") -@SessionScoped -public class AddressbookContactWebSessionBean extends BaseAddressbookController implements AddressbookContactWebSessionController { - - /** - * Serial number - */ - private static final long serialVersionUID = 542_145_347_916L; - - /** - * Academic academicTitle - */ - private String academicTitle; - - /** - * Birth day - */ - private Date birthday; - - /** - * City - */ - private String city; - - /** - * Optional comments - */ - private String comment; - - /** - * EJB for general contact purposes - */ - @EJB (lookup = "java:global/addressbook-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote") - private ContactSessionBeanRemote contactBean; - - /** - * Contact list - */ - private final List contactList; - - /** - * Country instance - */ - private Country country; - - /** - * Email address - */ - private String emailAddress; - - /** - * Email address list - */ - private final List emailAddressList; - - /** - * Email address repeated - */ - private String emailAddressRepeat; - - /** - * Family name - */ - private String familyName; - - /** - * Fax number's area code - */ - private Integer faxAreaCode; - - /** - * Country instance for fax number - */ - private Country faxCountry; - - /** - * Fax number - */ - private Long faxNumber; - - /** - * First name - */ - private String firstName; - - /** - * House number - */ - private Short houseNumber; - - /** - * House number extension - */ - private String houseNumberExtension; - - /** - * Whether a fax entry has been unlinked - */ - private boolean isFaxUnlinked; - - /** - * Whether a land-line number has been unlinked - */ - private boolean isLandLineUnlinked; - - /** - * Whether a mobile entry has been unlinked - */ - private boolean isMobileUnlinked; - - /** - * Phone number area code - */ - private Integer landLineAreaCode; - - /** - * Country instance for phone number - */ - private Country landLineCountry; - - /** - * Phone number - */ - private Long landLineNumber; - - /** - * Mobile number - */ - private Long mobileNumber; - - /** - * Mobile number's carrier - */ - private MobileProvider mobileProvider; - - /** - * Personal academicTitle - */ - private PersonalTitle personalTitle; - - /** - * A list of all selectable contacts - */ - private List selectableContacts; - - /** - * Street - */ - private String street; - - /** - * Regular user controller - */ - @Inject - private AddressbookUserWebSessionController userController; - - /** - * Login bean (controller) - */ - @Inject - private AddressbookUserLoginWebSessionController userLoginController; - - /** - * ZIP code - */ - private Integer zipCode; - - /** - * Default constructor - */ - public AddressbookContactWebSessionBean () { - // Call super constructor - super(); - - // Init lists/maps - this.contactList = new LinkedList<>(); - this.emailAddressList = new LinkedList<>(); - } - - /** - * Observes events being fired when an administrator has added a new - * contact. - *

- * @param event Event being fired - */ - public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent 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(); - - // Call other method - this.uniqueAddContact(event.getAddedContact()); - - // Add to selectable contacts - this.selectableContacts.add(event.getAddedContact()); - } - - /** - * Event observer for newly added users by administrator - *

- * @param event Event being fired - */ - public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent 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(); - } - - /** - * Observes events being fired when an administrator has linked a new user - * with existing contact data. - *

- * @param event Event being fired - */ - public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getLinkedUser() == null) { - // Throw NPE again - throw new NullPointerException("event.linkedUser is null"); //NOI18N - } else if (event.getLinkedUser().getUserContact() == null) { - // Throw NPE again - throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N - } else if (event.getLinkedUser().getUserContact().getContactId() == null) { - // userId is null - throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N - } else if (event.getLinkedUser().getUserContact().getContactId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N - } - - // Remove contact from list available contacts list - this.selectableContacts.remove(event.getLinkedUser().getUserContact()); - - // Clear all data - this.clear(); - } - - /** - * Event observer for updated contact data by administrators - *

- * @param event Updated contact data event - */ - public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent 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("userId of user={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N - } - - // Add contact instance only once - this.uniqueAddContact(event.getUpdatedContact()); - - // Add email address to list - this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress()); - } - - /** - * Event observer when user confirmed account. - *

- * @param event Event being fired - */ - public void afterUserConfirmedAccount (@Observes final ObservableUserConfirmedAccountEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getConfirmedUser() == null) { - // Throw NPE again - throw new NullPointerException("event.confirmedUser is null"); //NOI18N - } else if (event.getConfirmedUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N - } else if (event.getConfirmedUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N - } - - // Add contact instance only once - this.uniqueAddContact(event.getConfirmedUser().getUserContact()); - } - - /** - * Event observer for logged-in user - *

- * @param event Event instance - */ - public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getLoggedInUser() == null) { - // Throw NPE again - throw new NullPointerException("event.loggedInUser is null"); //NOI18N - } else if (event.getLoggedInUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N - } else if (event.getLoggedInUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N - } - - // Copy all data to this bean - this.copyContact(event.getLoggedInUser().getUserContact()); - } - - /** - * Event observer for new user registrations - *

- * @param event User registration event - */ - public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getRegisteredUser() == null) { - // Throw NPE again - throw new NullPointerException("event.registeredUser is null"); //NOI18N - } else if (event.getRegisteredUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N - } else if (event.getRegisteredUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N - } - - // Get user instance - Contact registeredContact = event.getRegisteredUser().getUserContact(); - - // Copy all data from registered->user - this.copyContact(registeredContact); - - // Add contact instance only once - this.uniqueAddContact(registeredContact); - - // Add user name and email address - this.addUserNameEmailAddress(registeredContact); - - // Clear all data - this.clear(); - } - - @Override - @SuppressWarnings ("ReturnOfCollectionOrArrayField") - public List allContacts () { - // Return un-modified list - return this.contactList; - } - - @Override - public void clearEmailAddresses () { - // Clear both - this.setEmailAddress(null); - this.setEmailAddressRepeat(null); - } - - @Override - public Contact createContactInstance () { - // Is all required data set? - if (!this.isRequiredPersonalDataSet()) { - // No, then abort here - throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N - } - - // Required personal data must be set - assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N - - // Generate phone number - DialableLandLineNumber phone = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber()); - DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber()); - DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber()); - - // Create new contact - Contact contact = new UserContact(this.getPersonalTitle(), this.getFirstName(), this.getFamilyName()); - contact.setContactStreet(this.getStreet()); - contact.setContactHouseNumber(this.getHouseNumber()); - contact.setContactHouseNumberExtension(this.getHouseNumberExtension()); - contact.setContactZipCode(this.getZipCode()); - contact.setContactCity(this.getCity()); - contact.setContactCountry(this.getCountry()); - contact.setContactEmailAddress(this.getEmailAddress()); - contact.setContactBirthday(this.getBirthday()); - contact.setContactComment(this.getComment()); - - // Don't set null or wrong references - if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) { - // Now the number must be given - if (phone.getPhoneAreaCode() == null) { - // Is null - throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N - } else if (phone.getPhoneAreaCode() < 1) { - // Abort here - throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N - } else if (phone.getPhoneNumber() == null) { - // Is null - throw new NullPointerException("phone.phoneNumber is null"); //NOI18N - } else if (phone.getPhoneNumber() < 1) { - // Abort here - throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N - } - - // Set phone number - contact.setContactLandLineNumber(phone); - } - - // Don't set null or wrong references - if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) { - // Now the number must be given - if (fax.getPhoneAreaCode() == null) { - // Is null - throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N - } else if (fax.getPhoneAreaCode() < 1) { - // Abort here - throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N - } else if (fax.getPhoneNumber() == null) { - // Is null - throw new NullPointerException("fax.phoneNumber is null"); //NOI18N - } else if (fax.getPhoneNumber() < 1) { - // Abort here - throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N - } - - // Set fax number - contact.setContactFaxNumber(fax); - } - - // Is the provider set? - if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) { - // Is the number set? - if (mobile.getPhoneNumber() == null) { - // Is null - throw new NullPointerException("mobile.phoneNumber is null"); //NOI18N - } else if (mobile.getPhoneNumber() < 1) { - // Abort here - throw new IllegalArgumentException("mobile.phoneNumber is zero or below."); //NOI18N - } - - // Set mobile number - contact.setContactMobileNumber(mobile); - } - - // Return it - return contact; - } - - @Override - public String doChangePersonalContactData () { - // This method shall only be called if the user is logged-in - if (!this.userLoginController.isUserLoggedIn()) { - // Not logged-in - throw new IllegalStateException("User is not logged-in"); //NOI18N - } else if (!this.isRequiredChangePersonalDataSet()) { - // Not all required fields are set - throw new FaceletException("Not all required fields are set."); //NOI18N - } else if (!this.userLoginController.ifCurrentPasswordMatches()) { - // Password not matching - this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N - return ""; //NOI18N - } - - // Get contact instance - Contact contact = this.userLoginController.getLoggedInUser().getUserContact(); - - // It should be there, so run some tests on it - assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N - assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N - assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N - - // Update all fields - contact.setContactPersonalTitle(this.getPersonalTitle()); - contact.setContactFirstName(this.getFirstName()); - contact.setContactFamilyName(this.getFamilyName()); - contact.setContactStreet(this.getStreet()); - contact.setContactHouseNumber(this.getHouseNumber()); - contact.setContactHouseNumberExtension(this.getHouseNumberExtension()); - contact.setContactZipCode(this.getZipCode()); - contact.setContactCity(this.getCity()); - contact.setContactCountry(this.getCountry()); - - // Update contact's mobile number - this.isMobileUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber()); - - // Update contact's land-line number - this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber()); - - // Update contact's fax number - this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber()); - - // Send it to the EJB - this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked); - - // All fine - return "contact_data_saved"; //NOI18N - } - - /** - * Getter for academic title - *

- * @return Academic title - */ - public String getAcademicTitle () { - return this.academicTitle; - } - - /** - * Setter for academic title - *

- * @param academicTitle Academic title - */ - public void setAcademicTitle (final String academicTitle) { - this.academicTitle = academicTitle; - } - - /** - * Getter for birth day - *

- * @return Birth day - */ - @SuppressWarnings ("ReturnOfDateField") - public Date getBirthday () { - return this.birthday; - } - - /** - * Setter for birth day - *

- * @param birthday Birth day - */ - @SuppressWarnings ("AssignmentToDateFieldFromParameter") - public void setBirthday (final Date birthday) { - this.birthday = birthday; - } - - /** - * Getter for city name - *

- * @return City name - */ - public String getCity () { - return this.city; - } - - /** - * Setter for city name - *

- * @param city City name - */ - public void setCity (final String city) { - this.city = city; - } - - /** - * Getter for comments - *

- * @return Comments - */ - public String getComment () { - return this.comment; - } - - /** - * Setter for comment - *

- * @param comment Comments - */ - public void setComment (final String comment) { - this.comment = comment; - } - - @Override - public String getControllerType () { - return "general"; //NOI18N - } - - @Override - @Deprecated - public void setControllerType (final String controllerType) { - throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N - } - - /** - * Getter for country instance - *

- * @return Country instance - */ - public Country getCountry () { - return this.country; - } - - /** - * Setter for country instance - *

- * @param country Country instance - */ - public void setCountry (final Country country) { - this.country = country; - } - - @Override - public String getEmailAddress () { - return this.emailAddress; - } - - /** - * Setter for email address - *

- * @param emailAddress Email address - */ - public void setEmailAddress (final String emailAddress) { - this.emailAddress = emailAddress; - } - - /** - * Getter for email address, repeated - *

- * @return the emailAddress, repeated - */ - public String getEmailAddressRepeat () { - return this.emailAddressRepeat; - } - - /** - * Setter for email address repeated - *

- * @param emailAddressRepeat the emailAddress to set - */ - public void setEmailAddressRepeat (final String emailAddressRepeat) { - this.emailAddressRepeat = emailAddressRepeat; - } - - /** - * Family name - *

- * @return the familyName - */ - public String getFamilyName () { - return this.familyName; - } - - /** - * Family name - *

- * @param familyName the familyName to set - */ - public void setFamilyName (final String familyName) { - this.familyName = familyName; - } - - /** - * Getter for fax number's area code - *

- * @return Fax number's area code - */ - public Integer getFaxAreaCode () { - return this.faxAreaCode; - } - - /** - * Setter for fax number's area code - *

- * @param faxAreaCode Fax number's area code - */ - public void setFaxAreaCode (final Integer faxAreaCode) { - this.faxAreaCode = faxAreaCode; - } - - /** - * Getter for fax's country instance - *

- * @return Fax' country instance - */ - public Country getFaxCountry () { - return this.faxCountry; - } - - /** - * Setter for fax's country instance - *

- * @param faxCountry Fax' country instance - */ - public void setFaxCountry (final Country faxCountry) { - this.faxCountry = faxCountry; - } - - /** - * Getter for fax number - *

- * @return Fax number - */ - public Long getFaxNumber () { - return this.faxNumber; - } - - /** - * Setter for fax number - *

- * @param faxNumber Fax number - */ - public void setFaxNumber (final Long faxNumber) { - this.faxNumber = faxNumber; - } - - /** - * First name - *

- * @return the first name - */ - public String getFirstName () { - return this.firstName; - } - - /** - * First name - *

- * @param firstName the first name to set - */ - public void setFirstName (final String firstName) { - this.firstName = firstName; - } - - /** - * House number - *

- * @return the houseNumber - */ - public Short getHouseNumber () { - return this.houseNumber; - } - - /** - * House number - *

- * @param houseNumber the houseNumber to set - */ - public void setHouseNumber (final Short houseNumber) { - this.houseNumber = houseNumber; - } - - /** - * Getter for house number extension, example: 123a 'a' is then the - * extension and 123 is the house number. - *

- * @return House number extension - */ - public String getHouseNumberExtension () { - return this.houseNumberExtension; - } - - /** - * Setter for house number extension - *

- * @param houseNumberExtension House number extension - */ - public void setHouseNumberExtension (final String houseNumberExtension) { - this.houseNumberExtension = houseNumberExtension; - } - - /** - * Getter for land-line number's area code - *

- * @return Land-line number's area code - */ - public Integer getLandLineAreaCode () { - return this.landLineAreaCode; - } - - /** - * Setter for land-line number's area code - *

- * @param landLineAreaCode Land-line number's area code - */ - public void setLandLineAreaCode (final Integer landLineAreaCode) { - this.landLineAreaCode = landLineAreaCode; - } - - /** - * Getter for land-line number's country instance - *

- * @return Land-line number's country instance - */ - public Country getLandLineCountry () { - return this.landLineCountry; - } - - /** - * Setter for land-line number's country instance - *

- * @param landLineCountry Land-line number's country instance - */ - public void setLandLineCountry (final Country landLineCountry) { - this.landLineCountry = landLineCountry; - } - - /** - * Getter for land-line number - *

- * @return Land-line number - */ - public Long getLandLineNumber () { - return this.landLineNumber; - } - - /** - * Setter for land-line number - *

- * @param landLineNumber Land-line number - */ - public void setLandLineNumber (final Long landLineNumber) { - this.landLineNumber = landLineNumber; - } - - /** - * Getter for mobile number - *

- * @return Mobile number - */ - public Long getMobileNumber () { - return this.mobileNumber; - } - - /** - * Setter for mobile number - *

- * @param mobileNumber Mobile number - */ - public void setMobileNumber (final Long mobileNumber) { - this.mobileNumber = mobileNumber; - } - - /** - * Getter for mobile number's carrier - *

- * @return Mobile number's carrier - */ - public MobileProvider getMobileProvider () { - return this.mobileProvider; - } - - /** - * Setter for mobile number's provider - *

- * @param mobileProvider Mobile number's provider - */ - public void setMobileProvider (final MobileProvider mobileProvider) { - this.mobileProvider = mobileProvider; - } - - /** - * Getter for personal title - *

- * @return Personal title - */ - public PersonalTitle getPersonalTitle () { - return this.personalTitle; - } - - /** - * Setter for personal title - *

- * @param personalTitle Personal title - */ - public void setPersonalTitle (final PersonalTitle personalTitle) { - this.personalTitle = personalTitle; - } - - /** - * Getter for street - *

- * @return Street - */ - public String getStreet () { - return this.street; - } - - /** - * Setter for street - *

- * @param street Street - */ - public void setStreet (final String street) { - this.street = street; - } - - /** - * Getter for ZIP code - *

- * @return ZIP code - */ - public Integer getZipCode () { - return this.zipCode; - } - - /** - * Setter for ZIP code - *

- * @param zipCode ZIP code - */ - public void setZipCode (final Integer zipCode) { - this.zipCode = zipCode; - } - - /** - * Post-construction method - */ - @PostConstruct - public void init () { - // Get full email address list for reducing EJB calls - this.emailAddressList.addAll(this.contactBean.getEmailAddressList()); - - // Get all contacts - List allContacts = this.contactBean.getAllContacts(); - - // Get full contact list - this.contactList.addAll(allContacts); - - // Get all users - List allUsers = this.userController.allUsers(); - - // Get iterator - Iterator iterator = allContacts.iterator(); - - // Loop through it - while (iterator.hasNext()) { - // Get next element - Contact next = iterator.next(); - - // Get iterator - Iterator userIterator = allUsers.iterator(); - - // Loop through all users - while (userIterator.hasNext()) { - // Get user instance - User nextUser = userIterator.next(); - - // Is contact same? - if (Objects.equals(next, nextUser.getUserContact())) { - // Found same - iterator.remove(); - break; - } - } - } - - // Set contact list - this.selectableContacts = allContacts; - } - - @Override - public boolean isEmailAddressRegistered (final Contact contact) { - // Cherck parameter - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactEmailAddress() == null) { - // Throw again - throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N - } else if (contact.getContactEmailAddress().isEmpty()) { - // Is empty - throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N - } - - // Determine it - return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress()))); - } - - @Override - public boolean isRequiredChangePersonalDataSet () { - return ((this.getPersonalTitle() != null) && - (this.getFirstName() != null) && - (this.getFamilyName() != null) && - (this.getStreet() != null) && - (this.getHouseNumber() != null) && - (this.getZipCode() != null) && - (this.getCity() != null)); - } - - @Override - public boolean isRequiredPersonalDataSet () { - return ((this.getPersonalTitle() != null) && - (this.getFirstName() != null) && - (this.getFamilyName() != null) && - (this.getStreet() != null) && - (this.getHouseNumber() != null) && - (this.getZipCode() != null) && - (this.getCity() != null) && - (this.getEmailAddress() != null) && - (this.getEmailAddressRepeat() != null)); - } - - @Override - public boolean isSameEmailAddressEntered () { - return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat())); - } - - /** - * Returns a list of all selectable contacts for user creation. Contacts - * from already existing users are excluded in this list. - *

- * @return A list of all selectable contacts - */ - public List selectableContacts () { - return Collections.unmodifiableList(this.selectableContacts); - } - - @Override - public void updateContactDataFromController (final Contact contact) { - // Is the instance valid? - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() == null) { - // Throw NPE - throw new NullPointerException("contact.contactId is null"); //NOI18N - } else if (contact.getContactId() < 1) { - // Not valid id number - throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N - } - - // Set all - this.copyContact(contact); - } - - /** - * Adds email address to bean's internal list. - *

- * @param contact Contact instance - */ - private void addUserNameEmailAddress (final Contact contact) { - // Make sure the entry is not added yet - if (this.emailAddressList.contains(contact.getContactEmailAddress())) { - // Already added - throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N - } - - // Add email addres - this.emailAddressList.add(contact.getContactEmailAddress()); - } - - /** - * Clears this bean - */ - private void clear () { - // Clear all data - // - personal data - this.setPersonalTitle(null); - this.setAcademicTitle(null); - this.setFirstName(null); - this.setFamilyName(null); - this.setStreet(null); - this.setHouseNumber(null); - this.setHouseNumberExtension(null); - this.setZipCode(null); - this.setCity(null); - this.setCountry(null); - - // - contact data - this.clearEmailAddresses(); - this.setLandLineAreaCode(null); - this.setLandLineCountry(null); - this.setLandLineNumber(null); - this.setMobileProvider(null); - this.setMobileNumber(null); - this.setFaxAreaCode(null); - this.setFaxCountry(null); - this.setFaxNumber(null); - - // - other data - this.setBirthday(null); - this.setComment(null); - } - - /** - * Copies given contact into the controller - *

- * @param contact Contact instance - */ - private void copyContact (final Contact contact) { - // Is the instance valid? - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() == null) { - // Throw NPE - throw new NullPointerException("contact.contactId is null"); //NOI18N - } else if (contact.getContactId() < 1) { - // Not valid id number - throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N - } - - // Copy all fields: - // - base data - this.setPersonalTitle(contact.getContactPersonalTitle()); - this.setAcademicTitle(contact.getContactTitle()); - this.setFirstName(contact.getContactFirstName()); - this.setFamilyName(contact.getContactFamilyName()); - this.setStreet(contact.getContactStreet()); - this.setHouseNumber(contact.getContactHouseNumber()); - this.setHouseNumberExtension(contact.getContactHouseNumberExtension()); - this.setZipCode(contact.getContactZipCode()); - this.setCity(contact.getContactCity()); - this.setCountry(contact.getContactCountry()); - this.setEmailAddress(contact.getContactEmailAddress()); - this.setBirthday(contact.getContactBirthday()); - this.setComment(contact.getContactComment()); - - // Get mobile, phone and fax instance - DialableMobileNumber mobile = contact.getContactMobileNumber(); - DialableFaxNumber fax = contact.getContactFaxNumber(); - DialableLandLineNumber phone = contact.getContactLandLineNumber(); - - // - contact data - if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) { - this.setLandLineCountry(phone.getPhoneCountry()); - this.setLandLineAreaCode(phone.getPhoneAreaCode()); - this.setLandLineNumber(phone.getPhoneNumber()); - } - - if ((mobile instanceof DialableMobileNumber) && (mobile.getMobileProvider() instanceof MobileProvider)) { - this.setMobileProvider(mobile.getMobileProvider()); - this.setMobileNumber(mobile.getPhoneNumber()); - } - - if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) { - this.setFaxCountry(fax.getPhoneCountry()); - this.setFaxAreaCode(fax.getPhoneAreaCode()); - this.setFaxNumber(fax.getPhoneNumber()); - } - } - - /** - * Removes given contact from all lists - *

- * @param contact Contact instance to remove - */ - private void removeContact (final Contact contact) { - // Is the instance valid? - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() == null) { - // Throw NPE - throw new NullPointerException("contact.contactId is null"); //NOI18N - } else if (contact.getContactId() < 1) { - // Not valid id number - throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N - } - - // Remove from general list - if (!this.contactList.remove(contact)) { - // Did not remove contact - throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N - } - - // Remove from other lists - this.emailAddressList.remove(contact.getContactEmailAddress()); - } - - /** - * Adds unique instance to contact list. First any existing instance is - * being removed, then the new instance is added. - *

- * @param contact Contact instance to add uniquely - */ - private void uniqueAddContact (final Contact contact) { - // Is the instance valid? - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() == null) { - // Throw NPE - throw new NullPointerException("contact.contactId is null"); //NOI18N - } else if (contact.getContactId() < 1) { - // Not valid id number - throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N - } - - // Get iterator from list - Iterator iterator = this.contactList.iterator(); - - // "Walk" through all entries - while (iterator.hasNext()) { - // Get next element - Contact next = iterator.next(); - - // Is id number the same? - if (Objects.equals(contact.getContactId(), next.getContactId())) { - // Found entry, so remove it and abort - this.removeContact(next); - break; - } - } - - // Add contact to list - this.contactList.add(contact); - } - -} diff --git a/src/java/org/mxchange/addressbook/beans/contact/AddressbookContactWebSessionController.java b/src/java/org/mxchange/addressbook/beans/contact/AddressbookContactWebSessionController.java deleted file mode 100644 index c41847e0..00000000 --- a/src/java/org/mxchange/addressbook/beans/contact/AddressbookContactWebSessionController.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * 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.addressbook.beans.contact; - -import java.io.Serializable; -import java.util.List; -import org.mxchange.jcontacts.model.contact.Contact; - -/** - * An interface for user beans - *

- * @author Roland Häder - */ -public interface AddressbookContactWebSessionController extends Serializable { - - /** - * Minimum password length - */ - public static final Integer MINIMUM_PASSWORD_LENGTH = 5; - - /** - * Getter for email address - *

- * @return Email address - */ - String getEmailAddress (); - - /** - * Clears both email address field - */ - void clearEmailAddresses (); - - /** - * Returns a list of all found contacts - *

- * @return A list of all contacts. - */ - List allContacts (); - - /** - * Updates all data from bean in given contact instance - *

- * @param userContact Contact instance to update - */ - void updateContactDataFromController (final Contact userContact); - - /** - * Creates an instance from all properties - *

- * @return A contact instance - */ - Contact createContactInstance (); - - /** - * Getter for controller type - *

- * @return controller type - */ - String getControllerType (); - - /** - * Setter for controller type - *

- * @param controllerType Controller type - *

- * @deprecated Don't use this method. - */ - @Deprecated - void setControllerType (final String controllerType); - - /** - * Checks whether contact instance's email address is used - *

- * @param contact Contact instance's email address to check - *

- * @return Whether it is already used - */ - boolean isEmailAddressRegistered (final Contact contact); - - /** - * Checks whether all required personal data is set - *

- * @return Whether the required personal data is set - */ - boolean isRequiredPersonalDataSet (); - - /** - * Checks whether all required personal data is set for changing them - *

- * @return Whether the required personal data is set - */ - boolean isRequiredChangePersonalDataSet (); - - /** - * Checks whether same email addresses have been entered - *

- * @return Whether same email addresses have been entered - */ - boolean isSameEmailAddressEntered (); - - /** - * Changes logged-in user's personal data if the current password matches - * and TAC + privacy statement has been accepted. - *

- * @return New target page - */ - String doChangePersonalContactData (); - -} diff --git a/src/java/org/mxchange/addressbook/beans/contact/phone/AddressbookContactPhoneWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/contact/phone/AddressbookContactPhoneWebRequestBean.java new file mode 100644 index 00000000..1b933fd2 --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/contact/phone/AddressbookContactPhoneWebRequestBean.java @@ -0,0 +1,500 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * 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.addressbook.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.enterprise.context.RequestScoped; +import javax.enterprise.event.Observes; +import javax.inject.Inject; +import javax.inject.Named; +import org.mxchange.addressbook.beans.BaseAddressbookController; +import org.mxchange.addressbook.beans.contact.AddressbookContactWebRequestController; +import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent; +import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent; +import org.mxchange.jcontacts.events.fax.unlinked.ObservableAdminUnlinkedFaxNumberEvent; +import org.mxchange.jcontacts.events.landline.unlinked.ObservableAdminUnlinkedLandLineNumberEvent; +import org.mxchange.jcontacts.events.mobile.unlinked.ObservableAdminUnlinkedMobileNumberEvent; +import org.mxchange.jcontacts.model.contact.Contact; +import org.mxchange.jphone.events.fax.created.ObservableCreatedFaxNumberEvent; +import org.mxchange.jphone.events.landline.created.ObservableCreatedLandLineNumberEvent; +import org.mxchange.jphone.events.mobile.created.ObservableCreatedMobileNumberEvent; +import org.mxchange.jphone.model.phonenumbers.DialableNumber; +import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber; +import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber; +import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber; +import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent; + +/** + * A general contact bean (controller) + *

+ * @author Roland Häder + */ +@Named ("contactPhoneController") +@RequestScoped +public class AddressbookContactPhoneWebRequestBean extends BaseAddressbookController implements AddressbookContactPhoneWebRequestController { + + /** + * Serial number + */ + private static final long serialVersionUID = 542_145_347_916L; + + /** + * General contact controller + */ + @Inject + private AddressbookContactWebRequestController contactController; + + /** + * "Cache" for contact's mobile, land-line and fax numbers. Currently one + * per each type is supported. Maybe later this will change into a OneToMany + * relationship (one contact, many numbers). + */ + private final Map> contacts; + + /** + * fax number + */ + private DialableFaxNumber faxNumber; + + /** + * land-line number + */ + private DialableLandLineNumber landLineNumber; + + /** + * Chosen mobile number + */ + private DialableMobileNumber mobileNumber; + + /** + * Default constructor + */ + public AddressbookContactPhoneWebRequestBean () { + // Call super constructor + super(); + + // Init lists/maps + this.contacts = new HashMap<>(10); + } + + /** + * Observes events being fired when an administrator has added a new + * contact. + *

+ * @param event Event being fired + */ + public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent 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 + } + + // Clear this bean + this.clear(); + } + + /** + * Event observer for newly added users by administrator + *

+ * @param event Event being fired + */ + public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent 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(); + } + + /** + * Event observer for unlinked fax contact by administrators + *

+ * @param event Unlinked fax contact event + */ + public void afterAdminUnlinkedFaxContactDataEvent (@Observes final ObservableAdminUnlinkedFaxNumberEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getUnlinkedFaxNumber() == null) { + // Throw NPE again + throw new NullPointerException("event.unlinkedFaxNumber is null"); //NOI18N + } else if (event.getUnlinkedFaxNumber().getPhoneId() == null) { + // userId is null + throw new NullPointerException("event.unlinkedFaxNumber.contactId is null"); //NOI18N + } else if (event.getUnlinkedFaxNumber().getPhoneId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedFaxNumber(), event.getUnlinkedFaxNumber().getPhoneId())); //NOI18N + } + + // Remove it from list + this.contacts.remove(event.getUnlinkedFaxNumber()); + + // Clear all data + this.clear(); + } + + /** + * Event observer for unlinked land-line contact by administrators + *

+ * @param event Unlinked land-line contact event + */ + public void afterAdminUnlinkedLandLineContactDataEvent (@Observes final ObservableAdminUnlinkedLandLineNumberEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getUnlinkedLandLineNumber() == null) { + // Throw NPE again + throw new NullPointerException("event.unlinkedLandLineNumber is null"); //NOI18N + } else if (event.getUnlinkedLandLineNumber().getPhoneId() == null) { + // userId is null + throw new NullPointerException("event.unlinkedLandLineNumber.contactId is null"); //NOI18N + } else if (event.getUnlinkedLandLineNumber().getPhoneId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedLandLineNumber(), event.getUnlinkedLandLineNumber().getPhoneId())); //NOI18N + } + + // Remove it from list + this.contacts.remove(event.getUnlinkedLandLineNumber()); + + // Clear all data + this.clear(); + } + + /** + * Event observer for unlinked mobile contact by administrators + *

+ * @param event Unlinked mobile contact event + */ + public void afterAdminUnlinkedMobileContactDataEvent (@Observes final ObservableAdminUnlinkedMobileNumberEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getUnlinkedMobileNumber() == null) { + // Throw NPE again + throw new NullPointerException("event.unlinkedMobileNumber is null"); //NOI18N + } else if (event.getUnlinkedMobileNumber().getPhoneId() == null) { + // userId is null + throw new NullPointerException("event.unlinkedMobileNumber.contactId is null"); //NOI18N + } else if (event.getUnlinkedMobileNumber().getPhoneId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedMobileNumber(), event.getUnlinkedMobileNumber().getPhoneId())); //NOI18N + } + + // Remove it from list + this.contacts.remove(event.getUnlinkedMobileNumber()); + + // Clear all data + this.clear(); + } + + /** + * Event observer for updated contact data by administrators + *

+ * @param event Updated contact data event + */ + public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent 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 + } + + // Clear all data + this.clear(); + } + + /** + * Observes events being fired when a bean helper has successfully created a + * fax number instance. + *

+ * @param event Event being fired + */ + public void afterCreatedFaxNumberEvent (@Observes final ObservableCreatedFaxNumberEvent event) { + // The event instance must be valid + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getFaxNumber() == null) { + // Throw NPE again + throw new NullPointerException("event.faxNumber is null"); //NOI18N + } else if (event.getFaxNumber().getPhoneId() == null) { + // Throw NPE yet again + throw new NullPointerException("event.faxNumber.phoneId is null"); //NOI18N + } else if (event.getFaxNumber().getPhoneId() < 1) { + // Throw NPE yet again + throw new NullPointerException(MessageFormat.format("event.faxNumber.phoneId={0} is invalid", event.getFaxNumber().getPhoneId())); //NOI18N + } + + // Set it here + this.setFaxNumber(event.getFaxNumber()); + } + + /** + * Observes events being fired when a bean helper has successfully created a + * land-line number instance. + *

+ * @param event Event being fired + */ + public void afterCreatedLandLineNumberEvent (@Observes final ObservableCreatedLandLineNumberEvent event) { + // The event instance must be valid + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getLandLineNumber() == null) { + // Throw NPE again + throw new NullPointerException("event.landLineNumber is null"); //NOI18N + } else if (event.getLandLineNumber().getPhoneId() == null) { + // Throw NPE yet again + throw new NullPointerException("event.landLineNumber.phoneId is null"); //NOI18N + } else if (event.getLandLineNumber().getPhoneId() < 1) { + // Throw NPE yet again + throw new NullPointerException(MessageFormat.format("event.landLineNumber.phoneId={0} is invalid", event.getLandLineNumber().getPhoneId())); //NOI18N + } + + // Set it here + this.setLandLineNumber(event.getLandLineNumber()); + } + + /** + * Observes events being fired when a bean helper has successfully created a + * mobile number instance. + *

+ * @param event Event being fired + */ + public void afterCreatedMobileNumberEvent (@Observes final ObservableCreatedMobileNumberEvent event) { + // The event instance must be valid + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getMobileNumber() == null) { + // Throw NPE again + throw new NullPointerException("event.mobileNumber is null"); //NOI18N + } else if (event.getMobileNumber().getPhoneId() == null) { + // Throw NPE yet again + throw new NullPointerException("event.mobileNumber.phoneId is null"); //NOI18N + } else if (event.getMobileNumber().getPhoneId() < 1) { + // Throw NPE yet again + throw new NullPointerException(MessageFormat.format("event.mobileNumber.phoneId={0} is invalid", event.getMobileNumber().getPhoneId())); //NOI18N + } + + // Set it here + this.setMobileNumber(event.getMobileNumber()); + } + + /** + * Getter for all contacts having current fax number linked + *

+ * @return List of all linked contacts + */ + public List allCurrentFaxNumberContacts () { + // Get id + DialableFaxNumber number = this.getFaxNumber(); + + // Is cache there? + if (this.contacts.containsKey(number)) { + // Return cached version + return this.contacts.get(number); + } else { + // Ask bean + List list = new LinkedList<>(); + + // "Walk" through all contacts + for (final Contact contact : this.contactController.allContacts()) { + // Is mobile instance the same? + if (Objects.equals(contact.getContactFaxNumber(), number)) { + // Found one + list.add(contact); + } + } + + // Store result in cache + this.contacts.put(number, list); + + // Return now-cached list + return list; + } + } + + /** + * Getter for all contacts having current land-line number linked + *

+ * @return List of all linked contacts + */ + public List allCurrentLandLineNumberContacts () { + // Get id + DialableLandLineNumber number = this.getLandLineNumber(); + + // Is cache there? + if (this.contacts.containsKey(number)) { + // Return cached version + return this.contacts.get(number); + } else { + // Ask bean + List list = new LinkedList<>(); + + // "Walk" through all contacts + for (final Contact contact : this.contactController.allContacts()) { + // Is mobile instance the same? + if (Objects.equals(contact.getContactLandLineNumber(), number)) { + // Found one + list.add(contact); + } + } + + // Store result in cache + this.contacts.put(number, list); + + // Return now-cached list + return list; + } + } + + /** + * Getter for all contacts having current mobile number linked + *

+ * @return List of all linked contacts + */ + public List allCurrentMobileNumberContacts () { + // Get id + DialableMobileNumber number = this.getMobileNumber(); + + // Is cache there? + if (this.contacts.containsKey(number)) { + // Return cached version + return this.contacts.get(number); + } else { + // Ask bean + List list = new LinkedList<>(); + + // "Walk" through all contacts + for (final Contact contact : this.contactController.allContacts()) { + // Is mobile instance the same? + if (Objects.equals(contact.getContactMobileNumber(), number)) { + // Found one + list.add(contact); + } + } + + // Store result in cache + this.contacts.put(number, list); + + // Return now-cached list + return list; + } + } + + /** + * Getter for chosen fax number + *

+ * @return fax number + */ + public DialableFaxNumber getFaxNumber () { + return this.faxNumber; + } + + /** + * Setter for chosen fax number + *

+ * @param faxNumber fax number + */ + public void setFaxNumber (final DialableFaxNumber faxNumber) { + this.faxNumber = faxNumber; + } + + /** + * Getter for chosen land-line number + *

+ * @return land-line number + */ + public DialableLandLineNumber getLandLineNumber () { + return this.landLineNumber; + } + + /** + * Setter for chosen land-line number + *

+ * @param landLineNumber land-line number + */ + public void setLandLineNumber (final DialableLandLineNumber landLineNumber) { + this.landLineNumber = landLineNumber; + } + + /** + * Getter for chosen mobile number + *

+ * @return mobile number + */ + public DialableMobileNumber getMobileNumber () { + return this.mobileNumber; + } + + /** + * Setter for chosen mobile number + *

+ * @param mobileNumber mobile number + */ + public void setMobileNumber (final DialableMobileNumber mobileNumber) { + this.mobileNumber = mobileNumber; + } + + /** + * Clears this bean + */ + private void clear () { + // Clear all data + } + +} diff --git a/src/java/org/mxchange/addressbook/beans/contact/phone/AddressbookContactPhoneWebRequestController.java b/src/java/org/mxchange/addressbook/beans/contact/phone/AddressbookContactPhoneWebRequestController.java new file mode 100644 index 00000000..09915e73 --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/contact/phone/AddressbookContactPhoneWebRequestController.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * 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.addressbook.beans.contact.phone; + +import java.io.Serializable; + +/** + * An interface for user beans + *

+ * @author Roland Häder + */ +public interface AddressbookContactPhoneWebRequestController extends Serializable { + +} diff --git a/src/java/org/mxchange/addressbook/beans/contact/phone/AddressbookContactPhoneWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/contact/phone/AddressbookContactPhoneWebSessionBean.java deleted file mode 100644 index c9a46394..00000000 --- a/src/java/org/mxchange/addressbook/beans/contact/phone/AddressbookContactPhoneWebSessionBean.java +++ /dev/null @@ -1,500 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * 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.addressbook.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.enterprise.context.SessionScoped; -import javax.enterprise.event.Observes; -import javax.inject.Inject; -import javax.inject.Named; -import org.mxchange.addressbook.beans.BaseAddressbookController; -import org.mxchange.addressbook.beans.contact.AddressbookContactWebSessionController; -import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent; -import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent; -import org.mxchange.jcontacts.events.fax.unlinked.ObservableAdminUnlinkedFaxNumberEvent; -import org.mxchange.jcontacts.events.landline.unlinked.ObservableAdminUnlinkedLandLineNumberEvent; -import org.mxchange.jcontacts.events.mobile.unlinked.ObservableAdminUnlinkedMobileNumberEvent; -import org.mxchange.jcontacts.model.contact.Contact; -import org.mxchange.jphone.events.fax.created.ObservableCreatedFaxNumberEvent; -import org.mxchange.jphone.events.landline.created.ObservableCreatedLandLineNumberEvent; -import org.mxchange.jphone.events.mobile.created.ObservableCreatedMobileNumberEvent; -import org.mxchange.jphone.model.phonenumbers.DialableNumber; -import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber; -import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber; -import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber; -import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent; - -/** - * A general contact bean (controller) - *

- * @author Roland Häder - */ -@Named ("contactPhoneController") -@SessionScoped -public class AddressbookContactPhoneWebSessionBean extends BaseAddressbookController implements AddressbookContactPhoneWebSessionController { - - /** - * Serial number - */ - private static final long serialVersionUID = 542_145_347_916L; - - /** - * General contact controller - */ - @Inject - private AddressbookContactWebSessionController contactController; - - /** - * "Cache" for contact's mobile, land-line and fax numbers. Currently one - * per each type is supported. Maybe later this will change into a OneToMany - * relationship (one contact, many numbers). - */ - private final Map> contacts; - - /** - * fax number - */ - private DialableFaxNumber faxNumber; - - /** - * land-line number - */ - private DialableLandLineNumber landLineNumber; - - /** - * Chosen mobile number - */ - private DialableMobileNumber mobileNumber; - - /** - * Default constructor - */ - public AddressbookContactPhoneWebSessionBean () { - // Call super constructor - super(); - - // Init lists/maps - this.contacts = new HashMap<>(10); - } - - /** - * Observes events being fired when an administrator has added a new - * contact. - *

- * @param event Event being fired - */ - public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent 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 - } - - // Clear this bean - this.clear(); - } - - /** - * Event observer for newly added users by administrator - *

- * @param event Event being fired - */ - public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent 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(); - } - - /** - * Event observer for unlinked fax contact by administrators - *

- * @param event Unlinked fax contact event - */ - public void afterAdminUnlinkedFaxContactDataEvent (@Observes final ObservableAdminUnlinkedFaxNumberEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getUnlinkedFaxNumber() == null) { - // Throw NPE again - throw new NullPointerException("event.unlinkedFaxNumber is null"); //NOI18N - } else if (event.getUnlinkedFaxNumber().getPhoneId() == null) { - // userId is null - throw new NullPointerException("event.unlinkedFaxNumber.contactId is null"); //NOI18N - } else if (event.getUnlinkedFaxNumber().getPhoneId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedFaxNumber(), event.getUnlinkedFaxNumber().getPhoneId())); //NOI18N - } - - // Remove it from list - this.contacts.remove(event.getUnlinkedFaxNumber()); - - // Clear all data - this.clear(); - } - - /** - * Event observer for unlinked land-line contact by administrators - *

- * @param event Unlinked land-line contact event - */ - public void afterAdminUnlinkedLandLineContactDataEvent (@Observes final ObservableAdminUnlinkedLandLineNumberEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getUnlinkedLandLineNumber() == null) { - // Throw NPE again - throw new NullPointerException("event.unlinkedLandLineNumber is null"); //NOI18N - } else if (event.getUnlinkedLandLineNumber().getPhoneId() == null) { - // userId is null - throw new NullPointerException("event.unlinkedLandLineNumber.contactId is null"); //NOI18N - } else if (event.getUnlinkedLandLineNumber().getPhoneId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedLandLineNumber(), event.getUnlinkedLandLineNumber().getPhoneId())); //NOI18N - } - - // Remove it from list - this.contacts.remove(event.getUnlinkedLandLineNumber()); - - // Clear all data - this.clear(); - } - - /** - * Event observer for unlinked mobile contact by administrators - *

- * @param event Unlinked mobile contact event - */ - public void afterAdminUnlinkedMobileContactDataEvent (@Observes final ObservableAdminUnlinkedMobileNumberEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getUnlinkedMobileNumber() == null) { - // Throw NPE again - throw new NullPointerException("event.unlinkedMobileNumber is null"); //NOI18N - } else if (event.getUnlinkedMobileNumber().getPhoneId() == null) { - // userId is null - throw new NullPointerException("event.unlinkedMobileNumber.contactId is null"); //NOI18N - } else if (event.getUnlinkedMobileNumber().getPhoneId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedMobileNumber(), event.getUnlinkedMobileNumber().getPhoneId())); //NOI18N - } - - // Remove it from list - this.contacts.remove(event.getUnlinkedMobileNumber()); - - // Clear all data - this.clear(); - } - - /** - * Event observer for updated contact data by administrators - *

- * @param event Updated contact data event - */ - public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent 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 - } - - // Clear all data - this.clear(); - } - - /** - * Observes events being fired when a bean helper has successfully created a - * fax number instance. - *

- * @param event Event being fired - */ - public void afterCreatedFaxNumberEvent (@Observes final ObservableCreatedFaxNumberEvent event) { - // The event instance must be valid - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getFaxNumber() == null) { - // Throw NPE again - throw new NullPointerException("event.faxNumber is null"); //NOI18N - } else if (event.getFaxNumber().getPhoneId() == null) { - // Throw NPE yet again - throw new NullPointerException("event.faxNumber.phoneId is null"); //NOI18N - } else if (event.getFaxNumber().getPhoneId() < 1) { - // Throw NPE yet again - throw new NullPointerException(MessageFormat.format("event.faxNumber.phoneId={0} is invalid", event.getFaxNumber().getPhoneId())); //NOI18N - } - - // Set it here - this.setFaxNumber(event.getFaxNumber()); - } - - /** - * Observes events being fired when a bean helper has successfully created a - * land-line number instance. - *

- * @param event Event being fired - */ - public void afterCreatedLandLineNumberEvent (@Observes final ObservableCreatedLandLineNumberEvent event) { - // The event instance must be valid - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getLandLineNumber() == null) { - // Throw NPE again - throw new NullPointerException("event.landLineNumber is null"); //NOI18N - } else if (event.getLandLineNumber().getPhoneId() == null) { - // Throw NPE yet again - throw new NullPointerException("event.landLineNumber.phoneId is null"); //NOI18N - } else if (event.getLandLineNumber().getPhoneId() < 1) { - // Throw NPE yet again - throw new NullPointerException(MessageFormat.format("event.landLineNumber.phoneId={0} is invalid", event.getLandLineNumber().getPhoneId())); //NOI18N - } - - // Set it here - this.setLandLineNumber(event.getLandLineNumber()); - } - - /** - * Observes events being fired when a bean helper has successfully created a - * mobile number instance. - *

- * @param event Event being fired - */ - public void afterCreatedMobileNumberEvent (@Observes final ObservableCreatedMobileNumberEvent event) { - // The event instance must be valid - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getMobileNumber() == null) { - // Throw NPE again - throw new NullPointerException("event.mobileNumber is null"); //NOI18N - } else if (event.getMobileNumber().getPhoneId() == null) { - // Throw NPE yet again - throw new NullPointerException("event.mobileNumber.phoneId is null"); //NOI18N - } else if (event.getMobileNumber().getPhoneId() < 1) { - // Throw NPE yet again - throw new NullPointerException(MessageFormat.format("event.mobileNumber.phoneId={0} is invalid", event.getMobileNumber().getPhoneId())); //NOI18N - } - - // Set it here - this.setMobileNumber(event.getMobileNumber()); - } - - /** - * Getter for all contacts having current fax number linked - *

- * @return List of all linked contacts - */ - public List allCurrentFaxNumberContacts () { - // Get id - DialableFaxNumber number = this.getFaxNumber(); - - // Is cache there? - if (this.contacts.containsKey(number)) { - // Return cached version - return this.contacts.get(number); - } else { - // Ask bean - List list = new LinkedList<>(); - - // "Walk" through all contacts - for (final Contact contact : this.contactController.allContacts()) { - // Is mobile instance the same? - if (Objects.equals(contact.getContactFaxNumber(), number)) { - // Found one - list.add(contact); - } - } - - // Store result in cache - this.contacts.put(number, list); - - // Return now-cached list - return list; - } - } - - /** - * Getter for all contacts having current land-line number linked - *

- * @return List of all linked contacts - */ - public List allCurrentLandLineNumberContacts () { - // Get id - DialableLandLineNumber number = this.getLandLineNumber(); - - // Is cache there? - if (this.contacts.containsKey(number)) { - // Return cached version - return this.contacts.get(number); - } else { - // Ask bean - List list = new LinkedList<>(); - - // "Walk" through all contacts - for (final Contact contact : this.contactController.allContacts()) { - // Is mobile instance the same? - if (Objects.equals(contact.getContactLandLineNumber(), number)) { - // Found one - list.add(contact); - } - } - - // Store result in cache - this.contacts.put(number, list); - - // Return now-cached list - return list; - } - } - - /** - * Getter for all contacts having current mobile number linked - *

- * @return List of all linked contacts - */ - public List allCurrentMobileNumberContacts () { - // Get id - DialableMobileNumber number = this.getMobileNumber(); - - // Is cache there? - if (this.contacts.containsKey(number)) { - // Return cached version - return this.contacts.get(number); - } else { - // Ask bean - List list = new LinkedList<>(); - - // "Walk" through all contacts - for (final Contact contact : this.contactController.allContacts()) { - // Is mobile instance the same? - if (Objects.equals(contact.getContactMobileNumber(), number)) { - // Found one - list.add(contact); - } - } - - // Store result in cache - this.contacts.put(number, list); - - // Return now-cached list - return list; - } - } - - /** - * Getter for chosen fax number - *

- * @return fax number - */ - public DialableFaxNumber getFaxNumber () { - return this.faxNumber; - } - - /** - * Setter for chosen fax number - *

- * @param faxNumber fax number - */ - public void setFaxNumber (final DialableFaxNumber faxNumber) { - this.faxNumber = faxNumber; - } - - /** - * Getter for chosen land-line number - *

- * @return land-line number - */ - public DialableLandLineNumber getLandLineNumber () { - return this.landLineNumber; - } - - /** - * Setter for chosen land-line number - *

- * @param landLineNumber land-line number - */ - public void setLandLineNumber (final DialableLandLineNumber landLineNumber) { - this.landLineNumber = landLineNumber; - } - - /** - * Getter for chosen mobile number - *

- * @return mobile number - */ - public DialableMobileNumber getMobileNumber () { - return this.mobileNumber; - } - - /** - * Setter for chosen mobile number - *

- * @param mobileNumber mobile number - */ - public void setMobileNumber (final DialableMobileNumber mobileNumber) { - this.mobileNumber = mobileNumber; - } - - /** - * Clears this bean - */ - private void clear () { - // Clear all data - } - -} diff --git a/src/java/org/mxchange/addressbook/beans/contact/phone/AddressbookContactPhoneWebSessionController.java b/src/java/org/mxchange/addressbook/beans/contact/phone/AddressbookContactPhoneWebSessionController.java deleted file mode 100644 index 586156c0..00000000 --- a/src/java/org/mxchange/addressbook/beans/contact/phone/AddressbookContactPhoneWebSessionController.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * 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.addressbook.beans.contact.phone; - -import java.io.Serializable; - -/** - * An interface for user beans - *

- * @author Roland Häder - */ -public interface AddressbookContactPhoneWebSessionController extends Serializable { - -} diff --git a/src/java/org/mxchange/addressbook/beans/country/AddressbookAdminCountryWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/country/AddressbookAdminCountryWebRequestBean.java index 496b64b1..6564ffd8 100644 --- a/src/java/org/mxchange/addressbook/beans/country/AddressbookAdminCountryWebRequestBean.java +++ b/src/java/org/mxchange/addressbook/beans/country/AddressbookAdminCountryWebRequestBean.java @@ -27,12 +27,12 @@ import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; import org.mxchange.addressbook.beans.BaseAddressbookController; -import org.mxchange.jcountry.model.data.Country; -import org.mxchange.jcountry.model.data.CountryData; -import org.mxchange.jcountry.model.data.CountrySingletonBeanRemote; import org.mxchange.jcountry.events.AdminAddedCountryEvent; import org.mxchange.jcountry.events.ObservableAdminAddedCountryEvent; import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException; +import org.mxchange.jcountry.model.data.Country; +import org.mxchange.jcountry.model.data.CountryData; +import org.mxchange.jcountry.model.data.CountrySingletonBeanRemote; /** * An administrative country bean @@ -75,7 +75,7 @@ public class AddressbookAdminCountryWebRequestBean extends BaseAddressbookContro * Regular country controller */ @Inject - private AddressbookCountryWebApplicationController countryController; + private AddressbookCountryWebRequestController countryController; /** * Local dial prefix diff --git a/src/java/org/mxchange/addressbook/beans/country/AddressbookCountryWebApplicationBean.java b/src/java/org/mxchange/addressbook/beans/country/AddressbookCountryWebApplicationBean.java deleted file mode 100644 index 42377c4b..00000000 --- a/src/java/org/mxchange/addressbook/beans/country/AddressbookCountryWebApplicationBean.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * 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.addressbook.beans.country; - -import java.text.MessageFormat; -import java.util.List; -import javax.annotation.PostConstruct; -import javax.ejb.EJB; -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.event.Observes; -import javax.inject.Named; -import org.mxchange.addressbook.beans.BaseAddressbookController; -import org.mxchange.jcountry.model.data.Country; -import org.mxchange.jcountry.model.data.CountrySingletonBeanRemote; -import org.mxchange.jcountry.events.ObservableAdminAddedCountryEvent; - -/** - * A country bean - *

- * @author Roland Häder - */ -@Named ("countryController") -@ApplicationScoped -public class AddressbookCountryWebApplicationBean extends BaseAddressbookController implements AddressbookCountryWebApplicationController { - - /** - * Serial number - */ - private static final long serialVersionUID = 176_985_298_681_742_960L; - - /** - * Remote country EJB - */ - @EJB (lookup = "java:global/addressbook-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote") - private CountrySingletonBeanRemote countryBean; - - /** - * List of all countries - */ - private List countryList; - - /** - * Default constructor - */ - public AddressbookCountryWebApplicationBean () { - // Call super constructor - super(); - } - - /** - * Observing method when the event is fired that an administrator added a - * new country - *

- * @param event Event instance - */ - public void afterAdminAddedCountryEvent (@Observes final ObservableAdminAddedCountryEvent event) { - // Is all valid? - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getAddedCountry() == null) { - // Throw again ... - throw new NullPointerException("event.addedCountry is null"); //NOI18N - } else if (event.getAddedCountry().getCountryId() == null) { - // And again ... - throw new NullPointerException("event.addedCountry.countryId is null"); //NOI18N - } else if (event.getAddedCountry().getCountryId() < 1) { - // Id is invalid - throw new IllegalArgumentException(MessageFormat.format("event.addedCountry.countryId={0} is not valid.", event.getAddedCountry().getCountryId())); //NOI18N - } - - // Add the event - this.countryList.add(event.getAddedCountry()); - } - - @Override - @SuppressWarnings ("ReturnOfCollectionOrArrayField") - public List allCountries () { - // Return "cached" version - return this.countryList; - } - - /** - * Post-construction method - */ - @PostConstruct - public void init () { - // "Cache" country list as this will not change so often. - this.countryList = this.countryBean.allCountries(); - } - -} diff --git a/src/java/org/mxchange/addressbook/beans/country/AddressbookCountryWebApplicationController.java b/src/java/org/mxchange/addressbook/beans/country/AddressbookCountryWebApplicationController.java deleted file mode 100644 index a30298cb..00000000 --- a/src/java/org/mxchange/addressbook/beans/country/AddressbookCountryWebApplicationController.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * 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.addressbook.beans.country; - -import java.io.Serializable; -import java.util.List; -import org.mxchange.jcountry.model.data.Country; - -/** - * An interface for country beans - *

- * @author Roland Häder - */ -public interface AddressbookCountryWebApplicationController extends Serializable { - - /** - * A list of all countries - *

- * @return All countries - */ - List allCountries (); - -} diff --git a/src/java/org/mxchange/addressbook/beans/country/AddressbookCountryWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/country/AddressbookCountryWebRequestBean.java new file mode 100644 index 00000000..62b35dd4 --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/country/AddressbookCountryWebRequestBean.java @@ -0,0 +1,138 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * 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.addressbook.beans.country; + +import java.text.MessageFormat; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import javax.annotation.PostConstruct; +import javax.ejb.EJB; +import javax.enterprise.context.RequestScoped; +import javax.enterprise.event.Observes; +import javax.inject.Inject; +import javax.inject.Named; +import org.mxchange.addressbook.beans.BaseAddressbookController; +import org.mxchange.jcountry.events.ObservableAdminAddedCountryEvent; +import org.mxchange.jcountry.model.data.Country; +import org.mxchange.jcountry.model.data.CountrySingletonBeanRemote; + +/** + * A country bean + *

+ * @author Roland Häder + */ +@Named ("countryController") +@RequestScoped +public class AddressbookCountryWebRequestBean extends BaseAddressbookController implements AddressbookCountryWebRequestController { + + /** + * Serial number + */ + private static final long serialVersionUID = 176_985_298_681_742_960L; + + /** + * Remote country EJB + */ + @EJB (lookup = "java:global/addressbook-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote") + private CountrySingletonBeanRemote countryBean; + + /** + * List of all countries + */ + @Inject + @Cached (cacheName = "countryCache") + private transient Cache countryCache; + + /** + * Default constructor + */ + public AddressbookCountryWebRequestBean () { + // Call super constructor + super(); + } + + /** + * Observing method when the event is fired that an administrator added a + * new country + *

+ * @param event Event instance + */ + public void afterAdminAddedCountryEvent (@Observes final ObservableAdminAddedCountryEvent event) { + // Is all valid? + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getAddedCountry() == null) { + // Throw again ... + throw new NullPointerException("event.addedCountry is null"); //NOI18N + } else if (event.getAddedCountry().getCountryId() == null) { + // And again ... + throw new NullPointerException("event.addedCountry.countryId is null"); //NOI18N + } else if (event.getAddedCountry().getCountryId() < 1) { + // Id is invalid + throw new IllegalArgumentException(MessageFormat.format("event.addedCountry.countryId={0} is not valid.", event.getAddedCountry().getCountryId())); //NOI18N + } + + // Add the event + this.countryCache.put(event.getAddedCountry().getCountryId(), event.getAddedCountry()); + } + + @Override + @SuppressWarnings ("ReturnOfCollectionOrArrayField") + public List allCountries () { + // Init list + List list = new LinkedList<>(); + + // Get iterator + Iterator> iterator = this.countryCache.iterator(); + + // Loop over all + while (iterator.hasNext()) { + // Get next entry + final Cache.Entry next = iterator.next(); + + // Add value to list + list.add(next.getValue()); + } + + // Return it + return list; + } + + /** + * Post-construction method + */ + @PostConstruct + public void init () { + // Is cache there? + if (!this.countryCache.iterator().hasNext()) { + // Get whole list + List list = this.countryBean.allCountries(); + + // Add all + for (final Iterator iterator = list.iterator(); iterator.hasNext();) { + // Get next element + final Country next = iterator.next(); + + // Add it to cache + this.countryCache.put(next.getCountryId(), next); + } + } + } + +} diff --git a/src/java/org/mxchange/addressbook/beans/country/AddressbookCountryWebRequestController.java b/src/java/org/mxchange/addressbook/beans/country/AddressbookCountryWebRequestController.java new file mode 100644 index 00000000..89fb0b6b --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/country/AddressbookCountryWebRequestController.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * 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.addressbook.beans.country; + +import java.io.Serializable; +import java.util.List; +import org.mxchange.jcountry.model.data.Country; + +/** + * An interface for country beans + *

+ * @author Roland Häder + */ +public interface AddressbookCountryWebRequestController extends Serializable { + + /** + * A list of all countries + *

+ * @return All countries + */ + List allCountries (); + +} diff --git a/src/java/org/mxchange/addressbook/beans/helper/AddressbookWebRequestHelperBean.java b/src/java/org/mxchange/addressbook/beans/helper/AddressbookWebRequestHelperBean.java index bdba46c2..72572c8c 100644 --- a/src/java/org/mxchange/addressbook/beans/helper/AddressbookWebRequestHelperBean.java +++ b/src/java/org/mxchange/addressbook/beans/helper/AddressbookWebRequestHelperBean.java @@ -25,7 +25,7 @@ import javax.inject.Named; import org.mxchange.addressbook.beans.contact.AddressbookAdminContactWebRequestController; import org.mxchange.addressbook.beans.phone.AddressbookAdminPhoneWebRequestController; import org.mxchange.addressbook.beans.user.AddressbookAdminUserWebRequestController; -import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController; +import org.mxchange.addressbook.beans.user.AddressbookUserWebRequestController; import org.mxchange.jcontacts.events.contact.created.CreatedContactEvent; import org.mxchange.jcontacts.events.contact.created.ObservableCreatedContactEvent; import org.mxchange.jcontacts.model.contact.Contact; @@ -136,7 +136,7 @@ public class AddressbookWebRequestHelperBean implements AddressbookWebRequestHel * Regular user controller */ @Inject - private AddressbookUserWebSessionController userController; + private AddressbookUserWebRequestController userController; /** * Event for when a user instance was created diff --git a/src/java/org/mxchange/addressbook/beans/mobileprovider/AddressbookMobileProviderWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/mobileprovider/AddressbookMobileProviderWebRequestBean.java index 7b34a89d..bf017338 100644 --- a/src/java/org/mxchange/addressbook/beans/mobileprovider/AddressbookMobileProviderWebRequestBean.java +++ b/src/java/org/mxchange/addressbook/beans/mobileprovider/AddressbookMobileProviderWebRequestBean.java @@ -17,11 +17,15 @@ package org.mxchange.addressbook.beans.mobileprovider; import java.text.MessageFormat; +import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import javax.annotation.PostConstruct; +import javax.cache.Cache; import javax.ejb.EJB; -import javax.enterprise.context.SessionScoped; +import javax.enterprise.context.RequestScoped; import javax.enterprise.event.Observes; +import javax.inject.Inject; import javax.inject.Named; import org.mxchange.addressbook.beans.BaseAddressbookController; import org.mxchange.jphone.events.mobileprovider.added.AdminAddedMobileProviderEvent; @@ -34,7 +38,7 @@ import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProviderSingl * @author Roland Häder */ @Named ("mobileProviderController") -@SessionScoped +@RequestScoped public class AddressbookMobileProviderWebRequestBean extends BaseAddressbookController implements AddressbookMobileProviderWebRequestController { /** @@ -45,13 +49,15 @@ public class AddressbookMobileProviderWebRequestBean extends BaseAddressbookCont /** * "Cached" list of mobile providers */ - private List mobileProviders; + @Inject + @Cached (cacheName = "mobileProviderCache") + private transient Cache mobileProviderCache; /** * Remote EJB for mobile providers (regular) */ @EJB (lookup = "java:global/addressbook-ejb/mobileprovider!org.mxchange.jphone.phonenumbers.mobileprovider.MobileProviderSingletonBeanRemote") - private MobileProviderSingletonBeanRemote mobileRemoteBean; + private MobileProviderSingletonBeanRemote mobileProviderBean; /** * Default constructor @@ -84,13 +90,29 @@ public class AddressbookMobileProviderWebRequestBean extends BaseAddressbookCont } // Add it to the list - this.mobileProviders.add(event.getAddedMobileProvider()); + this.mobileProviderCache.put(event.getAddedMobileProvider().getProviderId(), event.getAddedMobileProvider()); } @Override @SuppressWarnings ("ReturnOfCollectionOrArrayField") public List allMobileProviders () { - return this.mobileProviders; + // Init list + List list = new LinkedList<>(); + + // Get iterator + Iterator> iterator = this.mobileProviderCache.iterator(); + + // Loop over all + while (iterator.hasNext()) { + // Get next entry + final Cache.Entry next = iterator.next(); + + // Add value to list + list.add(next.getValue()); + } + + // Return it + return list; } /** @@ -98,8 +120,20 @@ public class AddressbookMobileProviderWebRequestBean extends BaseAddressbookCont */ @PostConstruct public void init () { - // Init list of mobile providers - this.mobileProviders = this.mobileRemoteBean.allMobileProviders(); + // Is cache there? + if (!this.mobileProviderCache.iterator().hasNext()) { + // Get whole list + List list = this.mobileProviderBean.allMobileProviders(); + + // Add all + for (final Iterator iterator = list.iterator(); iterator.hasNext();) { + // Get next element + final MobileProvider next = iterator.next(); + + // Add it to cache + this.mobileProviderCache.put(next.getProviderId(), next); + } + } } } diff --git a/src/java/org/mxchange/addressbook/beans/phone/AddressbookAdminPhoneWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/phone/AddressbookAdminPhoneWebRequestBean.java index 1425e38d..0ee37a8f 100644 --- a/src/java/org/mxchange/addressbook/beans/phone/AddressbookAdminPhoneWebRequestBean.java +++ b/src/java/org/mxchange/addressbook/beans/phone/AddressbookAdminPhoneWebRequestBean.java @@ -155,7 +155,7 @@ public class AddressbookAdminPhoneWebRequestBean extends BaseAddressbookControll * Generic hone controller */ @Inject - private AddressbookPhoneWebApplicationController phoneController; + private AddressbookPhoneWebRequestController phoneController; /** * Country (for dial prefix) diff --git a/src/java/org/mxchange/addressbook/beans/phone/AddressbookPhoneWebApplicationBean.java b/src/java/org/mxchange/addressbook/beans/phone/AddressbookPhoneWebApplicationBean.java deleted file mode 100644 index ab996357..00000000 --- a/src/java/org/mxchange/addressbook/beans/phone/AddressbookPhoneWebApplicationBean.java +++ /dev/null @@ -1,658 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * 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.addressbook.beans.phone; - -import java.text.MessageFormat; -import java.util.LinkedList; -import java.util.List; -import java.util.Objects; -import javax.annotation.PostConstruct; -import javax.ejb.EJB; -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.event.Observes; -import javax.inject.Named; -import org.mxchange.addressbook.beans.BaseAddressbookController; -import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent; -import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent; -import org.mxchange.jcontacts.events.fax.linked.ObservableAdminLinkedFaxNumberEvent; -import org.mxchange.jcontacts.events.landline.linked.ObservableAdminLinkedLandLineNumberEvent; -import org.mxchange.jcontacts.events.mobile.linked.ObservableAdminLinkedMobileNumberEvent; -import org.mxchange.jcontacts.model.contact.Contact; -import org.mxchange.jphone.events.fax.deleted.AdminDeletedFaxNumberEvent; -import org.mxchange.jphone.events.fax.updated.AdminUpdatedFaxNumberEvent; -import org.mxchange.jphone.events.landline.deleted.AdminDeletedLandLineNumberEvent; -import org.mxchange.jphone.events.landline.updated.AdminUpdatedLandLineNumberEvent; -import org.mxchange.jphone.events.mobile.deleted.AdminDeletedMobileNumberEvent; -import org.mxchange.jphone.events.mobile.updated.AdminUpdatedMobileNumberEvent; -import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber; -import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber; -import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber; -import org.mxchange.jphone.model.phonenumbers.phone.PhoneSessionBeanRemote; -import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent; - -/** - * Regular controller (bean) for phone numbers - *

- * @author Roland Häder - */ -@Named ("phoneController") -@ApplicationScoped -public class AddressbookPhoneWebApplicationBean extends BaseAddressbookController implements AddressbookPhoneWebApplicationController { - - /** - * Serial number - */ - private static final long serialVersionUID = 491_058_674_675_690_105L; - - /** - * All fax numbers - */ - private final List faxNumbers; - - /** - * All land-line numbers - */ - private final List landLineNumbers; - - /** - * All mobile numbers - */ - private final List mobileNumbers; - - /** - * General EJB for phone numbers - */ - @EJB (lookup = "java:global/addressbook-ejb/phone!org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote") - private PhoneSessionBeanRemote phoneBean; - - /** - * Default constructor - */ - public AddressbookPhoneWebApplicationBean () { - // Call super constructor - super(); - - // Init all lists - this.mobileNumbers = new LinkedList<>(); - this.faxNumbers = new LinkedList<>(); - this.landLineNumbers = new LinkedList<>(); - } - - /** - * Observes events being fired when an administrator has added a new - * contact. - *

- * @param event Event being fired - */ - public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent 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 - } - - // Update contact's mobile, land-line and fax number - this.updateContactPhoneNumbers(event.getAddedContact()); - - // Clear this bean - this.clear(); - } - - /** - * Event observer for newly added users by administrator - *

- * @param event Event being fired - */ - public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent 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 - } - - // Update contact's mobile, land-line and fax number - this.updateContactPhoneNumbers(event.getAddedUser().getUserContact()); - - // Clear all data - this.clear(); - } - - /** - * Observes events being fired when an administrator has deleted a fax - * number - *

- * @param event Event being fired - */ - public void afterAdminDeletedFaxNumberEvent (@Observes final AdminDeletedFaxNumberEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getDeletedFaxNumber() == null) { - // Throw NPE again - throw new NullPointerException("event.deletedFaxNumber is null"); //NOI18N - } else if (event.getDeletedFaxNumber().getPhoneId() == null) { - // userId is null - throw new NullPointerException("event.deletedFaxNumber.phoneId is null"); //NOI18N - } else if (event.getDeletedFaxNumber().getPhoneId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getDeletedFaxNumber(), event.getDeletedFaxNumber().getPhoneId())); //NOI18N - } - - // Update contact's mobile, land-line and fax number - this.allFaxNumbers().remove(event.getDeletedFaxNumber()); - - // Clear all data - this.clear(); - } - - /** - * Observes events being fired when an administrator has deleted a land-line - * number - *

- * @param event Event being fired - */ - public void afterAdminDeletedLandLineNumberEvent (@Observes final AdminDeletedLandLineNumberEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getDeletedLandLineNumber() == null) { - // Throw NPE again - throw new NullPointerException("event.deletedLandLineNumber is null"); //NOI18N - } else if (event.getDeletedLandLineNumber().getPhoneId() == null) { - // userId is null - throw new NullPointerException("event.deletedLandLineNumber.phoneId is null"); //NOI18N - } else if (event.getDeletedLandLineNumber().getPhoneId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getDeletedLandLineNumber(), event.getDeletedLandLineNumber().getPhoneId())); //NOI18N - } - - // Update contact's mobile, land-line and fax number - this.allLandLineNumbers().remove(event.getDeletedLandLineNumber()); - - // Clear all data - this.clear(); - } - - /** - * Observes events being fired when an administrator has deleted a mobile - * number - *

- * @param event Event being fired - */ - public void afterAdminDeletedMobileNumberEvent (@Observes final AdminDeletedMobileNumberEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getDeletedMobileNumber() == null) { - // Throw NPE again - throw new NullPointerException("event.deletedMobileNumber is null"); //NOI18N - } else if (event.getDeletedMobileNumber().getPhoneId() == null) { - // userId is null - throw new NullPointerException("event.deletedMobileNumber.phoneId is null"); //NOI18N - } else if (event.getDeletedMobileNumber().getPhoneId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getDeletedMobileNumber(), event.getDeletedMobileNumber().getPhoneId())); //NOI18N - } - - // Update contact's mobile, land-line and fax number - this.allMobileNumbers().remove(event.getDeletedMobileNumber()); - - // Clear all data - this.clear(); - } - - /** - * Observes events being fired when an administrator has a linked a fax - * number - *

- * @param event Event being fired - */ - public void afterAdminLinkedFaxNumberEvent (@Observes final ObservableAdminLinkedFaxNumberEvent event) { - // Is the event fine? - if (event == null) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getContact() == null) { - // Throw again ... - throw new NullPointerException("event.contact is null"); - } else if (event.getContact().getContactId() == null) { - // Throw again ... - throw new NullPointerException("event.contact.contactId is null"); - } else if (event.getContact().getContactId() < 1) { - // Throw again ... - throw new NullPointerException("event.contact.contactId=" + event.getContact().getContactId() + " is invalid"); - } else if (event.getContact().getContactFaxNumber() == null) { - // Throw again ... - throw new NullPointerException("event.contact.contactFaxNumber is null"); - } else if (event.getContact().getContactFaxNumber().getPhoneId() == null) { - // Throw again ... - throw new NullPointerException("event.contact.contactFaxNumber.phoneId is null"); - } else if (event.getContact().getContactFaxNumber().getPhoneId() < 1) { - // Throw again ... - throw new NullPointerException("event.contact.contactFaxNumber.phoneId=" + event.getContact().getContactFaxNumber().getPhoneId() + " is invalid"); - } else if (event.getLinkedFaxNumber() == null) { - // Throw again ... - throw new NullPointerException("event.linkedFaxNumer is null"); - } - - // Is the id number in linked number not set? - if (event.getLinkedFaxNumber().getPhoneId() == null) { - // Then it is a new number, so add it from contact as there the id number has been set - this.uniqueAddFaxNumber(event.getContact().getContactFaxNumber()); - } - } - - /** - * Observes events being fired when an administrator has a linked a - * land-line number - *

- * @param event Event being fired - */ - public void afterAdminLinkedLandLineNumberEvent (@Observes final ObservableAdminLinkedLandLineNumberEvent event) { - // Is the event fine? - if (event == null) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getContact() == null) { - // Throw again ... - throw new NullPointerException("event.contact is null"); - } else if (event.getContact().getContactId() == null) { - // Throw again ... - throw new NullPointerException("event.contact.contactId is null"); - } else if (event.getContact().getContactId() < 1) { - // Throw again ... - throw new NullPointerException("event.contact.contactId=" + event.getContact().getContactId() + " is invalid"); - } else if (event.getContact().getContactLandLineNumber() == null) { - // Throw again ... - throw new NullPointerException("event.contact.contactLandLineNumber is null"); - } else if (event.getContact().getContactLandLineNumber().getPhoneId() == null) { - // Throw again ... - throw new NullPointerException("event.contact.contactLandLineNumber.phoneId is null"); - } else if (event.getContact().getContactLandLineNumber().getPhoneId() < 1) { - // Throw again ... - throw new NullPointerException("event.contact.contactLandLineNumber.phoneId=" + event.getContact().getContactLandLineNumber().getPhoneId() + " is invalid"); - } else if (event.getLinkedLandLineNumber() == null) { - // Throw again ... - throw new NullPointerException("event.linkedLandLineNumer is null"); - } - - // Is the id number in linked number not set? - if (event.getLinkedLandLineNumber().getPhoneId() == null) { - // Then it is a new number, so add it from contact as there the id number has been set - this.uniqueAddLandLineNumber(event.getContact().getContactLandLineNumber()); - } - } - - /** - * Observes events being fired when an administrator has a linked a mobile - * number - *

- * @param event Event being fired - */ - public void afterAdminLinkedMobileNumberEvent (@Observes final ObservableAdminLinkedMobileNumberEvent event) { - // Is the event fine? - if (event == null) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getContact() == null) { - // Throw again ... - throw new NullPointerException("event.contact is null"); - } else if (event.getContact().getContactId() == null) { - // Throw again ... - throw new NullPointerException("event.contact.contactId is null"); - } else if (event.getContact().getContactId() < 1) { - // Throw again ... - throw new NullPointerException("event.contact.contactId=" + event.getContact().getContactId() + " is invalid"); - } else if (event.getContact().getContactMobileNumber() == null) { - // Throw again ... - throw new NullPointerException("event.contact.contactMobileNumber is null"); - } else if (event.getContact().getContactMobileNumber().getPhoneId() == null) { - // Throw again ... - throw new NullPointerException("event.contact.contactMobileNumber.phoneId is null"); - } else if (event.getContact().getContactMobileNumber().getPhoneId() < 1) { - // Throw again ... - throw new NullPointerException("event.contact.contactMobileNumber.phoneId=" + event.getContact().getContactMobileNumber().getPhoneId() + " is invalid"); - } else if (event.getLinkedMobileNumber() == null) { - // Throw again ... - throw new NullPointerException("event.linkedMobileNumer is null"); - } - - // Is the id number in linked number not set? - if (event.getLinkedMobileNumber().getPhoneId() == null) { - // Then it is a new number, so add it from contact as there the id number has been set - this.uniqueAddMobileNumber(event.getContact().getContactMobileNumber()); - } - } - - /** - * Observes events being fired when an administrator has updated contact - * data. - *

- * @param event Event being fired - */ - public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent 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 - } - - // Update contact's mobile, land-line and fax number - this.updateContactPhoneNumbers(event.getUpdatedContact()); - - // Clear all data - this.clear(); - } - - /** - * Observes events being fired when an administrator has updated a fax - * number. - *

- * @param event Event being fired - */ - public void afterAdminUpdatedFaxNumberEvent (@Observes final AdminUpdatedFaxNumberEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getUpdatedFaxNumber() == null) { - // Throw NPE again - throw new NullPointerException("event.updatedFaxNumber is null"); //NOI18N - } else if (event.getUpdatedFaxNumber().getPhoneId() == null) { - // userId is null - throw new NullPointerException("event.updatedFaxNumber.phoneId is null"); //NOI18N - } else if (event.getUpdatedFaxNumber().getPhoneId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getUpdatedFaxNumber(), event.getUpdatedFaxNumber().getPhoneId())); //NOI18N - } - - // Uniquely add it - this.uniqueAddFaxNumber(event.getUpdatedFaxNumber()); - - // Clear it - this.clear(); - } - - /** - * Observes events being fired when an administrator has updated a land-line - * number. - *

- * @param event Event being fired - */ - public void afterAdminUpdatedLandLineNumberEvent (@Observes final AdminUpdatedLandLineNumberEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getUpdatedLandLineNumber() == null) { - // Throw NPE again - throw new NullPointerException("event.updatedLandLineNumber is null"); //NOI18N - } else if (event.getUpdatedLandLineNumber().getPhoneId() == null) { - // userId is null - throw new NullPointerException("event.updatedLandLineNumber.phoneId is null"); //NOI18N - } else if (event.getUpdatedLandLineNumber().getPhoneId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getUpdatedLandLineNumber(), event.getUpdatedLandLineNumber().getPhoneId())); //NOI18N - } - - // Uniquely add it - this.uniqueAddLandLineNumber(event.getUpdatedLandLineNumber()); - - // Clear it - this.clear(); - } - - /** - * Observes events being fired when an administrator has updated a mobile - * number. - *

- * @param event Event being fired - */ - public void afterAdminUpdatedMobileNumberEvent (@Observes final AdminUpdatedMobileNumberEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getUpdatedMobileNumber() == null) { - // Throw NPE again - throw new NullPointerException("event.updatedMobileNumber is null"); //NOI18N - } else if (event.getUpdatedMobileNumber().getPhoneId() == null) { - // userId is null - throw new NullPointerException("event.updatedMobileNumber.phoneId is null"); //NOI18N - } else if (event.getUpdatedMobileNumber().getPhoneId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getUpdatedMobileNumber(), event.getUpdatedMobileNumber().getPhoneId())); //NOI18N - } - - // Uniquely add it - this.uniqueAddMobileNumber(event.getUpdatedMobileNumber()); - - // Clear it - this.clear(); - } - - @Override - @SuppressWarnings ("ReturnOfCollectionOrArrayField") - public List allFaxNumbers () { - return this.faxNumbers; - } - - @Override - @SuppressWarnings ("ReturnOfCollectionOrArrayField") - public List allLandLineNumbers () { - return this.landLineNumbers; - } - - @Override - @SuppressWarnings ("ReturnOfCollectionOrArrayField") - public List allMobileNumbers () { - return this.mobileNumbers; - } - - /** - * Post-construction method - */ - @PostConstruct - public void init () { - // All phone numbers - this.allMobileNumbers().addAll(this.phoneBean.allMobileNumbers()); - this.allFaxNumbers().addAll(this.phoneBean.allFaxNumbers()); - this.allLandLineNumbers().addAll(this.phoneBean.allLandLineNumbers()); - } - - /** - * Clears this bean - */ - private void clear () { - // Clear all data - } - - /** - * Uniquely add given fax number to this bean's list. First remove the old - * instance (by id number), then re-add it again. - *

- * @param faxNumber number to add - */ - private void uniqueAddFaxNumber (final DialableFaxNumber faxNumber) { - // Make sure the parameter is valid - if (null == faxNumber) { - // Throw NPE - throw new NullPointerException("faxNumber is null"); - } else if (faxNumber.getPhoneId() == null) { - // Throw again ... - throw new NullPointerException("faxNumber.phoneId is null"); - } else if (faxNumber.getPhoneId() < 1) { - // Not valid - throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneId={0} is not valid.", faxNumber.getPhoneId())); - } - - // First remove it - if (!this.allFaxNumbers().remove(faxNumber)) { - // Did not work, try by id number - for (final DialableFaxNumber fax : this.allFaxNumbers()) { - // Is id number the same? - if (Objects.equals(fax.getPhoneId(), faxNumber.getPhoneId())) { - // Found it - this.allFaxNumbers().remove(fax); - break; - } - } - } - - // ... then add it - this.allFaxNumbers().add(faxNumber); - } - - /** - * Uniquely add given land-line number to this bean's list. First remove the - * old instance (by id number), then re-add it again. - *

- * @param landLineNumber Land-line number to add - */ - private void uniqueAddLandLineNumber (final DialableLandLineNumber landLineNumber) { - // Make sure the parameter is valid - if (null == landLineNumber) { - // Throw NPE - throw new NullPointerException("landLineNumber is null"); - } else if (landLineNumber.getPhoneId() == null) { - // Throw again ... - throw new NullPointerException("landLineNumber.phoneId is null"); - } else if (landLineNumber.getPhoneId() < 1) { - // Not valid - throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneId={0} is not valid.", landLineNumber.getPhoneId())); - } - - // First remove it - if (!this.allLandLineNumbers().remove(landLineNumber)) { - // Did not work, try by id number - for (final DialableLandLineNumber landLine : this.allLandLineNumbers()) { - // Is id number the same? - if (Objects.equals(landLine.getPhoneId(), landLineNumber.getPhoneId())) { - // Found it - this.allLandLineNumbers().remove(landLine); - break; - } - } - } - - // ... then add it - this.allLandLineNumbers().add(landLineNumber); - } - - /** - * Uniquely add given mobile number to this bean's list. First remove the - * old instance (by id number), then re-add it again. - *

- * @param mobileNumber Mobile number to add - */ - private void uniqueAddMobileNumber (final DialableMobileNumber mobileNumber) { - // Make sure the parameter is valid - if (null == mobileNumber) { - // Throw NPE - throw new NullPointerException("mobileNumber is null"); - } else if (mobileNumber.getPhoneId() == null) { - // Throw again ... - throw new NullPointerException("mobileNumber.phoneId is null"); - } else if (mobileNumber.getPhoneId() < 1) { - // Not valid - throw new IllegalArgumentException(MessageFormat.format("mobileNumber.phoneId={0} is not valid.", mobileNumber.getPhoneId())); - } - - // First remove it by object - if (!this.allMobileNumbers().remove(mobileNumber)) { - // Did not work, try by id number - for (final DialableMobileNumber cell : this.allMobileNumbers()) { - // Is id number the same? - if (Objects.equals(cell.getPhoneId(), mobileNumber.getPhoneId())) { - // Found it - this.allMobileNumbers().remove(cell); - break; - } - } - } - - // ... then add it - this.allMobileNumbers().add(mobileNumber); - } - - /** - * Updates given contact's mobile, land-line and fax number - *

- * @param contact Contact instance - */ - private void updateContactPhoneNumbers (final Contact contact) { - // Parameter must be valid - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); - } else if (contact.getContactId() == null) { - // Throw again - throw new NullPointerException("contact.contactId is null"); - } else if (contact.getContactId() < 1) { - // Id number is not valid - } - - // Is mobile set? - if (contact.getContactMobileNumber() instanceof DialableMobileNumber) { - // Unique-add it - this.uniqueAddMobileNumber(contact.getContactMobileNumber()); - } - - // Is land-line set? - if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) { - // Unique-add it - this.uniqueAddLandLineNumber(contact.getContactLandLineNumber()); - } - - // Is fax set? - if (contact.getContactFaxNumber() instanceof DialableFaxNumber) { - // Unique-add it - this.uniqueAddFaxNumber(contact.getContactFaxNumber()); - } - } - -} diff --git a/src/java/org/mxchange/addressbook/beans/phone/AddressbookPhoneWebApplicationController.java b/src/java/org/mxchange/addressbook/beans/phone/AddressbookPhoneWebApplicationController.java deleted file mode 100644 index 2d2fb587..00000000 --- a/src/java/org/mxchange/addressbook/beans/phone/AddressbookPhoneWebApplicationController.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * 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.addressbook.beans.phone; - -import java.io.Serializable; -import java.util.List; -import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber; -import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber; -import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber; - -/** - * An interface for a request web controller (bean) for administrative phone - * number purposes. - *

- * @author Roland Häder - */ -public interface AddressbookPhoneWebApplicationController extends Serializable { - - /** - * Returns a list of all mobile numbers. For performance reasons, the - * controller (bean) should be application-scoped as from user to user - * nothing changes. And the controller's post-construct method should load - * all numbers and cache it in the controller. - *

- * @return List of all mobile numbers - */ - List allMobileNumbers (); - - /** - * Returns a list of all fax numbers. For performance reasons, the - * controller (bean) should be application-scoped as from user to user - * nothing changes. And the controller's post-construct method should load - * all numbers and cache it in the controller. - *

- * @return List of all fax numbers - */ - List allFaxNumbers (); - - /** - * Returns a list of all land-line numbers. For performance reasons, the - * controller (bean) should be application-scoped as from user to user - * nothing changes. And the controller's post-construct method should load - * all numbers and cache it in the controller. - *

- * @return List of all land-line numbers - */ - List allLandLineNumbers (); - -} diff --git a/src/java/org/mxchange/addressbook/beans/phone/AddressbookPhoneWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/phone/AddressbookPhoneWebRequestBean.java new file mode 100644 index 00000000..002b0cc1 --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/phone/AddressbookPhoneWebRequestBean.java @@ -0,0 +1,749 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * 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.addressbook.beans.phone; + +import java.text.MessageFormat; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; +import javax.annotation.PostConstruct; +import javax.ejb.EJB; +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.event.Observes; +import javax.inject.Inject; +import javax.inject.Named; +import org.mxchange.addressbook.beans.BaseAddressbookController; +import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent; +import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent; +import org.mxchange.jcontacts.events.fax.linked.ObservableAdminLinkedFaxNumberEvent; +import org.mxchange.jcontacts.events.landline.linked.ObservableAdminLinkedLandLineNumberEvent; +import org.mxchange.jcontacts.events.mobile.linked.ObservableAdminLinkedMobileNumberEvent; +import org.mxchange.jcontacts.model.contact.Contact; +import org.mxchange.jphone.events.fax.deleted.AdminDeletedFaxNumberEvent; +import org.mxchange.jphone.events.fax.updated.AdminUpdatedFaxNumberEvent; +import org.mxchange.jphone.events.landline.deleted.AdminDeletedLandLineNumberEvent; +import org.mxchange.jphone.events.landline.updated.AdminUpdatedLandLineNumberEvent; +import org.mxchange.jphone.events.mobile.deleted.AdminDeletedMobileNumberEvent; +import org.mxchange.jphone.events.mobile.updated.AdminUpdatedMobileNumberEvent; +import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber; +import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber; +import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber; +import org.mxchange.jphone.model.phonenumbers.phone.PhoneSessionBeanRemote; +import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent; + +/** + * Regular controller (bean) for phone numbers + *

+ * @author Roland Häder + */ +@Named ("phoneController") +@ApplicationScoped +public class AddressbookPhoneWebRequestBean extends BaseAddressbookController implements AddressbookPhoneWebRequestController { + + /** + * Serial number + */ + private static final long serialVersionUID = 491_058_674_675_690_105L; + + /** + * All fax numbers + */ + @Inject + @Cached(cacheName = "faxNumberCache") + private transient Cache faxNumberCache; + + /** + * All land-line numbers + */ + @Inject + @Cached(cacheName = "landLineNumberCache") + private transient Cache landLineNumberCache; + + /** + * All mobile numbers + */ + @Inject + @Cached(cacheName = "mobileNumberCache") + private transient Cache mobileNumberCache; + + /** + * General EJB for phone numbers + */ + @EJB (lookup = "java:global/addressbook-ejb/phone!org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote") + private PhoneSessionBeanRemote phoneBean; + + /** + * Default constructor + */ + public AddressbookPhoneWebRequestBean () { + // Call super constructor + super(); + } + + /** + * Observes events being fired when an administrator has added a new + * contact. + *

+ * @param event Event being fired + */ + public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent 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 + } + + // Update contact's mobile, land-line and fax number + this.updateContactPhoneNumbers(event.getAddedContact()); + + // Clear this bean + this.clear(); + } + + /** + * Event observer for newly added users by administrator + *

+ * @param event Event being fired + */ + public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent 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 + } + + // Update contact's mobile, land-line and fax number + this.updateContactPhoneNumbers(event.getAddedUser().getUserContact()); + + // Clear all data + this.clear(); + } + + /** + * Observes events being fired when an administrator has deleted a fax + * number + *

+ * @param event Event being fired + */ + public void afterAdminDeletedFaxNumberEvent (@Observes final AdminDeletedFaxNumberEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getDeletedFaxNumber() == null) { + // Throw NPE again + throw new NullPointerException("event.deletedFaxNumber is null"); //NOI18N + } else if (event.getDeletedFaxNumber().getPhoneId() == null) { + // userId is null + throw new NullPointerException("event.deletedFaxNumber.phoneId is null"); //NOI18N + } else if (event.getDeletedFaxNumber().getPhoneId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getDeletedFaxNumber(), event.getDeletedFaxNumber().getPhoneId())); //NOI18N + } + + // Update contact's mobile, land-line and fax number + this.allFaxNumbers().remove(event.getDeletedFaxNumber()); + + // Clear all data + this.clear(); + } + + /** + * Observes events being fired when an administrator has deleted a land-line + * number + *

+ * @param event Event being fired + */ + public void afterAdminDeletedLandLineNumberEvent (@Observes final AdminDeletedLandLineNumberEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getDeletedLandLineNumber() == null) { + // Throw NPE again + throw new NullPointerException("event.deletedLandLineNumber is null"); //NOI18N + } else if (event.getDeletedLandLineNumber().getPhoneId() == null) { + // userId is null + throw new NullPointerException("event.deletedLandLineNumber.phoneId is null"); //NOI18N + } else if (event.getDeletedLandLineNumber().getPhoneId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getDeletedLandLineNumber(), event.getDeletedLandLineNumber().getPhoneId())); //NOI18N + } + + // Update contact's mobile, land-line and fax number + this.allLandLineNumbers().remove(event.getDeletedLandLineNumber()); + + // Clear all data + this.clear(); + } + + /** + * Observes events being fired when an administrator has deleted a mobile + * number + *

+ * @param event Event being fired + */ + public void afterAdminDeletedMobileNumberEvent (@Observes final AdminDeletedMobileNumberEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getDeletedMobileNumber() == null) { + // Throw NPE again + throw new NullPointerException("event.deletedMobileNumber is null"); //NOI18N + } else if (event.getDeletedMobileNumber().getPhoneId() == null) { + // userId is null + throw new NullPointerException("event.deletedMobileNumber.phoneId is null"); //NOI18N + } else if (event.getDeletedMobileNumber().getPhoneId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getDeletedMobileNumber(), event.getDeletedMobileNumber().getPhoneId())); //NOI18N + } + + // Update contact's mobile, land-line and fax number + this.allMobileNumbers().remove(event.getDeletedMobileNumber()); + + // Clear all data + this.clear(); + } + + /** + * Observes events being fired when an administrator has a linked a fax + * number + *

+ * @param event Event being fired + */ + public void afterAdminLinkedFaxNumberEvent (@Observes final ObservableAdminLinkedFaxNumberEvent event) { + // Is the event fine? + if (event == null) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getContact() == null) { + // Throw again ... + throw new NullPointerException("event.contact is null"); + } else if (event.getContact().getContactId() == null) { + // Throw again ... + throw new NullPointerException("event.contact.contactId is null"); + } else if (event.getContact().getContactId() < 1) { + // Throw again ... + throw new NullPointerException("event.contact.contactId=" + event.getContact().getContactId() + " is invalid"); + } else if (event.getContact().getContactFaxNumber() == null) { + // Throw again ... + throw new NullPointerException("event.contact.contactFaxNumber is null"); + } else if (event.getContact().getContactFaxNumber().getPhoneId() == null) { + // Throw again ... + throw new NullPointerException("event.contact.contactFaxNumber.phoneId is null"); + } else if (event.getContact().getContactFaxNumber().getPhoneId() < 1) { + // Throw again ... + throw new NullPointerException("event.contact.contactFaxNumber.phoneId=" + event.getContact().getContactFaxNumber().getPhoneId() + " is invalid"); + } else if (event.getLinkedFaxNumber() == null) { + // Throw again ... + throw new NullPointerException("event.linkedFaxNumer is null"); + } + + // Is the id number in linked number not set? + if (event.getLinkedFaxNumber().getPhoneId() == null) { + // Then it is a new number, so add it from contact as there the id number has been set + this.uniqueAddFaxNumber(event.getContact().getContactFaxNumber()); + } + } + + /** + * Observes events being fired when an administrator has a linked a + * land-line number + *

+ * @param event Event being fired + */ + public void afterAdminLinkedLandLineNumberEvent (@Observes final ObservableAdminLinkedLandLineNumberEvent event) { + // Is the event fine? + if (event == null) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getContact() == null) { + // Throw again ... + throw new NullPointerException("event.contact is null"); + } else if (event.getContact().getContactId() == null) { + // Throw again ... + throw new NullPointerException("event.contact.contactId is null"); + } else if (event.getContact().getContactId() < 1) { + // Throw again ... + throw new NullPointerException("event.contact.contactId=" + event.getContact().getContactId() + " is invalid"); + } else if (event.getContact().getContactLandLineNumber() == null) { + // Throw again ... + throw new NullPointerException("event.contact.contactLandLineNumber is null"); + } else if (event.getContact().getContactLandLineNumber().getPhoneId() == null) { + // Throw again ... + throw new NullPointerException("event.contact.contactLandLineNumber.phoneId is null"); + } else if (event.getContact().getContactLandLineNumber().getPhoneId() < 1) { + // Throw again ... + throw new NullPointerException("event.contact.contactLandLineNumber.phoneId=" + event.getContact().getContactLandLineNumber().getPhoneId() + " is invalid"); + } else if (event.getLinkedLandLineNumber() == null) { + // Throw again ... + throw new NullPointerException("event.linkedLandLineNumer is null"); + } + + // Is the id number in linked number not set? + if (event.getLinkedLandLineNumber().getPhoneId() == null) { + // Then it is a new number, so add it from contact as there the id number has been set + this.uniqueAddLandLineNumber(event.getContact().getContactLandLineNumber()); + } + } + + /** + * Observes events being fired when an administrator has a linked a mobile + * number + *

+ * @param event Event being fired + */ + public void afterAdminLinkedMobileNumberEvent (@Observes final ObservableAdminLinkedMobileNumberEvent event) { + // Is the event fine? + if (event == null) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getContact() == null) { + // Throw again ... + throw new NullPointerException("event.contact is null"); + } else if (event.getContact().getContactId() == null) { + // Throw again ... + throw new NullPointerException("event.contact.contactId is null"); + } else if (event.getContact().getContactId() < 1) { + // Throw again ... + throw new NullPointerException("event.contact.contactId=" + event.getContact().getContactId() + " is invalid"); + } else if (event.getContact().getContactMobileNumber() == null) { + // Throw again ... + throw new NullPointerException("event.contact.contactMobileNumber is null"); + } else if (event.getContact().getContactMobileNumber().getPhoneId() == null) { + // Throw again ... + throw new NullPointerException("event.contact.contactMobileNumber.phoneId is null"); + } else if (event.getContact().getContactMobileNumber().getPhoneId() < 1) { + // Throw again ... + throw new NullPointerException("event.contact.contactMobileNumber.phoneId=" + event.getContact().getContactMobileNumber().getPhoneId() + " is invalid"); + } else if (event.getLinkedMobileNumber() == null) { + // Throw again ... + throw new NullPointerException("event.linkedMobileNumer is null"); + } + + // Is the id number in linked number not set? + if (event.getLinkedMobileNumber().getPhoneId() == null) { + // Then it is a new number, so add it from contact as there the id number has been set + this.uniqueAddMobileNumber(event.getContact().getContactMobileNumber()); + } + } + + /** + * Observes events being fired when an administrator has updated contact + * data. + *

+ * @param event Event being fired + */ + public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent 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 + } + + // Update contact's mobile, land-line and fax number + this.updateContactPhoneNumbers(event.getUpdatedContact()); + + // Clear all data + this.clear(); + } + + /** + * Observes events being fired when an administrator has updated a fax + * number. + *

+ * @param event Event being fired + */ + public void afterAdminUpdatedFaxNumberEvent (@Observes final AdminUpdatedFaxNumberEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getUpdatedFaxNumber() == null) { + // Throw NPE again + throw new NullPointerException("event.updatedFaxNumber is null"); //NOI18N + } else if (event.getUpdatedFaxNumber().getPhoneId() == null) { + // userId is null + throw new NullPointerException("event.updatedFaxNumber.phoneId is null"); //NOI18N + } else if (event.getUpdatedFaxNumber().getPhoneId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getUpdatedFaxNumber(), event.getUpdatedFaxNumber().getPhoneId())); //NOI18N + } + + // Uniquely add it + this.uniqueAddFaxNumber(event.getUpdatedFaxNumber()); + + // Clear it + this.clear(); + } + + /** + * Observes events being fired when an administrator has updated a land-line + * number. + *

+ * @param event Event being fired + */ + public void afterAdminUpdatedLandLineNumberEvent (@Observes final AdminUpdatedLandLineNumberEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getUpdatedLandLineNumber() == null) { + // Throw NPE again + throw new NullPointerException("event.updatedLandLineNumber is null"); //NOI18N + } else if (event.getUpdatedLandLineNumber().getPhoneId() == null) { + // userId is null + throw new NullPointerException("event.updatedLandLineNumber.phoneId is null"); //NOI18N + } else if (event.getUpdatedLandLineNumber().getPhoneId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getUpdatedLandLineNumber(), event.getUpdatedLandLineNumber().getPhoneId())); //NOI18N + } + + // Uniquely add it + this.uniqueAddLandLineNumber(event.getUpdatedLandLineNumber()); + + // Clear it + this.clear(); + } + + /** + * Observes events being fired when an administrator has updated a mobile + * number. + *

+ * @param event Event being fired + */ + public void afterAdminUpdatedMobileNumberEvent (@Observes final AdminUpdatedMobileNumberEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getUpdatedMobileNumber() == null) { + // Throw NPE again + throw new NullPointerException("event.updatedMobileNumber is null"); //NOI18N + } else if (event.getUpdatedMobileNumber().getPhoneId() == null) { + // userId is null + throw new NullPointerException("event.updatedMobileNumber.phoneId is null"); //NOI18N + } else if (event.getUpdatedMobileNumber().getPhoneId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getUpdatedMobileNumber(), event.getUpdatedMobileNumber().getPhoneId())); //NOI18N + } + + // Uniquely add it + this.uniqueAddMobileNumber(event.getUpdatedMobileNumber()); + + // Clear it + this.clear(); + } + + @Override + @SuppressWarnings ("ReturnOfCollectionOrArrayField") + public List allFaxNumbers () { + // Init list + List list = new LinkedList<>(); + + // Get iterator + Iterator> iterator = this.faxNumberCache.iterator(); + + // Loop over all + while (iterator.hasNext()) { + // Get next entry + final Cache.Entry next = iterator.next(); + + // Add value to list + list.add(next.getValue()); + } + + // Return it + return list; + } + + @Override + @SuppressWarnings ("ReturnOfCollectionOrArrayField") + public List allLandLineNumbers () { + // Init list + List list = new LinkedList<>(); + + // Get iterator + Iterator> iterator = this.landLineNumberCache.iterator(); + + // Loop over all + while (iterator.hasNext()) { + // Get next entry + final Cache.Entry next = iterator.next(); + + // Add value to list + list.add(next.getValue()); + } + + // Return it + return list; + } + + @Override + @SuppressWarnings ("ReturnOfCollectionOrArrayField") + public List allMobileNumbers () { + // Init list + List list = new LinkedList<>(); + + // Get iterator + Iterator> iterator = this.mobileNumberCache.iterator(); + + // Loop over all + while (iterator.hasNext()) { + // Get next entry + final Cache.Entry next = iterator.next(); + + // Add value to list + list.add(next.getValue()); + } + + // Return it + return list; + } + + /** + * Post-construction method + */ + @PostConstruct + public void init () { + // Is cache there? + if (!this.faxNumberCache.iterator().hasNext()) { + // Get whole list + List list = this.phoneBean.allFaxNumbers(); + + // Add all + for (final Iterator iterator = list.iterator(); iterator.hasNext();) { + // Get next element + final DialableFaxNumber next = iterator.next(); + + // Add it to cache + this.faxNumberCache.put(next.getPhoneId(), next); + } + } + + // Is cache there? + if (!this.landLineNumberCache.iterator().hasNext()) { + // Get whole list + List list = this.phoneBean.allLandLineNumbers(); + + // Add all + for (final Iterator iterator = list.iterator(); iterator.hasNext();) { + // Get next element + final DialableLandLineNumber next = iterator.next(); + + // Add it to cache + this.landLineNumberCache.put(next.getPhoneId(), next); + } + } + + // Is cache there? + if (!this.mobileNumberCache.iterator().hasNext()) { + // Get whole list + List list = this.phoneBean.allMobileNumbers(); + + // Add all + for (final Iterator iterator = list.iterator(); iterator.hasNext();) { + // Get next element + final DialableMobileNumber next = iterator.next(); + + // Add it to cache + this.mobileNumberCache.put(next.getPhoneId(), next); + } + } + } + + /** + * Clears this bean + */ + private void clear () { + // Clear all data + } + + /** + * Uniquely add given fax number to this bean's list. First remove the old + * instance (by id number), then re-add it again. + *

+ * @param faxNumber number to add + */ + private void uniqueAddFaxNumber (final DialableFaxNumber faxNumber) { + // Make sure the parameter is valid + if (null == faxNumber) { + // Throw NPE + throw new NullPointerException("faxNumber is null"); + } else if (faxNumber.getPhoneId() == null) { + // Throw again ... + throw new NullPointerException("faxNumber.phoneId is null"); + } else if (faxNumber.getPhoneId() < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneId={0} is not valid.", faxNumber.getPhoneId())); + } + + // First remove it + if (!this.allFaxNumbers().remove(faxNumber)) { + // Did not work, try by id number + for (final DialableFaxNumber fax : this.allFaxNumbers()) { + // Is id number the same? + if (Objects.equals(fax.getPhoneId(), faxNumber.getPhoneId())) { + // Found it + this.allFaxNumbers().remove(fax); + break; + } + } + } + + // ... then add it + this.allFaxNumbers().add(faxNumber); + } + + /** + * Uniquely add given land-line number to this bean's list. First remove the + * old instance (by id number), then re-add it again. + *

+ * @param landLineNumber Land-line number to add + */ + private void uniqueAddLandLineNumber (final DialableLandLineNumber landLineNumber) { + // Make sure the parameter is valid + if (null == landLineNumber) { + // Throw NPE + throw new NullPointerException("landLineNumber is null"); + } else if (landLineNumber.getPhoneId() == null) { + // Throw again ... + throw new NullPointerException("landLineNumber.phoneId is null"); + } else if (landLineNumber.getPhoneId() < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneId={0} is not valid.", landLineNumber.getPhoneId())); + } + + // First remove it + if (!this.allLandLineNumbers().remove(landLineNumber)) { + // Did not work, try by id number + for (final DialableLandLineNumber landLine : this.allLandLineNumbers()) { + // Is id number the same? + if (Objects.equals(landLine.getPhoneId(), landLineNumber.getPhoneId())) { + // Found it + this.allLandLineNumbers().remove(landLine); + break; + } + } + } + + // ... then add it + this.allLandLineNumbers().add(landLineNumber); + } + + /** + * Uniquely add given mobile number to this bean's list. First remove the + * old instance (by id number), then re-add it again. + *

+ * @param mobileNumber Mobile number to add + */ + private void uniqueAddMobileNumber (final DialableMobileNumber mobileNumber) { + // Make sure the parameter is valid + if (null == mobileNumber) { + // Throw NPE + throw new NullPointerException("mobileNumber is null"); + } else if (mobileNumber.getPhoneId() == null) { + // Throw again ... + throw new NullPointerException("mobileNumber.phoneId is null"); + } else if (mobileNumber.getPhoneId() < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("mobileNumber.phoneId={0} is not valid.", mobileNumber.getPhoneId())); + } + + // First remove it by object + if (!this.allMobileNumbers().remove(mobileNumber)) { + // Did not work, try by id number + for (final DialableMobileNumber cell : this.allMobileNumbers()) { + // Is id number the same? + if (Objects.equals(cell.getPhoneId(), mobileNumber.getPhoneId())) { + // Found it + this.allMobileNumbers().remove(cell); + break; + } + } + } + + // ... then add it + this.allMobileNumbers().add(mobileNumber); + } + + /** + * Updates given contact's mobile, land-line and fax number + *

+ * @param contact Contact instance + */ + private void updateContactPhoneNumbers (final Contact contact) { + // Parameter must be valid + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); + } else if (contact.getContactId() == null) { + // Throw again + throw new NullPointerException("contact.contactId is null"); + } else if (contact.getContactId() < 1) { + // Id number is not valid + } + + // Is mobile set? + if (contact.getContactMobileNumber() instanceof DialableMobileNumber) { + // Unique-add it + this.uniqueAddMobileNumber(contact.getContactMobileNumber()); + } + + // Is land-line set? + if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) { + // Unique-add it + this.uniqueAddLandLineNumber(contact.getContactLandLineNumber()); + } + + // Is fax set? + if (contact.getContactFaxNumber() instanceof DialableFaxNumber) { + // Unique-add it + this.uniqueAddFaxNumber(contact.getContactFaxNumber()); + } + } + +} diff --git a/src/java/org/mxchange/addressbook/beans/phone/AddressbookPhoneWebRequestController.java b/src/java/org/mxchange/addressbook/beans/phone/AddressbookPhoneWebRequestController.java new file mode 100644 index 00000000..d824d079 --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/phone/AddressbookPhoneWebRequestController.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * 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.addressbook.beans.phone; + +import java.io.Serializable; +import java.util.List; +import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber; +import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber; +import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber; + +/** + * An interface for a request web controller (bean) for administrative phone + * number purposes. + *

+ * @author Roland Häder + */ +public interface AddressbookPhoneWebRequestController extends Serializable { + + /** + * Returns a list of all mobile numbers. For performance reasons, the + * controller (bean) should be application-scoped as from user to user + * nothing changes. And the controller's post-construct method should load + * all numbers and cache it in the controller. + *

+ * @return List of all mobile numbers + */ + List allMobileNumbers (); + + /** + * Returns a list of all fax numbers. For performance reasons, the + * controller (bean) should be application-scoped as from user to user + * nothing changes. And the controller's post-construct method should load + * all numbers and cache it in the controller. + *

+ * @return List of all fax numbers + */ + List allFaxNumbers (); + + /** + * Returns a list of all land-line numbers. For performance reasons, the + * controller (bean) should be application-scoped as from user to user + * nothing changes. And the controller's post-construct method should load + * all numbers and cache it in the controller. + *

+ * @return List of all land-line numbers + */ + List allLandLineNumbers (); + +} diff --git a/src/java/org/mxchange/addressbook/beans/profile/AddressbookUserProfileWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/profile/AddressbookUserProfileWebRequestBean.java index 4b46e971..887483f7 100644 --- a/src/java/org/mxchange/addressbook/beans/profile/AddressbookUserProfileWebRequestBean.java +++ b/src/java/org/mxchange/addressbook/beans/profile/AddressbookUserProfileWebRequestBean.java @@ -17,17 +17,16 @@ package org.mxchange.addressbook.beans.profile; import java.text.MessageFormat; -import javax.annotation.PostConstruct; import javax.enterprise.context.RequestScoped; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; import org.mxchange.addressbook.beans.BaseAddressbookController; -import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController; import org.mxchange.addressbook.beans.user.login.AddressbookUserLoginWebSessionController; import org.mxchange.jusercore.exceptions.UserNotFoundException; import org.mxchange.jusercore.model.user.User; import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; +import org.mxchange.addressbook.beans.user.AddressbookUserWebRequestController; /** * A web request bean for user profiles @@ -47,7 +46,7 @@ public class AddressbookUserProfileWebRequestBean extends BaseAddressbookControl * User controller */ @Inject - private AddressbookUserWebSessionController userController; + private AddressbookUserWebRequestController userController; /** * Login controller @@ -63,13 +62,6 @@ public class AddressbookUserProfileWebRequestBean extends BaseAddressbookControl super(); } - /** - * Post-construction method - */ - @PostConstruct - public void init () { - } - @Override public boolean isProfileLinkVisibleById (final Long userId) { // Init user instance diff --git a/src/java/org/mxchange/addressbook/beans/user/AddressbookAdminUserWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/user/AddressbookAdminUserWebRequestBean.java index 4713ca3c..1a3e537f 100644 --- a/src/java/org/mxchange/addressbook/beans/user/AddressbookAdminUserWebRequestBean.java +++ b/src/java/org/mxchange/addressbook/beans/user/AddressbookAdminUserWebRequestBean.java @@ -29,12 +29,12 @@ import javax.faces.context.FacesContext; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; -import org.mxchange.addressbook.beans.BaseAddressbookController; -import org.mxchange.addressbook.beans.contact.AddressbookAdminContactWebRequestController; -import org.mxchange.addressbook.beans.contact.AddressbookContactWebSessionController; import org.mxchange.addressbook.beans.localization.AddressbookLocalizationSessionController; import org.mxchange.jcontacts.model.contact.Contact; import org.mxchange.jcoreee.utils.FacesUtils; +<<<<<<< HEAD:src/java/org/mxchange/addressbook/beans/user/AddressbookAdminUserWebRequestBean.java +import org.mxchange.addressbook.beans.BaseAddressbookController; +import org.mxchange.addressbook.beans.contact.AddressbookAdminContactWebRequestController; import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent; import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent; import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent; @@ -64,6 +64,7 @@ import org.mxchange.juserlogincore.container.login.UserLoginContainer; import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent; import org.mxchange.juserlogincore.exceptions.UserPasswordRepeatMismatchException; import org.mxchange.juserlogincore.login.UserLoginUtils; +import org.mxchange.addressbook.beans.contact.AddressbookContactWebRequestController; /** * A user bean (controller) @@ -107,7 +108,7 @@ public class AddressbookAdminUserWebRequestBean extends BaseAddressbookControlle * Regular contact controller */ @Inject - private AddressbookContactWebSessionController contactController; + private AddressbookContactWebRequestController contactController; /** * Event being fired when administrator has deleted user @@ -144,7 +145,7 @@ public class AddressbookAdminUserWebRequestBean extends BaseAddressbookControlle * Regular user controller */ @Inject - private AddressbookUserWebSessionController userController; + private AddressbookUserWebRequestController userController; /** * Delete reason @@ -277,7 +278,7 @@ public class AddressbookAdminUserWebRequestBean extends BaseAddressbookControlle return ""; //NOI18N } else if ((this.getUserPassword() == null && (this.getUserPasswordRepeat() == null)) || ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty()))) { // Empty password entered, then generate one - password = UserLoginUtils.createRandomPassword(AddressbookUserWebSessionController.MINIMUM_PASSWORD_LENGTH); + password = UserLoginUtils.createRandomPassword(AddressbookUserWebRequestController.MINIMUM_PASSWORD_LENGTH); } else if (!this.isSamePasswordEntered()) { // Both passwords don't match throw new FaceletException(new UserPasswordRepeatMismatchException(newUser)); @@ -288,7 +289,7 @@ public class AddressbookAdminUserWebRequestBean extends BaseAddressbookControlle // The password should not be null and at least 5 characters long assert (password != null) : "password is null"; //NOI18N - assert (password.length() >= AddressbookUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough."; //NOI18N + assert (password.length() >= AddressbookUserWebRequestController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough."; //NOI18N // Encrypt password and set it newUser.setUserEncryptedPassword(UserLoginUtils.encryptPassword(password)); diff --git a/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebRequestBean.java new file mode 100644 index 00000000..d7e4050e --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebRequestBean.java @@ -0,0 +1,1175 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * 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.addressbook.beans.user; + +import java.text.MessageFormat; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Locale; +import java.util.Objects; +import javax.annotation.PostConstruct; +import javax.ejb.EJB; +import javax.enterprise.context.RequestScoped; +import javax.enterprise.event.Event; +import javax.enterprise.event.Observes; +import javax.enterprise.inject.Any; +import javax.faces.context.FacesContext; +import javax.faces.view.facelets.FaceletException; +import javax.inject.Inject; +import javax.inject.Named; +import org.mxchange.addressbook.beans.BaseAddressbookController; +import org.mxchange.addressbook.beans.contact.AddressbookContactWebRequestController; +import org.mxchange.addressbook.beans.features.AddressbookFeaturesWebApplicationController; +import org.mxchange.addressbook.beans.localization.AddressbookLocalizationSessionController; +import org.mxchange.addressbook.beans.user.login.AddressbookUserLoginWebSessionController; +import org.mxchange.jcontacts.model.contact.Contact; +import org.mxchange.jcoreee.events.locale.ObservableLocaleChangeEvent; +import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent; +import org.mxchange.jusercore.events.user.clear.password.ObservableClearUserPasswordEvent; +import org.mxchange.jusercore.events.user.clear.username.ObservableClearUserNameEvent; +import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent; +import org.mxchange.jusercore.events.user.delete.ObservableAdminDeletedUserEvent; +import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent; +import org.mxchange.jusercore.events.user.locked.ObservableAdminLockedUserEvent; +import org.mxchange.jusercore.events.user.unlocked.ObservableAdminUnlockedUserEvent; +import org.mxchange.jusercore.events.user.update.ObservableAdminUpdatedUserDataEvent; +import org.mxchange.jusercore.events.user.update.ObservableUpdatedUserPersonalDataEvent; +import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent; +import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException; +import org.mxchange.jusercore.exceptions.UserNotFoundException; +import org.mxchange.jusercore.model.user.LoginUser; +import org.mxchange.jusercore.model.user.User; +import org.mxchange.jusercore.model.user.UserSessionBeanRemote; +import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; +import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent; +import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent; +import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent; +import org.mxchange.juserlogincore.events.user.password_change.ObservableUpdatedUserPasswordEvent; +import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException; +import org.mxchange.juserlogincore.login.UserLoginUtils; + +/** + * A user bean (controller) + *

+ * @author Roland Häder + */ +@Named ("userController") +@RequestScoped +public class AddressbookUserWebRequestBean extends BaseAddressbookController implements AddressbookUserWebRequestController { + + /** + * Serial number + */ + private static final long serialVersionUID = 542_145_347_916L; + + /** + * General contact controller + */ + @Inject + private AddressbookContactWebRequestController contactController; + + /** + * Features controller + */ + @Inject + private AddressbookFeaturesWebApplicationController featureController; + + /** + * Locale instance + */ + private Locale locale; + + /** + * Localization controller + */ + @Inject + private AddressbookLocalizationSessionController localizationController; + + /** + * Event being fired when user updated personal data + */ + @Inject + @Any + private Event updatedPersonalDataEvent; + + /** + * Remote user bean + */ + @EJB (lookup = "java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote") + private UserSessionBeanRemote userBean; + + /** + * User id + */ + private Long userId; + + /** + * A list of all user profiles + */ + @Inject + @Cached (cacheName = "userCache") + private transient Cache userCache; + + /** + * Login controller (bean) + */ + @Inject + private AddressbookUserLoginWebSessionController userLoginController; + + /** + * User name + */ + private String userName; + + /** + * User name list + */ + @Inject + @Cached (cacheName = "userNameCache") + private transient Cache userNameCache; + + /** + * User password (clear-text from web form) + */ + private String userPassword; + + /** + * User password repeated (clear-text from web form) + */ + private String userPasswordRepeat; + + /** + * Whether the user wants a public profile + */ + private ProfileMode userProfileMode; + + /** + * Default constructor + */ + public AddressbookUserWebRequestBean () { + // Call super constructor + super(); + } + + /** + * Event observer for newly added users by administrator + *

+ * @param event Event being fired + */ + public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent 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 + } + + // Update user list + this.updateList(event.getAddedUser()); + + // Clear all data + this.clear(); + + // Set user id again + this.setUserId(event.getAddedUser().getUserId()); + } + + /** + * Event observer for deleted user accounts (by administrator) + *

+ * @param event Event being fired + */ + public void afterAdminDeletedUserEvent (@Observes final ObservableAdminDeletedUserEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getDeletedUser() == null) { + // Throw NPE again + throw new NullPointerException("event.deletedUser is null"); //NOI18N + } else if (event.getDeletedUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.deletedUser.userId is null"); //NOI18N + } else if (event.getDeletedUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getDeletedUser(), event.getDeletedUser().getUserId())); //NOI18N + } + + // Update user list + this.removeFromList(event.getDeletedUser()); + + // Clear all data + this.clear(); + } + + /** + * Event observer for linked users with existing contact data + *

+ * @param event Event being fired + */ + public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getLinkedUser() == null) { + // Throw NPE again + throw new NullPointerException("event.linkedUser is null"); //NOI18N + } else if (event.getLinkedUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.linkedUser.userId is null"); //NOI18N + } else if (event.getLinkedUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserId())); //NOI18N + } + + // Update user list + this.updateList(event.getLinkedUser()); + + // Clear all data + this.clear(); + + // Set user id again + this.setUserId(event.getLinkedUser().getUserId()); + } + + /** + * Event observer for locked users + *

+ * @param event Event being fired + */ + public void afterAdminLockedUserEvent (@Observes final ObservableAdminLockedUserEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getLockedUser() == null) { + // Throw NPE again + throw new NullPointerException("event.lockedUser is null"); //NOI18N + } else if (event.getLockedUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.lockedUser.userId is null"); //NOI18N + } else if (event.getLockedUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLockedUser(), event.getLockedUser().getUserId())); //NOI18N + } + + // Update user list + this.updateList(event.getLockedUser()); + } + + /** + * Event observer for unlocked users + *

+ * @param event Event being fired + */ + public void afterAdminUnlockedUserEvent (@Observes final ObservableAdminUnlockedUserEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getUnlockedUser() == null) { + // Throw NPE again + throw new NullPointerException("event.unlockedUser is null"); //NOI18N + } else if (event.getUnlockedUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.unlockedUser.userId is null"); //NOI18N + } else if (event.getUnlockedUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUnlockedUser(), event.getUnlockedUser().getUserId())); //NOI18N + } + + // Update user list + this.updateList(event.getUnlockedUser()); + } + + /** + * Event observer for updated user data by administrator + *

+ * @param event Event being updated + */ + public void afterAdminUpdatedUserDataEvent (@Observes final ObservableAdminUpdatedUserDataEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getUpdatedUser() == null) { + // Throw NPE again + throw new NullPointerException("event.updatedUser is null"); //NOI18N + } else if (event.getUpdatedUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N + } else if (event.getUpdatedUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N + } + + // Update user list + this.updateList(event.getUpdatedUser()); + + // Clear all data + this.clear(); + } + + /** + * Event observer for when a bean helper has successfully created a user + * instance, means the user exists. If the user does not exist, this event + * should not fire but instead a proper exception must be thrown. + *

+ * @param event User created event + */ + public void afterCreatedUserEvent (@Observes final ObservableCreatedUserEvent event) { + // Is the instance valid? + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getCreatedUser() == null) { + // Throw NPE again + throw new NullPointerException("event.createdUser is null"); //NOI18N + } else if (event.getCreatedUser().getUserId() == null) { + // Throw NPE again + throw new NullPointerException("event.createdUser.userId is null"); //NOI18N + } else if (event.getCreatedUser().getUserId() < 1) { + // Throw NPE again + throw new NullPointerException(MessageFormat.format("event.createdUser.userId={0} is not valid", event.getCreatedUser().getUserId())); //NOI18N + } + + // Get user instance + User user = event.getCreatedUser(); + + // Set all fields here + this.copyUser(user); + } + + /** + * Observer method for events being fired when the application's locale has + * been changed. + *

+ * @param event Event being fired + */ + public void afterLocaleChangeEvent (@Observes final ObservableLocaleChangeEvent event) { + // Is the parameter valid? + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); + } else if (event.getLocale() == null) { + // Throw NPE again + throw new NullPointerException("event.locale is null"); + } + + // Set it here + this.setLocale(event.getLocale()); + } + + /** + * Event observer when user confirmed account. + *

+ * @param event Event being fired + */ + public void afterUserConfirmedAccountEvent (@Observes final ObservableUserConfirmedAccountEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getConfirmedUser() == null) { + // Throw NPE again + throw new NullPointerException("event.confirmedUser is null"); //NOI18N + } else if (event.getConfirmedUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N + } else if (event.getConfirmedUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N + } + + // Update user list + this.updateList(event.getConfirmedUser()); + } + + /** + * Event observer for logged-in user + *

+ * @param event Event instance + */ + public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getLoggedInUser() == null) { + // Throw NPE again + throw new NullPointerException("event.registeredUser is null"); //NOI18N + } else if (event.getLoggedInUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N + } else if (event.getLoggedInUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N + } + + // "Cache" user instance + User loggedInUser = event.getLoggedInUser(); + + // Copy all data to this bean + this.copyUser(loggedInUser); + } + + /** + * Event observer for user password changes + *

+ * @param event Event being fired + */ + public void afterUserPasswordChangedEvent (@Observes final ObservableUpdatedUserPasswordEvent event) { + // Is it valid? + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getUserPassword() == null) { + // Throw NPE + throw new NullPointerException("event.userPassword is null"); //NOI18N + } else if (event.getUserPassword().isEmpty()) { + // Throw NPE + throw new IllegalArgumentException("event.userPassword is empty"); //NOI18N + } + + // Set it here + this.setUserPassword(event.getUserPassword()); + this.setUserPasswordRepeat(event.getUserPassword()); + } + + /** + * Event observer for new user registrations + *

+ * @param event User registration event + */ + public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getRegisteredUser() == null) { + // Throw NPE again + throw new NullPointerException("event.registeredUser is null"); //NOI18N + } else if (event.getRegisteredUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N + } else if (event.getRegisteredUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N + } + + // Get user instance + User registeredUser = event.getRegisteredUser(); + + // Copy all data from registered->user + this.copyUser(registeredUser); + + // Clear all data + this.clear(); + + // Update user list + this.updateList(registeredUser); + + // Add user name + this.addUserName(registeredUser); + + // Set user id again + this.setUserId(registeredUser.getUserId()); + } + + /** + * Method being call after user's password has been updated (and history + * entry has been created). + *

+ * @param event Event being observed + */ + public void afterUserUpdatedPasswordEvent (@Observes final ObservableUpdatedUserPasswordEvent event) { + // Check parameter + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getPasswordHistory() == null) { + // Throw NPE again + throw new NullPointerException("event.passwordHistory is null"); //NOI18N + } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) { + // ... and again + throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N + } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) { + // Invalid value + throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N + } + + // Update user list + this.updateList(event.getPasswordHistory().getUserPasswordHistoryUser()); + } + + /** + * Listens to fired event when user updated personal data + *

+ * @param event Event being fired + */ + public void afterUserUpdatedPersonalDataEvent (@Observes final ObservableUpdatedUserPersonalDataEvent event) { + // Check parameter + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getUpdatedUser() == null) { + // Throw NPE again + throw new NullPointerException("event.updatedUser is null"); //NOI18N + } else if (event.getUpdatedUser().getUserId() == null) { + // ... and again + throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N + } else if (event.getUpdatedUser().getUserId() < 1) { + // Invalid value + throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N + } + + // Update user list + this.updateList(event.getUpdatedUser()); + } + + @Override + @SuppressWarnings ("ReturnOfCollectionOrArrayField") + public List allUsers () { + // Init list + List list = new LinkedList<>(); + + // Get iterator + Iterator> iterator = this.userCache.iterator(); + + // Loop over all + while (iterator.hasNext()) { + // Get next entry + final Cache.Entry next = iterator.next(); + + // Add value to list + list.add(next.getValue()); + } + + // Return it + return list; + } + + /** + * Event observer for when a user name should be cleared + *

+ * @param event Event being fired + */ + public void clearUserNameEvent (@Observes final ObservableClearUserNameEvent event) { + // Is it valid? + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); + } + + // Clear it + this.clearUserName(); + } + + /** + * Event observer for when both user passwords should be cleared + *

+ * @param event Event being fired + */ + public void clearUserPasswordEvent (@Observes final ObservableClearUserPasswordEvent event) { + // Is it valid? + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); + } + + // Clear it + this.clearUserPasswords(); + } + + @Override + public User createUserInstance (final boolean createContactData) { + // Trace message + //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: CALLED!", this.getClass().getSimpleName())); + + // Required personal data must be set + assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N + + // Create new user instance + User user = new LoginUser(); + + // Is user name required? + if (!this.isUserNameRequired()) { + // Generate pseudo-random user name + String randomName = this.userBean.generateRandomUserName(); + + // Set it and inivisible profile + this.setUserName(randomName); + this.setUserProfileMode(ProfileMode.INVISIBLE); + + // Generate random password + String randomPassword = UserLoginUtils.createRandomPassword(AddressbookUserWebRequestController.MINIMUM_PASSWORD_LENGTH); + + // Set random password + this.setUserPassword(randomPassword); + this.setUserPasswordRepeat(randomPassword); + } + + // Set user name profile mode and locale + user.setUserName(this.getUserName()); + user.setUserProfileMode(this.getUserProfileMode()); + user.setUserLocale(this.getLocale()); + + // Is multiple registration page + if ((createContactData) || (!this.featureController.isFeatureEnabled("user_register_multiple_page"))) { //NOI18N + // Create contact instance + Contact contact = this.contactController.createContactInstance(); + + // Debug message + //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact)); + // Set contact in user + user.setUserContact(contact); + } + + // Trace message + //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: user={1} - EXIT!", this.getClass().getSimpleName(), user)); + // Return it + return user; + } + + @Override + public User createUserLogin () { + // Trace message + //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: CALLED!", this.getClass().getSimpleName())); + + // Is all data set? + if (this.getUserName() == null) { + // Throw NPE + throw new NullPointerException("userName is null"); //NOI18N + } else if (this.getUserName().isEmpty()) { + // Is empty + throw new IllegalStateException("userName is empty."); //NOI18N + } + + // Create new user instance + User user = new LoginUser(); + + // Update all data ... + user.setUserName(this.getUserName()); + + // Trace message + //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: user={1} - EXIT!", this.getClass().getSimpleName(), user)); + // Return the new instance + return user; + } + + @Override + public String doChangePersonalData () { + // This method shall only be called if the user is logged-in + if (!this.userLoginController.isUserLoggedIn()) { + // Not logged-in + throw new IllegalStateException("User is not logged-in"); //NOI18N + } else if (!this.isRequiredChangePersonalDataSet()) { + // Not all required fields are set + throw new FaceletException("Not all required fields are set."); //NOI18N + } else if (!this.userLoginController.ifCurrentPasswordMatches()) { + // Password not matching + throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); + } else if (!this.featureController.isFeatureEnabled("change_user_personal_data")) { //NOI18N + // Editing is not allowed + throw new IllegalStateException("User tried to edit personal data."); //NOI18N + } + + // Get user instance + User user = this.userLoginController.getLoggedInUser(); + + // Copy contact data to contact instance + this.contactController.updateContactDataFromController(user.getUserContact()); + + // It should be there, so run some tests on it + assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N + assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N + assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N + assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N + assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N + assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N + + // Update all fields + user.setUserProfileMode(this.getUserProfileMode()); + + // Send it to the EJB + User updatedUser = this.userBean.updateUserPersonalData(user); + + // Fire event + this.updatedPersonalDataEvent.fire(new UpdatedUserPersonalDataEvent(updatedUser)); + + // All fine + return "user_contact_data_saved"; //NOI18N + } + + /** + * Getter for user id + *

+ * @return User id + */ + public Long getUserId () { + return this.userId; + } + + /** + * Setter for user id + *

+ * @param userId User id + */ + public void setUserId (final Long userId) { + this.userId = userId; + } + + /** + * Getter for user name + *

+ * @return User name + */ + public String getUserName () { + return this.userName; + } + + /** + * Setter for user name + *

+ * @param userName User name + */ + public void setUserName (final String userName) { + this.userName = userName; + } + + @Override + public String getUserPassword () { + return this.userPassword; + } + + /** + * Setter for clear-text user password + *

+ * @param userPassword Clear-text user password + */ + public void setUserPassword (final String userPassword) { + this.userPassword = userPassword; + } + + /** + * Getter for clear-text user password repeated + *

+ * @return Clear-text user password repeated + */ + public String getUserPasswordRepeat () { + return this.userPasswordRepeat; + } + + /** + * Setter for clear-text user password repeated + *

+ * @param userPasswordRepeat Clear-text user password repeated + */ + public void setUserPasswordRepeat (final String userPasswordRepeat) { + this.userPasswordRepeat = userPasswordRepeat; + } + + /** + * Getter for user profile mode + *

+ * @return User profile mode + */ + public ProfileMode getUserProfileMode () { + return this.userProfileMode; + } + + /** + * Setter for user profile mode + *

+ * @param userProfileMode User profile mode + */ + public void setUserProfileMode (final ProfileMode userProfileMode) { + this.userProfileMode = userProfileMode; + } + + @Override + public boolean ifBothPasswordsEmptyAllowed () { + // Check feature first + return ((this.featureController.isFeatureEnabled("allow_user_registration_empty_password")) && //NOI18N + ((this.getUserPassword() == null) || (this.getUserPassword().isEmpty())) && + ((this.getUserPasswordRepeat() == null) || (this.getUserPasswordRepeat().isEmpty()))); + } + + /** + * Post-initialization of this class + */ + @PostConstruct + public void init () { + // Is cache there? + if (!this.userCache.iterator().hasNext()) { + // Get whole list + List list = this.userBean.allUsers(); + + // Add all + for (final Iterator iterator = list.iterator(); iterator.hasNext();) { + // Get next element + final User next = iterator.next(); + + // Add it to cache + this.userCache.put(next.getUserId(), next); + this.userNameCache.put(next.getUserId(), next.getUserName()); + } + } + } + + @Override + public boolean isContactFound (final Contact contact) { + // The contact must be valid + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // Throw again ... + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N + } + + // Default is not found + boolean isFound = false; + + // Get iterator + Iterator iterator = this.allUsers().iterator(); + + // Loop through all entries + while (iterator.hasNext()) { + // Get user + User next = iterator.next(); + + // Compare both objects + if (Objects.equals(contact, next.getUserContact())) { + // Found it + isFound = true; + break; + } + } + + // Return status + return isFound; + } + + @Override + public boolean isPublicUserProfileEnabled () { + // Get context parameter + String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_public_profile_enabled"); //NOI18N + + // Is it set? + boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N + + // This requires user names being enabled, too. + if ((isEnabled) && (!this.isUserNameRequired())) { + // Not valid state, users must be able to modify their profile, especially when it is public + throw new IllegalStateException("Public user profiles are enabled but user name requirement is disabled, this is not possible."); //NOI18N + } + + // Return value + return isEnabled; + } + + @Override + public boolean isRequiredChangePersonalDataSet () { + return ((this.getUserProfileMode() != null) && + (this.getUserName() != null) && (!this.getUserName().isEmpty()) && + (this.contactController.isRequiredChangePersonalDataSet())); + } + + @Override + public boolean isRequiredPersonalDataSet () { + if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N + // Multiple registration page + return this.contactController.isRequiredPersonalDataSet(); + } else { + // Single registration page + return (((this.getUserName() != null) || (!this.isUserNameRequired())) && + (this.getUserProfileMode() != null) && + (this.contactController.isRequiredPersonalDataSet()) && + (this.getUserPassword() != null) && + (this.getUserPasswordRepeat() != null)); + } + } + + @Override + public boolean isSamePasswordEntered () { + return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat()))); + } + + @Override + public boolean isUserIdEmpty () { + return ((this.getUserId() == null) || (this.getUserId() == 0)); + } + + @Override + public boolean isUserNameRegistered (final User user) { + return ((this.userNameCache instanceof List) && (this.userNameCache.containsKey(user.getUserId()))); + } + + @Override + public boolean isUserNameRequired () { + // Get context parameter + String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_user_login_require_user_name"); //NOI18N + + // Is it set? + boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N + + // Return value + return isRequired; + } + + @Override + public User lookupUserByEmailAddress (final String emailAddress) throws UserEmailAddressNotFoundException { + // Parameter must be valid + if (null == emailAddress) { + // Throw NPE + throw new NullPointerException("emailAddress is null"); //NOI18N + } else if (emailAddress.isEmpty()) { + // Not valid + throw new IllegalArgumentException("emailAddress is empty"); //NOI18N + } + + // Init variable + User user = null; + + // Try to lookup it in visible user list + for (final Iterator> iterator = this.userCache.iterator(); iterator.hasNext();) { + // Get next user + Cache.Entry next = iterator.next(); + + // Contact should be set + if (next.getValue().getUserContact() == null) { + // Contact is null + throw new NullPointerException(MessageFormat.format("next.userContact is null for user id {0}", next.getKey())); //NOI18N + } else if (next.getValue().getUserContact().getContactEmailAddress() == null) { + // Email address should be set + throw new NullPointerException(MessageFormat.format("next.userContact.contactEmailAddress is null for user id {0}", next.getKey())); //NOI18N + } + + // Is the email address found? + if (Objects.equals(next.getValue().getUserContact().getContactEmailAddress(), emailAddress)) { + // Copy to other variable + user = next.getValue(); + break; + } + } + + // Is it still null? + if (null == user) { + // Not visible for the current user + throw new UserEmailAddressNotFoundException(emailAddress); + } + + // Return it + return user; + } + + @Override + public User lookupUserById (final Long userId) throws UserNotFoundException { + // Parameter must be valid + if (null == userId) { + // Throw NPE + throw new NullPointerException("userId is null"); //NOI18N + } else if (userId < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N + } + + // Init variable + User user = null; + + // Try to lookup it in visible user list + for (final Iterator> iterator = this.userCache.iterator(); iterator.hasNext();) { + // Get next user + Cache.Entry next = iterator.next(); + + // Is the user id found? + if (Objects.equals(next.getKey(), userId)) { + // Copy to other variable + user = next.getValue(); + break; + } + } + + // Is it still null? + if (null == user) { + // Not visible for the current user + throw new UserNotFoundException(userId); + } + + // Return it + return user; + } + + /** + * Adds user's name to bean's internal list. It also updates the public user + * list if the user has decided to have a public account, + *

+ * @param user User instance + */ + private void addUserName (final User user) { + // Make sure the entry is not added yet + if (this.userNameCache.containsKey(user.getUserId())) { + // Abort here + throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N + } + + // Add user name + this.userNameCache.put(user.getUserId(), user.getUserName()); + } + + /** + * Clears this bean + */ + private void clear () { + // Clear all data + // - personal data + this.setUserId(null); + this.setUserProfileMode(null); + + // - other data + this.clearUserName(); + this.clearUserPasswords(); + this.setLocale(null); + } + + /** + * Clears user name + */ + private void clearUserName () { + // Clear it + this.setUserName(null); + } + + /** + * Clears both user passwords + */ + private void clearUserPasswords () { + // Clear both + this.setUserPassword(null); + this.setUserPasswordRepeat(null); + } + + /** + * Copies given user into the controller + *

+ * @param user User instance + */ + private void copyUser (final User user) { + // Make sure the instance is valid + if (null == user) { + // Throw NPE + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserContact() == null) { + // Throw again ... + throw new NullPointerException("user.userContact is null"); //NOI18N + } + + // Copy all fields: + // - base data + this.setUserId(user.getUserId()); + this.setUserProfileMode(user.getUserProfileMode()); + } + + /** + * Getter for locale instance + *

+ * @return Locale instance + */ + private Locale getLocale () { + return this.locale; + } + + /** + * Setter for locale instance + *

+ * @param locale Locale instance + */ + private void setLocale (final Locale locale) { + this.locale = locale; + } + + /** + * Removes user from all lists + *

+ * @param user User to remove + */ + private void removeFromList (final User user) { + // The user should be valid + if (null == user) { + // Throw NPE + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // ... again NPE + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Invalid id + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N + } + + // Remove it from lists + this.userCache.remove(user.getUserId()); + + // Remove name from list + this.userNameCache.remove(user.getUserId()); + } + + /** + * Updates list with given user instance + *

+ * @param user User instance + */ + private void updateList (final User user) { + // The user should be valid + if (null == user) { + // Throw NPE + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // ... again NPE + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Invalid id + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N + } else if (user.getUserContact() == null) { + // Throw again ... + throw new NullPointerException("user.userContact is null"); //NOI18N + } else if (user.getUserContact().getContactId() == null) { + // Throw again ... + throw new NullPointerException("user.userContact.contactId is null"); //NOI18N + } else if (user.getUserContact().getContactId() < 1) { + // Throw again ... + throw new NullPointerException(MessageFormat.format("user.userContact.contactId={0} is invalid.", user.getUserContact().getContactId())); //NOI18N + } + + // Add/update user + this.userCache.put(user.getUserId(), user); + } + +} diff --git a/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebRequestController.java b/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebRequestController.java new file mode 100644 index 00000000..5a9b253d --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebRequestController.java @@ -0,0 +1,175 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * 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.addressbook.beans.user; + +import java.io.Serializable; +import java.util.List; +import org.mxchange.jcontacts.model.contact.Contact; +import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException; +import org.mxchange.jusercore.exceptions.UserNotFoundException; +import org.mxchange.jusercore.model.user.User; + +/** + * An interface for user beans + *

+ * @author Roland Häder + */ +public interface AddressbookUserWebRequestController extends Serializable { + + /** + * Minimum password length + *

+ * @deprecated Better set as context parameter + */ + public static final Integer MINIMUM_PASSWORD_LENGTH = 5; + + /** + * Getter for clear-text user password + *

+ * @return Clear-text user password + */ + String getUserPassword (); + + /** + * Checks if both user passwords are left empty and if this is enabled + * (allowed) in context parameter. If true, the calling bean should create a + * random password (preferable with UserUtils.createRandomPassword() and set + * it in both user password fields. + *

+ * @return Whether empty passwords are allowed + */ + boolean ifBothPasswordsEmptyAllowed (); + + /** + * All users + *

+ * @return A list of all public user profiles + */ + List allUsers (); + + /** + * Checks whether the given contact is a user + *

+ * @param contact Contact to check + *

+ * @return Whether the contact is a user + */ + boolean isContactFound (final Contact contact); + + /** + * Checks whether given user instance's name is used + *

+ * @param user User instance's name to check + *

+ * @return Whether it is already used + */ + boolean isUserNameRegistered (final User user); + + /** + * Tries to lookup user by given id number. If the user is not found or the + * account status is not CONFIRMED proper exceptions are thrown. + *

+ * @param userId User id + *

+ * @return User instance + *

+ * @throws UserNotFoundException If the user is not found + */ + User lookupUserById (final Long userId) throws UserNotFoundException; + + /** + * Tries to lookup user by given email address. If the user is not found a + * proper exceptions is thrown. + *

+ * @param emailAddress Email address + *

+ * @return User instance + *

+ * @throws UserEmailAddressNotFoundException If the user's email address is + * not found + */ + User lookupUserByEmailAddress (final String emailAddress) throws UserEmailAddressNotFoundException; + + /** + * Creates an instance from all properties + *

+ * @param createContactData Whether contact data should be created + *

+ * @return A user instance + */ + User createUserInstance (final boolean createContactData); + + /** + * Creates a user instance for login phase + *

+ * @return User instance + */ + User createUserLogin (); + + /** + * Checks whether all required personal data is set + *

+ * @return Whether the required personal data is set + */ + boolean isRequiredPersonalDataSet (); + + /** + * Checks whether all required personal data is set for changing them + *

+ * @return Whether the required personal data is set + */ + boolean isRequiredChangePersonalDataSet (); + + /** + * Checks whether same passwords has been entered + *

+ * @return Whether same passwords has been entered + */ + boolean isSamePasswordEntered (); + + /** + * Checks if the user id is empty + *

+ * @return Whether the user id is empty + */ + boolean isUserIdEmpty (); + + /** + * Changes logged-in user's personal data if the current password matches + * and TAC + privacy statement has been accepted. + *

+ * @return New target page + */ + String doChangePersonalData (); + + /** + * Checks whether this application requires a user name to be entered. + * Otherwise a random name like "userXXXXX" is generated + *

+ * @return Whether this application requires a user name + */ + boolean isUserNameRequired (); + + /** + * Checks wether public user profiles are enabled. This requires that user + * names are also enabled. + *

+ * @return Whether public user profiles are enabled + */ + boolean isPublicUserProfileEnabled (); + +} diff --git a/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionBean.java deleted file mode 100644 index e9ae01c2..00000000 --- a/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionBean.java +++ /dev/null @@ -1,1203 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * 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.addressbook.beans.user; - -import java.text.MessageFormat; -import java.util.Iterator; -import java.util.List; -import java.util.Locale; -import java.util.Objects; -import javax.annotation.PostConstruct; -import javax.ejb.EJB; -import javax.enterprise.context.SessionScoped; -import javax.enterprise.event.Event; -import javax.enterprise.event.Observes; -import javax.enterprise.inject.Any; -import javax.faces.context.FacesContext; -import javax.faces.view.facelets.FaceletException; -import javax.inject.Inject; -import javax.inject.Named; -import org.mxchange.addressbook.beans.BaseAddressbookController; -import org.mxchange.addressbook.beans.contact.AddressbookContactWebSessionController; -import org.mxchange.addressbook.beans.features.AddressbookFeaturesWebApplicationController; -import org.mxchange.addressbook.beans.localization.AddressbookLocalizationSessionController; -import org.mxchange.addressbook.beans.user.login.AddressbookUserLoginWebSessionController; -import org.mxchange.jcontacts.model.contact.Contact; -import org.mxchange.jcoreee.events.locale.ObservableLocaleChangeEvent; -import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent; -import org.mxchange.jusercore.events.user.clear.password.ObservableClearUserPasswordEvent; -import org.mxchange.jusercore.events.user.clear.username.ObservableClearUserNameEvent; -import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent; -import org.mxchange.jusercore.events.user.delete.ObservableAdminDeletedUserEvent; -import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent; -import org.mxchange.jusercore.events.user.locked.ObservableAdminLockedUserEvent; -import org.mxchange.jusercore.events.user.unlocked.ObservableAdminUnlockedUserEvent; -import org.mxchange.jusercore.events.user.update.ObservableAdminUpdatedUserDataEvent; -import org.mxchange.jusercore.events.user.update.ObservableUpdatedUserPersonalDataEvent; -import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent; -import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException; -import org.mxchange.jusercore.exceptions.UserNotFoundException; -import org.mxchange.jusercore.model.user.LoginUser; -import org.mxchange.jusercore.model.user.User; -import org.mxchange.jusercore.model.user.UserSessionBeanRemote; -import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; -import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent; -import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent; -import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent; -import org.mxchange.juserlogincore.events.user.password_change.ObservableUpdatedUserPasswordEvent; -import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException; -import org.mxchange.juserlogincore.login.UserLoginUtils; - -/** - * A user bean (controller) - *

- * @author Roland Häder - */ -@Named ("userController") -@SessionScoped -public class AddressbookUserWebSessionBean extends BaseAddressbookController implements AddressbookUserWebSessionController { - - /** - * Serial number - */ - private static final long serialVersionUID = 542_145_347_916L; - - /** - * General contact controller - */ - @Inject - private AddressbookContactWebSessionController contactController; - - /** - * Features controller - */ - @Inject - private AddressbookFeaturesWebApplicationController featureController; - - /** - * Locale instance - */ - private Locale locale; - - /** - * Localization controller - */ - @Inject - private AddressbookLocalizationSessionController localizationController; - - /** - * Event being fired when user updated personal data - */ - @Inject - @Any - private Event updatedPersonalDataEvent; - - /** - * Remote user bean - */ - @EJB (lookup = "java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote") - private UserSessionBeanRemote userBean; - - /** - * User id - */ - private Long userId; - - /** - * A list of all user profiles - */ - private List userList; - - /** - * Login controller (bean) - */ - @Inject - private AddressbookUserLoginWebSessionController userLoginController; - - /** - * User name - */ - private String userName; - - /** - * User name list - */ - private List userNameList; - - /** - * User password (clear-text from web form) - */ - private String userPassword; - - /** - * User password repeated (clear-text from web form) - */ - private String userPasswordRepeat; - - /** - * Whether the user wants a public profile - */ - private ProfileMode userProfileMode; - - /** - * A list of all public user profiles - *

- * @TODO Hmm, makes that sense? Having visible user list in current - * (session-scoped) user's visible user list? - */ - private List visibleUserList; - - /** - * Default constructor - */ - public AddressbookUserWebSessionBean () { - // Call super constructor - super(); - } - - /** - * Event observer for newly added users by administrator - *

- * @param event Event being fired - */ - public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent 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 - } - - // Update user list - this.updateList(event.getAddedUser()); - - // Clear all data - this.clear(); - - // Set user id again - this.setUserId(event.getAddedUser().getUserId()); - } - - /** - * Event observer for deleted user accounts (by administrator) - *

- * @param event Event being fired - */ - public void afterAdminDeletedUserEvent (@Observes final ObservableAdminDeletedUserEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getDeletedUser() == null) { - // Throw NPE again - throw new NullPointerException("event.deletedUser is null"); //NOI18N - } else if (event.getDeletedUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.deletedUser.userId is null"); //NOI18N - } else if (event.getDeletedUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getDeletedUser(), event.getDeletedUser().getUserId())); //NOI18N - } - - // Update user list - this.removeFromList(event.getDeletedUser()); - - // Clear all data - this.clear(); - } - - /** - * Event observer for linked users with existing contact data - *

- * @param event Event being fired - */ - public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getLinkedUser() == null) { - // Throw NPE again - throw new NullPointerException("event.linkedUser is null"); //NOI18N - } else if (event.getLinkedUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.linkedUser.userId is null"); //NOI18N - } else if (event.getLinkedUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserId())); //NOI18N - } - - // Update user list - this.updateList(event.getLinkedUser()); - - // Clear all data - this.clear(); - - // Set user id again - this.setUserId(event.getLinkedUser().getUserId()); - } - - /** - * Event observer for locked users - *

- * @param event Event being fired - */ - public void afterAdminLockedUserEvent (@Observes final ObservableAdminLockedUserEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getLockedUser() == null) { - // Throw NPE again - throw new NullPointerException("event.lockedUser is null"); //NOI18N - } else if (event.getLockedUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.lockedUser.userId is null"); //NOI18N - } else if (event.getLockedUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLockedUser(), event.getLockedUser().getUserId())); //NOI18N - } - - // Update user list - this.updateList(event.getLockedUser()); - } - - /** - * Event observer for unlocked users - *

- * @param event Event being fired - */ - public void afterAdminUnlockedUserEvent (@Observes final ObservableAdminUnlockedUserEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getUnlockedUser() == null) { - // Throw NPE again - throw new NullPointerException("event.unlockedUser is null"); //NOI18N - } else if (event.getUnlockedUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.unlockedUser.userId is null"); //NOI18N - } else if (event.getUnlockedUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUnlockedUser(), event.getUnlockedUser().getUserId())); //NOI18N - } - - // Update user list - this.updateList(event.getUnlockedUser()); - } - - /** - * Event observer for updated user data by administrator - *

- * @param event Event being updated - */ - public void afterAdminUpdatedUserDataEvent (@Observes final ObservableAdminUpdatedUserDataEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getUpdatedUser() == null) { - // Throw NPE again - throw new NullPointerException("event.updatedUser is null"); //NOI18N - } else if (event.getUpdatedUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N - } else if (event.getUpdatedUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N - } - - // Update user list - this.updateList(event.getUpdatedUser()); - - // Clear all data - this.clear(); - } - - /** - * Event observer for when a bean helper has successfully created a user - * instance, means the user exists. If the user does not exist, this event - * should not fire but instead a proper exception must be thrown. - *

- * @param event User created event - */ - public void afterCreatedUserEvent (@Observes final ObservableCreatedUserEvent event) { - // Is the instance valid? - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getCreatedUser() == null) { - // Throw NPE again - throw new NullPointerException("event.createdUser is null"); //NOI18N - } else if (event.getCreatedUser().getUserId() == null) { - // Throw NPE again - throw new NullPointerException("event.createdUser.userId is null"); //NOI18N - } else if (event.getCreatedUser().getUserId() < 1) { - // Throw NPE again - throw new NullPointerException(MessageFormat.format("event.createdUser.userId={0} is not valid", event.getCreatedUser().getUserId())); //NOI18N - } - - // Get user instance - User user = event.getCreatedUser(); - - // Set all fields here - this.copyUser(user); - } - - /** - * Observer method for events being fired when the application's locale has - * been changed. - *

- * @param event Event being fired - */ - public void afterLocaleChangeEvent (@Observes final ObservableLocaleChangeEvent event) { - // Is the parameter valid? - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); - } else if (event.getLocale() == null) { - // Throw NPE again - throw new NullPointerException("event.locale is null"); - } - - // Set it here - this.setLocale(event.getLocale()); - } - - /** - * Event observer when user confirmed account. - *

- * @param event Event being fired - */ - public void afterUserConfirmedAccountEvent (@Observes final ObservableUserConfirmedAccountEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getConfirmedUser() == null) { - // Throw NPE again - throw new NullPointerException("event.confirmedUser is null"); //NOI18N - } else if (event.getConfirmedUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N - } else if (event.getConfirmedUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N - } - - // Update user list - this.updateList(event.getConfirmedUser()); - } - - /** - * Event observer for logged-in user - *

- * @param event Event instance - */ - public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getLoggedInUser() == null) { - // Throw NPE again - throw new NullPointerException("event.registeredUser is null"); //NOI18N - } else if (event.getLoggedInUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N - } else if (event.getLoggedInUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N - } - - // "Cache" user instance - User loggedInUser = event.getLoggedInUser(); - - // Copy all data to this bean - this.copyUser(loggedInUser); - - // Is the user visible? - if (Objects.equals(loggedInUser.getUserProfileMode(), ProfileMode.PUBLIC)) { - // Yes, then add user - this.visibleUserList.add(loggedInUser); - } - } - - /** - * Event observer for user password changes - *

- * @param event Event being fired - */ - public void afterUserPasswordChangedEvent (@Observes final ObservableUpdatedUserPasswordEvent event) { - // Is it valid? - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getUserPassword() == null) { - // Throw NPE - throw new NullPointerException("event.userPassword is null"); //NOI18N - } else if (event.getUserPassword().isEmpty()) { - // Throw NPE - throw new IllegalArgumentException("event.userPassword is empty"); //NOI18N - } - - // Set it here - this.setUserPassword(event.getUserPassword()); - this.setUserPasswordRepeat(event.getUserPassword()); - } - - /** - * Event observer for new user registrations - *

- * @param event User registration event - */ - public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getRegisteredUser() == null) { - // Throw NPE again - throw new NullPointerException("event.registeredUser is null"); //NOI18N - } else if (event.getRegisteredUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N - } else if (event.getRegisteredUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N - } - - // Get user instance - User registeredUser = event.getRegisteredUser(); - - // Copy all data from registered->user - this.copyUser(registeredUser); - - // Clear all data - this.clear(); - - // Update user list - this.updateList(registeredUser); - - // Add user name - this.addUserName(registeredUser); - - // Is the account public? - if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) { - // Also add it to this list - this.visibleUserList.add(registeredUser); - } - - // Set user id again - this.setUserId(registeredUser.getUserId()); - } - - /** - * Method being call after user's password has been updated (and history - * entry has been created). - *

- * @param event Event being observed - */ - public void afterUserUpdatedPasswordEvent (@Observes final ObservableUpdatedUserPasswordEvent event) { - // Check parameter - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getPasswordHistory() == null) { - // Throw NPE again - throw new NullPointerException("event.passwordHistory is null"); //NOI18N - } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) { - // ... and again - throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N - } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) { - // Invalid value - throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N - } - - // Update user list - this.updateList(event.getPasswordHistory().getUserPasswordHistoryUser()); - } - - /** - * Listens to fired event when user updated personal data - *

- * @param event Event being fired - */ - public void afterUserUpdatedPersonalDataEvent (@Observes final ObservableUpdatedUserPersonalDataEvent event) { - // Check parameter - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getUpdatedUser() == null) { - // Throw NPE again - throw new NullPointerException("event.updatedUser is null"); //NOI18N - } else if (event.getUpdatedUser().getUserId() == null) { - // ... and again - throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N - } else if (event.getUpdatedUser().getUserId() < 1) { - // Invalid value - throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N - } - - // Update user list - this.updateList(event.getUpdatedUser()); - } - - @Override - @SuppressWarnings ("ReturnOfCollectionOrArrayField") - public List allUsers () { - // Return it - return this.userList; - } - - @Override - @SuppressWarnings ("ReturnOfCollectionOrArrayField") - public List allVisibleUsers () { - // Return it - return this.visibleUserList; - } - - /** - * Event observer for when a user name should be cleared - *

- * @param event Event being fired - */ - public void clearUserNameEvent (@Observes final ObservableClearUserNameEvent event) { - // Is it valid? - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); - } - - // Clear it - this.clearUserName(); - } - - /** - * Event observer for when both user passwords should be cleared - *

- * @param event Event being fired - */ - public void clearUserPasswordEvent (@Observes final ObservableClearUserPasswordEvent event) { - // Is it valid? - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); - } - - // Clear it - this.clearUserPasswords(); - } - - @Override - public User createUserInstance (final boolean createContactData) { - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: CALLED!", this.getClass().getSimpleName())); - - // Required personal data must be set - assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N - - // Create new user instance - User user = new LoginUser(); - - // Is user name required? - if (!this.isUserNameRequired()) { - // Generate pseudo-random user name - String randomName = this.userBean.generateRandomUserName(); - - // Set it and inivisible profile - this.setUserName(randomName); - this.setUserProfileMode(ProfileMode.INVISIBLE); - - // Generate random password - String randomPassword = UserLoginUtils.createRandomPassword(AddressbookUserWebSessionController.MINIMUM_PASSWORD_LENGTH); - - // Set random password - this.setUserPassword(randomPassword); - this.setUserPasswordRepeat(randomPassword); - } - - // Set user name profile mode and locale - user.setUserName(this.getUserName()); - user.setUserProfileMode(this.getUserProfileMode()); - user.setUserLocale(this.getLocale()); - - // Is multiple registration page - if ((createContactData) || (!this.featureController.isFeatureEnabled("user_register_multiple_page"))) { //NOI18N - // Create contact instance - Contact contact = this.contactController.createContactInstance(); - - // Debug message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact)); - // Set contact in user - user.setUserContact(contact); - } - - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: user={1} - EXIT!", this.getClass().getSimpleName(), user)); - // Return it - return user; - } - - @Override - public User createUserLogin () { - // Trace message - //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: CALLED!", this.getClass().getSimpleName())); - - // Is all data set? - if (this.getUserName() == null) { - // Throw NPE - throw new NullPointerException("userName is null"); //NOI18N - } else if (this.getUserName().isEmpty()) { - // Is empty - throw new IllegalStateException("userName is empty."); //NOI18N - } - - // Create new user instance - User user = new LoginUser(); - - // Update all data ... - user.setUserName(this.getUserName()); - - // Trace message - //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: user={1} - EXIT!", this.getClass().getSimpleName(), user)); - // Return the new instance - return user; - } - - @Override - public String doChangePersonalData () { - // This method shall only be called if the user is logged-in - if (!this.userLoginController.isUserLoggedIn()) { - // Not logged-in - throw new IllegalStateException("User is not logged-in"); //NOI18N - } else if (!this.isRequiredChangePersonalDataSet()) { - // Not all required fields are set - throw new FaceletException("Not all required fields are set."); //NOI18N - } else if (!this.userLoginController.ifCurrentPasswordMatches()) { - // Password not matching - throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); - } else if (!this.featureController.isFeatureEnabled("change_user_personal_data")) { //NOI18N - // Editing is not allowed - throw new IllegalStateException("User tried to edit personal data."); //NOI18N - } - - // Get user instance - User user = this.userLoginController.getLoggedInUser(); - - // Copy contact data to contact instance - this.contactController.updateContactDataFromController(user.getUserContact()); - - // It should be there, so run some tests on it - assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N - assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N - assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N - assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N - assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N - assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N - - // Update all fields - user.setUserProfileMode(this.getUserProfileMode()); - - // Send it to the EJB - User updatedUser = this.userBean.updateUserPersonalData(user); - - // Fire event - this.updatedPersonalDataEvent.fire(new UpdatedUserPersonalDataEvent(updatedUser)); - - // All fine - return "user_contact_data_saved"; //NOI18N - } - - /** - * Getter for user id - *

- * @return User id - */ - public Long getUserId () { - return this.userId; - } - - /** - * Setter for user id - *

- * @param userId User id - */ - public void setUserId (final Long userId) { - this.userId = userId; - } - - /** - * Getter for user name - *

- * @return User name - */ - public String getUserName () { - return this.userName; - } - - /** - * Setter for user name - *

- * @param userName User name - */ - public void setUserName (final String userName) { - this.userName = userName; - } - - @Override - public String getUserPassword () { - return this.userPassword; - } - - /** - * Setter for clear-text user password - *

- * @param userPassword Clear-text user password - */ - public void setUserPassword (final String userPassword) { - this.userPassword = userPassword; - } - - /** - * Getter for clear-text user password repeated - *

- * @return Clear-text user password repeated - */ - public String getUserPasswordRepeat () { - return this.userPasswordRepeat; - } - - /** - * Setter for clear-text user password repeated - *

- * @param userPasswordRepeat Clear-text user password repeated - */ - public void setUserPasswordRepeat (final String userPasswordRepeat) { - this.userPasswordRepeat = userPasswordRepeat; - } - - /** - * Getter for user profile mode - *

- * @return User profile mode - */ - public ProfileMode getUserProfileMode () { - return this.userProfileMode; - } - - /** - * Setter for user profile mode - *

- * @param userProfileMode User profile mode - */ - public void setUserProfileMode (final ProfileMode userProfileMode) { - this.userProfileMode = userProfileMode; - } - - @Override - public boolean ifBothPasswordsEmptyAllowed () { - // Check feature first - return ((this.featureController.isFeatureEnabled("allow_user_registration_empty_password")) && //NOI18N - ((this.getUserPassword() == null) || (this.getUserPassword().isEmpty())) && - ((this.getUserPasswordRepeat() == null) || (this.getUserPasswordRepeat().isEmpty()))); - } - - /** - * Post-initialization of this class - */ - @PostConstruct - public void init () { - // Initialize user list - this.userList = this.userBean.allUsers(); - - // Get full user name list for reducing EJB calls - this.userNameList = this.userBean.getUserNameList(); - - // Is the user logged-in? - if (this.userLoginController.isUserLoggedIn()) { - // Is logged-in, so load also users visible to memebers - this.visibleUserList = this.userBean.allMemberPublicVisibleUsers(); - } else { - // Initialize user list - this.visibleUserList = this.userBean.allPublicUsers(); - } - } - - @Override - public boolean isContactFound (final Contact contact) { - // The contact must be valid - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() == null) { - // Throw again ... - throw new NullPointerException("contact.contactId is null"); //NOI18N - } else if (contact.getContactId() < 1) { - // Not valid - throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N - } - - // Default is not found - boolean isFound = false; - - // Get iterator - Iterator iterator = this.allUsers().iterator(); - - // Loop through all entries - while (iterator.hasNext()) { - // Get user - User next = iterator.next(); - - // Compare both objects - if (Objects.equals(contact, next.getUserContact())) { - // Found it - isFound = true; - break; - } - } - - // Return status - return isFound; - } - - @Override - public boolean isPublicUserProfileEnabled () { - // Get context parameter - String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_public_profile_enabled"); //NOI18N - - // Is it set? - boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N - - // This requires user names being enabled, too. - if ((isEnabled) && (!this.isUserNameRequired())) { - // Not valid state, users must be able to modify their profile, especially when it is public - throw new IllegalStateException("Public user profiles are enabled but user name requirement is disabled, this is not possible."); //NOI18N - } - - // Return value - return isEnabled; - } - - @Override - public boolean isRequiredChangePersonalDataSet () { - return ((this.getUserProfileMode() != null) && - (this.getUserName() != null) && (!this.getUserName().isEmpty()) && - (this.contactController.isRequiredChangePersonalDataSet())); - } - - @Override - public boolean isRequiredPersonalDataSet () { - if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N - // Multiple registration page - return this.contactController.isRequiredPersonalDataSet(); - } else { - // Single registration page - return (((this.getUserName() != null) || (!this.isUserNameRequired())) && - (this.getUserProfileMode() != null) && - (this.contactController.isRequiredPersonalDataSet()) && - (this.getUserPassword() != null) && - (this.getUserPasswordRepeat() != null)); - } - } - - @Override - public boolean isSamePasswordEntered () { - return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat()))); - } - - @Override - public boolean isUserIdEmpty () { - return ((this.getUserId() == null) || (this.getUserId() == 0)); - } - - @Override - public boolean isUserNameRegistered (final User user) { - return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName()))); - } - - @Override - public boolean isUserNameRequired () { - // Get context parameter - String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_user_login_require_user_name"); //NOI18N - - // Is it set? - boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N - - // Return value - return isRequired; - } - - @Override - public boolean isVisibleUserFound () { - return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0)); - } - - @Override - public User lookupUserByEmailAddress (final String emailAddress) throws UserEmailAddressNotFoundException { - // Parameter must be valid - if (null == emailAddress) { - // Throw NPE - throw new NullPointerException("emailAddress is null"); //NOI18N - } else if (emailAddress.isEmpty()) { - // Not valid - throw new IllegalArgumentException("emailAddress is empty"); //NOI18N - } - - // Init variable - User user = null; - - // Try to lookup it in visible user list - for (final Iterator iterator = this.userList.iterator(); iterator.hasNext();) { - // Get next user - User next = iterator.next(); - - // Contact should be set - if (next.getUserContact() == null) { - // Contact is null - throw new NullPointerException(MessageFormat.format("next.userContact is null for user id {0}", next.getUserId())); //NOI18N - } else if (next.getUserContact().getContactEmailAddress() == null) { - // Email address should be set - throw new NullPointerException(MessageFormat.format("next.userContact.contactEmailAddress is null for user id {0}", next.getUserId())); //NOI18N - } - - // Is the email address found? - if (Objects.equals(next.getUserContact().getContactEmailAddress(), emailAddress)) { - // Copy to other variable - user = next; - break; - } - } - - // Is it still null? - if (null == user) { - // Not visible for the current user - throw new UserEmailAddressNotFoundException(emailAddress); - } - - // Return it - return user; - } - - @Override - public User lookupUserById (final Long userId) throws UserNotFoundException { - // Parameter must be valid - if (null == userId) { - // Throw NPE - throw new NullPointerException("userId is null"); //NOI18N - } else if (userId < 1) { - // Not valid - throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N - } - - // Init variable - User user = null; - - // Try to lookup it in visible user list - for (final Iterator iterator = this.userList.iterator(); iterator.hasNext();) { - // Get next user - User next = iterator.next(); - - // Is the user id found? - if (Objects.equals(next.getUserId(), userId)) { - // Copy to other variable - user = next; - break; - } - } - - // Is it still null? - if (null == user) { - // Not visible for the current user - throw new UserNotFoundException(userId); - } - - // Return it - return user; - } - - /** - * Adds user's name to bean's internal list. It also updates the public user - * list if the user has decided to have a public account, - *

- * @param user User instance - */ - private void addUserName (final User user) { - // Make sure the entry is not added yet - if (this.userNameList.contains(user.getUserName())) { - // Abort here - throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N - } - - // Add user name - this.userNameList.add(user.getUserName()); - } - - /** - * Clears this bean - */ - private void clear () { - // Clear all data - // - personal data - this.setUserId(null); - this.setUserProfileMode(null); - - // - other data - this.clearUserName(); - this.clearUserPasswords(); - this.setLocale(null); - } - - /** - * Clears user name - */ - private void clearUserName () { - // Clear it - this.setUserName(null); - } - - /** - * Clears both user passwords - */ - private void clearUserPasswords () { - // Clear both - this.setUserPassword(null); - this.setUserPasswordRepeat(null); - } - - /** - * Copies given user into the controller - *

- * @param user User instance - */ - private void copyUser (final User user) { - // Make sure the instance is valid - if (null == user) { - // Throw NPE - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserContact() == null) { - // Throw again ... - throw new NullPointerException("user.userContact is null"); //NOI18N - } - - // Copy all fields: - // - base data - this.setUserId(user.getUserId()); - this.setUserProfileMode(user.getUserProfileMode()); - } - - /** - * Getter for locale instance - *

- * @return Locale instance - */ - private Locale getLocale () { - return this.locale; - } - - /** - * Setter for locale instance - *

- * @param locale Locale instance - */ - private void setLocale (final Locale locale) { - this.locale = locale; - } - - /** - * Removes user from all lists - *

- * @param user User to remove - */ - private void removeFromList (final User user) { - // The user should be valid - if (null == user) { - // Throw NPE - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() == null) { - // ... again NPE - throw new NullPointerException("user.userId is null"); //NOI18N - } else if (user.getUserId() < 1) { - // Invalid id - throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N - } - - // Remove it from lists - this.userList.remove(user); - this.visibleUserList.remove(user); - - // Remove name from list - this.userNameList.remove(user.getUserName()); - } - - /** - * Updates list with given user instance - *

- * @param user User instance - */ - private void updateList (final User user) { - // The user should be valid - if (null == user) { - // Throw NPE - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() == null) { - // ... again NPE - throw new NullPointerException("user.userId is null"); //NOI18N - } else if (user.getUserId() < 1) { - // Invalid id - throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N - } else if (user.getUserContact() == null) { - // Throw again ... - throw new NullPointerException("user.userContact is null"); //NOI18N - } else if (user.getUserContact().getContactId() == null) { - // Throw again ... - throw new NullPointerException("user.userContact.contactId is null"); //NOI18N - } else if (user.getUserContact().getContactId() < 1) { - // Throw again ... - throw new NullPointerException(MessageFormat.format("user.userContact.contactId={0} is invalid.", user.getUserContact().getContactId())); //NOI18N - } - - // Get iterator from list - Iterator iterator = this.userList.iterator(); - - // "Walk" through all entries - while (iterator.hasNext()) { - // Get next element - User next = iterator.next(); - - // Is user id number the same? - if (Objects.equals(user.getUserId(), next.getUserId())) { - // Found entry, so remove it and abort - this.userList.remove(next); - break; - } - } - - // Re-add user - this.userList.add(user); - } - -} diff --git a/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionController.java b/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionController.java deleted file mode 100644 index 3809a656..00000000 --- a/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionController.java +++ /dev/null @@ -1,190 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * 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.addressbook.beans.user; - -import java.io.Serializable; -import java.util.List; -import org.mxchange.jcontacts.model.contact.Contact; -import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException; -import org.mxchange.jusercore.exceptions.UserNotFoundException; -import org.mxchange.jusercore.model.user.User; - -/** - * An interface for user beans - *

- * @author Roland Häder - */ -public interface AddressbookUserWebSessionController extends Serializable { - - /** - * Minimum password length - *

- * @deprecated Better set as context parameter - */ - public static final Integer MINIMUM_PASSWORD_LENGTH = 5; - - /** - * Getter for clear-text user password - *

- * @return Clear-text user password - */ - String getUserPassword (); - - /** - * Checks if both user passwords are left empty and if this is enabled - * (allowed) in context parameter. If true, the calling bean should create a - * random password (preferable with UserUtils.createRandomPassword() and set - * it in both user password fields. - *

- * @return Whether empty passwords are allowed - */ - boolean ifBothPasswordsEmptyAllowed (); - - /** - * All users - *

- * @return A list of all public user profiles - */ - List allUsers (); - - /** - * All public user profiles - *

- * @return A list of all public user profiles - */ - List allVisibleUsers (); - - /** - * Checks whether the given contact is a user - *

- * @param contact Contact to check - *

- * @return Whether the contact is a user - */ - boolean isContactFound (final Contact contact); - - /** - * Checks whether a public user account is registered. This means that at - * least one user profile has its flag "public user profile" enabled. - *

- * @return Whether at least one user has a public profile - */ - boolean isVisibleUserFound (); - - /** - * Checks whether given user instance's name is used - *

- * @param user User instance's name to check - *

- * @return Whether it is already used - */ - boolean isUserNameRegistered (final User user); - - /** - * Tries to lookup user by given id number. If the user is not found or the - * account status is not CONFIRMED proper exceptions are thrown. - *

- * @param userId User id - *

- * @return User instance - *

- * @throws UserNotFoundException If the user is not found - */ - User lookupUserById (final Long userId) throws UserNotFoundException; - - /** - * Tries to lookup user by given email address. If the user is not found a - * proper exceptions is thrown. - *

- * @param emailAddress Email address - *

- * @return User instance - *

- * @throws UserEmailAddressNotFoundException If the user's email address is - * not found - */ - User lookupUserByEmailAddress (final String emailAddress) throws UserEmailAddressNotFoundException; - - /** - * Creates an instance from all properties - *

- * @param createContactData Whether contact data should be created - *

- * @return A user instance - */ - User createUserInstance (final boolean createContactData); - - /** - * Creates a user instance for login phase - *

- * @return User instance - */ - User createUserLogin (); - - /** - * Checks whether all required personal data is set - *

- * @return Whether the required personal data is set - */ - boolean isRequiredPersonalDataSet (); - - /** - * Checks whether all required personal data is set for changing them - *

- * @return Whether the required personal data is set - */ - boolean isRequiredChangePersonalDataSet (); - - /** - * Checks whether same passwords has been entered - *

- * @return Whether same passwords has been entered - */ - boolean isSamePasswordEntered (); - - /** - * Checks if the user id is empty - *

- * @return Whether the user id is empty - */ - boolean isUserIdEmpty (); - - /** - * Changes logged-in user's personal data if the current password matches - * and TAC + privacy statement has been accepted. - *

- * @return New target page - */ - String doChangePersonalData (); - - /** - * Checks whether this application requires a user name to be entered. - * Otherwise a random name like "userXXXXX" is generated - *

- * @return Whether this application requires a user name - */ - boolean isUserNameRequired (); - - /** - * Checks wether public user profiles are enabled. This requires that user - * names are also enabled. - *

- * @return Whether public user profiles are enabled - */ - boolean isPublicUserProfileEnabled (); - -} diff --git a/src/java/org/mxchange/addressbook/beans/user/activity/AddressbookUserActivityWebApplicationBean.java b/src/java/org/mxchange/addressbook/beans/user/activity/AddressbookUserActivityWebApplicationBean.java deleted file mode 100644 index 28b6d53a..00000000 --- a/src/java/org/mxchange/addressbook/beans/user/activity/AddressbookUserActivityWebApplicationBean.java +++ /dev/null @@ -1,674 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * 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.addressbook.beans.user.activity; - -import java.text.MessageFormat; -import java.util.Collections; -import java.util.GregorianCalendar; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import javax.annotation.PostConstruct; -import javax.ejb.EJB; -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.event.Observes; -import javax.inject.Named; -import org.mxchange.addressbook.beans.BaseAddressbookController; -import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent; -import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent; -import org.mxchange.jusercore.events.user.delete.ObservableAdminDeletedUserEvent; -import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent; -import org.mxchange.jusercore.events.user.locked.ObservableAdminLockedUserEvent; -import org.mxchange.jusercore.events.user.unlocked.ObservableAdminUnlockedUserEvent; -import org.mxchange.jusercore.events.user.update.ObservableAdminUpdatedUserDataEvent; -import org.mxchange.jusercore.events.user.update.ObservableUpdatedUserPersonalDataEvent; -import org.mxchange.jusercore.model.user.User; -import org.mxchange.jusercore.model.user.activity.LogableUserActivity; -import org.mxchange.jusercore.model.user.activity.UserActivityLog; -import org.mxchange.jusercore.model.user.activity.UserActivityLogSessionBeanRemote; -import org.mxchange.jusercore.model.user.activity.comparator.UserActivityLogTimestampComparator; -import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent; -import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent; -import org.mxchange.juserlogincore.events.logout.ObservableUserLogoutEvent; -import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent; -import org.mxchange.juserlogincore.events.resendlink.ObservableUserResendLinkAccountEvent; -import org.mxchange.juserlogincore.events.user.password_change.ObservableUpdatedUserPasswordEvent; - -/** - * A controller (bean) for user activity log - *

- * @author Roland Häder - */ -@Named ("userActivityController") -@ApplicationScoped -public class AddressbookUserActivityWebApplicationBean extends BaseAddressbookController implements AddressbookUserActivityWebApplicationController { - - /** - * Serial number - */ - private static final long serialVersionUID = 192_586_376_717_856_904L; - - /** - * User instance - */ - private User user; - - /** - * EJB for user activity log - */ - @EJB (lookup = "java:global/addressbook-ejb/userActivity!org.mxchange.jusercore.model.user.activity.UserActivityLogSessionBeanRemote") - private UserActivityLogSessionBeanRemote userActivityBean; - - /** - * "Cache" for activity log per user - */ - private final Map> usersActivity; - - /** - * Default constructor - */ - @SuppressWarnings ("CollectionWithoutInitialCapacity") - public AddressbookUserActivityWebApplicationBean () { - // Init cache - this.usersActivity = new LinkedHashMap<>(); - } - - /** - * Event observer for newly added users by administrator - *

- * @param event Event being fired - */ - public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent 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 - } - - // Update user list - this.addUserActivity(event.getAddedUser(), "ADMIN_ADDED_USER_ACCOUNT"); //NOI18N - } - - /** - * Event observer for deleted user accounts (by administrator) - *

- * @param event Event being fired - */ - public void afterAdminDeletedUserEvent (@Observes final ObservableAdminDeletedUserEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getDeletedUser() == null) { - // Throw NPE again - throw new NullPointerException("event.deletedUser is null"); //NOI18N - } else if (event.getDeletedUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.deletedUser.userId is null"); //NOI18N - } else if (event.getDeletedUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getDeletedUser(), event.getDeletedUser().getUserId())); //NOI18N - } - - // Add acitivity - this.addUserActivity(event.getDeletedUser(), "ADMIN_DELETED_USER_ACCOUNT", event.getUserDeleteReason()); //NOI18N - } - - /** - * Event observer for linked users with existing contact data - *

- * @param event Event being fired - */ - public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getLinkedUser() == null) { - // Throw NPE again - throw new NullPointerException("event.linkedUser is null"); //NOI18N - } else if (event.getLinkedUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.linkedUser.userId is null"); //NOI18N - } else if (event.getLinkedUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserId())); //NOI18N - } - - // Update user list - this.addUserActivity(event.getLinkedUser(), "ADMIN_LINKED_USER_ACCOUNT"); //NOI18N - } - - /** - * Event observer for locked users - *

- * @param event Event being fired - */ - public void afterAdminLockedUserEvent (@Observes final ObservableAdminLockedUserEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getLockedUser() == null) { - // Throw NPE again - throw new NullPointerException("event.lockedUser is null"); //NOI18N - } else if (event.getLockedUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.lockedUser.userId is null"); //NOI18N - } else if (event.getLockedUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLockedUser(), event.getLockedUser().getUserId())); //NOI18N - } - - // Update user list - this.addUserActivity(event.getLockedUser(), "ADMIN_LOCKED_USER_ACCOUNT", event.getLockedUser().getUserLastLockedReason()); //NOI18N - } - - /** - * Event observer for unlocked users - *

- * @param event Event being fired - */ - public void afterAdminUnlockedUserEvent (@Observes final ObservableAdminUnlockedUserEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getUnlockedUser() == null) { - // Throw NPE again - throw new NullPointerException("event.unlockedUser is null"); //NOI18N - } else if (event.getUnlockedUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.unlockedUser.userId is null"); //NOI18N - } else if (event.getUnlockedUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUnlockedUser(), event.getUnlockedUser().getUserId())); //NOI18N - } - - // Update user list - this.addUserActivity(event.getUnlockedUser(), "ADMIN_UNLOCKED_USER_ACCOUNT"); //NOI18N - } - - /** - * Event observer for updated user data by administrator - *

- * @param event Event being updated - */ - public void afterAdminUpdatedUserDataEvent (@Observes final ObservableAdminUpdatedUserDataEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getUpdatedUser() == null) { - // Throw NPE again - throw new NullPointerException("event.updatedUser is null"); //NOI18N - } else if (event.getUpdatedUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N - } else if (event.getUpdatedUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N - } - - // Update user list - this.addUserActivity(event.getUpdatedUser(), "ADMIN_UPDATED_USER_PERSONAL_DATA"); //NOI18N - } - - /** - * Event observer for when a bean helper has successfully created a user - * instance, means the user exists. If the user does not exist, this event - * should not fire but instead a proper exception must be thrown. - *

- * @param event User created event - */ - public void afterCreatedUserEvent (@Observes final ObservableCreatedUserEvent event) { - // Is the instance valid? - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getCreatedUser() == null) { - // Throw NPE again - throw new NullPointerException("event.createdUser is null"); //NOI18N - } else if (event.getCreatedUser().getUserId() == null) { - // Throw NPE again - throw new NullPointerException("event.createdUser.userId is null"); //NOI18N - } else if (event.getCreatedUser().getUserId() < 1) { - // Throw NPE again - throw new NullPointerException(MessageFormat.format("event.createdUser.userId={0} is not valid", event.getCreatedUser().getUserId())); //NOI18N - } - - // Set whole user - this.setUser(event.getCreatedUser()); - } - - /** - * Event observer when user confirmed account. - *

- * @param event Event being fired - */ - public void afterUserConfirmedAccountEvent (@Observes final ObservableUserConfirmedAccountEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getConfirmedUser() == null) { - // Throw NPE again - throw new NullPointerException("event.confirmedUser is null"); //NOI18N - } else if (event.getConfirmedUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N - } else if (event.getConfirmedUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N - } - - // Update user list - this.addUserActivity(event.getConfirmedUser(), "USER_CONFIRMED_ACCOUNT"); //NOI18N - } - - /** - * Event observer for logged-in user - *

- * @param event Event being fired - */ - public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getLoggedInUser() == null) { - // Throw NPE again - throw new NullPointerException("event.registeredUser is null"); //NOI18N - } else if (event.getLoggedInUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N - } else if (event.getLoggedInUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N - } - - // Copy all data to this bean - this.addUserActivity(event.getLoggedInUser(), "USER_LOGGED_IN"); //NOI18N - } - - /** - * Event observer for logged-out user - *

- * @param event Event instance - */ - public void afterUserLogoutEvent (@Observes final ObservableUserLogoutEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getLoggedOutUser() == null) { - // Throw NPE again - throw new NullPointerException("event.loggedOutUser is null"); //NOI18N - } else if (event.getLoggedOutUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.loggedOutUser.userId is null"); //NOI18N - } else if (event.getLoggedOutUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedOutUser(), event.getLoggedOutUser().getUserId())); //NOI18N - } - - // Update user list - this.addUserActivity(event.getLoggedOutUser(), "USER_LOGGED_OUT"); //NOI18N - } - - /** - * Event observer for new user registrations - *

- * @param event Event being fired - */ - public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getRegisteredUser() == null) { - // Throw NPE again - throw new NullPointerException("event.registeredUser is null"); //NOI18N - } else if (event.getRegisteredUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N - } else if (event.getRegisteredUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N - } - - // Update user list - this.addUserActivity(event.getRegisteredUser(), "USER_REGISTERED_NEW_ACCOUNT"); //NOI18N - } - - /** - * Event observer for users resending their confirmation link - *

- * @param event Event being fired - */ - public void afterUserResendConfirmationLinkEvent (@Observes final ObservableUserResendLinkAccountEvent event) { - // event should not be null - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getResendLinkUser() == null) { - // Throw NPE again - throw new NullPointerException("event.resendLinkUser is null"); //NOI18N - } else if (event.getResendLinkUser().getUserId() == null) { - // userId is null - throw new NullPointerException("event.resendLinkUser.userId is null"); //NOI18N - } else if (event.getResendLinkUser().getUserId() < 1) { - // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getResendLinkUser(), event.getResendLinkUser().getUserId())); //NOI18N - } - - // Copy all data to this bean - this.addUserActivity(event.getResendLinkUser(), "USER_RESEND_CONFIRMATION_LINK"); //NOI18N - } - - /** - * Method being call after user's password has been updated (and history - * entry has been created). - *

- * @param event Event being observed - */ - public void afterUserUpdatedPasswordEvent (@Observes final ObservableUpdatedUserPasswordEvent event) { - // Check parameter - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getPasswordHistory() == null) { - // Throw NPE again - throw new NullPointerException("event.passwordHistory is null"); //NOI18N - } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) { - // ... and again - throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N - } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) { - // Invalid value - throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N - } - - // Update user list - this.addUserActivity(event.getPasswordHistory().getUserPasswordHistoryUser(), "USER_UPDATED_PASSWORD"); //NOI18N - } - - /** - * Listens to fired event when user updated personal data - *

- * @param event Event being fired - */ - public void afterUserUpdatedPersonalDataEvent (@Observes final ObservableUpdatedUserPersonalDataEvent event) { - // Check parameter - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getUpdatedUser() == null) { - // Throw NPE again - throw new NullPointerException("event.updatedUser is null"); //NOI18N - } else if (event.getUpdatedUser().getUserId() == null) { - // ... and again - throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N - } else if (event.getUpdatedUser().getUserId() < 1) { - // Invalid value - throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N - } - - // Update user list - this.addUserActivity(event.getUpdatedUser(), "USER_UPDATED_PERSONAL_DATA"); //NOI18N - } - - /** - * Expands given activity type into a i18n string for administrators - *

- * @param activityType Activity type - *

- * @return Expanded i18n string - */ - public String expandAdminActivityType (final String activityType) { - // Is it valid? - if (null == activityType) { - // Throw NPE - throw new NullPointerException("activityType is null"); //NOI18N - } else if (activityType.isEmpty()) { - // Is empty - throw new IllegalArgumentException("activityType is empty"); //NOI18N - } - - // Expand it - return "ADMIN_ACTIVITY_" + activityType; //NOI18N - } - - /** - * Expands given activity type into a i18n string for users - *

- * @param activityType Activity type - *

- * @return Expanded i18n string - */ - public String expandUserActivityType (final String activityType) { - // Is it valid? - if (null == activityType) { - // Throw NPE - throw new NullPointerException("activityType is null"); //NOI18N - } else if (activityType.isEmpty()) { - // Is empty - throw new IllegalArgumentException("activityType is empty"); //NOI18N - } - - // Expand it - return "USER_ACTIVITY_" + activityType; //NOI18N - } - - /** - * Returns a list of in beanHelper set user instance's activity log - *

- * @return List of user's activity log - */ - public List fetchCurrentUsersActivityLog () { - // beanHelper.user should be set and valid - if (this.getUser() == null) { - // Is not set - throw new NullPointerException("this.beanHelper.user is null"); //NOI18N - } else if (this.getUser().getUserId() == null) { - // Throw NPE again - throw new NullPointerException("this.beanHelper.user.userId is null"); //NOI18N - } else if (this.getUser().getUserId() < 1) { - // Invalid id number - throw new IllegalArgumentException(MessageFormat.format("this.beanHelper.user.userId={0} is not valid", this.getUser().getUserId())); //NOI18N - } - - // Init list - List list = new LinkedList<>(); - - // Is the user set? - if (this.usersActivity.containsKey(this.getUser())) { - // Return it - list.addAll(this.usersActivity.get(this.getUser())); - - // Sort list and reverse it - Collections.sort(list, new UserActivityLogTimestampComparator()); - Collections.reverse(list); - } - - // Return it - return list; - } - - /** - * Getter for user instance - *

- * @return User instance - */ - public User getUser () { - return this.user; - } - - /** - * Setter for user instance - *

- * @param user User instance - */ - public void setUser (final User user) { - this.user = user; - } - - /** - * Post-constructor method - */ - @PostConstruct - public void init () { - // Get whole list - List list = this.userActivityBean.fetchAllUserActivityLog(); - - // Put all in map, per-user - for (final LogableUserActivity userActivity : list) { - // Is the list there? - if (!this.usersActivity.containsKey(userActivity.getActivityUser())) { - // Init list - this.usersActivity.put(userActivity.getActivityUser(), new LinkedList()); - } - - // Add by user instance - boolean added = this.usersActivity.get(userActivity.getActivityUser()).add(userActivity); - - // Should be added - assert (added) : "Activity log not added"; //NOI18N - } - } - - /** - * Adds user activity entry with given type - *

- * @param user User instance - * @param activityType Activity type - */ - private void addUserActivity (final User user, final String activityType) { - // Better re-validate - if (null == user) { - // Throw NPE - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() == null) { - // Throw again - throw new NullPointerException("user.userId is null"); //NOI18N - } else if (user.getUserId() < 1) { - // Invalid id number - throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N - } else if (null == activityType) { - // Throw NPE again - throw new NullPointerException("activityType is null"); //NOI18N - } else if (activityType.isEmpty()) { - // Is empty - throw new IllegalArgumentException("activityType is empty"); //NOI18N - } - - // Create new activity object - LogableUserActivity userActivity = new UserActivityLog(activityType, user, new GregorianCalendar(), this.determinePrincipalName()); - - // Call bean to add it - this.userActivityBean.addUserActivityLog(userActivity); - - // Add to cache, too - this.addUserActivityToCache(userActivity); - } - - /** - * Adds user activity log with type and message - *

- * @param user User instance - * @param activityType Activity type - * @param message Activity message - */ - private void addUserActivity (final User user, final String activityType, final String message) { - // Better re-validate - if (null == user) { - // Throw NPE - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() == null) { - // Throw again - throw new NullPointerException("user.userId is null"); //NOI18N - } else if (user.getUserId() < 1) { - // Invalid id number - throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N - } else if (null == activityType) { - // Throw NPE again - throw new NullPointerException("activityType is null"); //NOI18N - } else if (activityType.isEmpty()) { - // Is empty - throw new IllegalArgumentException("activityType is empty"); //NOI18N - } else if (null == message) { - // Throw NPE again - throw new NullPointerException("message is null"); //NOI18N - } else if (message.isEmpty()) { - // Is empty - throw new IllegalArgumentException("message is empty"); //NOI18N - } - - // Create new activity object - LogableUserActivity userActivity = new UserActivityLog(message, activityType, user, new GregorianCalendar(), this.determinePrincipalName()); - - // Call bean to add it - this.userActivityBean.addUserActivityLog(userActivity); - - // Add to cache, too - this.addUserActivityToCache(userActivity); - } - - /** - * Adds given user activity instance to "cache" (local map) - *

- * @param userActivity User activity instance - */ - private void addUserActivityToCache (final LogableUserActivity userActivity) { - // Is the instance valid - if (null == userActivity) { - // Throw NPE again - throw new NullPointerException("userActivity is null"); - } else if (userActivity.getActivityId() instanceof Long) { - // Is not null - throw new IllegalArgumentException("userActivity.activityId=" + userActivity.getActivityId() + " is not null"); - } else if (userActivity.getActivityUser() == null) { - // Throw NPE again - throw new NullPointerException("userActivity.activityUser is null"); - } else if (userActivity.getActivityUser().getUserId() == null) { - // Throw it again - throw new NullPointerException("userActivity.activityUser.userId is null"); - } else if (userActivity.getActivityUser().getUserId() < 1) { - // Invalid id number - throw new IllegalArgumentException("userActivity.activityUser.userId=" + userActivity.getActivityUser().getUserId() + " is not valid"); - } - - // Is it there? - if (!this.usersActivity.containsKey(userActivity.getActivityUser())) { - // Init it - this.usersActivity.put(userActivity.getActivityUser(), new LinkedList()); - } - - // Add activity to log - this.usersActivity.get(userActivity.getActivityUser()).add(userActivity); - } - -} diff --git a/src/java/org/mxchange/addressbook/beans/user/activity/AddressbookUserActivityWebApplicationController.java b/src/java/org/mxchange/addressbook/beans/user/activity/AddressbookUserActivityWebApplicationController.java deleted file mode 100644 index e048d81f..00000000 --- a/src/java/org/mxchange/addressbook/beans/user/activity/AddressbookUserActivityWebApplicationController.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * 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.addressbook.beans.user.activity; - -import java.io.Serializable; - -/** - * A controller (bean) interface for user activity log - *

- * @author Roland Häder - */ -public interface AddressbookUserActivityWebApplicationController extends Serializable { - -} diff --git a/src/java/org/mxchange/addressbook/beans/user/activity/AddressbookUserActivityWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/user/activity/AddressbookUserActivityWebRequestBean.java new file mode 100644 index 00000000..aaa04f01 --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/user/activity/AddressbookUserActivityWebRequestBean.java @@ -0,0 +1,675 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * 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.addressbook.beans.user.activity; + +import java.text.MessageFormat; +import java.util.Collections; +import java.util.GregorianCalendar; +import java.util.LinkedList; +import java.util.List; +import javax.annotation.PostConstruct; +import javax.ejb.EJB; +import javax.enterprise.context.RequestScoped; +import javax.enterprise.event.Observes; +import javax.inject.Inject; +import javax.inject.Named; +import org.mxchange.addressbook.beans.BaseAddressbookController; +import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent; +import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent; +import org.mxchange.jusercore.events.user.delete.ObservableAdminDeletedUserEvent; +import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent; +import org.mxchange.jusercore.events.user.locked.ObservableAdminLockedUserEvent; +import org.mxchange.jusercore.events.user.unlocked.ObservableAdminUnlockedUserEvent; +import org.mxchange.jusercore.events.user.update.ObservableAdminUpdatedUserDataEvent; +import org.mxchange.jusercore.events.user.update.ObservableUpdatedUserPersonalDataEvent; +import org.mxchange.jusercore.model.user.User; +import org.mxchange.jusercore.model.user.activity.LogableUserActivity; +import org.mxchange.jusercore.model.user.activity.UserActivityLog; +import org.mxchange.jusercore.model.user.activity.UserActivityLogSessionBeanRemote; +import org.mxchange.jusercore.model.user.activity.comparator.UserActivityLogTimestampComparator; +import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent; +import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent; +import org.mxchange.juserlogincore.events.logout.ObservableUserLogoutEvent; +import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent; +import org.mxchange.juserlogincore.events.resendlink.ObservableUserResendLinkAccountEvent; +import org.mxchange.juserlogincore.events.user.password_change.ObservableUpdatedUserPasswordEvent; + +/** + * A controller (bean) for user activity log + *

+ * @author Roland Häder + */ +@Named ("userActivityController") +@RequestScoped +public class AddressbookUserActivityWebRequestBean extends BaseAddressbookController implements AddressbookUserActivityWebRequestController { + + /** + * Serial number + */ + private static final long serialVersionUID = 192_586_376_717_856_904L; + + /** + * User instance + */ + private User user; + + /** + * EJB for user activity log + */ + @EJB (lookup = "java:global/addressbook-ejb/userActivity!org.mxchange.jusercore.model.user.activity.UserActivityLogSessionBeanRemote") + private UserActivityLogSessionBeanRemote userActivityBean; + + /** + * "Cache" for activity log per user + */ + @Inject + @Cached(cacheName = "usersActivityCache") + private transient Cache> usersActivityCache; + + /** + * Default constructor + */ + @SuppressWarnings ("CollectionWithoutInitialCapacity") + public AddressbookUserActivityWebRequestBean () { + // Call super constructor + super(); + } + + /** + * Event observer for newly added users by administrator + *

+ * @param event Event being fired + */ + public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent 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 + } + + // Update user list + this.addUserActivity(event.getAddedUser(), "ADMIN_ADDED_USER_ACCOUNT"); //NOI18N + } + + /** + * Event observer for deleted user accounts (by administrator) + *

+ * @param event Event being fired + */ + public void afterAdminDeletedUserEvent (@Observes final ObservableAdminDeletedUserEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getDeletedUser() == null) { + // Throw NPE again + throw new NullPointerException("event.deletedUser is null"); //NOI18N + } else if (event.getDeletedUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.deletedUser.userId is null"); //NOI18N + } else if (event.getDeletedUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getDeletedUser(), event.getDeletedUser().getUserId())); //NOI18N + } + + // Add acitivity + this.addUserActivity(event.getDeletedUser(), "ADMIN_DELETED_USER_ACCOUNT", event.getUserDeleteReason()); //NOI18N + } + + /** + * Event observer for linked users with existing contact data + *

+ * @param event Event being fired + */ + public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getLinkedUser() == null) { + // Throw NPE again + throw new NullPointerException("event.linkedUser is null"); //NOI18N + } else if (event.getLinkedUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.linkedUser.userId is null"); //NOI18N + } else if (event.getLinkedUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserId())); //NOI18N + } + + // Update user list + this.addUserActivity(event.getLinkedUser(), "ADMIN_LINKED_USER_ACCOUNT"); //NOI18N + } + + /** + * Event observer for locked users + *

+ * @param event Event being fired + */ + public void afterAdminLockedUserEvent (@Observes final ObservableAdminLockedUserEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getLockedUser() == null) { + // Throw NPE again + throw new NullPointerException("event.lockedUser is null"); //NOI18N + } else if (event.getLockedUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.lockedUser.userId is null"); //NOI18N + } else if (event.getLockedUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLockedUser(), event.getLockedUser().getUserId())); //NOI18N + } + + // Update user list + this.addUserActivity(event.getLockedUser(), "ADMIN_LOCKED_USER_ACCOUNT", event.getLockedUser().getUserLastLockedReason()); //NOI18N + } + + /** + * Event observer for unlocked users + *

+ * @param event Event being fired + */ + public void afterAdminUnlockedUserEvent (@Observes final ObservableAdminUnlockedUserEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getUnlockedUser() == null) { + // Throw NPE again + throw new NullPointerException("event.unlockedUser is null"); //NOI18N + } else if (event.getUnlockedUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.unlockedUser.userId is null"); //NOI18N + } else if (event.getUnlockedUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUnlockedUser(), event.getUnlockedUser().getUserId())); //NOI18N + } + + // Update user list + this.addUserActivity(event.getUnlockedUser(), "ADMIN_UNLOCKED_USER_ACCOUNT"); //NOI18N + } + + /** + * Event observer for updated user data by administrator + *

+ * @param event Event being updated + */ + public void afterAdminUpdatedUserDataEvent (@Observes final ObservableAdminUpdatedUserDataEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getUpdatedUser() == null) { + // Throw NPE again + throw new NullPointerException("event.updatedUser is null"); //NOI18N + } else if (event.getUpdatedUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N + } else if (event.getUpdatedUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N + } + + // Update user list + this.addUserActivity(event.getUpdatedUser(), "ADMIN_UPDATED_USER_PERSONAL_DATA"); //NOI18N + } + + /** + * Event observer for when a bean helper has successfully created a user + * instance, means the user exists. If the user does not exist, this event + * should not fire but instead a proper exception must be thrown. + *

+ * @param event User created event + */ + public void afterCreatedUserEvent (@Observes final ObservableCreatedUserEvent event) { + // Is the instance valid? + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getCreatedUser() == null) { + // Throw NPE again + throw new NullPointerException("event.createdUser is null"); //NOI18N + } else if (event.getCreatedUser().getUserId() == null) { + // Throw NPE again + throw new NullPointerException("event.createdUser.userId is null"); //NOI18N + } else if (event.getCreatedUser().getUserId() < 1) { + // Throw NPE again + throw new NullPointerException(MessageFormat.format("event.createdUser.userId={0} is not valid", event.getCreatedUser().getUserId())); //NOI18N + } + + // Set whole user + this.setUser(event.getCreatedUser()); + } + + /** + * Event observer when user confirmed account. + *

+ * @param event Event being fired + */ + public void afterUserConfirmedAccountEvent (@Observes final ObservableUserConfirmedAccountEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getConfirmedUser() == null) { + // Throw NPE again + throw new NullPointerException("event.confirmedUser is null"); //NOI18N + } else if (event.getConfirmedUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N + } else if (event.getConfirmedUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N + } + + // Update user list + this.addUserActivity(event.getConfirmedUser(), "USER_CONFIRMED_ACCOUNT"); //NOI18N + } + + /** + * Event observer for logged-in user + *

+ * @param event Event being fired + */ + public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getLoggedInUser() == null) { + // Throw NPE again + throw new NullPointerException("event.registeredUser is null"); //NOI18N + } else if (event.getLoggedInUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N + } else if (event.getLoggedInUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N + } + + // Copy all data to this bean + this.addUserActivity(event.getLoggedInUser(), "USER_LOGGED_IN"); //NOI18N + } + + /** + * Event observer for logged-out user + *

+ * @param event Event instance + */ + public void afterUserLogoutEvent (@Observes final ObservableUserLogoutEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getLoggedOutUser() == null) { + // Throw NPE again + throw new NullPointerException("event.loggedOutUser is null"); //NOI18N + } else if (event.getLoggedOutUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.loggedOutUser.userId is null"); //NOI18N + } else if (event.getLoggedOutUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedOutUser(), event.getLoggedOutUser().getUserId())); //NOI18N + } + + // Update user list + this.addUserActivity(event.getLoggedOutUser(), "USER_LOGGED_OUT"); //NOI18N + } + + /** + * Event observer for new user registrations + *

+ * @param event Event being fired + */ + public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getRegisteredUser() == null) { + // Throw NPE again + throw new NullPointerException("event.registeredUser is null"); //NOI18N + } else if (event.getRegisteredUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N + } else if (event.getRegisteredUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N + } + + // Update user list + this.addUserActivity(event.getRegisteredUser(), "USER_REGISTERED_NEW_ACCOUNT"); //NOI18N + } + + /** + * Event observer for users resending their confirmation link + *

+ * @param event Event being fired + */ + public void afterUserResendConfirmationLinkEvent (@Observes final ObservableUserResendLinkAccountEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getResendLinkUser() == null) { + // Throw NPE again + throw new NullPointerException("event.resendLinkUser is null"); //NOI18N + } else if (event.getResendLinkUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.resendLinkUser.userId is null"); //NOI18N + } else if (event.getResendLinkUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getResendLinkUser(), event.getResendLinkUser().getUserId())); //NOI18N + } + + // Copy all data to this bean + this.addUserActivity(event.getResendLinkUser(), "USER_RESEND_CONFIRMATION_LINK"); //NOI18N + } + + /** + * Method being call after user's password has been updated (and history + * entry has been created). + *

+ * @param event Event being observed + */ + public void afterUserUpdatedPasswordEvent (@Observes final ObservableUpdatedUserPasswordEvent event) { + // Check parameter + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getPasswordHistory() == null) { + // Throw NPE again + throw new NullPointerException("event.passwordHistory is null"); //NOI18N + } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) { + // ... and again + throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N + } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) { + // Invalid value + throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N + } + + // Update user list + this.addUserActivity(event.getPasswordHistory().getUserPasswordHistoryUser(), "USER_UPDATED_PASSWORD"); //NOI18N + } + + /** + * Listens to fired event when user updated personal data + *

+ * @param event Event being fired + */ + public void afterUserUpdatedPersonalDataEvent (@Observes final ObservableUpdatedUserPersonalDataEvent event) { + // Check parameter + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getUpdatedUser() == null) { + // Throw NPE again + throw new NullPointerException("event.updatedUser is null"); //NOI18N + } else if (event.getUpdatedUser().getUserId() == null) { + // ... and again + throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N + } else if (event.getUpdatedUser().getUserId() < 1) { + // Invalid value + throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N + } + + // Update user list + this.addUserActivity(event.getUpdatedUser(), "USER_UPDATED_PERSONAL_DATA"); //NOI18N + } + + /** + * Expands given activity type into a i18n string for administrators + *

+ * @param activityType Activity type + *

+ * @return Expanded i18n string + */ + public String expandAdminActivityType (final String activityType) { + // Is it valid? + if (null == activityType) { + // Throw NPE + throw new NullPointerException("activityType is null"); //NOI18N + } else if (activityType.isEmpty()) { + // Is empty + throw new IllegalArgumentException("activityType is empty"); //NOI18N + } + + // Expand it + return "ADMIN_ACTIVITY_" + activityType; //NOI18N + } + + /** + * Expands given activity type into a i18n string for users + *

+ * @param activityType Activity type + *

+ * @return Expanded i18n string + */ + public String expandUserActivityType (final String activityType) { + // Is it valid? + if (null == activityType) { + // Throw NPE + throw new NullPointerException("activityType is null"); //NOI18N + } else if (activityType.isEmpty()) { + // Is empty + throw new IllegalArgumentException("activityType is empty"); //NOI18N + } + + // Expand it + return "USER_ACTIVITY_" + activityType; //NOI18N + } + + /** + * Returns a list of in beanHelper set user instance's activity log + *

+ * @return List of user's activity log + */ + public List fetchCurrentUsersActivityLog () { + // beanHelper.user should be set and valid + if (this.getUser() == null) { + // Is not set + throw new NullPointerException("this.beanHelper.user is null"); //NOI18N + } else if (this.getUser().getUserId() == null) { + // Throw NPE again + throw new NullPointerException("this.beanHelper.user.userId is null"); //NOI18N + } else if (this.getUser().getUserId() < 1) { + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("this.beanHelper.user.userId={0} is not valid", this.getUser().getUserId())); //NOI18N + } + + // Init list + List list = new LinkedList<>(); + + // Is the user set? + if (this.usersActivityCache.containsKey(this.getUser())) { + // Return it + list.addAll(this.usersActivityCache.get(this.getUser())); + + // Sort list and reverse it + Collections.sort(list, new UserActivityLogTimestampComparator()); + Collections.reverse(list); + } + + // Return it + return list; + } + + /** + * Getter for user instance + *

+ * @return User instance + */ + public User getUser () { + return this.user; + } + + /** + * Setter for user instance + *

+ * @param user User instance + */ + public void setUser (final User user) { + this.user = user; + } + + /** + * Post-constructor method + */ + @PostConstruct + public void init () { + // Get whole list + List list = this.userActivityBean.fetchAllUserActivityLog(); + + // Put all in map, per-user + for (final LogableUserActivity userActivity : list) { + // Is the list there? + if (!this.usersActivityCache.containsKey(userActivity.getActivityUser())) { + // Init list + this.usersActivityCache.put(userActivity.getActivityUser(), new LinkedList()); + } + + // Add by user instance + boolean added = this.usersActivityCache.get(userActivity.getActivityUser()).add(userActivity); + + // Should be added + assert (added) : "Activity log not added"; //NOI18N + } + } + + /** + * Adds user activity entry with given type + *

+ * @param user User instance + * @param activityType Activity type + */ + private void addUserActivity (final User user, final String activityType) { + // Better re-validate + if (null == user) { + // Throw NPE + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Throw again + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N + } else if (null == activityType) { + // Throw NPE again + throw new NullPointerException("activityType is null"); //NOI18N + } else if (activityType.isEmpty()) { + // Is empty + throw new IllegalArgumentException("activityType is empty"); //NOI18N + } + + // Create new activity object + LogableUserActivity userActivity = new UserActivityLog(activityType, user, new GregorianCalendar(), this.determinePrincipalName()); + + // Call bean to add it + this.userActivityBean.addUserActivityLog(userActivity); + + // Add to cache, too + this.addUserActivityToCache(userActivity); + } + + /** + * Adds user activity log with type and message + *

+ * @param user User instance + * @param activityType Activity type + * @param message Activity message + */ + private void addUserActivity (final User user, final String activityType, final String message) { + // Better re-validate + if (null == user) { + // Throw NPE + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Throw again + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N + } else if (null == activityType) { + // Throw NPE again + throw new NullPointerException("activityType is null"); //NOI18N + } else if (activityType.isEmpty()) { + // Is empty + throw new IllegalArgumentException("activityType is empty"); //NOI18N + } else if (null == message) { + // Throw NPE again + throw new NullPointerException("message is null"); //NOI18N + } else if (message.isEmpty()) { + // Is empty + throw new IllegalArgumentException("message is empty"); //NOI18N + } + + // Create new activity object + LogableUserActivity userActivity = new UserActivityLog(message, activityType, user, new GregorianCalendar(), this.determinePrincipalName()); + + // Call bean to add it + this.userActivityBean.addUserActivityLog(userActivity); + + // Add to cache, too + this.addUserActivityToCache(userActivity); + } + + /** + * Adds given user activity instance to "cache" (local map) + *

+ * @param userActivity User activity instance + */ + private void addUserActivityToCache (final LogableUserActivity userActivity) { + // Is the instance valid + if (null == userActivity) { + // Throw NPE again + throw new NullPointerException("userActivity is null"); + } else if (userActivity.getActivityId() instanceof Long) { + // Is not null + throw new IllegalArgumentException("userActivity.activityId=" + userActivity.getActivityId() + " is not null"); + } else if (userActivity.getActivityUser() == null) { + // Throw NPE again + throw new NullPointerException("userActivity.activityUser is null"); + } else if (userActivity.getActivityUser().getUserId() == null) { + // Throw it again + throw new NullPointerException("userActivity.activityUser.userId is null"); + } else if (userActivity.getActivityUser().getUserId() < 1) { + // Invalid id number + throw new IllegalArgumentException("userActivity.activityUser.userId=" + userActivity.getActivityUser().getUserId() + " is not valid"); + } + + // Is it there? + if (!this.usersActivityCache.containsKey(userActivity.getActivityUser())) { + // Init it + this.usersActivityCache.put(userActivity.getActivityUser(), new LinkedList()); + } + + // Add activity to log + this.usersActivityCache.get(userActivity.getActivityUser()).add(userActivity); + } + +} diff --git a/src/java/org/mxchange/addressbook/beans/user/activity/AddressbookUserActivityWebRequestController.java b/src/java/org/mxchange/addressbook/beans/user/activity/AddressbookUserActivityWebRequestController.java new file mode 100644 index 00000000..57560c64 --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/user/activity/AddressbookUserActivityWebRequestController.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * 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.addressbook.beans.user.activity; + +import java.io.Serializable; + +/** + * A controller (bean) interface for user activity log + *

+ * @author Roland Häder + */ +public interface AddressbookUserActivityWebRequestController extends Serializable { + +} diff --git a/src/java/org/mxchange/addressbook/beans/user/confirmlink/AddressbookConfirmationLinkWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/user/confirmlink/AddressbookConfirmationLinkWebRequestBean.java index d3b28b64..0f1ff869 100644 --- a/src/java/org/mxchange/addressbook/beans/user/confirmlink/AddressbookConfirmationLinkWebRequestBean.java +++ b/src/java/org/mxchange/addressbook/beans/user/confirmlink/AddressbookConfirmationLinkWebRequestBean.java @@ -27,11 +27,11 @@ import javax.enterprise.inject.Any; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; -import org.mxchange.addressbook.beans.BaseAddressbookController; -import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController; import org.mxchange.jcoreee.events.helper.clear.HelperCleanupEvent; import org.mxchange.jcoreee.events.helper.clear.ObservableHelperCleanupEvent; import org.mxchange.jcoreee.utils.FacesUtils; +<<<<<<< HEAD:src/java/org/mxchange/addressbook/beans/user/confirmlink/AddressbookConfirmationLinkWebRequestBean.java +import org.mxchange.addressbook.beans.BaseAddressbookController; import org.mxchange.jusercore.events.user.created.CreatedUserEvent; import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent; import org.mxchange.jusercore.exceptions.UserStatusConfirmedException; @@ -41,6 +41,7 @@ import org.mxchange.jusercore.model.user.UserSessionBeanRemote; import org.mxchange.jusercore.model.user.status.UserAccountStatus; import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent; import org.mxchange.juserlogincore.events.confirmation.UserConfirmedAccountEvent; +import org.mxchange.addressbook.beans.user.AddressbookUserWebRequestController; /** * A web request bean for confirmation link handling @@ -85,7 +86,7 @@ public class AddressbookConfirmationLinkWebRequestBean extends BaseAddressbookCo * User controller */ @Inject - private AddressbookUserWebSessionController userController; + private AddressbookUserWebRequestController userController; /** * Event for when a user instance was created diff --git a/src/java/org/mxchange/addressbook/beans/user/email_address/AddressbookEmailChangeWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/user/email_address/AddressbookEmailChangeWebRequestBean.java new file mode 100644 index 00000000..90579dd7 --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/user/email_address/AddressbookEmailChangeWebRequestBean.java @@ -0,0 +1,266 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * 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.addressbook.beans.user.email_address; + +import java.text.MessageFormat; +import java.util.Iterator; +import java.util.List; +import java.util.Objects; +import javax.annotation.PostConstruct; +import javax.ejb.EJB; +import javax.enterprise.context.SessionScoped; +import javax.faces.view.facelets.FaceletException; +import javax.inject.Inject; +import javax.inject.Named; +import org.mxchange.addressbook.beans.BaseAddressbookController; +import org.mxchange.addressbook.beans.features.AddressbookFeaturesWebApplicationController; +import org.mxchange.addressbook.beans.user.login.AddressbookUserLoginWebSessionController; +import org.mxchange.jcontacts.model.contact.Contact; +import org.mxchange.jcoreee.utils.FacesUtils; +import org.mxchange.jusercore.model.email_address.ChangeableEmailAddress; +import org.mxchange.jusercore.model.email_address.EmailAddressChange; +import org.mxchange.jusercore.model.user.User; +import org.mxchange.jusercore.model.user.email_address.UserEmailChangeSessionBeanRemote; +import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException; + +/** + * A web session-scoped bean for changing email addresses + *

+ * @author Roland Häder + */ +@Named ("userEmailChangeController") +@SessionScoped +public class AddressbookEmailChangeWebRequestBean extends BaseAddressbookController implements AddressbookEmailChangeWebRequestController { + + /** + * Serial number + */ + private static final long serialVersionUID = 186_078_724_659_153L; + + /** + * Email address 1 (changing) + */ + private String emailAddress; + + /** + * Email address 2 (repeat in changing) + */ + private String emailAddressRepeat; + + /** + * Remote email change bean + */ + @EJB (lookup = "java:global/addressbook-ejb/userEmailChange!org.mxchange.jusercore.model.user.email_address.UserEmailChangeSessionBeanRemote") + private UserEmailChangeSessionBeanRemote emailChangeBean; + + /** + * Features controller + */ + @Inject + private AddressbookFeaturesWebApplicationController featureController; + + /** + * Local list of already queued email addresses + */ + @Inject + @Cached (cacheName = "queuedEmailCache") + private transient Cache queuedEmailCache; + + /** + * Login controller (bean) + */ + @Inject + private AddressbookUserLoginWebSessionController userLoginController; + + /** + * Default constructor + */ + public AddressbookEmailChangeWebRequestBean () { + // Call super constructor + super(); + } + + /** + * Changes logged-in user's email address if the current password matches. + *

+ * @return Redirect outcome + */ + public String doUserChangeEmailAddress () { + // This method shall only be called if the user is logged-in + if (!this.userLoginController.isUserLoggedIn()) { + // Not logged-in + throw new IllegalStateException("User is not logged-in"); //NOI18N + } else if (!this.featureController.isFeatureEnabled("user_change_email_address")) { //NOI18N + // Editing is not allowed + throw new IllegalStateException("User tried to change email address"); //NOI18N + } else if (!this.isRequiredChangeEmailAddressSet()) { + // Not all required fields are set + throw new FaceletException("Not all required fields are set."); //NOI18N + } else if (!Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat())) { + // Email address 1+2 mismatch + this.showFacesMessage("form_user_change_email_address:emailAddressRepeat", "ERROR_USER_EMAIL_ADDRESSES_MISMATCH"); //NOI18N + return ""; //NOI18N + } else if (!this.userLoginController.ifCurrentPasswordMatches()) { + // Password not matching + this.showFacesMessage("form_login_user_change_email_address:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N + return ""; //NOI18N + } + + // Get user instance + User user = this.userLoginController.getLoggedInUser(); + + // It should be there, so run some tests on it + assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N + assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N + assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N + assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N + assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N + assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N + + // Check if the email address is already enqueued + if (this.isEmailAddressQueued(this.getEmailAddress())) { + // Clear both email addresses + this.setEmailAddress(null); + this.setEmailAddressRepeat(null); + + // Yes, then abort here + this.showFacesMessage("form_user_change_email_address:emailAddress", "ERROR_USER_CHANGE_EMAIL_ADDRESS_ALREADY_QUEUED"); //NOI18N + return ""; //NOI18N + } + + // Create change object, to save EJB calls, the hash is not generated here + ChangeableEmailAddress emailChange = new EmailAddressChange(user, this.getEmailAddress()); + + // Get base URL + String baseUrl = FacesUtils.generateBaseUrl(); + + // Call EJB + this.emailChangeBean.enqueueEmailAddressForChange(emailChange, baseUrl); + + // Unset all so the user is forced to re-enter it + this.clear(); + + // All fine + return "user_login_email_change_queued"; //NOI18N + } + + /** + * Getter for email address 1 (changing) + *

+ * @return Email address + */ + public String getEmailAddress () { + return this.emailAddress; + } + + /** + * Setter for email address 1 (changing) + *

+ * @param emailAddress Email address 1 + */ + public void setEmailAddress (final String emailAddress) { + this.emailAddress = emailAddress; + } + + /** + * Getter for email address 2 (repeat changing) + *

+ * @return Email address 2 + */ + public String getEmailAddressRepeat () { + return this.emailAddressRepeat; + } + + /** + * Setter for email address 2 (repeat changing) + *

+ * @param emailAddressRepeat Email address 2 + */ + public void setEmailAddressRepeat (final String emailAddressRepeat) { + this.emailAddressRepeat = emailAddressRepeat; + } + + /** + * Post-construction + */ + @PostConstruct + public void init () { + // Is cache there? + if (!this.queuedEmailCache.iterator().hasNext()) { + // Get whole list + List list = this.emailChangeBean.allQueuedAddresses(); + + // Add all + for (final Iterator iterator = list.iterator(); iterator.hasNext();) { + // Get next element + final String next = iterator.next(); + + // Add it to cache + this.queuedEmailCache.put(next, Boolean.TRUE); + } + } + } + + @Override + public boolean isRequiredChangeEmailAddressSet () { + return ((this.getEmailAddress() != null) && + (this.getEmailAddressRepeat() != null)); + } + + /** + * Clears email address fields so the user has to re-enter them + */ + private void clear () { + // Clear fields + this.setEmailAddress(null); + this.setEmailAddressRepeat(null); + } + + /** + * Checks if given email address has already been queued. First a local list + * is being checked, if not found, the EJB is called. Only if found, the + * result is "cached" in the list. + *

+ * @param emailAddress Email address to verify + *

+ * @return Whether the email address in field emailAddress is already queued + */ + private boolean isEmailAddressQueued (final String emailAddress) { + // It should be there + assert (emailAddress != null) : "emailAddress should not be null"; //NOI18N + assert (!emailAddress.trim().isEmpty()) : "emailAddress should not be empty"; //NOI18N + + // Check list + if (this.queuedEmailCache.containsKey(emailAddress)) { + // Okay, found it + return true; + } + + // Check EJB + boolean isQueued = this.emailChangeBean.isEmailAddressEnqueued(emailAddress); + + // Is it there? + if (isQueued) { + // Add to list + this.queuedEmailCache.put(emailAddress, Boolean.TRUE); + } + + // Return status + return isQueued; + } + +} diff --git a/src/java/org/mxchange/addressbook/beans/user/email_address/AddressbookEmailChangeWebRequestController.java b/src/java/org/mxchange/addressbook/beans/user/email_address/AddressbookEmailChangeWebRequestController.java new file mode 100644 index 00000000..0ae35e05 --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/user/email_address/AddressbookEmailChangeWebRequestController.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * 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.addressbook.beans.user.email_address; + +import java.io.Serializable; + +/** + * An interface for an email change controller + *

+ * @author Roland Häder + */ +public interface AddressbookEmailChangeWebRequestController extends Serializable { + + /** + * Checks whether all required are set for changing email address + *

+ * @return Whether the required personal data is set + */ + boolean isRequiredChangeEmailAddressSet (); + +} diff --git a/src/java/org/mxchange/addressbook/beans/user/email_address/AddressbookEmailChangeWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/user/email_address/AddressbookEmailChangeWebSessionBean.java deleted file mode 100644 index 67a7c0c3..00000000 --- a/src/java/org/mxchange/addressbook/beans/user/email_address/AddressbookEmailChangeWebSessionBean.java +++ /dev/null @@ -1,251 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * 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.addressbook.beans.user.email_address; - -import java.text.MessageFormat; -import java.util.List; -import java.util.Objects; -import javax.annotation.PostConstruct; -import javax.ejb.EJB; -import javax.enterprise.context.SessionScoped; -import javax.faces.view.facelets.FaceletException; -import javax.inject.Inject; -import javax.inject.Named; -import org.mxchange.addressbook.beans.BaseAddressbookController; -import org.mxchange.addressbook.beans.features.AddressbookFeaturesWebApplicationController; -import org.mxchange.addressbook.beans.user.login.AddressbookUserLoginWebSessionController; -import org.mxchange.jcontacts.model.contact.Contact; -import org.mxchange.jcoreee.utils.FacesUtils; -import org.mxchange.jusercore.model.email_address.ChangeableEmailAddress; -import org.mxchange.jusercore.model.email_address.EmailAddressChange; -import org.mxchange.jusercore.model.user.User; -import org.mxchange.jusercore.model.user.email_address.UserEmailChangeSessionBeanRemote; -import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException; - -/** - * A web session-scoped bean for changing email addresses - *

- * @author Roland Häder - */ -@Named ("userEmailChangeController") -@SessionScoped -public class AddressbookEmailChangeWebSessionBean extends BaseAddressbookController implements AddressbookEmailChangeWebSessionController { - - /** - * Serial number - */ - private static final long serialVersionUID = 186_078_724_659_153L; - - /** - * Email address 1 (changing) - */ - private String emailAddress; - - /** - * Email address 2 (repeat in changing) - */ - private String emailAddressRepeat; - - /** - * Local list of already queued email addresses - */ - private List emailAddresses; - - /** - * Remote email change bean - */ - @EJB (lookup = "java:global/addressbook-ejb/userEmailChange!org.mxchange.jusercore.model.user.email_address.UserEmailChangeSessionBeanRemote") - private UserEmailChangeSessionBeanRemote emailChangeBean; - - /** - * Features controller - */ - @Inject - private AddressbookFeaturesWebApplicationController featureController; - - /** - * Login controller (bean) - */ - @Inject - private AddressbookUserLoginWebSessionController userLoginController; - - /** - * Default constructor - */ - public AddressbookEmailChangeWebSessionBean () { - // Call super constructor - super(); - } - - /** - * Changes logged-in user's email address if the current password matches. - *

- * @return Redirect outcome - */ - public String doUserChangeEmailAddress () { - // This method shall only be called if the user is logged-in - if (!this.userLoginController.isUserLoggedIn()) { - // Not logged-in - throw new IllegalStateException("User is not logged-in"); //NOI18N - } else if (!this.featureController.isFeatureEnabled("user_change_email_address")) { //NOI18N - // Editing is not allowed - throw new IllegalStateException("User tried to change email address"); //NOI18N - } else if (!this.isRequiredChangeEmailAddressSet()) { - // Not all required fields are set - throw new FaceletException("Not all required fields are set."); //NOI18N - } else if (!Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat())) { - // Email address 1+2 mismatch - this.showFacesMessage("form_user_change_email_address:emailAddressRepeat", "ERROR_USER_EMAIL_ADDRESSES_MISMATCH"); //NOI18N - return ""; //NOI18N - } else if (!this.userLoginController.ifCurrentPasswordMatches()) { - // Password not matching - this.showFacesMessage("form_login_user_change_email_address:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N - return ""; //NOI18N - } - - // Get user instance - User user = this.userLoginController.getLoggedInUser(); - - // It should be there, so run some tests on it - assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N - assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N - assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N - assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N - assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N - assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N - - // Check if the email address is already enqueued - if (this.isEmailAddressQueued(this.getEmailAddress())) { - // Clear both email addresses - this.setEmailAddress(null); - this.setEmailAddressRepeat(null); - - // Yes, then abort here - this.showFacesMessage("form_user_change_email_address:emailAddress", "ERROR_USER_CHANGE_EMAIL_ADDRESS_ALREADY_QUEUED"); //NOI18N - return ""; //NOI18N - } - - // Create change object, to save EJB calls, the hash is not generated here - ChangeableEmailAddress emailChange = new EmailAddressChange(user, this.getEmailAddress()); - - // Get base URL - String baseUrl = FacesUtils.generateBaseUrl(); - - // Call EJB - this.emailChangeBean.enqueueEmailAddressForChange(emailChange, baseUrl); - - // Unset all so the user is forced to re-enter it - this.clear(); - - // All fine - return "user_login_email_change_queued"; //NOI18N - } - - /** - * Getter for email address 1 (changing) - *

- * @return Email address - */ - public String getEmailAddress () { - return this.emailAddress; - } - - /** - * Setter for email address 1 (changing) - *

- * @param emailAddress Email address 1 - */ - public void setEmailAddress (final String emailAddress) { - this.emailAddress = emailAddress; - } - - /** - * Getter for email address 2 (repeat changing) - *

- * @return Email address 2 - */ - public String getEmailAddressRepeat () { - return this.emailAddressRepeat; - } - - /** - * Setter for email address 2 (repeat changing) - *

- * @param emailAddressRepeat Email address 2 - */ - public void setEmailAddressRepeat (final String emailAddressRepeat) { - this.emailAddressRepeat = emailAddressRepeat; - } - - /** - * Post-construction - */ - @PostConstruct - public void init () { - // Init list - this.emailAddresses = this.emailChangeBean.allQueuedAddresses(); - } - - @Override - public boolean isRequiredChangeEmailAddressSet () { - return ((this.getEmailAddress() != null) && - (this.getEmailAddressRepeat() != null)); - } - - /** - * Clears email address fields so the user has to re-enter them - */ - private void clear () { - // Clear fields - this.setEmailAddress(null); - this.setEmailAddressRepeat(null); - } - - /** - * Checks if given email address has already been queued. First a local list - * is being checked, if not found, the EJB is called. Only if found, the - * result is "cached" in the list. - *

- * @param emailAddress Email address to verify - *

- * @return Whether the email address in field emailAddress is already queued - */ - private boolean isEmailAddressQueued (final String emailAddress) { - // It should be there - assert (emailAddress != null) : "emailAddress should not be null"; //NOI18N - assert (!emailAddress.trim().isEmpty()) : "emailAddress should not be empty"; //NOI18N - - // Check list - if (this.emailAddresses.contains(emailAddress)) { - // Okay, found it - return true; - } - - // Check EJB - boolean isQueued = this.emailChangeBean.isEmailAddressEnqueued(emailAddress); - - // Is it there? - if (isQueued) { - // Add to list - this.emailAddresses.add(emailAddress); - } - - // Return status - return isQueued; - } - -} diff --git a/src/java/org/mxchange/addressbook/beans/user/email_address/AddressbookEmailChangeWebSessionController.java b/src/java/org/mxchange/addressbook/beans/user/email_address/AddressbookEmailChangeWebSessionController.java deleted file mode 100644 index 9f75c211..00000000 --- a/src/java/org/mxchange/addressbook/beans/user/email_address/AddressbookEmailChangeWebSessionController.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * 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.addressbook.beans.user.email_address; - -import java.io.Serializable; - -/** - * An interface for an email change controller - *

- * @author Roland Häder - */ -public interface AddressbookEmailChangeWebSessionController extends Serializable { - - /** - * Checks whether all required are set for changing email address - *

- * @return Whether the required personal data is set - */ - boolean isRequiredChangeEmailAddressSet (); - -} diff --git a/src/java/org/mxchange/addressbook/beans/user/login/AddressbookUserLoginWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/user/login/AddressbookUserLoginWebSessionBean.java index ff6b4beb..981d523f 100644 --- a/src/java/org/mxchange/addressbook/beans/user/login/AddressbookUserLoginWebSessionBean.java +++ b/src/java/org/mxchange/addressbook/beans/user/login/AddressbookUserLoginWebSessionBean.java @@ -29,7 +29,7 @@ import javax.faces.context.FacesContext; import javax.inject.Inject; import javax.inject.Named; import org.mxchange.addressbook.beans.BaseAddressbookController; -import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController; +import org.mxchange.addressbook.beans.user.AddressbookUserWebRequestController; import org.mxchange.jusercore.exceptions.UserNotFoundException; import org.mxchange.jusercore.exceptions.UserStatusLockedException; import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException; @@ -88,7 +88,7 @@ public class AddressbookUserLoginWebSessionBean extends BaseAddressbookControlle * User controller */ @Inject - private AddressbookUserWebSessionController userController; + private AddressbookUserWebRequestController userController; /** * Current password diff --git a/src/java/org/mxchange/addressbook/beans/user/register/AddressbookUserRegisterWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/user/register/AddressbookUserRegisterWebRequestBean.java new file mode 100644 index 00000000..d9436457 --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/user/register/AddressbookUserRegisterWebRequestBean.java @@ -0,0 +1,326 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * 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.addressbook.beans.user.register; + +import javax.ejb.EJB; +import javax.enterprise.context.RequestScoped; +import javax.enterprise.event.Event; +import javax.enterprise.inject.Any; +import javax.faces.view.facelets.FaceletException; +import javax.inject.Inject; +import javax.inject.Named; +import org.mxchange.addressbook.beans.BaseAddressbookController; +import org.mxchange.addressbook.beans.contact.AddressbookContactWebRequestController; +import org.mxchange.addressbook.beans.features.AddressbookFeaturesWebApplicationController; +import org.mxchange.addressbook.beans.user.AddressbookAdminUserWebRequestController; +import org.mxchange.jcontacts.model.contact.Contact; +import org.mxchange.jcontacts.model.contact.UserContact; +import org.mxchange.jcoreee.utils.FacesUtils; +import org.mxchange.jusercore.events.user.clear.password.ClearUserPasswordEvent; +import org.mxchange.jusercore.events.user.clear.password.ObservableClearUserPasswordEvent; +import org.mxchange.jusercore.events.user.clear.username.ClearUserNameEvent; +import org.mxchange.jusercore.events.user.clear.username.ObservableClearUserNameEvent; +import org.mxchange.jusercore.exceptions.DataRepeatMismatchException; +import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException; +import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException; +import org.mxchange.jusercore.model.user.User; +import org.mxchange.jusercore.model.user.password_history.PasswordHistory; +import org.mxchange.jusercore.model.user.password_history.UserPasswordHistory; +import org.mxchange.jusercore.model.user.status.UserAccountStatus; +import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent; +import org.mxchange.juserlogincore.events.registration.UserRegisteredEvent; +import org.mxchange.juserlogincore.events.user.password_change.ObservableUpdatedUserPasswordEvent; +import org.mxchange.juserlogincore.events.user.password_change.UpdatedUserPasswordEvent; +import org.mxchange.juserlogincore.login.UserLoginUtils; +import org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote; +import org.mxchange.addressbook.beans.user.AddressbookUserWebRequestController; + +/** + * A web bean for user registration + *

+ * @author Roland Häder + */ +@Named ("userRegistrationController") +@RequestScoped +public class AddressbookUserRegisterWebRequestBean extends BaseAddressbookController implements AddressbookUserRegisterWebRequestController { + + /** + * Serial number + */ + private static final long serialVersionUID = 47_828_986_719_691_592L; + + /** + * Administrative user controller + */ + @Inject + private AddressbookAdminUserWebRequestController adminUserController; + + /** + * An event being fired when a user name should be cleared + */ + @Inject + @Any + private Event clearUserNameEvent; + + /** + * An event being fired when a user password should be cleared + */ + @Inject + @Any + private Event clearUserPasswordEvent; + + /** + * Contact controller + */ + @Inject + private AddressbookContactWebRequestController contactController; + + /** + * Features controller + */ + @Inject + private AddressbookFeaturesWebApplicationController featureController; + + /** + * Remote register session-scoped bean + */ + @EJB (lookup = "java:global/addressbook-ejb/userRegistration!org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote") + private UserRegistrationSessionBeanRemote registerBean; + + /** + * User controller + */ + @Inject + private AddressbookUserWebRequestController userController; + + /** + * An event being fired when a user password was changed + */ + @Inject + @Any + private Event userPasswordChangedEvent; + + /** + * An event being fired when a new user has registered + */ + @Inject + @Any + private Event userRegisteredEvent; + + /** + * Default constructor + */ + public AddressbookUserRegisterWebRequestBean () { + // Call super constructor + super(); + } + + /** + * Registers the user, if not found. Otherwise this method should throw an + * exception. + *

+ * @return Redirection target + */ + public String doFinishRegistration () { + // Is registration enabled? + if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N + // Is not enabled + throw new FaceletException("Registration is disabled."); //NOI18N + } + + // Get user instance + User user = this.userController.createUserInstance(true); + + // Null random password means registration requires user-entered password + String randomPassword = null; + + // Is the user already used? + if (null == user) { + // user must be set + throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N + } else if (!this.userController.isRequiredPersonalDataSet()) { + // Not all required fields are set + throw new FaceletException("Not all required fields are set."); //NOI18N + } else if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (this.userController.isUserNameRegistered(user))) { //NOI18N + // Is multi-page enabled? + if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N + // User name is already used, should not happen here + throw new FaceletException(new UserNameAlreadyRegisteredException(user)); + } else { + // May happen here, fire event + this.clearUserNameEvent.fire(new ClearUserNameEvent()); + + // Output message + this.showFacesMessage("form_register_single:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N + return ""; //NOI18N + } + } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) { + // Is multi-page enabled? + if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N + // Email address has already been taken, should not happen here + throw new FaceletException(new EmailAddressAlreadyRegisteredException(user)); + } else { + // May happen here, reset fields + this.contactController.clearEmailAddresses(); + this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N + return ""; //NOI18N + } + } else if (!this.contactController.isSameEmailAddressEntered()) { + // Is multi-page enabled? + if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N + // Not same email address entered, should not happen here + throw new FaceletException(new DataRepeatMismatchException("Email addresses not matching.")); //NOI18N + } else { + // May happen here, reset fields + this.contactController.clearEmailAddresses(); + this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING"); //NOI18N + return ""; //NOI18N + } + } else if (!this.userController.isSamePasswordEntered()) { + // Is multi-page enabled? + if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N + // Not same password entered, should no longer happen here + throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N + } else if (this.userController.ifBothPasswordsEmptyAllowed()) { + // Both passwords are left empty and is allowed, then generate a random password + randomPassword = UserLoginUtils.createRandomPassword(AddressbookUserWebRequestController.MINIMUM_PASSWORD_LENGTH); + + // Generate (ignored) password-history + PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user); + + // Fire event + this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword)); + } + } + + // Encrypt password + String encryptedPassword = UserLoginUtils.encryptPassword(this.userController.getUserPassword()); + + // Set it here + user.setUserEncryptedPassword(encryptedPassword); + + // Is developer mode? + if (this.isDebugModeEnabled("register")) { //NOI18N + // For debugging/programming only: + user.setUserAccountStatus(UserAccountStatus.CONFIRMED); + } else { + // No debugging of this part + user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED); + + // Ask EJB for generating a not-existing confirmation key + String confirmKey = this.registerBean.generateConfirmationKey(user); + + // Set it in user + user.setUserConfirmKey(confirmKey); + } + + try { + // Get base URL + String baseUrl = FacesUtils.generateBaseUrl(); + + // Call bean + User registeredUser = this.registerBean.registerUser(user, baseUrl, randomPassword); + + // The id number should be set + assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N + + // Fire event + this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser)); + + // All fine, redirect to proper page + return "user_register_done"; //NOI18N + } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) { + // Continue to throw + throw new FaceletException(ex); + } + } + + /** + * Handles registration request send from first page. The (maybe) entered + * user name and email address is not used and that privacy and T&C are + * accepted. + *

+ * @return Redirect + */ + public String doRegisterMultiPage1 () { + // Is registration enabled? + if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N + // Is not enabled + throw new FaceletException("Registration is disabled."); //NOI18N + } + + // Get user instance + User user = this.userController.createUserInstance(false); + + // First check if user is not null and user name is not used + if same email address is entered + if (null == user) { + // user must be set + throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N + } else if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (this.userController.isUserNameRegistered(user))) { //NOI18N + // User name is already used, so clear it + this.clearUserNameEvent.fire(new ClearUserNameEvent()); + + // Output message + this.showFacesMessage("form_register_page1:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N + return ""; //NOI18N + } else if (!this.contactController.isSameEmailAddressEntered()) { + // Not same email address entered, clear both + this.contactController.clearEmailAddresses(); + this.showFacesMessage("form_register_page1:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING"); //NOI18N + return ""; //NOI18N + } else if (!this.userController.isSamePasswordEntered()) { + // Is multi-page enabled? + if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N + // Unset both + this.clearUserPasswordEvent.fire(new ClearUserPasswordEvent()); + + // Output faces message + this.showFacesMessage("form_register_page1:userPassword", "ERROR_USER_PASSWORD_EMPTY"); //NOI18N + this.showFacesMessage("form_register_page1:userPasswordRepeat", "ERROR_USER_PASSWORD_REPEAT_EMPTY"); //NOI18N + return ""; //NOI18N + } else if (this.userController.ifBothPasswordsEmptyAllowed()) { + // Both passwords are left empty and is allowed, then generate a random password + String randomPassword = UserLoginUtils.createRandomPassword(AddressbookUserWebRequestController.MINIMUM_PASSWORD_LENGTH); + + // Generate (ignored) password-history + PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user); + + // Fire event + this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword)); + } + } + + // Create half contact instance with email address + Contact contact = new UserContact(); + contact.setContactEmailAddress(this.contactController.getEmailAddress()); + + // Set contact in user + user.setUserContact(contact); + + // Check if email address is registered + if (this.contactController.isEmailAddressRegistered(user.getUserContact())) { + // Email address has already been taken, clear both + this.contactController.clearEmailAddresses(); + this.showFacesMessage("form_register_page1:emailAddress", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N + return ""; //NOI18N + } + + // Now only redirect to next page as the JSF does it + return "user_register_page2"; //NOI18N + } + +} diff --git a/src/java/org/mxchange/addressbook/beans/user/register/AddressbookUserRegisterWebRequestController.java b/src/java/org/mxchange/addressbook/beans/user/register/AddressbookUserRegisterWebRequestController.java new file mode 100644 index 00000000..dc9a6429 --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/user/register/AddressbookUserRegisterWebRequestController.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * 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.addressbook.beans.user.register; + +import java.io.Serializable; + +/** + * An interface for registration web controllers + *

+ * @author Roland Häder + */ +public interface AddressbookUserRegisterWebRequestController extends Serializable { + +} diff --git a/src/java/org/mxchange/addressbook/beans/user/register/AddressbookUserRegisterWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/user/register/AddressbookUserRegisterWebSessionBean.java deleted file mode 100644 index 0040b6dc..00000000 --- a/src/java/org/mxchange/addressbook/beans/user/register/AddressbookUserRegisterWebSessionBean.java +++ /dev/null @@ -1,326 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * 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.addressbook.beans.user.register; - -import javax.ejb.EJB; -import javax.enterprise.context.SessionScoped; -import javax.enterprise.event.Event; -import javax.enterprise.inject.Any; -import javax.faces.view.facelets.FaceletException; -import javax.inject.Inject; -import javax.inject.Named; -import org.mxchange.addressbook.beans.BaseAddressbookController; -import org.mxchange.addressbook.beans.contact.AddressbookContactWebSessionController; -import org.mxchange.addressbook.beans.features.AddressbookFeaturesWebApplicationController; -import org.mxchange.addressbook.beans.user.AddressbookAdminUserWebRequestController; -import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController; -import org.mxchange.jcontacts.model.contact.Contact; -import org.mxchange.jcontacts.model.contact.UserContact; -import org.mxchange.jcoreee.utils.FacesUtils; -import org.mxchange.jusercore.events.user.clear.password.ClearUserPasswordEvent; -import org.mxchange.jusercore.events.user.clear.password.ObservableClearUserPasswordEvent; -import org.mxchange.jusercore.events.user.clear.username.ClearUserNameEvent; -import org.mxchange.jusercore.events.user.clear.username.ObservableClearUserNameEvent; -import org.mxchange.jusercore.exceptions.DataRepeatMismatchException; -import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException; -import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException; -import org.mxchange.jusercore.model.user.User; -import org.mxchange.jusercore.model.user.password_history.PasswordHistory; -import org.mxchange.jusercore.model.user.password_history.UserPasswordHistory; -import org.mxchange.jusercore.model.user.status.UserAccountStatus; -import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent; -import org.mxchange.juserlogincore.events.registration.UserRegisteredEvent; -import org.mxchange.juserlogincore.events.user.password_change.ObservableUpdatedUserPasswordEvent; -import org.mxchange.juserlogincore.events.user.password_change.UpdatedUserPasswordEvent; -import org.mxchange.juserlogincore.login.UserLoginUtils; -import org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote; - -/** - * A web bean for user registration - *

- * @author Roland Häder - */ -@Named ("userRegistrationController") -@SessionScoped -public class AddressbookUserRegisterWebSessionBean extends BaseAddressbookController implements AddressbookUserRegisterWebSessionController { - - /** - * Serial number - */ - private static final long serialVersionUID = 47_828_986_719_691_592L; - - /** - * Administrative user controller - */ - @Inject - private AddressbookAdminUserWebRequestController adminUserController; - - /** - * An event being fired when a user name should be cleared - */ - @Inject - @Any - private Event clearUserNameEvent; - - /** - * An event being fired when a user password should be cleared - */ - @Inject - @Any - private Event clearUserPasswordEvent; - - /** - * Contact controller - */ - @Inject - private AddressbookContactWebSessionController contactController; - - /** - * Features controller - */ - @Inject - private AddressbookFeaturesWebApplicationController featureController; - - /** - * Remote register session-scoped bean - */ - @EJB (lookup = "java:global/addressbook-ejb/userRegistration!org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote") - private UserRegistrationSessionBeanRemote registerBean; - - /** - * User controller - */ - @Inject - private AddressbookUserWebSessionController userController; - - /** - * An event being fired when a user password was changed - */ - @Inject - @Any - private Event userPasswordChangedEvent; - - /** - * An event being fired when a new user has registered - */ - @Inject - @Any - private Event userRegisteredEvent; - - /** - * Default constructor - */ - public AddressbookUserRegisterWebSessionBean () { - // Call super constructor - super(); - } - - /** - * Registers the user, if not found. Otherwise this method should throw an - * exception. - *

- * @return Redirection target - */ - public String doFinishRegistration () { - // Is registration enabled? - if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N - // Is not enabled - throw new FaceletException("Registration is disabled."); //NOI18N - } - - // Get user instance - User user = this.userController.createUserInstance(true); - - // Null random password means registration requires user-entered password - String randomPassword = null; - - // Is the user already used? - if (null == user) { - // user must be set - throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N - } else if (!this.userController.isRequiredPersonalDataSet()) { - // Not all required fields are set - throw new FaceletException("Not all required fields are set."); //NOI18N - } else if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (this.userController.isUserNameRegistered(user))) { //NOI18N - // Is multi-page enabled? - if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N - // User name is already used, should not happen here - throw new FaceletException(new UserNameAlreadyRegisteredException(user)); - } else { - // May happen here, fire event - this.clearUserNameEvent.fire(new ClearUserNameEvent()); - - // Output message - this.showFacesMessage("form_register_single:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N - return ""; //NOI18N - } - } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) { - // Is multi-page enabled? - if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N - // Email address has already been taken, should not happen here - throw new FaceletException(new EmailAddressAlreadyRegisteredException(user)); - } else { - // May happen here, reset fields - this.contactController.clearEmailAddresses(); - this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N - return ""; //NOI18N - } - } else if (!this.contactController.isSameEmailAddressEntered()) { - // Is multi-page enabled? - if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N - // Not same email address entered, should not happen here - throw new FaceletException(new DataRepeatMismatchException("Email addresses not matching.")); //NOI18N - } else { - // May happen here, reset fields - this.contactController.clearEmailAddresses(); - this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING"); //NOI18N - return ""; //NOI18N - } - } else if (!this.userController.isSamePasswordEntered()) { - // Is multi-page enabled? - if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N - // Not same password entered, should no longer happen here - throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N - } else if (this.userController.ifBothPasswordsEmptyAllowed()) { - // Both passwords are left empty and is allowed, then generate a random password - randomPassword = UserLoginUtils.createRandomPassword(AddressbookUserWebSessionController.MINIMUM_PASSWORD_LENGTH); - - // Generate (ignored) password-history - PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user); - - // Fire event - this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword)); - } - } - - // Encrypt password - String encryptedPassword = UserLoginUtils.encryptPassword(this.userController.getUserPassword()); - - // Set it here - user.setUserEncryptedPassword(encryptedPassword); - - // Is developer mode? - if (this.isDebugModeEnabled("register")) { //NOI18N - // For debugging/programming only: - user.setUserAccountStatus(UserAccountStatus.CONFIRMED); - } else { - // No debugging of this part - user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED); - - // Ask EJB for generating a not-existing confirmation key - String confirmKey = this.registerBean.generateConfirmationKey(user); - - // Set it in user - user.setUserConfirmKey(confirmKey); - } - - try { - // Get base URL - String baseUrl = FacesUtils.generateBaseUrl(); - - // Call bean - User registeredUser = this.registerBean.registerUser(user, baseUrl, randomPassword); - - // The id number should be set - assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N - - // Fire event - this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser)); - - // All fine, redirect to proper page - return "user_register_done"; //NOI18N - } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) { - // Continue to throw - throw new FaceletException(ex); - } - } - - /** - * Handles registration request send from first page. The (maybe) entered - * user name and email address is not used and that privacy and T&C are - * accepted. - *

- * @return Redirect - */ - public String doRegisterMultiPage1 () { - // Is registration enabled? - if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N - // Is not enabled - throw new FaceletException("Registration is disabled."); //NOI18N - } - - // Get user instance - User user = this.userController.createUserInstance(false); - - // First check if user is not null and user name is not used + if same email address is entered - if (null == user) { - // user must be set - throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N - } else if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (this.userController.isUserNameRegistered(user))) { //NOI18N - // User name is already used, so clear it - this.clearUserNameEvent.fire(new ClearUserNameEvent()); - - // Output message - this.showFacesMessage("form_register_page1:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N - return ""; //NOI18N - } else if (!this.contactController.isSameEmailAddressEntered()) { - // Not same email address entered, clear both - this.contactController.clearEmailAddresses(); - this.showFacesMessage("form_register_page1:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING"); //NOI18N - return ""; //NOI18N - } else if (!this.userController.isSamePasswordEntered()) { - // Is multi-page enabled? - if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N - // Unset both - this.clearUserPasswordEvent.fire(new ClearUserPasswordEvent()); - - // Output faces message - this.showFacesMessage("form_register_page1:userPassword", "ERROR_USER_PASSWORD_EMPTY"); //NOI18N - this.showFacesMessage("form_register_page1:userPasswordRepeat", "ERROR_USER_PASSWORD_REPEAT_EMPTY"); //NOI18N - return ""; //NOI18N - } else if (this.userController.ifBothPasswordsEmptyAllowed()) { - // Both passwords are left empty and is allowed, then generate a random password - String randomPassword = UserLoginUtils.createRandomPassword(AddressbookUserWebSessionController.MINIMUM_PASSWORD_LENGTH); - - // Generate (ignored) password-history - PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user); - - // Fire event - this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword)); - } - } - - // Create half contact instance with email address - Contact contact = new UserContact(); - contact.setContactEmailAddress(this.contactController.getEmailAddress()); - - // Set contact in user - user.setUserContact(contact); - - // Check if email address is registered - if (this.contactController.isEmailAddressRegistered(user.getUserContact())) { - // Email address has already been taken, clear both - this.contactController.clearEmailAddresses(); - this.showFacesMessage("form_register_page1:emailAddress", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N - return ""; //NOI18N - } - - // Now only redirect to next page as the JSF does it - return "user_register_page2"; //NOI18N - } - -} diff --git a/src/java/org/mxchange/addressbook/beans/user/register/AddressbookUserRegisterWebSessionController.java b/src/java/org/mxchange/addressbook/beans/user/register/AddressbookUserRegisterWebSessionController.java deleted file mode 100644 index 7171c240..00000000 --- a/src/java/org/mxchange/addressbook/beans/user/register/AddressbookUserRegisterWebSessionController.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * 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.addressbook.beans.user.register; - -import java.io.Serializable; - -/** - * An interface for registration web controllers - *

- * @author Roland Häder - */ -public interface AddressbookUserRegisterWebSessionController extends Serializable { - -} diff --git a/src/java/org/mxchange/addressbook/beans/user/resendlink/AddressbookResendLinkWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/user/resendlink/AddressbookResendLinkWebRequestBean.java new file mode 100644 index 00000000..fc66e68a --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/user/resendlink/AddressbookResendLinkWebRequestBean.java @@ -0,0 +1,240 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * 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.addressbook.beans.user.resendlink; + +import java.util.Locale; +import javax.ejb.EJB; +import javax.enterprise.context.RequestScoped; +import javax.enterprise.event.Event; +import javax.enterprise.event.Observes; +import javax.enterprise.inject.Any; +import javax.inject.Inject; +import javax.inject.Named; +import org.mxchange.addressbook.beans.BaseAddressbookController; +import org.mxchange.addressbook.beans.localization.AddressbookLocalizationSessionController; +import org.mxchange.addressbook.beans.user.AddressbookUserWebRequestController; +import org.mxchange.jcoreee.events.locale.ObservableLocaleChangeEvent; +import org.mxchange.jcoreee.utils.FacesUtils; +import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException; +import org.mxchange.jusercore.exceptions.UserNotFoundException; +import org.mxchange.jusercore.exceptions.UserStatusConfirmedException; +import org.mxchange.jusercore.exceptions.UserStatusLockedException; +import org.mxchange.jusercore.model.user.User; +import org.mxchange.jusercore.model.user.status.UserAccountStatus; +import org.mxchange.juserlogincore.events.resendlink.ObservableUserResendLinkAccountEvent; +import org.mxchange.juserlogincore.events.resendlink.UserResendLinkAccountEvent; +import org.mxchange.juserlogincore.model.user.resendlink.ResendLinkSessionBeanRemote; + +/** + * A web session-scoped bean for resending confirmation link + *

+ * @author Roland Häder + */ +@Named ("userResendConfirmationController") +@RequestScoped +public class AddressbookResendLinkWebRequestBean extends BaseAddressbookController implements AddressbookResendLinkWebRequestController { + + /** + * Serial number + */ + private static final long serialVersionUID = 186_078_724_659_153L; + + /** + * Email address + */ + private String emailAddress; + + /** + * Locale instance + */ + private Locale locale; + + /** + * Localization controller + */ + @Inject + private AddressbookLocalizationSessionController localizationController; + + /** + * EJB for resending confirmation link + */ + @EJB (lookup = "java:global/addressbook-ejb/userResendConfirmationLink!org.mxchange.juserlogincore.model.user.resendlink.ResendLinkSessionBeanRemote") + private ResendLinkSessionBeanRemote resendLinkBean; + + /** + * Regular user controller + */ + @Inject + private AddressbookUserWebRequestController userController; + + /** + * Event being fired after confirmation link is being sent + */ + @Inject + @Any + private Event userResendLinkEvent; + + /** + * Default constructor + */ + public AddressbookResendLinkWebRequestBean () { + // Call super constructor + super(); + } + + /** + * Observer method for events being fired when the application's locale has + * been changed. + *

+ * @param event Event being fired + */ + public void afterLocaleChangeEvent (@Observes final ObservableLocaleChangeEvent event) { + // Is the parameter valid? + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); + } else if (event.getLocale() == null) { + // Throw NPE again + throw new NullPointerException("event.locale is null"); + } + + // Set it here + this.setLocale(event.getLocale()); + } + + /** + * Resends (new) confirmation link to given email address, if found. + * Otherwise an exception is thrown. On success a redirect takes place. + *

+ * @return Redirect outcome + */ + public String doResendLink () { + // The email address should not be empty as the JSF validates this + if (this.getEmailAddress() == null) { + // Throw NPE + throw new NullPointerException("this.emailAddress is null"); //NOI18N + } + + // Init user instance + User user; + + try { + // Is the email address really not used? + user = this.userController.lookupUserByEmailAddress(this.getEmailAddress()); + } catch (final UserEmailAddressNotFoundException ex) { + // Always clear bean + this.clear(); + + // Not found, should not happen as the registered validator should find it + this.showFacesMessage("form_resend_link:", "ERROR_USER_EMAIL_ADDRESS_NOT_FOUND"); //NOI18N + return ""; //NOI18N + } + + // Is the user account already confirmed? + if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) { + // Always clear bean + this.clear(); + + // Then abort here + this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_ALREADY_CONFIRMED"); //NOI18N + return ""; //NOI18N + } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) { + // Always clear bean + this.clear(); + + // User account is locked + this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_LOCKED"); //NOI18N + return ""; //NOI18N + } else if (user.getUserConfirmKey() == null) { + // Status is UNCONFIRMED but confirmation key is NULL + throw new NullPointerException("user.userConfirmKey is null"); //NOI18N + } + + // Init managed user instance + User managedUser; + + try { + // Get base URL + String baseUrl = FacesUtils.generateBaseUrl(); + + // Call EJB and return redirect target + managedUser = this.resendLinkBean.resendConfirmationLink(user, this.getLocale(), baseUrl); + } catch (final UserNotFoundException ex) { + // User not found + this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_NOT_FOUND"); //NOI18N + return ""; //NOI18N + } catch (final UserStatusLockedException | UserStatusConfirmedException ex) { + // Output message, this should not happen as the confirmation key is being removed + this.showFacesMessage("form_resend_link:resendEmailAddress", ex); //NOI18N + return ""; //NOI18N + } + + // Clear this bean + this.clear(); + + // Fire event + this.userResendLinkEvent.fire(new UserResendLinkAccountEvent(managedUser)); + + // Return redirect target + return "user_resend_done"; //NOI18N + } + + /** + * Getter for email address 1 (changing) + *

+ * @return Email address + */ + public String getEmailAddress () { + return this.emailAddress; + } + + /** + * Setter for email address 1 (changing) + *

+ * @param emailAddress Email address 1 + */ + public void setEmailAddress (final String emailAddress) { + this.emailAddress = emailAddress; + } + + /** + * Clears email address fields so the user has to re-enter them + */ + private void clear () { + // Clear fields + this.setEmailAddress(null); + } + + /** + * Getter for locale instance + *

+ * @return Locale instance + */ + private Locale getLocale () { + return this.locale; + } + + /** + * Setter for locale instance + *

+ * @param locale Locale instance + */ + private void setLocale (final Locale locale) { + this.locale = locale; + } + +} diff --git a/src/java/org/mxchange/addressbook/beans/user/resendlink/AddressbookResendLinkWebRequestController.java b/src/java/org/mxchange/addressbook/beans/user/resendlink/AddressbookResendLinkWebRequestController.java new file mode 100644 index 00000000..946c7bf2 --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/user/resendlink/AddressbookResendLinkWebRequestController.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * 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.addressbook.beans.user.resendlink; + +import java.io.Serializable; + +/** + * An interface for an email change controller + *

+ * @author Roland Häder + */ +public interface AddressbookResendLinkWebRequestController extends Serializable { + +} diff --git a/src/java/org/mxchange/addressbook/beans/user/resendlink/AddressbookResendLinkWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/user/resendlink/AddressbookResendLinkWebSessionBean.java deleted file mode 100644 index 6d926c15..00000000 --- a/src/java/org/mxchange/addressbook/beans/user/resendlink/AddressbookResendLinkWebSessionBean.java +++ /dev/null @@ -1,240 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * 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.addressbook.beans.user.resendlink; - -import java.util.Locale; -import javax.ejb.EJB; -import javax.enterprise.context.SessionScoped; -import javax.enterprise.event.Event; -import javax.enterprise.event.Observes; -import javax.enterprise.inject.Any; -import javax.inject.Inject; -import javax.inject.Named; -import org.mxchange.addressbook.beans.BaseAddressbookController; -import org.mxchange.addressbook.beans.localization.AddressbookLocalizationSessionController; -import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController; -import org.mxchange.jcoreee.events.locale.ObservableLocaleChangeEvent; -import org.mxchange.jcoreee.utils.FacesUtils; -import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException; -import org.mxchange.jusercore.exceptions.UserNotFoundException; -import org.mxchange.jusercore.exceptions.UserStatusConfirmedException; -import org.mxchange.jusercore.exceptions.UserStatusLockedException; -import org.mxchange.jusercore.model.user.User; -import org.mxchange.jusercore.model.user.status.UserAccountStatus; -import org.mxchange.juserlogincore.events.resendlink.ObservableUserResendLinkAccountEvent; -import org.mxchange.juserlogincore.events.resendlink.UserResendLinkAccountEvent; -import org.mxchange.juserlogincore.model.user.resendlink.ResendLinkSessionBeanRemote; - -/** - * A web session-scoped bean for resending confirmation link - *

- * @author Roland Häder - */ -@Named ("userResendConfirmationController") -@SessionScoped -public class AddressbookResendLinkWebSessionBean extends BaseAddressbookController implements AddressbookResendLinkWebSessionController { - - /** - * Serial number - */ - private static final long serialVersionUID = 186_078_724_659_153L; - - /** - * Email address - */ - private String emailAddress; - - /** - * Locale instance - */ - private Locale locale; - - /** - * Localization controller - */ - @Inject - private AddressbookLocalizationSessionController localizationController; - - /** - * EJB for resending confirmation link - */ - @EJB (lookup = "java:global/addressbook-ejb/userResendConfirmationLink!org.mxchange.juserlogincore.model.user.resendlink.ResendLinkSessionBeanRemote") - private ResendLinkSessionBeanRemote resendLinkBean; - - /** - * Regular user controller - */ - @Inject - private AddressbookUserWebSessionController userController; - - /** - * Event being fired after confirmation link is being sent - */ - @Inject - @Any - private Event userResendLinkEvent; - - /** - * Default constructor - */ - public AddressbookResendLinkWebSessionBean () { - // Call super constructor - super(); - } - - /** - * Observer method for events being fired when the application's locale has - * been changed. - *

- * @param event Event being fired - */ - public void afterLocaleChangeEvent (@Observes final ObservableLocaleChangeEvent event) { - // Is the parameter valid? - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); - } else if (event.getLocale() == null) { - // Throw NPE again - throw new NullPointerException("event.locale is null"); - } - - // Set it here - this.setLocale(event.getLocale()); - } - - /** - * Resends (new) confirmation link to given email address, if found. - * Otherwise an exception is thrown. On success a redirect takes place. - *

- * @return Redirect outcome - */ - public String doResendLink () { - // The email address should not be empty as the JSF validates this - if (this.getEmailAddress() == null) { - // Throw NPE - throw new NullPointerException("this.emailAddress is null"); //NOI18N - } - - // Init user instance - User user; - - try { - // Is the email address really not used? - user = this.userController.lookupUserByEmailAddress(this.getEmailAddress()); - } catch (final UserEmailAddressNotFoundException ex) { - // Always clear bean - this.clear(); - - // Not found, should not happen as the registered validator should find it - this.showFacesMessage("form_resend_link:", "ERROR_USER_EMAIL_ADDRESS_NOT_FOUND"); //NOI18N - return ""; //NOI18N - } - - // Is the user account already confirmed? - if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) { - // Always clear bean - this.clear(); - - // Then abort here - this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_ALREADY_CONFIRMED"); //NOI18N - return ""; //NOI18N - } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) { - // Always clear bean - this.clear(); - - // User account is locked - this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_LOCKED"); //NOI18N - return ""; //NOI18N - } else if (user.getUserConfirmKey() == null) { - // Status is UNCONFIRMED but confirmation key is NULL - throw new NullPointerException("user.userConfirmKey is null"); //NOI18N - } - - // Init managed user instance - User managedUser; - - try { - // Get base URL - String baseUrl = FacesUtils.generateBaseUrl(); - - // Call EJB and return redirect target - managedUser = this.resendLinkBean.resendConfirmationLink(user, this.getLocale(), baseUrl); - } catch (final UserNotFoundException ex) { - // User not found - this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_NOT_FOUND"); //NOI18N - return ""; //NOI18N - } catch (final UserStatusLockedException | UserStatusConfirmedException ex) { - // Output message, this should not happen as the confirmation key is being removed - this.showFacesMessage("form_resend_link:resendEmailAddress", ex); //NOI18N - return ""; //NOI18N - } - - // Clear this bean - this.clear(); - - // Fire event - this.userResendLinkEvent.fire(new UserResendLinkAccountEvent(managedUser)); - - // Return redirect target - return "user_resend_done"; //NOI18N - } - - /** - * Getter for email address 1 (changing) - *

- * @return Email address - */ - public String getEmailAddress () { - return this.emailAddress; - } - - /** - * Setter for email address 1 (changing) - *

- * @param emailAddress Email address 1 - */ - public void setEmailAddress (final String emailAddress) { - this.emailAddress = emailAddress; - } - - /** - * Clears email address fields so the user has to re-enter them - */ - private void clear () { - // Clear fields - this.setEmailAddress(null); - } - - /** - * Getter for locale instance - *

- * @return Locale instance - */ - private Locale getLocale () { - return this.locale; - } - - /** - * Setter for locale instance - *

- * @param locale Locale instance - */ - private void setLocale (final Locale locale) { - this.locale = locale; - } - -} diff --git a/src/java/org/mxchange/addressbook/beans/user/resendlink/AddressbookResendLinkWebSessionController.java b/src/java/org/mxchange/addressbook/beans/user/resendlink/AddressbookResendLinkWebSessionController.java deleted file mode 100644 index 185ee381..00000000 --- a/src/java/org/mxchange/addressbook/beans/user/resendlink/AddressbookResendLinkWebSessionController.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * 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.addressbook.beans.user.resendlink; - -import java.io.Serializable; - -/** - * An interface for an email change controller - *

- * @author Roland Häder - */ -public interface AddressbookResendLinkWebSessionController extends Serializable { - -} diff --git a/src/java/org/mxchange/addressbook/converter/business/basicdata/AddressbookBusinessContactConverter.java b/src/java/org/mxchange/addressbook/converter/business/basicdata/AddressbookBusinessContactConverter.java index 9c7ebb95..f6b80ea5 100644 --- a/src/java/org/mxchange/addressbook/converter/business/basicdata/AddressbookBusinessContactConverter.java +++ b/src/java/org/mxchange/addressbook/converter/business/basicdata/AddressbookBusinessContactConverter.java @@ -37,7 +37,7 @@ public class AddressbookBusinessContactConverter implements Converter /** * CompanyEmployee EJB */ - @EJB (lookup = "java:global/jfinancials-ejb/companyEmployee!org.mxchange.jcontactsbusiness.employee.CompanyEmployeeSessionBeanRemote") + @EJB (lookup = "java:global/addressbook-ejb/companyEmployee!org.mxchange.jcontactsbusiness.employee.CompanyEmployeeSessionBeanRemote") private CompanyEmployeeSessionBeanRemote companyEmployeeBean; /** diff --git a/src/java/org/mxchange/addressbook/converter/business/headquarters/AddressbookCompanyHeadquartersConverter.java b/src/java/org/mxchange/addressbook/converter/business/headquarters/AddressbookCompanyHeadquartersConverter.java index e26d48d7..e9f2b4e7 100644 --- a/src/java/org/mxchange/addressbook/converter/business/headquarters/AddressbookCompanyHeadquartersConverter.java +++ b/src/java/org/mxchange/addressbook/converter/business/headquarters/AddressbookCompanyHeadquartersConverter.java @@ -37,7 +37,7 @@ public class AddressbookCompanyHeadquartersConverter implements Converter { /** * User EJB */ - @EJB (lookup = "java:global/jfinancials-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote") + @EJB (lookup = "java:global/addressbook-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote") private ContactSessionBeanRemote contactBean; /** diff --git a/src/java/org/mxchange/addressbook/converter/country/AddressbookCountryConverter.java b/src/java/org/mxchange/addressbook/converter/country/AddressbookCountryConverter.java index 43085de5..667e43cf 100644 --- a/src/java/org/mxchange/addressbook/converter/country/AddressbookCountryConverter.java +++ b/src/java/org/mxchange/addressbook/converter/country/AddressbookCountryConverter.java @@ -38,7 +38,7 @@ public class AddressbookCountryConverter implements Converter { /** * Country bean */ - @EJB (lookup = "java:global/jfinancials-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote") + @EJB (lookup = "java:global/addressbook-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote") private CountrySingletonBeanRemote countryBean; /** diff --git a/src/java/org/mxchange/addressbook/converter/fax/AddressbookFaxNumberConverter.java b/src/java/org/mxchange/addressbook/converter/fax/AddressbookFaxNumberConverter.java index 7f304aec..72fa8328 100644 --- a/src/java/org/mxchange/addressbook/converter/fax/AddressbookFaxNumberConverter.java +++ b/src/java/org/mxchange/addressbook/converter/fax/AddressbookFaxNumberConverter.java @@ -37,7 +37,7 @@ public class AddressbookFaxNumberConverter implements Converter { /** * User EJB */ - @EJB(lookup = "java:global/jfinancials-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote") + @EJB(lookup = "java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote") private UserSessionBeanRemote userBean; /** diff --git a/src/java/org/mxchange/addressbook/validator/business/basicdata/AddressbookCompanyNameValidator.java b/src/java/org/mxchange/addressbook/validator/business/basicdata/AddressbookCompanyNameValidator.java index 6fc5a143..9268f034 100644 --- a/src/java/org/mxchange/addressbook/validator/business/basicdata/AddressbookCompanyNameValidator.java +++ b/src/java/org/mxchange/addressbook/validator/business/basicdata/AddressbookCompanyNameValidator.java @@ -42,7 +42,7 @@ public class AddressbookCompanyNameValidator extends BaseStringValidator { /** * Business contact EJB */ - @EJB (lookup = "java:global/jfinancials-ejb/businessData!org.mxchange.jcontactsbusiness.basicdata.BusinessDataSessionBeanRemote") + @EJB (lookup = "java:global/addressbook-ejb/businessData!org.mxchange.jcontactsbusiness.basicdata.BusinessDataSessionBeanRemote") private BusinessDataSessionBeanRemote basicDataBean; @Override diff --git a/src/java/org/mxchange/addressbook/validator/emailaddress/AddressbookEmailAddressValidator.java b/src/java/org/mxchange/addressbook/validator/emailaddress/AddressbookEmailAddressValidator.java index d5ef9c06..c8ed7676 100644 --- a/src/java/org/mxchange/addressbook/validator/emailaddress/AddressbookEmailAddressValidator.java +++ b/src/java/org/mxchange/addressbook/validator/emailaddress/AddressbookEmailAddressValidator.java @@ -38,7 +38,7 @@ public class AddressbookEmailAddressValidator extends BaseStringValidator { /** * Contact session-scoped bean */ - @EJB (lookup = "java:global/jfinancials-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote") + @EJB (lookup = "java:global/addressbook-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote") private ContactSessionBeanRemote contactBean; /** diff --git a/src/java/org/mxchange/addressbook/validator/user/AddressbookUserIdValidator.java b/src/java/org/mxchange/addressbook/validator/user/AddressbookUserIdValidator.java index d97488d1..40739a9b 100644 --- a/src/java/org/mxchange/addressbook/validator/user/AddressbookUserIdValidator.java +++ b/src/java/org/mxchange/addressbook/validator/user/AddressbookUserIdValidator.java @@ -42,7 +42,7 @@ public class AddressbookUserIdValidator extends BaseNumberValidator { /** * Remote bean */ - @EJB (lookup = "java:global/jfinancials-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote") + @EJB (lookup = "java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote") private UserSessionBeanRemote userBean; /** diff --git a/web/guest/user/user_list.xhtml b/web/guest/user/user_list.xhtml index ba2287d6..b1726a33 100644 --- a/web/guest/user/user_list.xhtml +++ b/web/guest/user/user_list.xhtml @@ -22,22 +22,24 @@ - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + >