Search in sources :

Example 26 with ClassOrInterfaceTypeDetailsBuilder

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

the class ServiceOperationsImpl method createServiceImplementation.

/**
 * Method that creates the service implementation
 *
 * @param interfaceType
 * @param implType
 * @param domainType
 */
private void createServiceImplementation(final JavaType interfaceType, JavaType implType, ClassOrInterfaceTypeDetails repository, JavaType domainType) {
    Validate.notNull(interfaceType, "ERROR: Interface should be provided to be able to generate its implementation");
    Validate.notNull(interfaceType.getModule(), "ERROR: Interface module is required");
    Validate.notNull(domainType, "ERROR: Domain type required to generate service");
    // Generating implementation JavaType if needed
    if (implType == null) {
        implType = new JavaType(String.format("%sImpl", interfaceType.getFullyQualifiedTypeName()), interfaceType.getModule());
    }
    Validate.notNull(implType.getModule(), "ERROR: Implementation module is required");
    // Checks if new service interface already exists.
    final String implIdentifier = pathResolver.getCanonicalPath(implType.getModule(), Path.SRC_MAIN_JAVA, implType);
    if (fileManager.exists(implIdentifier)) {
        // Type already exists - nothing to do
        return;
    }
    // Generating @RooServiceImpl annotation
    final AnnotationMetadataBuilder implAnnotationMetadata = new AnnotationMetadataBuilder(ROO_SERVICE_IMPL);
    implAnnotationMetadata.addAttribute(new ClassAttributeValue(new JavaSymbolName("service"), interfaceType));
    // Creating class builder
    final String implMid = PhysicalTypeIdentifier.createIdentifier(implType, pathResolver.getPath(implIdentifier));
    final ClassOrInterfaceTypeDetailsBuilder implTypeBuilder = new ClassOrInterfaceTypeDetailsBuilder(implMid, PUBLIC, implType, PhysicalTypeCategory.CLASS);
    // Adding @RooService annotation to current interface
    implTypeBuilder.addAnnotation(implAnnotationMetadata.build());
    // Adding implements
    implTypeBuilder.addImplementsType(interfaceType);
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(implTypeBuilder.build().getType(), pathResolver.getPath(implType.getModule(), Path.SRC_MAIN_JAVA));
    // Write service implementation on disk
    typeManagementService.createOrUpdateTypeOnDisk(implTypeBuilder.build());
    // Add dependencies between modules
    projectOperations.addModuleDependency(implType.getModule(), interfaceType.getModule());
    projectOperations.addModuleDependency(implType.getModule(), repository.getName().getModule());
    projectOperations.addModuleDependency(implType.getModule(), domainType.getModule());
    // ROO-3799 Included dependency spring-tx if it's a multimodule project
    if (projectOperations.isMultimoduleProject()) {
        projectOperations.addDependency(implType.getModule(), new Dependency("org.springframework", "spring-tx", "", DependencyType.JAR, DependencyScope.COMPILE));
    }
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) Dependency(org.springframework.roo.project.Dependency) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 27 with ClassOrInterfaceTypeDetailsBuilder

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

the class DbreDatabaseListenerImpl method updateOrDeleteManagedEntity.

