package com.onaro.sanscreen.client.prefs; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; import com.onaro.commons.beans.IPropertyChangeSupport; import com.onaro.sanscreen.client.ejb.SessionsManager; import com.onaro.sanscreen.client.ejb.logging.UsageLog; import com.onaro.sanscreen.client.ejb.logging.UsageLogType; import com.onaro.sanscreen.client.ejb.logging.UsageLog.MessageBuilder; import com.onaro.sanscreen.server.interfaces.data.ServerInterfaceException; import com.onaro.sanscreen.server.sessions.ParamSession; public class BusinessEntityPreferenceManager implements IPropertyChangeSupport { /* Preferences that are stored on the server. These should match what is created in createdb.sql * on the server side since these are persisted on the server */ public static final String SERVER_PREF_TENANT_ENABLED = "TENANT_ENABLED"; //$NON-NLS-1$ public static final String SERVER_PREF_LOB_ENABLED = "LOB_ENABLED"; //$NON-NLS-1$ public static final String SERVER_PREF_BU_ENABLED = "BU_ENABLED"; //$NON-NLS-1$ public static final String SERVER_PREF_PROJECT_ENABLED = "PROJECT_ENABLED"; //$NON-NLS-1$ /* Use for UsageLog. Values are resource keys */ private static final String SERVER_PREF_TENANT_ENABLED_KEY = "be.tenant.enabled"; //$NON-NLS-1$ private static final String SERVER_PREF_LOB_ENABLED_KEY = "be.lob.enabled"; //$NON-NLS-1$ private static final String SERVER_PREF_BU_ENABLED_KEY = "be.bu.enabled"; //$NON-NLS-1$ private static final String SERVER_PREF_PROJECT_ENABLED_KEY = "be.project.enabled"; //$NON-NLS-1$ // Initialize preference values with their defaults private boolean tenantEnabled = false; private boolean lobEnabled = true; private boolean buEnabled = true; private boolean projectEnabled = true; private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this); private static final BusinessEntityPreferenceManager INSTANCE = new BusinessEntityPreferenceManager(); public static BusinessEntityPreferenceManager getInstance() { return INSTANCE; } private BusinessEntityPreferenceManager() { registerListeners(); readPreferences(); } private void registerListeners() { // addPropertyChangeListener(PROP_PROGRESS_DIALOG_ENABLED, new PropertyChangeListener() { // // TODO: Determine a non-deprecated substitute for setAutoPopupProgressDialog(). // @SuppressWarnings("deprecation") // public void propertyChange(PropertyChangeEvent evt) { // com.onaro.client.leekui.ui.compatibility.CompatibilityUtils.setAutoPopupProgressDialog(isProgressDialogEnabled()); // } // }); } /** * * @return true if the tenant component of a business entity will be in use. */ public boolean isTenantEnabled() { return tenantEnabled; } /** * Fires property change of {@link BusinessEntityPreferenceManager#SERVER_PREF_TENANT_ENABLED} * * @param tenantEnabled flag that indicates if the tenant component of a business entity will be in use. */ void setTenantEnabled(boolean tenantEnabled) { boolean oldValue = this.tenantEnabled; this.tenantEnabled = tenantEnabled; firePropertyChange(SERVER_PREF_TENANT_ENABLED_KEY, oldValue, tenantEnabled); } /** * * @return true if the LOB component of a business entity will be in use. */ public boolean isLOBEnabled() { return lobEnabled; } /** * Fires property change of {@link BusinessEntityPreferenceManager#SERVER_PREF_LOB_ENABLED} * * @param lobEnabled flag that indicates if the LOB component of a business entity will be in use. */ void setLOBEnabled(boolean lobEnabled) { boolean oldValue = this.lobEnabled; this.lobEnabled = lobEnabled; firePropertyChange(SERVER_PREF_LOB_ENABLED_KEY, oldValue, lobEnabled); } /** * * @return true if the BU component of a business entity will be in use. */ public boolean isBUEnabled() { return buEnabled; } /** * Fires property change of {@link BusinessEntityPreferenceManager#SERVER_PREF_BU_ENABLED} * * @param buEnabled flag that indicates if the BU component of a business entity will be in use. */ void setBUEnabled(boolean buEnabled) { boolean oldValue = this.buEnabled; this.buEnabled = buEnabled; firePropertyChange(SERVER_PREF_BU_ENABLED_KEY, oldValue, buEnabled); } /** * * @return true if the project component of a business entity will be in use. */ public boolean isProjectEnabled() { return projectEnabled; } /** * Fires property change of {@link BusinessEntityPreferenceManager#SERVER_PREF_PROJECT_ENABLED} * * @param projectEnabled flag that indicates if the project component of a business entity will be in use. */ void setProjectEnabled(boolean projectEnabled) { boolean oldValue = this.projectEnabled; this.projectEnabled = projectEnabled; firePropertyChange(SERVER_PREF_PROJECT_ENABLED_KEY, oldValue, projectEnabled); } void readPreferences() { /* Preferences on server */ ParamSession paramSession = SessionsManager.getInstance().getSession(ParamSession.class); setTenantEnabled(paramSession.get(SERVER_PREF_TENANT_ENABLED, isTenantEnabled())); setLOBEnabled(paramSession.get(SERVER_PREF_LOB_ENABLED, isLOBEnabled())); setBUEnabled(paramSession.get(SERVER_PREF_BU_ENABLED, isBUEnabled())); setProjectEnabled(paramSession.get(SERVER_PREF_PROJECT_ENABLED, isProjectEnabled())); } void writePreferences() { try { /* Save server preferences */ ParamSession paramSession = SessionsManager.getInstance().getSession(ParamSession.class); paramSession.set(SERVER_PREF_TENANT_ENABLED, isTenantEnabled()); paramSession.set(SERVER_PREF_LOB_ENABLED, isLOBEnabled()); paramSession.set(SERVER_PREF_BU_ENABLED, isBUEnabled()); paramSession.set(SERVER_PREF_PROJECT_ENABLED, isProjectEnabled()); if (UsageLog.isEnabled()) { MessageBuilder builder = UsageLog.builder(UsageLogType.BUSINESS_ENTITY_PREFERENCES_CHANGE); builder.append(SERVER_PREF_TENANT_ENABLED_KEY, isTenantEnabled()); builder.append(SERVER_PREF_LOB_ENABLED_KEY, isLOBEnabled()); builder.append(SERVER_PREF_BU_ENABLED_KEY, isBUEnabled()); builder.append(SERVER_PREF_PROJECT_ENABLED_KEY, isProjectEnabled()); builder.log(); } } catch(ServerInterfaceException pe) { //TODO: handle ServerInterfaceException pe.printStackTrace(); } } private void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue); } public void addPropertyChangeListener(PropertyChangeListener listener) { propertyChangeSupport.addPropertyChangeListener(listener); } public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { propertyChangeSupport.addPropertyChangeListener(propertyName, listener); } public void removePropertyChangeListener(PropertyChangeListener listener) { propertyChangeSupport.removePropertyChangeListener(listener); } public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { propertyChangeSupport.removePropertyChangeListener(propertyName, listener); } }