Index: openacs-4/packages/workflow/workflow.info
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/workflow/workflow.info,v
diff -u -r1.6 -r1.7
--- openacs-4/packages/workflow/workflow.info	28 Aug 2003 09:41:59 -0000	1.6
+++ openacs-4/packages/workflow/workflow.info	1 Sep 2003 13:43:50 -0000	1.7
@@ -8,7 +8,7 @@
     t
     workflow
 
-    
+    
     
         oracle
         postgresql
@@ -22,7 +22,7 @@
 <p>
 For more information, see: <a href="http://www.collaboraid.biz/developer/workflow-spec">the workflow specification</a>.
 
-        
+        
         
         
         
Index: openacs-4/packages/workflow/sql/oracle/workflow-tables-create.sql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/workflow/sql/oracle/workflow-tables-create.sql,v
diff -u -r1.3 -r1.4
--- openacs-4/packages/workflow/sql/oracle/workflow-tables-create.sql	28 Aug 2003 09:41:59 -0000	1.3
+++ openacs-4/packages/workflow/sql/oracle/workflow-tables-create.sql	1 Sep 2003 13:43:51 -0000	1.4
@@ -380,6 +380,66 @@
 );
 
 ---------------------------------
+-- Deputies
+---------------------------------
+
+-- When a user is away, for example on vacation, he
+-- can hand over his workflow roles to some other user - a deputy
+create table workflow_deputies (
+  user_id             integer
+		      constraint workflow_deputies_pk
+		      primary key
+		      constraint workflow_deputies_uid_fk
+		      references users(user_id),
+  deputy_user_id      integer
+		      constraint workflow_deputies_duid_fk
+		      references users(user_id),
+  start_date	      date
+		      constraint workflow_deputies_sdate_nn
+		      not null,
+  end_date	      date
+		      constraint workflow_deputies_edate_nn
+		      not null,
+  message	      varchar(4000)
+);
+
+-- role-to-user-map with deputies. Does not select users who
+-- have deputies, should we do that?
+create or replace view workflow_case_role_user_map as
+select distinct q.case_id,
+       q.role_id,
+       q.user_id,
+       q.on_behalf_of_user_id
+from (
+    select rpm.case_id,
+           rpm.role_id,
+           pmm.member_id as user_id,
+           pmm.member_id as on_behalf_of_user_id
+    from   workflow_case_role_party_map rpm, 
+           party_approved_member_map pmm,
+	   users u
+    where  rpm.party_id = pmm.party_id
+    and    pmm.member_id = u.user_id
+    and    not exists (select 1 
+                       from workflow_deputies 
+                       where user_id = pmm.member_id
+                       and sysdate between start_date and end_date)
+    union
+    select rpm.case_id,
+           rpm.role_id,
+           dep.deputy_user_id as user_id,
+           pmm.member_id as on_behalf_of_user_id
+    from   workflow_case_role_party_map rpm, 
+           party_approved_member_map pmm,
+	   users u,
+           workflow_deputies dep
+    where  rpm.party_id = pmm.party_id
+    and    pmm.member_id = u.user_id
+    and    dep.user_id = pmm.member_id
+    and    sysdate between dep.start_date and dep.end_date
+) q;
+
+---------------------------------
 -- Case level, Finite State Machine Model
 ---------------------------------
 
Index: openacs-4/packages/workflow/sql/oracle/workflow-tables-drop.sql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/workflow/sql/oracle/workflow-tables-drop.sql,v
diff -u -r1.2 -r1.3
--- openacs-4/packages/workflow/sql/oracle/workflow-tables-drop.sql	5 Mar 2003 17:17:18 -0000	1.2
+++ openacs-4/packages/workflow/sql/oracle/workflow-tables-drop.sql	1 Sep 2003 13:43:51 -0000	1.3
@@ -35,6 +35,8 @@
 -- Drop all tables
 drop table workflow_case_fsm;
 drop table workflow_case_role_party_map;
