use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class ThymeleafMetadata method getAddColumnToReportBuilderMethod.
/* Jasper Export Methods */
/**
* Generates a method to add columns to DynamicJasper report builder.
*
* @return MethodMetadata
*/
private MethodMetadata getAddColumnToReportBuilderMethod() {
JavaSymbolName methodName = ADD_COLUMN_TO_REPORT_BUILDER_METHOD_NAME;
// Including parameter types
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(STRING_PARAM);
parameterTypes.add(new AnnotatedJavaType(FAST_REPORT_BUILDER));
parameterTypes.add(LOCALE_PARAM);
parameterTypes.add(STRING_PARAM);
// Check method existence
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Including parameter names
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
final JavaSymbolName columnName = new JavaSymbolName("columnName");
parameterNames.add(columnName);
final JavaSymbolName reportBuilder = new JavaSymbolName("builder");
parameterNames.add(reportBuilder);
parameterNames.add(LOCALE_PARAM_NAME);
parameterNames.add(FILE_NAME_PARAM_NAME);
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// try {
bodyBuilder.appendFormalLine("try {");
for (int i = 0; i < this.entityValidFields.size(); i++) {
FieldMetadata fieldMetadata = this.entityValidFields.get(i);
String fieldName = fieldMetadata.getFieldName().getSymbolName();
if (i == 0) {
// if (columnName.equals("FIELD")) {
bodyBuilder.appendFormalLine("if (columnName.equals(\"%s\")) {", fieldName);
} else {
// else if (columnName.equals("FIELD")) {
bodyBuilder.appendFormalLine("else if (columnName.equals(\"%s\")) {", fieldName);
}
bodyBuilder.indent();
JavaType fieldType = fieldMetadata.getFieldType();
if (fieldMetadata.getFieldName().equals(this.entityMetadata.getCurrentIndentifierField().getFieldName())) {
// 50);
if (fieldType.isPrimitive()) {
// Print SimpleTypeName of JavaType when it is a primitive
bodyBuilder.appendFormalLine("builder.addColumn(%s().getMessage(\"%s_%s\", null, \"%s\", locale), \"%s\", %s.class.getName(), 50);", getAccessorMethod(this.messageSourceField).getMethodName(), this.entityLabel, fieldName.toLowerCase(), getFieldDefaultLabelValue(fieldName), fieldName, fieldType.getSimpleTypeName());
// assure import of fielType is in aj
getNameOfJavaType(fieldType);
} else {
bodyBuilder.appendFormalLine("builder.addColumn(%s().getMessage(\"%s_%s\", null, \"%s\", locale), \"%s\", %s.class.getName(), 50);", getAccessorMethod(this.messageSourceField).getMethodName(), this.entityLabel, fieldName.toLowerCase(), getFieldDefaultLabelValue(fieldName), fieldName, getNameOfJavaType(fieldType.withoutParameters()));
}
} else {
// 100);
if (fieldType.isPrimitive()) {
bodyBuilder.appendFormalLine("builder.addColumn(%s().getMessage(\"%s_%s\", null, \"%s\", locale), \"%s\", %s.class.getName(), 100);", getAccessorMethod(this.messageSourceField).getMethodName(), this.entityLabel, fieldName.toLowerCase(), getFieldDefaultLabelValue(fieldName), fieldName, fieldType.getSimpleTypeName());
// assure import of fielType is in aj
getNameOfJavaType(fieldType);
} else {
bodyBuilder.appendFormalLine("builder.addColumn(%s().getMessage(\"%s_%s\", null, \"%s\", locale), \"%s\", %s.class.getName(), 100);", getAccessorMethod(this.messageSourceField).getMethodName(), this.entityLabel, fieldName.toLowerCase(), getFieldDefaultLabelValue(fieldName), fieldName, getNameOfJavaType(fieldType.withoutParameters()));
}
}
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
}
// }
bodyBuilder.appendFormalLine("}");
// Build catch blocks
buildExportCatchBlock(bodyBuilder, COLUMN_BUILDER_EXCEPTION);
buildExportCatchBlock(bodyBuilder, CLASS_NOT_FOUND_EXCEPTION);
// Build method
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, parameterTypes, parameterNames, bodyBuilder);
// Add Javadoc to method
CommentStructure commentStructure = new CommentStructure();
String description = "This method contains all the entity fields that are able to be displayed in a ".concat(IOUtils.LINE_SEPARATOR).concat("report. The developer could add a new column to the report builder providing the ").concat(IOUtils.LINE_SEPARATOR).concat("field name and the builder where the new field will be added as column.");
List<String> paramInfo = new ArrayList<String>();
paramInfo.add("columnName the field name to show as column");
paramInfo.add("builder The builder where the new field will be added as column.");
commentStructure.addComment(new JavadocComment(description, paramInfo, null, null), CommentLocation.BEGINNING);
methodBuilder.setCommentStructure(commentStructure);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class JSONMetadata method getConstructor.
private ConstructorMetadata getConstructor(boolean autowired) {
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Generating constructor
ConstructorMetadataBuilder constructor = new ConstructorMetadataBuilder(getId());
constructor.setModifier(Modifier.PUBLIC);
if (autowired) {
constructor.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.AUTOWIRED));
}
// Getting serviceFieldName
String serviceFieldName = controllerMetadata.getServiceField().getFieldName().getSymbolName();
constructor.addParameter(serviceFieldName, controllerMetadata.getService());
// Generating body
bodyBuilder.appendFormalLine(String.format("this.%s = %s;", serviceFieldName, serviceFieldName));
if (this.type == ControllerType.DETAIL || this.type == ControllerType.DETAIL_ITEM) {
for (FieldMetadata serviceField : controllerMetadata.getDetailsServiceFields().values()) {
// Getting parentServiceFieldName
String childServiceFieldName = serviceField.getFieldName().getSymbolName();
// Adding parameters
constructor.addParameter(childServiceFieldName, serviceField.getFieldType());
// Generating body
bodyBuilder.appendFormalLine(String.format("this.%s = %s;", childServiceFieldName, childServiceFieldName));
}
}
// Adding body
constructor.setBodyBuilder(bodyBuilder);
return constructor.build();
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class JSONMetadata method getAddToDetailsMethod.
private MethodMetadata getAddToDetailsMethod() {
RelationInfo detailsInfo = controllerMetadata.getLastDetailsInfo();
final ServiceMetadata detailsServiceMetadata = controllerMetadata.getServiceMetadataForEntity(detailsInfo.entityType);
final MethodMetadata addToMethod = detailsServiceMetadata.getAddToRelationMethods().get(detailsInfo);
final FieldMetadata detailsServiceField = controllerMetadata.getDetailsServiceFields(detailsInfo.entityType);
// Define methodName
final JavaSymbolName methodName = addToMethod.getMethodName();
JavaSymbolName itemsName = addToMethod.getParameterNames().get(1);
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(new AnnotatedJavaType(addToMethod.getParameterTypes().get(0).getJavaType(), ANN_MODEL_ATTRIBUTE));
AnnotationMetadataBuilder pathVariableAnnotation = new AnnotationMetadataBuilder(SpringJavaType.PATH_VARIABLE);
pathVariableAnnotation.addStringAttribute("value", itemsName.getSymbolName());
parameterTypes.add(new AnnotatedJavaType(addToMethod.getParameterTypes().get(1).getJavaType().getParameters().get(0), pathVariableAnnotation.build()));
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @PostMapping annotation
AnnotationMetadataBuilder postMappingAnnotation = new AnnotationMetadataBuilder(POST_MAPPING);
postMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
annotations.add(postMappingAnnotation);
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.addAll(addToMethod.getParameterNames());
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// customerService.addToOrders(customer, Collections.singleton(order));
bodyBuilder.appendFormalLine("%s().%s(%s,%s.singleton(%s));", getAccessorMethod(detailsServiceField).getMethodName(), addToMethod.getMethodName(), addToMethod.getParameterNames().get(0), getNameOfJavaType(JavaType.COLLECTIONS), itemsName);
// return ResponseEntity.ok().build();
bodyBuilder.appendFormalLine("return %s.ok().build();", getNameOfJavaType(RESPONSE_ENTITY));
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.wrapperWilcard(RESPONSE_ENTITY), parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class ThymeleafMetadata method getFinderFormMethodForFinderInService.
/**
* Generates a finder method which delegates on entity service to get result
*
* @param finderName
* @param serviceFinderMethod
* @return
*/
private MethodMetadata getFinderFormMethodForFinderInService(String finderName, MethodMetadata serviceFinderMethod) {
// Define methodName
String pathName = finderName;
if (pathName.startsWith("findBy")) {
pathName = pathName.replace("findBy", "by");
}
final JavaSymbolName methodName = new JavaSymbolName(pathName.concat("Form"));
// Form Bean is always the first parameter of finder
final JavaType formBean = serviceFinderMethod.getParameterTypes().get(0).getJavaType();
List<AnnotationMetadata> formBeanAnnotations = new ArrayList<AnnotationMetadata>();
AnnotationMetadataBuilder formBeanAnnotation = new AnnotationMetadataBuilder(SpringJavaType.MODEL_ATTRIBUTE);
formBeanAnnotation.addStringAttribute("value", FORM_BEAN_PARAM_NAME.getSymbolName());
formBeanAnnotations.add(formBeanAnnotation.build());
AnnotatedJavaType annotatedFormBean = new AnnotatedJavaType(formBean, formBeanAnnotations);
// Including annotated formBean parameter and Model parameter
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(annotatedFormBean);
parameterTypes.add(MODEL_PARAM);
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(FORM_BEAN_PARAM_NAME);
parameterNames.add(MODEL_PARAM_NAME);
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @GetMapping annotation
AnnotationMetadataBuilder getMappingAnnotation = new AnnotationMetadataBuilder(GET_MAPPING);
getMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
// TODO Delegates on ControllerOperations to obtain the URL for this
// finder
getMappingAnnotation.addStringAttribute("value", "/" + pathName + "/search-form");
annotations.add(getMappingAnnotation);
this.mvcMethodNames.put(methodName.getSymbolName(), methodName.getSymbolName());
// Generate body
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
buildPopulateFormatBody(bodyBuilder, formBeansDateTimeFields.get(formBean));
final List<FieldMetadata> enumFileds = formBeansEnumFields.get(formBean);
if (enumFileds != null && !enumFileds.isEmpty()) {
buildPopulateEnumsBody(bodyBuilder, formBeansEnumFields.get(formBean));
}
// return new ModelAndView("customers/findByFirstNameLastNameForm");
bodyBuilder.appendFormalLine("return new %s(\"%s/%s\");", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW), viewsPath, finderName.concat("Form"));
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, SpringJavaType.MODEL_AND_VIEW, parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class JspViewManager method getFinderDocument.
public Document getFinderDocument(final FinderMetadataDetails finderMetadataDetails) {
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());
fieldElement.setAttribute("id", XmlUtils.convertId("f:" + formBackingType.getFullyQualifiedTypeName() + "." + paramName));
fieldElement.setAttribute("z", XmlRoundTripUtils.calculateUniqueKeyFor(fieldElement));
formFind.appendChild(fieldElement);
}
DomUtils.removeTextNodes(document);
return document;
}
Aggregations