private Table updateOrDeleteManagedEntity(final ClassOrInterfaceTypeDetails managedEntity, final Database database) {
    // Update the attributes of the existing JPA-related annotation
    final AnnotationMetadata jpaAnnotation = getJpaAnnotation(managedEntity);
    Validate.validState(jpaAnnotation != null, "@%s not found on existing DBRE-managed entity %s", ROO_JPA_ENTITY.getSimpleTypeName(), managedEntity.getName().getFullyQualifiedTypeName());
    // Find table in database using 'table' and 'schema' attributes from the
    // JPA annotation
    final AnnotationAttributeValue<?> tableAttribute = jpaAnnotation.getAttribute(new JavaSymbolName("table"));
    final String errMsg = "Unable to maintain database-managed entity " + managedEntity.getName().getFullyQualifiedTypeName() + " because its associated table could not be found";
    Validate.notNull(tableAttribute, errMsg);
    final String tableName = (String) tableAttribute.getValue();
    Validate.notBlank(tableName, errMsg);
    final AnnotationAttributeValue<?> schemaAttribute = jpaAnnotation.getAttribute(new JavaSymbolName("schema"));
    final String schemaName = schemaAttribute != null ? (String) schemaAttribute.getValue() : null;
    final Table table = database.getTable(tableName, schemaName);
    if (table == null) {
        // Table is missing and probably has been dropped so delete managed
        // type and its identifier if applicable
        deleteManagedType(managedEntity, "no database table called '" + tableName + "'");
        return null;
    }
    table.setIncludeNonPortableAttributes(database.isIncludeNonPortableAttributes());
    table.setDisableVersionFields(database.isDisableVersionFields());
    table.setDisableGeneratedIdentifiers(database.isDisableGeneratedIdentifiers());
    // Update the @RooJpaEntity attributes
    final AnnotationMetadataBuilder jpaAnnotationBuilder = new AnnotationMetadataBuilder(jpaAnnotation);
    final Set<JavaSymbolName> attributesToDeleteIfPresent = new LinkedHashSet<JavaSymbolName>();
    manageIdentifier(managedEntity.getName(), jpaAnnotationBuilder, attributesToDeleteIfPresent, table);
    // Manage versionField attribute
    final AnnotationAttributeValue<?> versionFieldAttribute = jpaAnnotation.getAttribute(new JavaSymbolName(VERSION_FIELD));
    if (versionFieldAttribute == null) {
        if (hasVersionField(table)) {
            attributesToDeleteIfPresent.add(new JavaSymbolName(VERSION_FIELD));
        } else {
            jpaAnnotationBuilder.addStringAttribute(VERSION_FIELD, "");
        }
    } else {
        final String versionFieldValue = (String) versionFieldAttribute.getValue();
        if (hasVersionField(table) && (StringUtils.isBlank(versionFieldValue) || VERSION.equals(versionFieldValue))) {
            attributesToDeleteIfPresent.add(new JavaSymbolName(VERSION_FIELD));
        }
    }
    final AnnotationAttributeValue<?> sequenceNameFieldAttribute = jpaAnnotation.getAttribute(new JavaSymbolName(SEQUENCE_NAME_FIELD));
    if (sequenceNameFieldAttribute == null) {
        if (!table.isDisableGeneratedIdentifiers()) {
            attributesToDeleteIfPresent.add(new JavaSymbolName(SEQUENCE_NAME_FIELD));
        } else {
            jpaAnnotationBuilder.addStringAttribute(SEQUENCE_NAME_FIELD, "");
        }
    } else {
        final String sequenceNameFieldValue = (String) sequenceNameFieldAttribute.getValue();
        if (!table.isDisableGeneratedIdentifiers() && ("".equals(sequenceNameFieldValue))) {
            attributesToDeleteIfPresent.add(new JavaSymbolName(SEQUENCE_NAME_FIELD));
        }
    }
    // Update the annotation on disk
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(managedEntity);
    cidBuilder.updateTypeAnnotation(jpaAnnotationBuilder.build(), attributesToDeleteIfPresent);
    getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
    return table;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) Table(org.springframework.roo.addon.dbre.addon.model.Table) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 28 with ClassOrInterfaceTypeDetailsBuilder

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

the class DtoOperationsImpl method createProjection.

