static void WorkflowRuntime_WorkflowTerminated(object sender, WorkflowTerminatedEventArgs e)
        {
            Trace.WriteLine("WorkflowTerminated", "GlobalWorkflowRuntime");

            WorkflowInstanceEntity wf = (WorkflowInstanceEntity)BusinessManager.Load(WorkflowInstanceEntity.ClassName, (PrimaryKeyId)e.WorkflowInstance.InstanceId);

            DateTime dateTimeNow = DataContext.Current.CurrentUserTimeZone.ToLocalTime(DateTime.UtcNow);

            if (e.Exception is CustomWorkflowTerminateException)
            {
                wf.ExecutionResult  = (int)((CustomWorkflowTerminateException)e.Exception).ExecutionResult;
                wf.ActualFinishDate = dateTimeNow;
                wf.State            = (int)BusinessProcessState.Closed;
            }
            else
            {
                wf.State            = (int)BusinessProcessState.Closed;
                wf.ActualFinishDate = dateTimeNow;
                wf.ExecutionResult  = (int)BusinessProcessExecutionResult.Canceled;                // Calculate Result
            }

            // TODO: Save Exception
            //e.Exception

            BusinessManager.Update(wf);

            // Cancel All Active Assignments
            CancelAllAssignments(wf);
        }
        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);
            }
        }
