From 150920e0c894eb286492ba487be688043cf6d588 Mon Sep 17 00:00:00 2001 From: Athou Date: Tue, 23 Jul 2013 15:27:56 +0200 Subject: [PATCH] delete old statuses --- pom.xml | 2 +- .../backend/dao/FeedEntryStatusDAO.java | 53 ++++++++++++------- .../backend/model/ApplicationSettings.java | 9 ++++ .../backend/model/FeedEntryStatus.java | 15 +++++- .../backend/services/CleaningService.java | 27 ++++++++++ .../com/commafeed/frontend/model/Entry.java | 12 +++++ src/main/resources/META-INF/orm.xml | 20 ++----- .../resources/changelogs/db.changelog-1.1.xml | 9 ++++ src/main/webapp/templates/admin.settings.html | 8 +++ src/main/webapp/templates/feeds.view.html | 2 +- 10 files changed, 120 insertions(+), 37 deletions(-) create mode 100644 src/main/java/com/commafeed/backend/services/CleaningService.java diff --git a/pom.xml b/pom.xml index e00dd03d..255938e8 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ UTF-8 false - false + true java:openejb/Resource/My DataSource false utf8mb4 diff --git a/src/main/java/com/commafeed/backend/dao/FeedEntryStatusDAO.java b/src/main/java/com/commafeed/backend/dao/FeedEntryStatusDAO.java index a1be6156..b844e9fc 100644 --- a/src/main/java/com/commafeed/backend/dao/FeedEntryStatusDAO.java +++ b/src/main/java/com/commafeed/backend/dao/FeedEntryStatusDAO.java @@ -14,10 +14,11 @@ import javax.persistence.criteria.Path; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; -import org.apache.commons.lang.StringUtils; import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.time.DateUtils; import org.hibernate.Criteria; import org.hibernate.criterion.Disjunction; +import org.hibernate.criterion.MatchMode; import org.hibernate.criterion.Order; import org.hibernate.criterion.ProjectionList; import org.hibernate.criterion.Projections; @@ -80,8 +81,14 @@ public class FeedEntryStatusDAO extends GenericDAO { List statuses = em.createQuery(query).getResultList(); FeedEntryStatus status = Iterables.getFirst(statuses, null); if (status == null) { + Date unreadThreshold = getUnreadThreshold(); + boolean read = unreadThreshold == null ? false : entry + .getInserted().before(unreadThreshold); status = new FeedEntryStatus(sub.getUser(), sub, entry); - status.setRead(false); + status.setRead(read); + status.setMarkable(!read); + } else { + status.setMarkable(true); } return status; } @@ -134,30 +141,33 @@ public class FeedEntryStatusDAO extends GenericDAO { FeedEntry_.content.getName(), "content", JoinType.INNER_JOIN); - String joinedKeywords = StringUtils.join(keywords.toLowerCase() - .split(" "), "%"); - joinedKeywords = "%" + joinedKeywords + "%"; - - Disjunction or = Restrictions.disjunction(); - or.add(Restrictions.ilike(FeedEntryContent_.content.getName(), - joinedKeywords)); - or.add(Restrictions.ilike(FeedEntryContent_.title.getName(), - joinedKeywords)); - contentJoin.add(or); + for (String keyword : keywords.split(" ")) { + Disjunction or = Restrictions.disjunction(); + or.add(Restrictions.ilike(FeedEntryContent_.content.getName(), + keyword, MatchMode.ANYWHERE)); + or.add(Restrictions.ilike(FeedEntryContent_.title.getName(), + keyword, MatchMode.ANYWHERE)); + contentJoin.add(or); + } } if (unreadOnly) { - Criteria statusJoin = criteria.createCriteria(FeedEntry_.statuses .getName(), "status", JoinType.LEFT_OUTER_JOIN, Restrictions.eq(FeedEntryStatus_.subscription.getName(), sub)); Disjunction or = Restrictions.disjunction(); - or.add(Restrictions.isNull(FeedEntryStatus_.id.getName())); + or.add(Restrictions.isNull(FeedEntryStatus_.read.getName())); or.add(Restrictions.eq(FeedEntryStatus_.read.getName(), false)); - statusJoin.add(or); + + Date unreadThreshold = getUnreadThreshold(); + if (unreadThreshold != null) { + criteria.add(Restrictions.ge(FeedEntry_.inserted.getName(), + unreadThreshold)); + } + } if (last != null) { @@ -175,11 +185,11 @@ public class FeedEntryStatusDAO extends GenericDAO { if (order != null) { Order o = null; if (order == ReadingOrder.asc) { - o = Order.asc(FeedEntry_.updated.getName()); + o = Order.asc(FeedFeedEntry_.entryUpdated.getName()); } else { - o = Order.desc(FeedEntry_.updated.getName()); + o = Order.desc(FeedFeedEntry_.entryUpdated.getName()); } - criteria.addOrder(o); + ffeJoin.addOrder(o); } if (offset > -1) { criteria.setFirstResult(offset); @@ -235,6 +245,13 @@ public class FeedEntryStatusDAO extends GenericDAO { return lazyLoadContent(includeContent, results); } + private Date getUnreadThreshold() { + int keepStatusDays = applicationSettingsService.get() + .getKeepStatusDays(); + return keepStatusDays > 0 ? DateUtils.addMinutes(new Date(), -1 + * keepStatusDays) : null; + } + @SuppressWarnings("unchecked") public Long getUnreadCount(FeedSubscription subscription) { Long count = null; diff --git a/src/main/java/com/commafeed/backend/model/ApplicationSettings.java b/src/main/java/com/commafeed/backend/model/ApplicationSettings.java index e6a18df3..133330be 100644 --- a/src/main/java/com/commafeed/backend/model/ApplicationSettings.java +++ b/src/main/java/com/commafeed/backend/model/ApplicationSettings.java @@ -35,6 +35,7 @@ public class ApplicationSettings extends AbstractModel { private boolean imageProxyEnabled; private int queryTimeout; private boolean crawlingPaused; + private int keepStatusDays = 0; @Column(length = 255) private String announcement; @@ -200,4 +201,12 @@ public class ApplicationSettings extends AbstractModel { this.crawlingPaused = crawlingPaused; } + public int getKeepStatusDays() { + return keepStatusDays; + } + + public void setKeepStatusDays(int keepStatusDays) { + this.keepStatusDays = keepStatusDays; + } + } diff --git a/src/main/java/com/commafeed/backend/model/FeedEntryStatus.java b/src/main/java/com/commafeed/backend/model/FeedEntryStatus.java index 57fa82b4..301926f7 100644 --- a/src/main/java/com/commafeed/backend/model/FeedEntryStatus.java +++ b/src/main/java/com/commafeed/backend/model/FeedEntryStatus.java @@ -11,6 +11,7 @@ import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; +import javax.persistence.Transient; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; @@ -34,6 +35,9 @@ public class FeedEntryStatus extends AbstractModel { private boolean read; private boolean starred; + @Transient + private boolean markable; + /** * Denormalization starts here */ @@ -52,7 +56,8 @@ public class FeedEntryStatus extends AbstractModel { } - public FeedEntryStatus(User user, FeedSubscription subscription, FeedEntry entry) { + public FeedEntryStatus(User user, FeedSubscription subscription, + FeedEntry entry) { setUser(user); setSubscription(subscription); setEntry(entry); @@ -116,4 +121,12 @@ public class FeedEntryStatus extends AbstractModel { this.user = user; } + public boolean isMarkable() { + return markable; + } + + public void setMarkable(boolean markable) { + this.markable = markable; + } + } diff --git a/src/main/java/com/commafeed/backend/services/CleaningService.java b/src/main/java/com/commafeed/backend/services/CleaningService.java new file mode 100644 index 00000000..341e1c11 --- /dev/null +++ b/src/main/java/com/commafeed/backend/services/CleaningService.java @@ -0,0 +1,27 @@ +package com.commafeed.backend.services; + +import javax.ejb.Stateless; +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; + +@Stateless +public class CleaningService { + + @Inject + ApplicationSettingsService applicationSettingsService; + + @PersistenceContext + EntityManager em; + +// @Schedule(hour = "*") + protected void cleanOldStatuses() { + int keepStatusDays = applicationSettingsService.get() + .getKeepStatusDays(); + if (keepStatusDays > 0) { + Query query = em.createNamedQuery("Statuses.deleteOld"); + query.executeUpdate(); + } + } +} diff --git a/src/main/java/com/commafeed/frontend/model/Entry.java b/src/main/java/com/commafeed/frontend/model/Entry.java index 41ae271e..31134bb6 100644 --- a/src/main/java/com/commafeed/frontend/model/Entry.java +++ b/src/main/java/com/commafeed/frontend/model/Entry.java @@ -33,6 +33,7 @@ public class Entry implements Serializable { entry.setRead(status.isRead()); entry.setStarred(status.isStarred()); + entry.setMarkable(status.isMarkable()); entry.setId(String.valueOf(feedEntry.getId())); entry.setGuid(feedEntry.getGuid()); @@ -124,6 +125,9 @@ public class Entry implements Serializable { @ApiProperty("starred status") private boolean starred; + @ApiProperty("wether the entry is still markable (old entry statuses are discarded)") + private boolean markable; + public String getId() { return id; } @@ -268,4 +272,12 @@ public class Entry implements Serializable { this.insertedDate = insertedDate; } + public boolean isMarkable() { + return markable; + } + + public void setMarkable(boolean markable) { + this.markable = markable; + } + } diff --git a/src/main/resources/META-INF/orm.xml b/src/main/resources/META-INF/orm.xml index 368beb8d..c66e91a3 100644 --- a/src/main/resources/META-INF/orm.xml +++ b/src/main/resources/META-INF/orm.xml @@ -4,21 +4,9 @@ xsi:schemaLocation=" http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"> - - - select s.subscription.id, count(s) from FeedEntryStatus s where s.user=:user and s.read=false group by s.subscription.id + + + delete from FeedEntryStatus s where s.entryInserted < :date and s.starred = false - - - select new com.commafeed.backend.dao.FeedEntryDAO$EntryWithFeed(e, f) FROM FeedEntry e LEFT JOIN e.feedRelationships f WITH f.feed.id = :feedId where e.guidHash = :guidHash and e.url = :url - - - - delete from FeedEntryStatus s where s.id in :ids - - - - delete from FeedFeedEntry ffe where ffe.feed.id = :feedId - - + \ No newline at end of file diff --git a/src/main/resources/changelogs/db.changelog-1.1.xml b/src/main/resources/changelogs/db.changelog-1.1.xml index ee98620e..6db876fd 100644 --- a/src/main/resources/changelogs/db.changelog-1.1.xml +++ b/src/main/resources/changelogs/db.changelog-1.1.xml @@ -346,4 +346,13 @@ + + + + + + + + + diff --git a/src/main/webapp/templates/admin.settings.html b/src/main/webapp/templates/admin.settings.html index 53f69ff9..5ae9b83b 100644 --- a/src/main/webapp/templates/admin.settings.html +++ b/src/main/webapp/templates/admin.settings.html @@ -163,6 +163,14 @@ ng-model="settings.queryTimeout" /> +
+ +
+ + 0 = keep forever +
+
diff --git a/src/main/webapp/templates/feeds.view.html b/src/main/webapp/templates/feeds.view.html index ffdceef1..d187ab7a 100644 --- a/src/main/webapp/templates/feeds.view.html +++ b/src/main/webapp/templates/feeds.view.html @@ -79,7 +79,7 @@ -