1 package org.collectionspace.services.id.part;
4 import java.util.Locale;
5 import java.text.DateFormat;
6 import java.text.SimpleDateFormat;
8 import java.util.Arrays;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
12 // Uses Java's interpreter for printf-style format strings.
13 // http://java.sun.com/javase/1.6/docs/api/java/util/Formatter.html
15 public class GregorianDateIDPartOutputFormatter implements IDPartOutputFormatter {
18 LoggerFactory.getLogger(GregorianDateIDPartOutputFormatter.class);
20 private int maxOutputLength = DEFAULT_MAX_OUTPUT_LENGTH;
21 private Locale locale = null;
22 private String language;
23 private String formatPattern;
25 public GregorianDateIDPartOutputFormatter(String formatPattern) {
26 setFormatPattern(formatPattern);
27 setLocale(Locale.getDefault());
30 public GregorianDateIDPartOutputFormatter(String formatPattern,
31 String languageCode) {
32 setFormatPattern(formatPattern);
33 setLocale(languageCode);
37 public int getMaxOutputLength () {
38 return this.maxOutputLength;
41 public void setMaxOutputLength (int length) {
42 this.maxOutputLength = length;
46 public String getFormatPattern () {
47 return this.formatPattern;
50 public void setFormatPattern(String pattern) {
51 if (pattern == null || pattern.trim().isEmpty()) {
52 logger.error("Format pattern cannot be null or empty.");
54 this.formatPattern = pattern;
59 public String format(String id) {
61 Long millisecondsInEpoch = 0L;
63 millisecondsInEpoch = (Long.parseLong(id));
64 } catch (NumberFormatException e) {
65 logger.error("Could not parse date milliseconds as a number.", e);
69 String formattedID = "";
70 if (millisecondsInEpoch > 0) {
71 Date d = new Date(millisecondsInEpoch);
72 formattedID = formatDate(d);
74 // @TODO Check for exceeding maximum length before
75 // returning formatted date value.
81 public String formatDate(Date date) {
82 SimpleDateFormat dateformatter;
83 if (getLocale() != null) {
84 dateformatter = new SimpleDateFormat(getFormatPattern(), getLocale());
86 dateformatter = new SimpleDateFormat(getFormatPattern());
88 return dateformatter.format(date);
91 // @TODO Consider generalizing locale-specific operations
92 // in a utility class outside of ID package.
94 public Locale getLocale() {
98 public void setLocale(Locale locale) {
102 public void setLocale(String languageCode) {
104 if (languageCode == null || languageCode.trim().isEmpty()) {
105 logger.error("Locale language code cannot be null or empty.");
109 if (languageCode.length() != 2) {
111 "Locale language code '" + languageCode +
112 "' must be a two-letter ISO-639 language code.");
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.
121 if (! languageCode.equals(languageCode.toLowerCase())) {
122 logger.error("Locale language code must be in lower case.");
127 Locale l = new Locale(languageCode, "");
128 if (isValidLocaleForDateFormatter(l)) {
131 logger.error("Locale language code '" + languageCode +
132 "' cannot be used for formatting dates.");
137 private boolean isValidLocaleForDateFormatter(Locale l) {
138 Locale[] locales = DateFormat.getAvailableLocales();
139 return (Arrays.asList(locales).contains(l)) ? true : false;
142 private boolean isValidLength(String id) {
143 if (id.length() > getMaxOutputLength()) {