@Override
public void createProjection(JavaType entity, JavaType name, String fields, String suffix, String formatExpression, String formatMessage) {
    Validate.notNull(name, "Use --class to select the name of the Projection.");
    // TODO: Validate fields for excluding entity collection, transient and
    // static fields from operations (already doing when comming from commands).
    // Set focus on projection module
    projectOperations.setModule(projectOperations.getPomFromModuleName(name.getModule()));
    // Add springlets-context dependency
    projectOperations.addDependency(name.getModule(), SPRINGLETS_CONTEXT_DEPENDENCY);
    projectOperations.addProperty("", SPRINGLETS_VERSION_PROPERTY);
    Map<String, FieldMetadata> fieldsToAdd = new LinkedHashMap<String, FieldMetadata>();
    boolean onlyMainEntityFields = true;
    if (fields != null) {
        onlyMainEntityFields = false;
        // Check that id field has been included. If not, include it.
        fields = checkAndAddIdField(entity, fields);
        fieldsToAdd = buildFieldsFromString(fields, entity);
    } else {
        List<FieldMetadata> allFields = memberDetailsScanner.getMemberDetails(this.getClass().getName(), typeLocationService.getTypeDetails(entity)).getFields();
        for (FieldMetadata field : allFields) {
            // Add only valid fields
            if (isFieldValidForProjection(field)) {
                fieldsToAdd.put(field.getFieldName().getSymbolName(), field);
            }
        }
    }
    // Create projection
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, LogicalPath.getInstance(Path.SRC_MAIN_JAVA, name.getModule()));
    final ClassOrInterfaceTypeDetailsBuilder projectionBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, name, PhysicalTypeCategory.CLASS);
    // Add fields to projection
    addFieldsToProjection(projectionBuilder, fieldsToAdd);
    // @RooJavaBean, @RooToString and @RooEquals
    AnnotationMetadataBuilder rooJavaBeanAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JAVA_BEAN);
    rooJavaBeanAnnotation.addBooleanAttribute("settersByDefault", false);
    projectionBuilder.addAnnotation(rooJavaBeanAnnotation);
    AnnotationMetadataBuilder rooToStringAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_TO_STRING);
    AnnotationMetadataBuilder rooEqualsAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_EQUALS);
    projectionBuilder.addAnnotation(rooToStringAnnotation);
    projectionBuilder.addAnnotation(rooEqualsAnnotation);
    // Add @RooEntityProjection
    AnnotationMetadataBuilder projectionAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_ENTITY_PROJECTION);
    projectionAnnotation.addClassAttribute("entity", entity);
    List<StringAttributeValue> fieldNames = new ArrayList<StringAttributeValue>();
    if (onlyMainEntityFields) {
        // Should add all entity fields
        for (FieldMetadata field : fieldsToAdd.values()) {
            fieldNames.add(new StringAttributeValue(new JavaSymbolName("fields"), field.getFieldName().getSymbolName()));
        }
    } else {
        // --fields option has been completed and validated, so build annotation 'fields'
        // param from selected fields
        String[] fieldsFromCommand = StringUtils.split(fields, ",");
        for (int i = 0; i < fieldsFromCommand.length; i++) {
            fieldNames.add(new StringAttributeValue(new JavaSymbolName("fields"), fieldsFromCommand[i]));
        }
    }
    projectionAnnotation.addAttribute(new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("fields"), fieldNames));
    // Check for each attribute individually
    if (StringUtils.isNotBlank(formatExpression)) {
        projectionAnnotation.addStringAttribute("formatExpression", formatExpression);
    }
    if (StringUtils.isNotBlank(formatMessage)) {
        projectionAnnotation.addStringAttribute("formatMessage", formatMessage);
    }
    projectionBuilder.addAnnotation(projectionAnnotation);
    // Build and save changes to disk
    typeManagementService.createOrUpdateTypeOnDisk(projectionBuilder.build());
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 29 with ClassOrInterfaceTypeDetailsBuilder

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

the class DtoOperationsImpl method createDto.

