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

View File

@@ -1,5 +1,7 @@
package com.commafeed.backend.model; package com.commafeed.backend.model;
import java.util.Date;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Table; 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.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.log4j.Level; import org.apache.log4j.Level;
@Entity @Entity
@@ -40,6 +43,13 @@ public class ApplicationSettings extends AbstractModel {
@Column(length = 255) @Column(length = 255)
private String announcement; 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() { public String getPublicUrl() {
return publicUrl; return publicUrl;
} }
@@ -124,8 +134,7 @@ public class ApplicationSettings extends AbstractModel {
return googleAnalyticsTrackingCode; return googleAnalyticsTrackingCode;
} }
public void setGoogleAnalyticsTrackingCode( public void setGoogleAnalyticsTrackingCode(String googleAnalyticsTrackingCode) {
String googleAnalyticsTrackingCode) {
this.googleAnalyticsTrackingCode = googleAnalyticsTrackingCode; this.googleAnalyticsTrackingCode = googleAnalyticsTrackingCode;
} }

View File

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