01: package com.technoetic.xplanner.charts;
02:
03: import org.jfree.data.DefaultPieDataset;
04: import com.technoetic.xplanner.db.hibernate.ThreadSession;
05: import de.laures.cewolf.DatasetProduceException;
06: import de.laures.cewolf.DatasetProducer;
07: import net.sf.hibernate.Session;
08:
09: import java.util.Date;
10: import java.util.List;
11: import java.util.Map;
12:
13: public class PieChartQuery implements DatasetProducer {
14: private String producerId = Long.toString(System
15: .currentTimeMillis());
16: private String query;
17: private boolean includeCountInLabel;
18:
19: public Object produceDataset(Map parameters)
20: throws DatasetProduceException {
21: List results = null;
22: try {
23: results = null;
24: Session session = ThreadSession.get();
25: results = session.find(query);
26: } catch (Exception e) {
27: throw new DatasetProduceException(e.getMessage());
28: }
29: DefaultPieDataset data = new DefaultPieDataset();
30: for (int i = 0; i < results.size(); i++) {
31: Object[] objects = (Object[]) results.get(i);
32: data.setValue(getLabel(objects), getCount(objects));
33: }
34: return data;
35: }
36:
37: private int getCount(Object[] objects) {
38: return ((Number) objects[1]).intValue();
39: }
40:
41: private Comparable getLabel(Object[] objects) {
42: return (Comparable) objects[0]
43: + (includeCountInLabel ? " (" + getCount(objects) + ")"
44: : "");
45: }
46:
47: public boolean hasExpired(Map map, Date date) {
48: return true;
49: }
50:
51: public String getProducerId() {
52: return producerId;
53: }
54:
55: public void setQuery(String query) {
56: this .query = query;
57: }
58:
59: public void setIncludeCountInLabel(boolean includeCountInLabel) {
60: this.includeCountInLabel = includeCountInLabel;
61: }
62: }
|