+drop view workflow_case_role_user_map;
+drop table workflow_deputies;
 drop table workflow_case_log_data;
 drop table workflow_case_log;
 drop table workflow_cases;
Index: openacs-4/packages/workflow/sql/oracle/upgrade/upgrade-1.0d4-1.0d5.sql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/workflow/sql/oracle/upgrade/upgrade-1.0d4-1.0d5.sql,v
diff -u
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ openacs-4/packages/workflow/sql/oracle/upgrade/upgrade-1.0d4-1.0d5.sql	1 Sep 2003 13:43:51 -0000	1.1
@@ -0,0 +1,68 @@
+--
+-- Upgrade script
+--
+-- Adds deputy feature
+--
+-- Peter Marklund (peter@collaboraid.biz)
+--
+-- $Id: upgrade-1.0d4-1.0d5.sql,v 1.1 2003/09/01 13:43:51 peterm Exp $
+
+---------------------------------
+-- Deputies
+---------------------------------
+
+-- When a user is away, for example on vacation, he
+-- can hand over his workflow roles to some other user - a deputy
+create table workflow_deputies (
+  user_id             integer
+		      constraint workflow_deputies_pk
+		      primary key
+		      constraint workflow_deputies_uid_fk
+		      references users(user_id),
+  deputy_user_id      integer
+		      constraint workflow_deputies_duid_fk
+		      references users(user_id),
+  start_date	      date
+		      constraint workflow_deputies_sdate_nn
+		      not null,
+  end_date	      date
+		      constraint workflow_deputies_edate_nn
+		      not null,
+  message	      varchar(4000)
+);
+
+-- role-to-user-map with deputies. Does not select users who
+-- have deputies, should we do that?
+create or replace view workflow_case_role_user_map as
+select distinct q.case_id,
+       q.role_id,
+       q.user_id,
+       q.on_behalf_of_user_id
+from (
+    select rpm.case_id,
+           rpm.role_id,
+           pmm.member_id as user_id,
+           pmm.member_id as on_behalf_of_user_id
+    from   workflow_case_role_party_map rpm, 
+           party_approved_member_map pmm,
+	   users u
+    where  rpm.party_id = pmm.party_id
+    and    pmm.member_id = u.user_id
+    and    not exists (select 1 
+                       from workflow_deputies 
+                       where user_id = pmm.member_id
+                       and sysdate between start_date and end_date)
+    union
+    select rpm.case_id,
+           rpm.role_id,
+           dep.deputy_user_id as user_id,
+           pmm.member_id as on_behalf_of_user_id
+    from   workflow_case_role_party_map rpm, 
+           party_approved_member_map pmm,
+	   users u,
+           workflow_deputies dep
+    where  rpm.party_id = pmm.party_id
+    and    pmm.member_id = u.user_id
+    and    dep.user_id = pmm.member_id
+    and    sysdate between dep.start_date and dep.end_date
+) q;
Index: openacs-4/packages/workflow/sql/postgresql/workflow-tables-create.sql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/workflow/sql/postgresql/workflow-tables-create.sql,v
diff -u -r1.10 -r1.11
--- openacs-4/packages/workflow/sql/postgresql/workflow-tables-create.sql	28 Aug 2003 12:03:23 -0000	1.10
+++ openacs-4/packages/workflow/sql/postgresql/workflow-tables-create.sql	1 Sep 2003 13:43:51 -0000	1.11
@@ -405,6 +405,66 @@
 );
 
 ---------------------------------
