Search in sources :

Example 51 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.

the class WsClientsMetadata method getLoggerField.

/**
 * This method obtains the LOGGER field
 *
 * @return FieldMetadataBuilder that contians information about
 * the LOGGER field
 */
public FieldMetadataBuilder getLoggerField() {
    if (loggerField != null) {
        return loggerField;
    }
    // Create the field
    FieldMetadataBuilder loggerField = new FieldMetadataBuilder(getId(), Modifier.PRIVATE + Modifier.STATIC + Modifier.FINAL, new JavaSymbolName("LOGGER"), new JavaType("org.slf4j.Logger"), String.format("%s.getLogger(%s.class);", getNameOfJavaType(new JavaType("org.slf4j.LoggerFactory")), getNameOfJavaType(this.governor)));
    this.loggerField = loggerField;
    return loggerField;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) SpringJavaType(org.springframework.roo.model.SpringJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 52 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.

the class WsEndpointsMetadata method getServiceField.

/**
 * This method obtains the service field that will be used in some different
 * methods.
 *
 * @param service JavaType with the information about the service related with the new Service field
 *
 * @return FieldMetadata with the necessary information about the service
 */
private FieldMetadata getServiceField(JavaType service) {
    // Check if already exists
    if (serviceFields.get(service) != null) {
        return serviceFields.get(service);
    }
    // Create the field
    FieldMetadataBuilder serviceField = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, new JavaSymbolName(StringUtils.uncapitalize(service.getSimpleTypeName())), service, null);
    serviceField.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.AUTOWIRED));
    serviceFields.put(service, serviceField.build());
    return serviceField.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 53 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.

the class MailOperationsImpl method installReceiveEmailSupport.

