Index: openacs-4/contrib/obsolete-packages/acs-workflow/sql/postgresql/workflow-case-package.sql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/contrib/obsolete-packages/acs-workflow/sql/postgresql/workflow-case-package.sql,v
diff -u -r1.4 -r1.5
--- openacs-4/contrib/obsolete-packages/acs-workflow/sql/postgresql/workflow-case-package.sql	13 Apr 2001 00:29:01 -0000	1.4
+++ openacs-4/contrib/obsolete-packages/acs-workflow/sql/postgresql/workflow-case-package.sql	13 Apr 2001 04:31:27 -0000	1.5
@@ -162,7 +162,7 @@
   v_workflow_case_table       varchar;
   v_context_key_for_query     varchar;
 begin
-        if new__context_key is null then
+        if new__context_key = '''' or new__context_key is null then
             v_context_key_for_query := ''default'';
         else
             v_context_key_for_query := new__context_key;
@@ -487,7 +487,7 @@
         and    tr.transition_key = t.transition_key;
 
         if v_trigger_type != ''message'' then
-            raise EXCEPTION ''-20000: Transition \\\''' || v_transition_name || ''\\\' is not message triggered'';
+            raise EXCEPTION ''-20000: Transition "'' || v_transition_name || ''" is not message triggered'';
         end if;
 
         /* Add an entry to the journal */
@@ -537,7 +537,7 @@
 
         if begin_task_action__action = ''start'' then
             if v_state != ''enabled'' then
-                raise EXCEPTION ''-20000: Task is in state \\\'%\\\', but it must be in state \\\'enabled\\\' to be started.'', v_state_name;
+                raise EXCEPTION ''-20000: Task is in state "%", but it must be in state "enabled" to be started.'', v_state;
             end if;
         
             select case when count(*) = 0 then 0 else 1 end into v_num_rows
@@ -561,7 +561,7 @@
                 end if;
             else if v_state = ''enabled'' then
                 if begin_task_action__action = ''cancel'' then
-                    raise EXCEPTION ''-20000: You can only cancel a task in state \\\'started\\\', but this task is in state \\\'%\\\''', v_state;
+                    raise EXCEPTION ''-20000: You can only cancel a task in state "started", but this task is in state "%"'', v_state;
                 end if;
 
                 /* Is this user assigned to this task? */
@@ -579,7 +579,7 @@
                 set    holding_user = begin_task_action__user_id 
                 where  task_id = begin_task_action__task_id;
             else
-                raise EXCEPTION ''-20000: Task is in state \\\'%\\\', but it must be in state \\\'enabled\\\' or \\\'started\\\' to be finished'', v_state_name;
+                raise EXCEPTION ''-20000: Task is in state "%", but it must be in state "enabled" or "started" to be finished'', v_state;
             end if; end if;
 
         else if begin_task_action__action = ''comment'' then
@@ -642,7 +642,7 @@
                                                end_task_action__journal_id
                     );
         else if end_task_action__action != ''comment'' then
-            raise EXCEPTION ''-20000: Unknown action \\\'%\\\''', end_task_action__action;
+            raise EXCEPTION ''-20000: Unknown action "%"'', end_task_action__action;
         end if; end if; end if; end if;
 
         return 0; 
@@ -718,7 +718,7 @@
 
 -- function get_attribute_value
 create function workflow_case__get_attribute_value (integer,varchar)
-returns integer as '
+returns varchar as '
 declare
   get_attribute_value__case_id                alias for $1;  
   get_attribute_value__attribute_name         alias for $2;  
@@ -936,23 +936,26 @@
   evaluate_guard__direction              alias for $7;  
   v_guard_happy_p                        boolean;
   v_rec                                  record;
+  v_str                                  text default '''';
 begin
