diff --git a/src/main/java/org/olat/core/util/prefs/Preferences.java b/src/main/java/org/olat/core/util/prefs/Preferences.java index 00155548520535126018811e01936cc5ce213b3a..9ad4fef1e197c4a997f9fdfafcc2a19ebb1797e8 100644 --- a/src/main/java/org/olat/core/util/prefs/Preferences.java +++ b/src/main/java/org/olat/core/util/prefs/Preferences.java @@ -42,7 +42,7 @@ public interface Preferences { * @param key * @return the object */ - public Object get(Class attributedClass, String key); + public Object get(Class<?> attributedClass, String key); /** * @param attributedClass @@ -50,7 +50,7 @@ public interface Preferences { * @param defaultValue the value returned if no such config exists * @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 { * @param key * @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() @@ -68,7 +68,7 @@ public interface Preferences { * @param key * @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 diff --git a/src/main/java/org/olat/core/util/prefs/PreferencesFactory.java b/src/main/java/org/olat/core/util/prefs/PreferencesFactory.java index e4de89794ff2d8d3f3922760b9672174775cdfa7..18a502773404a67d03dda17be42d285c39a4f7d1 100644 --- a/src/main/java/org/olat/core/util/prefs/PreferencesFactory.java +++ b/src/main/java/org/olat/core/util/prefs/PreferencesFactory.java @@ -67,5 +67,9 @@ public class PreferencesFactory { return preferencesStorage.getPreferencesFor(identity, useTransientPreferences); } + public PreferencesStorage getStorage() { + return preferencesStorage; + } + } diff --git a/src/main/java/org/olat/core/util/prefs/PreferencesStorage.java b/src/main/java/org/olat/core/util/prefs/PreferencesStorage.java index 6129244dd92038fd718ec503bddb90bf34803a40..4e501aa6d94d21b177eeb16b3dcd62fc18922836 100644 --- a/src/main/java/org/olat/core/util/prefs/PreferencesStorage.java +++ b/src/main/java/org/olat/core/util/prefs/PreferencesStorage.java @@ -47,4 +47,6 @@ public interface PreferencesStorage { * @return the preferences for the user */ public Preferences getPreferencesFor(Identity identity, boolean useTransientPreferences); + + public void updatePreferencesFor(Preferences prefs, Identity identity); } diff --git a/src/main/java/org/olat/core/util/prefs/db/DbPrefs.java b/src/main/java/org/olat/core/util/prefs/db/DbPrefs.java index 0e2a6dea45f3e13f10c82ce8dbf594fc9c7f8a5b..0cf840a816b965bd9ee8d9e08dc1fbb6f914a010 100644 --- a/src/main/java/org/olat/core/util/prefs/db/DbPrefs.java +++ b/src/main/java/org/olat/core/util/prefs/db/DbPrefs.java @@ -32,11 +32,10 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; +import org.olat.core.CoreSpringFactory; import org.olat.core.id.Identity; import org.olat.core.util.prefs.Preferences; -import org.olat.core.util.xml.XStreamHelper; -import org.olat.properties.Property; -import org.olat.properties.PropertyManager; +import org.olat.core.util.prefs.PreferencesStorage; /** * Description:<br> @@ -49,36 +48,29 @@ import org.olat.properties.PropertyManager; public class DbPrefs implements Preferences { // keys: prefs-keys; values: any Prefs-Objects - private Map prefstore = new HashMap(); - - // simply to indicate preferences version (serialized in the xstream, do not remove!) - private int version = 1; - + private Map<String,Object> prefstore = new HashMap<String,Object>(); + private transient Identity owner; // true: don't save to disk, only in ram - transient boolean isTransient = false; - transient Property dbProperty = null; + private transient boolean isTransient = false; public DbPrefs() { // must have a default constructor for serialization! } - - + + public boolean isTransient() { + return isTransient; + } + + public void setTransient(boolean isTransient) { + this.isTransient = isTransient; + } + public void save() { if (!isTransient) { - PropertyManager pm = PropertyManager.getInstance(); - // generate x-stream serialization of this object - 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); - } + PreferencesStorage storage = (PreferencesStorage)CoreSpringFactory.getBean("core.preferences.PreferencesStorage"); + storage.updatePreferencesFor(this, owner); } } @@ -87,14 +79,14 @@ public class DbPrefs implements Preferences { * @param key * @return Object */ - public Object get(Class attributedClass, String key) { + public Object get(Class<?> attributedClass, String key) { return prefstore.get(attributedClass.getName()+"::"+key); } /** * @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); if (value == null) return defaultValue; return value; @@ -106,7 +98,7 @@ public class DbPrefs implements Preferences { * @param value * 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); } @@ -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) */ - public void putAndSave(Class attributedClass, String key, Object value) { + public void putAndSave(Class<?> attributedClass, String key, Object value) { put(attributedClass, key, value); save(); } @@ -131,9 +123,11 @@ public class DbPrefs implements Preferences { * @see org.olat.core.util.prefs.Preferences#findPrefByKey(java.lang.String) */ public Object findPrefByKey(String partOfKey) { - for (Iterator iterator = prefstore.keySet().iterator(); iterator.hasNext();) { - String key = (String) iterator.next(); - if (key.endsWith(partOfKey)) return prefstore.get(key); + for (Iterator<String> iterator = prefstore.keySet().iterator(); iterator.hasNext();) { + String key = iterator.next(); + if (key.endsWith(partOfKey)) { + return prefstore.get(key); + } } return null; } diff --git a/src/main/java/org/olat/core/util/prefs/db/DbStorage.java b/src/main/java/org/olat/core/util/prefs/db/DbStorage.java index cbbe770fedbe496f0f01ea31d307d03e2fa8e7df..dda1161d25bd00bd95f87b8360ddb9ba862fdbca 100644 --- a/src/main/java/org/olat/core/util/prefs/db/DbStorage.java +++ b/src/main/java/org/olat/core/util/prefs/db/DbStorage.java @@ -32,7 +32,6 @@ import java.util.Iterator; import java.util.List; import org.olat.core.id.Identity; -import org.olat.core.logging.AssertException; import org.olat.core.logging.LogDelegator; import org.olat.core.util.prefs.Preferences; import org.olat.core.util.prefs.PreferencesStorage; @@ -40,6 +39,8 @@ import org.olat.core.util.xml.XStreamHelper; import org.olat.properties.Property; import org.olat.properties.PropertyManager; +import com.thoughtworks.xstream.XStream; + /** * Description:<br> * <P> @@ -51,6 +52,8 @@ public class DbStorage extends LogDelegator implements PreferencesStorage{ static final String USER_PROPERTY_KEY = "v2guipreferences"; + private XStream xstream = XStreamHelper.createXStreamInstance(); + public Preferences getPreferencesFor(Identity identity, boolean useTransientPreferences) { if (useTransientPreferences) { return createEmptyDbPrefs(identity,true); @@ -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 * @param identity * @return */ private DbPrefs getPreferencesFor(final 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) { + Property guiProperty = getPreferencesProperty(identity); + if (guiProperty == null) { + return createEmptyDbPrefs(identity,false); + } else { + return getPreferencesForProperty(identity, guiProperty); + } + } + + 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); guiProperty = guiPropertyList.get(0); Iterator<Property> iterator = guiPropertyList.iterator(); iterator.next(); while (iterator.hasNext()) { - Property property = (Property) iterator.next(); + Property property = iterator.next(); PropertyManager.getInstance().deleteProperty(property); 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) { @@ -104,14 +124,13 @@ public class DbStorage extends LogDelegator implements PreferencesStorage{ private DbPrefs createEmptyDbPrefs(Identity identity, boolean isTransient) { DbPrefs prefs = new DbPrefs(); prefs.setIdentity(identity); - prefs.isTransient = isTransient; + prefs.setTransient(isTransient); return prefs; } 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.dbProperty = guiProperty; // set property for later use return prefs; } diff --git a/src/main/java/org/olat/core/util/prefs/ram/RamPreferences.java b/src/main/java/org/olat/core/util/prefs/ram/RamPreferences.java index bec26a36fc14463d3c59609be805505295509440..d628c45843bf6705e501709c78d1a495e1a35e0d 100644 --- a/src/main/java/org/olat/core/util/prefs/ram/RamPreferences.java +++ b/src/main/java/org/olat/core/util/prefs/ram/RamPreferences.java @@ -39,20 +39,20 @@ import org.olat.core.util.prefs.Preferences; * @author Felix */ 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) */ - public Object get(Class attributedClass, String key) { + public Object get(Class<?> attributedClass, String key) { return store.get(getCompoundKey(attributedClass, key)); } /** * @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); if (value == null) return defaultValue; return value; @@ -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) */ - public void put(Class attributedClass, String key, Object value) { + public void put(Class<?> attributedClass, String key, Object value) { store.put(getCompoundKey(attributedClass, key), value); } /** * @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); save(); @@ -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; } public Object findPrefByKey(String partOfKey) { - for (Iterator iterator = store.keySet().iterator(); iterator.hasNext();) { - String key = (String) iterator.next(); + for (Iterator<String> iterator = store.keySet().iterator(); iterator.hasNext();) { + String key = iterator.next(); if (key.endsWith(partOfKey)) return store.get(key); } return null; diff --git a/src/main/java/org/olat/core/util/prefs/ram/RamPreferencesStorage.java b/src/main/java/org/olat/core/util/prefs/ram/RamPreferencesStorage.java index 302033c539acd3e7f09a0cc63b2f6a5604a68f21..402ec5ce10e7606db719a5b2cb6f24d47e12b0e5 100644 --- a/src/main/java/org/olat/core/util/prefs/ram/RamPreferencesStorage.java +++ b/src/main/java/org/olat/core/util/prefs/ram/RamPreferencesStorage.java @@ -43,10 +43,10 @@ import org.olat.core.util.prefs.PreferencesStorage; * @author Felix Jost */ public class RamPreferencesStorage implements PreferencesStorage { - private Map identToPrefs; + private Map<String, Preferences> identToPrefs; public RamPreferencesStorage() { - identToPrefs = new HashMap(); + identToPrefs = new HashMap<String, Preferences>(); } /** @@ -65,4 +65,11 @@ public class RamPreferencesStorage implements PreferencesStorage { return p; } + @Override + public void updatePreferencesFor(Preferences prefs, Identity identity) { + // + } + + + } \ No newline at end of file diff --git a/src/main/java/org/olat/user/ChangePrefsController.java b/src/main/java/org/olat/user/ChangePrefsController.java index fe414dbc7709ed5b06cf2be4c18b24401dab5609..7a2b9ea8240e95035163fe30963055a483d98919 100644 --- a/src/main/java/org/olat/user/ChangePrefsController.java +++ b/src/main/java/org/olat/user/ChangePrefsController.java @@ -206,19 +206,20 @@ class SpecialPrefsForm extends FormBasicController { } 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")); //fxdiff BAKS-7 Resume function if(resumeElement != null) { - prefs.putAndSave(WindowManager.class, "resume-prefs", resumeElement.getSelectedKey()); + prefs.put(WindowManager.class, "resume-prefs", resumeElement.getSelectedKey()); } 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)) { showInfo("preferences.successful"); } + prefs.save(); } @Override