You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ad...@apache.org on 2007/12/21 20:02:30 UTC

svn commit: r606269 - in /ofbiz/trunk/framework/common: config/ data/ entitydef/ servicedef/ src/org/ofbiz/common/preferences/ webcommon/WEB-INF/

Author: adrianc
Date: Fri Dec 21 11:02:28 2007
New Revision: 606269

URL: http://svn.apache.org/viewvc?rev=606269&view=rev
Log:
User preferences feature - https://issues.apache.org/jira/browse/OFBIZ-495. This is the Java version - I'll commit the simple method version as soon as one is available.

Added:
    ofbiz/trunk/framework/common/config/PrefErrorUiLabels.properties
    ofbiz/trunk/framework/common/src/org/ofbiz/common/preferences/
    ofbiz/trunk/framework/common/src/org/ofbiz/common/preferences/PreferenceServices.java
    ofbiz/trunk/framework/common/src/org/ofbiz/common/preferences/PreferenceWorker.java
Modified:
    ofbiz/trunk/framework/common/data/CommonSecurityData.xml
    ofbiz/trunk/framework/common/data/CommonTypeData.xml
    ofbiz/trunk/framework/common/entitydef/entitygroup.xml
    ofbiz/trunk/framework/common/entitydef/entitymodel.xml
    ofbiz/trunk/framework/common/servicedef/services.xml
    ofbiz/trunk/framework/common/webcommon/WEB-INF/common-controller.xml

Added: ofbiz/trunk/framework/common/config/PrefErrorUiLabels.properties
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/config/PrefErrorUiLabels.properties?rev=606269&view=auto
==============================================================================
--- ofbiz/trunk/framework/common/config/PrefErrorUiLabels.properties (added)
+++ ofbiz/trunk/framework/common/config/PrefErrorUiLabels.properties Fri Dec 21 11:02:28 2007
@@ -0,0 +1,24 @@
+###############################################################################
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+# 
+# http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+###############################################################################
+copyPreference.invalidArgument=Could not copy user preferences: Invalid argument(s)
+copyPreference.writeFailure=Could not copy user preferences (read/write failure): {0}
+getPreference.invalidArgument=Could not get user preferences: Invalid argument(s)
+getPreference.readFailure=Could not get user preferences (read failure): {0}
+setPreference.invalidArgument=Could not set user preferences: Invalid argument(s)
+setPreference.writeFailure=Could not set user preferences (write failure): {0}

Modified: ofbiz/trunk/framework/common/data/CommonSecurityData.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/data/CommonSecurityData.xml?rev=606269&r1=606268&r2=606269&view=diff
==============================================================================
--- ofbiz/trunk/framework/common/data/CommonSecurityData.xml (original)
+++ ofbiz/trunk/framework/common/data/CommonSecurityData.xml Fri Dec 21 11:02:28 2007
@@ -31,4 +31,8 @@
     <SecurityGroupPermission groupId="FLEXADMIN" permissionId="COMMON_VIEW"/>
 
     <SecurityGroupPermission groupId="VIEWADMIN" permissionId="COMMON_VIEW"/>
+
+    <!-- User preferences security -->
+    <SecurityPermission description="User preferences admin" permissionId="USERPREF_ADMIN"/>
+    <SecurityGroupPermission groupId="FULLADMIN" permissionId="USERPREF_ADMIN"/>
 </entity-engine-xml>

Modified: ofbiz/trunk/framework/common/data/CommonTypeData.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/data/CommonTypeData.xml?rev=606269&r1=606268&r2=606269&view=diff
==============================================================================
--- ofbiz/trunk/framework/common/data/CommonTypeData.xml (original)
+++ ofbiz/trunk/framework/common/data/CommonTypeData.xml Fri Dec 21 11:02:28 2007
@@ -87,4 +87,7 @@
     <UomType description="Energy" hasTable="N" parentTypeId="" uomTypeId="ENERGY_MEASURE"/>
     <UomType description="Temperature" hasTable="N" parentTypeId="" uomTypeId="TEMP_MEASURE"/>
     <UomType description="Other" hasTable="N" parentTypeId="" uomTypeId="OTHER_MEASURE"/>
+
+    <!-- User preferences seed data -->
+    <UserPrefGroupType userPrefGroupId="GLOBAL_PREFERENCES" description="Global preferences"/>
 </entity-engine-xml>

