roller-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From snoopd...@apache.org
Subject roller git commit: Minor refactoring
Date Sun, 09 Dec 2018 19:24:03 GMT
Repository: roller
Updated Branches:
  refs/heads/master bcea95ea4 -> 911700883


Minor refactoring


Project: http://git-wip-us.apache.org/repos/asf/roller/repo
Commit: http://git-wip-us.apache.org/repos/asf/roller/commit/91170088
Tree: http://git-wip-us.apache.org/repos/asf/roller/tree/91170088
Diff: http://git-wip-us.apache.org/repos/asf/roller/diff/91170088

Branch: refs/heads/master
Commit: 911700883e2f1abcf883042715d8630f52529d8e
Parents: bcea95e
Author: Dave Johnson <snoopdave@gmail.com>
Authored: Sun Dec 9 14:23:59 2018 -0500
Committer: Dave Johnson <snoopdave@gmail.com>
Committed: Sun Dec 9 14:23:59 2018 -0500

----------------------------------------------------------------------
 .../business/jpa/JPAWeblogEntryManagerImpl.java | 76 ++++++++------------
 .../weblogger/ui/rendering/model/SiteModel.java | 16 ++---
 2 files changed, 32 insertions(+), 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/roller/blob/91170088/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWeblogEntryManagerImpl.java
----------------------------------------------------------------------
diff --git a/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWeblogEntryManagerImpl.java
b/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWeblogEntryManagerImpl.java
index 5035f93..7fe517d 100644
--- a/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWeblogEntryManagerImpl.java
+++ b/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWeblogEntryManagerImpl.java
@@ -288,7 +288,7 @@ public class JPAWeblogEntryManagerImpl implements WeblogEntryManager {
         this.entryAnchorToIdMap.remove(entry.getWebsite().getHandle()+":"+entry.getAnchor());
     }
     