+-- Deputies
+---------------------------------
+
+-- When a user is away, for example on vacation, he
+-- can hand over his workflow roles to some other user - a deputy
+create table workflow_deputies (
+  user_id             integer
+		      constraint workflow_deputies_pk
+		      primary key
+		      constraint workflow_deputies_uid_fk
+		      references users(user_id),
+  deputy_user_id      integer
+		      constraint workflow_deputies_duid_fk
+		      references users(user_id),
+  start_date	      date
+		      constraint workflow_deputies_sdate_nn
+		      not null,
+  end_date	      date
+		      constraint workflow_deputies_edate_nn
+		      not null,
+  message	      varchar(4000)
+);
+
+-- role-to-user-map with deputies. Does not select users who
+-- have deputies, should we do that?
+create view workflow_case_role_user_map as
+select distinct q.case_id,
+       q.role_id,
+       q.user_id,
+       q.on_behalf_of_user_id
+from (
+    select rpm.case_id,
+           rpm.role_id,
+           pmm.member_id as user_id,
+           pmm.member_id as on_behalf_of_user_id
+    from   workflow_case_role_party_map rpm, 
+           party_approved_member_map pmm,
+	   users u
+    where  rpm.party_id = pmm.party_id
+    and    pmm.member_id = u.user_id
+    and    not exists (select 1 
+                       from workflow_deputies 
+                       where user_id = pmm.member_id
+                       and now() between start_date and end_date)
+    union
+    select rpm.case_id,
+           rpm.role_id,
+           dep.deputy_user_id as user_id,
+           pmm.member_id as on_behalf_of_user_id
+    from   workflow_case_role_party_map rpm, 
+           party_approved_member_map pmm,
+	   users u,
+           workflow_deputies dep
+    where  rpm.party_id = pmm.party_id
+    and    pmm.member_id = u.user_id
+    and    dep.user_id = pmm.member_id
+    and    now() between dep.start_date and dep.end_date
+) q;
+
+---------------------------------
 -- Case level, Finite State Machine Model
 ---------------------------------
 
Index: openacs-4/packages/workflow/sql/postgresql/workflow-tables-drop.sql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/workflow/sql/postgresql/workflow-tables-drop.sql,v
diff -u -r1.6 -r1.7
--- openacs-4/packages/workflow/sql/postgresql/workflow-tables-drop.sql	5 Mar 2003 17:17:33 -0000	1.6
+++ openacs-4/packages/workflow/sql/postgresql/workflow-tables-drop.sql	1 Sep 2003 13:43:51 -0000	1.7
@@ -37,6 +37,8 @@
 
 -- Drop all tables
 drop table workflow_case_fsm;
+drop view workflow_case_role_user_map;
+drop table workflow_deputies;
 drop table workflow_case_role_party_map;
 drop table workflow_case_log_data;
 drop table workflow_case_log;
