protected override void Execute(CodeActivityContext context) { // Get the connection string DBConnection ext = context.GetExtension <DBConnection>(); if (ext == null) { throw new InvalidProgramException("No connection string available"); } // Lookup the operator UserTasksDataContext dc = new UserTasksDataContext(ext.ConnectionString); OperatorConfig oc = dc.OperatorConfigs.SingleOrDefault(x => x.OperatorKey == OperatorKey.Get(context)); if (oc == null) { oc = new OperatorConfig(); oc.OperatorKey = OperatorKey.Get(context); oc.UnderEvaluation = false; oc.Frequency = 5; oc.NumberSinceLastEval = 0; dc.OperatorConfigs.InsertOnSubmit(oc); dc.SubmitChanges(); } // Determine the Queue info char[] delimiter = { '|' }; string[] values = QueueKey.Get(context).Split(delimiter, 3); // Lookup the queue and subqueue Queue q = dc.Queues.SingleOrDefault(x => x.QueueName == values[0]); if (q == null) { throw new InvalidProgramException("The specified queue (" + values[0] + ") was not found"); } SubQueue sq = dc.SubQueues.SingleOrDefault(x => x.QueueID == q.QueueID && x.SubQueueName == values[1]); if (sq == null) { throw new InvalidProgramException("The specified subqueue (" + values[0] + " - " + values[1] + ") was not found"); } bool bQC = bool.Parse(values[2]); if (sq.AllowSelection) { // Return all the available instances IEnumerable <QueueInstance> instances; if (bQC) { instances = dc.QueueInstances .Where(x => x.CurrentSubQueueID == sq.SubQueueID && x.QC == bQC && (x.AssignedOperatorID == null || x.AssignedOperatorID == oc.OperatorConfigID)) .OrderBy(x => x.Priority.Value) .OrderBy(x => x.CreateDate); } else { instances = dc.QueueInstances .Where(x => x.CurrentSubQueueID == sq.SubQueueID && x.QC == bQC && (x.AssignedOperatorID == null || x.AssignedOperatorID == oc.OperatorConfigID)) .OrderBy(x => x.CreateDate); } if (instances.Count() > 0) { QueueInstance[] qiList = new QueueInstance[instances.Count()]; int i = 0; foreach (QueueInstance r in instances) { qiList[i++] = r; } QueueInstanceList.Set(context, qiList); } } else { // Return the oldest instance IEnumerable <QueueInstance> instances; if (bQC) { instances = dc.QueueInstances .Where(x => x.CurrentSubQueueID == sq.SubQueueID && x.QC == bQC && (x.AssignedOperatorID == null || x.AssignedOperatorID == oc.OperatorConfigID)) .OrderBy(x => x.Priority.Value) .OrderBy(x => x.CreateDate) .Take(1); } else { instances = dc.QueueInstances .Where(x => x.CurrentSubQueueID == sq.SubQueueID && x.QC == bQC && (x.AssignedOperatorID == null || x.AssignedOperatorID == oc.OperatorConfigID)) .OrderBy(x => (x.Priority.HasValue ? 1 : 0) * x.Priority.Value * (bQC ? 1 : 0)) .OrderBy(x => x.CreateDate) .Take(1); } if (instances.Count() > 0) { QueueInstance qi = instances.First <QueueInstance>(); qi.AssignedOperatorID = oc.OperatorConfigID; dc.SubmitChanges(); QueueInstance[] qiList = new QueueInstance[1]; qiList[0] = qi; QueueInstanceList.Set(context, qiList); } } }
protected override void Track(TrackingRecord record, TimeSpan timeout) { CustomTrackingRecord customTrackingRecord = record as CustomTrackingRecord; if (customTrackingRecord != null) { if (customTrackingRecord.Name == "Start" || customTrackingRecord.Name == "Route" || customTrackingRecord.Name == "Assign" || customTrackingRecord.Name == "UnAssign" || customTrackingRecord.Name == "QC") { QueueTrack t = new QueueTrack(); // Extract all the user data if ((customTrackingRecord != null) && (customTrackingRecord.Data.Count > 0)) { foreach (string key in customTrackingRecord.Data.Keys) { switch (key) { case "QueueInstanceKey": if (customTrackingRecord.Data[key] != null) { t.QueueInstanceKey = (Guid)customTrackingRecord.Data[key]; } break; case "SubQueueID": if (customTrackingRecord.Data[key] != null) { t.SubQueueID = (int)customTrackingRecord.Data[key]; } break; case "QC": if (customTrackingRecord.Data[key] != null) { t.QC = (bool)customTrackingRecord.Data[key]; } break; case "OperatorKey": if (customTrackingRecord.Data[key] != null) { t.OperatorKey = (Guid)customTrackingRecord.Data[key]; } break; } } } if (t.SubQueueID != null && t.QC == null) { t.QC = false; } t.EventType = customTrackingRecord.Name; t.EventDate = DateTime.UtcNow; // Insert a record into the TrackUser table UserTasksDataContext dc = new UserTasksDataContext(_connectionString); dc.QueueTracks.InsertOnSubmit(t); dc.SubmitChanges(); } } }
protected override void CollectValues (out IDictionary <XName, object> readWriteValues, out IDictionary <XName, object> writeOnlyValues) { // We're not actually providing data to the caller readWriteValues = null; writeOnlyValues = null; //_actions.Clear(); // See if there is any work to do... if (_actions.Count > 0) { // Get the current transaction Transaction t = System.Transactions.Transaction.Current; // Setup the DataContext UserTasksDataContext dc = new UserTasksDataContext(_connectionString); // Open the connection, if necessary if (dc.Connection.State == System.Data.ConnectionState.Closed) { dc.Connection.Open(); } if (t != null) { dc.Connection.EnlistTransaction(t); } // Process each object in our work queue foreach (KeyValuePair <string, QueueInstance> kvp in _actions) { QueueInstance qi = kvp.Value as QueueInstance; // Perform the insert if (kvp.Key == "") { dc.QueueInstances.InsertOnSubmit(qi); } // Perform the update else { dc.Refresh(RefreshMode.OverwriteCurrentValues, dc.QueueInstances); QueueInstance qiTmp = dc.QueueInstances.SingleOrDefault <QueueInstance> (x => x.QueueInstanceID == qi.QueueInstanceID); if (qiTmp != null) { qiTmp.InstanceID = qi.InstanceID; qiTmp.AssignedDate = qi.AssignedDate; qiTmp.AssignedOperatorID = qi.AssignedOperatorID; qiTmp.CurrentSubQueueID = qi.CurrentSubQueueID; qiTmp.QC = qi.QC; } } } // Submit all the changes to the database dc.SubmitChanges(); // Remove all objects since the changes have been submitted _actions.Clear(); } }