[jboss-cvs] jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/preferences ...
Christian Bauer
christian at hibernate.org
Sat Dec 29 21:33:21 EST 2007
User: cbauer
Date: 07/12/29 21:33:21
Modified: examples/wiki/src/main/org/jboss/seam/wiki/preferences
PreferenceProvider.java PreferenceVisibility.java
PreferenceValue.java
Added: examples/wiki/src/main/org/jboss/seam/wiki/preferences
Preferences.java PreferenceValueTemplate.java
Removed: examples/wiki/src/main/org/jboss/seam/wiki/preferences
PreferenceProperty.java Preference.java
PreferenceSupport.java PreferenceRegistry.java
PreferenceComponent.java
Log:
Complete overhaul of the preferences system
Revision Changes Path
1.2 +6 -62 jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/preferences/PreferenceProvider.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: PreferenceProvider.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/preferences/PreferenceProvider.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- PreferenceProvider.java 2 Apr 2007 18:25:05 -0000 1.1
+++ PreferenceProvider.java 30 Dec 2007 02:33:21 -0000 1.2
@@ -1,70 +1,14 @@
package org.jboss.seam.wiki.preferences;
import java.util.Set;
+import java.util.List;
-/**
- * Interface for loading and storing of preference values.
- * <p>
- * Implement this interface to load and store preference values grouped by "preference component". Expose
- * your implementation as an auto-created Seam component with the name <tt>preferenceProvider</tt>.
- * <p>
- *
- * @author Christian Bauer
- */
-public interface PreferenceProvider {
+public interface PreferenceProvider<U, I> {
- /**
- * Load preference values for a particular component.
- * <p>
- * The <tt>user</tt> and <tt>instance</tt> arguments can be null, in that case, you need to only load system
- * preference values (see <tt>PreferenceVisibility</tt>) for that component. If <tt>user</tt> and <tt>instance</tt>
- * are provided, you can return a combination of values that represent the override. E.g. if the component has
- * preference properties foo1, foo2, and foo3, and foo2 allows user override while foo3 allows instance override,
- * you can return three values which you looked up accordingly.
- * <p>
- * If <tt>includeSystemPreferences</tt> is true, the provider should return all system-level preference values even
- * if they do not allow user and instance override. If this argument is false, the provider should not return any
- * system-level preference values, unless these propertis allow override on the user and instance level. To understand
- * this, think about the two use cases how preference values are loaded: We need the "current" preference values, optionally
- * resolved against the given user and instance. We also need the "current" preference values for editing, in that case however,
- * we don't want to see any values that can't be edited.
- *
- * @param component the preference component meta data, read this to know what to load
- * @param user an optional (nullable) user argument useful for override lookup
- * @param instance an optional (nullable) instance argument useful for override lookup
- * @param includeSystemPreferences true if the provider should load
- * @return a set of <tt>PreferenceValue</tt> objects, can be a sorted set
- */
- public Set<PreferenceValue> load(PreferenceComponent component, Object user, Object instance, boolean includeSystemPreferences);
+ public Set<PreferenceValue> loadValues(String preferenceEntityName, U user, I instance, List<PreferenceVisibility> visibilities);
+ public void storeValues(Set<PreferenceValue> valueHolders, U user, I instance);
- /**
- * Store preference values for particular component.
- * <p>
- * This method should not directly and immediately store the preference values, but queue them in some way.
- * They should only be flushed to a permanent data store when <tt>flush</tt> is called.
- *
- * @param component the preference component metadata for which the values should be stored
- * @param valueHolders the values to store, wrapped in the <tt>PreferenceValue</tt> interface
- * @param user an optional (nullable) user argument that can be used to convert the value holders before storing
- * @param instance an optional (nullable) instance argument that can be used to convert the value holders before storing
- * @return an updated set of <tt>PreferenceValue</tt> objects, if some value holders were converted
- */
- public Set<PreferenceValue> store(PreferenceComponent component, Set<PreferenceValue> valueHolders, Object user, Object instance);
+ public void deleteUserPreferenceValues(U user);
- /**
- * Delete all preference setting for a particular user (because the user was deleted)
- * @param user the user the preference values should be deleted for
- */
- public void deleteUserPreferences(Object user);
-
- /**
- * Delete all preference setting for a particular instance (because the instance was deleted)
- * @param instance the oinstance the preference values should be deleted for
- */
- public void deleteInstancePreferences(Object instance);
-
- /**
- * Write the queued preference values to a permanent data store
- */
public void flush();
}
1.2 +1 -11 jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/preferences/PreferenceVisibility.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: PreferenceVisibility.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/preferences/PreferenceVisibility.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- PreferenceVisibility.java 2 Apr 2007 18:25:05 -0000 1.1
+++ PreferenceVisibility.java 30 Dec 2007 02:33:21 -0000 1.2
@@ -2,16 +2,6 @@
import java.io.Serializable;
-/**
- * Support for multi-level preference overrides.
- * <p>
- * You can either only use <tt>SYSTEM</tt> and have one level of preferences that can
- * be changed with a system-level preferences editor, or you can override preference values
- * for a logged-in user or even for a currently active instance (e.g. the currently visible
- * document).
- *
- * @author Christian Bauer
- */
public enum PreferenceVisibility implements Serializable {
SYSTEM, USER, INSTANCE
}
1.2 +7 -16 jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/preferences/PreferenceValue.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: PreferenceValue.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/preferences/PreferenceValue.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- PreferenceValue.java 2 Apr 2007 18:25:05 -0000 1.1
+++ PreferenceValue.java 30 Dec 2007 02:33:21 -0000 1.2
@@ -1,26 +1,17 @@
package org.jboss.seam.wiki.preferences;
-/**
- * Implementation of a value holder, load and stored by <tt>PreferenceProvider</tt>.
- * <p>
- * Use this interface to plug-in your own preference values, your <tt>PreferenceProvider</tt>
- * has to return values wrapped in this interface and it will receive values wrapped in this
- * interface.
- *
- * @author Christian Bauer
- */
+import org.jboss.seam.wiki.preferences.metamodel.PreferenceEntity;
+
public interface PreferenceValue {
public Object getValue();
public void setValue(Object value);
- public void setPreferenceProperty(PreferenceProperty property);
- public PreferenceProperty getPreferenceProperty();
-
+ // Useful for provider, only set if the value really changed
public boolean isDirty();
- public boolean isSystemAssigned();
- public boolean isUserAssigned();
- public boolean isInstanceAssigned();
+ // Reference to meta model
+ public void setPreferenceProperty(PreferenceEntity.Property property);
+ public PreferenceEntity.Property getPreferenceProperty();
}
1.1 date: 2007/12/30 02:33:21; author: cbauer; state: Exp;jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/preferences/Preferences.java
Index: Preferences.java
===================================================================
package org.jboss.seam.wiki.preferences;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.*;
import org.jboss.seam.core.Expressions;
import org.jboss.seam.log.Log;
import org.jboss.seam.wiki.preferences.metamodel.PreferenceRegistry;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Name("preferences")
@Scope(ScopeType.CONVERSATION)
public class Preferences implements Serializable {
@Logger
Log log;
@In
PreferenceRegistry preferenceRegistry;
@In
org.jboss.seam.wiki.preferences.PreferenceProvider preferenceProvider;
@In(value = "#{currentPreferencesUser}", required = false)
Object currentUser;
// Cache SYSTEM and USER-level preferences per conversation
private Map<CacheKey, Object> conversationPreferencesCache = new HashMap<CacheKey, Object>();
// Cache INSTANCE-level preferences per event
@In(required = false) @Out(required = false, scope = ScopeType.EVENT)
private Map<CacheKey, Object> eventPreferencesCache;
public Object get(String preferenceEntityName) {
return get(preferenceEntityName, null);
}
public Object get(String preferenceEntityName, Object instance) {
log.trace("getting preferences for entity: " + preferenceEntityName +
" with current user: " + currentUser +
" and current instance: " + instance);
if (!preferenceRegistry.getPreferenceEntitiesByName().containsKey(preferenceEntityName))
throw new IllegalArgumentException("Preference entity not found in registry: " + preferenceEntityName);
// Prepare caching
CacheKey cacheKey = new CacheKey(preferenceEntityName);
if (eventPreferencesCache == null) eventPreferencesCache = new HashMap<CacheKey, Object>();
List<PreferenceVisibility> visibilities = new ArrayList<PreferenceVisibility>();
visibilities.add(PreferenceVisibility.SYSTEM);
if (currentUser != null) {
visibilities.add(PreferenceVisibility.USER);
cacheKey.setUser(currentUser);
}
if (instance != null ) {
visibilities.add(PreferenceVisibility.INSTANCE);
cacheKey.setInstance(instance);
}
// First check the caches
if (conversationPreferencesCache.containsKey(cacheKey)) {
log.trace("returning cached preference entity of current conversation");
return conversationPreferencesCache.get(cacheKey);
}
if (eventPreferencesCache.containsKey(cacheKey)) {
log.trace("returning cached preference entity of current event");
return eventPreferencesCache.get(cacheKey);
}
// No cache hit, ask the provider for the values and assemble the instance
Object preferenceEntityInstance =
preferenceRegistry.getPreferenceEntitiesByName().get(preferenceEntityName).materialize(
preferenceProvider.loadValues(preferenceEntityName, currentUser, instance, visibilities)
);
// Now put it in the right cache
if (instance != null) {
// INSTANCE-level goes into a per-event cache because the INSTANCE changes during a conversation
eventPreferencesCache.put(cacheKey, preferenceEntityInstance);
} else {
// SYSTEM and USER-level goes into a per-conversation cache because they never change during a conversation
conversationPreferencesCache.put(cacheKey, preferenceEntityInstance);
}
return preferenceEntityInstance;
}
public static Object getInstance(String preferenceEntityName, String instanceName) {
return Expressions.instance().createValueExpression("#{preferences.get('" + preferenceEntityName + "', "+instanceName+")}").getValue();
}
public static Object getInstance(String preferenceEntityName) {
return Expressions.instance().createValueExpression("#{preferences.get('" + preferenceEntityName + "')}").getValue();
}
private class CacheKey implements Serializable {
private String entityName;
private Object user;
private Object instance;
public CacheKey(String entityName) {
this.entityName = entityName;
}
public void setUser(Object user) {
this.user = user;
}
public void setInstance(Object instance) {
this.instance = instance;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CacheKey cacheKey = (CacheKey) o;
return !(entityName != null ? !entityName.equals(cacheKey.entityName) : cacheKey.entityName != null) &&
!(instance != null ? !instance.equals(cacheKey.instance) : cacheKey.instance!= null) &&
!(user != null ? !user.equals(cacheKey.user) : cacheKey.user != null);
}
public int hashCode() {
int result;
result = (entityName != null ? entityName.hashCode() : 0);
result = 31 * result + (instance != null ? instance.hashCode() : 0);
result = 31 * result + (user != null ? user.hashCode() : 0);
return result;
}
}
}
1.1 date: 2007/12/30 02:33:21; author: cbauer; state: Exp;jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/preferences/PreferenceValueTemplate.java
Index: PreferenceValueTemplate.java
===================================================================
package org.jboss.seam.wiki.preferences;
import java.util.List;
public interface PreferenceValueTemplate {
public List<String> getTemplateValues();
}
More information about the jboss-cvs-commits
mailing list