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 public CollectingCommand(Map bucket, TimesliceValue max, TimesliceFactory timesliceFactory, AffiliateTotals totals) { 16 _bucket = bucket; 17 _timesliceFactory = timesliceFactory; 18 _maxValue = max; 19 this.totals = totals; 20 } 21 22 public Object visit(Object visited) { 23 Graphable item = (Graphable) visited; 24 EquatableComparable thisPeriod = _timesliceFactory.create(item.getDate()); 25 TimesliceValue amount = (TimesliceValue) _bucket.get(thisPeriod); 26 if (amount == null) { 27 amount = new TimesliceValue(thisPeriod, 0); 28 } 29 amount.setValue(new Double(amount.doubleValue() + item.getAmountForGraph())); 30 item.total(totals); 31 _bucket.put(thisPeriod, amount); 32 if (amount.doubleValue() > _maxValue.doubleValue()) { 33 amount.copyInto(_maxValue); 34 } 35 return item; 36 } 37 38 public void lastInteraction() { 39 EquatableComparable maxDate = _timesliceFactory.create(new Timestamp(_timesliceFactory.getEndDate().getTime())); 40 EquatableComparable earliest = maxDate.earliest(); 41 EquatableComparable previous = maxDate.previous(); 42 while (previous.compareTo(earliest) > -1) { 43 TimesliceValue previousTimeslice = new TimesliceValue(previous, 0); 44 if (!_bucket.containsKey(previous)) { 45 _bucket.put(previous,previousTimeslice); 46 } 47 previous = previousTimeslice._date.previous(); 48 } 49 } 50 51 }