-        if evaluate_guard__callback is null then
+        if evaluate_guard__callback = '''' or 
+           evaluate_guard__callback is null then
             -- null guard evaluates to true
             return ''t'';
         else
             if evaluate_guard__callback = ''#'' then
                 return ''f'';
             else
-                for v_rec in 
-                    execute ''select '' || evaluate_guard__callback
+                v_str := ''select '' || evaluate_guard__callback
                 || ''('' || 
                 evaluate_guard__case_id || '','' || 
-                evaluate_guard__workflow_key || '','' || 
-                evaluate_guard__transition_key || '','' || 
-                evaluate_guard__place_key || '','' || 
-                evaluate_guard__direction || '','' || 
-                evaluate_guard__custom_arg || '') as guard_happy_p''
+                quote_literal(evaluate_guard__workflow_key) || '','' || 
+                quote_literal(evaluate_guard__transition_key) || '','' || 
+                quote_literal(evaluate_guard__place_key) || '','' || 
+                quote_literal(evaluate_guard__direction) || '','' || 
+                quote_literal(evaluate_guard__custom_arg) || '') as guard_happy_p'';
+                for v_rec in 
+                    execute v_str
                 LOOP
                     return v_rec.guard_happy_p;                        
                 end LOOP;
@@ -973,11 +976,11 @@
   execute_transition_callback__case_id                alias for $3;  
   execute_transition_callback__transition_key         alias for $4;  
 begin
-        if execute_transition_callback__callback != '''' then 
+        if execute_transition_callback__callback != '''' and execute_transition_callback__callback is not null then 
             execute ''select '' || execute_transition_callback__callback
             || ''('' || execute_transition_callback__case_id || '','' || 
-            execute_transition_callback__transition_key || '','' || 
-            execute_transition_callback__custom_arg || '')'';
+            quote_literal(execute_transition_callback__transition_key) || '','' || 
+            quote_literal(execute_transition_callback__custom_arg) || '')'';
         end if;
 
         return 0; 
@@ -994,14 +997,14 @@
   execute_time_callback__transition_key         alias for $4;  
   v_rec                                         record;
 begin
-        if execute_time_callback__callback is null then
-            raise EXCEPTION ''-20000: There\\\'s no time_callback function for the timed transition \\\'%\\\''', execute_time_callback__transition_key;
+        if execute_time_callback__callback = '''' or execute_time_callback__callback is null then
+            raise EXCEPTION ''-20000: There''''s no time_callback function for the timed transition "%"'', execute_time_callback__transition_key;
         end if;
  
         for v_rec in execute ''select '' || execute_time_callback__callback || ''('' || 
                   execute_time_callback__case_id || '','' || 
-                  execute_time_callback__transition_key || '','' || 
-                  execute_time_callback__custom_arg || '') as trigger_time''
+                  quote_literal(execute_time_callback__transition_key) || '','' || 
+                  quote_literal(execute_time_callback__custom_arg) || '') as trigger_time''
         LOOP
             return v_rec.trigger_time;
         end LOOP;
@@ -1024,7 +1027,6 @@
   v_rec                                     record;
   v_str                                     varchar;
 begin
-        raise notice ''workflow_case__get_task_deadline'';
         /*
          * 1. or if there is a row in wf_case_deadlines, we use that
          * 2. if there is a callback, we execute that
@@ -1037,21 +1039,19 @@
             where case_id = get_task_deadline__case_id
             and   transition_key = get_task_deadline__transition_key;
 
-        raise notice ''get_task_deadline__callback = %'',get_task_deadline__callback;
         if NOT FOUND then
-            if get_task_deadline__callback != '''' then
+            if get_task_deadline__callback != '''' and get_task_deadline__callback is not null then
                 /* callback */
                 v_str := ''select '' || get_task_deadline__callback || ''('' || 
                 get_task_deadline__case_id || '','' || 
-                get_task_deadline__transition_key || '','' || 
-                get_task_deadline__custom_arg || '') as deadline'';
-                raise notice ''v_str = %'', v_str;
+                quote_literal(get_task_deadline__transition_key) || '','' || 
+                quote_literal(get_task_deadline__custom_arg) || '') as deadline'';
                 for v_rec in execute v_str
                 LOOP
                     v_deadline := v_rec.deadline;
                     exit;
                 end LOOP;
-            else if get_task_deadline__attribute_name != '''' then
+            else if get_task_deadline__attribute_name != '''' and get_task_deadline__attribute_name is not null then
                 /* attribute */
                 v_deadline := acs_object__get_attribute(
                     get_task_deadline__case_id,
@@ -1078,16 +1078,16 @@
   v_hold_timeout                                        timestamp;
   v_rec                                                 record;
 begin
-        if execute_hold_timeout_callback__callback is null then
+        if execute_hold_timeout_callback__callback = '''' or execute_hold_timeout_callback__callback is null then
             return null;
         end if;
  
         for v_rec in 
         execute ''select '' || execute_hold_timeout_callback__callback 
         || ''('' ||
         execute_hold_timeout_callback__case_id || '','' ||
-        execute_hold_timeout_callback__transition_key || '','' ||
-        execute_hold_timeout_callback__custom_arg || '') as hold_timeout''
+        quote_literal(execute_hold_timeout_callback__transition_key) || '','' ||
+        quote_literal(execute_hold_timeout_callback__custom_arg) || '') as hold_timeout''
         LOOP
            return v_rec.hold_timeout;
         end LOOP;
