/**
* Adapted from DateUtils to support Timezones, and parse ical dates into {@link java.util.Date}.
* Note: Replace FastDateFormat to java.time, when shifting to Java 8 or higher.
*
* @param str Date representation in String.
* @param patterns Patterns to parse the date against
* @param inTimeZone Timezone of the Date.
* @return <code>java.util.Date</code> representation of string or
* <code>null</code> if the Date could not be parsed.
*/
public Date parseDate(String str, String[] patterns, TimeZone inTimeZone) {
FastDateFormat parser;
Locale locale = WebSession.get().getLocale();
TimeZone timeZone = str.endsWith("Z") ? TimeZone.getTimeZone("UTC") : inTimeZone;
ParsePosition pos = new ParsePosition(0);
for (String pattern : patterns) {
parser = FastDateFormat.getInstance(pattern, timeZone, locale);
pos.setIndex(0);
Date date = parser.parse(str, pos);
if (date != null && pos.getIndex() == str.length()) {
return date;
}
}
log.error("Unable to parse the date: {} at {}", str, -1);
return null;
}
public static IApplication ensureApplication(Long langId) {
IApplication a = ensureApplication();
if (ThreadContext.getRequestCycle() == null) {
ServletWebRequest req = new ServletWebRequest(new MockHttpServletRequest((Application)a, new MockHttpSession(a.getServletContext()), a.getServletContext()), "");
RequestCycleContext rctx = new RequestCycleContext(req, new MockWebResponse(), a.getRootRequestMapper(), a.getExceptionMapperProvider().get());
ThreadContext.setRequestCycle(new RequestCycle(rctx));
}
if (ThreadContext.getSession() == null) {
WebSession s = WebSession.get();
if (langId > 0) {
((IWebSession)s).setLanguage(langId);
}
}
return a;
}
public static IWebSession getOmSession() {
return (IWebSession)WebSession.get();
}