Return-Path: X-Original-To: apmail-roller-commits-archive@www.apache.org Delivered-To: apmail-roller-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id A2F6B110F5 for ; Sun, 29 Jun 2014 19:09:44 +0000 (UTC) Received: (qmail 15639 invoked by uid 500); 29 Jun 2014 19:09:44 -0000 Delivered-To: apmail-roller-commits-archive@roller.apache.org Received: (qmail 15606 invoked by uid 500); 29 Jun 2014 19:09:44 -0000 Mailing-List: contact commits-help@roller.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@roller.apache.org Delivered-To: mailing list commits@roller.apache.org Received: (qmail 15596 invoked by uid 99); 29 Jun 2014 19:09:44 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 29 Jun 2014 19:09:44 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 29 Jun 2014 19:09:40 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id D43E723889FD; Sun, 29 Jun 2014 19:09:14 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1606568 [2/2] - in /roller/trunk/app/src/main/java/org/apache/roller: planet/business/jpa/ weblogger/business/ weblogger/business/jpa/ weblogger/business/pings/ weblogger/pojos/ weblogger/ui/rendering/model/ Date: Sun, 29 Jun 2014 19:09:13 -0000 To: commits@roller.apache.org From: gmazza@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140629190914.D43E723889FD@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Modified: roller/trunk/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWeblogEntryManagerImpl.java URL: http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWeblogEntryManagerImpl.java?rev=1606568&r1=1606567&r2=1606568&view=diff ============================================================================== --- roller/trunk/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWeblogEntryManagerImpl.java (original) +++ roller/trunk/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWeblogEntryManagerImpl.java Sun Jun 29 19:09:13 2014 @@ -23,6 +23,7 @@ import java.text.SimpleDateFormat; import java.sql.Timestamp; import javax.persistence.NoResultException; import javax.persistence.Query; +import javax.persistence.TypedQuery; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; @@ -69,12 +70,12 @@ public class JPAWeblogEntryManagerImpl i // cached mapping of entryAnchors -> entryIds private Map entryAnchorToIdMap = new HashMap(); - private static final Comparator TAG_STAT_NAME_COMPARATOR = new TagStatComparator(); + private static final Comparator TAG_STAT_NAME_COMPARATOR = new TagStatComparator(); - private static final Comparator TAG_STAT_COUNT_REVERSE_COMPARATOR = + private static final Comparator TAG_STAT_COUNT_REVERSE_COMPARATOR = Collections.reverseOrder(TagStatCountComparator.getInstance()); - private static final Comparator STAT_COUNT_COUNT_REVERSE_COMPARATOR = + private static final Comparator STAT_COUNT_COUNT_REVERSE_COMPARATOR = Collections.reverseOrder(StatCountCountComparator.getInstance()); @@ -258,9 +259,7 @@ public class JPAWeblogEntryManagerImpl i this.strategy.remove(att); } } - // TODO: can we eliminate this unnecessary flush with OpenJPA 1.0 - this.strategy.flush(); - + // remove entry this.strategy.remove(entry); @@ -282,7 +281,7 @@ public class JPAWeblogEntryManagerImpl i return Collections.emptyList(); } - Query query; + TypedQuery query; WeblogCategory category; List params = new ArrayList(); @@ -327,7 +326,7 @@ public class JPAWeblogEntryManagerImpl i } else { whereClause.append(" ORDER BY e.pubTime DESC"); } - query = strategy.getDynamicQuery(queryString + whereClause.toString()); + query = strategy.getDynamicQuery(queryString + whereClause.toString(), WeblogEntry.class); for (int i=0; i q = strategy.getNamedQuery( + "WeblogCategory.getByWeblog", WeblogCategory.class); q.setParameter(1, website); return q.getResultList(); } @@ -442,7 +441,7 @@ public class JPAWeblogEntryManagerImpl i } - Query query = strategy.getDynamicQuery(queryString.toString()); + TypedQuery query = strategy.getDynamicQuery(queryString.toString(), WeblogEntry.class); for (int i=0; i getWeblogEntriesPinnedToMain(Integer max) throws WebloggerException { - Query query = strategy.getNamedQuery( - "WeblogEntry.getByPinnedToMain&statusOrderByPubTimeDesc"); + TypedQuery query = strategy.getNamedQuery( + "WeblogEntry.getByPinnedToMain&statusOrderByPubTimeDesc", WeblogEntry.class); query.setParameter(1, Boolean.TRUE); query.setParameter(2, PubStatus.PUBLISHED); if (max != null) { @@ -527,13 +526,13 @@ public class JPAWeblogEntryManagerImpl i } // mapping key is combo of weblog + anchor - String mappingKey = website.getHandle()+":"+anchor; + String mappingKey = website.getHandle() + ":" + anchor; // check cache first // NOTE: if we ever allow changing anchors then this needs updating if(this.entryAnchorToIdMap.containsKey(mappingKey)) { - WeblogEntry entry = this.getWeblogEntry((String) this.entryAnchorToIdMap.get(mappingKey)); + WeblogEntry entry = this.getWeblogEntry(this.entryAnchorToIdMap.get(mappingKey)); if(entry != null) { LOG.debug("entryAnchorToIdMap CACHE HIT - " + mappingKey); return entry; @@ -544,13 +543,13 @@ public class JPAWeblogEntryManagerImpl i } // cache failed, do lookup - Query q = strategy.getNamedQuery( - "WeblogEntry.getByWebsite&AnchorOrderByPubTimeDesc"); + TypedQuery q = strategy.getNamedQuery( + "WeblogEntry.getByWebsite&AnchorOrderByPubTimeDesc", WeblogEntry.class); q.setParameter(1, website); q.setParameter(2, anchor); WeblogEntry entry; try { - entry = (WeblogEntry)q.getSingleResult(); + entry = q.getSingleResult(); } catch (NoResultException e) { entry = null; } @@ -577,8 +576,8 @@ public class JPAWeblogEntryManagerImpl i name = base + count; } - Query q = strategy.getNamedQuery( - "WeblogEntry.getByWebsite&Anchor"); + TypedQuery q = strategy.getNamedQuery( + "WeblogEntry.getByWebsite&Anchor", WeblogEntry.class); q.setParameter(1, entry.getWebsite()); q.setParameter(2, name); List results = q.getResultList(); @@ -609,7 +608,7 @@ public class JPAWeblogEntryManagerImpl i if (cat.getWeblog().getBloggerCategory().equals(cat)) { return true; } - Query q = strategy.getNamedQuery("WeblogEntry.getByCategory"); + TypedQuery q = strategy.getNamedQuery("WeblogEntry.getByCategory", WeblogEntry.class); q.setParameter(1, cat); int entryCount = q.getResultList().size(); return entryCount > 0; @@ -666,7 +665,7 @@ public class JPAWeblogEntryManagerImpl i queryString.append(" ORDER BY c.postTime ASC"); } - Query query = strategy.getDynamicQuery(queryString.toString()); + TypedQuery query = strategy.getDynamicQuery(queryString.toString(), WeblogEntryComment.class); if (csc.getOffset() != 0) { query.setFirstResult(csc.getOffset()); } @@ -730,12 +729,12 @@ public class JPAWeblogEntryManagerImpl i */ public WeblogCategory getWeblogCategoryByName(Weblog weblog, String categoryName) throws WebloggerException { - Query q = strategy.getNamedQuery( - "WeblogCategory.getByWeblog&Name"); + TypedQuery q = strategy.getNamedQuery( + "WeblogCategory.getByWeblog&Name", WeblogCategory.class); q.setParameter(1, weblog); q.setParameter(2, categoryName); try { - return (WeblogCategory)q.getSingleResult(); + return q.getSingleResult(); } catch (NoResultException e) { return null; } @@ -759,19 +758,32 @@ public class JPAWeblogEntryManagerImpl i * @inheritDoc */ public Map> getWeblogEntryObjectMap(WeblogEntrySearchCriteria wesc) throws WebloggerException { - return getWeblogEntryMap(wesc, false); + TreeMap> map = new TreeMap>(Collections.reverseOrder()); + + List entries = getWeblogEntries(wesc); + + Calendar cal = Calendar.getInstance(); + if (wesc.getWeblog() != null) { + cal.setTimeZone(wesc.getWeblog().getTimeZoneInstance()); + } + + for (WeblogEntry entry : entries) { + Date sDate = DateUtil.getNoonOfDay(entry.getPubTime(), cal); + List dayEntries = map.get(sDate); + if (dayEntries == null) { + dayEntries = new ArrayList(); + map.put(sDate, dayEntries); + } + dayEntries.add(entry); + } + return map; } /** * @inheritDoc */ public Map getWeblogEntryStringMap(WeblogEntrySearchCriteria wesc) throws WebloggerException { - return getWeblogEntryMap(wesc, true); - } - - private Map getWeblogEntryMap(WeblogEntrySearchCriteria wesc, boolean stringsOnly) throws WebloggerException { - - TreeMap map = new TreeMap(Collections.reverseOrder()); + TreeMap map = new TreeMap(Collections.reverseOrder()); List entries = getWeblogEntries(wesc); @@ -779,33 +791,24 @@ public class JPAWeblogEntryManagerImpl i if (wesc.getWeblog() != null) { cal.setTimeZone(wesc.getWeblog().getTimeZoneInstance()); } - + SimpleDateFormat formatter = DateUtil.get8charDateFormat(); for (WeblogEntry entry : entries) { Date sDate = DateUtil.getNoonOfDay(entry.getPubTime(), cal); - if (stringsOnly) { - if (map.get(sDate) == null) { - map.put(sDate, formatter.format(sDate)); - } - } else { - List dayEntries = (List) map.get(sDate); - if (dayEntries == null) { - dayEntries = new ArrayList(); - map.put(sDate, dayEntries); - } - dayEntries.add(entry); + if (map.get(sDate) == null) { + map.put(sDate, formatter.format(sDate)); } } return map; } - + /** * @inheritDoc */ public List getMostCommentedWeblogEntries(Weblog website, Date startDate, Date endDate, int offset, int length) throws WebloggerException { - Query query; + TypedQuery query; List queryResults; Timestamp end = new Timestamp(endDate != null? endDate.getTime() : new Date().getTime()); @@ -814,13 +817,14 @@ public class JPAWeblogEntryManagerImpl i if (startDate != null) { Timestamp start = new Timestamp(startDate.getTime()); query = strategy.getNamedQuery( - "WeblogEntryComment.getMostCommentedWeblogEntryByWebsite&EndDate&StartDate"); + "WeblogEntryComment.getMostCommentedWeblogEntryByWebsite&EndDate&StartDate", + WeblogEntryComment.class); query.setParameter(1, website); query.setParameter(2, end); query.setParameter(3, start); } else { query = strategy.getNamedQuery( - "WeblogEntryComment.getMostCommentedWeblogEntryByWebsite&EndDate"); + "WeblogEntryComment.getMostCommentedWeblogEntryByWebsite&EndDate", WeblogEntryComment.class); query.setParameter(1, website); query.setParameter(2, end); } @@ -828,12 +832,12 @@ public class JPAWeblogEntryManagerImpl i if (startDate != null) { Timestamp start = new Timestamp(startDate.getTime()); query = strategy.getNamedQuery( - "WeblogEntryComment.getMostCommentedWeblogEntryByEndDate&StartDate"); + "WeblogEntryComment.getMostCommentedWeblogEntryByEndDate&StartDate", WeblogEntryComment.class); query.setParameter(1, end); query.setParameter(2, start); } else { query = strategy.getNamedQuery( - "WeblogEntryComment.getMostCommentedWeblogEntryByEndDate"); + "WeblogEntryComment.getMostCommentedWeblogEntryByEndDate", WeblogEntryComment.class); query.setParameter(1, end); } } @@ -845,16 +849,18 @@ public class JPAWeblogEntryManagerImpl i } queryResults = query.getResultList(); List results = new ArrayList(); - for (Iterator iter = queryResults.iterator(); iter.hasNext();) { - Object[] row = (Object[]) iter.next(); - StatCount sc = new StatCount( - (String)row[1], // weblog handle - (String)row[2], // entry anchor - (String)row[3], // entry title - "statCount.weblogEntryCommentCountType", // stat desc - ((Long)row[0])); // count - sc.setWeblogHandle((String)row[1]); - results.add(sc); + if (queryResults != null) { + for (Object obj : queryResults) { + Object[] row = (Object[]) obj; + StatCount sc = new StatCount( + (String)row[1], // weblog handle + (String)row[2], // entry anchor + (String)row[3], // entry title + "statCount.weblogEntryCommentCountType", // stat desc + ((Long)row[0])); // count + sc.setWeblogHandle((String)row[1]); + results.add(sc); + } } // Original query ordered by desc count. // JPA QL doesn't allow queries to be ordered by agregates; do it in memory @@ -917,30 +923,30 @@ public class JPAWeblogEntryManagerImpl i */ public List getPopularTags(Weblog website, Date startDate, int offset, int limit) throws WebloggerException { - Query query; + TypedQuery query; List queryResults; if (website != null) { if (startDate != null) { Timestamp start = new Timestamp(startDate.getTime()); query = strategy.getNamedQuery( - "WeblogEntryTagAggregate.getPopularTagsByWebsite&StartDate"); + "WeblogEntryTagAggregate.getPopularTagsByWebsite&StartDate", TagStat.class); query.setParameter(1, website); query.setParameter(2, start); } else { query = strategy.getNamedQuery( - "WeblogEntryTagAggregate.getPopularTagsByWebsite"); + "WeblogEntryTagAggregate.getPopularTagsByWebsite", TagStat.class); query.setParameter(1, website); } } else { if (startDate != null) { Timestamp start = new Timestamp(startDate.getTime()); query = strategy.getNamedQuery( - "WeblogEntryTagAggregate.getPopularTagsByWebsiteNull&StartDate"); + "WeblogEntryTagAggregate.getPopularTagsByWebsiteNull&StartDate", TagStat.class); query.setParameter(1, start); } else { query = strategy.getNamedQuery( - "WeblogEntryTagAggregate.getPopularTagsByWebsiteNull"); + "WeblogEntryTagAggregate.getPopularTagsByWebsiteNull", TagStat.class); } } if (offset != 0) { @@ -956,17 +962,19 @@ public class JPAWeblogEntryManagerImpl i List results = new ArrayList(limit >= 0 ? limit : 25); - for (Iterator iter = queryResults.iterator(); iter.hasNext();) { - Object[] row = (Object[]) iter.next(); - TagStat t = new TagStat(); - t.setName((String) row[0]); - t.setCount(((Number) row[1]).intValue()); - - min = Math.min(min, t.getCount()); - max = Math.max(max, t.getCount()); - results.add(t); + if (queryResults != null) { + for (Object obj : queryResults) { + Object[] row = (Object[]) obj; + TagStat t = new TagStat(); + t.setName((String) row[0]); + t.setCount(((Number) row[1]).intValue()); + + min = Math.min(min, t.getCount()); + max = Math.max(max, t.getCount()); + results.add(t); + } } - + min = Math.log(1+min); max = Math.log(1+max); @@ -1005,7 +1013,7 @@ public class JPAWeblogEntryManagerImpl i if (startsWith != null && startsWith.length() > 0) { params.add(size++, startsWith + '%'); - queryString.append(" AND w.name LIKE ?" + size); + queryString.append(" AND w.name LIKE ?").append(size); } if (sortBy != null && sortBy.equals("count")) { @@ -1028,13 +1036,15 @@ public class JPAWeblogEntryManagerImpl i queryResults = query.getResultList(); List results = new ArrayList(); - for (Iterator iter = queryResults.iterator(); iter.hasNext();) { - Object[] row = (Object[]) iter.next(); - TagStat ce = new TagStat(); - ce.setName((String) row[0]); - // The JPA query retrieves SUM(w.total) always as long - ce.setCount(((Long) row[1]).intValue()); - results.add(ce); + if (queryResults != null) { + for (Object obj : queryResults) { + Object[] row = (Object[]) obj; + TagStat ce = new TagStat(); + ce.setName((String) row[0]); + // The JPA query retrieves SUM(w.total) always as long + ce.setCount(((Long) row[1]).intValue()); + results.add(ce); + } } if (sortByName) { @@ -1082,11 +1092,11 @@ public class JPAWeblogEntryManagerImpl i queryString.append(" AND w.weblog IS NULL"); } - Query q = strategy.getDynamicQuery(queryString.toString()); + TypedQuery q = strategy.getDynamicQuery(queryString.toString(), String.class); for (int j=0; j results = q.getResultList(); //TODO: DatamapperPort: Since we are only interested in knowing whether //results.size() == tags.size(). This query can be optimized to just fetch COUNT @@ -1111,23 +1121,23 @@ public class JPAWeblogEntryManagerImpl i // one in the case where we have multiple rows (clustered environment) // eventually that second entry will have a very low total (most likely 1) and // won't matter - Query weblogQuery = strategy.getNamedQuery( - "WeblogEntryTagAggregate.getByName&WebsiteOrderByLastUsedDesc"); + TypedQuery weblogQuery = strategy.getNamedQuery( + "WeblogEntryTagAggregate.getByName&WebsiteOrderByLastUsedDesc", WeblogEntryTagAggregate.class); weblogQuery.setParameter(1, name); weblogQuery.setParameter(2, website); WeblogEntryTagAggregate weblogTagData; try { - weblogTagData = (WeblogEntryTagAggregate)weblogQuery.getSingleResult(); + weblogTagData = weblogQuery.getSingleResult(); } catch (NoResultException e) { weblogTagData = null; } - - Query siteQuery = strategy.getNamedQuery( - "WeblogEntryTagAggregate.getByName&WebsiteNullOrderByLastUsedDesc"); + + TypedQuery siteQuery = strategy.getNamedQuery( + "WeblogEntryTagAggregate.getByName&WebsiteNullOrderByLastUsedDesc", WeblogEntryTagAggregate.class); siteQuery.setParameter(1, name); WeblogEntryTagAggregate siteTagData; try { - siteTagData = (WeblogEntryTagAggregate)siteQuery.getSingleResult(); + siteTagData = siteQuery.getSingleResult(); } catch (NoResultException e) { siteTagData = null; } @@ -1178,10 +1188,10 @@ public class JPAWeblogEntryManagerImpl i */ public WeblogHitCount getHitCountByWeblog(Weblog weblog) throws WebloggerException { - Query q = strategy.getNamedQuery("WeblogHitCount.getByWeblog"); + TypedQuery q = strategy.getNamedQuery("WeblogHitCount.getByWeblog", WeblogHitCount.class); q.setParameter(1, weblog); try { - return (WeblogHitCount)q.getSingleResult(); + return q.getSingleResult(); } catch (NoResultException e) { return null; } @@ -1198,13 +1208,12 @@ public class JPAWeblogEntryManagerImpl i cal.setTime(new Date()); cal.add(Calendar.DATE, -1 * sinceDays); Date startDate = cal.getTime(); - - Query query = strategy.getNamedQuery( - "WeblogHitCount.getByWeblogEnabledTrueAndActiveTrue&DailyHitsGreaterThenZero&WeblogLastModifiedGreaterOrderByDailyHitsDesc"); + + TypedQuery query = strategy.getNamedQuery( + "WeblogHitCount.getByWeblogEnabledTrueAndActiveTrue&DailyHitsGreaterThenZero&WeblogLastModifiedGreaterOrderByDailyHitsDesc", + WeblogHitCount.class); query.setParameter(1, startDate); - // Was commented out due to https://glassfish.dev.java.net/issues/show_bug.cgi?id=2084 - // TODO: determine if this is still an issue. Is it a problem with other JPA implementations? if (offset != 0) { query.setFirstResult(offset); } @@ -1244,12 +1253,12 @@ public class JPAWeblogEntryManagerImpl i if(weblog == null) { throw new WebloggerException("Website cannot be NULL."); } - - Query q = strategy.getNamedQuery("WeblogHitCount.getByWeblog"); + + TypedQuery q = strategy.getNamedQuery("WeblogHitCount.getByWeblog", WeblogHitCount.class); q.setParameter(1, weblog); WeblogHitCount hitCount; try { - hitCount = (WeblogHitCount)q.getSingleResult(); + hitCount = q.getSingleResult(); } catch (NoResultException e) { hitCount = null; } @@ -1278,11 +1287,11 @@ public class JPAWeblogEntryManagerImpl i * @inheritDoc */ public void resetHitCount(Weblog weblog) throws WebloggerException { - Query q = strategy.getNamedQuery("WeblogHitCount.getByWeblog"); + TypedQuery q = strategy.getNamedQuery("WeblogHitCount.getByWeblog", WeblogHitCount.class); q.setParameter(1, weblog); WeblogHitCount hitCount; try { - hitCount = (WeblogHitCount)q.getSingleResult(); + hitCount = q.getSingleResult(); hitCount.setDailyHits(0); strategy.store(hitCount); } catch (NoResultException e) { @@ -1295,46 +1304,42 @@ public class JPAWeblogEntryManagerImpl i * @inheritDoc */ public long getCommentCount() throws WebloggerException { - Query q = strategy.getNamedQuery( - "WeblogEntryComment.getCountAllDistinctByStatus"); + TypedQuery q = strategy.getNamedQuery( + "WeblogEntryComment.getCountAllDistinctByStatus", Long.class); q.setParameter(1, ApprovalStatus.APPROVED); - List results = q.getResultList(); - return ((Long)results.get(0)); + return q.getResultList().get(0); } /** * @inheritDoc */ public long getCommentCount(Weblog website) throws WebloggerException { - Query q = strategy.getNamedQuery( - "WeblogEntryComment.getCountDistinctByWebsite&Status"); + TypedQuery q = strategy.getNamedQuery( + "WeblogEntryComment.getCountDistinctByWebsite&Status", Long.class); q.setParameter(1, website); q.setParameter(2, ApprovalStatus.APPROVED); - List results = q.getResultList(); - return ((Long)results.get(0)); + return q.getResultList().get(0); } /** * @inheritDoc */ public long getEntryCount() throws WebloggerException { - Query q = strategy.getNamedQuery( - "WeblogEntry.getCountDistinctByStatus"); + TypedQuery q = strategy.getNamedQuery( + "WeblogEntry.getCountDistinctByStatus", Long.class); q.setParameter(1, PubStatus.PUBLISHED); - List results = q.getResultList(); - return ((Long)results.get(0)); + return q.getResultList().get(0); } /** * @inheritDoc */ public long getEntryCount(Weblog website) throws WebloggerException { - Query q = strategy.getNamedQuery( - "WeblogEntry.getCountDistinctByStatus&Website"); + TypedQuery q = strategy.getNamedQuery( + "WeblogEntry.getCountDistinctByStatus&Website", Long.class); q.setParameter(1, PubStatus.PUBLISHED); q.setParameter(2, website); - List results = q.getResultList(); - return ((Long)results.get(0)); + return q.getResultList().get(0); } /** @@ -1347,7 +1352,7 @@ public class JPAWeblogEntryManagerImpl i */ private static StringBuilder appendConjuctionToWhereclause(StringBuilder whereClause, String expression) { - if(whereClause.length() != 0 && expression.length() != 0) { + if (whereClause.length() != 0 && expression.length() != 0) { whereClause.append(" AND "); } return whereClause.append(expression); Modified: roller/trunk/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWeblogManagerImpl.java URL: http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWeblogManagerImpl.java?rev=1606568&r1=1606567&r2=1606568&view=diff ============================================================================== --- roller/trunk/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWeblogManagerImpl.java (original) +++ roller/trunk/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWeblogManagerImpl.java Sun Jun 29 19:09:13 2014 @@ -28,6 +28,7 @@ import org.apache.roller.weblogger.confi import javax.persistence.NoResultException; import javax.persistence.Query; +import javax.persistence.TypedQuery; import java.sql.Timestamp; import java.util.*; import org.apache.roller.weblogger.business.MediaFileManager; @@ -37,6 +38,7 @@ import org.apache.roller.weblogger.busin import org.apache.roller.weblogger.business.Weblogger; import org.apache.roller.weblogger.business.WebloggerFactory; import org.apache.roller.weblogger.pojos.AutoPing; +import org.apache.roller.weblogger.pojos.PingQueueEntry; import org.apache.roller.weblogger.pojos.PingTarget; import org.apache.roller.weblogger.pojos.StatCount; import org.apache.roller.weblogger.pojos.StatCountCountComparator; @@ -66,7 +68,7 @@ public class JPAWeblogManagerImpl implem /** The logger instance for this class. */ private static Log log = LogFactory.getLog(JPAWeblogManagerImpl.class); - private static final Comparator STAT_COUNT_COUNT_REVERSE_COMPARATOR = + private static final Comparator STAT_COUNT_COUNT_REVERSE_COMPARATOR = Collections.reverseOrder(StatCountCountComparator.getInstance()); private final Weblogger roller; @@ -116,7 +118,8 @@ public class JPAWeblogManagerImpl implem WeblogEntryManager emgr = roller.getWeblogEntryManager(); //remove theme Assocs - Query themeAssocQuery = strategy.getNamedQuery("WeblogThemeAssoc.getThemeAssocsByWeblog"); + TypedQuery themeAssocQuery = strategy.getNamedQuery("WeblogThemeAssoc.getThemeAssocsByWeblog", + WeblogThemeAssoc.class); themeAssocQuery.setParameter(1,website); List assocResults = themeAssocQuery.getResultList(); for (WeblogThemeAssoc themeAssoc : assocResults) { @@ -124,7 +127,8 @@ public class JPAWeblogManagerImpl implem } // remove tags - Query tagQuery = strategy.getNamedQuery("WeblogEntryTag.getByWeblog"); + TypedQuery tagQuery = strategy.getNamedQuery("WeblogEntryTag.getByWeblog", + WeblogEntryTag.class); tagQuery.setParameter(1, website); List results = tagQuery.getResultList(); @@ -136,7 +140,7 @@ public class JPAWeblogManagerImpl implem } // remove site tag aggregates - List tags = emgr.getTags(website, null, null, 0, -1); + List tags = emgr.getTags(website, null, null, 0, -1); updateTagAggregates(tags); // delete all weblog tag aggregates @@ -152,7 +156,7 @@ public class JPAWeblogManagerImpl implem removeCounts.executeUpdate(); // Remove the website's ping queue entries - Query q = strategy.getNamedQuery("PingQueueEntry.getByWebsite"); + TypedQuery q = strategy.getNamedQuery("PingQueueEntry.getByWebsite", PingQueueEntry.class); q.setParameter(1, website); List queueEntries = q.getResultList(); for (Object obj : queueEntries) { @@ -166,24 +170,21 @@ public class JPAWeblogManagerImpl implem this.strategy.remove(autoPing); } - // TODO: can we eliminate this unnecessary flush with OpenJPA 1.0 - this.strategy.flush(); - // remove associated pages - Query pageQuery = strategy.getNamedQuery("WeblogTemplate.getByWebsite"); + TypedQuery pageQuery = strategy.getNamedQuery("WeblogTemplate.getByWebsite", + WeblogTemplate.class); pageQuery.setParameter(1, website); - List pages = pageQuery.getResultList(); - for (Object obj : pages) { - WeblogTemplate page = (WeblogTemplate) obj; + List pages = pageQuery.getResultList(); - //remove associated templateCode objects + for (WeblogTemplate page : pages) { + // remove associated templateCode objects this.removeTemplateCodeObjs(page); - this.strategy.remove(page); } // remove folders (including bookmarks) - Query folderQuery = strategy.getNamedQuery("WeblogBookmarkFolder.getByWebsite"); + TypedQuery folderQuery = strategy.getNamedQuery("WeblogBookmarkFolder.getByWebsite", + WeblogBookmarkFolder.class); folderQuery.setParameter(1, website); List folders = folderQuery.getResultList(); for (WeblogBookmarkFolder wbf : folders) { @@ -201,7 +202,7 @@ public class JPAWeblogManagerImpl implem this.strategy.flush(); // remove entries - Query refQuery = strategy.getNamedQuery("WeblogEntry.getByWebsite"); + TypedQuery refQuery = strategy.getNamedQuery("WeblogEntry.getByWebsite", WeblogEntry.class); refQuery.setParameter(1, website); List entries = refQuery.getResultList(); for (WeblogEntry entry : entries) { @@ -210,8 +211,7 @@ public class JPAWeblogManagerImpl implem this.strategy.flush(); // delete all weblog categories - Query removeCategories= strategy.getNamedUpdate( - "WeblogCategory.removeByWeblog"); + Query removeCategories= strategy.getNamedUpdate("WeblogCategory.removeByWeblog"); removeCategories.setParameter(1, website); removeCategories.executeUpdate(); @@ -227,11 +227,11 @@ public class JPAWeblogManagerImpl implem protected void updateTagAggregates(List tags) throws WebloggerException { for (TagStat stat : tags) { - Query query = strategy.getNamedUpdate( - "WeblogEntryTagAggregate.getByName&WebsiteNullOrderByLastUsedDesc"); + TypedQuery query = strategy.getNamedQueryCommitFirst( + "WeblogEntryTagAggregate.getByName&WebsiteNullOrderByLastUsedDesc", WeblogEntryTagAggregate.class); query.setParameter(1, stat.getName()); try { - WeblogEntryTagAggregate agg = (WeblogEntryTagAggregate)query.getSingleResult(); + WeblogEntryTagAggregate agg = query.getSingleResult(); agg.setTotal(agg.getTotal() - stat.getCount()); } catch (NoResultException ignored) { // nothing to update @@ -264,7 +264,6 @@ public class JPAWeblogManagerImpl implem } public void addWeblog(Weblog newWeblog) throws WebloggerException { - this.strategy.store(newWeblog); this.strategy.flush(); this.addWeblogContents(newWeblog); @@ -281,20 +280,22 @@ public class JPAWeblogManagerImpl implem String cats = WebloggerConfig.getProperty("newuser.categories"); WeblogCategory firstCat = null; - String[] splitcats = cats.split(","); - for (int i=0; i 1) { + if (rollitems.length > 1) { WeblogBookmark b = new WeblogBookmark( defaultFolder, rollitems[0], @@ -369,7 +370,7 @@ public class JPAWeblogManagerImpl implem if(this.weblogHandleToIdMap.containsKey(handle)) { Weblog weblog = this.getWeblog( - (String) this.weblogHandleToIdMap.get(handle)); + this.weblogHandleToIdMap.get(handle)); if(weblog != null) { // only return weblog if enabled status matches if(enabled == null || enabled.equals(weblog.getEnabled())) { @@ -382,11 +383,11 @@ public class JPAWeblogManagerImpl implem } } - Query query = strategy.getNamedQuery("Weblog.getByHandle"); + TypedQuery query = strategy.getNamedQuery("Weblog.getByHandle", Weblog.class); query.setParameter(1, handle); Weblog website; try { - website = (Weblog)query.getSingleResult(); + website = query.getSingleResult(); } catch (NoResultException e) { website = null; } @@ -414,7 +415,7 @@ public class JPAWeblogManagerImpl implem //if (endDate == null) endDate = new Date(); - List params = new ArrayList(); + List params = new ArrayList(); int size = 0; String queryString; StringBuilder whereClause = new StringBuilder(); @@ -427,7 +428,7 @@ public class JPAWeblogManagerImpl implem whereClause.append(" AND "); } params.add(size++, start); - whereClause.append(" w.dateCreated > ?" + size); + whereClause.append(" w.dateCreated > ?").append(size); } if (endDate != null) { Timestamp end = new Timestamp(endDate.getTime()); @@ -435,26 +436,26 @@ public class JPAWeblogManagerImpl implem whereClause.append(" AND "); } params.add(size++, end); - whereClause.append(" w.dateCreated < ?" + size); + whereClause.append(" w.dateCreated < ?").append(size); } if (enabled != null) { if (whereClause.length() > 0) { whereClause.append(" AND "); } params.add(size++, enabled); - whereClause.append(" w.enabled = ?" + size); + whereClause.append(" w.enabled = ?").append(size); } if (active != null) { if (whereClause.length() > 0) { whereClause.append(" AND "); } params.add(size++, active); - whereClause.append(" w.active = ?" + size); + whereClause.append(" w.active = ?").append(size); } whereClause.append(" ORDER BY w.dateCreated DESC"); - Query query = strategy.getDynamicQuery(queryString + whereClause.toString()); + TypedQuery query = strategy.getDynamicQuery(queryString + whereClause.toString(), Weblog.class); if (offset != 0) { query.setFirstResult(offset); } @@ -470,21 +471,22 @@ public class JPAWeblogManagerImpl implem public WeblogThemeAssoc getThemeAssoc(Weblog weblog , String type) throws WebloggerException { - if(weblog == null){ + if (weblog == null) { throw new WebloggerException("Weblog is null"); } - if(type == null){ + if (type == null) { throw new WebloggerException("Type is null"); } - Query query = strategy.getNamedQuery("WeblogThemeAssoc.getThemeAssocByType") ; + TypedQuery query = strategy.getNamedQuery("WeblogThemeAssoc.getThemeAssocByType", + WeblogThemeAssoc.class); query.setParameter(1,weblog); query.setParameter(2,type); - return (WeblogThemeAssoc) query.getSingleResult(); + return query.getSingleResult(); } public void saveThemeAssoc(WeblogThemeAssoc themeAssoc) throws WebloggerException { - this.strategy.store(themeAssoc); + this.strategy.store(themeAssoc); // update weblog last modified date. date updated by saveWebsite() roller.getWeblogManager().saveWeblog(themeAssoc.getWeblog()); @@ -544,11 +546,12 @@ public class JPAWeblogManagerImpl implem throw new WebloggerException("Pagelink is null"); } - Query query = strategy.getNamedQuery("WeblogTemplate.getByWebsite&Link"); + TypedQuery query = strategy.getNamedQuery("WeblogTemplate.getByWebsite&Link", + WeblogTemplate.class); query.setParameter(1, website); query.setParameter(2, pagelink); try { - return (WeblogTemplate) query.getSingleResult(); + return query.getSingleResult(); } catch (NoResultException e) { return null; } @@ -568,11 +571,12 @@ public class JPAWeblogManagerImpl implem throw new WebloggerException("Action name is null"); } - Query query = strategy.getNamedQuery("WeblogTemplate.getByAction"); + TypedQuery query = strategy.getNamedQuery("WeblogTemplate.getByAction", + WeblogTemplate.class); query.setParameter(1, website); query.setParameter(2, action); try { - return (WeblogTemplate)query.getSingleResult(); + return query.getSingleResult(); } catch (NoResultException e) { return null; } @@ -592,11 +596,12 @@ public class JPAWeblogManagerImpl implem throw new WebloggerException("Page name is null"); } - Query query = strategy.getNamedQuery("WeblogTemplate.getByWebsite&Name"); + TypedQuery query = strategy.getNamedQuery("WeblogTemplate.getByWebsite&Name", + WeblogTemplate.class); query.setParameter(1, website); query.setParameter(2, pagename); try { - return (WeblogTemplate)query.getSingleResult(); + return query.getSingleResult(); } catch (NoResultException e) { return null; } @@ -611,11 +616,12 @@ public class JPAWeblogManagerImpl implem throw new WebloggerException("Type is null"); } - Query query = strategy.getNamedQuery("WeblogThemeTemplateCode.getTemplateCodeByType"); + TypedQuery query = strategy.getNamedQuery("WeblogThemeTemplateCode.getTemplateCodeByType", + WeblogThemeTemplateCode.class); query.setParameter(1, templateId); query.setParameter(2, type); try { - return (WeblogThemeTemplateCode)query.getSingleResult(); + return query.getSingleResult(); } catch (NoResultException e) { return null; } @@ -628,18 +634,18 @@ public class JPAWeblogManagerImpl implem if (website == null) { throw new WebloggerException("website is null"); } - Query q = strategy.getNamedQuery( - "WeblogTemplate.getByWebsiteOrderByName"); + TypedQuery q = strategy.getNamedQuery( + "WeblogTemplate.getByWebsiteOrderByName", WeblogTemplate.class); q.setParameter(1, website); return q.getResultList(); } - public Map getWeblogHandleLetterMap() throws WebloggerException { + public Map getWeblogHandleLetterMap() throws WebloggerException { String lc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - Map results = new TreeMap(); - Query query = strategy.getNamedQuery( - "Weblog.getCountByHandleLike"); + Map results = new TreeMap(); + TypedQuery query = strategy.getNamedQuery( + "Weblog.getCountByHandleLike", Long.class); for (int i=0; i<26; i++) { char currentChar = lc.charAt(i); query.setParameter(1, currentChar + "%"); @@ -652,8 +658,8 @@ public class JPAWeblogManagerImpl implem public List getWeblogsByLetter(char letter, int offset, int length) throws WebloggerException { - Query query = strategy.getNamedQuery( - "Weblog.getByLetterOrderByHandle"); + TypedQuery query = strategy.getNamedQuery( + "Weblog.getByLetterOrderByHandle", Weblog.class); query.setParameter(1, letter + "%"); if (offset != 0) { query.setFirstResult(offset); @@ -668,7 +674,7 @@ public class JPAWeblogManagerImpl implem int offset, int length) throws WebloggerException { - Query query = null; + Query query; if (endDate == null) { endDate = new Date(); @@ -695,19 +701,22 @@ public class JPAWeblogManagerImpl implem } List queryResults = query.getResultList(); List results = new ArrayList(); - for (Iterator iter = queryResults.iterator(); iter.hasNext();) { - Object[] row = (Object[]) iter.next(); - StatCount sc = new StatCount( - (String)row[1], // website id - (String)row[2], // website handle - (String)row[3], // website name - "statCount.weblogCommentCountType", // stat type - ((Long)row[0])); // # comments - sc.setWeblogHandle((String)row[2]); - results.add(sc); + if (queryResults != null) { + for (Object obj : queryResults) { + Object[] row = (Object[]) obj; + StatCount sc = new StatCount( + (String)row[1], // website id + (String)row[2], // website handle + (String)row[3], // website name + "statCount.weblogCommentCountType", // stat type + ((Long)row[0])); // # comments + sc.setWeblogHandle((String)row[2]); + results.add(sc); + } } + // Original query ordered by desc # comments. - // JPA QL doesn't allow queries to be ordered by agregates; do it in memory + // JPA QL doesn't allow queries to be ordered by aggregates; do it in memory Collections.sort(results, STAT_COUNT_COUNT_REVERSE_COMPARATOR); return results; @@ -717,22 +726,19 @@ public class JPAWeblogManagerImpl implem * Get count of weblogs, active and inactive */ public long getWeblogCount() throws WebloggerException { - long ret = 0; - List results = strategy.getNamedQuery( - "Weblog.getCountAllDistinct").getResultList(); - - ret = (Long)results.get(0); - - return ret; + List results = strategy.getNamedQuery( + "Weblog.getCountAllDistinct", Long.class).getResultList(); + return results.get(0); } private void removeTemplateCodeObjs(WeblogTemplate page) throws WebloggerException { - Query codeQuery = strategy.getNamedQuery("WeblogThemeTemplateCode.getTemplateCodesByTemplateId"); + TypedQuery codeQuery = strategy.getNamedQuery( + "WeblogThemeTemplateCode.getTemplateCodesByTemplateId", WeblogThemeTemplateCode.class); codeQuery.setParameter(1, page.getId()); - List codeList = codeQuery.getResultList(); + List codeList = codeQuery.getResultList(); - for (Object obj : codeList) { - this.strategy.remove(obj); + for (WeblogThemeTemplateCode code : codeList) { + this.strategy.remove(code); } } Modified: roller/trunk/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWebloggerImpl.java URL: http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWebloggerImpl.java?rev=1606568&r1=1606567&r2=1606568&view=diff ============================================================================== --- roller/trunk/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWebloggerImpl.java (original) +++ roller/trunk/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWebloggerImpl.java Sun Jun 29 19:09:13 2014 @@ -45,8 +45,6 @@ import org.apache.roller.weblogger.busin @com.google.inject.Singleton public class JPAWebloggerImpl extends WebloggerImpl { - static final long serialVersionUID = 5256135928578074652L; - // a persistence utility class private final JPAPersistenceStrategy strategy; Modified: roller/trunk/app/src/main/java/org/apache/roller/weblogger/business/pings/AutoPingManager.java URL: http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/business/pings/AutoPingManager.java?rev=1606568&r1=1606567&r2=1606568&view=diff ============================================================================== --- roller/trunk/app/src/main/java/org/apache/roller/weblogger/business/pings/AutoPingManager.java (original) +++ roller/trunk/app/src/main/java/org/apache/roller/weblogger/business/pings/AutoPingManager.java Sun Jun 29 19:09:13 2014 @@ -68,7 +68,7 @@ public interface AutoPingManager { * @param autopings a Collection of AAutoPing/code> objects * @throws WebloggerException */ - void removeAutoPings(Collection autopings) throws WebloggerException; + void removeAutoPings(Collection autopings) throws WebloggerException; /** Modified: roller/trunk/app/src/main/java/org/apache/roller/weblogger/pojos/StatCountCountComparator.java URL: http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/pojos/StatCountCountComparator.java?rev=1606568&r1=1606567&r2=1606568&view=diff ============================================================================== --- roller/trunk/app/src/main/java/org/apache/roller/weblogger/pojos/StatCountCountComparator.java (original) +++ roller/trunk/app/src/main/java/org/apache/roller/weblogger/pojos/StatCountCountComparator.java Sun Jun 29 19:09:13 2014 @@ -24,7 +24,7 @@ import java.io.Serializable; /** * @author Markus Fuchs */ -public final class StatCountCountComparator implements Comparator, Serializable { +public final class StatCountCountComparator implements Comparator, Serializable { private static final long serialVersionUID = 4811314286365625712L; @@ -38,9 +38,7 @@ public final class StatCountCountCompara * @throws ClassCastException if arguments are not instances of StatCount * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) */ - public int compare(Object obj1, Object obj2) { - StatCount sc1 = (StatCount) obj1; - StatCount sc2 = (StatCount) obj2; + public int compare(StatCount sc1, StatCount sc2) { int compVal = sc1.getCount() < sc2.getCount() ? -1 : (sc1.getCount() == sc2.getCount() ? 0 : 1); Modified: roller/trunk/app/src/main/java/org/apache/roller/weblogger/pojos/TagStatComparator.java URL: http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/pojos/TagStatComparator.java?rev=1606568&r1=1606567&r2=1606568&view=diff ============================================================================== --- roller/trunk/app/src/main/java/org/apache/roller/weblogger/pojos/TagStatComparator.java (original) +++ roller/trunk/app/src/main/java/org/apache/roller/weblogger/pojos/TagStatComparator.java Sun Jun 29 19:09:13 2014 @@ -24,7 +24,7 @@ import java.util.Comparator; * @author Elias Torres (eliast@us.ibm.com) * */ -public class TagStatComparator implements Comparator, Serializable { +public class TagStatComparator implements Comparator, Serializable { private static final long serialVersionUID = -3272396777374523757L; @@ -39,10 +39,8 @@ public class TagStatComparator implement * @throws ClassCastException if arguments are not instances of TagStat * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) */ - public int compare(Object obj1, Object obj2) { - TagStat st1 = (TagStat) obj1; - TagStat st2 = (TagStat) obj2; - return st1.getName().compareToIgnoreCase(st2.getName()); + public int compare(TagStat ts1, TagStat ts2) { + return ts1.getName().compareToIgnoreCase(ts2.getName()); } } Modified: roller/trunk/app/src/main/java/org/apache/roller/weblogger/pojos/TagStatCountComparator.java URL: http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/pojos/TagStatCountComparator.java?rev=1606568&r1=1606567&r2=1606568&view=diff ============================================================================== --- roller/trunk/app/src/main/java/org/apache/roller/weblogger/pojos/TagStatCountComparator.java (original) +++ roller/trunk/app/src/main/java/org/apache/roller/weblogger/pojos/TagStatCountComparator.java Sun Jun 29 19:09:13 2014 @@ -24,7 +24,7 @@ import java.io.Serializable; /** * @author Markus Fuchs */ -public class TagStatCountComparator implements Comparator, Serializable { +public class TagStatCountComparator implements Comparator, Serializable { private static final long serialVersionUID = 1155112837815739929L; @@ -43,9 +43,7 @@ public class TagStatCountComparator impl * @throws ClassCastException if arguments are not instances of TagStat * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) */ - public int compare(Object obj1, Object obj2) { - TagStat st1 = (TagStat) obj1; - TagStat st2 = (TagStat) obj2; + public int compare(TagStat st1, TagStat st2) { int compVal = st1.getCount() < st2.getCount() ? -1 : (st1.getCount() == st2.getCount() ? 0 : 1); Modified: roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/rendering/model/PlanetModel.java URL: http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/rendering/model/PlanetModel.java?rev=1606568&r1=1606567&r2=1606568&view=diff ============================================================================== --- roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/rendering/model/PlanetModel.java (original) +++ roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/rendering/model/PlanetModel.java Sun Jun 29 19:09:13 2014 @@ -201,7 +201,7 @@ public class PlanetModel implements Mode try { PlanetManager planetManager = WebloggerFactory.getWeblogger().getPlanetManager(); Planet defaultPlanet = planetManager.getWeblogger(DEFAULT_PLANET_HANDLE); - Set groups = (Set)defaultPlanet.getGroups(); + Set groups = defaultPlanet.getGroups(); for (PlanetGroup group : groups) { // TODO needs pojo wrapping from planet list.add(group);