import java.text.MessageFormat;
import java.util.LinkedList;
import java.util.List;
+import java.util.Locale;
+import java.util.MissingResourceException;
import java.util.Objects;
+import java.util.ResourceBundle;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
+import javax.faces.application.FacesMessage;
+import javax.faces.context.FacesContext;
import javax.faces.view.facelets.FaceletException;
import javax.inject.Named;
import javax.naming.Context;
*/
@Named ("phoneController")
@ApplicationScoped
-public class JobsPhoneWebApplicationBean extends BaseJobsController implements JobsPhoneWebApplicationController {
+public abstract class JobsPhoneWebApplicationBean extends BaseJobsController implements JobsPhoneWebApplicationController {
/**
* Serial number
this.landLineNumbers = new LinkedList<>();
}
- @Override
+ /**
+ * Observes events being fired when an administrator has added a new
+ * contact.
+ * <p>
+ * @param event Event being fired
+ */
public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
// The event must be valid
if (null == event) {
this.clear();
}
- @Override
+ /**
+ * Event observer for newly added users by administrator
+ * <p>
+ * @param event Event being fired
+ */
public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
// event should not be null
if (null == event) {
this.clear();
}
- @Override
+ /**
+ * Observes events being fired when an administrator has deleted a fax
+ * number
+ * <p>
+ * @param event Event being fired
+ */
public void afterAdminDeletedFaxNumberEvent (@Observes final AdminDeletedFaxNumberEvent event) {
// event should not be null
if (null == event) {
this.clear();
}
- @Override
+ /**
+ * Observes events being fired when an administrator has deleted a land-line
+ * number
+ * <p>
+ * @param event Event being fired
+ */
public void afterAdminDeletedLandLineNumberEvent (@Observes final AdminDeletedLandLineNumberEvent event) {
// event should not be null
if (null == event) {
this.clear();
}
- @Override
+ /**
+ * Observes events being fired when an administrator has deleted a mobile
+ * number
+ * <p>
+ * @param event Event being fired
+ */
public void afterAdminDeletedMobileNumberEvent (@Observes final AdminDeletedMobileNumberEvent event) {
// event should not be null
if (null == event) {
this.clear();
}
- @Override
+ /**
+ * Observes events being fired when an administrator has a linked a fax
+ * number
+ * <p>
+ * @param event Event being fired
+ */
public void afterAdminLinkedFaxNumberEvent (@Observes final ObservableAdminLinkedFaxNumberEvent event) {
// Is the event fine?
if (event == null) {
}
}
- @Override
+ /**
+ * Observes events being fired when an administrator has a linked a
+ * land-line number
+ * <p>
+ * @param event Event being fired
+ */
public void afterAdminLinkedLandLineNumberEvent (@Observes final ObservableAdminLinkedLandLineNumberEvent event) {
// Is the event fine?
if (event == null) {
}
}
- @Override
+ /**
+ * Observes events being fired when an administrator has a linked a mobile
+ * number
+ * <p>
+ * @param event Event being fired
+ */
public void afterAdminLinkedMobileNumberEvent (@Observes final ObservableAdminLinkedMobileNumberEvent event) {
// Is the event fine?
if (event == null) {
}
}
- @Override
+ /**
+ * Observes events being fired when an administrator has updated contact
+ * data.
+ * <p>
+ * @param event Event being fired
+ */
public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
// event should not be null
if (null == event) {
this.clear();
}
- @Override
+ /**
+ * Observes events being fired when an administrator has updated a fax
+ * number.
+ * <p>
+ * @param event Event being fired
+ */
public void afterAdminUpdatedFaxNumberEvent (@Observes final AdminUpdatedFaxNumberEvent event) {
// event should not be null
if (null == event) {
this.clear();
}
- @Override
+ /**
+ * Observes events being fired when an administrator has updated a land-line
+ * number.
+ * <p>
+ * @param event Event being fired
+ */
public void afterAdminUpdatedLandLineNumberEvent (@Observes final AdminUpdatedLandLineNumberEvent event) {
// event should not be null
if (null == event) {
this.clear();
}
- @Override
+ /**
+ * Observes events being fired when an administrator has updated a mobile
+ * number.
+ * <p>
+ * @param event Event being fired
+ */
public void afterAdminUpdatedMobileNumberEvent (@Observes final AdminUpdatedMobileNumberEvent event) {
// event should not be null
if (null == event) {
}
}
+ /**
+ * Returns given property key or throws an exception if not found.
+ * <p>
+ * @param parameterKey Property key
+ * <p>
+ * @return Property value
+ * <p>
+ * @throws NullPointerException If given key is not found
+ * @throws NumberFormatException If no number is given in context parameter
+ */
+ protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
+ // Get context parameter
+ Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
+ // Return it
+ return contextValue;
+ }
+
+ /**
+ * Returns given property key or throws an exception if not found.
+ * <p>
+ * @param parameterKey Property key
+ * <p>
+ * @return Property value
+ * <p>
+ * @throws NullPointerException If given key is not found
+ */
+ protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
+ // Get context parameter
+ String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
+ // Is it null?
+ if (null == contextValue) {
+ // Throw NPE
+ throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
+ }
+ // Return it
+ return contextValue;
+ }
+
+ /**
+ * Checks whether debug mode is enabled for given controller
+ * <p>
+ * @param controllerName Name of controller
+ * <p>
+ * @return Whether debug mode is enabled
+ */
+ protected boolean isDebugModeEnabled (final String controllerName) {
+ // Parameters should be valid
+ if (null == controllerName) {
+ // Throw NPE
+ throw new NullPointerException("controllerName is null"); //NOI18N
+ } else if (controllerName.isEmpty()) {
+ // Is empty
+ throw new IllegalArgumentException("controllerName is empty"); //NOI18N
+ }
+ // Try to get context parameter
+ String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
+ // Is it set and true?
+ boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE;
+ // Return it
+ return isEnabled;
+ }
+
+ /**
+ * Loads resource bundle for given locale. This must be implemented per
+ * project so all projects can still customize their methods. Calling
+ * ResourceBundleloadBundle() in this class means that also the bundle files
+ * must be present here.
+ * <p>
+ * @param locale Locale from e.g. FacesContext
+ * <p>
+ * @return Initialized and loaded resource bundle
+ */
+ protected abstract ResourceBundle loadResourceBundle (final Locale locale);
+
+ /**
+ * Shows a faces message for given causing exception. The message from the
+ * exception is being inserted into the message.
+ * <p>
+ * @param clientId Client id to send message to
+ * @param cause Causing exception
+ */
+ protected void showFacesMessage (final String clientId, final Throwable cause) {
+ // Get context and add message
+ this.showFacesMessage(clientId, cause.getMessage());
+ }
+
+ /**
+ * Shows a faces message with given message (i18n) key.
+ * <p>
+ * @param clientId Client id to send message to
+ * @param i18nKey Message key
+ * <p>
+ * @throws NullPointerException If clientId or i18nKey is null
+ * @throws IllegalArgumentException If clientId or i18nKey is empty
+ */
+ protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
+ // Both parameter must be valid
+ if (null == clientId) {
+ // Throw NPE
+ throw new NullPointerException("clientId is null"); //NOI18N
+ } else if (clientId.isEmpty()) {
+ // Is empty
+ throw new IllegalArgumentException("clientId is null"); //NOI18N
+ } else if (null == i18nKey) {
+ // Throw NPE
+ throw new NullPointerException("i18nKey is null"); //NOI18N
+ } else if (i18nKey.isEmpty()) {
+ // Is empty
+ throw new IllegalArgumentException("i18nKey is null"); //NOI18N
+ }
+ // Get current locale
+ Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
+ // Get bundle bundle
+ ResourceBundle bundle = this.loadResourceBundle(locale);
+ // Default is i18nKey
+ String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N
+ // Try it
+ try {
+ // Get message
+ message = bundle.getString(i18nKey);
+ } catch (final MissingResourceException ex) {
+ // Did not find it, ignored
+ }
+ // Get context and add message
+ FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));
+ }
+
}