@Override
public void createDto(JavaType name, boolean immutable, boolean utilityMethods, boolean serializable, String formatExpression, String formatMessage) {
    Validate.isTrue(!JdkJavaType.isPartOfJavaLang(name.getSimpleTypeName()), "Class name '%s' is part of java.lang", name.getSimpleTypeName());
    // Set focus on dto module
    projectOperations.setModule(projectOperations.getPomFromModuleName(name.getModule()));
    // Add springlets-context dependency
    projectOperations.addDependency(name.getModule(), SPRINGLETS_CONTEXT_DEPENDENCY);
    projectOperations.addProperty("", SPRINGLETS_VERSION_PROPERTY);
    // Create file
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, LogicalPath.getInstance(Path.SRC_MAIN_JAVA, name.getModule()));
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, name, PhysicalTypeCategory.CLASS);
    // Add @RooDTO and @RooJavaBean
    AnnotationMetadataBuilder rooDtoAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_DTO);
    AnnotationMetadataBuilder rooJavaBeanAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JAVA_BEAN);
    if (immutable) {
        rooDtoAnnotation.addBooleanAttribute("immutable", immutable);
        rooJavaBeanAnnotation.addBooleanAttribute("settersByDefault", false);
    }
    // Check for each attribute individually
    if (StringUtils.isNotBlank(formatExpression)) {
        rooDtoAnnotation.addStringAttribute("formatExpression", formatExpression);
    }
    if (StringUtils.isNotBlank(formatMessage)) {
        rooDtoAnnotation.addStringAttribute("formatMessage", formatMessage);
    }
    cidBuilder.addAnnotation(rooDtoAnnotation);
    cidBuilder.addAnnotation(rooJavaBeanAnnotation);
    // Add utility annotations if necessary
    if (utilityMethods) {
        AnnotationMetadataBuilder rooToStringAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_TO_STRING);
        AnnotationMetadataBuilder rooEqualsAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_EQUALS);
        cidBuilder.addAnnotation(rooToStringAnnotation);
        cidBuilder.addAnnotation(rooEqualsAnnotation);
    }
    // Add @RooSerializable if necessary
    if (serializable) {
        AnnotationMetadataBuilder rooSerializableAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_SERIALIZABLE);
        cidBuilder.addAnnotation(rooSerializableAnnotation);
    }
    typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
}
Also used : ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 30 with ClassOrInterfaceTypeDetailsBuilder

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

the class CacheOperationsImpl method setupCache.

@Override
public void setupCache(CacheProvider provider, String profile) {
    // Add spring-boot-starter-cache dependency
    List<Pom> modules = (List<Pom>) getTypeLocationService().getModules(ModuleFeatureName.APPLICATION);
    if (modules.size() == 0) {
        throw new RuntimeException(String.format("ERROR: Not found a module with %s feature", ModuleFeatureName.APPLICATION));
    }
    // Do the setup for each @SpringBootApplication module
    for (Pom module : modules) {
        addSpringCacheDependency(module);
    }
    // Add @EnableCache annotation to each application file
    Set<ClassOrInterfaceTypeDetails> applicationClasses = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(SpringJavaType.SPRING_BOOT_APPLICATION);
    for (ClassOrInterfaceTypeDetails applicationClass : applicationClasses) {
        if (applicationClass.getAnnotation(SpringJavaType.ENABLE_CACHING) == null) {
            ClassOrInterfaceTypeDetailsBuilder builder = new ClassOrInterfaceTypeDetailsBuilder(applicationClass);
            builder.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.ENABLE_CACHING));
            getTypeManagementService().createOrUpdateTypeOnDisk(builder.build());
        }
    }
    if (provider != null) {
        // Do setup of cache provider
        if (!provider.isInstalled()) {
            provider.setup(profile);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) Pom(org.springframework.roo.project.maven.Pom)

Aggregations

ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)68 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)54 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)43 JavaType (org.springframework.roo.model.JavaType)30 ArrayList (java.util.ArrayList)29 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)29 RooJavaType (org.springframework.roo.model.RooJavaType)26 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)19 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)14 SpringJavaType (org.springframework.roo.model.SpringJavaType)12 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)10 ClassAttributeValue (org.springframework.roo.classpath.details.annotations.ClassAttributeValue)10 JpaJavaType (org.springframework.roo.model.JpaJavaType)10 Dependency (org.springframework.roo.project.Dependency)10 LogicalPath (org.springframework.roo.project.LogicalPath)10 Pom (org.springframework.roo.project.maven.Pom)9 List (java.util.List)8 NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)7 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)7 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)7