/*
 * JBoss, Home of Professional Open Source
 * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @authors tag. All rights reserved.
 */
package org.jboss.planet.service;

import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Named;
import javax.persistence.NoResultException;
import javax.persistence.TemporalType;

import org.jboss.planet.model.Post;
import org.jboss.planet.model.PostStatus;
import org.jboss.planet.model.RemoteFeed;

/**
 * Service related to {@link Post} entity
 *
 * @author Libor Krzyzanek
 */
@Named
@Stateless
public class PostService extends EntityServiceJpa<Post> {

	public PostService() {
		super(Post.class);
	}

	/**
	 * Find post based on its titleAsId
	 *
	 * @param titleAsId
	 * @return post or null
	 */
	public Post find(String titleAsId) {
		try {
			return (Post) getEntityManager().createQuery("select post from Post post WHERE post.titleAsId = ?1")
					.setParameter(1, titleAsId).getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}

	/**
	 * Find posts based on its status
	 *
	 * @param status
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Integer> find(PostStatus status) {
		return getEntityManager().createQuery("select post.id from Post post WHERE post.status = ?1")
				.setParameter(1, status).getResultList();
	}

	/**
	 * Find posts based on input parameters
	 *
	 * @param feed
	 * @param title
	 * @param published only date is taken. time is ignored.
	 * @return list of posts
	 */
	@SuppressWarnings("unchecked")
	public List<Post> find(RemoteFeed feed, String title, Date published) {
		return getEntityManager()
				.createQuery("select p from Post p WHERE p.feed = ?1 and date(p.published) = ?2 and p.title = ?3")
				.setParameter(1, feed).setParameter(2, published, TemporalType.DATE).setParameter(3, title)
				.getResultList();
	}

	/**
	 * Update status of all posts belonging to specified feed.
	 *
	 * @param status
	 * @param feed
	 * @return number of affected posts
	 */
	public int updateStatus(PostStatus status, RemoteFeed feed) {
		return getEntityManager()
				.createQuery("update Post set status = :status where feed = :feed")
				.setParameter("status", status)
				.setParameter("feed", feed)
				.executeUpdate();
	}

}

@@ -1495,44 +1495,32 @@
 hed 
-only date is taken. time is ignored.
+posts within 1 day range
 %0A%09 *
@@ -1738,19 +1738,68 @@
 d = 
-?1 and date
+:feed and ADDDATE(p.published, -1) %3C= :published and ADDDATE
 (p.p
@@ -1810,14 +1810,26 @@
 shed
+, 1
 ) 
+%3E
 = 
-?2
+:published
  and
@@ -1843,10 +1843,14 @@
 e = 
-?3
+:title
 %22)%0A%09
@@ -1870,16 +1870,26 @@
 ter(
-1
+%22feed%22
 , feed)
+%0A%09%09%09%09
 .set
@@ -1898,17 +1898,27 @@
 rameter(
-2
+%22published%22
 , publis
@@ -1940,16 +1940,21 @@
 pe.DATE)
+%0A%09%09%09%09
 .setPara
@@ -1959,17 +1959,23 @@
 rameter(
-3
+%22title%22
 , title)

Increase range for finding same post to +- 1 day instead of date with no time comparison. fixes #ORG-1991