01: /*
02: * Copyright (c) Mateusz Prokopowicz. All Rights Reserved.
03: */
04:
05: package com.technoetic.xplanner.charts;
06:
07: import java.util.Calendar;
08: import java.util.Date;
09: import java.util.Iterator;
10: import java.util.List;
11:
12: import org.apache.log4j.Logger;
13: import org.springframework.orm.hibernate.support.HibernateDaoSupport;
14:
15: import com.technoetic.xplanner.Command;
16: import com.technoetic.xplanner.domain.Iteration;
17: import com.technoetic.xplanner.util.TimeGenerator;
18:
19: public class DataSamplingCommand extends HibernateDaoSupport implements
20: Command {
21: public static final Logger LOG = Logger
22: .getLogger(DataSamplingCommand.class);
23: public static final String ITERATION_TO_SAMPLE_QUERY = "com.technoetic.xplanner.domain.IterationToSample";
24: private DataSampler dataSampler;
25: private TimeGenerator timeGenerator;
26:
27: public void setTimeGenerator(TimeGenerator timeGenerator) {
28: this .timeGenerator = timeGenerator;
29: }
30:
31: public void setDataSampler(DataSampler dataSampler) {
32: this .dataSampler = dataSampler;
33: }
34:
35: public void execute() {
36: final Date samplingDate = timeGenerator.getTodaysMidnight();
37: List iterations = getIterationList(samplingDate);
38: for (int i = 0; i < iterations.size(); i++) {
39: try {
40: final Iteration iteration = (Iteration) iterations
41: .get(i);
42: LOG.debug("Generate datasamples at "
43: + samplingDate.toString());
44: LOG.debug(" for iteration: projectId ["
45: + iteration.getProjectId() + "], id ["
46: + iteration.getId() + "], name ["
47: + iteration.getName() + "]");
48:
49: dataSampler.generateDataSamples(iteration);
50:
51: } catch (Exception e) {
52: LOG.error("Error saving datasamples " + e.getMessage());
53: LOG.debug("Stack trace: ", e);
54: }
55: }
56: }
57:
58: public List getIterationList(Date samplingDate) {
59: Date prevSamplingDate = TimeGenerator.shiftDate(samplingDate,
60: Calendar.DAY_OF_MONTH, -1);
61: List iterationToSampleList = getHibernateTemplate()
62: .findByNamedQueryAndNamedParam(
63: ITERATION_TO_SAMPLE_QUERY,
64: new String[] { "prevSamplingDate",
65: "samplingDate" },
66: new Object[] { prevSamplingDate, samplingDate });
67: for (Iterator iterator = iterationToSampleList.iterator(); iterator
68: .hasNext();) {
69: Iteration iteration = (Iteration) iterator.next();
70: LOG.debug("Iteration " + iteration.getName() + " contains "
71: + iteration.getUserStories().size() + " stories.");
72: }
73: LOG.debug("Iterations to sample on " + samplingDate + " size: "
74: + iterationToSampleList.size());
75: return iterationToSampleList;
76:
77: }
78: }
|