Coverage Report - org.homeunix.thecave.buddi.plugin.api.util.HtmlHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
HtmlHelper
0%
0/106
0%
0/34
4.167
 
 1  
 /*
 2  
  * Created on Oct 7, 2006 by wyatt
 3  
  */
 4  
 package org.homeunix.thecave.buddi.plugin.api.util;
 5  
 
 6  
 import java.io.ByteArrayOutputStream;
 7  
 import java.io.File;
 8  
 import java.io.FileInputStream;
 9  
 import java.io.FileNotFoundException;
 10  
 import java.io.IOException;
 11  
 import java.io.InputStream;
 12  
 import java.util.Date;
 13  
 import java.util.logging.Logger;
 14  
 
 15  
 import org.homeunix.thecave.buddi.i18n.BuddiKeys;
 16  
 import org.homeunix.thecave.buddi.model.prefs.PrefsModel;
 17  
 import org.homeunix.thecave.buddi.plugin.api.model.ImmutableSource;
 18  
 import org.homeunix.thecave.buddi.plugin.api.model.ImmutableSplit;
 19  
 import org.homeunix.thecave.buddi.plugin.api.model.ImmutableTransaction;
 20  
 import org.homeunix.thecave.buddi.plugin.api.model.ImmutableTransactionSplit;
 21  
 
 22  
 import ca.digitalcave.moss.common.OperatingSystemUtil;
 23  
 import ca.digitalcave.moss.common.StreamUtil;
 24  
 
 25  0
 public class HtmlHelper {
 26  
 
 27  
 
 28  
         /**
 29  
          * Get a StringBuilder with an HTML header, including some CSS for a 
 30  
          * basic printable page. 
 31  
          * @param title The main title - appears on the browser title, as well as on the top of the page
 32  
          * @param subtitle The subtitle - optional - appears right below main title.  Set to null to not use.
 33  
          * @param startDate Start date.  Set to null if this is not a report with date range.
 34  
          * @param endDate End date.  Set to null if this is not a report with date range. 
 35  
          * @return
 36  
          */
 37  
         public static StringBuilder getHtmlHeader(String title, String subtitle, Date startDate, Date endDate){
 38  0
                 StringBuilder sb = new StringBuilder();
 39  0
                 sb.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
 40  0
                 sb.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n"); 
 41  0
                 sb.append("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
 42  0
                 sb.append("<html>\n");
 43  0
                 sb.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n");
 44  0
                 sb.append("<head>\n");
 45  0
                 sb.append("<title>");
 46  0
                 sb.append(TextFormatter.getTranslation(title));
 47  0
                 sb.append("</title>\n");
 48  
 
 49  
                 //Screen CSS
 50  0
                 sb.append("<style type='text/css' media='screen'>\n");
 51  0
                 includeCss("screen.css", sb);
 52  0
                 sb.append("</style>\n");
 53  
 
 54  
                 //Printing CSS
 55  0
                 sb.append("<style type='text/css' media='print'>\n");
 56  0
                 includeCss("print.css", sb);
 57  0
                 sb.append("</style>\n");
 58  
 
 59  
 //                sb.append("<SCRIPT LANGUAGE=\"JavaScript\">\n");
 60  
 //                sb.append("<!--\n");
 61  
 //                sb.append("function launchTransactions(link) {\n");
 62  
 //                sb.append("window.open(link, \"\", \"height=1,width=1\");\n");
 63  
 //                sb.append("return false;\n");
 64  
 //                sb.append("}\n");
 65  
 //                sb.append("//-->\n");
 66  
 //                sb.append("</SCRIPT>\n");
 67  
 
 68  0
                 sb.append("</head>\n");
 69  0
                 if ("Hebrew".equals(PrefsModel.getInstance().getLanguage()))
 70  0
                         sb.append("<body dir='rtl'>\n");
 71  
                 else
 72  0
                         sb.append("<body>\n");
 73  0
                 sb.append("<div class='separator'></div>\n");
 74  
 
 75  0
                 sb.append("<div class='header'>\n<div class='empty'>&nbsp;</div>");
 76  0
                 sb.append("<h1>").append(TextFormatter.getTranslation(title)).append("</h1>\n");
 77  
 
 78  0
                 if (subtitle != null){
 79  0
                         sb.append("<h2>").append(TextFormatter.getTranslation(subtitle)).append("</h2>\n");
 80  
                 }
 81  
 
 82  0
                 if (startDate != null && endDate != null){
 83  0
                         sb.append("<h2>"); 
 84  0
                         sb.append(TextFormatter.getDateFormat().format(startDate));
 85  0
                         sb.append(" - ");
 86  0
                         sb.append(TextFormatter.getDateFormat().format(endDate));
 87  0
                         sb.append("</h2>\n");
 88  
                 }
 89  0
                 else if (startDate != null){
 90  0
                         sb.append("<h2>"); 
 91  0
                         sb.append(TextFormatter.getDateFormat().format(startDate));
 92  0
                         sb.append("</h2>\n");                        
 93  
                 }
 94  0
                 else if (endDate != null){
 95  0
                         sb.append("<h2>"); 
 96  0
                         sb.append(TextFormatter.getDateFormat().format(endDate));
 97  0
                         sb.append("</h2>\n");        
 98  
                 }
 99  
 
 100  0
                 sb.append("</div>\n<div class='separator'></div>\n<div class='content'>\n\n");
 101  
 
 102  0
                 return sb;
 103  
         }
 104  
 
 105  
         /**
 106  
          * Get the HTML footer, matched to the header supplied from getHtmlHeader()
 107  
          * @return
 108  
          */
 109  
         public static StringBuilder getHtmlFooter(){
 110  0
                 StringBuilder sb = new StringBuilder();
 111  
 
 112  0
                 sb.append("</div>\n</body>\n</html>");
 113  
 
 114  0
                 return sb;
 115  
         }
 116  
 
 117  
         /**
 118  
          * Returns an HTML table row consisting of information from the given transaction. 
 119  
          * @param t Transaction to display.
 120  
          * @param source Associated source.  This would be the account which 
 121  
          * the transaction frame is associated with, for instance.  This can be null
 122  
          * if there is none.
 123  
          * @return
 124  
          */
 125  
         public static StringBuilder getHtmlTransactionRow(ImmutableTransaction t, ImmutableSource source){
 126  0
                 StringBuilder sb = new StringBuilder();
 127  
 
 128  0
                 sb.append("<tr><td width='15%'>");
 129  0
                 sb.append(TextFormatter.getDateFormat().format(t.getDate()));
 130  
 
 131  0
                 sb.append("</td><td width='20%'>");
 132  0
                 sb.append(TextFormatter.getTranslation(t.getDescription()));
 133  
 
 134  0
                 sb.append("</td><td width='30%'>");
 135  0
                 if (t.getFrom() instanceof ImmutableSplit)
 136  
                         //We assume that if there is an immutable split, then the given source is the from / to source.  Is this right?
 137  0
                         sb.append(TextFormatter.getTranslation(source + ""));
 138  
                 else 
 139  0
                         sb.append(TextFormatter.getTranslation(t.getFrom().toString()));
 140  
                 
 141  0
                 sb.append(TextFormatter.getTranslation(BuddiKeys.HTML_TO));
 142  
                 
 143  0
                 if (t.getTo() instanceof ImmutableSplit)
 144  
                         //We assume that if there is an immutable split, then the given source is the from / to source.  Is this right?
 145  0
                         sb.append(TextFormatter.getTranslation(source + ""));
 146  
                 else 
 147  0
                         sb.append(TextFormatter.getTranslation(t.getTo().toString()));
 148  
                         
 149  
 
 150  
                 boolean red;
 151  0
                 if (source != null)
 152  0
                         red = TextFormatter.isRed(t, t.getFrom().equals(source));
 153  
                 else 
 154  0
                         red = TextFormatter.isRed(t);
 155  
 
 156  0
                 sb.append("</td><td width='15%' class='right" + (red ? " red'" : "'") + "'>");
 157  0
                 if (t.getFrom().equals(source) || t.getTo().equals(source))
 158  0
                         sb.append(TextFormatter.getFormattedCurrency(t.getAmount()));
 159  
                 else {
 160  0
                         long amount = 0;
 161  0
                         for (ImmutableTransactionSplit split : t.getImmutableFromSplits()) {
 162  0
                                 if (split.getSource().equals(source)){
 163  0
                                         amount += split.getAmount();
 164  
                                 }
 165  
                         }
 166  0
                         for (ImmutableTransactionSplit split : t.getImmutableToSplits()) {
 167  0
                                 if (split.getSource().equals(source)){
 168  0
                                         amount += split.getAmount();
 169  
                                 }
 170  
                         }
 171  0
                         sb.append(TextFormatter.getFormattedCurrency(amount));
 172  
                 }
 173  0
                 sb.append("</td><td width='20%'>");
 174  0
                 sb.append(t.getMemo());
 175  0
                 sb.append("</td></tr>\n");
 176  
 
 177  0
                 return sb;                
 178  
         }
 179  
 
 180  
         /**
 181  
          * Returns the start of a table for displaying transactions,
 182  
          * including the header row.
 183  
          * @return
 184  
          */
 185  
         public static StringBuilder getHtmlTransactionHeader(){
 186  0
                 StringBuilder sb = new StringBuilder();
 187  
 
 188  0
                 sb.append("<table class='main'>\n");
 189  0
                 sb.append("<tr><th>");
 190  0
                 sb.append(TextFormatter.getTranslation(BuddiKeys.DATE));
 191  0
                 sb.append("</th><th>");
 192  0
                 sb.append(TextFormatter.getTranslation(BuddiKeys.DESCRIPTION));
 193  0
                 sb.append("</th><th>");
 194  0
                 sb.append(TextFormatter.getTranslation(BuddiKeys.SOURCE_TO_FROM));
 195  0
                 sb.append("</th><th>");
 196  0
                 sb.append(TextFormatter.getTranslation(BuddiKeys.AMOUNT));
 197  0
                 sb.append("</th><th>");
 198  0
                 sb.append(TextFormatter.getTranslation(BuddiKeys.MEMO));
 199  0
                 sb.append("</th></tr>\n");
 200  
 
 201  0
                 return sb;
 202  
         }
 203  
 
 204  
         /**
 205  
          * Returns the end of a table for displaying transactions.
 206  
          * @return
 207  
          */
 208  
         public static StringBuilder getHtmlTransactionFooter(){
 209  0
                 return new StringBuilder("</table>\n\n");
 210  
         }
 211  
         
 212  
         private static void includeCss(String cssName, StringBuilder sb){
 213  0
                 InputStream is = HtmlHelper.class.getResourceAsStream("/css/" + cssName);
 214  0
                 File css = OperatingSystemUtil.getUserFile("Buddi", cssName);
 215  0
                 if (css.exists()){
 216  
                         try {
 217  0
                                 FileInputStream cssStream = new FileInputStream(css);
 218  0
                                 is = cssStream;
 219  
                         }
 220  0
                         catch (FileNotFoundException fnfe){
 221  0
                                 Logger.getLogger(HtmlHelper.class.getName()).warning("Error opening css file " + css.getAbsolutePath() + "; using default css for report.");
 222  0
                         }
 223  
                 }
 224  
                 
 225  0
                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 226  
                 try {
 227  0
                         StreamUtil.copyStream(is, baos);
 228  0
                         sb.append(baos.toString());
 229  
                 }
 230  0
                 catch (IOException ioe){
 231  0
                         Logger.getLogger(HtmlHelper.class.getName()).warning("Error reading CSS input stream; you are probably not running this from an official Buddi release bundle.  I am therefore not using CSS for the report.");
 232  0
                 }
 233  0
         }
 234  
 }