protected FormFieldValue getFieldNullValue(final FormWidget formWidget) { FormFieldValue fieldValue = null; if (formWidget.getType().equals(WidgetType.CHECKBOX)) { fieldValue = new FormFieldValue(Boolean.FALSE, Boolean.class.getName()); } else if (formWidget.getType().equals(WidgetType.LISTBOX_SIMPLE) || formWidget.getType().equals(WidgetType.RADIOBUTTON_GROUP)) { if (formWidget.getAvailableValues() != null && formWidget.getAvailableValues().size() > 0) { fieldValue = new FormFieldValue( formWidget.getAvailableValues().get(0).getValue(), String.class.getName()); } else { fieldValue = new FormFieldValue("", String.class.getName()); } } else if (formWidget.getType().equals(WidgetType.DATE)) { String formatPattern = null; if (formWidget.getDisplayFormat() != null && formWidget.getDisplayFormat().length() > 0) { formatPattern = formWidget.getDisplayFormat(); } else { formatPattern = defaultDateFormatPattern; } fieldValue = new FormFieldValue(null, Date.class.getName(), formatPattern); } else if (formWidget.getType().equals(WidgetType.DURATION)) { fieldValue = new FormFieldValue(Long.valueOf(0), Long.class.getName()); } else { fieldValue = new FormFieldValue(null, Serializable.class.getName()); } return fieldValue; }
/** * Build a field value object from the process definition * * @param value the value retrieved from the engine * @param formWidget the {@link FormWidget} associated with this field value * @return a {@link FormFieldValue} object */ public FormFieldValue getFieldValue( final Object value, final FormWidget formWidget, final Locale locale) { FormFieldValue fieldValue = null; if (value != null) { // deals with the types whose corresponding form fields // input and output value is not a string if (value instanceof Date) { String formatPattern = null; if (formWidget.getDisplayFormat() != null && formWidget.getDisplayFormat().length() > 0) { formatPattern = formWidget.getDisplayFormat(); } else { formatPattern = defaultDateFormatPattern; } fieldValue = new FormFieldValue((Date) value, Date.class.getName(), formatPattern); } else if (value instanceof Collection<?>) { // perform several levels of toString on the items of the collection fieldValue = new FormFieldValue( (Serializable) processCollectionValue(value), Collection.class.getName()); } else if (value instanceof String && formWidget.getType().equals(WidgetType.DATE)) { final String valueStr = (String) value; String formatPattern = null; if (formWidget.getDisplayFormat() != null && formWidget.getDisplayFormat().length() > 0) { formatPattern = formWidget.getDisplayFormat(); } else { formatPattern = defaultDateFormatPattern; } Date dateValue = null; if (valueStr.length() > 0) { try { final DateFormat dateFormat = new SimpleDateFormat(formatPattern, locale); dateValue = dateFormat.parse(valueStr); } catch (final ParseException e) { if (LOGGER.isLoggable(Level.SEVERE)) { LOGGER.log( Level.SEVERE, "The initial value for widget " + formWidget.getId() + " is not consistent with the pattern " + formatPattern, e); } } } fieldValue = new FormFieldValue(dateValue, Date.class.getName(), formatPattern); } else if (value instanceof String && formWidget.getType().equals(WidgetType.CHECKBOX)) { Boolean booleanValue = null; try { booleanValue = Boolean.parseBoolean((String) value); } catch (final Exception e) { if (LOGGER.isLoggable(Level.SEVERE)) { LOGGER.log( Level.SEVERE, "The initial value for widget " + formWidget.getId() + " is not consistent with a boolean.", e); } booleanValue = Boolean.valueOf(false); } fieldValue = new FormFieldValue(booleanValue, Boolean.class.getName()); } else if (value instanceof Serializable) { fieldValue = new FormFieldValue((Serializable) value, value.getClass().getName()); } else { fieldValue = new FormFieldValue(value.toString(), String.class.getName()); } } else { fieldValue = getFieldNullValue(formWidget); } return fieldValue; }