Coverage Report - org.homeunix.thecave.buddi.plugin.BuddiPluginHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
BuddiPluginHelper
48%
25/52
37%
3/8
1.467
BuddiPluginHelper$1
0%
0/11
N/A
1.467
BuddiPluginHelper$DateChoice
78%
11/14
N/A
1.467
 
 1  
 /*
 2  
  * Created on Oct 7, 2006 by wyatt
 3  
  */
 4  
 package org.homeunix.thecave.buddi.plugin;
 5  
 
 6  
 import java.io.File;
 7  
 import java.util.Date;
 8  
 import java.util.Vector;
 9  
 import java.util.logging.Level;
 10  
 import java.util.logging.Logger;
 11  
 
 12  
 import net.java.dev.SwingWorker;
 13  
 
 14  
 import org.homeunix.thecave.buddi.i18n.BuddiKeys;
 15  
 import org.homeunix.thecave.buddi.i18n.keys.PluginRangeFilters;
 16  
 import org.homeunix.thecave.buddi.model.Document;
 17  
 import org.homeunix.thecave.buddi.model.impl.BudgetCategoryTypeSemiMonthly;
 18  
 import org.homeunix.thecave.buddi.plugin.api.BuddiReportPlugin;
 19  
 import org.homeunix.thecave.buddi.plugin.api.model.ImmutableDocument;
 20  
 import org.homeunix.thecave.buddi.plugin.api.model.impl.ImmutableDocumentImpl;
 21  
 import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter;
 22  
 import org.homeunix.thecave.buddi.view.MainFrame;
 23  
 
 24  
 import ca.digitalcave.moss.common.DateUtil;
 25  
 import ca.digitalcave.moss.swing.MossStatusDialog;
 26  
 import ca.digitalcave.moss.swing.exception.WindowOpenException;
 27  
 import edu.stanford.ejalbert.BrowserLauncher;
 28  
 
 29  0
 public class BuddiPluginHelper {
 30  
         public static void openReport(final MainFrame frame, final BuddiReportPlugin report, final Date startDate, final Date endDate){
 31  
                 
 32  0
                 final ImmutableDocument model = new ImmutableDocumentImpl((Document) frame.getDocument());
 33  
                 
 34  
                 //We want to run the GUI in the EventDispatch thread.  If the user has not
 35  
                 // cancelled, then we will proceed.
 36  0
                 if (!report.getReportGUI(model,
 37  
                                 frame,
 38  
                                 startDate, 
 39  
                                 endDate))
 40  0
                         return;
 41  
                 
 42  0
                 final MossStatusDialog status = new MossStatusDialog(
 43  
                                 frame,
 44  
                                 TextFormatter.getTranslation(BuddiKeys.MESSAGE_GENERATING_REPORT));
 45  
 
 46  
                 try {
 47  0
                         status.openWindow();
 48  
                 }
 49  0
                 catch (WindowOpenException woe){}
 50  
 
 51  0
                 SwingWorker worker = new SwingWorker(){
 52  
                         @Override
 53  
                         public Object construct() {
 54  
                                 try {
 55  0
                                         File index = report.getReport(
 56  
                                                                         model,
 57  
                                                                         frame,
 58  
                                                                         startDate, 
 59  
                                                                         endDate).createHTML("report");
 60  0
                                         BrowserLauncher bl = new BrowserLauncher(null);
 61  0
                                         bl.openURLinBrowser(index.toURI().toURL().toString());
 62  
                                 }
 63  0
                                 catch (Exception e){
 64  0
                                         Logger.getLogger(BuddiPluginHelper.class.getName()).log(Level.WARNING, "Error making HTML", e);
 65  0
                                 }
 66  
                                 
 67  0
                                 return null;
 68  
                         }
 69  
 
 70  
                         @Override
 71  
                         public void finished() {
 72  0
                                 status.closeWindow();
 73  0
                                 super.finished();
 74  0
                         }
 75  
                 };
 76  
 
 77  0
                 worker.start();
 78  0
         }
 79  
 
 80  
         public static Vector<DateChoice> getInterval() {
 81  0
                 return getInterval(null);
 82  
         }
 83  
         
 84  
         public static Vector<DateChoice> getInterval(Document model) {
 85  12100
                 Vector<DateChoice> intervals = new Vector<DateChoice>();
 86  
 
 87  12100
                 intervals.add(null);
 88  12100
                 intervals.add(new DateChoice(
 89  
                                 DateUtil.getStartOfWeek(new Date()),
 90  
                                 DateUtil.getEndOfWeek(new Date()),
 91  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_THIS_WEEK)
 92  
                 ));
 93  12100
                 intervals.add(new DateChoice(
 94  
                                 DateUtil.getStartOfWeek(DateUtil.addDays(new Date(), -7)),
 95  
                                 DateUtil.getEndOfWeek(DateUtil.addDays(new Date(), -7)),
 96  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_LAST_WEEK)
 97  
                 ));
 98  12100
                 intervals.add(new DateChoice(
 99  
                                 DateUtil.getStartOfDay(DateUtil.getStartOfMonth(new Date())),
 100  
                                 DateUtil.getEndOfDay(DateUtil.getEndOfMonth(new Date())),
 101  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_THIS_MONTH)
 102  
                 ));
 103  12100
                 intervals.add(new DateChoice(
 104  
                                 DateUtil.getStartOfMonth(DateUtil.addMonths(new Date(), -1)),
 105  
                                 DateUtil.getEndOfMonth(DateUtil.addMonths(new Date(), -1)),
 106  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_LAST_MONTH)
 107  
                 ));
 108  
                 //It's easier to just make the category type and use it, rather than duplicate all the calculations. 
 109  12100
                 BudgetCategoryTypeSemiMonthly semiMonth = new BudgetCategoryTypeSemiMonthly();
 110  12100
                 intervals.add(new DateChoice(
 111  
                                 semiMonth.getStartOfBudgetPeriod(new Date()),
 112  
                                 semiMonth.getEndOfBudgetPeriod(new Date()),
 113  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_THIS_SEMI_MONTH)
 114  
                 ));
 115  12100
                 intervals.add(new DateChoice(
 116  
                                 semiMonth.getStartOfBudgetPeriod(semiMonth.getBudgetPeriodOffset(new Date(), -1)),
 117  
                                 semiMonth.getEndOfBudgetPeriod(semiMonth.getBudgetPeriodOffset(new Date(), -1)),
 118  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_LAST_SEMI_MONTH)
 119  
                 ));
 120  12100
                 intervals.add(new DateChoice(
 121  
                                 DateUtil.getStartOfQuarter(new Date()),
 122  
                                 DateUtil.getEndOfQuarter(new Date()),
 123  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_THIS_QUARTER)
 124  
                 ));
 125  12100
                 intervals.add(new DateChoice(
 126  
                                 DateUtil.addQuarters(DateUtil.getStartOfQuarter(new Date()), -1),
 127  
                                 DateUtil.addQuarters(DateUtil.getEndOfQuarter(new Date()), -1),
 128  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_LAST_QUARTER)
 129  
                 ));
 130  12100
                 intervals.add(new DateChoice(
 131  
                                 DateUtil.getStartOfDay(DateUtil.getStartOfYear(new Date())),
 132  
                                 DateUtil.getEndOfDay(DateUtil.getEndOfYear(new Date())),
 133  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_THIS_YEAR)
 134  
                 ));
 135  12100
                 intervals.add(new DateChoice(
 136  
                                 DateUtil.getStartOfDay(DateUtil.getStartOfYear(new Date())),
 137  
                                 DateUtil.getEndOfDay(new Date()),
 138  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_THIS_YEAR_TO_DATE)
 139  
                 ));
 140  12100
                 intervals.add(new DateChoice(
 141  
                                 DateUtil.addYears(DateUtil.getStartOfYear(new Date()), -1),
 142  
                                 DateUtil.addYears(DateUtil.getEndOfYear(new Date()), -1),
 143  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_LAST_YEAR)
 144  
                 ));
 145  12100
                 intervals.add(new DateChoice(
 146  
                                 //Note that this is called when the data file is first opened, and it will not change until the main window is re-opened (i.e., 
 147  
                                 // we start Buddi again).  To try to ensure that we don't miss anything, including new entries, we go to the beginning of the 
 148  
                                 // year of the first transaction, and go to one week after today.  If people comment on this, we may expand the range,
 149  
                                 // or possibly update the ranges at plugin runtime.
 150  
                                 DateUtil.getStartOfYear((model != null && model.getTransactions() != null && model.getTransactions().size() > 0 ? model.getTransactions().get(0).getDate() : new Date())),
 151  
                                 DateUtil.getEndOfDay(DateUtil.addDays(new Date(), 7)),
 152  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_ALL_TIME)
 153  
                 ));                
 154  12100
                 intervals.add(new DateChoice(
 155  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_OTHER)
 156  
                 ));
 157  
 
 158  12100
                 return intervals;
 159  
         }
 160  
 
 161  
         public static Vector<DateChoice> getEndOnly() {
 162  0
                 return getEndOnly(null);
 163  
         }
 164  
         
 165  
         public static Vector<DateChoice> getEndOnly(Document model) {
 166  0
                 Vector<DateChoice> endDates = new Vector<DateChoice>();
 167  
 
 168  0
                 endDates.add(null);
 169  0
                 endDates.add(new DateChoice(
 170  
                                 null,
 171  
                                 DateUtil.addYears(new Date(), -1),
 172  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_ONE_YEAR_AGO)
 173  
                 ));
 174  0
                 endDates.add(new DateChoice(
 175  
                                 null,
 176  
                                 DateUtil.addMonths(new Date(), -1),
 177  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_ONE_MONTH_AGO)
 178  
                 ));
 179  0
                 endDates.add(new DateChoice(
 180  
                                 null,
 181  
                                 DateUtil.getEndOfDay(DateUtil.addDays(new Date(), -7)),
 182  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_ONE_WEEK_AGO)
 183  
                 ));
 184  0
                 endDates.add(new DateChoice(
 185  
                                 null,
 186  
                                 DateUtil.getEndOfDay(DateUtil.addDays(new Date(), -1)),
 187  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_YESTERDAY)
 188  
                 ));
 189  0
                 endDates.add(new DateChoice(
 190  
                                 null,
 191  
                                 DateUtil.getEndOfDay(new Date()),
 192  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_TODAY)
 193  
                 ));
 194  0
                 endDates.add(new DateChoice(
 195  
                                 null,
 196  
                                 DateUtil.getEndOfDay(DateUtil.addDays(new Date(), 7)),
 197  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_ONE_WEEK_FROM_NOW)
 198  
                 ));
 199  0
                 endDates.add(new DateChoice(
 200  
                                 null,
 201  
                                 DateUtil.getEndOfMonth(DateUtil.addMonths(new Date(), 1)),
 202  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_END_OF_NEXT_MONTH)
 203  
                 ));
 204  0
                 endDates.add(new DateChoice(
 205  
                                 null,
 206  
                                 DateUtil.getEndOfYear(new Date()),
 207  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_END_OF_THIS_YEAR)
 208  
                 ));
 209  0
                 endDates.add(new DateChoice(
 210  
                                 null,
 211  
                                 DateUtil.getEndOfYear(DateUtil.addYears(new Date(), 1)),
 212  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_END_OF_NEXT_YEAR)
 213  
                 ));
 214  0
                 endDates.add(new DateChoice(
 215  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_OTHER)
 216  
                 ));
 217  
 
 218  
 
 219  0
                 return endDates;
 220  
         }
 221  
 
 222  
         public static Vector<DateChoice> getStartOnly() {
 223  0
                 return getStartOnly(null);
 224  
         }
 225  
         
 226  
         public static Vector<DateChoice> getStartOnly(Document model) {
 227  3025
                 Vector<DateChoice> startDates = new Vector<DateChoice>();
 228  
 
 229  3025
                 startDates.add(null);
 230  3025
                 startDates.add(new DateChoice(
 231  
                                 DateUtil.addMonths(DateUtil.getStartOfMonth(new Date()), -1),
 232  
                                 null,
 233  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_ONE_MONTH)
 234  
                 ));
 235  3025
                 startDates.add(new DateChoice(
 236  
                                 DateUtil.addMonths(DateUtil.getStartOfMonth(new Date()), -2),
 237  
                                 null,
 238  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_TWO_MONTHS)
 239  
                 ));
 240  3025
                 startDates.add(new DateChoice(
 241  
                                 DateUtil.addMonths(DateUtil.getStartOfMonth(new Date()), -6),
 242  
                                 null,
 243  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_SIX_MONTHS)
 244  
                 ));
 245  
 
 246  3025
                 startDates.add(new DateChoice(
 247  
                                 DateUtil.addMonths(DateUtil.getStartOfMonth(new Date()), -12),
 248  
                                 null,
 249  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_YEAR)
 250  
                 ));
 251  
 
 252  3025
                 startDates.add(new DateChoice(
 253  
                                 TextFormatter.getTranslation(PluginRangeFilters.PLUGIN_FILTER_OTHER)
 254  
                 ));
 255  
 
 256  3025
                 return startDates;
 257  
         }
 258  
 
 259  0
         public static class DateChoice {
 260  
                 private Date startDate;
 261  
                 private Date endDate;
 262  
                 private String name;
 263  172425
                 private boolean custom = false;
 264  
 
 265  15125
                 public DateChoice(String name){
 266  15125
                         this.name = name;
 267  15125
                         custom = true;
 268  15125
                 }
 269  
 
 270  157300
                 public DateChoice(Date startDate, Date endDate, String name){
 271  157300
                         this.startDate = startDate;
 272  157300
                         this.endDate = endDate;
 273  157300
                         this.name = name;
 274  157300
                 }
 275  
 
 276  
                 public String toString(){
 277  345465
                         return name;
 278  
                 }
 279  
 
 280  
                 public Date getStartDate(){
 281  0
                         return startDate;
 282  
                 }
 283  
 
 284  
                 public Date getEndDate(){
 285  0
                         return endDate;
 286  
                 }
 287  
 
 288  
                 public boolean isCustom(){
 289  0
                         return custom;
 290  
                 }
 291  
         }        
 292  
 }