Index: openacs-4/packages/workflow/sql/postgresql/upgrade/upgrade-1.0d4-1.0d5.sql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/workflow/sql/postgresql/upgrade/upgrade-1.0d4-1.0d5.sql,v
diff -u
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ openacs-4/packages/workflow/sql/postgresql/upgrade/upgrade-1.0d4-1.0d5.sql	1 Sep 2003 13:43:51 -0000	1.1
@@ -0,0 +1,68 @@
+--
+-- Upgrade script
+--
+-- Adds deputy feature
+--
+-- Peter Marklund (peter@collaboraid.biz)
+--
+-- $Id: upgrade-1.0d4-1.0d5.sql,v 1.1 2003/09/01 13:43:51 peterm Exp $
+
+---------------------------------
+-- Deputies
+---------------------------------
+
+-- When a user is away, for example on vacation, he
+-- can hand over his workflow roles to some other user - a deputy
+create table workflow_deputies (
+  user_id             integer
+		      constraint workflow_deputies_pk
+		      primary key
+		      constraint workflow_deputies_uid_fk
+		      references users(user_id),
+  deputy_user_id      integer
+		      constraint workflow_deputies_duid_fk
+		      references users(user_id),
+  start_date	      date
+		      constraint workflow_deputies_sdate_nn
+		      not null,
+  end_date	      date
+		      constraint workflow_deputies_edate_nn
+		      not null,
+  message	      varchar(4000)
+);
+
+-- role-to-user-map with deputies. Does not select users who
+-- have deputies, should we do that?
+create view workflow_case_role_user_map as
+select distinct q.case_id,
+       q.role_id,
+       q.user_id,
+       q.on_behalf_of_user_id
+from (
+    select rpm.case_id,
+           rpm.role_id,
+           pmm.member_id as user_id,
+           pmm.member_id as on_behalf_of_user_id
+    from   workflow_case_role_party_map rpm, 
+           party_approved_member_map pmm,
+	   users u
+    where  rpm.party_id = pmm.party_id
+    and    pmm.member_id = u.user_id
+    and    not exists (select 1 
+                       from workflow_deputies 
+                       where user_id = pmm.member_id
+                       and now() between start_date and end_date)
+    union
+    select rpm.case_id,
+           rpm.role_id,
+           dep.deputy_user_id as user_id,
+           pmm.member_id as on_behalf_of_user_id
+    from   workflow_case_role_party_map rpm, 
+           party_approved_member_map pmm,
+	   users u,
+           workflow_deputies dep
+    where  rpm.party_id = pmm.party_id
+    and    pmm.member_id = u.user_id
+    and    dep.user_id = pmm.member_id
+    and    now() between dep.start_date and dep.end_date
+) q;
Index: openacs-4/packages/workflow/tcl/case-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/workflow/tcl/case-procs.tcl,v
diff -u -r1.9 -r1.10
--- openacs-4/packages/workflow/tcl/case-procs.tcl	28 Aug 2003 14:24:55 -0000	1.9
+++ openacs-4/packages/workflow/tcl/case-procs.tcl	1 Sep 2003 13:43:51 -0000	1.10
@@ -113,8 +113,10 @@
 } {
     Get information about a case
 
-    @param caes_id The case ID
-    @param array The name of an array in which information will be returned.
+    @param case_id     The case ID
+    @param array       The name of an array in which information will be returned.
+    @param action_id   If specified, will return the case information as if the given action had already been executed. 
+                       This is useful for presenting forms for actions that do not take place until the user hits OK.
 
     @author Lars Pind (lars@collaboraid.biz)
 } {
@@ -137,10 +139,13 @@
 } {
     Return a single element from the information about a case.
 
-    @param case_id The ID of the case
-    @param element The element you want
-    @return The element you asked for
+    @param case_id     The ID of the case
+    @param element     The element you want
+    @param action_id   If specified, will return the case information as if the given action had already been executed. 
+                       This is useful for presenting forms for actions that do not take place until the user hits OK.
 
+    @return            The element you asked for
+
     @author Lars Pind (lars@collaboraid.biz)
 } {
     get -case_id $case_id -action_id $action_id -array row
@@ -163,11 +168,13 @@
     {-case_id:required}
     -user_id
 } {
-    Get the roles which this user is assigned to
-of 
+    Get the roles which this user is assigned to. 
+    Takes deputies into account, so that if the user is a deputy for someone else, 
+    he or she will have the roles of the user for whom he/she is a deputy.
+
     @param case_id     The ID of the case.
-    @param user_id     The user_id of the user for which you want to know the roles.
-    @return A list of role_id's of the roles which the user is assigned to in this case.
+    @param user_id     The user_id of the user for which you want to know the roles. Defaults to ad_conn user_id.
+    @return            A list of role_id's of the roles which the user is assigned to in this case.
 
     @author Lars Pind (lars@collaboraid.biz)
 } {
@@ -271,12 +278,13 @@
         }
     }
 
