Update of 
/var/cvs/contributions/CMSContainer_Modules/community/src/java/com/finalist/cmsc/services/community/preferences
In directory 
james.mmbase.org:/tmp/cvs-serv13333/src/java/com/finalist/cmsc/services/community/preferences

Modified Files:
        PreferenceService.java PreferenceHibernateService.java 
        Preference.java 
Log Message:



See also: 
http://cvs.mmbase.org/viewcvs/contributions/CMSContainer_Modules/community/src/java/com/finalist/cmsc/services/community/preferences


Index: PreferenceService.java
===================================================================
RCS file: 
/var/cvs/contributions/CMSContainer_Modules/community/src/java/com/finalist/cmsc/services/community/preferences/PreferenceService.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- PreferenceService.java      25 Jan 2008 13:03:39 -0000      1.1
+++ PreferenceService.java      29 Jan 2008 09:59:39 -0000      1.2
@@ -9,15 +9,51 @@
 */
 package com.finalist.cmsc.services.community.preferences;
 
-import java.io.Serializable;
 import java.util.Map;
 
 
+/**
+ * @author Remco Bos
+ */
 public interface PreferenceService {
 
-    Map<String, Serializable> getPreferences(String module, String userName, 
String key, String value);
-    
-    Map<String, Serializable> createPreferences(String module, String 
userName, String key, String value);
-    
+       /**
+        * Returns all preferences by module
+        * 
+        * @param module Module name
+        * @return map A map with preferences for a module, grouped by 
autenticationId
+        */
+//     Map<Long, Map<String, String>> getPreferencesByModule(String module);
+       
+       /**
+        * Returns all preferences by userId
+        * 
+        * @param user User id
+        * @return map A map with preferences for a userId, grouped by module
+        */
+//     Map<String, Map<String, String>> getPreferencesByUserId(String userId);
+       
+    /**
+     * Returns all preferences by module and user
+     * @param module
+     * @param userId
+     * @return map A map with preferences for a module and userId 
+     */
+//     Map<String, String> getPreferences(String module, String userId);
+
+       /**
+     * Returns all preferences by module, userId and key
+     * @param module
+     * @param userId
+     * @param key
+     * @return map A map with preferences for a module, userId and key 
+     */
+//     Map<String, String> getPreferences(String module, String userId, String 
key);
+    Map<Long, Map<String, String>> getPreferencesByModule(String module);
+    Map<String, Map<String, String>> getPreferencesByUserId(String userId);
+    Map<String, String> getPreferences(String module, String userId, String 
key);
+    void createPreference(String module, String userId, String key, String 
value);
+       void updatePreference(String module, String userId, String key, String 
oldValue, String newValue);
+       void deletePreference(String module, String userId, String key, String 
value);
     
 }


Index: PreferenceHibernateService.java
===================================================================
RCS file: 
/var/cvs/contributions/CMSContainer_Modules/community/src/java/com/finalist/cmsc/services/community/preferences/PreferenceHibernateService.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- PreferenceHibernateService.java     25 Jan 2008 13:03:39 -0000      1.1
+++ PreferenceHibernateService.java     29 Jan 2008 09:59:39 -0000      1.2
@@ -10,19 +10,16 @@
 package com.finalist.cmsc.services.community.preferences;
 
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.Iterator;
-import java.io.Serializable;
 
-import org.hibernate.criterion.Restrictions;
-import org.hibernate.criterion.DetachedCriteria;
 import org.hibernate.Criteria;
-import org.springframework.transaction.annotation.Transactional;
+import org.hibernate.criterion.Restrictions;
 import org.springframework.beans.factory.annotation.Required;
+import org.springframework.transaction.annotation.Transactional;
 
 import com.finalist.cmsc.services.HibernateService;
-import com.finalist.cmsc.services.community.security.Authentication;
 import com.finalist.cmsc.services.community.security.AuthenticationService;
 
 /**
@@ -34,61 +31,122 @@
 
     /** [EMAIL PROTECTED] */
     @Transactional(readOnly = true)