Exemple #3
0
        /// <summary>
        /// Creates the workflow view.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <returns></returns>
        public static WorkflowView CreateWorkflowView(WorkflowInstanceEntity instance)
        {
            if (instance == null)
                throw new ArgumentNullException("instance");

            return CreateWorkflowView(McWorkflowSerializer.GetObject<Activity>(instance.Xaml));
        }
        /// <summary>
        /// Suspends the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        protected virtual void Suspend(BusinessContext context)
        {
            //string strPrimaryKeyId = "28ad9324-85a5-4dd2-96a2-ace25bd14be4";

            //BusinessManager.Execute<CompleteAssignmentRequest, Response>(new CompleteAssignmentRequest(PrimaryKeyId.Parse(strPrimaryKeyId)));

            // Load WorkflowInstanceEntity
            WorkflowInstanceEntity wf = (WorkflowInstanceEntity)BusinessManager.Load(WorkflowInstanceEntity.ClassName, context.GetTargetPrimaryKeyId().Value);

            // Check Wf State
            if (wf.State != (int)BusinessProcessState.Active)
            {
                throw new InvalidOperationException();
            }

            WorkflowInstance instance = GlobalWorkflowRuntime.WorkflowRuntime.GetWorkflow((Guid)wf.PrimaryKeyId.Value);

            instance.Suspend(string.Empty);
            GlobalWorkflowRuntime.RunWorkflow(instance);

            //wf.State = (int)BusinessProcessState.Suspended;
            //BusinessManager.Update(wf);

            // Susupend All Active Assignments
        }
        private void UpdateTimeStatus(BusinessContext context)
        {
            WorkflowInstanceEntity entity = context.Request.Target as WorkflowInstanceEntity;

            if (entity != null)
            {
                entity.TimeStatus = null;

                if (entity.State == (int)BusinessProcessState.Active)
                {
                    DateTime dateTimeNow = DataContext.Current.CurrentUserTimeZone.ToLocalTime(DateTime.UtcNow);

                    if (entity.PlanFinishDate.HasValue && entity.PlanFinishDate.Value < dateTimeNow)
                    {
                        entity.TimeStatus = (int)WorkflowInstanceTimeStatus.OverDue;
                    }
                }
                //else if (entity.State == (int)BusinessProcessState.Pending)
                //{
                //    if (entity.PlanStartDate.HasValue && entity.PlanFinishDate.Value < DateTime.Now)
                //    {
                //        entity.TimeStatus = (int)WorkflowInstanceTimeStatus.OverStart;
                //    }
                //}
            }
        }
        /// <summary>
        /// Creates the workflow view.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <returns></returns>
        public static WorkflowView CreateWorkflowView(WorkflowInstanceEntity instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            return(CreateWorkflowView(McWorkflowSerializer.GetObject <Activity>(instance.Xaml)));
        }
        /// <summary>
        /// Creates the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static WorkflowChangesScope Create(WorkflowInstanceEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            WorkflowChangesScope retVal = new WorkflowChangesScope(entity);

            return(retVal);
        }
        /// <summary>
        /// Handles the WorkflowStarted event of the WorkflowRuntime control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Workflow.Runtime.WorkflowEventArgs"/> instance containing the event data.</param>
        static void WorkflowRuntime_WorkflowStarted(object sender, WorkflowEventArgs e)
        {
            DateTime dateTimeNow = DataContext.Current.CurrentUserTimeZone.ToLocalTime(DateTime.UtcNow);

            WorkflowInstanceEntity wf = (WorkflowInstanceEntity)BusinessManager.Load(WorkflowInstanceEntity.ClassName, (PrimaryKeyId)e.WorkflowInstance.InstanceId);

            wf.ActualStartDate = dateTimeNow;
            wf.State           = (int)BusinessProcessState.Active;

            BusinessManager.Update(wf);
        }
        /// <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 == WorkflowInstanceEntity.ClassName)
            {
                WorkflowInstanceEntity retVal = new WorkflowInstanceEntity();
                retVal.PrimaryKeyId = primaryKeyId;
                return(retVal);
            }

            return(base.CreateEntityObject(metaClassName, primaryKeyId));
        }
        static void WorkflowRuntime_WorkflowSuspended(object sender, WorkflowSuspendedEventArgs e)
        {
            Trace.WriteLine("WorkflowSuspended", "GlobalWorkflowRuntime");

            WorkflowInstanceEntity wf = (WorkflowInstanceEntity)BusinessManager.Load(WorkflowInstanceEntity.ClassName, (PrimaryKeyId)e.WorkflowInstance.InstanceId);

            wf.State = (int)BusinessProcessState.Suspended;
            BusinessManager.Update(wf);

            // Suspend All Active Assignments
            SuspendAllActiveAssignments(wf);
        }
        /// <summary>
        /// Suspends all active assignments.
        /// </summary>
        /// <param name="wf">The wf.</param>
        private static void SuspendAllActiveAssignments(WorkflowInstanceEntity wf)
        {
            foreach (AssignmentEntity assignment in BusinessManager.List(AssignmentEntity.ClassName, new FilterElement[]
            {
                FilterElement.EqualElement(AssignmentEntity.FieldState, (int)AssignmentState.Active),
                FilterElement.EqualElement(AssignmentEntity.FieldWorkflowInstanceId, wf.PrimaryKeyId.Value)
            }))
            {
                assignment.State = (int)AssignmentState.Suspended;

                BusinessManager.Update(assignment);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="WorkflowChangesScope"/> class.
        /// </summary>
        /// <param name="entity">The entity.</param>
        protected WorkflowChangesScope(WorkflowInstanceEntity entity)
        {
            this.InstanceId     = entity.PrimaryKeyId.HasValue ? (Guid)entity.PrimaryKeyId.Value : Guid.Empty;
            this.WorkflowEntity = entity;

            if (this.InstanceId != Guid.Empty && TryLoadFromMsWorkflow())
            {
                // TODO: Custom Login here
            }
            else
            {
                this.TransientWorkflow = McWorkflowSerializer.GetObject <CompositeActivity>((string)this.WorkflowEntity[WorkflowInstanceEntity.FieldXaml]);
            }
        }
        /// <summary>
        /// Cancels all assignments.
        /// </summary>
        /// <param name="wf">The wf.</param>
        private static void CancelAllAssignments(WorkflowInstanceEntity wf)
        {
            foreach (AssignmentEntity assignment in BusinessManager.List(AssignmentEntity.ClassName, new FilterElement[]
            {
                FilterElement.NotEqualElement(AssignmentEntity.FieldState, (int)AssignmentState.Closed),
                FilterElement.EqualElement(AssignmentEntity.FieldWorkflowInstanceId, wf.PrimaryKeyId.Value)
            }))
            {
                assignment.State           = (int)AssignmentState.Closed;
                assignment.ExecutionResult = (int)AssignmentExecutionResult.Canceled;

                BusinessManager.Update(assignment);
            }
        }
Exemple #14
0
        /// <summary>
        /// Initializes a new instance of the <see cref="WorkflowChangesScope"/> class.
        /// </summary>
        /// <param name="entity">The entity.</param>
        protected WorkflowChangesScope(WorkflowInstanceEntity entity)
        {
            this.InstanceId = entity.PrimaryKeyId.HasValue ? (Guid)entity.PrimaryKeyId.Value : Guid.Empty;
            this.WorkflowEntity = entity;

            if (this.InstanceId != Guid.Empty && TryLoadFromMsWorkflow())
            {
                // TODO: Custom Login here
            }
            else
            {
                this.TransientWorkflow = McWorkflowSerializer.GetObject<CompositeActivity>((string)this.WorkflowEntity[WorkflowInstanceEntity.FieldXaml]);
            }
        }
        /// <summary>
        /// Handles the WorkflowCompleted event of the WorkflowRuntime control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Workflow.Runtime.WorkflowCompletedEventArgs"/> instance containing the event data.</param>
        static void WorkflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)
        {
            Trace.WriteLine("WorkflowCompleted", "GlobalWorkflowRuntime");

            DateTime dateTimeNow = DataContext.Current.CurrentUserTimeZone.ToLocalTime(DateTime.UtcNow);

            WorkflowInstanceEntity wf = (WorkflowInstanceEntity)BusinessManager.Load(WorkflowInstanceEntity.ClassName, (PrimaryKeyId)e.WorkflowInstance.InstanceId);

            wf.State            = (int)BusinessProcessState.Closed;
            wf.ExecutionResult  = (int)BusinessProcessExecutionResult.Accepted;            // Calculate Result
            wf.ActualFinishDate = dateTimeNow;

            BusinessManager.Update(wf);

            // Cancel All Assignments
            CancelAllAssignments(wf);
        }
        /// <summary>
        /// Resumes the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        protected virtual void Resume(BusinessContext context)
        {
            // Load WorkflowInstanceEntity
            WorkflowInstanceEntity wf = (WorkflowInstanceEntity)BusinessManager.Load(WorkflowInstanceEntity.ClassName, context.GetTargetPrimaryKeyId().Value);

            // Check Wf State
            if (wf.State != (int)BusinessProcessState.Suspended)
            {
                throw new InvalidOperationException();
            }

            WorkflowInstance instance = GlobalWorkflowRuntime.WorkflowRuntime.GetWorkflow((Guid)wf.PrimaryKeyId.Value);

            instance.Resume();
            GlobalWorkflowRuntime.RunWorkflow(instance);

            //wf.State = (int)BusinessProcessState.Active;
            //BusinessManager.Update(wf);
        }
        /// <summary>
        /// Terminates the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        protected virtual void Terminate(BusinessContext context)
        {
            // Load WorkflowInstanceEntity
            WorkflowInstanceEntity wf = (WorkflowInstanceEntity)BusinessManager.Load(WorkflowInstanceEntity.ClassName, context.GetTargetPrimaryKeyId().Value);

            // Check Wf State
            if (wf.State != (int)BusinessProcessState.Active)
            {
                throw new InvalidOperationException();
            }

            WorkflowInstance instance = GlobalWorkflowRuntime.WorkflowRuntime.GetWorkflow((Guid)wf.PrimaryKeyId.Value);

            instance.Terminate("Cancel by user");
            GlobalWorkflowRuntime.RunWorkflow(instance);

            //wf.State = (int)BusinessProcessState.Completed;
            //wf.ExecutionResult = (int)BusinessProcessExecutionResult.Canceled;

            //BusinessManager.Update(wf);
        }
        /// <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>
        /// 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;
                    }
                }
            }
        }
        /// <summary>
        /// Creates the workflow participiant.
        /// </summary>
        /// <param name="workflowInstanceId">The workflow instance id.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="entity">The entity.</param>
        private void CreateWorkflowParticipiant(Guid workflowInstanceId, int userId, WorkflowInstanceEntity entity)
        {
            WorkflowParticipantRow row = new WorkflowParticipantRow();
            row.UserId = userId;
            row.WorkflowInstanceId = workflowInstanceId;

            // TODO: Extend Owner Processing
            row.ObjectId = entity.OwnerDocumentId.Value;
            row.ObjectType = (int)ObjectTypes.Document;

            row.Update();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ObjectId != PrimaryKeyId.Empty)
                template = (WorkflowDefinitionEntity)BusinessManager.Load(WorkflowDefinitionEntity.ClassName, ObjectId);
            else if (FromInstanceId != PrimaryKeyId.Empty)
                instance = (WorkflowInstanceEntity)BusinessManager.Load(WorkflowInstanceEntity.ClassName, FromInstanceId);

            if (!IsPostBack)
            {
                BindInfo();		// texts, icons
                BindData();
                BindToolbar();
            }

            LoadControlToPlaceHolder(!IsPostBack);

            if (!this.Page.ClientScript.IsStartupScriptRegistered(this.Page.GetType(), "EnumEdit"))
                this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "EnumEdit",
                    @"function enumEdit_OpenWindow(query, w, h, resize){
                        var l = (screen.width - w) / 2;
                        var t = (screen.height - h) / 2;

                        winprops = 'height='+h+',width='+w+',top='+t+',left='+l;
                        if (scroll) winprops+=',scrollbars=1';
                        if (resize)
                            winprops+=',resizable=1';
                        else
                            winprops+=',resizable=0';
                        var f = window.open(query, '_blank', winprops);
                    }", true);
        }
        /// <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 == WorkflowInstanceEntity.ClassName)
            {
                WorkflowInstanceEntity retVal = new WorkflowInstanceEntity();
                retVal.PrimaryKeyId = primaryKeyId;
                return retVal;
            }

            return base.CreateEntityObject(metaClassName, primaryKeyId);
        }