-    workflow::case::role::flush_cache $case_id
+    workflow::case::role::flush_cache -case_id $case_id
 }
 
 ad_proc -private workflow::case::get_activity_html { 
     {-case_id:required}
     {-action_id ""}
+    {-max_n_actions ""}
 } {
     Get the activity log for a case as an HTML chunk.
     If action_id is non-empty, it means that we're in 
@@ -285,6 +293,7 @@
 
     @param case_id The case for which you want the activity log.
     @param action_id optional action which is currently being executed.
+    @param max_n_actions Limit history to the max_n_actions number of most recent actions
     @return Activity log as HTML
 
     @author Lars Pind (lars@collaboraid.biz)
@@ -299,7 +308,14 @@
     # Compile and evaluate the template
     set code [template::adp_compile -string $template]
 
-    foreach entry_arraylist [get_activity_log_info -case_id $case_id] {
+    set activity_entry_list [get_activity_log_info_not_cached -case_id $case_id]
+    set start_index 0
+    if { ![empty_string_p $max_n_actions] && [llength $activity_entry_list] > $max_n_actions} { 
+	# Only return the last max_n_actions actions
+	set start_index [expr [llength $activity_entry_list] - $max_n_actions]
+    } 
+
+    foreach entry_arraylist [lrange $activity_entry_list $start_index end] {
         foreach { var value } $entry_arraylist {
             set $var $value
         }
@@ -397,6 +413,7 @@
 
     
     set rowcount [template::multirow -local size entries]
+
     set counter 1
 
     set last_entry_id {}
@@ -604,9 +621,42 @@
     db_string select_log_data {} -default {}
 }
 
+ad_proc -private workflow::case::cache_timeout {} {
+    Number of seconds before we timeout the case level workflow cache.
 
+    @author Peter Marklund
+} {
+    # 60 * 60 seconds is 1 hour
+    return 3600
+}
 
+ad_proc -private workflow::case::flush_cache { 
+    {-case_id ""}
+} {
+    Flush all cached data for a given case or for all
+    cases if none is specified.
 
+    @param case_id The id of the workflow case to flush. If not provided the
+                   cache will be flushed for all workflow cases.
+
+    @author Peter Marklund
+} {
+    foreach proc_name {
+        workflow::case::fsm::get_info_not_cached
+        workflow::case::get_user_roles_not_cached
+        workflow::case::get_enabled_actions_not_cached
+    } {
+        util_memoize_flush_regexp "^$proc_name [ad_decode $case_id "" {\.*} $case_id]"
+    }
+
+    util_memoize_flush_regexp [list workflow::case::get_activity_log_info_not_cached -case_id $case_id]
+
+    # Flush role info (assignees etc)
+    workflow::case::role::flush_cache -case_id $case_id
+}
+
+
+
 #####
 #
 # workflow::case::role namespace
@@ -832,7 +882,7 @@
                 append display_value " ($cur_assignee(email))"
             }
 
-            uplevel [list element set_properties bug $element -display_value $display_value]
+            uplevel [list element set_properties $form_name $element -display_value $display_value]
         }
     }
 }
@@ -868,11 +918,13 @@
     return $result    
 }
 
-ad_proc -private workflow::case::role::flush_cache { case_id } {
-    Flush all role related info for a certain case.
-
+ad_proc -private workflow::case::role::flush_cache { 
+    {-case_id ""}
+ } {
+    Flush all role related info for a certain case or for all
+    cases if none is specified.
 } {
-    util_memoize_flush_regexp [list workflow::case::role::get_assignees_not_cached $case_id]    
+    util_memoize_flush_regexp "^workflow::case::role::get_assignees_not_cached [ad_decode $case_id "" {\.*} $case_id]"
 }
 
 ad_proc -public workflow::case::role::assignee_insert {
@@ -907,7 +959,7 @@
         }
     }
 
-    workflow::case::role::flush_cache $case_id
+    workflow::case::role::flush_cache -case_id $case_id
 }
 
 ad_proc -public workflow::case::role::assign {
@@ -962,20 +1014,21 @@
 
     @author Lars Pind (lars@collaboraid.biz)
 } {
-    return [db_string select_current_state {}]
+    #return [db_string select_current_state {}]
+    return [workflow::case::fsm::get_element -case_id $case_id -element state_id]
 }
 
 ad_proc -public workflow::case::fsm::get {
     {-case_id:required}
     {-array:required}
     {-action_id {}}
 } {
-    Get information about an FSM case
+    Get information about an FSM case set as values in your array.
 
-    @param caes_id The case ID
-    @param array The name of an array in which information will be returned.
-    @param action_id If you supply an action here, you'll get 
-    the information as it'll look after executing the given action.
+    @param case_id     The ID of the case
+    @param array       The name of an array in which information will be returned.
+    @param action_id   If specified, will return the case information as if the given action had already been executed. 
+                       This is useful for presenting forms for actions that do not take place until the user hits OK.
 
     @author Lars Pind (lars@collaboraid.biz)
 } {
@@ -993,13 +1046,24 @@
     }
 }
 
