001: /*
002: * Copyright (c) 2005, Your Corporation. All Rights Reserved.
003: */
004:
005: package com.technoetic.xplanner.charts;
006:
007: import java.util.Calendar;
008: import java.util.Date;
009: import java.util.List;
010: import java.util.Properties;
011:
012: import net.sf.hibernate.Hibernate;
013: import net.sf.hibernate.type.Type;
014: import org.apache.log4j.Logger;
015: import org.springframework.orm.hibernate.HibernateOperations;
016: import org.springframework.orm.hibernate.support.HibernateDaoSupport;
017:
018: import com.technoetic.xplanner.domain.Iteration;
019: import com.technoetic.xplanner.domain.IterationStatus;
020: import com.technoetic.xplanner.util.TimeGenerator;
021:
022: /**
023: * Created by IntelliJ IDEA.
024: * User: sg620641
025: * Date: Dec 9, 2005
026: * Time: 4:07:47 PM
027: * To change this template use File | Settings | File Templates.
028: */
029: public class DataSamplerImpl extends HibernateDaoSupport implements
030: DataSampler {
031: protected final Logger LOG = Logger
032: .getLogger(DataSamplerImpl.class);
033: protected TimeGenerator timeGenerator;
034: private HibernateOperations hibernateOperations;
035: public static final String DATASAMPLE_FOR_OBJECT_IN_TIME_QUERY = "com.technoetic.xplanner.domain.DataSamplesForObjectInTime";
036: public static final String IS_TODAY_SAMPLE_ALREADY_GENERATED_QUERY = "com.technoetic.xplanner.domain.IsTodaySampleAlreadyGenerated";
037: public static final String GET_DATASAMPLE_QUERY = "com.technoetic.xplanner.domain.GetDataSample";
038: public static final String AUTOMATICALLY_EXTEND_END_DATE_PROP = "iteration.automatically.extend.endDate";
039: private Properties properties;
040:
041: public void setProperties(Properties properties) {
042: this .properties = properties;
043: }
044:
045: private void generateDataSamples(Iteration iteration, Date date) {
046: saveSamples(date, iteration);
047: extendIterationEndDateIfNeeded(iteration, date);
048: }
049:
050: public void generateDataSamples(Iteration iteration) {
051: Date todayMidnight = timeGenerator.getTodaysMidnight();
052: Date tomorrowMidnight = TimeGenerator.shiftDate(todayMidnight,
053: Calendar.DATE, 1);
054: generateDataSamples(iteration, tomorrowMidnight);
055: }
056:
057: public void generateOpeningDataSamples(Iteration iteration) {
058: Date date = timeGenerator.getTodaysMidnight();
059: generateDataSamples(iteration, date);
060: }
061:
062: public void generateClosingDataSamples(Iteration iteration) {
063: if (iteration.getEndDate().before(
064: timeGenerator.getCurrentTime())) {
065: Date todayMidnight = timeGenerator.getTodaysMidnight();
066: generateDataSamples(iteration, todayMidnight);
067: } else {
068: generateDataSamples(iteration);
069: }
070: }
071:
072: public void setTimeGenerator(TimeGenerator timeGenerator) {
073: this .timeGenerator = timeGenerator;
074: }
075:
076: public HibernateOperations getHibernateOperations() {
077: if (hibernateOperations != null) {
078: return hibernateOperations;
079: }
080: return getHibernateTemplate();
081: }
082:
083: public void setHibernateOperations(
084: HibernateOperations hibernateOperations) {
085: this .hibernateOperations = hibernateOperations;
086: }
087:
088: protected void extendIterationEndDateIfNeeded(Iteration iteration,
089: Date midnight) {
090: boolean automaticallyExtendEndDate = Boolean.valueOf(
091: properties.getProperty(
092: AUTOMATICALLY_EXTEND_END_DATE_PROP, "false"))
093: .booleanValue();
094: if (automaticallyExtendEndDate
095: && IterationStatus.ACTIVE.equals(iteration.getStatus())
096: && iteration.getEndDate().compareTo(midnight) < 0) {
097: LOG.debug("Extend iteration end day to " + midnight);
098: iteration.setEndDate(midnight);
099: getHibernateOperations().save(iteration);
100: }
101: }
102:
103: protected void saveSamples(Date date, Iteration iteration) {
104: saveSample(date, iteration, "estimatedHours", iteration
105: .getEstimatedHours());
106: saveSample(date, iteration, "actualHours", iteration
107: .getCachedActualHours());
108: saveSample(date, iteration, "remainingHours", iteration
109: .getTaskRemainingHours());
110: }
111:
112: protected void saveSample(Date date, Iteration iteration,
113: String aspect, double value) {
114: DataSample sample;
115: List samples = getHibernateOperations().findByNamedQuery(
116: GET_DATASAMPLE_QUERY,
117: new Object[] { date, new Integer(iteration.getId()),
118: aspect },
119: new Type[] { Hibernate.DATE, Hibernate.INTEGER,
120: Hibernate.STRING });
121: if (!samples.isEmpty()) {
122: sample = (DataSample) samples.get(0);
123: sample.setValue(value);
124: getHibernateOperations().update(sample);
125: LOG.debug("update existing datasample");
126: } else {
127: sample = new DataSample(date, iteration.getId(), aspect,
128: value);
129: getHibernateOperations().save(sample);
130: LOG.debug("Generated a new sample:" + sample);
131: }
132: }
133:
134: }
|