Exemple #23
0
        /// <summary>
        /// Cancels all assignments.
        /// </summary>
        /// <param name="wf">The wf.</param>
        private static void CancelAllAssignments(WorkflowInstanceEntity wf)
        {
            foreach (AssignmentEntity assignment in BusinessManager.List(AssignmentEntity.ClassName, new FilterElement[]
                            {
                                FilterElement.NotEqualElement(AssignmentEntity.FieldState, (int)AssignmentState.Closed),
                                FilterElement.EqualElement(AssignmentEntity.FieldWorkflowInstanceId, wf.PrimaryKeyId.Value)
                            }))
            {
                assignment.State = (int)AssignmentState.Closed;
                assignment.ExecutionResult = (int)AssignmentExecutionResult.Canceled;

                BusinessManager.Update(assignment);
            }
        }
Exemple #24
0
        /// <summary>
        /// Creates the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static WorkflowChangesScope Create(WorkflowInstanceEntity entity)
        {
            if (entity == null)
                throw new ArgumentNullException("entity");

            WorkflowChangesScope retVal = new WorkflowChangesScope(entity);

            return retVal;
        }
Exemple #25
0
        /// <summary>
        /// Suspends all active assignments.
        /// </summary>
        /// <param name="wf">The wf.</param>
        private static void SuspendAllActiveAssignments(WorkflowInstanceEntity wf)
        {
            foreach (AssignmentEntity assignment in BusinessManager.List(AssignmentEntity.ClassName, new FilterElement[]
                            {
                                FilterElement.EqualElement(AssignmentEntity.FieldState, (int)AssignmentState.Active),
                                FilterElement.EqualElement(AssignmentEntity.FieldWorkflowInstanceId, wf.PrimaryKeyId.Value)
                            }))
            {
                assignment.State = (int)AssignmentState.Suspended;

                BusinessManager.Update(assignment);
            }
        }
