/*
 * $HeadURL$
 * $Id$
 * Copyright (c) 2006-2013 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.0Unless 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 org.ambraproject.models.Article;
import org.ambraproject.rhino.content.ArticleState;
import org.ambraproject.rhino.identity.ArticleIdentity;
import org.ambraproject.rhino.rest.MetadataFormat;
import org.ambraproject.rhino.service.ArticleStateService;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.support.DataAccessUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * {@inheritDoc}
 */
public class ArticleStateServiceImpl extends AmbraService implements ArticleStateService {

  /**
   * {@inheritDoc}
   */
  @Override
  public void read(HttpServletResponse response, ArticleIdentity articleId, MetadataFormat format)
      throws IOException {
    Article article = loadArticle(articleId);
    ArticleState state = new ArticleState();
    state.setPublished(article.getState() == 0);

    assert format == MetadataFormat.JSON;
    writeJsonToResponse(response, state);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void write(ArticleIdentity articleId, ArticleState state) {
    Article article = loadArticle(articleId);
    article.setState(state.isPublished() ? 0 : 1);
    hibernateTemplate.update(article);
  }

  private Article loadArticle(ArticleIdentity articleId) {
    return (Article) DataAccessUtils.uniqueResult((List<?>)
        hibernateTemplate.findByCriteria(DetachedCriteria
            .forClass(Article.class)
            .add(Restrictions.eq("doi", articleId.getKey()))
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
        ));
  }
}

@@ -901,16 +901,72 @@
 Format;%0A
+import org.ambraproject.rhino.rest.RestClientException;%0A
 import o
@@ -1196,16 +1196,60 @@
 ssUtils;
+%0Aimport org.springframework.http.HttpStatus;
 %0A%0Aimport
@@ -2177,14 +2177,24 @@
     
-return
+Article result =
  (Ar
@@ -2472,14 +2472,185 @@
     ));%0A
+    if (result == null) %7B%0A      throw new RestClientException(%22Article not found: %22 + articleId.getIdentifier(),%0A          HttpStatus.NOT_FOUND);%0A    %7D%0A    return result;%0A
   %7D%0A%7D%0A

return 404, instead of NullPointerException, if article is not found.