| 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 | |
|
| 16 | |
|
| 17 | |
|
| 18 | 0 | public class JammyJoesUtil { |
| 19 | |
|
| 20 | |
public static Locale makeLocale(String localeString) { |
| 21 | 0 | 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 | 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("€"); |
| 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 | |
|
| 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 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
public static String shoppingTemplate(Melati melati, String name) { |
| 119 | 0 | return JammyJoesUtil.telesales(melati) ? "telesales/" + name : "shopping/" + name; |
| 120 | |
} |
| 121 | |
|
| 122 | |
} |