@@ -1105,9 +1105,9 @@
   task_id                alias for $2;  
   custom_arg             alias for $3;  
 begin
-        if callback != '''' then
+        if callback != '''' and callback is not null then
             execute ''select '' || callback
-            || ''('' || task_id || '','' || custom_arg || '')'';
+            || ''('' || task_id || '','' || quote_literal(custom_arg) || '')'';
         end if;
 
         return 0; 
@@ -1149,13 +1149,12 @@
                 case_assignment_rec.party_id
             );
         end loop;
-
         if v_done_p != ''t'' then
 
-            if set_task_assignments__callback != '''' then
+            if set_task_assignments__callback != '''' and set_task_assignments__callback is not null then
                 execute ''select ''|| set_task_assignments__callback || ''('' || 
                 set_task_assignments__task_id || '','' || 
-                set_task_assignments__custom_arg || '')'';
+                quote_literal(set_task_assignments__custom_arg) || '')'';
             else
                 for context_assignment_rec in  
                     select party_id
@@ -1233,7 +1232,8 @@
                              from wf_tokens 
                             where case_id = lock_token__case_id
                               and place_key = lock_token__place_key
-                              and state = ''free'');
+                              and state = ''free''
+                            limit 1);
 
         return 0; 
 end;' language 'plpgsql';
@@ -1256,7 +1256,7 @@
             where  state = ''locked''
             and    locked_task_id = release_token__task_id
         LOOP
