<commit_before>/*
 * Copyright (c) 2006-2012 by Public Library of Science
 * http://plos.org
 * http://ambraproject.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.ambraproject.rhino.service.impl;

import com.google.common.base.Preconditions;
import org.ambraproject.rhino.identity.ArticleIdentifier;
import org.ambraproject.rhino.identity.VolumeIdentifier;
import org.ambraproject.rhino.model.ArticleTable;
import org.ambraproject.rhino.model.Issue;
import org.ambraproject.rhino.model.Journal;
import org.ambraproject.rhino.model.Volume;
import org.ambraproject.rhino.rest.RestClientException;
import org.ambraproject.rhino.service.ArticleCrudService;
import org.ambraproject.rhino.service.JournalCrudService;
import org.ambraproject.rhino.service.VolumeCrudService;
import org.ambraproject.rhino.util.response.EntityTransceiver;
import org.ambraproject.rhino.util.response.Transceiver;
import org.ambraproject.rhino.view.journal.VolumeInputView;
import org.ambraproject.rhino.view.journal.VolumeOutputView;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;

import java.io.IOException;
import java.util.Optional;

@SuppressWarnings("JpaQlInspection")
public class VolumeCrudServiceImpl extends AmbraService implements VolumeCrudService {

  @Autowired
  private JournalCrudService journalCrudService;
  @Autowired
  private ArticleCrudService articleCrudService;
  @Autowired
  private VolumeOutputView.Factory volumeOutputViewFactory;

  @Override
  public Volume readVolume(VolumeIdentifier volumeId) {
    return getVolume(volumeId).orElseThrow(() ->
        new RestClientException("Volume not found with DOI: " + volumeId.getDoi(), HttpStatus.NOT_FOUND));
  }

  @Override
  public Optional<Volume> getVolume(VolumeIdentifier volumeId) {
    return Optional.ofNullable(hibernateTemplate.execute(session -> {
      Query query = session.createQuery("FROM Volume WHERE doi = :doi");
      query.setParameter("doi", volumeId.getDoi().getName());
      return (Volume) query.uniqueResult();
    }));
  }

  @Override
  public Volume readVolumeByIssue(Issue issue) {
    return hibernateTemplate.execute(session -> {
      Query query = session.createQuery("FROM Volume WHERE :issue IN ELEMENTS(issues)");
      query.setParameter("issue", issue);
      return (Volume) query.uniqueResult();
    });
  }

  @Override
  public Volume create(String journalKey, VolumeInputView input) {
    Preconditions.checkNotNull(journalKey);

    VolumeIdentifier volumeId = VolumeIdentifier.create(input.getDoi());
    if (getVolume(volumeId).isPresent()) {
      throw new RestClientException("Volume already exists with DOI: "
          + volumeId.getDoi(), HttpStatus.BAD_REQUEST);
    }

    Volume volume = applyInput(new Volume(), input);
    Journal journal = journalCrudService.readJournal(journalKey);
    journal.getVolumes().add(volume);
    hibernateTemplate.save(journal);

    return volume;
  }

  @Override
  public Volume update(VolumeIdentifier volumeId, VolumeInputView input) {
    Preconditions.checkNotNull(input);
    Volume volume = readVolume(volumeId);
    volume = applyInput(volume, input);
    hibernateTemplate.update(volume);
    return volume;
  }<commit_msg>Fix bug on reporting result of volume creation<commit_after>/*
 * Copyright (c) 2006-2012 by Public Library of Science
 * http://plos.org
 * http://ambraproject.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.ambraproject.rhino.service.impl;

import com.google.common.base.Preconditions;
import org.ambraproject.rhino.identity.ArticleIdentifier;
import org.ambraproject.rhino.identity.VolumeIdentifier;
import org.ambraproject.rhino.model.ArticleTable;
import org.ambraproject.rhino.model.Issue;
import org.ambraproject.rhino.model.Journal;
import org.ambraproject.rhino.model.Volume;
import org.ambraproject.rhino.rest.RestClientException;
import org.ambraproject.rhino.service.ArticleCrudService;
import org.ambraproject.rhino.service.JournalCrudService;
import org.ambraproject.rhino.service.VolumeCrudService;
import org.ambraproject.rhino.util.response.EntityTransceiver;
import org.ambraproject.rhino.util.response.Transceiver;
import org.ambraproject.rhino.view.journal.VolumeInputView;
import org.ambraproject.rhino.view.journal.VolumeOutputView;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Optional;

@SuppressWarnings("JpaQlInspection")
public class VolumeCrudServiceImpl extends AmbraService implements VolumeCrudService {

  @Autowired
  private JournalCrudService journalCrudService;
  @Autowired
  private ArticleCrudService articleCrudService;
  @Autowired
  private VolumeOutputView.Factory volumeOutputViewFactory;

  @Override
  public Volume readVolume(VolumeIdentifier volumeId) {
    return getVolume(volumeId).orElseThrow(() ->
        new RestClientException("Volume not found with DOI: " + volumeId.getDoi(), HttpStatus.NOT_FOUND));
  }

  @Override
  public Optional<Volume> getVolume(VolumeIdentifier volumeId) {
    return Optional.ofNullable(hibernateTemplate.execute(session -> {
      Query query = session.createQuery("FROM Volume WHERE doi = :doi");
      query.setParameter("doi", volumeId.getDoi().getName());
      return (Volume) query.uniqueResult();
    }));
  }

  @Override
  public Volume readVolumeByIssue(Issue issue) {
    return hibernateTemplate.execute(session -> {
      Query query = session.createQuery("FROM Volume WHERE :issue IN ELEMENTS(issues)");
      query.setParameter("issue", issue);
      return (Volume) query.uniqueResult();
    });
  }

  @Override
  public Volume create(String journalKey, VolumeInputView input) {
    Preconditions.checkNotNull(journalKey);

    VolumeIdentifier volumeId = VolumeIdentifier.create(input.getDoi());
    if (getVolume(volumeId).isPresent()) {
      throw new RestClientException("Volume already exists with DOI: "
          + volumeId.getDoi(), HttpStatus.BAD_REQUEST);
    }

    Volume volume = new Volume();
    volume.setIssues(new ArrayList<>(0));
    volume = applyInput(volume, input);
    Journal journal = journalCrudService.readJournal(journalKey);
    journal.getVolumes().add(volume);
    hibernateTemplate.save(journal);

    return volume;
  }

  @Override
  public Volume update(VolumeIdentifier volumeId, VolumeInputView input) {
    Preconditions.checkNotNull(input);
    Volume volume = readVolume(volumeId);
    volume = applyInput(volume, input);
    hibernateTemplate.update(volume);
    return volume;
  }
{'fraction_non_alphanumeric': 0.09704586680487173, 'fraction_numerical': 0.0032391811350090697, 'mean_word_length': 4.847727272727273, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '378485', 'n_tokens_mistral': 2205, 'n_tokens_neox': 2120, 'n_words': 525}