-    public Map<String, Serializable> getPreferences(String module, String 
userName, String key, String value) {
-        Long userId = null;
-        if (!isEmpty(userName)) {
-            Authentication authentication = 
authenticationService.findAuthentication(userName);
-            if (authentication != null) {
-                userId = authentication.getId();
+    @SuppressWarnings("unchecked")
+       public Map<Long, Map<String, String>> getPreferencesByModule(String 
module) {
+        Criteria criteria = getSession().createCriteria(Preference.class);
+        criteria.add(Restrictions.eq("module", module));
+        List preferenceList = criteria.list();
+        Map<Long, Map<String, String>> userPreferenceMap = new HashMap<Long, 
Map<String, String>>();
+        for (Iterator iter = preferenceList.iterator(); iter.hasNext();) {
+            Preference p = (Preference) iter.next();
+            Map<String, String> preferenceMap = (Map<String, 
String>)userPreferenceMap.get(p.getAuthenticationId());
+            if (preferenceMap == null) {
+               preferenceMap = new HashMap<String, String>();
+               userPreferenceMap.put(p.getAuthenticationId(), preferenceMap);
             }
+            preferenceMap.put(p.getKey(), p.getValue());
         }
-        return findPreferences(module, userId, key, value);
+        return userPreferenceMap;
     }
 
     /** [EMAIL PROTECTED] */
-    @Transactional
-    public Map<String, Serializable> createPreferences(String module, String 
userName, String key, String value) {
-        Map<String, Serializable> result = null;
-        Authentication authentication = 
authenticationService.findAuthentication(userName);
-        if (authentication != null) {
-            Preference preference = new Preference();
-            preference.setModule(module);
-            preference.setUserId(authentication.getId());
-            preference.setKey(key);
-            preference.setValue(value);
-            getSession().save(preference);
-            result = findPreferences(module, authentication.getId(), key, 
value);
+    @Transactional(readOnly = true)
+       @SuppressWarnings("unchecked")
+       public Map<String, Map<String, String>> getPreferencesByUserId(String 
userId) {
+       Long authenticationId = 
authenticationService.getAuthenticationIdForUserId(userId);
+        Criteria criteria = getSession().createCriteria(Preference.class);
+        criteria.add(Restrictions.eq("authenticationId", authenticationId));
+        List preferenceList = criteria.list();
+        Map<String, Map<String, String>> modulePreferenceMap = new 
HashMap<String, Map<String, String>>();
+        for (Iterator iter = preferenceList.iterator(); iter.hasNext();) {
+            Preference p = (Preference) iter.next();
+            Map<String, String> preferenceMap = (Map<String, 
String>)modulePreferenceMap.get(p.getModule());
+            if (preferenceMap == null) {
+               preferenceMap = new HashMap<String, String>();
+               modulePreferenceMap.put(p.getModule(), preferenceMap);
         }
-        return result;
+            preferenceMap.put(p.getKey(), p.getValue());
+        }
+        return modulePreferenceMap;
     }
 
-    private Map<String, Serializable> findPreferences(String module, Long 
userId, String key, String value) {
+       /** [EMAIL PROTECTED] */
+    @Transactional(readOnly = true)
+       public Map<String, String> getPreferences(String module, String userId) 
{
+       Long authenticationId = 
authenticationService.getAuthenticationIdForUserId(userId);
         Criteria criteria = getSession().createCriteria(Preference.class);
-        if (!isEmpty(module)) {
             criteria.add(Restrictions.eq("module", module));
+        criteria.add(Restrictions.eq("authenticationId", authenticationId));
+        return gePreferencesMap(criteria);
         }
-        if (userId != null) {
-            criteria.add(Restrictions.eq("userId", userId));
-        }
-        if (!isEmpty(key)) {
+
+       /** [EMAIL PROTECTED] */
+    @Transactional(readOnly = true)
+       public Map<String, String> getPreferences(String module, String userId, 
String key) {
+       Long authenticationId = 
authenticationService.getAuthenticationIdForUserId(userId);
+        Criteria criteria = getSession().createCriteria(Preference.class);
+        criteria.add(Restrictions.eq("module", module));
+        criteria.add(Restrictions.eq("authenticationId", authenticationId));
             criteria.add(Restrictions.eq("key", key));
+        return gePreferencesMap(criteria);
         }
-        if (!isEmpty(value)) {
-            criteria.add(Restrictions.eq("value", value));
-        }
-        List preferenceList = criteria.list();
-        if (preferenceList.size() == 0) return null;
 
-        Map preferenceMap = new HashMap<String, Serializable>();
-        for (Iterator iter = preferenceList.iterator(); iter.hasNext();) {
+    @SuppressWarnings("unchecked")
+    private Map<String, String> gePreferencesMap(Criteria criteria) {
+        List userPreferenceList = criteria.list();
+        Map<String, String> preferenceMap = new HashMap<String, String>();
+        for (Iterator iter = userPreferenceList.iterator(); iter.hasNext();) {
             Preference p = (Preference) iter.next();
             preferenceMap.put(p.getKey(), p.getValue());
         }
         return preferenceMap;
     }
 
-    private boolean isEmpty(String stringValue) {
-        return stringValue == null || stringValue.equals("");
+       /** [EMAIL PROTECTED] */
+    @Transactional
+       public void createPreference(String module, String userId, String key, 
String value) {
+       Long authenticationId = 
authenticationService.getAuthenticationIdForUserId(userId);
+        Preference preference = new Preference();
+        preference.setModule(module);
+        preference.setAuthenticationId(authenticationId);
+        preference.setKey(key);
+        preference.setValue(value);
+        getSession().save(preference);
+       }
+
+
+       /** [EMAIL PROTECTED] */
+    @Transactional
+    @SuppressWarnings("unchecked")
+    public void updatePreference(String module, String userId, String key, 
String oldvalue, String newValue) {
+       Long authenticationId = 
authenticationService.getAuthenticationIdForUserId(userId);
+       Criteria criteria = getSession().createCriteria(Preference.class);
+        criteria.add(Restrictions.eq("module", module));
+        criteria.add(Restrictions.eq("authenticationId", authenticationId));
+        criteria.add(Restrictions.eq("key", key));
+        criteria.add(Restrictions.eq("value", oldvalue));
+        List preferences = criteria.list();
+        if (preferences.size() == 1) {
+               Preference p = (Preference)preferences.get(0);
+               p.setValue(newValue);
+        }
+       }
+       
+       /** [EMAIL PROTECTED] */
+    @Transactional
+    @SuppressWarnings("unchecked")
+    public void deletePreference(String module, String userId, String key, 
String value) {
+       Long authenticationId = 
authenticationService.getAuthenticationIdForUserId(userId);
+       Criteria criteria = getSession().createCriteria(Preference.class);
+        criteria.add(Restrictions.eq("module", module));
+        criteria.add(Restrictions.eq("authenticationId", authenticationId));
+        criteria.add(Restrictions.eq("key", key));
+        criteria.add(Restrictions.eq("value", value));
+        List preferences = criteria.list();
+        if (preferences.size() == 1) {
+               Preference p = (Preference)preferences.get(0);
+               getSession().delete(p);
+        }
     }
 
     @Required


Index: Preference.java
===================================================================
RCS file: 
/var/cvs/contributions/CMSContainer_Modules/community/src/java/com/finalist/cmsc/services/community/preferences/Preference.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- Preference.java     25 Jan 2008 13:03:39 -0000      1.1
+++ Preference.java     29 Jan 2008 09:59:39 -0000      1.2
@@ -28,8 +28,9 @@
     @Id
     @GeneratedValue
     private Long id;
+    
     private String module;
-    private Long userId;
+    private Long authenticationId;
     private String key;
     private String value;
 
@@ -49,12 +50,12 @@
         this.module = module;
     }
 
-    public Long getUserId() {
-        return userId;
+    public Long getAuthenticationId() {
+        return authenticationId;
     }
 
-    public void setUserId(Long userId) {
-        this.userId = userId;
+    public void setAuthenticationId(Long authenticationId) {
+        this.authenticationId = authenticationId;
     }
 
     public String getKey() {
@@ -72,4 +73,47 @@
     public void setValue(String value) {
         this.value = value;
     }
+
+       @Override
+       public int hashCode() {
+               final int prime = 31;
+               int result = 1;
+               result = prime * result + ((key == null) ? 0 : key.hashCode());
+               result = prime * result + ((module == null) ? 0 : 
module.hashCode());
+               result = prime * result + ((authenticationId == null) ? 0 : 
authenticationId.hashCode());
+               result = prime * result + ((value == null) ? 0 : 
value.hashCode());
+               return result;
+       }
+
+       @Override
+       public boolean equals(Object obj) {
+               if (this == obj)
+                       return true;
+               if (obj == null)
+                       return false;
+               if (getClass() != obj.getClass())
+                       return false;
+               final Preference other = (Preference) obj;
+               if (key == null) {
+                       if (other.key != null)
+                               return false;
+               } else if (!key.equals(other.key))
+                       return false;
+               if (module == null) {
+                       if (other.module != null)
+                               return false;
+               } else if (!module.equals(other.module))
+                       return false;
+               if (authenticationId == null) {
+                       if (other.authenticationId != null)
+                               return false;
+               } else if (!authenticationId.equals(other.authenticationId))
+                       return false;
+               if (value == null) {
+                       if (other.value != null)
+                               return false;
+               } else if (!value.equals(other.value))
+                       return false;
+               return true;
+       }
 }
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to