-ad_proc -private workflow::case::cache_timeout {} {
-    Number of seconds before we timeout the case level workflow cache.
+ad_proc -public workflow::case::fsm::get_element {
+    {-case_id:required}
+    {-element:required}
+    {-action_id {}}
+} {
+    Return a single element from the information about a case.
 
-    @author Peter Marklund
+    @param case_id     The ID of the case
+    @param element     The element you want
+    @param action_id   If specified, will return the case information as if the given action had already been executed. 
+                       This is useful for presenting forms for actions that do not take place until the user hits OK.
+
+    @return            The element you asked for
+
+    @author Lars Pind (lars@collaboraid.biz)
 } {
-    # 60 * 60 seconds is 1 hour
-    return 3600
+    get -case_id $case_id -action_id $action_id -array row
+    return $row($element)
 }
 
 ad_proc -private workflow::case::fsm::get_info_not_cached { case_id } {
@@ -1013,25 +1077,8 @@
     return [array get row]
 }
 
-ad_proc -private workflow::case::flush_cache { case_id } {
-    Flush all cached data for the given case.
 
-    @author Peter Marklund
-} {
-    # Flush scalar attributes (for fsm::get proc)
-    util_memoize_flush [list workflow::case::fsm::get_info_not_cached $case_id]
 
-    # Flush role info (assignees etc)
-    workflow::case::role::flush_cache $case_id
-
-    # Flush roles
-    util_memoize_flush_regexp [list workflow::case::get_user_roles_not_cached $case_id]
-
-    # Flush enabled actions
-    util_memoize_flush [list workflow::case::get_enabled_actions_not_cached $case_id]
-}
-
-
 #####
 #
 # workflow::case::action 
@@ -1222,7 +1269,7 @@
         
     }
 
-    workflow::case::flush_cache $case_id
+    workflow::case::flush_cache -case_id $case_id
 
     # Notifications
     notify \
@@ -1372,9 +1419,11 @@
     set the_subject "[ad_decode $object_notification_tag "" "" "\[$object_notification_tag\] "]$object_one_line: $latest_action(action_pretty_past_tense) [ad_decode $latest_action(log_title) "" "" "$latest_action(log_title) "]by $latest_action(user_first_names) $latest_action(user_last_name)"
 
     # List of user_id's for people who are in the assigned_role to any enabled actions
+    # This takes deputies into account
     set assignee_list [db_list enabled_action_assignees {}]
 
     # List of users who play some role in this case
+    # This takes deputies into account
     set case_player_list [db_list case_players {}]
 
     # Get pretty_name and pretty_plural for the case's object type
Index: openacs-4/packages/workflow/tcl/case-procs.xql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/workflow/tcl/case-procs.xql,v
diff -u -r1.7 -r1.8
--- openacs-4/packages/workflow/tcl/case-procs.xql	28 Aug 2003 09:41:59 -0000	1.7
+++ openacs-4/packages/workflow/tcl/case-procs.xql	1 Sep 2003 13:43:51 -0000	1.8
@@ -44,12 +44,10 @@
 
   
     
-      select distinct rpm.role_id
-      from   workflow_case_role_party_map rpm, 
-             party_approved_member_map pmm
-      where  rpm.case_id = :case_id
-      and    rpm.party_id = pmm.party_id
-      and    pmm.member_id = :user_id
+      select distinct role_id
+      from   workflow_case_role_user_map
+      where  case_id = :case_id
+      and    user_id = :user_id
     
   
 
