001: package com.technoetic.xplanner.charts;
002:
003: import java.text.SimpleDateFormat;
004: import java.util.Calendar;
005: import java.util.Date;
006: import java.util.List;
007: import java.util.Map;
008:
009: import net.sf.hibernate.Hibernate;
010: import net.sf.hibernate.type.Type;
011: import org.apache.log4j.Logger;
012: import org.jfree.data.DefaultCategoryDataset;
013: import de.laures.cewolf.DatasetProduceException;
014: import de.laures.cewolf.DatasetProducer;
015:
016: import com.technoetic.xplanner.db.hibernate.ThreadSession;
017: import com.technoetic.xplanner.domain.Iteration;
018: import com.technoetic.xplanner.util.TimeGenerator;
019:
020: public class DataSampleData implements DatasetProducer {
021: private Logger log = Logger.getLogger(getClass());
022: private Iteration iteration;
023: private String aspects;
024: private String categories;
025: private boolean includeWeekends;
026: public static final int DAY = 60 * 60 * 1000;
027:
028: public Object produceDataset(Map map)
029: throws DatasetProduceException {
030: DefaultCategoryDataset data = new DefaultCategoryDataset();
031: String[] aspectArray = aspects.split(",");
032: String[] categoryArray = categories.split(",");
033: for (int i = 0; i < categoryArray.length; i++) {
034: String category = categoryArray[i];
035: String aspect = aspectArray[i];
036: addData(data, aspect, category);
037: }
038:
039: return data;
040: }
041:
042: private void addData(DefaultCategoryDataset data, String aspect,
043: String category) {
044: try {
045: List samples = ThreadSession
046: .get()
047: .find(
048: " from s in "
049: + DataSample.class
050: + " where s.referenceId = ? and s.aspect = ? order by sampleTime",
051: new Object[] {
052: new Integer(iteration.getId()),
053: aspect },
054: new Type[] { Hibernate.INTEGER,
055: Hibernate.STRING });
056: log.debug("retrieved " + samples.size() + " samples");
057:
058: Calendar endDay = Calendar.getInstance();
059: if (samples.size() > 0) {
060: DataSample latestDataSample = (DataSample) samples
061: .get(samples.size() - 1);
062: endDay.setTime(getLatestDate(latestDataSample
063: .getSampleTime(), TimeGenerator.shiftDate(
064: iteration.getEndDate(), Calendar.DATE, 1)));
065: } else {
066: endDay.setTime(TimeGenerator.shiftDate(iteration
067: .getEndDate(), Calendar.DATE, 1));
068: }
069: Calendar currentDay = getMidnightOnIterationStart();
070:
071: while (currentDay.getTimeInMillis() <= endDay
072: .getTimeInMillis()) {
073: if (includeWeekends || !isWeekendDay(currentDay)) {
074: DataSample dataSample = closestSample(samples,
075: currentDay, 2 * DAY);
076: Number value = dataSample != null ? new Double(
077: dataSample.getValue()) : null;
078: data.addValue(value, category,
079: formatDay(currentDay));
080: }
081: currentDay.add(Calendar.DAY_OF_MONTH, 1);
082: }
083: } catch (Exception e) {
084: log.error("error loading data samples", e);
085: }
086: }
087:
088: protected Date getLatestDate(Date date1, Date date2) {
089: return date1.getTime() > date2.getTime() ? date1 : date2;
090: }
091:
092: private Calendar getMidnightOnIterationStart() {
093: Calendar currentDay = Calendar.getInstance();
094: currentDay.setTime(iteration.getStartDate());
095: currentDay.set(Calendar.HOUR_OF_DAY, 0);
096: currentDay.set(Calendar.MINUTE, 0);
097: currentDay.set(Calendar.SECOND, 0);
098: currentDay.set(Calendar.MILLISECOND, 0);
099: return currentDay;
100: }
101:
102: private SimpleDateFormat dateFormat = new SimpleDateFormat(
103: "yyyy-MM-dd");
104:
105: private String formatDay(Calendar currentDay) {
106: if (currentDay.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
107: return dateFormat.format(currentDay.getTime());
108: } else {
109: return Integer.toString(currentDay
110: .get(Calendar.DAY_OF_MONTH));
111: }
112: }
113:
114: private boolean isWeekendDay(Calendar currentDay) {
115: return currentDay.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
116: || currentDay.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY;
117: }
118:
119: private DataSample closestSample(List samples, Calendar currentDay,
120: long precision) {
121: long now = currentDay.getTimeInMillis();
122: long delta = Long.MAX_VALUE;
123: DataSample sample = null;
124: for (int i = 0; i < samples.size(); i++) {
125: DataSample dataSample = (DataSample) samples.get(i);
126: long d = Math.abs(now
127: - dataSample.getSampleTime().getTime());
128: if (d < precision && d < delta) {
129: delta = d;
130: sample = dataSample;
131: }
132: }
133: return sample;
134: }
135:
136: public boolean hasExpired(Map map, Date date) {
137: return true;
138: }
139:
140: public String getProducerId() {
141: return Long.toString(System.currentTimeMillis());
142: }
143:
144: public void setIteration(Iteration iteration) {
145: this .iteration = iteration;
146: }
147:
148: public void setAspects(String aspects) {
149: this .aspects = aspects;
150: }
151:
152: public void setCategories(String categories) {
153: this .categories = categories;
154: }
155:
156: public void setIncludeWeekends(boolean includeWeekends) {
157: this.includeWeekends = includeWeekends;
158: }
159: }
|