bodyBuilder.appendFormalLine("HtmlPanelGrid " + HTML_PANEL_GRID_ID
+ " = " + getComponentCreation("HtmlPanelGrid"));
bodyBuilder.appendFormalLine("");
for (final FieldMetadata field : locatedFields) {
final CustomData customData = field.getCustomData();
final JavaType fieldType = field.getFieldType();
final String simpleTypeName = fieldType.getSimpleTypeName();
final String fieldName = field.getFieldName().getSymbolName();
final String fieldLabelId = fieldName + suffix1;
final String fieldValueId = fieldName + suffix2;
final BigDecimal minValue = ObjectUtils.max(
getMinOrMaxValue(field, MIN),
getMinOrMaxValue(field, DECIMAL_MIN));
final BigDecimal maxValue = ObjectUtils.min(
getMinOrMaxValue(field, MAX),
getMinOrMaxValue(field, DECIMAL_MAX));
final Integer sizeMinValue = getSizeMinOrMax(field, "min");
final Integer min = ObjectUtils.min(getSizeMinOrMax(field, "max"),
getColumnLength(field));
final BigDecimal sizeMaxValue = min != null ? new BigDecimal(min)
: null;
final boolean required = action != Action.VIEW
&& (!isNullable(field) || minValue != null
|| maxValue != null || sizeMinValue != null || sizeMaxValue != null);
final boolean isTextarea = sizeMinValue != null
&& sizeMinValue.intValue() > 30 || sizeMaxValue != null
&& sizeMaxValue.intValue() > 30
|| customData.keySet().contains(CustomDataKeys.LOB_FIELD);
final boolean isUIComponent = isUIComponent(field, fieldType,
customData);
// Field label
if (action.equals(Action.VIEW) || !isUIComponent) {
bodyBuilder.appendFormalLine("HtmlOutputText " + fieldLabelId
+ " = " + getComponentCreation("HtmlOutputText"));
}
else {
bodyBuilder.appendFormalLine("OutputLabel " + fieldLabelId
+ " = " + getComponentCreation("OutputLabel"));
bodyBuilder.appendFormalLine(fieldLabelId + ".setFor(\""
+ fieldValueId + "\");");
}
bodyBuilder.appendFormalLine(fieldLabelId + ".setId(\""
+ fieldLabelId + "\");");
bodyBuilder.appendFormalLine(fieldLabelId + ".setValue(\""
+ field.getFieldName().getReadableSymbolName() + ":\");");
bodyBuilder.appendFormalLine(getAddToPanelText(fieldLabelId));
bodyBuilder.appendFormalLine("");
// Field value
final String converterName = fieldValueId + "Converter";
final String htmlOutputTextStr = "HtmlOutputText " + fieldValueId
+ " = " + getComponentCreation("HtmlOutputText");
final String inputTextStr = "InputText " + fieldValueId + " = "
+ getComponentCreation("InputText");
final String componentIdStr = fieldValueId + ".setId(\""
+ fieldValueId + "\");";
final String requiredStr = fieldValueId + ".setRequired("
+ required + ");";
if (field.getAnnotation(ROO_UPLOADED_FILE) != null) {
final AnnotationMetadata annotation = field
.getAnnotation(ROO_UPLOADED_FILE);
final String contentType = (String) annotation.getAttribute(
"contentType").getValue();
final String allowedType = UploadedFileContentType
.getFileExtension(contentType).name();
if (action == Action.VIEW) {
builder.getImportRegistrationResolver().addImports(
UI_COMPONENT,
PRIMEFACES_FILE_DOWNLOAD_ACTION_LISTENER,
PRIMEFACES_COMMAND_BUTTON,
PRIMEFACES_STREAMED_CONTENT);
// bodyBuilder.appendFormalLine("CommandButton " +
// fieldValueId + " = " +
// getComponentCreation("CommandButton"));
// bodyBuilder.appendFormalLine(fieldValueId +
// ".addActionListener(new FileDownloadActionListener(expressionFactory.createValueExpression(elContext, \"#{"
// + beanName + "." +
// fieldName +
// "StreamedContent}\", StreamedContent.class), null));");
// bodyBuilder.appendFormalLine(fieldValueId +
// ".setValue(\"Download\");");
// bodyBuilder.appendFormalLine(fieldValueId +
// ".setAjax(false);");
// TODO Make following code work as currently the view panel
// is not refreshed and the download field is always seen as
// null
bodyBuilder.appendFormalLine("UIComponent " + fieldValueId
+ ";");
bodyBuilder.appendFormalLine("if (" + entityName
+ " != null && " + entityName + ".get"
+ StringUtils.capitalize(fieldName)
+ "() != null && " + entityName + ".get"
+ StringUtils.capitalize(fieldName)
+ "().length > 0) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldValueId + " = "
+ getComponentCreation("CommandButton"));
bodyBuilder
.appendFormalLine("((CommandButton) "
+ fieldValueId
+ ").addActionListener(new FileDownloadActionListener(expressionFactory.createValueExpression(elContext, \"#{"
+ beanName
+ "."
+ fieldName
+ "StreamedContent}\", StreamedContent.class), null));");
bodyBuilder.appendFormalLine("((CommandButton) "
+ fieldValueId + ").setValue(\"Download\");");
bodyBuilder.appendFormalLine("((CommandButton) "
+ fieldValueId + ").setAjax(false);");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("} else {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldValueId + " = "
+ getComponentCreation("HtmlOutputText"));
bodyBuilder.appendFormalLine("((HtmlOutputText) "
+ fieldValueId + ").setValue(\"\");");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
}
else {
builder.getImportRegistrationResolver().addImports(
PRIMEFACES_FILE_UPLOAD,
PRIMEFACES_FILE_UPLOAD_EVENT);
bodyBuilder.appendFormalLine("FileUpload " + fieldValueId
+ " = " + getComponentCreation("FileUpload"));
bodyBuilder.appendFormalLine(componentIdStr);
bodyBuilder
.appendFormalLine(fieldValueId
+ ".setFileUploadListener(expressionFactory.createMethodExpression(elContext, \"#{"
+ beanName
+ "."
+ getFileUploadMethodName(fieldName)
+ "}\", void.class, new Class[] { FileUploadEvent.class }));");
bodyBuilder.appendFormalLine(fieldValueId
+ ".setMode(\"advanced\");");
bodyBuilder.appendFormalLine(fieldValueId
+ ".setAllowTypes(\"/(\\\\.|\\\\/)("
+ getAllowTypeRegex(allowedType) + ")$/\");");
bodyBuilder.appendFormalLine(fieldValueId
+ ".setUpdate(\":growlForm:growl\");");
final AnnotationAttributeValue<?> autoUploadAttr = annotation
.getAttribute("autoUpload");
if (autoUploadAttr != null
&& (Boolean) autoUploadAttr.getValue()) {
bodyBuilder.appendFormalLine(fieldValueId
+ ".setAuto(true);");
}
bodyBuilder.appendFormalLine(requiredStr);
}
}
else if (fieldType.equals(BOOLEAN_OBJECT)
|| fieldType.equals(BOOLEAN_PRIMITIVE)) {
if (action == Action.VIEW) {
bodyBuilder.appendFormalLine(htmlOutputTextStr);
bodyBuilder.appendFormalLine(getSetValueExpression(
fieldValueId, fieldName));
}
else {
builder.getImportRegistrationResolver().addImport(
PRIMEFACES_SELECT_BOOLEAN_CHECKBOX);
bodyBuilder.appendFormalLine("SelectBooleanCheckbox "
+ fieldValueId + " = "
+ getComponentCreation("SelectBooleanCheckbox"));
bodyBuilder.appendFormalLine(componentIdStr);
bodyBuilder.appendFormalLine(getSetValueExpression(
fieldValueId, fieldName, simpleTypeName));
bodyBuilder.appendFormalLine(requiredStr);
}
}
else if (customData.keySet().contains(ENUMERATED_KEY)) {
if (action == Action.VIEW) {
bodyBuilder.appendFormalLine(htmlOutputTextStr);
bodyBuilder.appendFormalLine(getSetValueExpression(
fieldValueId, fieldName));
}
else {
builder.getImportRegistrationResolver().addImports(
PRIMEFACES_AUTO_COMPLETE, fieldType);
bodyBuilder.appendFormalLine("AutoComplete " + fieldValueId
+ " = " + getComponentCreation("AutoComplete"));
bodyBuilder.appendFormalLine(componentIdStr);
bodyBuilder.appendFormalLine(getSetValueExpression(
fieldValueId, fieldName, simpleTypeName));
bodyBuilder.appendFormalLine(getSetCompleteMethod(
fieldValueId, fieldName));
bodyBuilder.appendFormalLine(fieldValueId
+ ".setDropdown(true);");
bodyBuilder.appendFormalLine(requiredStr);
}
}
else if (JdkJavaType.isDateField(fieldType)) {
if (action == Action.VIEW) {
builder.getImportRegistrationResolver().addImport(
DATE_TIME_CONVERTER);
bodyBuilder.appendFormalLine(htmlOutputTextStr);
bodyBuilder.appendFormalLine(getSetValueExpression(
fieldValueId, fieldName, simpleTypeName));
bodyBuilder
.appendFormalLine("DateTimeConverter "
+ converterName
+ " = (DateTimeConverter) application.createConverter(DateTimeConverter.CONVERTER_ID);");
// TODO Get working:
// bodyBuilder.appendFormalLine(converterName +
// ".setPattern(((SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT)).toPattern());");
bodyBuilder.appendFormalLine(converterName
+ ".setPattern(\"dd/MM/yyyy\");");
bodyBuilder.appendFormalLine(fieldValueId
+ ".setConverter(" + converterName + ");");
}
else {
builder.getImportRegistrationResolver().addImports(
PRIMEFACES_CALENDAR, DATE);
// builder.getImportRegistrationResolver().addImports(DATE_FORMAT,
// SIMPLE_DATE_FORMAT);
bodyBuilder.appendFormalLine("Calendar " + fieldValueId
+ " = " + getComponentCreation("Calendar"));
bodyBuilder.appendFormalLine(componentIdStr);
bodyBuilder.appendFormalLine(getSetValueExpression(
fieldValueId, fieldName, "Date"));
bodyBuilder.appendFormalLine(fieldValueId
+ ".setNavigator(true);");
bodyBuilder.appendFormalLine(fieldValueId
+ ".setEffect(\"slideDown\");");
// TODO Get working:
// bodyBuilder.appendFormalLine(fieldValueId +
// ".setPattern(((SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT)).toPattern());");
bodyBuilder.appendFormalLine(fieldValueId
+ ".setPattern(\"dd/MM/yyyy\");");
bodyBuilder.appendFormalLine(requiredStr);
if (MemberFindingUtils.getAnnotationOfType(
field.getAnnotations(), PAST) != null) {
bodyBuilder.appendFormalLine(fieldValueId
+ ".setMaxdate(new Date());");
}
if (MemberFindingUtils.getAnnotationOfType(
field.getAnnotations(), FUTURE) != null) {
bodyBuilder.appendFormalLine(fieldValueId
+ ".setMindate(new Date());");
}
}
}
else if (JdkJavaType.isIntegerType(fieldType)) {
if (action == Action.VIEW) {
bodyBuilder.appendFormalLine(htmlOutputTextStr);
bodyBuilder.appendFormalLine(getSetValueExpression(
fieldValueId, fieldName));
}
else {
builder.getImportRegistrationResolver().addImports(
PRIMEFACES_INPUT_TEXT, PRIMEFACES_SPINNER);
if (fieldType.equals(JdkJavaType.BIG_INTEGER)) {
builder.getImportRegistrationResolver().addImport(
fieldType);
}
bodyBuilder.appendFormalLine("Spinner " + fieldValueId
+ " = " + getComponentCreation("Spinner"));
bodyBuilder.appendFormalLine(componentIdStr);
bodyBuilder.appendFormalLine(getSetValueExpression(
fieldValueId, fieldName, simpleTypeName));
bodyBuilder.appendFormalLine(requiredStr);
if (minValue != null || maxValue != null) {
if (minValue != null) {
bodyBuilder.appendFormalLine(fieldValueId
+ ".setMin(" + minValue.doubleValue()
+ ");");
}
if (maxValue != null) {
bodyBuilder.appendFormalLine(fieldValueId
+ ".setMax(" + maxValue.doubleValue()
+ ");");
}
bodyBuilder.append(getLongRangeValdatorString(
fieldValueId, minValue, maxValue));
}
bodyBuilder.appendFormalLine("");
}
}
else if (JdkJavaType.isDecimalType(fieldType)) {
if (action == Action.VIEW) {
bodyBuilder.appendFormalLine(htmlOutputTextStr);
bodyBuilder.appendFormalLine(getSetValueExpression(
fieldValueId, fieldName));
}
else {
builder.getImportRegistrationResolver().addImport(
PRIMEFACES_INPUT_TEXT);
if (fieldType.equals(JdkJavaType.BIG_DECIMAL)) {
builder.getImportRegistrationResolver().addImport(
fieldType);
}
bodyBuilder.appendFormalLine(inputTextStr);
bodyBuilder.appendFormalLine(componentIdStr);
bodyBuilder.appendFormalLine(getSetValueExpression(
fieldValueId, fieldName, simpleTypeName));
bodyBuilder.appendFormalLine(requiredStr);
if (minValue != null || maxValue != null) {
bodyBuilder.append(getDoubleRangeValdatorString(
fieldValueId, minValue, maxValue));
}
}
}
else if (fieldType.equals(STRING)) {
if (isTextarea) {
builder.getImportRegistrationResolver().addImport(
PRIMEFACES_INPUT_TEXTAREA);
bodyBuilder.appendFormalLine("InputTextarea "
+ fieldValueId + " = "
+ getComponentCreation("InputTextarea"));
}
else {
if (action == Action.VIEW) {
bodyBuilder.appendFormalLine(htmlOutputTextStr);
}
else {
builder.getImportRegistrationResolver().addImport(
PRIMEFACES_INPUT_TEXT);
bodyBuilder.appendFormalLine(inputTextStr);
}
}
bodyBuilder.appendFormalLine(componentIdStr);
bodyBuilder.appendFormalLine(getSetValueExpression(
fieldValueId, fieldName));
if (action == Action.VIEW) {
if (isTextarea) {
bodyBuilder.appendFormalLine(fieldValueId
+ ".setReadonly(true);");
bodyBuilder.appendFormalLine(fieldValueId
+ ".setDisabled(true);");
}
}
else {
if (sizeMinValue != null || sizeMaxValue != null) {
bodyBuilder.append(getLengthValdatorString(
fieldValueId, sizeMinValue, sizeMaxValue));
}
setRegexPatternValidationString(field, fieldValueId,
bodyBuilder);
bodyBuilder.appendFormalLine(requiredStr);
}
}
else if (customData.keySet().contains(PARAMETER_TYPE_KEY)) {
final JavaType parameterType = (JavaType) customData
.get(PARAMETER_TYPE_KEY);
final String parameterTypeSimpleTypeName = parameterType
.getSimpleTypeName();
final String parameterTypeFieldName = StringUtils
.uncapitalize(parameterTypeSimpleTypeName);
final String parameterTypeManagedBeanName = (String) customData
.get(PARAMETER_TYPE_MANAGED_BEAN_NAME_KEY);
final String parameterTypePlural = (String) customData
.get(PARAMETER_TYPE_PLURAL_KEY);
if (StringUtils.isNotBlank(parameterTypeManagedBeanName)) {
if (customData.keySet().contains(ONE_TO_MANY_FIELD)
|| customData.keySet().contains(MANY_TO_MANY_FIELD)
&& isInverseSideOfRelationship(field, ONE_TO_MANY,
MANY_TO_MANY)) {
bodyBuilder.appendFormalLine(htmlOutputTextStr);
bodyBuilder.appendFormalLine(componentIdStr);
bodyBuilder
.appendFormalLine(fieldValueId
+ ".setValue(\"This relationship is managed from the "
+ parameterTypeSimpleTypeName
+ " side\");");
}
else {
final JavaType converterType = new JavaType(destination
.getPackage().getFullyQualifiedPackageName()
+ ".converter."
+ parameterTypeSimpleTypeName
+ "Converter");
builder.getImportRegistrationResolver().addImports(
PRIMEFACES_SELECT_MANY_MENU, UI_SELECT_ITEMS,
fieldType, converterType);
bodyBuilder.appendFormalLine("SelectManyMenu "
+ fieldValueId + " = "
+ getComponentCreation("SelectManyMenu"));
bodyBuilder.appendFormalLine(componentIdStr);
bodyBuilder.appendFormalLine(fieldValueId
+ ".setConverter(new "
+ converterType.getSimpleTypeName() + "());");
bodyBuilder
.appendFormalLine(fieldValueId
+ ".setValueExpression(\"value\", expressionFactory.createValueExpression(elContext, \"#{"
+ beanName + "."
+ getSelectedFieldName(fieldName)
+ "}\", List.class));");
bodyBuilder
.appendFormalLine("UISelectItems "
+ fieldValueId
+ "Items = (UISelectItems) application.createComponent(UISelectItems.COMPONENT_TYPE);");
if (action == Action.VIEW) {
bodyBuilder.appendFormalLine(fieldValueId
+ ".setReadonly(true);");
bodyBuilder.appendFormalLine(fieldValueId
+ ".setDisabled(true);");
bodyBuilder
.appendFormalLine(fieldValueId
+ "Items.setValueExpression(\"value\", expressionFactory.createValueExpression(elContext, \"#{"
+ beanName + "."
+ entityName.getSymbolName() + "."
+ fieldName + "}\", "
+ simpleTypeName + ".class));");
}
else {
bodyBuilder
.appendFormalLine(fieldValueId
+ "Items.setValueExpression(\"value\", expressionFactory.createValueExpression(elContext, \"#{"
+ parameterTypeManagedBeanName
+ ".all"
+ StringUtils
.capitalize(parameterTypePlural)
+ "}\", List.class));");
bodyBuilder.appendFormalLine(requiredStr);
}
bodyBuilder
.appendFormalLine(fieldValueId
+ "Items.setValueExpression(\"var\", expressionFactory.createValueExpression(elContext, \""
+ parameterTypeFieldName
+ "\", String.class));");
bodyBuilder
.appendFormalLine(fieldValueId
+ "Items.setValueExpression(\"itemLabel\", expressionFactory.createValueExpression(elContext, \"#{"
+ parameterTypeFieldName
+ "}\", String.class));");
bodyBuilder
.appendFormalLine(fieldValueId
+ "Items.setValueExpression(\"itemValue\", expressionFactory.createValueExpression(elContext, \"#{"
+ parameterTypeFieldName + "}\", "
+ parameterTypeSimpleTypeName
+ ".class));");
bodyBuilder.appendFormalLine(getAddChildToComponent(
fieldValueId, fieldValueId + "Items"));
}
}
else {
// Parameter type is an enum
bodyBuilder.appendFormalLine("SelectManyMenu "
+ fieldValueId + " = "
+ getComponentCreation("SelectManyMenu"));
bodyBuilder.appendFormalLine(componentIdStr);
if (action == Action.VIEW) {
bodyBuilder.appendFormalLine(fieldValueId
+ ".setReadonly(true);");
bodyBuilder.appendFormalLine(fieldValueId
+ ".setDisabled(true);");
bodyBuilder
.appendFormalLine(fieldValueId
+ ".setValueExpression(\"value\", expressionFactory.createValueExpression(elContext, \"#{"
+ beanName + "."
+ getSelectedFieldName(fieldName)
+ "}\", List.class));");
bodyBuilder
.appendFormalLine("UISelectItems "
+ fieldValueId
+ "Items = (UISelectItems) application.createComponent(UISelectItems.COMPONENT_TYPE);");
bodyBuilder
.appendFormalLine(fieldValueId
+ "Items.setValueExpression(\"value\", expressionFactory.createValueExpression(elContext, \"#{"
+ beanName + "."
+ entityName.getSymbolName() + "."
+ fieldName + "}\", " + simpleTypeName
+ ".class));");
bodyBuilder
.appendFormalLine(fieldValueId
+ "Items.setValueExpression(\"var\", expressionFactory.createValueExpression(elContext, \""
+ parameterTypeFieldName
+ "\", String.class));");
bodyBuilder
.appendFormalLine(fieldValueId
+ "Items.setValueExpression(\"itemLabel\", expressionFactory.createValueExpression(elContext, \"#{"
+ parameterTypeFieldName
+ "}\", String.class));");
bodyBuilder
.appendFormalLine(fieldValueId
+ "Items.setValueExpression(\"itemValue\", expressionFactory.createValueExpression(elContext, \"#{"
+ parameterTypeFieldName + "}\", "
+ parameterTypeSimpleTypeName
+ ".class));");
bodyBuilder.appendFormalLine(getAddChildToComponent(
fieldValueId, fieldValueId + "Items"));
}
else {
builder.getImportRegistrationResolver().addImports(
UI_SELECT_ITEM, ENUM_CONVERTER);
bodyBuilder
.appendFormalLine(fieldValueId
+ ".setValueExpression(\"value\", expressionFactory.createValueExpression(elContext, \"#{"
+ beanName + "."
+ getSelectedFieldName(fieldName)
+ "}\", List.class));");
bodyBuilder.appendFormalLine(fieldValueId
+ ".setConverter(new EnumConverter("
+ parameterTypeSimpleTypeName + ".class));");
bodyBuilder.appendFormalLine(requiredStr);
bodyBuilder.appendFormalLine("UISelectItem "
+ fieldValueId + "Item;");
bodyBuilder
.appendFormalLine("for ("
+ parameterTypeSimpleTypeName
+ " "
+ StringUtils
.uncapitalize(parameterTypeSimpleTypeName)
+ " : " + parameterTypeSimpleTypeName
+ ".values()) {");
bodyBuilder.indent();
bodyBuilder
.appendFormalLine(fieldValueId
+ "Item = (UISelectItem) application.createComponent(UISelectItem.COMPONENT_TYPE);");
bodyBuilder
.appendFormalLine(fieldValueId
+ "Item.setItemLabel("
+ StringUtils
.uncapitalize(parameterTypeSimpleTypeName)
+ ".name());");
bodyBuilder
.appendFormalLine(fieldValueId
+ "Item.setItemValue("
+ StringUtils
.uncapitalize(parameterTypeSimpleTypeName)
+ ");");
bodyBuilder.appendFormalLine(getAddChildToComponent(
fieldValueId, fieldValueId + "Item"));
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
}
}
}
else if (customData.keySet().contains(APPLICATION_TYPE_KEY)) {
if (customData.keySet().contains(ONE_TO_ONE_FIELD)
&& isInverseSideOfRelationship(field, ONE_TO_ONE)) {
bodyBuilder.appendFormalLine(htmlOutputTextStr);
bodyBuilder.appendFormalLine(componentIdStr);
bodyBuilder
.appendFormalLine(fieldValueId