Esempio n. 1
0
        /// <summary>
        /// Sends the document to the target user id user.
        /// </summary>
        /// <param name="workflowOperation"></param>
        public void ForwardTo(WorkflowOperation workflowOperation)
        {
            var configuration  = documentWorkflowConfigurationService.Get(workflowOperation.WorkflowId);
            var accessProvider = unityContainer.Resolve <IDocumentWorkflowAccessProvider>(configuration.AccessProviderName);

            if (workflowOperation.OperationType == WorkflowOperationType.User)
            {
                // Add path to forwarded user
                var workflow = documentWorkflowUserService.Get(workflowOperation.TargetUserId);

                if (workflow == null)
                {
                    throw new DocumentWorkflowException("workflow is null");
                }

                var targetStructure = fileStructureService.GetByInstanceDataGuid(workflow.Guid);

                if (targetStructure == null)
                {
                    throw new DocumentWorkflowException("targetStructure is null");
                }

                var existingStructures = fileStructureDocumentPathService.GetByDocumentId(workflowOperation.DocumentId)
                                         .ToList();

                if (existingStructures == null)
                {
                    throw new DocumentWorkflowException("existingStructures is null");
                }

                var targetPath = existingStructures.FirstOrDefault(x => x.FileStructureGuid == targetStructure.Id);

                if (targetPath != null)
                {
                    var returnFolder = GetReturnDirectory(targetPath.FileStructureGuid, targetPath.WorkflowId.Value);
                    targetPath.DirectoryGuid = returnFolder.Id;
                    targetPath.WorkflowState = DocumentWorkflowStateType.InReview;
                }

                else
                {
                    var firstDirectory = FindWorkflowDirectory(
                        targetStructure,
                        workflowOperation.WorkflowId,
                        workflowOperation.DocumentId,
                        workflowOperation.TargetUserId);

                    if (firstDirectory == null)
                    {
                        throw new DocumentWorkflowException("existingStructures is null");
                    }

                    targetPath = new FileStructureDocumenPath
                    {
                        DirectoryGuid     = firstDirectory.Id,
                        FileStructureGuid = targetStructure.Id,
                        Id              = Guid.NewGuid(),
                        DocumentGuid    = workflowOperation.DocumentId,
                        WorkflowId      = workflowOperation.WorkflowId,
                        IsProtectedPath = false,
                        WorkflowState   = DocumentWorkflowStateType.InReview
                    };
                }

                var tracker = new DocumentWorkflowTracker
                {
                    ActionName     = DocumentWorkflowStateType.Forwarded,
                    CreateDateTime = DateTime.Now,
                    DocumentId     = targetPath.DocumentGuid,
                    TargetUserId   = workflowOperation.TargetUserId,
                    UserId         = workflowOperation.UserId
                };

                documentWorkflowTrackerService.Save(tracker);
                fileStructureDocumentPathService.Save(targetPath);

                if (accessProvider != null)
                {
                    accessProvider.SetUserAccess(workflowOperation.TargetUserId, workflowOperation.DocumentId, targetPath.Id, targetStructure.Id, configuration);
                }
            }
            else
            {
                SaveWorkflowOrganizationUnitAssignment(workflowOperation, configuration.StateProviderName);
                TrackChanges(workflowOperation, DocumentWorkflowStateType.Forwarded);
                if (accessProvider != null && workflowOperation.WorkflowOrganizationId.HasValue)
                {
                    accessProvider.SetOrganizationUnitAcess(workflowOperation.WorkflowOrganizationId.Value, workflowOperation.DocumentId, configuration);
                }
            }

            var path = fileStructureDocumentPathService.Get(workflowOperation.DocumentPath);

            path.WorkflowState = DocumentWorkflowStateType.Completed;
            fileStructureDocumentPathService.Save(path);

            flowEventService.InvokeEvent("DocumentForwarded", workflowOperation.Guid, workflowOperation, workflowOperation.UserId);
        }
Esempio n. 2
0
        /// <summary>
        /// Save file structure to database
        /// </summary>
        /// <param name="obj">Object to save</param>
        /// <returns>True if successfull</returns>
        public bool Save(FileStructureDocumenPath obj)
        {
            var fileStructure = structureService.Get(obj.FileStructureGuid);

            obj.Path = "";

            if (fileStructure != null)
            {
                var currentItem = fileStructure.Directories.FirstOrDefault(x => x.Id == obj.DirectoryGuid);
                while (currentItem != null)
                {
                    obj.Path = obj.Path.Insert(0, $"/{currentItem.Name}");

                    if (currentItem.Parent != null)
                    {
                        currentItem = currentItem.Parent;
                    }
                    else
                    {
                        currentItem = null;
                        break;
                    }
                }
            }

            var result = repository.Save(obj);

            // Path has changed
            if (obj.PreviousPath != obj.Path || obj.PreviousWorkflowState != obj.WorkflowState)
            {
                try
                {
                    fileStructureDocumentPathTrackingRepository.Save(new FileStructureDocumenPathTracking
                    {
                        Id                = Guid.NewGuid(),
                        DirectoryGuid     = obj.DirectoryGuid,
                        DocumentGuid      = obj.DocumentGuid,
                        WorkflowState     = obj.WorkflowState,
                        FileStructureGuid = obj.FileStructureGuid,
                        FileStructureHash = obj.FileStructureHash,
                        IsProtectedPath   = obj.IsProtectedPath,
                        Path              = obj.Path ?? "",
                        StorageHash       = obj.StorageHash,
                        UserId            = sessionService.CurrentSession.UserId,
                        PreviousPath      = obj.PreviousPath ?? ""
                    });

                    if (obj.WorkflowId.HasValue)
                    {
                        var workflowId = obj.WorkflowId.Value;

                        var workflow = documentWorkflowConfigurationService.Get(workflowId);
                        if (workflow == null)
                        {
                            throw new Exception($"Could not find workflow: {workflowId}");
                        }

                        var stateProvider = unityContainer.Resolve <IDocumentWorkflowStateProvider>(workflow.StateProviderName);
                        var state         = stateProvider.ResolveDocumentWorkflowState(obj.DocumentGuid, workflowId);
                        if (state == null)
                        {
                            throw new Exception($"Could not resolve initial state for document: {obj.DocumentGuid}");
                        }

                        if (!documentWorkflowAssignmentService.Exists(obj.DocumentGuid, workflowId))
                        {
                            var assignment = new DocumentWorkflowAssignment
                            {
                                DocumentId = obj.DocumentGuid,
                                WorkflowId = workflowId,
                                StateId    = state.Guid
                            };

                            documentWorkflowAssignmentService.Save(assignment);
                        }
                        else
                        {
                            documentWorkflowAssignmentService.SetState(obj.DocumentGuid, workflowId, state.Guid);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.LogManagerInstance.Instance.Error($"Could not write file structure path tracking: Document id: {obj.DocumentGuid}", ex);
                }
            }

            return(result);
        }