use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class ThymeleafMetadata method buildPopulateFormatBody.
/**
* Build method body which populate in model all required formats for a form
* based on dateTimeField of a type.
*
* Also populate current locale in model.
*
* @param bodyBuilder
* @param dateTimeFields
* dateTime fields (optional could be empty or null)
*/
private void buildPopulateFormatBody(InvocableMemberBodyBuilder bodyBuilder, List<FieldMetadata> dateTimeFields) {
// Always save locale
bodyBuilder.appendFormalLine("model.addAttribute(\"application_locale\", %s.getLocale().getLanguage());", getNameOfJavaType(SpringJavaType.LOCALE_CONTEXT_HOLDER));
if (dateTimeFields == null || dateTimeFields.isEmpty()) {
// All done;
return;
}
// Getting all enum types from provided entity
for (FieldMetadata field : dateTimeFields) {
// Getting annotation format
AnnotationMetadata dateTimeFormatAnnotation = field.getAnnotation(SpringJavaType.DATE_TIME_FORMAT);
if (dateTimeFormatAnnotation != null && (dateTimeFormatAnnotation.getAttribute("style") != null || dateTimeFormatAnnotation.getAttribute("pattern") != null)) {
AnnotationAttributeValue<String> formatAttr = dateTimeFormatAnnotation.getAttribute("style");
if (formatAttr != null) {
String format = formatAttr.getValue();
// model.addAttribute("field_date_format",
// DateTimeFormat.patternForStyle("M-",
// LocaleContextHolder.getLocale()));
bodyBuilder.appendFormalLine("model.addAttribute(\"%s_date_format\", %s.patternForStyle(\"%s\", %s.getLocale()));", field.getFieldName(), getNameOfJavaType(JODA_DATETIME_FORMAT_JAVA_TYPE), format, getNameOfJavaType(SpringJavaType.LOCALE_CONTEXT_HOLDER));
} else {
formatAttr = dateTimeFormatAnnotation.getAttribute("pattern");
String format = formatAttr.getValue();
// model.addAttribute("field_date_format", "pattern");
bodyBuilder.appendFormalLine(String.format("model.addAttribute(\"%s_date_format\", \"%s\");", field.getFieldName(), format));
}
}
}
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class JsonControllerIntegrationTestMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
final JsonControllerIntegrationTestAnnotationValues annotationValues = new JsonControllerIntegrationTestAnnotationValues(governorPhysicalTypeMetadata);
// Get JSON controller target class
final JavaType jsonController = annotationValues.getTargetClass();
// Get the controller managed entity
ClassOrInterfaceTypeDetails controllerCid = getTypeLocationService().getTypeDetails(jsonController);
AnnotationMetadata rooControllerAnnotation = controllerCid.getAnnotation(RooJavaType.ROO_CONTROLLER);
Validate.notNull(rooControllerAnnotation, "The target class must be annotated with @RooController.");
Validate.notNull(rooControllerAnnotation.getAttribute("entity"), "The @RooController must have an 'entity' attribute, targeting managed entity.");
final JavaType managedEntity = (JavaType) rooControllerAnnotation.getAttribute("entity").getValue();
// Get the entity factory of managed entity
final JavaType entityFactory = getJpaEntityFactoryLocator().getFirstJpaEntityFactoryForEntity(managedEntity);
// Get the service related to managed entity
final JavaType entityService = getServiceLocator().getFirstService(managedEntity).getType();
return new JsonControllerIntegrationTestMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, jsonController, managedEntity, entityFactory, entityService);
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class ThymeleafControllerIntegrationTestMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
final ThymeleafControllerIntegrationTestAnnotationValues annotationValues = new ThymeleafControllerIntegrationTestAnnotationValues(governorPhysicalTypeMetadata);
// Get JSON controller target class
final JavaType jsonController = annotationValues.getTargetClass();
// Get the controller managed entity
ClassOrInterfaceTypeDetails controllerCid = getTypeLocationService().getTypeDetails(jsonController);
AnnotationMetadata rooControllerAnnotation = controllerCid.getAnnotation(RooJavaType.ROO_CONTROLLER);
if (rooControllerAnnotation == null) {
LOGGER.warning(String.format("Ignoring %s of %s as missing %s annotation in %s", ROO_THYMELEAF_CONTROLLER_INTEGRATION_TEST.getSimpleTypeName(), governorPhysicalTypeMetadata.getType().getFullyQualifiedTypeName(), RooJavaType.ROO_CONTROLLER.getSimpleTypeName(), jsonController.getSimpleTypeName()));
return null;
}
Validate.notNull(rooControllerAnnotation.getAttribute("entity"), "The @RooController must have an 'entity' attribute, targeting managed entity.");
final JavaType managedEntity = (JavaType) rooControllerAnnotation.getAttribute("entity").getValue();
// Get the entity metadata
String jpaEntityIdentifier = JpaEntityMetadata.createIdentifier(getTypeLocationService().getTypeDetails(managedEntity));
JpaEntityMetadata entityMetadata = getMetadataService().get(jpaEntityIdentifier);
if (entityMetadata == null) {
return null;
}
// Get child related entities
Collection<RelationInfo> relatioInfos = entityMetadata.getRelationInfos().values();
Set<JavaType> relatedEntities = new TreeSet<JavaType>();
// First, add managed entity
relatedEntities.add(managedEntity);
for (RelationInfo relationInfo : relatioInfos) {
if (relationInfo.cardinality == Cardinality.ONE_TO_ONE && relationInfo.type == JpaRelationType.COMPOSITION) {
// OneToOne composition is managed by owner's service
continue;
}
relatedEntities.add(relationInfo.childType);
}
// Get the entity factory of managed entity
final JavaType entityFactory = getJpaEntityFactoryLocator().getFirstJpaEntityFactoryForEntity(managedEntity);
// Get the services related to managed entity
List<JavaType> relatedServices = new ArrayList<JavaType>();
for (JavaType entity : relatedEntities) {
final ClassOrInterfaceTypeDetails serviceDetails = getServiceLocator().getFirstService(entity);
if (serviceDetails == null) {
LOGGER.warning(String.format("Couldn't find service of related entity %s in %s", entity.getSimpleTypeName(), this.getClass().getName()));
} else {
if (!relatedServices.contains(serviceDetails.getType())) {
relatedServices.add(serviceDetails.getType());
}
}
}
return new ThymeleafControllerIntegrationTestMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, jsonController, managedEntity, entityFactory, relatedServices);
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata 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.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class WebMvcConfigurationMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
AnnotationMetadata annotation = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails().getAnnotation(RooJavaType.ROO_WEB_MVC_CONFIGURATION);
// Getting language attribute
AnnotationAttributeValue<String> defaultLanguageAttr = annotation.getAttribute("defaultLanguage");
String defaultLanguage = "";
if (defaultLanguageAttr != null) {
defaultLanguage = defaultLanguageAttr.getValue();
}
return new WebMvcConfigurationMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, defaultLanguage);
}
Aggregations