Modified: ofbiz/trunk/framework/common/entitydef/entitygroup.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/entitydef/entitygroup.xml?rev=606269&r1=606268&r2=606269&view=diff
==============================================================================
--- ofbiz/trunk/framework/common/entitydef/entitygroup.xml (original)
+++ ofbiz/trunk/framework/common/entitydef/entitygroup.xml Fri Dec 21 11:02:28 2007
@@ -100,5 +100,15 @@
     <entity-group group="org.ofbiz" entity="UomConversion" />
     <entity-group group="org.ofbiz" entity="UomConversionDated" />
     <entity-group group="org.ofbiz" entity="UomType" />
+
+  <!-- ========================================================= -->
+  <!-- org.ofbiz.common.preferences -->
+  <!-- ========================================================= -->
+
+    <entity-group group="org.ofbiz" entity="UserPreference" />
+    <entity-group group="org.ofbiz" entity="UserPrefGroupType" />
+
 </entitygroup>
+
+
 

Modified: ofbiz/trunk/framework/common/entitydef/entitymodel.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/entitydef/entitymodel.xml?rev=606269&r1=606268&r2=606269&view=diff
==============================================================================
--- ofbiz/trunk/framework/common/entitydef/entitymodel.xml (original)
+++ ofbiz/trunk/framework/common/entitydef/entitymodel.xml Fri Dec 21 11:02:28 2007
@@ -483,4 +483,35 @@
         <key-map field-name="parentTypeId" rel-field-name="uomTypeId"/>
       </relation>
     </entity>
+
+  <!-- ========================================================= -->
+  <!-- org.ofbiz.common.user -->
+  <!-- ========================================================= -->
+
+    <entity entity-name="UserPreference"
+            package-name="org.ofbiz.common.user"
+            title="Application preferences for a given userLogin.">
+      <description>The UserPreference entity contains one entry per preference per
+          userLogin. User preferences are stored as key/value pairs (userPrefTypeId/userPrefValue).
+          All values are stored as strings. Value strings can be converted to
+          other data types by specifying a java data type in the userPrefDataType field.
+      </description>
+      <field name="userLoginId" type="id-vlong-ne"></field>
+      <field name="userPrefTypeId" type="id-ne"><description>A unique identifier for this preference</description></field>
+      <field name="userPrefGroupId" type="id-long"><description>Used to assemble groups of preferences</description></field>
+      <field name="userPrefValue" type="value"><description>Contains the value of this preference</description></field>
+      <field name="userPrefDataType" type="id-long"><description>The java data type of this preference (empty = java.lang.String)</description></field>
+    </entity>
+
+    <entity entity-name="UserPrefGroupType"
+            package-name="org.ofbiz.common.user"
+            title="Defines a group of User Preferences">
+      <description>The UserPrefGroupType entity contains one entry per preference
+          group type.
+      </description>
+      <field name="userPrefGroupId" type="id-long-ne"></field>
+      <field name="description" type="description"></field>
+      <prim-key field="userPrefGroupId"/>
+    </entity>
+
 </entitymodel>

Modified: ofbiz/trunk/framework/common/servicedef/services.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/servicedef/services.xml?rev=606269&r1=606268&r2=606269&view=diff
==============================================================================
--- ofbiz/trunk/framework/common/servicedef/services.xml (original)
+++ ofbiz/trunk/framework/common/servicedef/services.xml Fri Dec 21 11:02:28 2007
@@ -416,4 +416,66 @@
         <attribute name="entryLink" type="String" mode="IN" optional="false"/>        
         <attribute name="wireFeed" type="com.sun.syndication.feed.WireFeed" mode="OUT" optional="false"/>
     </service>
