]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
83ccf3d50539f8c7e0a2395813c92d803c9635d7
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.id.part;
2
3 import java.util.Date;
4 import java.util.Locale;
5 import java.text.DateFormat;
6 import java.text.SimpleDateFormat;
7
8 import java.util.Arrays;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 // Uses Java's interpreter for printf-style format strings.
13 // http://java.sun.com/javase/1.6/docs/api/java/util/Formatter.html
14
15 public class GregorianDateIDPartOutputFormatter implements IDPartOutputFormatter {
16
17     final Logger logger =
18         LoggerFactory.getLogger(GregorianDateIDPartOutputFormatter.class);
19
20     private int maxOutputLength = DEFAULT_MAX_OUTPUT_LENGTH;
21     private Locale locale = null;
22     private String language;
23     private String formatPattern;
24
25     public GregorianDateIDPartOutputFormatter(String formatPattern) {
26         setFormatPattern(formatPattern);
27         setLocale(Locale.getDefault());
28     }
29
30     public GregorianDateIDPartOutputFormatter(String formatPattern,
31             String languageCode) {
32         setFormatPattern(formatPattern);
33         setLocale(languageCode);
34     }
35
36     @Override
37     public int getMaxOutputLength () {
38         return this.maxOutputLength;
39     }
40
41     public void setMaxOutputLength (int length) {
42         this.maxOutputLength = length;
43     }
44
45     @Override
46     public String getFormatPattern () {
47         return this.formatPattern;
48     }
49
50     public void setFormatPattern(String pattern) {
51         if (pattern == null || pattern.trim().isEmpty()) {
52             logger.error("Format pattern cannot be null or empty.");
53         } else {
54             this.formatPattern = pattern;
55         }
56     }
57
58     @Override
59     public String format(String id) {
60
61         Long millisecondsInEpoch = 0L;
62         try {
63             millisecondsInEpoch = (Long.parseLong(id));
64         } catch (NumberFormatException e) {
65             logger.error("Could not parse date milliseconds as a number.", e);
66             return "";
67         }
68         
69         String formattedID = "";
70         if (millisecondsInEpoch > 0) {
71           Date d = new Date(millisecondsInEpoch);
72           formattedID = formatDate(d);
73
74           // @TODO Check for exceeding maximum length before
75           // returning formatted date value.
76         }
77
78         return formattedID;
79     }
80
81     public String formatDate(Date date) {
82         SimpleDateFormat dateformatter;
83         if (getLocale() != null) {
84             dateformatter = new SimpleDateFormat(getFormatPattern(), getLocale());
85         } else {
86             dateformatter = new SimpleDateFormat(getFormatPattern());
87         }
88         return dateformatter.format(date);
89     }
90
91     // @TODO Consider generalizing locale-specific operations
92     // in a utility class outside of ID package.
93
94     public Locale getLocale() {
95         return this.locale;
96     }
97
98     public void setLocale(Locale locale) {
99         this.locale = locale;
100     }
101
102     public void setLocale(String languageCode) {
103
104         if (languageCode == null || languageCode.trim().isEmpty()) {
105             logger.error("Locale language code cannot be null or empty.");
106             return;
107         }
108
109         if (languageCode.length() != 2) {
110             logger.error(
111                 "Locale language code '" + languageCode +
112                 "' must be a two-letter ISO-639 language code.");
113             return;
114         }
115
116         // Although language codes are documented as required to be
117         // in lowercase, and they are output in that way in
118         // DateFormat.getAvailableLocales(), they appear to be
119         // matched - within Locales - in a case-insensitive manner.
120         /*
121         if (! languageCode.equals(languageCode.toLowerCase())) {
122             logger.error("Locale language code must be in lower case.");
123             return;
124         }
125         */
126
127         Locale l = new Locale(languageCode, "");
128         if (isValidLocaleForDateFormatter(l)) {
129             setLocale(l);
130         } else {
131             logger.error("Locale language code '" + languageCode +
132                 "' cannot be used for formatting dates.");
133             return;
134         }
135     }
136     
137     private boolean isValidLocaleForDateFormatter(Locale l) {
138         Locale[] locales = DateFormat.getAvailableLocales();
139         return (Arrays.asList(locales).contains(l)) ? true : false;
140     }
141
142     private boolean isValidLength(String id) {
143         if (id.length() > getMaxOutputLength()) {
144             return false;
145         } else {
146             return true;
147         }
148     }
149
150 }
151