Skip to content
Snippets Groups Projects
Commit 55a3a998 authored by srosse's avatar srosse
Browse files

FXOLAT-423: never hold the GUI preferences property, if updated -> select and update in one shoot

parent 857e5f42
No related branches found
No related tags found
No related merge requests found
...@@ -42,7 +42,7 @@ public interface Preferences { ...@@ -42,7 +42,7 @@ public interface Preferences {
* @param key * @param key
* @return the object * @return the object
*/ */
public Object get(Class attributedClass, String key); public Object get(Class<?> attributedClass, String key);
/** /**
* @param attributedClass * @param attributedClass
...@@ -50,7 +50,7 @@ public interface Preferences { ...@@ -50,7 +50,7 @@ public interface Preferences {
* @param defaultValue the value returned if no such config exists * @param defaultValue the value returned if no such config exists
* @return the object * @return the object
*/ */
public Object get(Class attributedClass, String key, Object defaultValue); public Object get(Class<?> attributedClass, String key, Object defaultValue);
/** /**
...@@ -59,7 +59,7 @@ public interface Preferences { ...@@ -59,7 +59,7 @@ public interface Preferences {
* @param key * @param key
* @param value the object to save, which class must have a public default contructor (for xstreams to save/load) * @param value the object to save, which class must have a public default contructor (for xstreams to save/load)
*/ */
public void put(Class attributedClass, String key, Object value); public void put(Class<?> attributedClass, String key, Object value);
/** /**
* convenience method: as put, followed by a save() * convenience method: as put, followed by a save()
...@@ -68,7 +68,7 @@ public interface Preferences { ...@@ -68,7 +68,7 @@ public interface Preferences {
* @param key * @param key
* @param value * @param value
*/ */
public void putAndSave(Class attributedClass, String key, Object value); public void putAndSave(Class<?> attributedClass, String key, Object value);
//FIXME fj: remove method is missing //FIXME fj: remove method is missing
......
...@@ -67,5 +67,9 @@ public class PreferencesFactory { ...@@ -67,5 +67,9 @@ public class PreferencesFactory {
return preferencesStorage.getPreferencesFor(identity, useTransientPreferences); return preferencesStorage.getPreferencesFor(identity, useTransientPreferences);
} }
public PreferencesStorage getStorage() {
return preferencesStorage;
}
} }
...@@ -47,4 +47,6 @@ public interface PreferencesStorage { ...@@ -47,4 +47,6 @@ public interface PreferencesStorage {
* @return the preferences for the user * @return the preferences for the user
*/ */
public Preferences getPreferencesFor(Identity identity, boolean useTransientPreferences); public Preferences getPreferencesFor(Identity identity, boolean useTransientPreferences);
public void updatePreferencesFor(Preferences prefs, Identity identity);
} }
...@@ -32,11 +32,10 @@ import java.util.HashMap; ...@@ -32,11 +32,10 @@ import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import org.olat.core.CoreSpringFactory;
import org.olat.core.id.Identity; import org.olat.core.id.Identity;
import org.olat.core.util.prefs.Preferences; import org.olat.core.util.prefs.Preferences;
import org.olat.core.util.xml.XStreamHelper; import org.olat.core.util.prefs.PreferencesStorage;
import org.olat.properties.Property;
import org.olat.properties.PropertyManager;
/** /**
* Description:<br> * Description:<br>
...@@ -49,36 +48,29 @@ import org.olat.properties.PropertyManager; ...@@ -49,36 +48,29 @@ import org.olat.properties.PropertyManager;
public class DbPrefs implements Preferences { public class DbPrefs implements Preferences {
// keys: prefs-keys; values: any Prefs-Objects // keys: prefs-keys; values: any Prefs-Objects
private Map prefstore = new HashMap(); private Map<String,Object> prefstore = new HashMap<String,Object>();
// simply to indicate preferences version (serialized in the xstream, do not remove!)
private int version = 1;
private transient Identity owner; private transient Identity owner;
// true: don't save to disk, only in ram // true: don't save to disk, only in ram
transient boolean isTransient = false; private transient boolean isTransient = false;
transient Property dbProperty = null;
public DbPrefs() { public DbPrefs() {
// must have a default constructor for serialization! // must have a default constructor for serialization!
} }
public boolean isTransient() {
return isTransient;
}
public void setTransient(boolean isTransient) {
this.isTransient = isTransient;
}
public void save() { public void save() {
if (!isTransient) { if (!isTransient) {
PropertyManager pm = PropertyManager.getInstance(); PreferencesStorage storage = (PreferencesStorage)CoreSpringFactory.getBean("core.preferences.PreferencesStorage");
// generate x-stream serialization of this object storage.updatePreferencesFor(this, owner);
String props = XStreamHelper.toXML(this);
if (this.dbProperty == null) {
// save as new property
this.dbProperty = pm.createPropertyInstance(owner, null, null, null, DbStorage.USER_PROPERTY_KEY, null, null, null, props);
pm.saveProperty(this.dbProperty);
} else {
// update exising property
this.dbProperty.setTextValue(props);
pm.updateProperty(this.dbProperty);
}
} }
} }
...@@ -87,14 +79,14 @@ public class DbPrefs implements Preferences { ...@@ -87,14 +79,14 @@ public class DbPrefs implements Preferences {
* @param key * @param key
* @return Object * @return Object
*/ */
public Object get(Class attributedClass, String key) { public Object get(Class<?> attributedClass, String key) {
return prefstore.get(attributedClass.getName()+"::"+key); return prefstore.get(attributedClass.getName()+"::"+key);
} }
/** /**
* @see org.olat.core.util.prefs.Preferences#get(java.lang.Class, java.lang.String, java.lang.Object) * @see org.olat.core.util.prefs.Preferences#get(java.lang.Class, java.lang.String, java.lang.Object)
*/ */
public Object get(Class attributedClass, String key, Object defaultValue) { public Object get(Class<?> attributedClass, String key, Object defaultValue) {
Object value = get(attributedClass, key); Object value = get(attributedClass, key);
if (value == null) return defaultValue; if (value == null) return defaultValue;
return value; return value;
...@@ -106,7 +98,7 @@ public class DbPrefs implements Preferences { ...@@ -106,7 +98,7 @@ public class DbPrefs implements Preferences {
* @param value * @param value
* TODO: make value not object, but basetypemap or such? * TODO: make value not object, but basetypemap or such?
*/ */
public void put(Class attributedClass, String key, Object value) { public void put(Class<?> attributedClass, String key, Object value) {
prefstore.put(attributedClass.getName()+"::"+key, value); prefstore.put(attributedClass.getName()+"::"+key, value);
} }
...@@ -121,7 +113,7 @@ public class DbPrefs implements Preferences { ...@@ -121,7 +113,7 @@ public class DbPrefs implements Preferences {
* *
* @see org.olat.core.util.prefs.Preferences#putAndSave(java.lang.Class, java.lang.String, java.lang.Object) * @see org.olat.core.util.prefs.Preferences#putAndSave(java.lang.Class, java.lang.String, java.lang.Object)
*/ */
public void putAndSave(Class attributedClass, String key, Object value) { public void putAndSave(Class<?> attributedClass, String key, Object value) {
put(attributedClass, key, value); put(attributedClass, key, value);
save(); save();
} }
...@@ -131,9 +123,11 @@ public class DbPrefs implements Preferences { ...@@ -131,9 +123,11 @@ public class DbPrefs implements Preferences {
* @see org.olat.core.util.prefs.Preferences#findPrefByKey(java.lang.String) * @see org.olat.core.util.prefs.Preferences#findPrefByKey(java.lang.String)
*/ */
public Object findPrefByKey(String partOfKey) { public Object findPrefByKey(String partOfKey) {
for (Iterator iterator = prefstore.keySet().iterator(); iterator.hasNext();) { for (Iterator<String> iterator = prefstore.keySet().iterator(); iterator.hasNext();) {
String key = (String) iterator.next(); String key = iterator.next();
if (key.endsWith(partOfKey)) return prefstore.get(key); if (key.endsWith(partOfKey)) {
return prefstore.get(key);
}
} }
return null; return null;
} }
......
...@@ -32,7 +32,6 @@ import java.util.Iterator; ...@@ -32,7 +32,6 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import org.olat.core.id.Identity; import org.olat.core.id.Identity;
import org.olat.core.logging.AssertException;
import org.olat.core.logging.LogDelegator; import org.olat.core.logging.LogDelegator;
import org.olat.core.util.prefs.Preferences; import org.olat.core.util.prefs.Preferences;
import org.olat.core.util.prefs.PreferencesStorage; import org.olat.core.util.prefs.PreferencesStorage;
...@@ -40,6 +39,8 @@ import org.olat.core.util.xml.XStreamHelper; ...@@ -40,6 +39,8 @@ import org.olat.core.util.xml.XStreamHelper;
import org.olat.properties.Property; import org.olat.properties.Property;
import org.olat.properties.PropertyManager; import org.olat.properties.PropertyManager;
import com.thoughtworks.xstream.XStream;
/** /**
* Description:<br> * Description:<br>
* <P> * <P>
...@@ -51,6 +52,8 @@ public class DbStorage extends LogDelegator implements PreferencesStorage{ ...@@ -51,6 +52,8 @@ public class DbStorage extends LogDelegator implements PreferencesStorage{
static final String USER_PROPERTY_KEY = "v2guipreferences"; static final String USER_PROPERTY_KEY = "v2guipreferences";
private XStream xstream = XStreamHelper.createXStreamInstance();
public Preferences getPreferencesFor(Identity identity, boolean useTransientPreferences) { public Preferences getPreferencesFor(Identity identity, boolean useTransientPreferences) {
if (useTransientPreferences) { if (useTransientPreferences) {
return createEmptyDbPrefs(identity,true); return createEmptyDbPrefs(identity,true);
...@@ -59,36 +62,53 @@ public class DbStorage extends LogDelegator implements PreferencesStorage{ ...@@ -59,36 +62,53 @@ public class DbStorage extends LogDelegator implements PreferencesStorage{
} }
} }
@Override
public void updatePreferencesFor(Preferences prefs, Identity identity) {
String props = xstream.toXML(prefs);
System.out.println("UPdate preferences");
Property property = getPreferencesProperty(identity);
if(property == null) {
PropertyManager.getInstance().createPropertyInstance(identity, null, null, null, DbStorage.USER_PROPERTY_KEY, null, null, null, props);
} else {
property.setTextValue(props);
PropertyManager.getInstance().updateProperty(property);
}
}
/** /**
* search x-stream serialization in properties table, create new if not found * search x-stream serialization in properties table, create new if not found
* @param identity * @param identity
* @return * @return
*/ */
private DbPrefs getPreferencesFor(final Identity identity) { private DbPrefs getPreferencesFor(final Identity identity) {
Property guiProperty = null; Property guiProperty = getPreferencesProperty(identity);
try { if (guiProperty == null) {
guiProperty = PropertyManager.getInstance().findProperty(identity, null, null, null, USER_PROPERTY_KEY); return createEmptyDbPrefs(identity,false);
} catch (Exception e) { } else {
// OLAT-6429 detect and delete multiple prefs objects, keep the first one only return getPreferencesForProperty(identity, guiProperty);
List<Property> guiPropertyList = PropertyManager.getInstance().findProperties(identity, null, null, null, USER_PROPERTY_KEY); }
if (guiPropertyList != null && guiPropertyList.size() > 0) { }
private Property getPreferencesProperty(Identity identity) {
Property guiProperty = null;
try {
guiProperty = PropertyManager.getInstance().findProperty(identity, null, null, null, USER_PROPERTY_KEY);
} catch (Exception e) {
// OLAT-6429 detect and delete multiple prefs objects, keep the first one only
List<Property> guiPropertyList = PropertyManager.getInstance().findProperties(identity, null, null, null, USER_PROPERTY_KEY);
if (guiPropertyList != null && guiPropertyList.size() > 0) {
logError("Found more than 1 entry for " + USER_PROPERTY_KEY + " in o_property table for user " + identity.getName() + ". Use first of them, deleting the others!", e); logError("Found more than 1 entry for " + USER_PROPERTY_KEY + " in o_property table for user " + identity.getName() + ". Use first of them, deleting the others!", e);
guiProperty = guiPropertyList.get(0); guiProperty = guiPropertyList.get(0);
Iterator<Property> iterator = guiPropertyList.iterator(); Iterator<Property> iterator = guiPropertyList.iterator();
iterator.next(); iterator.next();
while (iterator.hasNext()) { while (iterator.hasNext()) {
Property property = (Property) iterator.next(); Property property = iterator.next();
PropertyManager.getInstance().deleteProperty(property); PropertyManager.getInstance().deleteProperty(property);
logInfo("Will delete old property: " + property.getTextValue()); logInfo("Will delete old property: " + property.getTextValue());
} }
} }
}
if (guiProperty == null) {
return createEmptyDbPrefs(identity,false);
} else {
return getPreferencesForProperty(identity, guiProperty);
} }
return guiProperty;
} }
private DbPrefs getPreferencesForProperty(Identity identity, Property guiProperty) { private DbPrefs getPreferencesForProperty(Identity identity, Property guiProperty) {
...@@ -104,14 +124,13 @@ public class DbStorage extends LogDelegator implements PreferencesStorage{ ...@@ -104,14 +124,13 @@ public class DbStorage extends LogDelegator implements PreferencesStorage{
private DbPrefs createEmptyDbPrefs(Identity identity, boolean isTransient) { private DbPrefs createEmptyDbPrefs(Identity identity, boolean isTransient) {
DbPrefs prefs = new DbPrefs(); DbPrefs prefs = new DbPrefs();
prefs.setIdentity(identity); prefs.setIdentity(identity);
prefs.isTransient = isTransient; prefs.setTransient(isTransient);
return prefs; return prefs;
} }
private DbPrefs createDbPrefsFrom(Identity identity, Property guiProperty, String textValue) { private DbPrefs createDbPrefsFrom(Identity identity, Property guiProperty, String textValue) {
DbPrefs prefs = (DbPrefs) XStreamHelper.fromXML(textValue); DbPrefs prefs = (DbPrefs) xstream.fromXML(textValue);
prefs.setIdentity(identity); // reset transient value prefs.setIdentity(identity); // reset transient value
prefs.dbProperty = guiProperty; // set property for later use
return prefs; return prefs;
} }
......
...@@ -39,20 +39,20 @@ import org.olat.core.util.prefs.Preferences; ...@@ -39,20 +39,20 @@ import org.olat.core.util.prefs.Preferences;
* @author Felix * @author Felix
*/ */
public class RamPreferences implements Preferences { public class RamPreferences implements Preferences {
private Map store = new HashMap(); private Map<String, Object> store = new HashMap<String, Object>();
/** /**
* @see org.olat.core.util.prefs.Preferences#get(java.lang.Class, java.lang.String) * @see org.olat.core.util.prefs.Preferences#get(java.lang.Class, java.lang.String)
*/ */
public Object get(Class attributedClass, String key) { public Object get(Class<?> attributedClass, String key) {
return store.get(getCompoundKey(attributedClass, key)); return store.get(getCompoundKey(attributedClass, key));
} }
/** /**
* @see org.olat.core.util.prefs.Preferences#get(java.lang.Class, java.lang.String, java.lang.Object) * @see org.olat.core.util.prefs.Preferences#get(java.lang.Class, java.lang.String, java.lang.Object)
*/ */
public Object get(Class attributedClass, String key, Object defaultValue) { public Object get(Class<?> attributedClass, String key, Object defaultValue) {
Object value = get(attributedClass, key); Object value = get(attributedClass, key);
if (value == null) return defaultValue; if (value == null) return defaultValue;
return value; return value;
...@@ -61,14 +61,14 @@ public class RamPreferences implements Preferences { ...@@ -61,14 +61,14 @@ public class RamPreferences implements Preferences {
/** /**
* @see org.olat.core.util.prefs.Preferences#put(java.lang.Class, java.lang.String, java.lang.Object) * @see org.olat.core.util.prefs.Preferences#put(java.lang.Class, java.lang.String, java.lang.Object)
*/ */
public void put(Class attributedClass, String key, Object value) { public void put(Class<?> attributedClass, String key, Object value) {
store.put(getCompoundKey(attributedClass, key), value); store.put(getCompoundKey(attributedClass, key), value);
} }
/** /**
* @see org.olat.core.util.prefs.Preferences#putAndSave(java.lang.Class, java.lang.String, java.lang.Object) * @see org.olat.core.util.prefs.Preferences#putAndSave(java.lang.Class, java.lang.String, java.lang.Object)
*/ */
public void putAndSave(Class attributedClass, String key, Object value) { public void putAndSave(Class<?> attributedClass, String key, Object value) {
put(attributedClass, key, value); put(attributedClass, key, value);
save(); save();
...@@ -82,13 +82,13 @@ public class RamPreferences implements Preferences { ...@@ -82,13 +82,13 @@ public class RamPreferences implements Preferences {
} }
private String getCompoundKey(Class attributedClass, String key) { private String getCompoundKey(Class<?> attributedClass, String key) {
return attributedClass.getName()+":"+key; return attributedClass.getName()+":"+key;
} }
public Object findPrefByKey(String partOfKey) { public Object findPrefByKey(String partOfKey) {
for (Iterator iterator = store.keySet().iterator(); iterator.hasNext();) { for (Iterator<String> iterator = store.keySet().iterator(); iterator.hasNext();) {
String key = (String) iterator.next(); String key = iterator.next();
if (key.endsWith(partOfKey)) return store.get(key); if (key.endsWith(partOfKey)) return store.get(key);
} }
return null; return null;
......
...@@ -43,10 +43,10 @@ import org.olat.core.util.prefs.PreferencesStorage; ...@@ -43,10 +43,10 @@ import org.olat.core.util.prefs.PreferencesStorage;
* @author Felix Jost * @author Felix Jost
*/ */
public class RamPreferencesStorage implements PreferencesStorage { public class RamPreferencesStorage implements PreferencesStorage {
private Map identToPrefs; private Map<String, Preferences> identToPrefs;
public RamPreferencesStorage() { public RamPreferencesStorage() {
identToPrefs = new HashMap(); identToPrefs = new HashMap<String, Preferences>();
} }
/** /**
...@@ -65,4 +65,11 @@ public class RamPreferencesStorage implements PreferencesStorage { ...@@ -65,4 +65,11 @@ public class RamPreferencesStorage implements PreferencesStorage {
return p; return p;
} }
@Override
public void updatePreferencesFor(Preferences prefs, Identity identity) {
//
}
} }
\ No newline at end of file
...@@ -206,19 +206,20 @@ class SpecialPrefsForm extends FormBasicController { ...@@ -206,19 +206,20 @@ class SpecialPrefsForm extends FormBasicController {
} }
if (useAjaxCheckbox) { if (useAjaxCheckbox) {
prefs.putAndSave(WindowManager.class, "ajax-beta-on", prefsElement.getSelectedKeys().contains("ajax")); prefs.put(WindowManager.class, "ajax-beta-on", prefsElement.getSelectedKeys().contains("ajax"));
} }
prefs.putAndSave(WindowManager.class, "web2a-beta-on", prefsElement.getSelectedKeys().contains("web2a")); prefs.putAndSave(WindowManager.class, "web2a-beta-on", prefsElement.getSelectedKeys().contains("web2a"));
//fxdiff BAKS-7 Resume function //fxdiff BAKS-7 Resume function
if(resumeElement != null) { if(resumeElement != null) {
prefs.putAndSave(WindowManager.class, "resume-prefs", resumeElement.getSelectedKey()); prefs.put(WindowManager.class, "resume-prefs", resumeElement.getSelectedKey());
} }
if(backElement != null) { if(backElement != null) {
prefs.putAndSave(WindowManager.class, "back-enabled", backElement.isSelected(0)); prefs.put(WindowManager.class, "back-enabled", backElement.isSelected(0));
} }
if (ureq.getIdentity().equalsByPersistableKey(tobeChangedIdentity)) { if (ureq.getIdentity().equalsByPersistableKey(tobeChangedIdentity)) {
showInfo("preferences.successful"); showInfo("preferences.successful");
} }
prefs.save();
} }
@Override @Override
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment