set parameters for delete query

This commit is contained in:
Athou
2013-07-25 09:12:56 +02:00
parent 9bac3f424f
commit 02f1090fe7
3 changed files with 28 additions and 17 deletions

View File

@@ -15,7 +15,6 @@ import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Disjunction;
@@ -91,7 +90,7 @@ public class FeedEntryStatusDAO extends GenericDAO<FeedEntryStatus> {
private FeedEntryStatus handleStatus(FeedEntryStatus status,
FeedSubscription sub, FeedEntry entry) {
if (status == null) {
Date unreadThreshold = getUnreadThreshold();
Date unreadThreshold = applicationSettingsService.get().getUnreadThreshold();
boolean read = unreadThreshold == null ? false : entry.getUpdated()
.before(unreadThreshold);
status = new FeedEntryStatus(sub.getUser(), sub, entry);
@@ -167,7 +166,7 @@ public class FeedEntryStatusDAO extends GenericDAO<FeedEntryStatus> {
or.add(Restrictions.eq(FeedEntryStatus_.read.getName(), false));
statusJoin.add(or);
Date unreadThreshold = getUnreadThreshold();
Date unreadThreshold = applicationSettingsService.get().getUnreadThreshold();
if (unreadThreshold != null) {
criteria.add(Restrictions.ge(FeedEntry_.updated.getName(),
unreadThreshold));
@@ -250,13 +249,6 @@ public class FeedEntryStatusDAO extends GenericDAO<FeedEntryStatus> {
return lazyLoadContent(includeContent, statuses);
}
private Date getUnreadThreshold() {
int keepStatusDays = applicationSettingsService.get()
.getKeepStatusDays();
return keepStatusDays > 0 ? DateUtils.addDays(new Date(), -1
* keepStatusDays) : null;
}
@SuppressWarnings("unchecked")
public Long getUnreadCount(FeedSubscription subscription) {
Long count = null;

View File

@@ -1,5 +1,7 @@
package com.commafeed.backend.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
@@ -7,6 +9,7 @@ import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.log4j.Level;
@Entity
@@ -40,6 +43,13 @@ public class ApplicationSettings extends AbstractModel {
@Column(length = 255)
private String announcement;
public Date getUnreadThreshold() {
int keepStatusDays = getKeepStatusDays();
return keepStatusDays > 0 ? DateUtils.addDays(new Date(), -1 * keepStatusDays) : null;
}
/* getters and setters below */
public String getPublicUrl() {
return publicUrl;
}
@@ -124,8 +134,7 @@ public class ApplicationSettings extends AbstractModel {
return googleAnalyticsTrackingCode;
}
public void setGoogleAnalyticsTrackingCode(
String googleAnalyticsTrackingCode) {
public void setGoogleAnalyticsTrackingCode(String googleAnalyticsTrackingCode) {
this.googleAnalyticsTrackingCode = googleAnalyticsTrackingCode;
}

View File

@@ -1,26 +1,36 @@
package com.commafeed.backend.services;
import java.util.Date;
import javax.ejb.Schedule;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Stateless
public class CleaningService {
protected final static Logger log = LoggerFactory.getLogger(CleaningService.class);
@Inject
ApplicationSettingsService applicationSettingsService;
@PersistenceContext
EntityManager em;
// @Schedule(hour = "*")
protected void cleanOldStatuses() {
int keepStatusDays = applicationSettingsService.get()
.getKeepStatusDays();
if (keepStatusDays > 0) {
// every day at midnight
@Schedule(hour = "*", minute = "*", persistent = false)
private void cleanOldStatuses() {
Date threshold = applicationSettingsService.get().getUnreadThreshold();
if (threshold != null) {
log.info("cleaning old statuses");
Query query = em.createNamedQuery("Statuses.deleteOld");
query.setParameter("date", threshold);
query.executeUpdate();
}
}