source: branches/1.2/workflow/inc/engine/src/ProcessManager/InstanceManager.php @ 1349

Revision 1349, 10.9 KB checked in by niltonneto, 15 years ago (diff)

Ticket #561 - Inclusão do módulo Workflow faltante nessa versão.

  • Property svn:executable set to *
Line 
1<?php
2require_once(GALAXIA_LIBRARY.SEP.'src'.SEP.'ProcessManager'.SEP.'BaseManager.php');
3/**
4 * Add, removes, modifies and lists instances.
5 *
6 * @package Galaxia
7 * @license http://www.gnu.org/copyleft/gpl.html GPL
8 */
9class InstanceManager extends BaseManager {
10 
11  /**
12   * Constructor
13   *
14   * @param object &$db ADOdb
15   * @return object InstanceManager
16   * @access public
17   */
18  function InstanceManager(&$db)
19  {
20    parent::BaseManager($db);
21    $this->child_name = 'InstanceManager';
22  }
23
24  /**
25   * Gets an activities related to some instance
26   *
27   * @param int $iid Instance Id
28   * @return array Associative, describing activities and their relation with the instance
29   * @access public
30   */
31  function get_instance_activities($iid)
32  {
33    $query = 'select ga.wf_type,ga.wf_is_interactive,ga.wf_is_autorouted,ga.wf_activity_id,ga.wf_name,
34            gi.wf_p_id,gi.wf_instance_id,gi.wf_status,gi.wf_started,
35            gia.wf_activity_id,gia.wf_user,gia.wf_status as wf_act_status
36            from '.GALAXIA_TABLE_PREFIX.'activities ga,
37            INNER JOIN '.GALAXIA_TABLE_PREFIX.'instance_activities gia ON ga.wf_activity_id=gia.wf_activity_id
38            INNER JOIN '.GALAXIA_TABLE_PREFIX.'instances gi ON gia.wf_instance_id=gi.wf_instance_id,
39            where gi.wf_instance_id=?';
40    $result = $this->query($query, array($iid));
41    $ret = Array();
42    if (!(empty($result)))
43    {
44      while($res = $result->fetchRow())
45      {
46        // Number of active instances
47        $ret[] = $res;
48      }
49    }
50    return $ret;
51  }
52
53  /**
54  * Describes given instance
55  *
56  * @access public
57  * @param int $iid Instance Id
58  * @return array Associative, describing the instance
59  */
60  function get_instance($iid)
61  {
62    $query = 'select * from '.GALAXIA_TABLE_PREFIX.'instances gi where wf_instance_id=?';
63    $result = $this->query($query, array($iid));
64    $res = Array();
65    if (!(empty($result)))
66    {
67      $res = $result->fetchRow();
68      $res['wf_next_activity']=unserialize(base64_decode($res['wf_next_activity']));
69      $res['wf_workitems']=$this->getOne('select count(*) from '.GALAXIA_TABLE_PREFIX.'workitems where wf_instance_id=?', array($iid));
70    }
71    return $res;
72  }
73
74  /**
75   * Describes instance properties
76   *
77   * @access public
78   * @param int $iid Instance id
79   * @return array Associative, describing the instance properties
80   */
81  function get_instance_properties($iid)
82  {
83    $prop = unserialize(base64_decode($this->getOne('select wf_properties from '.GALAXIA_TABLE_PREFIX.'instances gi where wf_instance_id=?',array($iid))));
84    return $prop;
85  }
86
87  /**
88  * Start a transaction and lock the instance table on the given instance row.
89  * It can lock as weel the instance-activities table.
90  *
91  * @access private
92  * @param int $instanceId Instance id
93  * @param int $activityId Activity id, 0 b default, the instance-activities table is not locked, instead the instance-activities table will be locked on the corresponding instance-activity row
94  * @return bool
95  */
96  function lockAndStartTrans($instanceId, $activityId=0)
97  {
98    //do it in a transaction, for activities running
99    $this->db->StartTrans();
100    //we need to make a row lock now, first on the instance table (always first!)
101    $where = 'wf_instance_id='.(int)$instanceId;
102    if (!($this->db->RowLock(GALAXIA_TABLE_PREFIX.'instances', $where)))
103    {
104      $this->error[] = 'Process Manager: '.tra('failed to obtain lock on %1 table', 'instances');
105      $this->db->FailTrans();
106    }
107    if ($activityId)
108    {
109      //we need to make a row lock now, on the instance_activities table (always second!)
110      $where = 'wf_instance_id='.(int)$instanceId.' and wf_activity_id='.(int)$activityId;
111      if (!($this->db->RowLock(GALAXIA_TABLE_PREFIX.'instance_activities', $where)))
112      {
113        $this->error[] = 'Process Manager: '.tra('failed to obtain lock on %1 table','instances_activities');
114        return false;
115      }
116    }
117   }
118
119 
120  /**
121  * Saves given instance properties
122  *
123  * @access public
124  * @param int $iid Instance Id
125  * @param array $prop Associative, describing the instance properties
126  * @return bool
127  */
128  function set_instance_properties($iid,&$prop)
129  {
130    $this->lockAndStartTrans($iid);
131    //no more serialize, done by the core security_cleanup, empty array and bad properties names handled
132    $prop = $this->security_cleanup($prop, false);
133    $query = 'update '.GALAXIA_TABLE_PREFIX.'instances set wf_properties=? where wf_instance_id=?';
134    $this->query($query, array($prop,$iid));
135    return $this->db->CompleteTrans();
136  }
137 
138  /**
139  * Saves given instance name
140  *
141  * @access public
142  * @param int $iid Instance Id
143  * @param string $name Instance name
144  * @return bool
145  */
146  function set_instance_name($iid,$name)
147  {
148    $this->lockAndStartTrans($iid);
149    $query = 'update '.GALAXIA_TABLE_PREFIX.'instances set wf_name=? where wf_instance_id=?';
150    $this->query($query, array($name,$iid));
151    return $this->db->CompleteTrans();
152  }
153
154  /**
155  * Saves given instance priority
156  *
157  * @access public
158  * @param int $iid Instance id
159  * @param int $priority Instance priority
160  * @return bool
161  */
162  function set_instance_priority($iid,$priority)
163  {
164    $this->lockAndStartTrans($iid);
165    $query = 'update '.GALAXIA_TABLE_PREFIX.'instances set wf_priority=? where wf_instance_id=?';
166    $this->query($query, array((int)$priority, (int)$iid));
167    return $this->db->CompleteTrans();
168  }
169
170  /**
171  * Saves given instance category
172  *
173  * @access public
174  * @param int $iid Instance Id
175  * @param string $category Instance category
176  * @return bool
177  */
178  function set_instance_category($iid,$category)
179  {
180    $this->lockAndStartTrans($iid);
181    $query = 'update '.GALAXIA_TABLE_PREFIX.'instances set wf_category=? where wf_instance_id=?';
182    $this->query($query, array((int)$category, (int)$iid));
183    return $this->db->CompleteTrans();
184  }
185
186  /**
187  * Saves given instance owner
188  *
189  * @access public
190  * @param int $iid Instance Id
191  * @param int $owner Instance owner id
192  * @return bool
193  */
194  function set_instance_owner($iid,$owner)
195  {
196    $this->lockAndStartTrans($iid);
197    $query = 'update '.GALAXIA_TABLE_PREFIX.'instances set wf_owner=? where wf_instance_id=?';
198    $this->query($query, array($owner, $iid));
199    return $this->db->CompleteTrans();
200  }
201 
202  /**
203  * Saves given instance status
204  *
205  * @access public
206  * @param int $iid Instance Id
207  * @param string $status Instance status, should be one of 'active', 'completed', 'exception' or 'aborted
208  * @return bool
209  */
210  function set_instance_status($iid,$status)
211  {
212    if (!(($status=='completed') || ($status=='active') || ($status=='aborted') || ($status=='exception')))
213    {
214      $this->error[] = tra('unknown status');
215      return false;
216    }
217    $this->lockAndStartTrans($iid);
218    $query = 'update '.GALAXIA_TABLE_PREFIX.'instances set wf_status=? where wf_instance_id=?';
219    $this->query($query, array($status,$iid));
220    return $this->db->CompleteTrans();
221  }
222 
223  /**
224  * Removes all previous activities on this instance and create a new activity on the activity given
225  *
226  * @access public
227  * @param int $iid Instance id
228  * @param int $activityId Activity id
229  * @param mixed $user '*' by default and could be an user id
230  * @param string $status 'running' by default but you could send 'completed' as well
231  * @return bool False if any problems was encoutered (the database is then intact), true if everything was ok;
232  * WARNING: if they were multiple activities ALL previous activities avaible on this instance are deleted
233  */
234  function set_instance_destination($iid,$activityId, $user='*', $status='running')
235  {
236    $this->lockAndStartTrans($iid, $activityId);
237    $query = 'delete from '.GALAXIA_TABLE_PREFIX.'instance_activities where wf_instance_id=?';
238    $this->query($query, array($iid));
239    $query = 'insert into '.GALAXIA_TABLE_PREFIX.'instance_activities(wf_instance_id,wf_activity_id,wf_user,wf_status, wf_started, wf_ended)
240    values(?,?,?,?,?,?)';
241    $this->query($query, array($iid,$activityId,$user,$status,date('U'),0));
242    // perform commit (return true) or Rollback (return false)
243    return $this->db->CompleteTrans();
244  }
245 
246  /**
247  * Sets new user for activity $activityId if this activity is really related to the instance
248  * 
249  * @access public
250  * @param int $iid Instance Id
251  * @param int $activityId Activity Id
252  * @param int $user New user id
253  * @return bool
254  */
255  function set_instance_user($iid,$activityId,$user)
256  {
257    $this->lockAndStartTrans($iid, $activityId);
258    $query = "update ".GALAXIA_TABLE_PREFIX."instance_activities set wf_user=? where wf_instance_id=? and wf_activity_id=?";
259    $this->query($query, array($user, $iid, $activityId));
260    return $this->db->CompleteTrans();
261  }
262 
263  /**
264  * Deletes all references to given user on all instances, concerning wf_user, wf_owner and wf_next_user fields
265  *
266  * @param int $user User id to remove
267  * @return bool
268  * @access public
269  */
270  function remove_user($user) 
271  {
272    //TODO: add a global lock on the whole tables
273    // user=id => user='*'
274    $query = 'update '.GALAXIA_TABLE_PREFIX.'instance_activities set wf_user=? where wf_user=?';
275    $this->query($query,array('*',$user));
276    // owner=id => owner=0
277    $query = 'update '.GALAXIA_TABLE_PREFIX.'instances set wf_owner=? where wf_owner=?';
278    $this->query($query,array(0,$user));
279    // next_user=id => next_user=NULL
280    $query = 'update '.GALAXIA_TABLE_PREFIX.'instances set wf_next_user=? where wf_next_user=?';
281    $this->query($query,array(NULL,$user));
282    return true;
283  }
284 
285  /**
286  * Transfers all references concerning one user to another user, concerning wf_user, wf_owner and wf_next_user fields
287  * This function will not check access on the instance for the new user, it is the task of the admin to ensure the new user will have the necessary access rights
288  *
289  * @param array $user_array Associative, keys are 'old_user' : current user id and 'new_user' : the new user id
290  * @return bool
291  * @access public
292  */
293  function transfer_user($user_array) 
294  {
295    $new_user = $user_array['new_user'];
296    $old_user = $user_array['old_user'];
297    //TODO: add a global lock on the whole tables
298    // user
299    $query = 'update '.GALAXIA_TABLE_PREFIX.'instance_activities set wf_user=? where wf_user=?';
300    $this->query($query,array($new_user,$old_user));
301    // owner
302    $query = 'update '.GALAXIA_TABLE_PREFIX.'instances set wf_owner=? where wf_owner=?';
303    $this->query($query,array($new_user,$old_user));
304    // next_user
305    $query = 'update '.GALAXIA_TABLE_PREFIX.'instances set wf_next_user=? where wf_next_user=?';
306    $this->query($query,array($new_user,$old_user));
307    return true;
308  }
309 
310  /**
311  * Normalizes a property name
312  *
313  * @access public
314  * @param string $name Name to normalize
315  * @return string Property name
316  */
317  function normalize_name($name)
318  {
319    $name = trim($name);
320    $name = str_replace(" ","_",$name);
321    $name = preg_replace("/[^0-9A-Za-z\_]/",'',$name);
322    return $name;
323  }
324
325}   
326
327?>
Note: See TracBrowser for help on using the repository browser.