Search in sources :

Example 86 with AnnotationMetadataBuilder

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

the class I18nOperationsImpl method installLanguage.

@Override
public void installLanguage(final I18n language, final boolean useAsDefault, final Pom module) {
    // Check if provided module match with application modules features
    Validate.isTrue(getTypeLocationService().hasModuleFeature(module, ModuleFeatureName.APPLICATION), "ERROR: Provided module doesn't match with application modules features. " + "Execute this operation again and provide a valid application module.");
    Validate.notNull(language, "ERROR: You should provide a valid language code.");
    if (language.getLocale() == null) {
        LOGGER.warning("ERROR: Provided language is not valid.");
        return;
    }
    final LogicalPath resourcesPath = LogicalPath.getInstance(Path.SRC_MAIN_RESOURCES, module.getModuleName());
    final String targetDirectory = getPathResolver().getIdentifier(resourcesPath, "");
    // Getting message.properties file
    String messageBundle = "";
    if (language.getLocale().equals(Locale.ENGLISH)) {
        messageBundle = targetDirectory + "messages.properties";
    } else {
        messageBundle = targetDirectory.concat("messages_").concat(language.getLocale().getLanguage().concat(".properties"));
    }
    if (!getFileManager().exists(messageBundle)) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = language.getMessageBundle();
            outputStream = getFileManager().createFile(messageBundle).getOutputStream();
            IOUtils.copy(inputStream, outputStream);
        } catch (final Exception e) {
            throw new IllegalStateException("Encountered an error during copying of message bundle MVC JSP addon.", e);
        } finally {
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(outputStream);
        }
    }
    // Install flag
    final String flagGraphic = targetDirectory.concat("static/public/img/").concat(language.getLocale().getLanguage()).concat(".png");
    if (!getFileManager().exists(flagGraphic)) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = language.getFlagGraphic();
            outputStream = getFileManager().createFile(flagGraphic).getOutputStream();
            IOUtils.copy(inputStream, outputStream);
        } catch (final Exception e) {
            throw new IllegalStateException("Encountered an error during copying of flag graphic for MVC JSP addon.", e);
        } finally {
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(outputStream);
        }
    }
    // attribute
    if (useAsDefault) {
        // Obtain all existing configuration classes annotated with
        // @RooWebMvcConfiguration
        Set<ClassOrInterfaceTypeDetails> configurationClasses = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_WEB_MVC_CONFIGURATION);
        for (ClassOrInterfaceTypeDetails configurationClass : configurationClasses) {
            // If configuration class is located in the provided module
            if (configurationClass.getType().getModule().equals(module.getModuleName())) {
                ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(configurationClass);
                AnnotationMetadataBuilder annotation = cidBuilder.getDeclaredTypeAnnotation(RooJavaType.ROO_WEB_MVC_CONFIGURATION);
                annotation.addStringAttribute("defaultLanguage", language.getLocale().getLanguage());
                // Update configuration class
                getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
            }
        }
        LOGGER.log(Level.INFO, String.format("INFO: Default language of your project has been changed to %s.", language.getLanguage()));
    }
    // Get all controllers and update its message bundles
    Set<ClassOrInterfaceTypeDetails> controllers = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_CONTROLLER);
    for (ClassOrInterfaceTypeDetails controller : controllers) {
        getMetadataService().evictAndGet(ControllerMetadata.createIdentifier(controller));
    }
    // Add application property
    getApplicationConfigService().addProperty(module.getModuleName(), "spring.messages.fallback-to-system-locale", "false", "", true);
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) LogicalPath(org.springframework.roo.project.LogicalPath) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 87 with AnnotationMetadataBuilder

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

the class RepositoryJpaOperationsImpl method addRepositoryCustom.

/**
 * Method that generates RepositoryCustom interface and its implementation
 * for an specific entity
 *
 * @param domainType
 * @param repositoryType
 * @param repositoryPackage
 * @param defaultReturnType
 *
 * @return JavaType with new RepositoryCustom interface.
 */