-    public List getNextPrevEntries(WeblogEntry current, String catName,
+    private List getNextPrevEntries(WeblogEntry current, String catName,
             String locale, int maxEntries, boolean next)
             throws WebloggerException {
 
@@ -462,13 +462,7 @@ public class JPAWeblogEntryManagerImpl implements WeblogEntryManager
{
             query.setParameter(i+1, params.get(i));
         }
         
-        if (wesc.getOffset() != 0) {
-            query.setFirstResult(wesc.getOffset());
-        }
-        if (wesc.getMaxResults() != -1) {
-            query.setMaxResults(wesc.getMaxResults());
-        }
-        
+        setFirstMax( query, wesc.getOffset(), wesc.getMaxResults() );
         return query.getResultList();
     }
     
@@ -666,12 +660,7 @@ public class JPAWeblogEntryManagerImpl implements WeblogEntryManager
{
         }
         
         TypedQuery<WeblogEntryComment> query = strategy.getDynamicQuery(queryString.toString(),
WeblogEntryComment.class);
-        if (csc.getOffset() != 0) {
-            query.setFirstResult(csc.getOffset());
-        }
-        if (csc.getMaxResults() != -1) {
-            query.setMaxResults(csc.getMaxResults());
-        }
+        setFirstMax( query, csc.getOffset(), csc.getMaxResults());
         for (int i=0; i<params.size(); i++) {
             query.setParameter(i+1, params.get(i));
         }
@@ -758,7 +747,7 @@ public class JPAWeblogEntryManagerImpl implements WeblogEntryManager {
      * @inheritDoc
      */
     public Map<Date, List<WeblogEntry>> getWeblogEntryObjectMap(WeblogEntrySearchCriteria
wesc) throws WebloggerException {
-        TreeMap<Date, List<WeblogEntry>> map = new TreeMap<Date, List<WeblogEntry>>(Collections.reverseOrder());
+        TreeMap<Date, List<WeblogEntry>> map = new TreeMap<>(Collections.reverseOrder());
 
         List<WeblogEntry> entries = getWeblogEntries(wesc);
 
@@ -769,11 +758,7 @@ public class JPAWeblogEntryManagerImpl implements WeblogEntryManager
{
 
         for (WeblogEntry entry : entries) {
             Date sDate = DateUtil.getNoonOfDay(entry.getPubTime(), cal);
-            List<WeblogEntry> dayEntries = map.get(sDate);
-            if (dayEntries == null) {
-                dayEntries = new ArrayList<WeblogEntry>();
-                map.put(sDate, dayEntries);
-            }
+            List<WeblogEntry> dayEntries = map.computeIfAbsent(sDate, k -> new ArrayList<>());
             dayEntries.add(entry);
         }
         return map;
@@ -843,12 +828,7 @@ public class JPAWeblogEntryManagerImpl implements WeblogEntryManager
{
                 query.setParameter(1, end);
             }
         }
-        if (offset != 0) {
-            query.setFirstResult(offset);
-        }
-        if (length != -1) {
-            query.setMaxResults(length);
-        }
+        setFirstMax( query, offset, length);
         queryResults = query.getResultList();
         List<StatCount> results = new ArrayList<StatCount>();
         if (queryResults != null) {
@@ -951,12 +931,7 @@ public class JPAWeblogEntryManagerImpl implements WeblogEntryManager
{
                         "WeblogEntryTagAggregate.getPopularTagsByWebsiteNull", TagStat.class);
             }
         }
-        if (offset != 0) {
-            query.setFirstResult(offset);
-        }
-        if (limit != -1) {
-            query.setMaxResults(limit);
-        }
+        setFirstMax( query, offset, limit);
         queryResults = query.getResultList();
         
         double min = Integer.MAX_VALUE;
@@ -1029,12 +1004,7 @@ public class JPAWeblogEntryManagerImpl implements WeblogEntryManager
{
         for (int i=0; i<params.size(); i++) {
             query.setParameter(i+1, params.get(i));
         }
-        if (offset != 0) {
-            query.setFirstResult(offset);
-        }
-        if (limit != -1) {
-            query.setMaxResults(limit);
-        }
+        setFirstMax( query, offset, limit);
         queryResults = query.getResultList();
         
         List<TagStat> results = new ArrayList<TagStat>();
@@ -1214,26 +1184,36 @@ public class JPAWeblogEntryManagerImpl implements WeblogEntryManager
{
     throws WebloggerException {
         
         // figure out start date
-        Calendar cal = Calendar.getInstance();
-        cal.setTime(new Date());
-        cal.add(Calendar.DATE, -1 * sinceDays);
-        Date startDate = cal.getTime();
+        Date startDate = getStartDateNow(sinceDays);
 
-        TypedQuery<WeblogHitCount> query = strategy.getNamedQuery(
+        TypedQuery<WeblogHitCount> query;
+        query = strategy.getNamedQuery(
                 "WeblogHitCount.getByWeblogEnabledTrueAndActiveTrue&DailyHitsGreaterThenZero&WeblogLastModifiedGreaterOrderByDailyHitsDesc",
                 WeblogHitCount.class);
         query.setParameter(1, startDate);
-        
+        setFirstMax( query, offset, length );
+        return query.getResultList();
+    }
+
+
+    private static void setFirstMax( Query query, int offset, int length )  {
         if (offset != 0) {
             query.setFirstResult(offset);
         }
         if (length != -1) {
             query.setMaxResults(length);
-        }        
-        return query.getResultList();
+        }
     }
-    
-    
+
+
+    public static Date getStartDateNow(int sinceDays) {
+        Calendar cal = Calendar.getInstance();
+        cal.setTime(new Date());
+        cal.add(Calendar.DATE, -1 * sinceDays);
+        return cal.getTime();
+    }
+
+
     /**
      * @inheritDoc
      */

http://git-wip-us.apache.org/repos/asf/roller/blob/91170088/app/src/main/java/org/apache/roller/weblogger/ui/rendering/model/SiteModel.java
----------------------------------------------------------------------
diff --git a/app/src/main/java/org/apache/roller/weblogger/ui/rendering/model/SiteModel.java
b/app/src/main/java/org/apache/roller/weblogger/ui/rendering/model/SiteModel.java
index ada1b24..e3fd7b3 100644
--- a/app/src/main/java/org/apache/roller/weblogger/ui/rendering/model/SiteModel.java
+++ b/app/src/main/java/org/apache/roller/weblogger/ui/rendering/model/SiteModel.java
@@ -33,6 +33,7 @@ import org.apache.roller.weblogger.business.Weblogger;
 import org.apache.roller.weblogger.business.WebloggerFactory;
 import org.apache.roller.weblogger.business.UserManager;
 import org.apache.roller.weblogger.business.WeblogEntryManager;
+import org.apache.roller.weblogger.business.jpa.JPAWeblogEntryManagerImpl;
 import org.apache.roller.weblogger.pojos.TagStat;
 import org.apache.roller.weblogger.pojos.WeblogHitCount;
 import org.apache.roller.weblogger.pojos.StatCount;
@@ -389,10 +390,7 @@ public class SiteModel implements Model {
      */
     public List<WeblogWrapper> getNewWeblogs(int sinceDays, int length) {
         List<WeblogWrapper> results = new ArrayList<WeblogWrapper>();
-        Calendar cal = Calendar.getInstance();
-        cal.setTime(new Date());
-        cal.add(Calendar.DATE, -1 * sinceDays);
-        Date startDate = cal.getTime();
+        Date startDate = JPAWeblogEntryManagerImpl.getStartDateNow(sinceDays);
         try {            
             List<Weblog> weblogs = WebloggerFactory.getWeblogger().getWeblogManager().getWeblogs(
                 Boolean.TRUE, Boolean.TRUE, startDate, null, 0, length);
@@ -466,10 +464,7 @@ public class SiteModel implements Model {
      */
     public List getMostCommentedWeblogs(int sinceDays , int length) {
         List results = new ArrayList();
-        Calendar cal = Calendar.getInstance();
-        cal.setTime(new Date());
-        cal.add(Calendar.DATE, -1 * sinceDays);
-        Date startDate = cal.getTime();
+        Date startDate = JPAWeblogEntryManagerImpl.getStartDateNow(sinceDays);
         try {            
             results = WebloggerFactory.getWeblogger().getWeblogManager().getMostCommentedWeblogs(
                     startDate, new Date(), 0, length);
@@ -490,10 +485,7 @@ public class SiteModel implements Model {
     public List getMostCommentedWeblogEntries(
             List cats, int sinceDays, int length) {
         List results = new ArrayList();
-        Calendar cal = Calendar.getInstance();
-        cal.setTime(new Date());
-        cal.add(Calendar.DATE, -1 * sinceDays);
-        Date startDate = cal.getTime();
+        Date startDate = JPAWeblogEntryManagerImpl.getStartDateNow(sinceDays);
         try {            
             Weblogger roller = WebloggerFactory.getWeblogger();
             WeblogEntryManager wmgr = roller.getWeblogEntryManager();


Mime
View raw message