public async Task <NodeEntry> ToDocument(string conceptId, string authorId, string subject, int attachmentCount, GenerateSsid ssidConfiguration, DateTime?settleTo = null) { var authorInfo = await _alfrescoHttpClient.GetPerson(authorId); if (authorInfo == null) { throw new BadRequestException("", "Provided author does not exists"); } var myNode = await _alfrescoHttpClient.GetNodeInfo(AlfrescoNames.Aliases.My); var conceptInfo = await _alfrescoHttpClient.GetNodeInfo(conceptId); // Create a copy of concept var conceptCopyInfo = await _alfrescoHttpClient.NodeCopy(conceptId, new NodeBodyCopy { TargetParentId = myNode?.Entry?.Id, Name = IdGenerator.GenerateId() }); // Copy association of components var conceptChildrens = await _nodesService.GetSecondaryChildren(conceptId, SpisumNames.Associations.Components); await conceptChildrens.ForEachAsync(async x => { // Remove old versions old file and associate with new node await _nodesService.RemoveAllVersions(x?.Entry?.Id); }); var properties = conceptInfo?.Entry?.Properties?.As <JObject>().ToDictionary(); var authorProperties = authorInfo?.Entry?.Properties?.As <JObject>().ToDictionary(); // Save properties var documentInfo = await _alfrescoHttpClient.UpdateNode(conceptCopyInfo?.Entry?.Id, new NodeBodyUpdate { NodeType = SpisumNames.NodeTypes.Document } .AddProperty(SpisumNames.Properties.Pid, null) .AddProperty(SpisumNames.Properties.Author, authorId) .AddProperty(SpisumNames.Properties.Subject, subject) .AddProperty(SpisumNames.Properties.SettleToDate, settleTo) .AddProperty(SpisumNames.Properties.AttachmentsCount, attachmentCount) .AddProperty(SpisumNames.Properties.AuthorId, authorProperties.GetNestedValueOrDefault(SpisumNames.Properties.UserId)?.ToString()) .AddProperty(SpisumNames.Properties.AuthorOrgId, authorProperties.GetNestedValueOrDefault(SpisumNames.Properties.UserOrgId)?.ToString()) .AddProperty(SpisumNames.Properties.AuthorOrgName, authorProperties.GetNestedValueOrDefault(SpisumNames.Properties.UserOrgName)?.ToString()) .AddProperty(SpisumNames.Properties.AuthorOrgUnit, authorProperties.GetNestedValueOrDefault(SpisumNames.Properties.UserOrgUnit)?.ToString()) .AddProperty(SpisumNames.Properties.AuthorJob, authorProperties.GetNestedValueOrDefault(SpisumNames.Properties.UserJob)?.ToString()) .AddProperty(SpisumNames.Properties.AuthorOrgAddress, authorProperties.GetNestedValueOrDefault(SpisumNames.Properties.UserOrgAddress)?.ToString()) .AddProperty(SpisumNames.Properties.KeepForm, SpisumNames.StoreForm.Original) .AddProperty(SpisumNames.Properties.SenderType, SpisumNames.SenderType.Own) .AddProperty(SpisumNames.Properties.Sender, SpisumNames.Other.Own) .AddProperty(SpisumNames.Properties.Sender_Name, SpisumNames.Other.Own) .AddProperty(SpisumNames.Properties.Sender_Address, SpisumNames.Other.Own) .AddProperty(SpisumNames.Properties.Sender_Contact, SpisumNames.Other.Own) .AddProperty(SpisumNames.Properties.State, SpisumNames.State.Unprocessed) .AddProperty(AlfrescoNames.ContentModel.Owner, properties.GetNestedValueOrDefault(AlfrescoNames.ContentModel.Owner, "id")?.ToString()) .AddProperty(SpisumNames.Properties.Version, 1) ); // Create permissions for the node await _nodesService.CreatePermissions(conceptCopyInfo?.Entry?.Id, _identityUser.RequestGroup, _identityUser.Id); await _nodesService.MoveByPath(conceptCopyInfo?.Entry?.Id, SpisumNames.Paths.EvidenceDocumentsForProcessing(_identityUser.RequestGroup)); // Generate Ssid var conceptEntry = await _nodesService.GenerateSsid(conceptCopyInfo?.Entry?.Id, ssidConfiguration); // Delete deleted components var deletedComponents = await _nodesService.GetSecondaryChildren(conceptId, SpisumNames.Associations.DeletedComponents); await deletedComponents.ForEachAsync(async x => { await _nodesService.DeleteNodePermanent(x?.Entry?.Id); }); // Delete concept await _nodesService.DeleteNodePermanent(conceptId); try { var conceptPid = conceptInfo?.GetPid(); // Audit log for a document await _auditLogService.Record(documentInfo?.Entry.Id, SpisumNames.NodeTypes.Document, conceptPid, NodeTypeCodes.Dokument, EventCodes.Zalozeni, TransactinoHistoryMessages.ConceptToDocument); var fileId = await _documentService.GetDocumentFileId(documentInfo?.Entry.Id); if (fileId != null) { await _auditLogService.Record(fileId, SpisumNames.NodeTypes.Document, conceptPid, NodeTypeCodes.Dokument, EventCodes.Zalozeni, TransactinoHistoryMessages.ConceptToDocument); } } catch (Exception ex) { Log.Logger?.Error(ex, "Audit log failed"); } return(conceptEntry); }
private async Task <NodeEntry> CreateArhiveAndMoveAllFiles(NodeUpdate body, string archiveFolder, string nodeType) { var copiedNodes = new List <NodeEntry>(); var updatedNodes = new List <NodeEntry>(); var connections = new List <Tuple <string, string> >(); var alfrescoBody = _mapper.Map <NodeBodyUpdate>(body); var documentInfo = await _alfrescoHttpClient.UpdateNode(body.NodeId, alfrescoBody, ImmutableList <Parameter> .Empty .Add(new Parameter(AlfrescoNames.Headers.Include, AlfrescoNames.Includes.Path, ParameterType.QueryString))); var parentFolderInfo = await _alfrescoHttpClient.GetNodeInfo(documentInfo.Entry.ParentId); // create folder in archive because copy folder violation var copyFolderInfo = await _nodesService.CreateFolder(archiveFolder); AlfrescoPagingRequest <NodeChildAssociationPagingFixed, List19Fixed, NodeChildAssociationEntry> request; // copy and remove all associations - Copies .eml/.zfo and all it's children to the archive folder var entries = (await _alfrescoHttpClient.GetNodeChildren(body.NodeId, ImmutableList <Parameter> .Empty .Add(new Parameter(AlfrescoNames.Headers.MaxItems, 1, ParameterType.QueryString)) .Add(new Parameter(AlfrescoNames.Headers.Where, $"(nodeType='{nodeType}')", ParameterType.QueryString)))) ?.List?.Entries?.ToList(); if (entries?.Count > 0) { var nodeId = entries[0]?.Entry?.Id; request = new AlfrescoPagingRequest <NodeChildAssociationPagingFixed, List19Fixed, NodeChildAssociationEntry>( parameters => _alfrescoHttpClient.GetNodeSecondaryChildren(nodeId, parameters) ); while (await request.Next()) { var response = request.Response(); if (!(response?.List?.Entries?.Count > 0)) { break; } foreach (var item in response.List.Entries.ToList()) { await _alfrescoHttpClient.DeleteSecondaryChildren(nodeId, item.Entry.Id); var copyInfo = await _alfrescoHttpClient.NodeCopy(item.Entry.Id, new NodeBodyCopy { TargetParentId = copyFolderInfo.Entry.Id }); var properties = copyInfo.Entry.Properties.As <JObject>().ToDictionary(); var pid = properties.GetNestedValueOrDefault(SpisumNames.Properties.Pid)?.ToString(); await _alfrescoHttpClient.UpdateNode(copyInfo.Entry.Id, new NodeBodyUpdate() .AddProperty(SpisumNames.Properties.Pid, null) .AddProperty(SpisumNames.Properties.PidRef, pid) .AddProperty(SpisumNames.Properties.Ref, item.Entry.Id)); copiedNodes.Add(copyInfo); } } // Copy .eml / .zfo var copyEmlZfo = await _alfrescoHttpClient.NodeCopy(nodeId, new NodeBodyCopy { TargetParentId = copyFolderInfo.Entry.Id }); var propertiesEmlZfo = copyEmlZfo.Entry.Properties.As <JObject>().ToDictionary(); var pidEmlZfo = propertiesEmlZfo.GetNestedValueOrDefault(SpisumNames.Properties.Pid)?.ToString(); await _alfrescoHttpClient.UpdateNode(copyEmlZfo?.Entry?.Id, new NodeBodyUpdate() .AddProperty(SpisumNames.Properties.Pid, null) .AddProperty(SpisumNames.Properties.PidRef, pidEmlZfo) .AddProperty(SpisumNames.Properties.Ref, nodeId)); copiedNodes.Add(copyEmlZfo); } // Take all files and move them to the new location // find all children and rename + change node type request = new AlfrescoPagingRequest <NodeChildAssociationPagingFixed, List19Fixed, NodeChildAssociationEntry>( parameters => _alfrescoHttpClient.GetNodeSecondaryChildren(body.NodeId, parameters) ); while (await request.Next()) { var response = request.Response(); if (!(response?.List?.Entries?.Count > 0)) { break; } foreach (var item in response.List.Entries.ToList()) { if (item.Entry.NodeType == SpisumNames.NodeTypes.Component) { continue; } connections.Add(new Tuple <string, string>(item.Entry.Id, copiedNodes.FirstOrDefault(x => x.Entry.Name == item.Entry.Name)?.Entry?.Id)); var updateBody = new NodeBodyUpdate(); if (item.Entry.NodeType == AlfrescoNames.ContentModel.Content) { updateBody.NodeType = SpisumNames.NodeTypes.Component; } if (item.Entry.NodeType == SpisumNames.NodeTypes.Email) { updateBody.NodeType = SpisumNames.NodeTypes.EmailComponent; } if (item.Entry.NodeType == SpisumNames.NodeTypes.DataBox) { updateBody.NodeType = SpisumNames.NodeTypes.DataBoxComponent; } var personGroup = await GetCreateUserGroup(); updateBody.Permissions = _nodesService.SetPermissions(personGroup.GroupPrefix, _identityUser.Id).Permissions; updateBody.Properties = new Dictionary <string, object> { { AlfrescoNames.ContentModel.Owner, _identityUser.Id }, { SpisumNames.Properties.Group, _identityUser.RequestGroup } }; updatedNodes.Add(await _alfrescoHttpClient.UpdateNode(item.Entry.Id, updateBody)); if (item.Entry.NodeType == AlfrescoNames.ContentModel.Content) { } } } // Set PID to archive List <NodeEntry> movedNodes = new List <NodeEntry>(); movedNodes.Add(await _nodesService.MoveByPath(body.NodeId, SpisumNames.Paths.EvidenceDocumentsForProcessing(_identityUser.RequestGroup))); movedNodes.AddRange(await _nodesService.MoveAllComponets(body.NodeId)); foreach (var copy in copiedNodes) { var id = connections.First(x => x.Item2 == copy.Entry.Id); var node = movedNodes.FirstOrDefault(x => x.Entry.Id == id.Item1); if (node == null) { continue; } var properties = node.Entry.Properties.As <JObject>().ToDictionary(); var pid = properties.GetNestedValueOrDefault(SpisumNames.Properties.Pid)?.ToString(); if (string.IsNullOrWhiteSpace(pid)) { continue; } await _alfrescoHttpClient.UpdateNode(copy.Entry.Id, new NodeBodyUpdate { Properties = new Dictionary <string, object> { { SpisumNames.Properties.PidArchive, pid } } }); } return(await _alfrescoHttpClient.GetNodeInfo(body.NodeId)); }