private JavaType addRepositoryCustom(JavaType domainType, JavaType repositoryType, JavaPackage repositoryPackage) {
    // Getting RepositoryCustom interface JavaTYpe
    JavaType interfaceType = new JavaType(repositoryPackage.getFullyQualifiedPackageName().concat(".").concat(repositoryType.getSimpleTypeName()).concat("Custom"), repositoryType.getModule());
    // Check if new interface exists yet
    final String interfaceIdentifier = getPathResolver().getCanonicalPath(interfaceType.getModule(), Path.SRC_MAIN_JAVA, interfaceType);
    if (getFileManager().exists(interfaceIdentifier)) {
        // Type already exists - return
        return interfaceType;
    }
    final String interfaceMdId = PhysicalTypeIdentifier.createIdentifier(interfaceType, getPathResolver().getPath(interfaceIdentifier));
    final ClassOrInterfaceTypeDetailsBuilder interfaceBuilder = new ClassOrInterfaceTypeDetailsBuilder(interfaceMdId, Modifier.PUBLIC, interfaceType, PhysicalTypeCategory.INTERFACE);
    // Generates @RooJpaRepositoryCustom annotation with referenced entity value
    final AnnotationMetadataBuilder repositoryCustomAnnotationMetadata = new AnnotationMetadataBuilder(RooJavaType.ROO_REPOSITORY_JPA_CUSTOM);
    repositoryCustomAnnotationMetadata.addAttribute(new ClassAttributeValue(new JavaSymbolName("entity"), domainType));
    interfaceBuilder.addAnnotation(repositoryCustomAnnotationMetadata);
    // Save RepositoryCustom interface and its implementation on disk
    getTypeManagementService().createOrUpdateTypeOnDisk(interfaceBuilder.build());
    generateRepositoryCustomImpl(interfaceType, repositoryType, domainType);
    return interfaceType;
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) 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) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 88 with AnnotationMetadataBuilder

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

the class RepositoryJpaIntegrationTestMetadata method getDodField.

/**
 * Builds and returns a `private` data-on-demand field.
 *
 * @return {@link FieldMetadataBuilder}
 */
private FieldMetadataBuilder getDodField() {
    FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(this.getId(), Modifier.PRIVATE, DATA_ON_DEMAND_FIELD_NAME, this.annotationValues.getDodClass(), null);
    // Add @Autowired
    fieldBuilder.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.AUTOWIRED));
    return fieldBuilder;
}
Also used : AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 89 with AnnotationMetadataBuilder

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

the class RepositoryJpaIntegrationTestMetadata method getExpectedExceptionField.

/**
 * Builds and returns a <code>public</code> ExpectedException field.
 *
 * @return {@link FieldMetadataBuilder}
 */
private FieldMetadataBuilder getExpectedExceptionField() {
    FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(this.getId(), Modifier.PUBLIC, EXPECTED_EXCEPTION_FIELD_NAME, EXPECTED_EXCEPTION, String.format("%s.none()", getNameOfJavaType(EXPECTED_EXCEPTION)));
    // Add @Rule
    fieldBuilder.addAnnotation(new AnnotationMetadataBuilder(RULE));
    return fieldBuilder;
}
Also used : AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 90 with AnnotationMetadataBuilder

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

the class RepositoryJpaIntegrationTestMetadata method getFindAllCustomNotFilteredPagedTestMethod.

/**
 * Builds a method to test the find all custom method not filtered and paged.
 *
 * @return {@link MethodMetadata}
 */
private MethodMetadata getFindAllCustomNotFilteredPagedTestMethod() {
    JavaSymbolName methodName = new JavaSymbolName(String.format("findAllCustomNotFilteredPagedShouldReturnA%sPage", this.entityPlural));
    // Check if method exists on governor
    MethodMetadata method = getGovernorMethod(methodName);
    if (method != null) {
        return method;
    }
    // Build method body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Exercise
    bodyBuilder.appendFormalLine("// Exercise");
    // Page<Pet> all = repository.findAll((GlobalSearch) null, new PageRequest(0, 3));
    bodyBuilder.appendFormalLine("%s<%s> all = %s().findAll((%s) null, new %s(0, 3));", getNameOfJavaType(SpringJavaType.PAGE), getNameOfJavaType(this.defaultReturnType), getAccessorMethod(this.repositoryField).getMethodName(), getNameOfJavaType(SpringletsJavaType.SPRINGLETS_GLOBAL_SEARCH), getNameOfJavaType(SpringJavaType.PAGE_REQUEST));
    // Verify
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("// Verify");
    // assertThat(all.getNumberOfElements())
    bodyBuilder.appendFormalLine("%s(all.getNumberOfElements())", getNameOfJavaType(ASSERT_THAT));
    // .as("Check result number is not greater than the page size").isLessThanOrEqualTo(3);
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(".as(\"Check result number is not greater than the page size\").isLessThanOrEqualTo(3);");
    bodyBuilder.indentRemove();
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, bodyBuilder);
    // Add @Test
    methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
    return methodBuilder.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) 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)

Aggregations

AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)232 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)155 ArrayList (java.util.ArrayList)129 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)90 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)87 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)81 JavaType (org.springframework.roo.model.JavaType)78 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)73 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)54 SpringJavaType (org.springframework.roo.model.SpringJavaType)47 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)42 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)40 JdkJavaType (org.springframework.roo.model.JdkJavaType)39 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)31 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)30 RooJavaType (org.springframework.roo.model.RooJavaType)28 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)28 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)26 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)25 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)23