use of org.ambraproject.rhino.model.ArticleIngestion in project rhino by PLOS.
the class ArticleRevisionWriteServiceImpl method createRevision.
@Override
public ArticleRevision createRevision(ArticleIngestionIdentifier ingestionId) {
ArticleIngestion ingestion = articleCrudService.readIngestion(ingestionId);
Optional<ArticleRevision> previousLatest = articleCrudService.getLatestRevision(ingestion.getArticle());
int newRevisionNumber = 1 + previousLatest.map(ArticleRevision::getRevisionNumber).orElse(0);
ArticleRevision revision = new ArticleRevision();
revision.setIngestion(ingestion);
revision.setRevisionNumber(newRevisionNumber);
hibernateTemplate.save(revision);
refreshForLatestRevision(revision);
return revision;
}
use of org.ambraproject.rhino.model.ArticleIngestion in project rhino by PLOS.
the class IngestibleZipController method zipUpload.
/**
* Create an article based on a POST containing an article .zip archive file.
*
* @param requestFile body of the archive param, with the encoded article .zip file
* @throws java.io.IOException
*/
@Transactional(rollbackFor = { Throwable.class })
@RequestMapping(value = "/articles", method = RequestMethod.POST)
public ResponseEntity<?> zipUpload(@RequestParam(value = "archive", required = true) MultipartFile requestFile, @RequestParam(value = "bucket", required = false) String bucket) throws IOException {
String ingestedFileName = requestFile.getOriginalFilename();
ArticleIngestion ingestion;
try (InputStream requestInputStream = requestFile.getInputStream();
Archive archive = Archive.readZipFile(ingestedFileName, requestInputStream)) {
ingestion = ingestionService.ingest(archive, Optional.ofNullable(bucket));
} catch (ManifestXml.ManifestDataException e) {
throw new RestClientException("Invalid manifest: " + e.getMessage(), HttpStatus.BAD_REQUEST, e);
}
// Report the written data, as JSON, in the response.
ArticleIngestionView view = articleIngestionViewFactory.getView(ingestion);
return ServiceResponse.reportCreated(view).asJsonResponse(entityGson);
}
use of org.ambraproject.rhino.model.ArticleIngestion in project rhino by PLOS.
the class ArticleCrudServiceImpl method getItemOverview.
@Override
public Optional<ResolvedDoiView> getItemOverview(Doi doi) {
return hibernateTemplate.execute(session -> {
Query ingestionQuery = session.createQuery("FROM ArticleItem WHERE doi = :doi");
ingestionQuery.setParameter("doi", doi.getName());
List<ArticleItem> items = ingestionQuery.list();
if (items.isEmpty())
return Optional.empty();
ResolvedDoiView.DoiWorkType type = items.stream().allMatch(ArticleCrudServiceImpl::isMainArticleItem) ? ResolvedDoiView.DoiWorkType.ARTICLE : ResolvedDoiView.DoiWorkType.ASSET;
ArticleIdentifier articleId = Iterables.getOnlyElement(items.stream().map(item -> ArticleIdentifier.create(item.getIngestion().getArticle().getDoi())).collect(Collectors.toSet()));
Query revisionQuery = session.createQuery("" + "FROM ArticleRevision WHERE ingestion IN " + " (SELECT ingestion FROM ArticleItem WHERE doi = :doi)");
revisionQuery.setParameter("doi", doi.getName());
List<ArticleRevision> revisions = revisionQuery.list();
Collection<ArticleIngestion> ingestions = Collections2.transform(items, ArticleItem::getIngestion);
ArticleOverview articleOverview = ArticleOverview.build(articleId, ingestions, revisions);
return Optional.of(ResolvedDoiView.createForArticle(doi, type, articleOverview));
});
}
use of org.ambraproject.rhino.model.ArticleIngestion in project rhino by PLOS.
the class ArticleRevisionWriteServiceImpl method writeRevision.
@Override
public ArticleRevision writeRevision(ArticleRevisionIdentifier revisionId, ArticleIngestionIdentifier ingestionId) {
Preconditions.checkArgument(revisionId.getArticleIdentifier().equals(ingestionId.getArticleIdentifier()));
ArticleIngestion ingestion = articleCrudService.readIngestion(ingestionId);
Article article = ingestion.getArticle();
Optional<ArticleRevision> previousLatest = articleCrudService.getLatestRevision(article);
ArticleRevision newRevision = articleCrudService.getRevision(revisionId).orElseGet(() -> {
ArticleRevision revision = new ArticleRevision();
revision.setRevisionNumber(revisionId.getRevision());
return revision;
});
newRevision.setIngestion(ingestion);
hibernateTemplate.saveOrUpdate(newRevision);
if (!previousLatest.isPresent() || previousLatest.get().getRevisionNumber() <= newRevision.getRevisionNumber()) {
refreshForLatestRevision(newRevision);
}
return newRevision;
}
use of org.ambraproject.rhino.model.ArticleIngestion in project rhino by PLOS.
the class ArticleCrudServiceImpl method repack.
@Override
public Archive repack(ArticleIngestionIdentifier ingestionId) {
ArticleIngestion ingestion = readIngestion(ingestionId);
List<ArticleFile> files = hibernateTemplate.execute(session -> {
Query query = session.createQuery("FROM ArticleFile WHERE ingestion = :ingestion");
query.setParameter("ingestion", ingestion);
return (List<ArticleFile>) query.list();
});
Map<String, ByteSource> archiveMap = files.stream().collect(Collectors.toMap(ArticleFile::getIngestedFileName, (ArticleFile file) -> new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return contentRepoService.getRepoObject(file.getCrepoVersion());
}
}));
return Archive.pack(extractFilenameStub(ingestionId.getDoiName()) + ".zip", archiveMap);
}
Aggregations