use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.
the class ThymeleafControllerIntegrationTestMetadata method getTestExampleMethod.
/**
* Builds and return a test example method.
*
* @return {@link MethodMetadata}
*/
private MethodMetadata getTestExampleMethod() {
JavaSymbolName methodName = new JavaSymbolName("testMethodExample");
// Check if method exists in governor
MethodMetadata method = getGovernorMethod(methodName);
if (method != null) {
return method;
}
// Build method body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Setup
bodyBuilder.appendFormalLine("// Setup");
bodyBuilder.appendFormalLine("// Previous tasks");
bodyBuilder.newLine();
// Exercise
bodyBuilder.appendFormalLine("// Exercise");
bodyBuilder.appendFormalLine("// Execute method to test");
bodyBuilder.newLine();
// Verify
bodyBuilder.appendFormalLine("// Verify");
bodyBuilder.appendFormalLine("// Check results with assertions");
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(this.getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, bodyBuilder);
// Add @Test
methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
CommentStructure commentStructure = new CommentStructure();
commentStructure.addComment(new JavadocComment("Test method example. To be implemented by developer."), CommentLocation.BEGINNING);
methodBuilder.setCommentStructure(commentStructure);
return methodBuilder.build();
}
use of org.springframework.roo.model.JavaSymbolName 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.model.JavaSymbolName in project spring-roo by spring-projects.
the class WebMvcConfigurationMetadata method getAddInterceptors.
/**
* Method that generates "addInterceptors" method.
*
* @return MethodMetadata
*/
private MethodMetadata getAddInterceptors() {
// Define method name
JavaSymbolName methodName = new JavaSymbolName("addInterceptors");
// Define method parameter types
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(new JavaType("org.springframework.web.servlet.config.annotation.InterceptorRegistry")));
// Define method parameter names
List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(new JavaSymbolName("registry"));
if (governorHasMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes))) {
return getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
}
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine("registry.addInterceptor(localeChangeInterceptor());");
// Add TracEE interceptor
bodyBuilder.appendFormalLine("registry.addInterceptor(new %s());", getNameOfJavaType(TRACEE_INTERCEPTOR_JAVATYPE));
// Use the MethodMetadataBuilder for easy creation of MethodMetadata
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, parameterTypes, parameterNames, bodyBuilder);
// Add Bean annotation
methodBuilder.addAnnotation(new AnnotationMetadataBuilder(JavaType.OVERRIDE));
// Build and return a MethodMetadata
return methodBuilder.build();
// instance
}
use of org.springframework.roo.model.JavaSymbolName 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;
}
use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.
the class JspViewManager method addCommonAttributes.
private void addCommonAttributes(final FieldMetadata field, final Element fieldElement) {
AnnotationMetadata annotationMetadata;
if (field.getFieldType().equals(INT_OBJECT) || field.getFieldType().getFullyQualifiedTypeName().equals(int.class.getName()) || field.getFieldType().equals(SHORT_OBJECT) || field.getFieldType().getFullyQualifiedTypeName().equals(short.class.getName()) || field.getFieldType().equals(LONG_OBJECT) || field.getFieldType().getFullyQualifiedTypeName().equals(long.class.getName()) || field.getFieldType().equals(BIG_INTEGER)) {
fieldElement.setAttribute("validationMessageCode", "field_invalid_integer");
} else if (isEmailField(field)) {
fieldElement.setAttribute("validationMessageCode", "field_invalid_email");
} else if (field.getFieldType().equals(DOUBLE_OBJECT) || field.getFieldType().getFullyQualifiedTypeName().equals(double.class.getName()) || field.getFieldType().equals(FLOAT_OBJECT) || field.getFieldType().getFullyQualifiedTypeName().equals(float.class.getName()) || field.getFieldType().equals(BIG_DECIMAL)) {
fieldElement.setAttribute("validationMessageCode", "field_invalid_number");
}
if ("field:input".equals(fieldElement.getTagName()) && null != (annotationMetadata = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), MIN))) {
final AnnotationAttributeValue<?> min = annotationMetadata.getAttribute(VALUE);
if (min != null) {
fieldElement.setAttribute("min", min.getValue().toString());
fieldElement.setAttribute("required", "true");
}
}
if ("field:input".equals(fieldElement.getTagName()) && null != (annotationMetadata = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), MAX)) && !"field:textarea".equals(fieldElement.getTagName())) {
final AnnotationAttributeValue<?> maxA = annotationMetadata.getAttribute(VALUE);
if (maxA != null) {
fieldElement.setAttribute("max", maxA.getValue().toString());
}
}
if ("field:input".equals(fieldElement.getTagName()) && null != (annotationMetadata = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), DECIMAL_MIN)) && !"field:textarea".equals(fieldElement.getTagName())) {
final AnnotationAttributeValue<?> decimalMin = annotationMetadata.getAttribute(VALUE);
if (decimalMin != null) {
fieldElement.setAttribute("decimalMin", decimalMin.getValue().toString());
fieldElement.setAttribute("required", "true");
}
}
if ("field:input".equals(fieldElement.getTagName()) && null != (annotationMetadata = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), DECIMAL_MAX))) {
final AnnotationAttributeValue<?> decimalMax = annotationMetadata.getAttribute(VALUE);
if (decimalMax != null) {
fieldElement.setAttribute("decimalMax", decimalMax.getValue().toString());
}
}
if (null != (annotationMetadata = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), PATTERN))) {
final AnnotationAttributeValue<?> regexp = annotationMetadata.getAttribute(new JavaSymbolName("regexp"));
if (regexp != null) {
fieldElement.setAttribute("validationRegex", regexp.getValue().toString());
}
}
if ("field:input".equals(fieldElement.getTagName()) && null != (annotationMetadata = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), SIZE))) {
final AnnotationAttributeValue<?> max = annotationMetadata.getAttribute(new JavaSymbolName("max"));
if (max != null) {
fieldElement.setAttribute("max", max.getValue().toString());
}
final AnnotationAttributeValue<?> min = annotationMetadata.getAttribute(new JavaSymbolName("min"));
if (min != null) {
fieldElement.setAttribute("min", min.getValue().toString());
fieldElement.setAttribute("required", "true");
}
}
if (null != (annotationMetadata = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), NOT_NULL))) {
final String tagName = fieldElement.getTagName();
if (tagName.endsWith("textarea") || tagName.endsWith("input") || tagName.endsWith("datetime") || tagName.endsWith("textarea") || tagName.endsWith("select") || tagName.endsWith("reference")) {
fieldElement.setAttribute("required", "true");
}
}
if (field.getCustomData().keySet().contains(CustomDataKeys.COLUMN_FIELD)) {
@SuppressWarnings("unchecked") final Map<String, Object> values = (Map<String, Object>) field.getCustomData().get(CustomDataKeys.COLUMN_FIELD);
if (values.keySet().contains("nullable") && (Boolean) values.get("nullable") == false) {
fieldElement.setAttribute("required", "true");
}
}
// Disable form binding for nested fields (mainly PKs)
if (field.getFieldName().getSymbolName().contains(".")) {
fieldElement.setAttribute("disableFormBinding", "true");
}
}
Aggregations