@@ -78,7 +76,7 @@
   
     
       select count(*)
-      from   workflow_case_role_party_map
+      from   workflow_case_role_user_map
       where  case_id = :case_id
       and    role_id = :role_id
     
@@ -216,12 +214,10 @@
 
   
     
-        select distinct u.user_id
+        select distinct rum.user_id
         from   workflow_cases c,
                workflow_actions a,
-               workflow_case_role_party_map rpm, 
-               party_approved_member_map pmm,
-               users u
+               workflow_case_role_user_map rum 
         where  c.case_id = :case_id
         and    a.workflow_id = c.workflow_id
         and    (a.always_enabled_p = 't' or 
@@ -232,22 +228,16 @@
                         and    c_fsm.case_id = c.case_id
                         and    waeis.state_id = c_fsm.current_state)
                )
-        and    rpm.case_id = c.case_id
-        and    rpm.role_id = a.assigned_role
-        and    pmm.party_id = rpm.party_id
-        and    u.user_id = pmm.member_id
+        and    rum.case_id = c.case_id
+        and    rum.role_id = a.assigned_role
     
   
 
   
     
-        select distinct u.user_id
-        from   workflow_case_role_party_map rpm, 
-               party_approved_member_map pmm,
-               users u
-        where  rpm.case_id = :case_id
-        and    rpm.party_id = pmm.party_id
-        and    pmm.member_id = u.user_id
+        select distinct user_id
+        from   workflow_case_role_user_map
+        where  case_id = :case_id
     
   
 
Index: openacs-4/packages/workflow/tcl/workflow-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/workflow/tcl/workflow-procs.tcl,v
diff -u -r1.8 -r1.9
--- openacs-4/packages/workflow/tcl/workflow-procs.tcl	28 Aug 2003 09:41:59 -0000	1.8
+++ openacs-4/packages/workflow/tcl/workflow-procs.tcl	1 Sep 2003 13:43:51 -0000	1.9
@@ -235,6 +235,12 @@
     workflow::role::flush_cache -workflow_id $workflow_id
     workflow::action::flush_cache -workflow_id $workflow_id
     workflow::state::flush_cache -workflow_id $workflow_id
+
+    # Flush all workflow cases from the cache. We are flushing more than needed here
+    # but this approach seems easier and faster than looping over a potentially big number
+    # of cases mapped to the workflow in the database, only a few of which may actually be 
+    # cached and need flushing
+    workflow::case::flush_cache
 }
 
 ad_proc -private workflow::cache_timeout {} {
Index: openacs-4/packages/workflow/tcl/test/workflow-test-init.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/workflow/tcl/test/Attic/workflow-test-init.tcl,v
diff -u -r1.1 -r1.2
--- openacs-4/packages/workflow/tcl/test/workflow-test-init.tcl	4 Apr 2003 09:48:41 -0000	1.1
+++ openacs-4/packages/workflow/tcl/test/workflow-test-init.tcl	1 Sep 2003 13:43:51 -0000	1.2
@@ -93,3 +93,4 @@
     
     workflow::test::run_with_teardown $test_chunk $teardown_chunk
 }
+
Index: openacs-4/packages/workflow/tcl/test/workflow-test-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/workflow/tcl/test/workflow-test-procs.tcl,v
diff -u -r1.8 -r1.9
--- openacs-4/packages/workflow/tcl/test/workflow-test-procs.tcl	28 Aug 2003 09:41:59 -0000	1.8
+++ openacs-4/packages/workflow/tcl/test/workflow-test-procs.tcl	1 Sep 2003 13:43:51 -0000	1.9
@@ -238,8 +238,6 @@
     set len1 [llength $list1]
     set len2 [llength $list2]
 
