<commit_before>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.model.ArticleTable;
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;

import java.util.Optional;

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);

    ArticleTable article = ingestion.getArticle();
    Optional<ArticleRevision> previousLatest = articleCrudService.getLatestRevision(article);

    ArticleRevision newRevision = 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;
        });

    if (!previousLatest.isPresent() || previousLatest.get().getRevisionNumber() <= newRevision.getRevisionNumber()) {
      refreshForLatestRevision(newRevision);
    }

    return newRevision;
  }

  @Override
  public void deleteRevision(ArticleRevisionIdentifier revisionId) {<commit_msg>DPRO-2829: Refactor duplication out of if-else<commit_after>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.model.ArticleTable;
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;

import java.util.Optional;

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);

    ArticleTable 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;
  }

  @Override
  public void deleteRevision(ArticleRevisionIdentifier revisionId) {
{'fraction_non_alphanumeric': 0.08388783269961977, 'fraction_numerical': 0.0014258555133079848, 'mean_word_length': 5.607535321821036, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5531853', 'n_tokens_mistral': 1218, 'n_tokens_neox': 1083, 'n_words': 169}