Search in sources :

Example 26 with AnnotationMetadata

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));
            }
        }
    }
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 27 with AnnotationMetadata

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);
}
Also used : JavaType(org.springframework.roo.model.JavaType) RooJavaType(org.springframework.roo.model.RooJavaType) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 28 with AnnotationMetadata

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);
}
Also used : ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) RelationInfo(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo) TreeSet(java.util.TreeSet) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)

Example 29 with AnnotationMetadata

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();
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) Jsr303JavaType(org.springframework.roo.model.Jsr303JavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JavaType(org.springframework.roo.model.JavaType) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 30 with AnnotationMetadata

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);
}
Also used : AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Aggregations

AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)109 JavaType (org.springframework.roo.model.JavaType)59 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)52 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)47 ArrayList (java.util.ArrayList)40 RooJavaType (org.springframework.roo.model.RooJavaType)34 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)30 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)24 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)21 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)20 List (java.util.List)19 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)19 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)18 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)17 NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)16 SpringJavaType (org.springframework.roo.model.SpringJavaType)15 JdkJavaType (org.springframework.roo.model.JdkJavaType)12 JpaJavaType (org.springframework.roo.model.JpaJavaType)12 ArrayAttributeValue (org.springframework.roo.classpath.details.annotations.ArrayAttributeValue)11 LinkedHashMap (java.util.LinkedHashMap)10