-    ns_log Notice "LARS2: lists_equal_p, len1=$len1, len2=$len2\nList1=$list1\nList2=$list2"
-    
     if { $len1 != $len2 } {
         return 0
     }
@@ -274,8 +272,6 @@
         # List, treat as normal list
         
         foreach elm1 $list1 elm2 $list2 {
-            ns_log Notice "LARS2: foreach, len1=[llength $elm1], len2=[llength $elm2]\nElm1=$elm1\nElm2=$elm2"
-            
             if { ![array_lists_equal_p $elm1 $elm2] } {
                 return 0
             }
@@ -464,9 +460,6 @@
 
         set generated_spec [workflow::fsm::generate_spec -workflow_id $workflow_id]
         
-        ns_log Notice "LARS: Generated spec 1: $generated_spec"
-        ns_log Notice "LARS: Hard-coded spec 1: [workflow_get_array_style_spec]"
-
         aa_true "Checking that generated spec 1 is identical to the spec that we created from (except for ordering)" \
                 [array_lists_equal_p $generated_spec [workflow_get_array_style_spec]]
         
@@ -549,6 +542,82 @@
                 -expect_enabled_actions $expect_enabled_actions \
                 -expect_user_actions $expect_enabled_actions \
                 -expect_user_roles {}
+
+
+        #####
+        #
+        # Intermezzo: Check cache and flushing
+        #
+        #####
+
+        # -1. Basic sanity check
+        aa_equals "Stat is resolved" [workflow::case::get_element -case_id $case_id -element state_short_name] "resolved"
+
+        # 0. Desired output
+        global desired_output 
+        set desired_output [workflow::case::fsm::get_info_not_cached $case_id]
+
+        ns_log Notice "LARS: desired_output = '$desired_output' ([llength $desired_output])"
+
+        # 1. Make sure the cache is populated
+        set dummy [workflow::case::get_element -case_id $case_id -element state_short_name]
+
+        with_catch errmsg {
+
+            # 2. Stub the cache proc
+            aa_stub workflow::case::fsm::get_info_not_cached {
+                # Note that we got called
+                global i_got_called_p desired_output
+                set i_got_called_p 1
+                
+                return $desired_output
+            }
+            global i_got_called_p
+        
+            # 3. Check that it doesn't call stubbed proc
+            set i_got_called_p 0
+            set dummy [workflow::case::get_element -case_id $case_id -element state_short_name]
+            aa_false "Check that the value is in the cache (1st time)" $i_got_called_p
+            
+            # 4. Flush
+            workflow::case::flush_cache -case_id $case_id
+            
+            # 5. Check that it DOES call stubbed proc
+            set i_got_called_p 0
+            set dummy [workflow::case::get_element -case_id $case_id -element state_short_name]
+            aa_true "Check that the value is NOT in the cache (1st time)" $i_got_called_p
+            
+            # 6. Manually populate the cache
+            util_memoize_seed [list workflow::case::fsm::get_info_not_cached $case_id] $desired_output [workflow::case::cache_timeout]
+            
+            # 7. Check that it doesn't call stubbed proc
+            set i_got_called_p 0
+            set dummy [workflow::case::get_element -case_id $case_id -element state_short_name]
+            aa_false "Check that the value is in the cache (2nd time)" $i_got_called_p
+            
+            # 8. Flush
+            workflow::case::flush_cache
+            
+            # 9. Check that it DOES call stubbed proc
+            set i_got_called_p 0
+            set dummy [workflow::case::get_element -case_id $case_id -element state_short_name]
+            aa_true "Check that the value is NOT in the cache (2nd time)" $i_got_called_p
+        } {
+            aa_unstub workflow::case::fsm::get_info_not_cached
+            
+            global errorInfo
+            error $errmsg $errorInfo
+        }
+        
+        # 10. Unstub
+        aa_unstub workflow::case::fsm::get_info_not_cached
+
+        
+        #####
+        #
+        # Continue with case
+        #
+        #####
                                   
         # Close the bug
         workflow::case::action::execute \