package org.ambraproject.rhino.service.impl;

import com.google.common.base.Preconditions;
import org.ambraproject.rhino.identity.ArticleIngestionIdentifier;
import org.ambraproject.rhino.identity.ArticleRevisionIdentifier;
import org.ambraproject.rhino.model.ArticleIngestion;
import org.ambraproject.rhino.model.ArticleRevision;
import org.ambraproject.rhino.service.ArticleCrudService;
import org.ambraproject.rhino.service.ArticleRevisionWriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class ArticleRevisionWriteServiceImpl implements ArticleRevisionWriteService {

  @Autowired
  private ArticleCrudService articleCrudService;
  @Autowired
  private HibernateTemplate hibernateTemplate;

  @Override
  public ArticleRevision writeRevision(ArticleRevisionIdentifier revisionId, ArticleIngestionIdentifier ingestionId) {
    Preconditions.checkArgument(revisionId.getArticleIdentifier().equals(ingestionId.getArticleIdentifier()));
    ArticleIngestion ingestion = articleCrudService.readIngestion(ingestionId);
    return articleCrudService.getRevision(revisionId)
        .map((ArticleRevision revision) -> {
          revision.setIngestion(ingestion);
          hibernateTemplate.update(revision);
          return revision;
        })
        .orElseGet(() -> {
          ArticleRevision revision = new ArticleRevision();
          revision.setIngestion(ingestion);
          revision.setRevisionNumber(revisionId.getRevision());
          hibernateTemplate.save(revision);
          return revision;
        });
  }

  @Override
  public void deleteRevision(ArticleRevisionIdentifier revisionId) {
    ArticleRevision revision = articleCrudService.readRevision(revisionId);
    hibernateTemplate.delete(revision);
  }

}

@@ -320,24 +320,74 @@
 leRevision;%0A
+import org.ambraproject.rhino.model.ArticleTable;%0A
 import org.a
@@ -624,16 +624,44 @@
 plate;%0A%0A
+import java.util.Optional;%0A%0A
 public c
@@ -1184,26 +1184,196 @@
 ionId);%0A
+%0A
     
-return
+ArticleTable article = ingestion.getArticle();%0A    Optional%3CArticleRevision%3E previousLatest = articleCrudService.getLatestRevision(article);%0A%0A    ArticleRevision newRevision =
  article
@@ -1863,209 +1863,1021 @@
 %7D);%0A
-  %7D%0A%0A  @Override%0A  public void deleteRevision(ArticleRevisionIdentifier revisionId) %7B%0A    ArticleRevision revision = articleCrudService.readRevision(revisionId);%0A    hibernateTemplate.delete(revision);
+%0A    if (!previousLatest.isPresent() %7C%7C previousLatest.get().getRevisionNumber() %3C= newRevision.getRevisionNumber()) %7B%0A      refreshForLatestRevision(newRevision);%0A    %7D%0A%0A    return newRevision;%0A  %7D%0A%0A  @Override%0A  public void deleteRevision(ArticleRevisionIdentifier revisionId) %7B%0A    ArticleRevision revision = articleCrudService.readRevision(revisionId);%0A    ArticleTable article = revision.getIngestion().getArticle();%0A%0A    ArticleRevision latestRevision = articleCrudService.getLatestRevision(article)%0A        .orElseThrow(RuntimeException::new); // should be guaranteed to exist because at least one revision exists%0A    boolean deletingLatest = latestRevision.equals(revision);%0A%0A    hibernateTemplate.delete(revision);%0A%0A    if (deletingLatest) %7B%0A      articleCrudService.getLatestRevision(article)%0A          .ifPresent(this::refreshForLatestRevision);%0A      // else, we deleted the only revision%0A    %7D%0A  %7D%0A%0A  private void refreshForLatestRevision(ArticleRevision newlyLatestRevision) %7B%0A    // TODO: Implement
 %0A  %7D

Check if a new latest revision is being written