| 1 | |
package org.paneris.jammyjoes.mvp; |
| 2 | |
|
| 3 | |
import java.sql.Timestamp; |
| 4 | |
import java.util.Map; |
| 5 | |
|
| 6 | |
import org.paneris.jammyjoes.model.Graphable; |
| 7 | |
|
| 8 | |
public class CollectingCommand implements Command { |
| 9 | |
|
| 10 | |
private Map _bucket; |
| 11 | |
private TimesliceValue _maxValue; |
| 12 | |
private AffiliateTotals totals; |
| 13 | |
private TimesliceFactory _timesliceFactory; |
| 14 | |
|
| 15 | 0 | public CollectingCommand(Map bucket, TimesliceValue max, TimesliceFactory timesliceFactory, AffiliateTotals totals) { |
| 16 | 0 | _bucket = bucket; |
| 17 | 0 | _timesliceFactory = timesliceFactory; |
| 18 | 0 | _maxValue = max; |
| 19 | 0 | this.totals = totals; |
| 20 | 0 | } |
| 21 | |
|
| 22 | |
public Object visit(Object visited) { |
| 23 | 0 | Graphable item = (Graphable) visited; |
| 24 | 0 | EquatableComparable thisPeriod = _timesliceFactory.create(item.getDate()); |
| 25 | 0 | TimesliceValue amount = (TimesliceValue) _bucket.get(thisPeriod); |
| 26 | 0 | if (amount == null) { |
| 27 | 0 | amount = new TimesliceValue(thisPeriod, 0); |
| 28 | |
} |
| 29 | 0 | amount.setValue(new Double(amount.doubleValue() + item.getAmountForGraph())); |
| 30 | 0 | item.total(totals); |
| 31 | 0 | _bucket.put(thisPeriod, amount); |
| 32 | 0 | if (amount.doubleValue() > _maxValue.doubleValue()) { |
| 33 | 0 | amount.copyInto(_maxValue); |
| 34 | |
} |
| 35 | 0 | return item; |
| 36 | |
} |
| 37 | |
|
| 38 | |
public void lastInteraction() { |
| 39 | 0 | EquatableComparable maxDate = _timesliceFactory.create(new Timestamp(_timesliceFactory.getEndDate().getTime())); |
| 40 | 0 | EquatableComparable earliest = maxDate.earliest(); |
| 41 | 0 | EquatableComparable previous = maxDate.previous(); |
| 42 | 0 | while (previous.compareTo(earliest) > -1) { |
| 43 | 0 | TimesliceValue previousTimeslice = new TimesliceValue(previous, 0); |
| 44 | 0 | if (!_bucket.containsKey(previous)) { |
| 45 | 0 | _bucket.put(previous,previousTimeslice); |
| 46 | |
} |
| 47 | 0 | previous = previousTimeslice._date.previous(); |
| 48 | 0 | } |
| 49 | 0 | } |
| 50 | |
|
| 51 | |
} |