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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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("€");
55 } else {
56 out.append(c);
57 }
58 }
59 return out.toString();
60 }
61
62 public static String getCookieValue(Melati melati, String key) {
63
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 }