-
-
Save wendelicious/5780251 to your computer and use it in GitHub Desktop.
package com.infusionsoft.personalization; | |
/** | |
* This represents the valid date formats Infusionsoft will accept and render. | |
*/ | |
public class StyleGuide { | |
public enum Date { | |
ReadableDowMonthDdYear("formats.readable.dow-month-dd-year"), | |
ReadableMonthDdYear("formats.readable.month-dd-year"), | |
ReadableMonDdYearTime("formats.readable.mon-dd-year-time"), | |
ReadableMonDdYear("formats.readable.mon-dd-year"), | |
ReadableMonYear("formats.readable.mon-year"), | |
SlashDateMD("formats.slash.m-d"), | |
SlashDateStandard("formats.slash.date"), | |
SlashDateMMDD("formats.slash.mm-dd"), | |
SlashDateMMDDYY("formats.slash.date.short-year"), | |
SlashDateMDYYYY("formats.slash.date.no-zero"), | |
SlashDateTimeMDYYYY("formats.slash.datetime.no-zero"), | |
SlashDateTime("formats.slash.datetime"), | |
SlashDateMDYY("formats.slash.date.no-zero-short-year"), | |
DateDateStandard("formats.dash.date"), | |
DashDateDDMonYYYY("formats.dash.dd-mon-yyyy"), | |
DashDateMMDD("formats.dash.mm-dd"), | |
DashDateTimeYearFirst("formats.dash.datetime.year-first"), | |
DashDateFormDisplay("formats.dash.form-display"), | |
DumpDate("formats.dump.date"), | |
DumpDateTime("formats.dump.datetime"), | |
IcalDateTime("formats.ical.datetime"), | |
InternalDate("formats.internal"), | |
MySqlDateBinding("formats.mysql.date-binding"), | |
MySqlDateTimeBinding("formats.mysql.datetime-binding"), | |
DnsBinding("formats.dns-binding"), | |
CompanionlinkDate("formats.companionlink.date"), | |
CompanionlinkDateTime("formats.companionlink.datetime"), | |
CompanionlinkAdhocDate("formats.companionlink.adhoc-date") | |
; | |
private final String messageCode; | |
private Date(String messageCode) { | |
this.messageCode = messageCode; | |
} | |
public String getMessageCode(){ | |
return messageCode; | |
} | |
} | |
// not meant to instantiated | |
private StyleGuide(){} | |
} | |
package com.infusionsoft.personalization.dates; | |
import com.infusion.util.AssertUtils; | |
import org.joda.time.DateTime; | |
import org.joda.time.LocalDateTime; | |
import org.joda.time.format.DateTimeFormat; | |
import org.joda.time.format.DateTimeFormatter; | |
/** | |
* This interface defines the contract between the app and the outside world. | |
* | |
* Contextually, the app will request one of these appropriate for what it is doing. | |
*/ | |
public class PersonalizedDateFormat { | |
private final String dateMask; | |
private final DateTimeFormatter formatter; | |
public PersonalizedDateFormat(String dateMask){ | |
AssertUtils.paramNotNull(dateMask, "date format mask"); | |
this.dateMask = dateMask; | |
formatter = DateTimeFormat.forPattern(dateMask); | |
} | |
public String format(DateTime date) { | |
return formatter.print(date); | |
} | |
public String format(LocalDateTime local) { | |
return formatter.print(local); | |
} | |
public DateTime parseDateTime(String dateString) { | |
return formatter.parseDateTime(dateString); | |
} | |
public LocalDateTime parseLocalDateTime(String dateString){ | |
return parseDateTime(dateString).toLocalDateTime(); | |
} | |
} | |
package com.infusionsoft.personalization.service; | |
import com.infusionsoft.personalization.StyleGuide; | |
import com.infusionsoft.personalization.dates.PersonalizedDateFormat; | |
import org.springframework.stereotype.Service; | |
import javax.annotation.PostConstruct; | |
import java.util.Locale; | |
import java.util.Map; | |
import java.util.concurrent.ConcurrentHashMap; | |
/** | |
* This factory service will give you a locale-appropriate formatter/renderer for the style you request. | |
*/ | |
@Service | |
public class PersonalizedDateFormatFactory { | |
static final String DATE_FORMAT_BUNDLE_NAME = "date"; | |
private static final Map<Locale, PersonalizedDateFormatStyleFactory> localeStyleFactories = new ConcurrentHashMap<Locale, PersonalizedDateFormatStyleFactory>(); | |
@PostConstruct | |
public void init(){ | |
PersonalizedDateFormatStyleFactory us = new PersonalizedDateFormatStyleFactory(Locale.US); | |
PersonalizedDateFormatStyleFactory uk = new PersonalizedDateFormatStyleFactory(Locale.UK); | |
localeStyleFactories.put(us.getLocale(), us); | |
localeStyleFactories.put(uk.getLocale(), uk); | |
} | |
public PersonalizedDateFormat getLocalizedFormat(StyleGuide.Date style, Locale locale){ | |
PersonalizedDateFormatStyleFactory localeStyles = localeStyleFactories.get(locale); | |
if(localeStyles == null){ | |
localeStyles = new PersonalizedDateFormatStyleFactory(locale); | |
localeStyleFactories.put(locale, localeStyles); | |
} | |
return localeStyles.getStyleFormat(style); | |
} | |
} | |
package com.infusionsoft.personalization.service; | |
import com.infusion.util.AssertUtils; | |
import com.infusionsoft.personalization.StyleGuide; | |
import com.infusionsoft.personalization.dates.PersonalizedDateFormat; | |
import java.util.Locale; | |
import java.util.Map; | |
import java.util.ResourceBundle; | |
import java.util.concurrent.ConcurrentHashMap; | |
/** | |
* This uses the per-locale resources to identify the mask and initialize the appropriate personalized format. | |
*/ | |
class PersonalizedDateFormatStyleFactory { | |
private final Locale locale; | |
private final ResourceBundle dateFormatsBundle; | |
private final Map<StyleGuide.Date, PersonalizedDateFormat> namedFormats; | |
PersonalizedDateFormatStyleFactory(Locale locale) { | |
this.locale = locale; | |
this.dateFormatsBundle = ResourceBundle.getBundle(PersonalizedDateFormatFactory.DATE_FORMAT_BUNDLE_NAME, locale); | |
this.namedFormats = new ConcurrentHashMap<StyleGuide.Date, PersonalizedDateFormat>(); | |
} | |
public PersonalizedDateFormat getStyleFormat(StyleGuide.Date style){ | |
PersonalizedDateFormat format = namedFormats.get(style); | |
if(format == null){ | |
final String mask = dateFormatsBundle.getString(style.getMessageCode()); | |
AssertUtils.paramNotNull(mask, String.format("Missing localized date format named '%s' (mask '%s') for locale '%s'", style.name(), style.getMessageCode(), locale)); | |
format = new PersonalizedDateFormat(mask); | |
namedFormats.put(style, format); | |
} | |
return format; | |
} | |
public Locale getLocale(){ | |
return locale; | |
} | |
} |
date_en_US.properties:
formats.slash.m-d = M/d
formats.slash.mm-dd = MM/dd
formats.slash.date = MM/dd/yyyy
formats.slash.date.short-year = MM/dd/yy
formats.slash.date.no-zero = M/d/yyyy
formats.slash.datetime.no-zero = M/d/yyyy h:mm a
formats.slash.datetime = MM/dd/yyyy h:mm a
formats.slash.date.no-zero-short-year = M/d/yyyy h:mm a
formats.dash.dd-mon-yyyy = dd-MMM-yyyy
formats.dash.mm-dd = MM-dd
formats.dash.date = MM-dd-yyyy
formats.dash.datetime.year-first = yyyy-MM-dd h:mm a
formats.dash.form-display = MM-dd-yyyy,HH:mm
date_en_GB.properties:
formats.slash.m-d = M/d
formats.slash.mm-dd = dd/MM
formats.slash.date = dd/MM/yyyy
formats.slash.date.short-year = dd/MM/yy
formats.slash.date.no-zero = M/d/yyyy
formats.slash.datetime.no-zero = M/d/yyyy h:mm a
formats.slash.datetime = dd/MM/yyyy h:mm a
formats.slash.date.no-zero-short-year = M/d/yyyy h:mm a
formats.dash.dd-mon-yyyy = dd-MMM-yyyy
formats.dash.mm-dd = dd-MM
formats.dash.date = dd-MM-yyyy
formats.dash.datetime.year-first = yyyy-dd-MM h:mm a
formats.dash.form-display = dd-MM-yyyy,HH:mm
date.properties:
' DateLocale - What date locale the app should operate in. This handles the formatting
' of dates, either mm-dd-yyyy or dd-mm-yyyy.
' In code, this is known as "legacy block locale"
' Currently, possible values are "us" and "uk"
dateLocale=@dateLocale@
The following date formats are to be used as per the dates/times style guide
formats.internal = yyyy-MM-dd
formats.mysql.date-binding = yyyy-MM-dd
formats.mysql.datetime-binding = yyyy-MM-dd HH:mm:ss
formats.dns-binding = yyyyMMdd
formats.companionlink.datetime = yyyy-MM-dd HH:mm:ss
formats.companionlink.date = yyyy-MM-dd
formats.companionlink.adhoc-date = MM-dd-yyyy
formats.dump.date = yyyyMMdd
formats.dump.datetime = yyyyMMddhhmmss
formats.ical.datetime = yyyyMMdd'T'HHmmss
formats.readable.dow-month-dd-year = EEEE, MMMM dd, yyyy
formats.readable.month-dd-year = MMMM dd, yyyy
formats.readable.mon-dd-year-time = MMM dd, yyyy h:mm a
formats.readable.mon-dd-year = MMM dd, yyyy
formats.readable.mon-year = MMM yyyy
formats.hour-minutes = HH:mm