Coverage Report - org.paneris.jammyjoes.util.JammyJoesUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
JammyJoesUtil
0%
0/35
0%
0/16
1.889
 
 1  
 package org.paneris.jammyjoes.util;
 2  
 
 3  
 import java.text.NumberFormat;
 4  
 import java.text.SimpleDateFormat;
 5  
 import java.util.Date;
 6  
 import java.util.Locale;
 7  
 
 8  
 import javax.servlet.http.Cookie;
 9  
 
 10  
 import org.melati.Melati;
 11  
 import org.melati.poem.Database;
 12  
 import org.melati.poem.dbms.Hsqldb;
 13  
 
 14  
 /**
 15  
  * BibliomaniaUtil is a place where useful Static methods can be put.
 16  
  */
 17  
 
 18  0
 public class JammyJoesUtil {
 19  
 
 20  
   public static Locale makeLocale(String localeString) {
 21  0
     return Locale.UK;
 22  
     /*    Locale locale = null;
 23  
         if (localeString != null) {
 24  
           if (localeString.equals("en-gb")) return locale = Locale.UK;
 25  
           if (localeString.equals("en-us")) return locale = Locale.US;
 26  
           String language = "";
 27  
           String country = "";
 28  
           int i = localeString.indexOf("-");
 29  
           if (i == -1) {
 30  
             language = localeString;
 31  
           } else {
 32  
             language = localeString.substring(0,i);
 33  
           }
 34  
           if (localeString.length() > i) country = localeString.substring(i+1, localeString.length());
 35  
           if (country.equals("at") || country.equals("de") || country.equals("lu") ||
 36  
               country.equals("ie") || country.equals("es") || country.equals("fl") ||
 37  
               country.equals("be") || country.equals("fr") || country.equals("it") ||
 38  
               country.equals("nl") || country.equals("pt")
 39  
               ) {
 40  
                 locale = new Locale("en", "IE", "EURO");
 41  
           }
 42  
         }
 43  
         if (locale == null) locale = Locale.UK;
 44  
         return locale;
 45  
      */
 46  
   }
 47  
 
 48  
   public static String euroFormat(Locale locale, double value) {
 49  0
     String formatted = NumberFormat.getCurrencyInstance(locale).format(value);
 50  0
     StringBuffer out = new StringBuffer();
 51  0
     for (int i = 0; i < formatted.length(); i++) {
 52  0
       char c = formatted.charAt(i);
 53  0
       if (c == '\u20AC') {
 54  0
         out.append("&euro;");
 55  
       } else {
 56  0
         out.append(c);
 57  
       }
 58  
     }
 59  0
     return out.toString();
 60  
   }
 61  
 
 62  
   public static String getCookieValue(Melati melati, String key) {
 63  
     // try and get from cookie
 64  0
     Cookie[] cookies = melati.getRequest().getCookies();
 65  0
     if (cookies == null) {
 66  0
       return null;
 67  
     }
 68  0
     for (int i = 0; i < cookies.length; i++) {
 69  0
       Cookie c = cookies[i];
 70  0
       if (c.getName().equals(key))
 71  0
         return c.getValue();
 72  
     }
 73  0
     return null;
 74  
   }
 75  
 
 76  
   public static Cookie makeCookie(String key, String value, int days) {
 77  0
     Cookie c = new Cookie(key, value);
 78  0
     c.setPath("/");
 79  0
     c.setMaxAge(days * 24 * 60 * 60);
 80  0
     c.setComment("This cookie is used to track the JammyJoes affiliate programme");
 81  0
     return c;
 82  
   }
 83  
 
 84  
   public static String formatDateForSQL(Database db, Date date) {
 85  
     SimpleDateFormat formatter;
 86  0
     if (db.getDbms().getClass().equals(Hsqldb.class)) {
 87  0
       formatter = new SimpleDateFormat("yyyy-MM-dd");
 88  
     } else {
 89  0
       formatter = new SimpleDateFormat("MM/dd/yyyy");
 90  
     }
 91  0
     return "'" + formatter.format(date) + "'";
 92  
   }
 93  
 
 94  
   public static String formatDateForPostgresSQL(Date date) {
 95  0
     SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
 96  0
     return "'" + formatter.format(date) + "'";
 97  
   }
 98  
 
 99  
   public static double roundTo2dp(double num) {
 100  0
     int a = Math.round(new Float(num * 100).floatValue());
 101  0
     double b = new Double(a).doubleValue();
 102  0
     return (b / 100);
 103  
   }
 104  
 
 105  
   public static boolean telesales(Melati melati) {
 106  0
     String requestURI = melati.getRequest().getRequestURI();
 107  0
     return requestURI.startsWith("/telesales/");
 108  
   }
 109  
 
 110  
   /** 
 111  
    * create the full name of the template to be returned
 112  
    *
 113  
    * @param melati - the melati for this request (not used)
 114  
    * @param name - the name of the template
 115  
    *
 116  
    * @return - the full path to the template
 117  
    */
 118  
   public static String shoppingTemplate(Melati melati, String name) {
 119  0
     return JammyJoesUtil.telesales(melati) ? "telesales/" + name : "shopping/" + name;
 120  
   }
 121  
 
 122  
 }