/** @inheritDoc */ public void incrementHitCount(Weblog weblog, int amount) throws WebloggerException { if (amount == 0) { throw new WebloggerException("Tag increment amount cannot be zero."); } if (weblog == null) { throw new WebloggerException("Website cannot be NULL."); } TypedQuery<WeblogHitCount> q = strategy.getNamedQuery("WeblogHitCount.getByWeblog", WeblogHitCount.class); q.setParameter(1, weblog); WeblogHitCount hitCount; try { hitCount = q.getSingleResult(); } catch (NoResultException e) { hitCount = null; } // create it if it doesn't exist if (hitCount == null && amount > 0) { hitCount = new WeblogHitCount(); hitCount.setWeblog(weblog); hitCount.setDailyHits(amount); strategy.store(hitCount); } else if (hitCount != null) { hitCount.setDailyHits(hitCount.getDailyHits() + amount); strategy.store(hitCount); } }
/** @inheritDoc */ public void resetHitCount(Weblog weblog) throws WebloggerException { TypedQuery<WeblogHitCount> q = strategy.getNamedQuery("WeblogHitCount.getByWeblog", WeblogHitCount.class); q.setParameter(1, weblog); WeblogHitCount hitCount; try { hitCount = q.getSingleResult(); hitCount.setDailyHits(0); strategy.store(hitCount); } catch (NoResultException e) { // ignore: no hit count for weblog } }
/** @inheritDoc */ public long getCommentCount(Weblog website) throws WebloggerException { TypedQuery<Long> q = strategy.getNamedQuery("WeblogEntryComment.getCountDistinctByWebsite&Status", Long.class); q.setParameter(1, website); q.setParameter(2, ApprovalStatus.APPROVED); return q.getResultList().get(0); }
/** @inheritDoc */ public String createAnchor(WeblogEntry entry) throws WebloggerException { // Check for uniqueness of anchor String base = entry.createAnchorBase(); String name = base; int count = 0; while (true) { if (count > 0) { name = base + count; } TypedQuery<WeblogEntry> q = strategy.getNamedQuery("WeblogEntry.getByWebsite&Anchor", WeblogEntry.class); q.setParameter(1, entry.getWebsite()); q.setParameter(2, name); List results = q.getResultList(); if (results.size() < 1) { break; } else { count++; } } return name; }
/** @inheritDoc */ public List<WeblogEntryComment> getComments(CommentSearchCriteria csc) throws WebloggerException { List<Object> params = new ArrayList<Object>(); int size = 0; StringBuilder queryString = new StringBuilder(); queryString.append("SELECT c FROM WeblogEntryComment c "); StringBuilder whereClause = new StringBuilder(); if (csc.getEntry() != null) { params.add(size++, csc.getEntry()); whereClause.append("c.weblogEntry = ?").append(size); } else if (csc.getWeblog() != null) { params.add(size++, csc.getWeblog()); whereClause.append("c.weblogEntry.website = ?").append(size); } if (csc.getSearchText() != null) { params.add(size++, "%" + csc.getSearchText().toUpperCase() + "%"); appendConjuctionToWhereclause(whereClause, "upper(c.content) LIKE ?").append(size); } if (csc.getStartDate() != null) { Timestamp start = new Timestamp(csc.getStartDate().getTime()); params.add(size++, start); appendConjuctionToWhereclause(whereClause, "c.postTime >= ?").append(size); } if (csc.getEndDate() != null) { Timestamp end = new Timestamp(csc.getEndDate().getTime()); params.add(size++, end); appendConjuctionToWhereclause(whereClause, "c.postTime <= ?").append(size); } if (csc.getStatus() != null) { params.add(size++, csc.getStatus()); appendConjuctionToWhereclause(whereClause, "c.status = ?").append(size); } if (whereClause.length() != 0) { queryString.append(" WHERE ").append(whereClause); } if (csc.isReverseChrono()) { queryString.append(" ORDER BY c.postTime DESC"); } else { queryString.append(" ORDER BY c.postTime ASC"); } 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()); } for (int i = 0; i < params.size(); i++) { query.setParameter(i + 1, params.get(i)); } return query.getResultList(); }
/** @inheritDoc */ public long getEntryCount(Weblog website) throws WebloggerException { TypedQuery<Long> q = strategy.getNamedQuery("WeblogEntry.getCountDistinctByStatus&Website", Long.class); q.setParameter(1, PubStatus.PUBLISHED); q.setParameter(2, website); return q.getResultList().get(0); }
/** Save all properties. */ public void saveProperties(Map properties) throws PlanetException { // just go through the list and saveProperties each property Iterator props = properties.values().iterator(); while (props.hasNext()) { strategy.store((RuntimeConfigProperty) props.next()); } }
/** @inheritDoc */ public WeblogHitCount getHitCountByWeblog(Weblog weblog) throws WebloggerException { TypedQuery<WeblogHitCount> q = strategy.getNamedQuery("WeblogHitCount.getByWeblog", WeblogHitCount.class); q.setParameter(1, weblog); try { return q.getSingleResult(); } catch (NoResultException e) { return null; } }
/** @inheritDoc */ public boolean isWeblogCategoryInUse(WeblogCategory cat) throws WebloggerException { if (cat.getWeblog().getBloggerCategory().equals(cat)) { return true; } TypedQuery<WeblogEntry> q = strategy.getNamedQuery("WeblogEntry.getByCategory", WeblogEntry.class); q.setParameter(1, cat); int entryCount = q.getResultList().size(); return entryCount > 0; }
/** @inheritDoc */ @Override public List<WeblogCategory> getWeblogCategories(Weblog website) throws WebloggerException { if (website == null) { throw new WebloggerException("website is null"); } TypedQuery<WeblogCategory> q = strategy.getNamedQuery("WeblogCategory.getByWeblog", WeblogCategory.class); q.setParameter(1, website); return q.getResultList(); }
/** @inheritDoc */ public List<WeblogEntry> getWeblogEntriesPinnedToMain(Integer max) throws WebloggerException { TypedQuery<WeblogEntry> query = strategy.getNamedQuery( "WeblogEntry.getByPinnedToMain&statusOrderByPubTimeDesc", WeblogEntry.class); query.setParameter(1, Boolean.TRUE); query.setParameter(2, PubStatus.PUBLISHED); if (max != null) { query.setMaxResults(max); } return query.getResultList(); }
/** @inheritDoc */ public void applyCommentDefaultsToEntries(Weblog website) throws WebloggerException { if (LOG.isDebugEnabled()) { LOG.debug("applyCommentDefaults"); } // TODO: Non-standard JPA bulk update, using parameter values in set clause Query q = strategy.getNamedUpdate("WeblogEntry.updateAllowComments&CommentDaysByWebsite"); q.setParameter(1, website.getDefaultAllowComments()); q.setParameter(2, website.getDefaultCommentDays()); q.setParameter(3, website); q.executeUpdate(); }
/** @inheritDoc */ public WeblogCategory getWeblogCategoryByName(Weblog weblog, String categoryName) throws WebloggerException { TypedQuery<WeblogCategory> q = strategy.getNamedQuery("WeblogCategory.getByWeblog&Name", WeblogCategory.class); q.setParameter(1, weblog); q.setParameter(2, categoryName); try { return q.getSingleResult(); } catch (NoResultException e) { return null; } }
/** @inheritDoc */ public WeblogEntry getWeblogEntryByAnchor(Weblog website, String anchor) throws WebloggerException { if (website == null) { throw new WebloggerException("Website is null"); } if (anchor == null) { throw new WebloggerException("Anchor is null"); } // mapping key is combo of weblog + 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(this.entryAnchorToIdMap.get(mappingKey)); if (entry != null) { LOG.debug("entryAnchorToIdMap CACHE HIT - " + mappingKey); return entry; } else { // mapping hit with lookup miss? mapping must be old, remove it this.entryAnchorToIdMap.remove(mappingKey); } } // cache failed, do lookup TypedQuery<WeblogEntry> q = strategy.getNamedQuery( "WeblogEntry.getByWebsite&AnchorOrderByPubTimeDesc", WeblogEntry.class); q.setParameter(1, website); q.setParameter(2, anchor); WeblogEntry entry; try { entry = q.getSingleResult(); } catch (NoResultException e) { entry = null; } // add mapping to cache if (entry != null) { LOG.debug("entryAnchorToIdMap CACHE MISS - " + mappingKey); this.entryAnchorToIdMap.put(mappingKey, entry.getId()); } return entry; }
/** @inheritDoc */ public boolean getTagComboExists(List tags, Weblog weblog) throws WebloggerException { if (tags == null || tags.size() == 0) { return false; } StringBuilder queryString = new StringBuilder(); queryString.append("SELECT DISTINCT w.name "); queryString.append("FROM WeblogEntryTagAggregate w WHERE w.name IN ("); // Append tags as parameter markers to avoid potential escaping issues // The IN clause would be of form (?1, ?2, ?3, ..) List<Object> params = new ArrayList<Object>(tags.size() + 1); final String paramSeparator = ", "; int i; for (i = 0; i < tags.size(); i++) { queryString.append('?').append(i + 1).append(paramSeparator); params.add(tags.get(i)); } // Remove the trailing paramSeparator queryString.delete(queryString.length() - paramSeparator.length(), queryString.length()); // Close the brace of IN clause queryString.append(')'); if (weblog != null) { queryString.append(" AND w.weblog = ?").append(i + 1); params.add(weblog); } else { queryString.append(" AND w.weblog IS NULL"); } TypedQuery<String> q = strategy.getDynamicQuery(queryString.toString(), String.class); for (int j = 0; j < params.size(); j++) { q.setParameter(j + 1, params.get(j)); } List<String> 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 // instead of objects as done currently return (results != null && results.size() == tags.size()); }
/** * Retrieve all properties. * * <p>Properties are returned in a Map to make them easy to lookup. The Map uses the property name * as the key and the RuntimeConfigProperty object as the value. */ public Map getProperties() throws PlanetException { HashMap props = new HashMap(); List list = strategy.getNamedQuery("RuntimeConfigProperty.getAll").getResultList(); /* * for convenience sake we are going to put the list of props * into a map for users to access it. The value element of the * hash still needs to be the RuntimeConfigProperty object so that * we can save the elements again after they have been updated */ RuntimeConfigProperty prop = null; Iterator it = list.iterator(); while (it.hasNext()) { prop = (RuntimeConfigProperty) it.next(); props.put(prop.getName(), prop); } return props; }
/** @inheritDoc */ public List<WeblogHitCount> getHotWeblogs(int sinceDays, int offset, int length) throws WebloggerException { // figure out start date Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.add(Calendar.DATE, -1 * sinceDays); Date startDate = cal.getTime(); TypedQuery<WeblogHitCount> query = strategy.getNamedQuery( "WeblogHitCount.getByWeblogEnabledTrueAndActiveTrue&DailyHitsGreaterThenZero&WeblogLastModifiedGreaterOrderByDailyHitsDesc", WeblogHitCount.class); query.setParameter(1, startDate); if (offset != 0) { query.setFirstResult(offset); } if (length != -1) { query.setMaxResults(length); } return query.getResultList(); }
public List getNextPrevEntries( WeblogEntry current, String catName, String locale, int maxEntries, boolean next) throws WebloggerException { if (current == null) { LOG.debug("current WeblogEntry cannot be null"); return Collections.emptyList(); } TypedQuery<WeblogEntry> query; WeblogCategory category; List<Object> params = new ArrayList<Object>(); int size = 0; String queryString = "SELECT e FROM WeblogEntry e WHERE "; StringBuilder whereClause = new StringBuilder(); params.add(size++, current.getWebsite()); whereClause.append("e.website = ?").append(size); params.add(size++, PubStatus.PUBLISHED); whereClause.append(" AND e.status = ?").append(size); if (next) { params.add(size++, current.getPubTime()); whereClause.append(" AND e.pubTime > ?").append(size); } else { // pub time null if current article not yet published, in Draft view if (current.getPubTime() != null) { params.add(size++, current.getPubTime()); whereClause.append(" AND e.pubTime < ?").append(size); } } if (catName != null) { category = getWeblogCategoryByName(current.getWebsite(), catName); if (category != null) { params.add(size++, category); whereClause.append(" AND e.category = ?").append(size); } else { throw new WebloggerException("Cannot find category: " + catName); } } if (locale != null) { params.add(size++, locale + '%'); whereClause.append(" AND e.locale like ?").append(size); } if (next) { whereClause.append(" ORDER BY e.pubTime ASC"); } else { whereClause.append(" ORDER BY e.pubTime DESC"); } query = strategy.getDynamicQuery(queryString + whereClause.toString(), WeblogEntry.class); for (int i = 0; i < params.size(); i++) { query.setParameter(i + 1, params.get(i)); } query.setMaxResults(maxEntries); return query.getResultList(); }
/** @inheritDoc */ public List<StatCount> getMostCommentedWeblogEntries( Weblog website, Date startDate, Date endDate, int offset, int length) throws WebloggerException { TypedQuery<WeblogEntryComment> query; List queryResults; Timestamp end = new Timestamp(endDate != null ? endDate.getTime() : new Date().getTime()); if (website != null) { if (startDate != null) { Timestamp start = new Timestamp(startDate.getTime()); query = strategy.getNamedQuery( "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.class); query.setParameter(1, website); query.setParameter(2, end); } } else { if (startDate != null) { Timestamp start = new Timestamp(startDate.getTime()); query = strategy.getNamedQuery( "WeblogEntryComment.getMostCommentedWeblogEntryByEndDate&StartDate", WeblogEntryComment.class); query.setParameter(1, end); query.setParameter(2, start); } else { query = strategy.getNamedQuery( "WeblogEntryComment.getMostCommentedWeblogEntryByEndDate", WeblogEntryComment.class); query.setParameter(1, end); } } if (offset != 0) { query.setFirstResult(offset); } if (length != -1) { query.setMaxResults(length); } queryResults = query.getResultList(); List<StatCount> results = new ArrayList<StatCount>(); 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 Collections.sort(results, STAT_COUNT_COUNT_REVERSE_COMPARATOR); return results; }
/** @inheritDoc */ public WeblogEntry getWeblogEntry(String id) throws WebloggerException { return (WeblogEntry) strategy.load(WeblogEntry.class, id); }
/** Save a single property. */ public void saveProperty(RuntimeConfigProperty property) throws PlanetException { strategy.store(property); }
/** * This method maintains the tag aggregate table up-to-date with total counts. More specifically * every time this method is called it will act upon exactly two rows in the database * (tag,website,count), one with website matching the argument passed and one where website is * null. If the count ever reaches zero, the row must be deleted. * * @param name The tag name * @param website The website to used when updating the stats. * @param amount The amount to increment the tag count (it can be positive or negative). * @throws WebloggerException */ private void updateTagCount(String name, Weblog website, int amount) throws WebloggerException { if (amount == 0) { throw new WebloggerException("Tag increment amount cannot be zero."); } if (website == null) { throw new WebloggerException("Website cannot be NULL."); } // The reason why add order lastUsed desc is to make sure we keep picking the most recent // 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 TypedQuery<WeblogEntryTagAggregate> weblogQuery = strategy.getNamedQuery( "WeblogEntryTagAggregate.getByName&WebsiteOrderByLastUsedDesc", WeblogEntryTagAggregate.class); weblogQuery.setParameter(1, name); weblogQuery.setParameter(2, website); WeblogEntryTagAggregate weblogTagData; try { weblogTagData = weblogQuery.getSingleResult(); } catch (NoResultException e) { weblogTagData = null; } TypedQuery<WeblogEntryTagAggregate> siteQuery = strategy.getNamedQuery( "WeblogEntryTagAggregate.getByName&WebsiteNullOrderByLastUsedDesc", WeblogEntryTagAggregate.class); siteQuery.setParameter(1, name); WeblogEntryTagAggregate siteTagData; try { siteTagData = siteQuery.getSingleResult(); } catch (NoResultException e) { siteTagData = null; } Timestamp lastUsed = new Timestamp((new Date()).getTime()); // create it only if we are going to need it. if (weblogTagData == null && amount > 0) { weblogTagData = new WeblogEntryTagAggregate(null, website, name, amount); weblogTagData.setLastUsed(lastUsed); strategy.store(weblogTagData); } else if (weblogTagData != null) { weblogTagData.setTotal(weblogTagData.getTotal() + amount); weblogTagData.setLastUsed(lastUsed); strategy.store(weblogTagData); } // create it only if we are going to need it. if (siteTagData == null && amount > 0) { siteTagData = new WeblogEntryTagAggregate(null, null, name, amount); siteTagData.setLastUsed(lastUsed); strategy.store(siteTagData); } else if (siteTagData != null) { siteTagData.setTotal(siteTagData.getTotal() + amount); siteTagData.setLastUsed(lastUsed); strategy.store(siteTagData); } // delete all bad counts Query removeq = strategy.getNamedUpdate("WeblogEntryTagAggregate.removeByTotalLessEqual"); removeq.setParameter(1, 0); removeq.executeUpdate(); }
/** @inheritDoc */ public WeblogHitCount getHitCount(String id) throws WebloggerException { // do lookup return (WeblogHitCount) strategy.load(WeblogHitCount.class, id); }
/** @inheritDoc */ public List<WeblogEntry> getWeblogEntries(WeblogEntrySearchCriteria wesc) throws WebloggerException { WeblogCategory cat = null; if (StringUtils.isNotEmpty(wesc.getCatName()) && wesc.getWeblog() != null) { cat = getWeblogCategoryByName(wesc.getWeblog(), wesc.getCatName()); } List<Object> params = new ArrayList<Object>(); int size = 0; StringBuilder queryString = new StringBuilder(); if (wesc.getTags() == null || wesc.getTags().size() == 0) { queryString.append("SELECT e FROM WeblogEntry e WHERE "); } else { queryString.append("SELECT e FROM WeblogEntry e JOIN e.tags t WHERE "); queryString.append("("); for (int i = 0; i < wesc.getTags().size(); i++) { if (i != 0) { queryString.append(" OR "); } params.add(size++, wesc.getTags().get(i)); queryString.append(" t.name = ?").append(size); } queryString.append(") AND "); } if (wesc.getWeblog() != null) { params.add(size++, wesc.getWeblog().getId()); queryString.append("e.website.id = ?").append(size); } else { params.add(size++, Boolean.TRUE); queryString.append("e.website.visible = ?").append(size); } if (wesc.getUser() != null) { params.add(size++, wesc.getUser().getUserName()); queryString.append(" AND e.creatorUserName = ?").append(size); } if (wesc.getStartDate() != null) { Timestamp start = new Timestamp(wesc.getStartDate().getTime()); params.add(size++, start); queryString.append(" AND e.pubTime >= ?").append(size); } if (wesc.getEndDate() != null) { Timestamp end = new Timestamp(wesc.getEndDate().getTime()); params.add(size++, end); queryString.append(" AND e.pubTime <= ?").append(size); } if (cat != null) { params.add(size++, cat.getId()); queryString.append(" AND e.category.id = ?").append(size); } if (wesc.getStatus() != null) { params.add(size++, wesc.getStatus()); queryString.append(" AND e.status = ?").append(size); } if (wesc.getLocale() != null) { params.add(size++, wesc.getLocale() + '%'); queryString.append(" AND e.locale like ?").append(size); } if (StringUtils.isNotEmpty(wesc.getText())) { params.add(size++, '%' + wesc.getText() + '%'); queryString.append(" AND ( e.text LIKE ?").append(size); queryString.append(" OR e.summary LIKE ?").append(size); queryString.append(" OR e.title LIKE ?").append(size); queryString.append(") "); } if (wesc.getSortBy() != null && wesc.getSortBy().equals(WeblogEntrySearchCriteria.SortBy.UPDATE_TIME)) { queryString.append(" ORDER BY e.updateTime "); } else { queryString.append(" ORDER BY e.pubTime "); } if (wesc.getSortOrder() != null && wesc.getSortOrder().equals(WeblogEntrySearchCriteria.SortOrder.ASCENDING)) { queryString.append("ASC "); } else { queryString.append("DESC "); } TypedQuery<WeblogEntry> query = strategy.getDynamicQuery(queryString.toString(), WeblogEntry.class); for (int i = 0; i < params.size(); i++) { query.setParameter(i + 1, params.get(i)); } if (wesc.getOffset() != 0) { query.setFirstResult(wesc.getOffset()); } if (wesc.getMaxResults() != -1) { query.setMaxResults(wesc.getMaxResults()); } return query.getResultList(); }
/** @inheritDoc */ public List<TagStat> getPopularTags(Weblog website, Date startDate, int offset, int limit) throws WebloggerException { TypedQuery<TagStat> query; List queryResults; if (website != null) { if (startDate != null) { Timestamp start = new Timestamp(startDate.getTime()); query = strategy.getNamedQuery( "WeblogEntryTagAggregate.getPopularTagsByWebsite&StartDate", TagStat.class); query.setParameter(1, website); query.setParameter(2, start); } else { query = strategy.getNamedQuery( "WeblogEntryTagAggregate.getPopularTagsByWebsite", TagStat.class); query.setParameter(1, website); } } else { if (startDate != null) { Timestamp start = new Timestamp(startDate.getTime()); query = strategy.getNamedQuery( "WeblogEntryTagAggregate.getPopularTagsByWebsiteNull&StartDate", TagStat.class); query.setParameter(1, start); } else { query = strategy.getNamedQuery( "WeblogEntryTagAggregate.getPopularTagsByWebsiteNull", TagStat.class); } } if (offset != 0) { query.setFirstResult(offset); } if (limit != -1) { query.setMaxResults(limit); } queryResults = query.getResultList(); double min = Integer.MAX_VALUE; double max = Integer.MIN_VALUE; List<TagStat> results = new ArrayList<TagStat>(limit >= 0 ? limit : 25); 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); double range = Math.max(.01, max - min) * 1.0001; for (TagStat t : results) { t.setIntensity((int) (1 + Math.floor(5 * (Math.log(1 + t.getCount()) - min) / range))); } // sort results by name, because query had to sort by total Collections.sort(results, TAG_STAT_NAME_COMPARATOR); return results; }
/** @inheritDoc */ public void resetAllHitCounts() throws WebloggerException { Query q = strategy.getNamedUpdate("WeblogHitCount.updateDailyHitCountZero"); q.executeUpdate(); }
/** @inheritDoc */ public List<TagStat> getTags( Weblog website, String sortBy, String startsWith, int offset, int limit) throws WebloggerException { Query query; List queryResults; boolean sortByName = sortBy == null || !sortBy.equals("count"); List<Object> params = new ArrayList<Object>(); int size = 0; StringBuilder queryString = new StringBuilder(); queryString.append("SELECT w.name, SUM(w.total) FROM WeblogEntryTagAggregate w WHERE "); if (website != null) { params.add(size++, website.getId()); queryString.append(" w.weblog.id = ?").append(size); } else { queryString.append(" w.weblog IS NULL"); } if (startsWith != null && startsWith.length() > 0) { params.add(size++, startsWith + '%'); queryString.append(" AND w.name LIKE ?").append(size); } if (sortBy != null && sortBy.equals("count")) { sortBy = "w.total DESC"; } else { sortBy = "w.name"; } queryString.append(" GROUP BY w.name, w.total ORDER BY ").append(sortBy); query = strategy.getDynamicQuery(queryString.toString()); 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); } queryResults = query.getResultList(); List<TagStat> results = new ArrayList<TagStat>(); 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) { Collections.sort(results, TAG_STAT_NAME_COMPARATOR); } else { Collections.sort(results, TAG_STAT_COUNT_REVERSE_COMPARATOR); } return results; }
/** Retrieve a single property by name. */ public RuntimeConfigProperty getProperty(String name) throws PlanetException { return (RuntimeConfigProperty) strategy.load(RuntimeConfigProperty.class, name); }