+
+    <!-- User preferences services -->    
+    <service name="copyUserPrefGroup" engine="java"
+            location="org.ofbiz.common.preferences.PreferenceServices" invoke="copyUserPreferenceGroup" auth="true" use-transaction="true">
+        <description>Copies the preferences from one userLoginId and preference group to another.
+            If no userLoginId is specified, preferences are copied to current user's preferences.</description>
+        <permission-service service-name="preferenceCopyPermission"/>
+        <attribute name="fromUserLoginId" type="String" mode="IN" optional="false"/>
+        <attribute name="userPrefGroupId" type="String" mode="IN" optional="false"/>
+        <attribute name="userLoginId" type="String" mode="IN" optional="true"/>
+    </service>
+
+    <service name="getUserPreference" engine="java"
+            location="org.ofbiz.common.preferences.PreferenceServices" invoke="getUserPreference">
+        <description>Gets a single user preference.</description>
+        <permission-service service-name="preferenceGetSetPermission" main-action="VIEW"/>
+        <attribute name="userPrefTypeId" type="String" mode="IN" optional="false"/>
+        <attribute name="userLoginId" type="String" mode="IN" optional="true"/>
+        <attribute name="userPrefGroupId" type="String" mode="IN" optional="true"/>
+        <attribute name="userPrefMap" type="Map" mode="OUT" optional="true"/>
+    </service>
+
+    <service name="getUserPreferenceGroup" engine="java"
+            location="org.ofbiz.common.preferences.PreferenceServices" invoke="getUserPreferenceGroup">
+        <description>Gets a group of user preferences.</description>
+        <permission-service service-name="preferenceGetSetPermission" main-action="VIEW"/>
+        <attribute name="userPrefGroupId" type="String" mode="IN" optional="false"/>
+        <attribute name="userLoginId" type="String" mode="IN" optional="true"/>
+        <attribute name="userPrefMap" type="Map" mode="OUT" optional="true"/>
+    </service>
+
+    <service name="setUserPreference" engine="java"
+            location="org.ofbiz.common.preferences.PreferenceServices" invoke="setUserPreference" auth="true">
+        <description>Sets a single user preference.</description>
+        <permission-service service-name="preferenceGetSetPermission" main-action="CREATE"/>
+        <attribute name="userPrefTypeId" type="String" mode="IN" optional="false"/>
+        <attribute name="userPrefValue" type="String" mode="IN" optional="false"/>
+        <attribute name="userPrefGroupId" type="String" mode="IN" optional="true"/>
+        <attribute name="userLoginId" type="String" mode="IN" optional="true"/>
+    </service>
+
+    <service name="setUserPreferenceGroup" engine="java"
+            location="org.ofbiz.common.preferences.PreferenceServices" invoke="setUserPreferenceGroup" auth="true">
+        <description>Sets a group of user preferences.</description>
+        <permission-service service-name="preferenceGetSetPermission" main-action="CREATE"/>
+        <attribute name="userPrefMap" type="Map" mode="IN" optional="false"/>
+        <attribute name="userPrefGroupId" type="String" mode="IN" optional="false"/>
+        <attribute name="userLoginId" type="String" mode="IN" optional="true"/>
+    </service>
+
+    <service name="preferenceGetSetPermission" engine="java"
+            location="org.ofbiz.common.preferences.PreferenceWorker" invoke="checkPermission">
+        <description>User preference get/set permission checking.</description>
+        <implements service="permissionInterface"/>
+        <attribute name="userLoginId" type="String" mode="IN" optional="true"/>
+    </service>
+
+    <service name="preferenceCopyPermission" engine="java"
+            location="org.ofbiz.common.preferences.PreferenceWorker" invoke="checkCopyPermission">
+        <description>User preference copy permission checking.</description>
+        <implements service="permissionInterface"/>
+    </service>
 </services>

