View Javadoc

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  public class JammyJoesUtil {
19  
20    public static Locale makeLocale(String localeString) {
21      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      String formatted = NumberFormat.getCurrencyInstance(locale).format(value);
50      StringBuffer out = new StringBuffer();
51      for (int i = 0; i < formatted.length(); i++) {
52        char c = formatted.charAt(i);
53        if (c == '\u20AC') {
54          out.append("&euro;");
55        } else {
56          out.append(c);
57        }
58      }
59      return out.toString();
60    }
61  
62    public static String getCookieValue(Melati melati, String key) {
63      // try and get from cookie
64      Cookie[] cookies = melati.getRequest().getCookies();
65      if (cookies == null) {
66        return null;
67      }
68      for (int i = 0; i < cookies.length; i++) {
69        Cookie c = cookies[i];
70        if (c.getName().equals(key))
71          return c.getValue();
72      }
73      return null;
74    }
75  
76    public static Cookie makeCookie(String key, String value, int days) {
77      Cookie c = new Cookie(key, value);
78      c.setPath("/");
79      c.setMaxAge(days * 24 * 60 * 60);
80      c.setComment("This cookie is used to track the JammyJoes affiliate programme");
81      return c;
82    }
83  
84    public static String formatDateForSQL(Database db, Date date) {
85      SimpleDateFormat formatter;
86      if (db.getDbms().getClass().equals(Hsqldb.class)) {
87        formatter = new SimpleDateFormat("yyyy-MM-dd");
88      } else {
89        formatter = new SimpleDateFormat("MM/dd/yyyy");
90      }
91      return "'" + formatter.format(date) + "'";
92    }
93  
94    public static String formatDateForPostgresSQL(Date date) {
95      SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
96      return "'" + formatter.format(date) + "'";
97    }
98  
99    public static double roundTo2dp(double num) {
100     int a = Math.round(new Float(num * 100).floatValue());
101     double b = new Double(a).doubleValue();
102     return (b / 100);
103   }
104 
105   public static boolean telesales(Melati melati) {
106     String requestURI = melati.getRequest().getRequestURI();
107     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     return JammyJoesUtil.telesales(melati) ? "telesales/" + name : "shopping/" + name;
120   }
121 
122 }