-            PERFROM workflow_case__add_token(
+            PERFORM workflow_case__add_token(
                 token_rec.case_id,
                 token_rec.place_key,
                 release_token__journal_id
@@ -1289,10 +1289,12 @@
             set    state = ''consumed'',
                    consumed_date = now(),
                    consumed_journal_id = consume_token__journal_id
-            where  case_id = consume_token__case_id
-            and    place_key = consume_token__place_key
-            and    state = ''free''
-            and    rownum = 1;
+            where  token_id = (select token_id 
+                                 from wf_tokens 
+                                where case_id = consume_token__case_id
+                                  and place_key = consume_token__place_key
+                                  and state = ''free''
+                                limit 1);
         else
             update wf_tokens
             set    state = ''consumed'',
@@ -1320,7 +1322,6 @@
 begin
 
         PERFORM workflow_case__enable_transitions(sweep_automatic_transitions__case_id);
-
         LOOP
             v_done_p := ''t'';
             v_finished_p := workflow_case__finished_p(
@@ -1341,6 +1342,7 @@
                         task_rec.task_id,
                         sweep_automatic_transitions__journal_id
                     );
+
                     v_done_p := ''f'';
                 end loop;
                 PERFORM workflow_case__enable_transitions(sweep_automatic_transitions__case_id);
@@ -1355,7 +1357,7 @@
 
 -- function finished_p
 create function workflow_case__finished_p (integer,integer)
-returns booleanr as '
+returns boolean as '
 declare
   finished_p__case_id                alias for $1;  
   finished_p__journal_id             alias for $2;  
@@ -1416,9 +1418,9 @@
                 /* Add an extra entry to the journal */
                 v_journal_id := journal_entry__new(
                     null,
-                    object_id => finished_p__case_id,
-                    action => ''case finish'',
-                    action_pretty => ''Case finished'',
+                    finished_p__case_id,
+                    ''case finish'',
+                    ''Case finished'',
                     now(),
                     null,
                     null,
@@ -1476,7 +1478,7 @@
 Object  : '' || v_object_name || ''
 '';
 
-        if v_deadline_pretty != '''' then
+        if v_deadline_pretty != '''' and v_deadline_pretty is not null then
             v_body := v_body || ''Deadline: '' || v_deadline_pretty || ''
 '';
         end if;
@@ -1486,18 +1488,18 @@
     
         -- FIXME: last three args are also out varibles.
 
-        if notify_assignee__callback != '''' then
+        if notify_assignee__callback != '''' and notify_assignee__callback is not null then
             execute ''select '' || notify_assignee__callback || ''('' || 
             notify_assignee__task_id || '','' ||
-            notify_assignee__custom_arg || '','' ||
+            quote_literal(notify_assignee__custom_arg) || '','' ||
             notify_assignee__user_id || '','' ||
             v_party_from || '','' ||
             v_subject || '','' ||
             v_body || '')'';
         end if;
             
         -- FIXME: notifications package not ported yet.
-/*
+
         v_request_id := nt__post_request (       
             v_party_from,
             notify_assignee__user_id,
@@ -1506,7 +1508,7 @@
             v_body,
             3
         );
-*/
+
     return 0; 
 end;' language 'plpgsql';
 
@@ -1669,9 +1671,8 @@
         else if v_state = ''started'' then
             v_locked_task_id := fire_transition_internal__task_id;
         else 
-            raise EXCEPTION ''-20000: Can\\\'t fire the transition if it\\\'s not in state enabled or started'';
+            raise EXCEPTION ''-20000: Can''''t fire the transition if it''''s not in state enabled or started'';
         end if; end if;
-        
 
         /* Mark the task finished */
 
@@ -1709,6 +1710,7 @@
         LOOP
             v_place_key := place_rec.place_key;
             v_direction := place_rec.direction;
+
             v_guard_happy_p := workflow_case__evaluate_guard(
                 place_rec.guard_callback, 
                 place_rec.guard_custom_arg,
@@ -1741,6 +1743,7 @@
                 and    tp.direction = ''out''
                 and    tp.guard_callback = ''#''
             loop
+
                 PERFORM workflow_case__add_token(
                     v_case_id, 
                     place_rec.place_key,
@@ -1771,13 +1774,13 @@
   ensure_task_in_state__state           alias for $2;  
   v_count                               integer;        
 begin
-        select case when count(*) 0 then 0 else 1 end into v_count
+        select case when count(*) = 0 then 0 else 1 end into v_count
         from   wf_tasks 
         where  task_id = ensure_task_in_state__task_id
         and    state = ensure_task_in_state__state;
     
         if v_count != 1 then
-            raise EXCEPTION ''-20000: The task %  is not in state \\\'%\\\''', ensure_task_in_state__task_id, ensure_task_in_state__state
+            raise EXCEPTION ''-20000: The task %  is not in state "%"'', ensure_task_in_state__task_id, ensure_task_in_state__state;
         end if;
         
         return 0; 
@@ -1852,6 +1855,7 @@
 declare
   cancel_task__task_id                alias for $1;  
   cancel_task__journal_id             alias for $2;  
+  v_case_id                           integer;
 begin
         PERFORM workflow_case__ensure_task_in_state(cancel_task__task_id, 
                                                     ''started'');
@@ -1891,6 +1895,7 @@
 declare
   finish_task__task_id                alias for $1;  
   finish_task__journal_id             alias for $2;  
+  v_case_id                           integer;
 begin
         select case_id into v_case_id
         from   wf_tasks
@@ -1918,6 +1923,7 @@
 -- schedule processing of the notification queue
 --
 
+-- FIXME: notifications is not ported yet.
 create function inline_0 ()
 returns integer as '
 begin
@@ -1926,7 +1932,7 @@
     return 0;
 end;' language 'plpgsql';
 
--- select inline_0 ();
+select inline_0 ();
 
 drop function inline_0 ();
 