Added: ofbiz/trunk/framework/common/src/org/ofbiz/common/preferences/PreferenceServices.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/src/org/ofbiz/common/preferences/PreferenceServices.java?rev=606269&view=auto
==============================================================================
--- ofbiz/trunk/framework/common/src/org/ofbiz/common/preferences/PreferenceServices.java (added)
+++ ofbiz/trunk/framework/common/src/org/ofbiz/common/preferences/PreferenceServices.java Fri Dec 21 11:02:28 2007
@@ -0,0 +1,245 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *******************************************************************************/
+package org.ofbiz.common.preferences;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import org.ofbiz.base.util.Debug;
+import org.ofbiz.base.util.GeneralException;
+import org.ofbiz.base.util.ObjectType;
+import org.ofbiz.base.util.UtilMisc;
+import org.ofbiz.base.util.UtilProperties;
+import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.entity.GenericDelegator;
+import org.ofbiz.entity.GenericEntityException;
+import org.ofbiz.entity.GenericValue;
+import org.ofbiz.entity.util.EntityUtil;
+import org.ofbiz.service.DispatchContext;
+import org.ofbiz.service.ServiceUtil;
+
+/**
+ * User preference services.<p>User preferences are stored as key-value pairs.
+ * <p>User preferences can be grouped - so that multiple preference pairs can be
+ * handled at once. Preference groups also allow a single userPrefTypeId to be
+ * used more than once - with each occurence having a unique userPrefGroupId.</p>
+ * <p>User preference values are stored as Strings, so the easiest and most
+ * efficient way to handle user preference values is to keep them as strings.
+ * This class handles any data conversion needed.</p>
+ */
+public class PreferenceServices {
+    public static final String module = PreferenceServices.class.getName();
+
+    public static final String resource = "PrefErrorUiLabels";
+
+    /**
+     * Retrieves a single user preference from persistent storage. Call with
+     * userPrefTypeId and optional userLoginId. If userLoginId isn't
+     * specified, then the currently logged-in user's userLoginId will be
+     * used. The retrieved preference is contained in the <b>userPrefMap</b> element.
+     * @param ctx The DispatchContext that this service is operating in.
+     * @param context Map containing the input arguments.
+     * @return Map with the result of the service, the output parameters.
+     */
+    public static Map getUserPreference(DispatchContext ctx, Map context) {
+        GenericDelegator delegator = ctx.getDelegator();
+        Locale locale = (Locale) context.get("locale");
+
+        String userPrefTypeId = (String) context.get("userPrefTypeId");
+        if (UtilValidate.isEmpty(userPrefTypeId)) {
+            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "getPreference.invalidArgument", locale));
+        }
+        String userLoginId = PreferenceWorker.getUserLoginId(context, true);
+        Map fieldMap = UtilMisc.toMap("userLoginId", userLoginId, "userPrefTypeId", userPrefTypeId);
+        String userPrefGroupId = (String) context.get("userPrefGroupId");
+        if (UtilValidate.isNotEmpty(userPrefGroupId)) {
+            fieldMap.put("userPrefGroupId", userPrefGroupId);
+        }
+
+        Map userPrefMap = null;
+        try {
+            GenericValue preference = EntityUtil.getFirst(delegator.findByAnd("UserPreference", fieldMap));
+            if (preference != null) {
+                userPrefMap = PreferenceWorker.createUserPrefMap(preference);
+            }
+        } catch (GenericEntityException e) {
+            Debug.logWarning(e.getMessage(), module);
+            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "getPreference.readFailure", new Object[] { e.getMessage() }, locale));
+        } catch (GeneralException e) {
+            Debug.logWarning(e.getMessage(), module);
+            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "getPreference.readFailure", new Object[] { e.getMessage() }, locale));
+        }
+
+        Map<String, Object> result = ServiceUtil.returnSuccess();
+        result.put("userPrefMap", userPrefMap);
+        return result;
+    }
+
+    /**
+     * Retrieves a group of user preferences from persistent storage. Call with
+     * userPrefGroupId  and optional userLoginId. If userLoginId isn't
+     * specified, then the currently logged-in user's userLoginId will be
+     * used. The retrieved preferences group is contained in the <b>userPrefMap</b> element.
+     * @param ctx The DispatchContext that this service is operating in.
+     * @param context Map containing the input arguments.
+     * @return Map with the result of the service, the output parameters.
+     */
+    public static Map getUserPreferenceGroup(DispatchContext ctx, Map context) {
+        GenericDelegator delegator = ctx.getDelegator();
+        Locale locale = (Locale) context.get("locale");
+
+        String userPrefGroupId = (String) context.get("userPrefGroupId");
+        if (UtilValidate.isEmpty(userPrefGroupId)) {
+            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "getPreference.invalidArgument", locale));
+        }
+        String userLoginId = PreferenceWorker.getUserLoginId(context, true);
+
+        Map userPrefMap = null;
+        try {
+            Map fieldMap = UtilMisc.toMap("userLoginId", userLoginId, "userPrefGroupId", userPrefGroupId);
+            userPrefMap = PreferenceWorker.createUserPrefMap(delegator.findByAnd("UserPreference", fieldMap));
+        } catch (GenericEntityException e) {
+            Debug.logWarning(e.getMessage(), module);
+            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "getPreference.readFailure", new Object[] { e.getMessage() }, locale));
+        } catch (GeneralException e) {
+            Debug.logWarning(e.getMessage(), module);
+            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "getPreference.readFailure", new Object[] { e.getMessage() }, locale));
+        }
+
+        Map<String, Object> result = ServiceUtil.returnSuccess();
+        result.put("userPrefMap", userPrefMap);
+        return result;
+    }
+
+    /**
+     * Stores a single user preference in persistent storage. Call with
+     * userPrefTypeId, userPrefGroupId, userPrefValue and optional userLoginId.
+     * If userLoginId isn't specified, then the currently logged-in user's
+     * userLoginId will be used.
+     * @param ctx The DispatchContext that this service is operating in.
+     * @param context Map containing the input arguments.
+     * @return Map with the result of the service, the output parameters.
+     */
+    public static Map setUserPreference(DispatchContext ctx, Map context) {
+        GenericDelegator delegator = ctx.getDelegator();
+        Locale locale = (Locale) context.get("locale");
+
+        String userLoginId = PreferenceWorker.getUserLoginId(context, false);
+        String userPrefTypeId = (String) context.get("userPrefTypeId");
+        Object userPrefValue = (String) context.get("userPrefValue");
+        if (UtilValidate.isEmpty(userLoginId) || UtilValidate.isEmpty(userPrefTypeId) || userPrefValue == null) {
+            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "setPreference.invalidArgument", locale));
+        }
+        String userPrefGroupId = (String) context.get("userPrefGroupId");
+        String userPrefDataType = (String) context.get("userPrefDataType");
+
+        try {
+            if (UtilValidate.isNotEmpty(userPrefDataType)) {
+                userPrefValue = ObjectType.simpleTypeConvert(userPrefValue, userPrefDataType, null, null, false);
+            }
+            GenericValue rec = delegator.makeValidValue("UserPreference", PreferenceWorker.toFieldMap(userLoginId, userPrefTypeId, userPrefGroupId, userPrefValue));
+            delegator.createOrStore(rec);
+        } catch (GenericEntityException e) {
+            Debug.logWarning(e.getMessage(), module);
+            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "setPreference.writeFailure", new Object[] { e.getMessage() }, locale));
+        } catch (GeneralException e) {
+            Debug.logWarning(e.getMessage(), module);
+            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "setPreference.writeFailure", new Object[] { e.getMessage() }, locale));
+        }
+
+        return ServiceUtil.returnSuccess();
+    }
+
+    /**
+     * Stores a user preference group in persistent storage. Call with
+     * userPrefMap, userPrefGroupId and optional userLoginId. If userLoginId
+     * isn't specified, then the currently logged-in user's userLoginId will be
+     * used.
+     * @param ctx The DispatchContext that this service is operating in.
+     * @param context Map containing the input arguments.
+     * @return Map with the result of the service, the output parameters.
+     */
+    public static Map setUserPreferenceGroup(DispatchContext ctx, Map context) {
+        GenericDelegator delegator = ctx.getDelegator();
+        Locale locale = (Locale) context.get("locale");
+
+        String userLoginId = PreferenceWorker.getUserLoginId(context, false);
+        Map userPrefMap = (Map) context.get("userPrefMap");
+        String userPrefGroupId = (String) context.get("userPrefGroupId");
+        if (UtilValidate.isEmpty(userLoginId) || UtilValidate.isEmpty(userPrefGroupId) || userPrefMap == null) {
+            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "setPreference.invalidArgument", locale));
+        }
+
+        try {
+            for (Iterator i = userPrefMap.entrySet().iterator(); i.hasNext();) {
+                Map.Entry mapEntry = (Map.Entry) i.next();
+                GenericValue rec = delegator.makeValidValue("UserPreference", PreferenceWorker.toFieldMap(userLoginId, (String) mapEntry.getKey(), userPrefGroupId, (String) mapEntry.getValue()));
+                delegator.createOrStore(rec);
+            }
+        } catch (GenericEntityException e) {
+            Debug.logWarning(e.getMessage(), module);
+            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "setPreference.writeFailure", new Object[] { e.getMessage() }, locale));
+        } catch (GeneralException e) {
+            Debug.logWarning(e.getMessage(), module);
+            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "setPreference.writeFailure", new Object[] { e.getMessage() }, locale));
+        }
+
+        return ServiceUtil.returnSuccess();
+    }
+
+    /**
+     * Copies a user preference group. Call with
+     * fromUserLoginId, userPrefGroupId and optional userLoginId. If userLoginId
+     * isn't specified, then the currently logged-in user's userLoginId will be
+     * used.
+     * @param ctx The DispatchContext that this service is operating in.
+     * @param context Map containing the input arguments.
+     * @return Map with the result of the service, the output parameters.
+     */
+    public static Map copyUserPreferenceGroup(DispatchContext ctx, Map context) {
+        GenericDelegator delegator = ctx.getDelegator();
+        Locale locale = (Locale) context.get("locale");
+
+        String userLoginId = PreferenceWorker.getUserLoginId(context, false);
+        String fromUserLoginId = (String) context.get("fromUserLoginId");
+        String userPrefGroupId = (String) context.get("userPrefGroupId");
+        if (UtilValidate.isEmpty(userLoginId) || UtilValidate.isEmpty(userPrefGroupId) || UtilValidate.isEmpty(fromUserLoginId)) {
+            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "copyPreference.invalidArgument", locale));
+        }
+
+        try {
+            Map fieldMap = UtilMisc.toMap("userLoginId", fromUserLoginId, "userPrefGroupId", userPrefGroupId);
+            List<GenericValue> resultList = delegator.findByAnd("UserPreference", fieldMap);
+            if (resultList != null) {
+                for (Iterator i = resultList.iterator(); i.hasNext();) {
+                    GenericValue preference = (GenericValue) i.next();
+                    preference.set("userLoginId", userLoginId);
+                }
+                delegator.storeAll(resultList);
+            }
+        } catch (GenericEntityException e) {
+            Debug.logWarning(e.getMessage(), module);
+            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "copyPreference.writeFailure", new Object[] { e.getMessage() }, locale));
+        }
+
+        return ServiceUtil.returnSuccess();
+    }
+}