@Override
public void installReceiveEmailSupport(String host, String port, String protocol, String username, String password, Boolean starttls, String jndiName, String profile, Pom module, JavaType service, boolean force) {
    // Include springlets-boot-starter-mail in module
    getProjectOperations().addDependency(module.getModuleName(), DEPENDENCY_SPRINGLETS_STARTER_MAIL);
    // Include property version
    getProjectOperations().addProperty("", PROPERTY_SPRINGLETS_VERSION);
    // check if exists any property
    if (getApplicationConfigService().existsSpringConfigFile(module.getModuleName(), profile) && areDefinedReceiveEmailProperties(module.getModuleName(), profile) && !force) {
        // Send error message to user. He needs to set --force parameter
        String profileStr = "profile " + profile;
        if (profile == null) {
            profileStr = "default profile";
        }
        String moduleStr = " and module " + module.getModuleName();
        if (StringUtils.isEmpty(module.getModuleName())) {
            moduleStr = "";
        }
        LOGGER.log(Level.INFO, String.format("There are defined the mail properties to %s" + "%s. Using this command with '--force' " + "will overwrite the current values.", profileStr, moduleStr));
    } else {
        Map<String, String> propertiesFormattedToInsert = getReceiveEmailPropertiesFormattedToInsert(host, port, protocol, username, password, starttls, jndiName);
        // Set properties in file
        getApplicationConfigService().addProperties(module.getModuleName(), propertiesFormattedToInsert, profile, force);
    }
    if (service != null) {
        // Add dependency springlets-mail to the module of the selected Service
        if (!service.getModule().equals(StringUtils.EMPTY)) {
            getProjectOperations().addDependency(service.getModule(), DEPENDENCY_SPRINGLETS_MAIL);
        }
        // Add MailReceiverService to service
        final ClassOrInterfaceTypeDetails serviceTypeDetails = getTypeLocationService().getTypeDetails(service);
        Validate.isTrue(serviceTypeDetails != null, "Cannot locate source for '%s'", service.getFullyQualifiedTypeName());
        final String declaredByMetadataId = serviceTypeDetails.getDeclaredByMetadataId();
        final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(serviceTypeDetails);
        // Create the field
        cidBuilder.addField(new FieldMetadataBuilder(declaredByMetadataId, PRIVATE, Arrays.asList(new AnnotationMetadataBuilder(AUTOWIRED)), new JavaSymbolName("mailReceiver"), SpringletsJavaType.SPRINGLETS_MAIL_RECEIVER_SERVICE));
        getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
    }
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 54 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.

the class MailOperationsImpl method installSendEmailSupport.

@Override
public void installSendEmailSupport(String host, String port, String protocol, String username, String password, Boolean starttls, String jndiName, String profile, Pom module, JavaType service, boolean force) {
    // Include spring-boot-starter-mail in module
    getProjectOperations().addDependency(module.getModuleName(), DEPENDENCY_SPRING_BOOT_STARTER_MAIL);
    // check if exists any property
    if (getApplicationConfigService().existsSpringConfigFile(module.getModuleName(), profile) && areDefinedSendEmailProperties(module.getModuleName(), profile) && !force) {
        // Send error message to user. He needs to set --force parameter
        String profileStr = "profile " + profile;
        if (profile == null) {
            profileStr = "default profile";
        }
        String moduleStr = " and module " + module.getModuleName();
        if (StringUtils.isEmpty(module.getModuleName())) {
            moduleStr = "";
        }
        LOGGER.log(Level.INFO, String.format("There are defined the mail properties to %s" + "%s. Using this command with '--force' " + "will overwrite the current values.", profileStr, moduleStr));
    } else {
        Map<String, String> propertiesFormattedToInsert = getSendEmailPropertiesFormattedToInsert(host, port, protocol, username, password, starttls, jndiName);
        // Set properties in file
        getApplicationConfigService().addProperties(module.getModuleName(), propertiesFormattedToInsert, profile, force);
    }
    if (service != null) {
        // Add dependency spring-context-support to the module of the selected Service
        getProjectOperations().addDependency(service.getModule(), DEPENDENCY_SPRING_CONTEXT_SUPPORT);
        // Add JavaMailSender to service
        final ClassOrInterfaceTypeDetails serviceTypeDetails = getTypeLocationService().getTypeDetails(service);
        Validate.isTrue(serviceTypeDetails != null, "Cannot locate source for '%s'", service.getFullyQualifiedTypeName());
        final String declaredByMetadataId = serviceTypeDetails.getDeclaredByMetadataId();
        final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(serviceTypeDetails);
        // Create the field
        cidBuilder.addField(new FieldMetadataBuilder(declaredByMetadataId, PRIVATE, Arrays.asList(new AnnotationMetadataBuilder(AUTOWIRED)), new JavaSymbolName("mailSender"), SpringJavaType.JAVA_MAIL_SENDER));
        getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
    }
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 55 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.

the class DtoOperationsImpl method addFieldsToProjection.

/**
 * Removes persistence annotations of provided fields and adds them to a
 * ClassOrInterfaceTypeDetailsBuilder representing a Projection in construction.
 * Also adds final modifier to fields if required.
 *
 * @param projectionBuilder the ClassOrInterfaceTypeDetailsBuilder for building the
 *            Projection class.
 * @param fieldsToAdd the List<FieldMetadata> to add.
 */
private void addFieldsToProjection(ClassOrInterfaceTypeDetailsBuilder projectionBuilder, Map<String, FieldMetadata> fieldsToAdd) {
    Iterator<Entry<String, FieldMetadata>> iterator = fieldsToAdd.entrySet().iterator();
    while (iterator.hasNext()) {
        Entry<String, FieldMetadata> entry = iterator.next();
        FieldMetadata field = entry.getValue();
        // List and Set types require special management
        FieldMetadataBuilder fieldBuilder = null;
        FieldDetails fieldDetails = null;
        if (field.getFieldType().getFullyQualifiedTypeName().equals("java.util.Set")) {
            JavaType fieldType = field.getFieldType().getParameters().get(0);
            fieldDetails = new SetField(projectionBuilder.getDeclaredByMetadataId(), new JavaType(JdkJavaType.SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), field.getFieldName(), fieldType, null, null, true);
            fieldBuilder = new FieldMetadataBuilder(fieldDetails);
            fieldBuilder.setModifier(field.getModifier());
            fieldBuilder.setAnnotations(field.getAnnotations());
        } else if (field.getFieldType().getFullyQualifiedTypeName().equals("java.util.List")) {
            JavaType fieldType = field.getFieldType().getParameters().get(0);
            fieldDetails = new ListField(projectionBuilder.getDeclaredByMetadataId(), new JavaType(JdkJavaType.LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), field.getFieldName(), fieldType, null, null, true);
            fieldBuilder = new FieldMetadataBuilder(fieldDetails);
            fieldBuilder.setModifier(field.getModifier());
            fieldBuilder.setAnnotations(field.getAnnotations());
        } else {
            // Can't just copy FieldMetadata because field.declaredByMetadataId will be the same
            fieldBuilder = new FieldMetadataBuilder(projectionBuilder.getDeclaredByMetadataId(), field);
        }
        // Add dependency between modules
        typeLocationService.addModuleDependency(projectionBuilder.getName().getModule(), field.getFieldType());
        // Set new fieldName
        fieldBuilder.setFieldName(new JavaSymbolName(entry.getKey()));
        // If it is a CollectionField it needs an initializer
        String initializer = null;
        if (fieldDetails instanceof CollectionField) {
            final CollectionField collectionField = (CollectionField) fieldDetails;
            initializer = "new " + collectionField.getInitializer() + "()";
        } else if (fieldDetails instanceof DateField && fieldDetails.getFieldName().getSymbolName().equals("created")) {
            initializer = "new Date()";
        }
        fieldBuilder.setFieldInitializer(initializer);
        // Remove persistence annotations
        List<AnnotationMetadata> annotations = field.getAnnotations();
        for (AnnotationMetadata annotation : annotations) {
            if (annotation.getAnnotationType().getFullyQualifiedTypeName().contains("javax.persistence")) {
                fieldBuilder.removeAnnotation(annotation.getAnnotationType());
            } else if (annotation.getAnnotationType().getFullyQualifiedTypeName().startsWith("javax.validation")) {
                // Add validation dependency
                projectOperations.addDependency(projectionBuilder.getName().getModule(), new Dependency("javax.validation", "validation-api", null));
            } else if (annotation.getAnnotationType().equals(RooJavaType.ROO_JPA_RELATION)) {
                fieldBuilder.removeAnnotation(annotation.getAnnotationType());
            }
        }
        fieldBuilder.setModifier(Modifier.PRIVATE);
        // Build field
        FieldMetadata projectionField = fieldBuilder.build();
        // Add field to DTO
        projectionBuilder.addField(projectionField);
    }
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) FieldDetails(org.springframework.roo.classpath.details.FieldDetails) ListField(org.springframework.roo.classpath.operations.jsr303.ListField) Dependency(org.springframework.roo.project.Dependency) SetField(org.springframework.roo.classpath.operations.jsr303.SetField) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder) Entry(java.util.Map.Entry) JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) DateField(org.springframework.roo.classpath.operations.jsr303.DateField) CollectionField(org.springframework.roo.classpath.operations.jsr303.CollectionField)

Aggregations

FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)66 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)48 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)40 ArrayList (java.util.ArrayList)21 JavaType (org.springframework.roo.model.JavaType)19 JdkJavaType (org.springframework.roo.model.JdkJavaType)10 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)8 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)7 FieldDetails (org.springframework.roo.classpath.details.FieldDetails)7 ForeignKey (org.springframework.roo.addon.dbre.addon.model.ForeignKey)5 CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)5 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)5 LinkedHashMap (java.util.LinkedHashMap)4 Reference (org.springframework.roo.addon.dbre.addon.model.Reference)4 Table (org.springframework.roo.addon.dbre.addon.model.Table)4 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)4 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)4 CollectionField (org.springframework.roo.classpath.operations.jsr303.CollectionField)4 DateField (org.springframework.roo.classpath.operations.jsr303.DateField)4 EnumDetails (org.springframework.roo.model.EnumDetails)4