public static VelocityContext prepareContext(long userId, Event event, Position position) { User user = Context.getPermissionsManager().getUser(userId); Device device = Context.getIdentityManager().getById(event.getDeviceId()); VelocityContext velocityContext = new VelocityContext(); velocityContext.put("user", user); velocityContext.put("device", device); velocityContext.put("event", event); if (position != null) { velocityContext.put("position", position); velocityContext.put("speedUnit", ReportUtils.getSpeedUnit(userId)); velocityContext.put("distanceUnit", ReportUtils.getDistanceUnit(userId)); velocityContext.put("volumeUnit", ReportUtils.getVolumeUnit(userId)); } if (event.getGeofenceId() != 0) { velocityContext.put("geofence", Context.getGeofenceManager().getById(event.getGeofenceId())); } if (event.getMaintenanceId() != 0) { velocityContext.put("maintenance", Context.getMaintenancesManager().getById(event.getMaintenanceId())); } String driverUniqueId = event.getString(Position.KEY_DRIVER_UNIQUE_ID); if (driverUniqueId != null) { velocityContext.put("driver", Context.getDriversManager().getDriverByUniqueId(driverUniqueId)); } velocityContext.put("webUrl", Context.getVelocityEngine().getProperty("web.url")); velocityContext.put("dateTool", new DateTool()); velocityContext.put("numberTool", new NumberTool()); velocityContext.put("timezone", ReportUtils.getTimezone(userId)); velocityContext.put("locale", Locale.getDefault()); return velocityContext; }
private VelocityContext createVelocityContext(List<TemplateContextObject> templateContexts, Locale locale, RenderingMedia renderingMedia) { VelocityContext context = new VelocityContext(); templateContexts.stream() .forEach(templateContext -> context.put(templateContext.getKey(), templateContext.getValue())); DateTool dateTool = new DateTool(); Map<String, Object> dateToolConfig = new HashMap<>(); dateToolConfig.put("locale", locale); dateTool.configure(dateToolConfig); context.put("date", dateTool); TemplateResourcesTool templateResourcesTool = new TemplateResourcesTool(renderingMedia, referencedResourceProvider); context.put(RESOURCE_TOOL_CONTEXT_KEY, templateResourcesTool); return context; }
/** * Converts an object to an instance of {@link Date} using the * format returned by {@link #getFormat()},the {@link Locale} returned * by {@link #getLocale()}, and the {@link TimeZone} returned by * {@link #getTimeZone()} if the object is not already an instance * of Date, Calendar, or Long. * * @param obj the date to convert * @return the object as a {@link Date} or <code>null</code> if no * conversion is possible */ public Date toDate(Object obj) { return toDate(getFormat(), obj, getLocale(), getTimeZone()); }
/** * Converts an object to an instance of {@link Date} using the * specified format,the {@link Locale} returned by * {@link #getLocale()}, and the {@link TimeZone} returned by * {@link #getTimeZone()} if the object is not already an instance * of Date, Calendar, or Long. * * @param format - the format the date is in * @param obj - the date to convert * @return the object as a {@link Date} or <code>null</code> if no * conversion is possible * @see #toDate(String format, Object obj, Locale locale) */ public Date toDate(String format, Object obj) { return toDate(format, obj, getLocale(), getTimeZone()); }
/** * Converts the specified object to a date and returns * a formatted string representing that date in the specified * {@link Locale}. * * @param format the formatting instructions * @param obj the date object to be formatted * @param locale the locale to be used when formatting * @return the given date as a formatted string * @see #format(String format, Object obj, Locale locale, TimeZone timezone) */ public String format(String format, Object obj, Locale locale) { return format(format, obj, locale, getTimeZone()); }
@Override public TimeZone getTimeZone() { TimeZone timeZone = RequestContextUtils.getTimeZone(this.request); return (timeZone != null ? timeZone : super.getTimeZone()); } }
/** * Converts an object to an instance of {@link Date} using the * specified format and {@link Locale} if the object is not already * an instance of Date, Calendar, or Long. * * @param format - the format the date is in * @param obj - the date to convert * @param locale - the {@link Locale} * @return the object as a {@link Date} or <code>null</code> if no * conversion is possible * @see SimpleDateFormat#parse */ public Date toDate(String format, Object obj, Locale locale) { return toDate(format, obj, locale, getTimeZone()); }
/** * Returns a {@link Calendar} instance created using the timezone and * locale returned by getTimeZone() and getLocale(). This allows subclasses * to easily override the default locale and timezone used by this tool. * * <p>Sub-classes may override this method to return a Calendar instance * not based on the system date. * Doing so will also cause the getDate(), get(String), get(String,String), * and toString() methods to return dates equivalent to the Calendar * returned by this method, because those methods return values derived * from the result of this method.</p> * * @return a {@link Calendar} instance created using the results of * {@link #getTimeZone()} and {@link #getLocale()}. * @see Calendar#getInstance(TimeZone zone, Locale aLocale) */ public Calendar getCalendar() { return Calendar.getInstance(getTimeZone(), getLocale()); }
/** * Returns a localized date format pattern for the given format. * * SimpleDateFormat uses patterns that are based upon English * words (such as Month = M, Day = d, and Year = y). When displaying * a format pattern to readers of other languages, it is appropriate * to display these patterns using their localized expectations. * For instance, the date pattern yyyy-MM-dd should, for French speakers * appear as "aaaa-MM-jj". {@link SimpleDateFormat#toLocalizedPattern} * provides this functionality, and this method merely calls * that on an appropriately-constructed SimpleDateFormat object. * * @param format the custom or standard pattern to convert * @param locale the {@link Locale} to format for pattern for * @return a format string appropriate for the specified Locale * @since VelocityTools 2.0 */ public String toLocalizedPattern(String format, Locale locale) { DateFormat df = getDateFormat(format, locale, getTimeZone()); // Just in case DateFormat.getInstance doesn't return SimpleDateFormat if(df instanceof SimpleDateFormat) return ((SimpleDateFormat)df).toLocalizedPattern(); else return null; // Got a better idea? }
/** * Calls the superclass implementation, then looks for a bundle name * and any time units to be skipped. */ protected void configure(ValueParser values) { // do DateTool config super.configure(values); // look for an alternate bundle String bundle = values.getString(BUNDLE_NAME_KEY); if (bundle != null) { this.bundleName = bundle; } this.depth = values.getInt(DEPTH_KEY, 1); // look for time units to be ignored String[] skip = values.getStrings(SKIPPED_UNITS_KEY); if (skip != null) { timeUnits = new LinkedHashMap(TIME_UNITS); for (int i=0; i < skip.length; i++) { timeUnits.remove(skip[i]); } } }
/** * Returns a formatted string representing the date and/or time given by * {@link #getDate()} in standard, localized patterns. * * @param dateStyle the style pattern for the date * @param timeStyle the style pattern for the time * @return a formatted representation of the date returned by * {@link #getDate()} * @see DateFormat * @see #format(String dateStyle, String timeStyle, Object obj, Locale locale, TimeZone timezone) * @since VelocityTools 1.1 */ public String get(String dateStyle, String timeStyle) { return format(dateStyle, timeStyle, getDate(), getLocale()); }
/** * Converts the specified object to a date and returns * a formatted string representing that date in the locale * returned by {@link #getLocale()}. * * @param format the formatting instructions * @param obj the date object to be formatted * @return a formatted string for this locale representing the specified * date or <code>null</code> if the parameters are invalid * @see #format(String format, Object obj, Locale locale, TimeZone timezone) */ public String format(String format, Object obj) { return format(format, obj, getLocale()); }
/** * Converts the specified object to a date and formats it according to * the pattern or style returned by {@link #getFormat()}. * * @param obj the date object to be formatted * @return the specified date formatted as a string * @see #format(String format, Object obj, Locale locale, TimeZone timezone) * @since VelocityTools 1.1 */ public String format(Object obj) { return format(getFormat(), obj); }
/** * Returns a formatted string representing the date returned by * {@link #getDate()}. In its default implementation, this method * allows you to retrieve the current date in standard formats by * simply doing things like <code>$date.medium</code> or * <code>$date.full</code>. If you want only the date or time portion * you can specify that along with the standard formats. (e.g. * <code>$date.medium_date</code> or <code>$date.short_time</code>) * More complex or custom formats can be retrieved * by using the full method syntax. (e.g. $date.get('E, MMMM d')) * * @param format the formatting instructions * @return a formatted representation of the date returned by * {@link #getDate()} * @see #format(String format, Object obj, Locale locale, TimeZone timezone) * @since VelocityTools 1.1 */ public String get(String format) { return format(format, getDate()); }
/** * Initializes Velocity engine */ private void init() { velocityEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "class"); velocityEngine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); setLogFile(); DateTool dateTool = new DateTool(); dateTool.configure(this.configMap); MathTool mathTool = new MathTool(); NumberTool numberTool = new NumberTool(); numberTool.configure(this.configMap); SortTool sortTool = new SortTool(); defaultContext = new VelocityContext(); defaultContext.put("dateTool", dateTool); defaultContext.put("dateComparisonTool", new ComparisonDateTool()); defaultContext.put("mathTool", mathTool); defaultContext.put("numberTool", numberTool); defaultContext.put("sortTool", sortTool); // Following tools need VelocityTools version 2.0+ //defaultContext.put("displayTool", new DisplayTool()); //defaultContext.put("xmlTool", new XmlTool()); try { velocityEngine.init(); } catch (Exception e) { throw new VelocityException(e); } }
/** * Returns the specified date as a string formatted according to the * specified {@link Locale} and date and/or time styles. * * @param dateStyle the style pattern for the date * @param timeStyle the style pattern for the time * @param obj the date to be formatted * @param locale the {@link Locale} to be used for formatting the date * @return a formatted representation of the given date * @see #format(String dateStyle, String timeStyle, Object obj, Locale locale, TimeZone timezone) * @since VelocityTools 1.1 */ public String format(String dateStyle, String timeStyle, Object obj, Locale locale) { return format(dateStyle, timeStyle, obj, locale, getTimeZone()); }
@Override public TimeZone getTimeZone() { TimeZone timeZone = RequestContextUtils.getTimeZone(this.request); return (timeZone != null ? timeZone : super.getTimeZone()); } }
/** * Returns the specified date as a string formatted according to the * specified date and/or time styles. * * @param dateStyle the style pattern for the date * @param timeStyle the style pattern for the time * @param obj the date to be formatted * @return a formatted representation of the given date * @see #format(String dateStyle, String timeStyle, Object obj, Locale locale, TimeZone timezone) * @since VelocityTools 1.1 */ public String format(String dateStyle, String timeStyle, Object obj) { return format(dateStyle, timeStyle, obj, getLocale()); }
public static org.jxls.common.Context initializeContext(long userId) { org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext(); jxlsContext.putVar("distanceUnit", getDistanceUnit(userId)); jxlsContext.putVar("speedUnit", getSpeedUnit(userId)); jxlsContext.putVar("volumeUnit", getVolumeUnit(userId)); jxlsContext.putVar("webUrl", Context.getVelocityEngine().getProperty("web.url")); jxlsContext.putVar("dateTool", new DateTool()); jxlsContext.putVar("numberTool", new NumberTool()); jxlsContext.putVar("timezone", getTimezone(userId)); jxlsContext.putVar("locale", Locale.getDefault()); jxlsContext.putVar("bracketsRegex", "[\\{\\}\"]"); return jxlsContext; }