public List getPopularTags(Weblog website, Date startDate, int offset, int
limit)
    throws WebloggerException {
        Query query = null;
        List queryResults = null;

        if (website != null) {
            if (startDate != null) {
                Timestamp start = new Timestamp(startDate.getTime());
                query = strategy.getNamedQuery(

"WeblogEntryTagAggregate.getPopularTagsByWebsite&StartDate");
                query.setParameter(1, website);
                query.setParameter(2, start);
            } else {
                query = strategy.getNamedQuery(
                        "WeblogEntryTagAggregate.getPopularTagsByWebsite");
                query.setParameter(1, website);
            }
        } else {
            if (startDate != null) {
                Timestamp start = new Timestamp(startDate.getTime());
                query = strategy.getNamedQuery(

"WeblogEntryTagAggregate.getPopularTagsByWebsiteNull&StartDate");
                query.setParameter(1, start);
            } else {
                query = strategy.getNamedQuery(

"WeblogEntryTagAggregate.getPopularTagsByWebsiteNull");
            }
        }
        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 results = new ArrayList(limit);    // if the limit is -1 it
will thrown the exception and -1 is for no limit*

        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);
        }

        min = Math.log(1+min);
        max = Math.log(1+max);

        double range = Math.max(.01, max - min) * 1.0001;

        for (Iterator iter = results.iterator(); iter.hasNext(); ) {
            TagStat t = (TagStat) iter.next();
            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, tagStatNameComparator);

        return results;
    }

Reply via email to