/// <summary> /// Updates the time status. /// </summary> /// <param name="context">The context.</param> private void UpdateTimeStatus(BusinessContext context) { AssignmentEntity entity = context.Request.Target as AssignmentEntity; if (entity != null) { entity.TimeStatus = null; DateTime dateTimeNow = DataContext.Current.CurrentUserTimeZone.ToLocalTime(DateTime.UtcNow); if (entity.State == (int)AssignmentState.Active) { if (entity.PlanFinishDate.HasValue && entity.PlanFinishDate.Value < dateTimeNow) { entity.TimeStatus = (int)AssignmentTimeStatus.OverDue; } } else if (entity.State == (int)AssignmentState.Pending) { if (entity.PlanStartDate.HasValue && entity.PlanFinishDate.Value < dateTimeNow) { entity.TimeStatus = (int)AssignmentTimeStatus.OverStart; } } } }
/// <summary> /// Closes the assignment. /// </summary> /// <param name="context">The context.</param> protected virtual void CloseAssignment(BusinessContext context) { CloseAssignmentRequest request = (CloseAssignmentRequest)context.Request; // Load Assignment MetaObject metaObject = MetaObjectActivator.CreateInstance(AssignmentEntity.ClassName, context.GetTargetPrimaryKeyId().Value); if (((int)metaObject[AssignmentEntity.FieldState]) != (int)AssignmentState.Active) { throw new InvalidOperationException(); } DateTime dateTimeNow = DataContext.Current.CurrentUserTimeZone.ToLocalTime(DateTime.UtcNow); // Update Meta Object Fields metaObject[AssignmentEntity.FieldClosedBy] = (int)DataContext.Current.CurrentUserId; metaObject[AssignmentEntity.FieldState] = (int)AssignmentState.Closed; metaObject[AssignmentEntity.FieldExecutionResult] = request.ExecutionResult; metaObject[AssignmentEntity.FieldComment] = request.Comment; metaObject[AssignmentEntity.FieldActualFinishDate] = dateTimeNow; // Save Meta Object metaObject.Save(); if (metaObject[AssignmentEntity.FieldWorkflowInstanceId] != null) { // Run Workflow AssignmentEntity entity = (AssignmentEntity)BusinessManager.Load(AssignmentEntity.ClassName, context.GetTargetPrimaryKeyId().Value); RunWorkflow(entity); } context.SetResponse(new Response()); }
/// <summary> /// Suspends the assignment. /// </summary> /// <param name="context">The context.</param> private void SuspendAssignment(BusinessContext context) { // Load Assignment MetaObject metaObject = MetaObjectActivator.CreateInstance(AssignmentEntity.ClassName, context.GetTargetPrimaryKeyId().Value); if (((int)metaObject[AssignmentEntity.FieldState]) != (int)AssignmentState.Active) { throw new InvalidOperationException(); } // Load Meta Object metaObject[AssignmentEntity.FieldState] = (int)AssignmentState.Suspended; // Save Meta Object metaObject.Save(); if (metaObject[AssignmentEntity.FieldWorkflowInstanceId] != null) { // Run Workflow AssignmentEntity entity = (AssignmentEntity)BusinessManager.Load(AssignmentEntity.ClassName, context.GetTargetPrimaryKeyId().Value); RunWorkflow(entity); } context.SetResponse(new Response()); }
/// <summary> /// Assignments the is request. /// </summary> /// <param name="assignment">The assignment.</param> /// <returns></returns> private bool AssignmentIsRequest(AssignmentEntity assignment) { if (this.UseRequest && assignment.ParentAssignmentId.HasValue) { return(true); } return(false); }
/// <summary> /// Creates the entity object. /// </summary> /// <param name="metaClassName">Name of the meta class.</param> /// <param name="primaryKeyId">The primary key id.</param> /// <returns></returns> protected override EntityObject CreateEntityObject(string metaClassName, PrimaryKeyId? primaryKeyId) { if (metaClassName == AssignmentEntity.ClassName) { AssignmentEntity retVal = new AssignmentEntity(); retVal.PrimaryKeyId = primaryKeyId; return retVal; } return base.CreateEntityObject(metaClassName, primaryKeyId); }
/// <summary> /// Creates the entity object. /// </summary> /// <param name="metaClassName">Name of the meta class.</param> /// <param name="primaryKeyId">The primary key id.</param> /// <returns></returns> protected override EntityObject CreateEntityObject(string metaClassName, PrimaryKeyId?primaryKeyId) { if (metaClassName == AssignmentEntity.ClassName) { AssignmentEntity retVal = new AssignmentEntity(); retVal.PrimaryKeyId = primaryKeyId; return(retVal); } return(base.CreateEntityObject(metaClassName, primaryKeyId)); }
/// <summary> /// Runs the workflow. /// </summary> /// <param name="entity">The entity.</param> /// <param name="executionResult">The execution result.</param> private static void RunWorkflow(AssignmentEntity entity) { Guid wfInstanceId = (Guid)entity.WorkflowInstanceId; string queueName = entity.WorkflowActivityName; WorkflowInstance instance = GlobalWorkflowRuntime.WorkflowRuntime.GetWorkflow(wfInstanceId); instance.EnqueueItem(queueName, entity, null, null); GlobalWorkflowRuntime.RunWorkflow(instance); }
/// <summary> /// Queues the item available. /// </summary> /// <param name="ctx">The CTX.</param> protected ActivityExecutionStatus ProcessQueueElement(ActivityExecutionContext ctx) { WorkflowQueue queue = GetQueue(ctx); AssignmentEntity assignment = queue.Dequeue() as AssignmentEntity; if (assignment != null) { return(CheckAssignmentCompletion(assignment)); } return(ActivityExecutionStatus.Executing); }
/// <summary> /// Creates the assignment. /// </summary> private void CreateAssignment() { AssignmentEntity assignment = BusinessManager.InitializeEntity <AssignmentEntity>(AssignmentEntity.ClassName); WorkflowInstanceEntity ownerWfInstance = (WorkflowInstanceEntity)BusinessManager.Load(WorkflowInstanceEntity.ClassName, (PrimaryKeyId)this.WorkflowInstanceId); // Step 2-1. Set Assignment Properties InitAssignmentFields(ownerWfInstance, assignment, this.AssignmentProperties); // Step 2-2 Initialize Plan Finish Date InitializePlanFinishDate(ownerWfInstance, assignment); assignment.Type = "Assignment"; assignment.PrimaryKeyId = BusinessManager.Create(assignment); BusinessManager.Execute(new ActivateAssignmentRequest(assignment)); // Step 3. Create Assignment Request if (this.UseRequest) { foreach (int userId in ResolveRequestUserIdByProperties()) { AssignmentEntity request = BusinessManager.InitializeEntity <AssignmentEntity>(AssignmentEntity.ClassName); InitAssignmentFields(ownerWfInstance, request, this.RequestProperties); request.Type = "AssignmentRequest"; request.UserId = userId; request.ParentAssignmentId = assignment.PrimaryKeyId; // Create request.PrimaryKeyId = BusinessManager.Create(request); // Activate BusinessManager.Execute(new ActivateAssignmentRequest(request)); } } }
/// <summary> /// Checks the assignment states. /// </summary> /// <param name="assignment">The assignment.</param> /// <returns></returns> private ActivityExecutionStatus CheckAssignmentCompletion(AssignmentEntity assignment) { if (assignment.State == (int)AssignmentState.Closed) { if (AssignmentIsRequest(assignment)) { if (assignment.ExecutionResult == (int)AssignmentExecutionResult.Accepted) { // TODO: Close all assignment request foreach (AssignmentEntity request in BusinessManager.List(AssignmentEntity.ClassName, new FilterElement[] { FilterElement.EqualElement(AssignmentEntity.FieldParentAssignmentId, assignment.ParentAssignmentId.Value) })) { BusinessManager.Execute(new CloseAssignmentRequest(request.PrimaryKeyId.Value, (int)AssignmentExecutionResult.Canceled)); } // Update User In Parent Assignment AssignmentEntity parentAssignment = (AssignmentEntity)BusinessManager.Load(AssignmentEntity.ClassName, assignment.ParentAssignmentId.Value); parentAssignment.UserId = assignment.UserId; BusinessManager.Update(parentAssignment); } } else { if (assignment.ExecutionResult == (int)AssignmentExecutionResult.Declined) { return(ActivityExecutionStatus.Canceling); } return(ActivityExecutionStatus.Closed); } } return(ActivityExecutionStatus.Executing); }
/// <summary> /// Inits the assignment fields. /// </summary> /// <param name="assignment">The assignment.</param> /// <param name="strPrototypeId">The STR prototype id.</param> private void InitAssignmentFields(WorkflowInstanceEntity ownerWfInstance, AssignmentEntity assignment, PropertyValueCollection properties) { // Init Workflow Properties assignment.WorkflowInstanceId = (PrimaryKeyId)this.WorkflowInstanceId; assignment.WorkflowActivityName = this.Name; // Copy Owner From assignment.OwnerDocumentId = ownerWfInstance.OwnerDocumentId; if (properties != null) { foreach (PropertyValue prop in properties) { if (prop.IsDynamicValue) { assignment[prop.Name] = ((DynamicValue)prop.Value).EvaluateValue(this); } else { assignment[prop.Name] = prop.Value; } } } }
private void InitializePlanFinishDate(WorkflowInstanceEntity ownerWfInstance, AssignmentEntity assignment) { if (ownerWfInstance.PlanFinishTimeType == (int)TimeType.NotSet || assignment.PlanFinishDate != null) { return; } DateTime dateTimeNow = DataContext.Current.CurrentUserTimeZone.ToLocalTime(DateTime.UtcNow); if (dateTimeNow >= ownerWfInstance.PlanFinishDate.Value) { assignment.PlanFinishDate = ownerWfInstance.PlanFinishDate; return; } int currentAssignment = CalculateCurrentAssignmentCount(); int postAssignmentCount = CalculatePostAssignmentCount(); if (postAssignmentCount == 0) { assignment.PlanFinishDate = ownerWfInstance.PlanFinishDate; } else { double avgDurations = (ownerWfInstance.PlanFinishDate.Value - dateTimeNow).TotalMinutes / (postAssignmentCount + currentAssignment); assignment.PlanFinishDate = dateTimeNow.AddMinutes(avgDurations * currentAssignment); } }
/// <summary> /// Called when [assignment updated]. /// </summary> /// <param name="primaryKey">The primary key.</param> private void OnAssignmentUpdated(AssignmentEntity oldEntity, AssignmentEntity newEntity) { PrimaryKeyId primaryKey = oldEntity.PrimaryKeyId.Value; // Check User if (newEntity.UserId != oldEntity.UserId) OnAssignmentUserAssigned(primaryKey, oldEntity.UserId, newEntity.UserId); // Subject if (newEntity.Subject != oldEntity.Subject ) SystemEvents.AddSystemEvents(SystemEventTypes.Assignment_Updated_GeneralInfo, (Guid)primaryKey); // Priority if (newEntity.Priority != oldEntity.Priority) SystemEvents.AddSystemEvents(SystemEventTypes.Assignment_Updated_Priority, (Guid)primaryKey); // State if (newEntity.State != oldEntity.State) { SystemEvents.AddSystemEvents(SystemEventTypes.Assignment_Updated_Status, (Guid)primaryKey); // delete DateTypeValue for closed assignment (we notify about active only) if (newEntity.State == (int)AssignmentState.Closed) Schedule.DeleteDateTypeValue(DateTypes.AssignmentFinishDate, (Guid)primaryKey); } // Finish Date if (newEntity.PlanFinishDate != oldEntity.PlanFinishDate) { if (oldEntity.PlanFinishDate.HasValue) { Schedule.DeleteDateTypeValue(DateTypes.AssignmentFinishDate, (Guid)primaryKey); } DateTime dateTimeNow = DataContext.Current.CurrentUserTimeZone.ToLocalTime(DateTime.UtcNow); if (newEntity.PlanFinishDate.HasValue && newEntity.PlanFinishDate.Value > dateTimeNow) { Schedule.UpdateDateTypeValue(DateTypes.AssignmentFinishDate, (Guid)primaryKey, DataContext.Current.CurrentUserTimeZone.ToUniversalTime(newEntity.PlanFinishDate.Value)); } SystemEvents.AddSystemEvents(SystemEventTypes.Assignment_Updated_FinishDate, (Guid)primaryKey); } }
/// <summary> /// Gets the control path. /// </summary> /// <param name="entity">The entity.</param> /// <returns></returns> public static string GetControlPath(AssignmentEntity entity) { return("~/Apps/BusinessProcess/AssignmentControls/ApprovalWithComment.ascx"); }
/// <summary> /// Saves the old assignment entity. /// </summary> /// <param name="context">The context.</param> /// <param name="entity">The entity.</param> private void SaveOldAssignmentEntity(BusinessContext context, AssignmentEntity entity) { context.Items[OldAssignmentEntity] = entity; }
/// <summary> /// Creates the business process activity info. /// </summary> /// <param name="businessProcess">The business process.</param> /// <param name="scope">The scope.</param> /// <param name="parentActivity">The parent activity.</param> /// <param name="activityInfoItems">The activity info items.</param> private static void CreateBusinessProcessActivityInfo(BusinessProcessInfo businessProcess, WorkflowChangesScope scope, EntityObject[] assignments, CompositeActivity parentActivity, ActivityInfoCollection activityInfoItems) { foreach (Activity activity in parentActivity.Activities) { if (activity is CreateAssignmentAndWaitResultActivity) { CreateAssignmentAndWaitResultActivity asgActivity = (CreateAssignmentAndWaitResultActivity)activity; PropertyValueCollection properties = asgActivity.AssignmentProperties; ActivityInfo activityInfo = new ActivityInfo(); activityInfoItems.Add(activityInfo); activityInfo.Name = activity.Name; activityInfo.Type = ActivityType.Assignment; AssignmentEntity assignment = FindAssignmentByActivityName(assignments, activity.Name); if (assignment != null) { activityInfo.State = (AssignmentState)assignment.State; activityInfo.Subject = assignment.Subject; activityInfo.TimeStatus = (AssignmentTimeStatus?)assignment.TimeStatus; activityInfo.PlanStartDate = assignment.ActualStartDate; activityInfo.PlanFinishDate = assignment.PlanFinishDate; activityInfo.ActualStartDate = assignment.ActualStartDate; activityInfo.ActualFinishDate = assignment.ActualFinishDate; activityInfo.Comment = assignment.Comment; activityInfo.UserId = assignment.UserId; activityInfo.ClosedBy = assignment.ClosedBy; activityInfo.ExecutionResult = (AssignmentExecutionResult?)assignment.ExecutionResult; } else { activityInfo.State = businessProcess.State == BusinessProcessState.Closed? AssignmentState.Closed: AssignmentState.Pending; if (asgActivity.AssignmentProperties.Contains(AssignmentEntity.FieldSubject)) { activityInfo.Subject = (string)asgActivity.AssignmentProperties[AssignmentEntity.FieldSubject]; } if (asgActivity.AssignmentProperties.Contains(AssignmentEntity.FieldUserId)) { activityInfo.UserId = (int?)asgActivity.AssignmentProperties[AssignmentEntity.FieldUserId]; } // Resolve Plan Time //if (activityInfo.State == AssignmentState.Pending && // businessProcess.State == BusinessProcessState.Active && // businessProcess.PlanFinishDate.HasValue) //{ // //CalculatePlanFinishDate(businessProcess, parentActivity, activity, activityInfo); //} //if (activityInfo.PlanStartDate.HasValue && // activityInfo.PlanStartDate.Value < DateTime.Now) // activityInfo.TimeStatus = AssignmentTimeStatus.OverStart; //if (activityInfo.PlanFinishDate.HasValue && // activityInfo.PlanFinishDate.Value < DateTime.Now) // activityInfo.TimeStatus = AssignmentTimeStatus.OverDue; } // 2008-07-07 Added Assignment Properties activityInfo.AssignmentProperties.AddRange(asgActivity.AssignmentProperties); // } else if (activity is BlockActivity) { // Create Block Info ActivityInfo blockInfo = new ActivityInfo(); activityInfoItems.Add(blockInfo); blockInfo.Name = activity.Name; blockInfo.Type = ActivityType.Block; blockInfo.State = businessProcess.State == BusinessProcessState.Closed ? AssignmentState.Closed : AssignmentState.Pending; CreateBusinessProcessActivityInfo(businessProcess, scope, assignments, (BlockActivity)activity, blockInfo.Activities); if (blockInfo.State == AssignmentState.Pending) { if (blockInfo.Activities.Count > 0) { foreach (ActivityInfo childActivitiInfo in blockInfo.Activities) { if (childActivitiInfo.State == AssignmentState.Active) { blockInfo.State = AssignmentState.Active; break; } else if (childActivitiInfo.State == AssignmentState.Closed) { blockInfo.State = AssignmentState.Closed; } } } else { int blockIndex = activityInfoItems.IndexOf(blockInfo); if (blockIndex == 0) { blockInfo.State = AssignmentState.Closed; } else { blockInfo.State = activityInfoItems[blockIndex - 1].State == AssignmentState.Active? AssignmentState.Pending: activityInfoItems[blockIndex - 1].State; } } } if (blockInfo.Activities.Count > 0 && (blockInfo.State == AssignmentState.Active || blockInfo.State == AssignmentState.Closed)) { blockInfo.PlanStartDate = blockInfo.Activities[0].PlanStartDate; blockInfo.PlanFinishDate = blockInfo.Activities[0].PlanFinishDate; } } } }
/// <summary> /// Gets the control path. /// </summary> /// <param name="entity">The entity.</param> /// <returns></returns> public static string GetControlPath(AssignmentEntity entity) { return "~/Apps/BusinessProcess/AssignmentControls/ApprovalWithComment.ascx"; }