final DocumentBuilder builder = XmlUtils.getDocumentBuilder();
final Document document = builder.newDocument();
// Add document namespaces
final Element div = (Element) document
.appendChild(new XmlElementBuilder("div", document)
.addAttribute("xmlns:form",
"urn:jsptagdir:/WEB-INF/tags/form")
.addAttribute("xmlns:field",
"urn:jsptagdir:/WEB-INF/tags/form/fields")
.addAttribute("xmlns:jsp",
"http://java.sun.com/JSP/Page")
.addAttribute("version", "2.0")
.addChild(
new XmlElementBuilder("jsp:directive.page",
document).addAttribute("contentType",
"text/html;charset=UTF-8").build())
.addChild(
new XmlElementBuilder("jsp:output", document)
.addAttribute("omit-xml-declaration",
"yes").build()).build());
final Element formFind = new XmlElementBuilder("form:find", document)
.addAttribute(
"id",
XmlUtils.convertId("ff:"
+ formBackingType.getFullyQualifiedTypeName()))
.addAttribute("path", controllerPath)
.addAttribute(
"finderName",
finderMetadataDetails
.getFinderMethodMetadata()
.getMethodName()
.getSymbolName()
.replace(
"find"
+ formBackingTypeMetadata
.getPlural(), ""))
.build();
formFind.setAttribute("z",
XmlRoundTripUtils.calculateUniqueKeyFor(formFind));
div.appendChild(formFind);
for (final FieldMetadata field : finderMetadataDetails
.getFinderMethodParamFields()) {
final JavaType type = field.getFieldType();
final JavaSymbolName paramName = field.getFieldName();
JavaSymbolName fieldName = null;
if (paramName.getSymbolName().startsWith("max")
|| paramName.getSymbolName()
.startsWith("min")) {
fieldName = new JavaSymbolName(
Introspector.decapitalize(StringUtils
.capitalize(paramName
.getSymbolName().substring(3))));
}
else {
fieldName = paramName;
}
// Ignoring java.util.Map field types (see ROO-194)
if (type.equals(new JavaType(Map.class.getName()))) {
continue;
}
Validate.notNull(paramName, "Could not find field '%s' in '%s'",
paramName, type.getFullyQualifiedTypeName());
Element fieldElement = null;
final JavaTypeMetadataDetails typeMetadataHolder = relatedDomainTypes
.get(getJavaTypeForField(field));
if (type.isCommonCollectionType()
&& relatedDomainTypes
.containsKey(getJavaTypeForField(field))) {
final JavaTypeMetadataDetails collectionTypeMetadataHolder = relatedDomainTypes
.get(getJavaTypeForField(field));
final JavaTypePersistenceMetadataDetails typePersistenceMetadataHolder = collectionTypeMetadataHolder
.getPersistenceDetails();
if (typePersistenceMetadataHolder != null) {
fieldElement = new XmlElementBuilder("field:select",
document)
.addAttribute("required", "true")
.addAttribute(
"items",
"${"
+ collectionTypeMetadataHolder
.getPlural().toLowerCase()
+ "}")
.addAttribute(
"itemValue",
typePersistenceMetadataHolder
.getIdentifierField()
.getFieldName().getSymbolName())
.addAttribute(
"path",
"/"
+ getPathForType(getJavaTypeForField(field)))
.build();
if (field.getCustomData().keySet()
.contains(CustomDataKeys.MANY_TO_MANY_FIELD)) {
fieldElement.setAttribute("multiple", "true");
}
}
}
else if (typeMetadataHolder != null
&& typeMetadataHolder.isEnumType()
&& field.getCustomData().keySet()
.contains(CustomDataKeys.ENUMERATED_FIELD)) {
fieldElement = new XmlElementBuilder("field:select", document)
.addAttribute("required", "true")
.addAttribute(
"items",
"${"
+ typeMetadataHolder.getPlural()
.toLowerCase() + "}")
.addAttribute("path", "/" + getPathForType(type))
.build();
}
else if (type.equals(BOOLEAN_OBJECT)
|| type.equals(BOOLEAN_PRIMITIVE)) {
fieldElement = document.createElement("field:checkbox");
}
else if (typeMetadataHolder != null
&& typeMetadataHolder.isApplicationType()) {
final JavaTypePersistenceMetadataDetails typePersistenceMetadataHolder = typeMetadataHolder
.getPersistenceDetails();
if (typePersistenceMetadataHolder != null) {
fieldElement = new XmlElementBuilder("field:select",
document)
.addAttribute("required", "true")
.addAttribute(
"items",
"${"
+ typeMetadataHolder.getPlural()
.toLowerCase() + "}")
.addAttribute(
"itemValue",
typePersistenceMetadataHolder
.getIdentifierField()
.getFieldName().getSymbolName())
.addAttribute("path", "/" + getPathForType(type))
.build();
}
}
else if (type.equals(DATE) || type.equals(CALENDAR)) {
fieldElement = new XmlElementBuilder("field:datetime", document)
.addAttribute("required", "true")
.addAttribute(
"dateTimePattern",
"${"
+ entityName
+ "_"
+ fieldName.getSymbolName()
.toLowerCase()
+ "_date_format}").build();
}
if (fieldElement == null) {
fieldElement = new XmlElementBuilder("field:input", document)
.addAttribute("required", "true").build();
}
addCommonAttributes(field, fieldElement);
fieldElement.setAttribute("disableFormBinding", "true");
fieldElement.setAttribute("field", paramName.getSymbolName());