Exemple #26
0
        private void ProcessCollection(DataTable dt, EntityObject[] assignments, WorkflowInstanceEntity wfEntity, int level)
        {
            level++;

            foreach (AssignmentEntity assignment in assignments)
            {
                DataRow row = dt.NewRow();
                row["AssignmentId"] = assignment.PrimaryKeyId.ToString();
                row["Subject"] = assignment.Subject;
                if (assignment.UserId.HasValue)
                    row["User"] = CommonHelper.GetUserStatus(assignment.UserId.Value);
                row["State"] = CHelper.GetResFileString(MetaEnum.GetFriendlyName(MetaDataWrapper.GetEnumByName("AssignmentState"), assignment.State));
                if (assignment.ExecutionResult.HasValue)
                    row["Result"] = assignment.ExecutionResult.Value;
                if (assignment.ActualFinishDate.HasValue)
                    row["FinishDate"] = String.Concat(assignment.ActualFinishDate.Value.ToShortDateString(), " ", assignment.ActualFinishDate.Value.ToShortTimeString());
                row["Comment"] = CHelper.ParseText(assignment.Comment, true, true, false);
                row["Indent"] = (level - 1) * indentSize;
                if (assignment.ClosedBy.HasValue)
                {
                    row["ClosedBy"] = CommonHelper.GetUserStatus(assignment.ClosedBy.Value);
                }
                dt.Rows.Add(row);

                // Filter:
                //	1: WorkflowInstanceId = wfEntity.PrimaryKeyId,
                //	2: ParentAssignmentId = assignment.PrimaryKeyId
                FilterElementCollection fec = new FilterElementCollection();
                fec.Add(FilterElement.EqualElement(AssignmentEntity.FieldWorkflowInstanceId, wfEntity.PrimaryKeyId.Value));
                fec.Add(FilterElement.EqualElement(AssignmentEntity.FieldParentAssignmentId, assignment.PrimaryKeyId.Value));

                // Sorting
                SortingElementCollection sec = new SortingElementCollection();
                sec.Add(new SortingElement(AssignmentEntity.FieldCreated, SortingElementType.Asc));

                EntityObject[] children = BusinessManager.List(AssignmentEntity.ClassName, fec.ToArray(), sec.ToArray());

                ProcessCollection(dt, children, wfEntity, level);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ObjectId != PrimaryKeyId.Empty)
                instance = (WorkflowInstanceEntity)BusinessManager.Load(WorkflowInstanceEntity.ClassName, ObjectId);

            if (!IsPostBack)
                BindData();

            BindCommands();
            BindToolbar();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ObjectId != PrimaryKeyId.Empty)
                instance = (WorkflowInstanceEntity)BusinessManager.Load(WorkflowInstanceEntity.ClassName, ObjectId);
            else if (FromId != PrimaryKeyId.Empty)
                instance = (WorkflowInstanceEntity)BusinessManager.Load(WorkflowInstanceEntity.ClassName, FromId);

            if (!IsPostBack)
            {
                BindInfo();
                BindData();

                // disable Save button for completed business-process
                if (ObjectId != PrimaryKeyId.Empty && instance.State == (int)BusinessProcessState.Closed)
                    SaveButton.Disabled = true;
            }

            LoadControlToPlaceHolder(!IsPostBack);

            BindToolbar();
        }
        /// <summary>
        /// Gets the user list.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        private int[] GetUserList(WorkflowInstanceEntity entity)
        {
            object workflowRoot = McWorkflowSerializer.GetObject(entity.Xaml);

            return WorkflowActivityWrapper.GetActivityUserList(workflowRoot);
        }
        /// <summary>
        /// Loads the file library acl.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        private bool LoadFileLibraryAcl(WorkflowInstanceEntity entity, out FileStorage fs, out AccessControlList acl)
        {
            acl = null;
            fs = null;

            // Resolve ContainerKey
            string containerName = "FileLibrary";
            string containerKey = string.Empty;

            if (entity.OwnerDocumentId.HasValue)
                containerKey = UserRoleHelper.CreateDocumentContainerKey(entity.OwnerDocumentId.Value);
            //else
            // TODO: Extend Owner Processing

            // Check ContainerKey
            if (string.IsNullOrEmpty(containerKey))
                return false;

            // Open ACL
            BaseIbnContainer bic = BaseIbnContainer.Create(containerName, containerKey);
            fs = (FileStorage)bic.LoadControl("FileStorage");

            acl = AccessControlList.GetACL(fs.Root.Id);

            return true;
        }
        protected void SaveButton_ServerClick(object sender, EventArgs e)
        {
            if (ObjectId != PrimaryKeyId.Empty)	// edit
            {
                // Step 1. Modify instance
                instance.Name = NameText.Text.Trim();
                instance.Description = DescriptionText.Text;

                // Step 2. Create WorkflowChangesScope
                using (WorkflowChangesScope scope = WorkflowChangesScope.Create(instance))
                {
                    SchemaMaster currentShemaMaster = SchemaManager.GetShemaMaster(instance.SchemaId);

                    WorkflowDescription wfDescription = new WorkflowDescription((Guid)instance.PrimaryKeyId.Value, instance.Name, currentShemaMaster, scope.TransientWorkflow);

                    // Step 3. Apply Modifications
                    if (MainPlaceHolder.Controls.Count > 0)
                    {
                        MCDataSavedControl control = (MCDataSavedControl)MainPlaceHolder.Controls[0];
                        control.Save(wfDescription);
                    }

                    instance.PlanFinishTimeType = (int)wfDescription.PlanFinishTimeType;
                    if (wfDescription.PlanFinishTimeType == TimeType.Duration)
                        instance.PlanDuration = wfDescription.PlanDuration;
                    else if (wfDescription.PlanFinishTimeType == TimeType.DateTime)
                        instance.PlanFinishDate = wfDescription.PlanFinishDate;

                    // Lock Library
                    WorkflowParameters.SetOwnerReadOnly(instance, LockLibraryCheckBox.Checked);

                    // Overdue Action
                    WorkflowParameters.SetAssignmentOverdueAction(instance, (AssignmentOverdueAction)int.Parse(AssignmentOverdueActionList.SelectedValue));

                    // Step 4. Accept Changes
                    scope.ApplyWorkflowChanges();
                }
            }
            else // new or duplicate
            {
                // Step 1. Initialize a new instance
                instance = BusinessManager.InitializeEntity<WorkflowInstanceEntity>(WorkflowInstanceEntity.ClassName);
                instance.Name = NameText.Text.Trim();
                instance.Description = DescriptionText.Text;

                if (!String.IsNullOrEmpty(OwnerName) && OwnerId > 0)
                    instance[OwnerName] = OwnerId;

                instance.SchemaId = new Guid(SchemaMasterList.SelectedValue);

                SchemaMaster currentShemaMaster = SchemaManager.GetShemaMaster(instance.SchemaId);
                object rootActivity = currentShemaMaster.InstanceFactory.CreateInstance();

                instance.Xaml = McWorkflowSerializer.GetString(rootActivity);

                // Step 2. Create WorkflowChangesScope
                WorkflowDescription wfDescription = new WorkflowDescription(instance.Name, currentShemaMaster, rootActivity);

                // Step 3. Apply Modifications
                if (MainPlaceHolder.Controls.Count > 0)
                {
                    MCDataSavedControl control = (MCDataSavedControl)MainPlaceHolder.Controls[0];
                    control.Save(wfDescription);
                }

                instance.PlanFinishTimeType = (int)wfDescription.PlanFinishTimeType;
                if (wfDescription.PlanFinishTimeType == TimeType.Duration)
                    instance.PlanDuration = wfDescription.PlanDuration;
                else if (wfDescription.PlanFinishTimeType == TimeType.DateTime)
                    instance.PlanFinishDate = wfDescription.PlanFinishDate;

                // Lock Library
                WorkflowParameters.SetOwnerReadOnly(instance, LockLibraryCheckBox.Checked);

                // Overdue Action
                WorkflowParameters.SetAssignmentOverdueAction(instance, (AssignmentOverdueAction)int.Parse(AssignmentOverdueActionList.SelectedValue));

                // Step 4. Accept Changes
                instance.Xaml = McWorkflowSerializer.GetString(wfDescription.TransientWorkflow);
                BusinessManager.Create(instance);
            }

            // Final Redirect
            RedirectToOwner();
        }