Search in sources :

Example 6 with ArticleIngestion

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;
}
Also used : ArticleIngestion(org.ambraproject.rhino.model.ArticleIngestion) ArticleRevision(org.ambraproject.rhino.model.ArticleRevision)

Example 7 with ArticleIngestion

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);
}
Also used : ArticleIngestion(org.ambraproject.rhino.model.ArticleIngestion) Archive(org.ambraproject.rhino.util.Archive) InputStream(java.io.InputStream) ManifestXml(org.ambraproject.rhino.content.xml.ManifestXml) RestClientException(org.ambraproject.rhino.rest.RestClientException) ArticleIngestionView(org.ambraproject.rhino.view.article.ArticleIngestionView) Transactional(org.springframework.transaction.annotation.Transactional) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with ArticleIngestion

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));
    });
}
Also used : ArticleItem(org.ambraproject.rhino.model.ArticleItem) ArticleRevision(org.ambraproject.rhino.model.ArticleRevision) ArticleIngestion(org.ambraproject.rhino.model.ArticleIngestion) ResolvedDoiView(org.ambraproject.rhino.view.ResolvedDoiView) ArticleIdentifier(org.ambraproject.rhino.identity.ArticleIdentifier) Query(org.hibernate.Query) ArticleOverview(org.ambraproject.rhino.view.article.ArticleOverview)

Example 9 with ArticleIngestion

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;
}
Also used : ArticleIngestion(org.ambraproject.rhino.model.ArticleIngestion) ArticleRevision(org.ambraproject.rhino.model.ArticleRevision) Article(org.ambraproject.rhino.model.Article)

Example 10 with ArticleIngestion

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);
}
Also used : ArticleIngestion(org.ambraproject.rhino.model.ArticleIngestion) Query(org.hibernate.Query) ByteSource(com.google.common.io.ByteSource) List(java.util.List) ArrayList(java.util.ArrayList) ArticleFile(org.ambraproject.rhino.model.ArticleFile)

Aggregations

ArticleIngestion (org.ambraproject.rhino.model.ArticleIngestion)16 Article (org.ambraproject.rhino.model.Article)7 ArticleRevision (org.ambraproject.rhino.model.ArticleRevision)7 Query (org.hibernate.Query)6 ArticleItem (org.ambraproject.rhino.model.ArticleItem)4 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 ArticleIdentifier (org.ambraproject.rhino.identity.ArticleIdentifier)3 Journal (org.ambraproject.rhino.model.Journal)3 Document (org.w3c.dom.Document)3 InputStream (java.io.InputStream)2 Optional (java.util.Optional)2 ManifestXml (org.ambraproject.rhino.content.xml.ManifestXml)2 ArticleFile (org.ambraproject.rhino.model.ArticleFile)2 RestClientException (org.ambraproject.rhino.rest.RestClientException)2 ArticleOverview (org.ambraproject.rhino.view.article.ArticleOverview)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 ImmutableList (com.google.common.collect.ImmutableList)1