Example #1
0
        public async Task <ResourceWriteResultCTO> PublishResource(Uri pidUri)
        {
            CheckIfResourceExist(pidUri);

            using (var lockService = _lockServiceFactory.CreateLockService())
            {
                await lockService.CreateLockAsync(pidUri.ToString());

                var resourcesCTO = GetResourcesByPidUri(pidUri);

                if (resourcesCTO.HasPublishedAndNoDraft)
                {
                    throw new BusinessException("The resource has already been published");
                }

                var requestResource = _mapper.Map <ResourceRequestDTO>(resourcesCTO.Draft);
                var draftId         = resourcesCTO.Draft.Id;

                var(validationResult, failed, validationFacade) =
                    await _resourcePreprocessService.ValidateAndPreProcessResource(draftId, requestResource,
                                                                                   resourcesCTO, ResourceCrudAction.Publish);

                HandleValidationFailures(resourcesCTO.Draft, draftId, validationResult, failed, validationFacade);

                string entityType = resourcesCTO.GetDraftOrPublishedVersion().Properties
                                    .GetValueOrNull(Graph.Metadata.Constants.RDF.Type, true).ToString();
                var metadata = _metadataService.GetMetadataForEntityType(entityType);

                using (var transaction = _resourceRepository.CreateTransaction())
                {
                    // Try to delete draft and all inbound edges are changed to the new entry.
                    _resourceRepository.DeleteDraft(validationFacade.RequestResource.PidUri,
                                                    new Uri(validationFacade.RequestResource.Id));

                    // Try to delete published and all inbound edges are changed to the new entry.
                    _resourceRepository.DeletePublished(validationFacade.RequestResource.PidUri,
                                                        new Uri(validationFacade.RequestResource.Id));
                    _identifierService.DeleteAllUnpublishedIdentifiers(resourcesCTO.Draft);

                    _resourceRepository.Create(validationFacade.RequestResource, metadata);

                    if (resourcesCTO.HasPublished)
                    {
                        // This logic is implicit and the order is important. Creating the inbound links for the historic versions
                        // and creating the link to the latest historic versions both base on successful creation in method CreateHistoric.
                        _historyResourceService.CreateHistoricResource((Resource)resourcesCTO.Published, metadata);

                        // all inbound links of new published, link to historic id
                        _historyResourceService.CreateInboundLinksForHistoricResource((Resource)resourcesCTO.Published);
                        _resourceRepository.CreateLinkOnLatestHistorizedResource(pidUri);
                    }

                    transaction.Commit();

                    await _remoteAppDataService.NotifyResourcePublished(validationFacade.RequestResource);

                    _indexingService.SendResourcePublished(validationFacade.RequestResource, validationFacade.ResourcesCTO.Published, metadata);
                }

                return(new ResourceWriteResultCTO(validationFacade.RequestResource, validationResult));
            }
        }