Added: ofbiz/trunk/framework/common/src/org/ofbiz/common/preferences/PreferenceWorker.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/src/org/ofbiz/common/preferences/PreferenceWorker.java?rev=606269&view=auto
==============================================================================
--- ofbiz/trunk/framework/common/src/org/ofbiz/common/preferences/PreferenceWorker.java (added)
+++ ofbiz/trunk/framework/common/src/org/ofbiz/common/preferences/PreferenceWorker.java Fri Dec 21 11:02:28 2007
@@ -0,0 +1,266 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *******************************************************************************/
+package org.ofbiz.common.preferences;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javolution.util.FastMap;
+
+import org.ofbiz.base.util.Debug;
+import org.ofbiz.base.util.GeneralException;
+import org.ofbiz.base.util.ObjectType;
+import org.ofbiz.base.util.UtilMisc;
+import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.entity.GenericValue;
+import org.ofbiz.security.Security;
+import org.ofbiz.service.DispatchContext;
+import org.ofbiz.service.ServiceUtil;
+
+/**
+ * User preference worker methods.
+ */
+public class PreferenceWorker {
+    public static final String module = PreferenceWorker.class.getName();
+    /**
+     * User preference administrator permission. Currently set to "USERPREF_ADMIN".
+     */
+    public static final String ADMIN_PERMISSION = "USERPREF_ADMIN";
+
+    /**
+     * Default userLoginId. Currently set to "DEFAULT_USER_PREFS". This userLoginId is used to
+     * retrieve preferences when the user is not logged in.
+     */
+    public static final String DEFAULT_UID = "DEFAULT_USER_PREFS";
+
+    /**
+     * Add a UserPreference GenericValue to a Map.
+     * @param rec GenericValue to convert
+     * @param userPrefMap user preference Map
+     * @throws GeneralException
+     * @return user preference map
+     */
+    public static Map<Object, Object> addPrefToMap(GenericValue rec, Map<Object, Object> userPrefMap) throws GeneralException {
+        String prefDataType = rec.getString("userPrefDataType");
+        if (UtilValidate.isEmpty(prefDataType)) {
+            // default to String
+            userPrefMap.put(rec.getString("userPrefTypeId"), rec.getString("userPrefValue"));
+        } else {
+            userPrefMap.put(rec.getString("userPrefTypeId"), ObjectType.simpleTypeConvert(rec.get("userPrefValue"), prefDataType, null, null, false));
+        }
+        return userPrefMap;
+    }
+
+    /**
+     * Checks preference copy permissions. Returns hasPermission=true if permission
+     * is granted.
+     * <p>Users can copy from any set of preferences to their own preferences.
+     * Copying to another user's preferences requires <a href="#ADMIN_PERMISSION">ADMIN_PERMISSION</a>
+     * permission.</p>
+     * @param ctx The DispatchContext that this service is operating in.
+     * @param context Map containing the input arguments.
+     * @return Map with the result of the service, the output parameters.
+     */
+    public static Map checkCopyPermission(DispatchContext ctx, Map context) {
+        boolean hasPermission = false;
+        GenericValue userLogin = (GenericValue) context.get("userLogin");
+        if (userLogin != null) {
+            String userLoginId = userLogin.getString("userLoginId");
+            String userLoginIdArg = (String) context.get("userLoginId");
+            if (userLoginId.equals(userLoginIdArg)) {
+                // users can copy to their own preferences
+                hasPermission = true;
+            } else {
+                Security security = ctx.getSecurity();
+                hasPermission = security.hasPermission(ADMIN_PERMISSION, userLogin);
+            }
+        }
+        Map<String, Object> result = ServiceUtil.returnSuccess();
+        result.put("hasPermission", new Boolean(hasPermission));
+        return result;
+    }
+
+    /**
+     * Checks preference get/set permissions. Returns hasPermission=true if 
+     * permission is granted.
+     * <p>This method is a simple wrapper around the isValidxxxId methods.</p>
+     * @param ctx The DispatchContext that this service is operating in.
+     * @param context Map containing the input arguments.
+     * @return Map with the result of the service, the output parameters.
+     */
+    public static Map checkPermission(DispatchContext ctx, Map context) {
+        boolean hasPermission = false;
+        String mainAction = (String) context.get("mainAction");
+        Debug.logInfo("mainAction = " + mainAction, module);
+        Debug.logInfo("userLoginId = " + context.get("userLoginId"), module);
+        if ("VIEW".equals(mainAction)) {
+            if (DEFAULT_UID.equals(context.get("userLoginId"))) {
+                hasPermission = true;
+            } else {
+                hasPermission = isValidGetId(ctx, context);
+            }
+        } else if ("CREATE~UPDATE~DELETE".contains(mainAction)) {
+            hasPermission = isValidSetId(ctx, context);
+        } else {
+            hasPermission = false;
+        }
+        Map<String, Object> result = ServiceUtil.returnSuccess();
+        result.put("hasPermission", new Boolean(hasPermission));
+        return result;
+    }
+
+    /**
+     * Convert a UserPreference GenericValue to a userPrefMap.
+     * @param rec GenericValue to convert
+     * @throws GeneralException
+     * @return user preference map
+     */
+    public static Map createUserPrefMap(GenericValue rec) throws GeneralException {
+        return addPrefToMap(rec, FastMap.newInstance());
+    }
+
+    /**
+     * Convert a List of UserPreference GenericValues to a userPrefMap.
+     * @param recList List of GenericValues to convert
+     * @throws GeneralException
+     * @return user preference map
+     */
+    public static Map<Object, Object> createUserPrefMap(List recList) throws GeneralException {
+        Map<Object, Object> userPrefMap = FastMap.newInstance();
+        if (recList != null) {
+            for (Iterator i = recList.iterator(); i.hasNext();) {
+                addPrefToMap((GenericValue) i.next(), userPrefMap);
+            }
+        }
+        return userPrefMap;
+    }
+
+    /**
+     * Gets a valid userLoginId parameter from the context Map.
+     * <p>This method searches the context Map for a userLoginId key. If none is
+     * found, the method attempts to get the current user's userLoginId. If the user
+     * isn't logged in, then the method returns <a href="#DEFAULT_UID">DEFAULT_UID</a>
+     * if returnDefault is set to true, otherwise the method returns a null or empty string.</p>
+     * 
+     * @param context Map containing the input arguments.
+     * @param returnDefault return <a href="#DEFAULT_UID">DEFAULT_UID</a> if no userLoginId is found.
+     * @return userLoginId String
+     */
+    public static String getUserLoginId(Map context, boolean returnDefault) {
+        String userLoginId = (String) context.get("userLoginId");
+        if (UtilValidate.isEmpty(userLoginId)) {
+            GenericValue userLogin = (GenericValue) context.get("userLogin");
+            if (userLogin != null) {
+                userLoginId = userLogin.getString("userLoginId");
+            }
+        }
+        if (UtilValidate.isEmpty(userLoginId) && returnDefault) {
+            userLoginId = DEFAULT_UID;
+        }
+        return userLoginId;
+    }
+
+    /**
+     * Checks for valid userLoginId to get preferences. Returns true if valid.
+     * <p>This method applies a small rule set to determine if user preferences
+     * can be retrieved by the current user:
+     * <ul>
+     * <li>If the user isn't logged in, then the method returns true</li>
+     * <li>If the user is logged in and the userLoginId specified in the context Map
+     * matches the user's userLoginId, then the method returns true.</li>
+     * <li>If the user is logged in and the userLoginId specified in the context Map
+     * is different than the user's userLoginId, then a security permission check is performed.
+     * If the user has the <a href="#ADMIN_PERMISSION">ADMIN_PERMISSION</a> permission then the
+     *  method returns true.</li>
+     * </ul></p>
+     * 
+     * @param ctx The DispatchContext that this service is operating in.
+     * @param context Map containing the input arguments.
+     * @return true if the userLoginId arguments are valid
+     */
+    public static boolean isValidGetId(DispatchContext ctx, Map context) {
+        String currentUserLoginId = null;
+        GenericValue userLogin = (GenericValue) context.get("userLogin");
+        if (userLogin == null) {
+            currentUserLoginId = DEFAULT_UID;
+        } else {
+            currentUserLoginId = userLogin.getString("userLoginId");
+        }
+        String userLoginIdArg = (String) context.get("userLoginId");
+        if (!currentUserLoginId.equals(DEFAULT_UID) && !currentUserLoginId.equals(userLoginIdArg)
+                && userLoginIdArg != null) {
+            Security security = ctx.getSecurity();
+            return security.hasPermission(ADMIN_PERMISSION, userLogin);
+        }
+        return true;
+    }
+
+    /**
+     * Checks for valid userLoginId to set preferences. Returns true if valid.
+     * <p>This method applies a small rule set to determine if user preferences
+     * can be set by the current user:
+     * <ul>
+     * <li>If the user isn't logged in, then the method returns false</li>
+     * <li>If the user is logged in and the userLoginId specified in the context Map
+     * matches the user's userLoginId, then the method returns true.</li>
+     * <li>If the user is logged in and the userLoginId specified in the context Map
+     * is different than the user's userLoginId, then a security permission check is performed.
+     * If the user has the <a href="#ADMIN_PERMISSION">ADMIN_PERMISSION</a>
+     * permission then the method returns true.</li>
+     * </ul></p>
+     * @param ctx The DispatchContext that this service is operating in.
+     * @param context Map containing the input arguments.
+     * @return true if arguments are valid
+     */
+    public static boolean isValidSetId(DispatchContext ctx, Map context) {
+        GenericValue userLogin = (GenericValue) context.get("userLogin");
+        if (userLogin == null) {
+            return false;
+        }
+        String currentUserLoginId = userLogin.getString("userLoginId");
+        String userLoginIdArg = (String) context.get("userLoginId");
+        if (!currentUserLoginId.equals(userLoginIdArg) && userLoginIdArg != null) {
+            Security security = ctx.getSecurity();
+            return security.hasPermission(ADMIN_PERMISSION, userLogin);
+        }
+        return true;
+    }
+
+    /**
+     * Creates a field Map to be used in GenericValue create or store methods.
+     * @param userLoginId The user's login ID
+     * @param userPrefTypeId The preference ID
+     * @param userPrefGroupId The preference group ID (may be null or empty)
+     * @param userPrefValue The preference value (will be converted to java.lang.String data type)
+     * @throws GeneralException
+     * @return field map
+     */
+    public static Map<String, Object> toFieldMap(String userLoginId, String userPrefTypeId, String userPrefGroupId, Object userPrefValue) throws GeneralException {
+        Map<String, Object> fieldMap = UtilMisc.toMap("userLoginId", userLoginId, "userPrefTypeId", userPrefTypeId, "userPrefValue", ObjectType.simpleTypeConvert(userPrefValue, "String", null, null, false));
+        if (UtilValidate.isNotEmpty(userPrefGroupId)) {
+            fieldMap.put("userPrefGroupId", userPrefGroupId);
+        }
+        String valueDataType = userPrefValue.getClass().getName();
+        if (!"java.lang.String".equals(valueDataType)) {
+            fieldMap.put("userPrefDataType", valueDataType);
+        }
+        return fieldMap;
+    }
+}

Modified: ofbiz/trunk/framework/common/webcommon/WEB-INF/common-controller.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/webcommon/WEB-INF/common-controller.xml?rev=606269&r1=606268&r2=606269&view=diff
==============================================================================
--- ofbiz/trunk/framework/common/webcommon/WEB-INF/common-controller.xml (original)
+++ ofbiz/trunk/framework/common/webcommon/WEB-INF/common-controller.xml Fri Dec 21 11:02:28 2007
@@ -92,6 +92,14 @@
         <response name="success" type="view" value="LookupTimezones" />
     </request-map>
 
+    <!-- User preference mapping -->
+    <request-map uri="setUserPreference">
+        <security https="true" auth="true"/>
+        <event type="java" path="org.ofbiz.common.preferences.PreferenceServices" invoke="setUserPreference"/>
+        <response name="success" type="view" value="main"/>
+        <response name="error" type="view" value="main"/>
+    </request-map>
+
     <!-- View Mappings -->
     <view-map name="error" page="/error/error.jsp"/>
     <view-map name="login" type="screen" page="component://common/widget/CommonScreens.xml#login"/>