Search in sources :

Example 21 with ClassOrInterfaceTypeDetailsBuilder

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

the class ControllerOperationsImpl method createLinkFactoryClass.

/**
 * Creates a new class which supports its associated controller building
 * URL's for its methods
 *
 * @param controller
 *            the JavaType of the associated controller
 */
@Override
public void createLinkFactoryClass(JavaType controller) {
    // Create name
    String name = controller.getSimpleTypeName().concat("LinkFactory");
    if (name.contains("Controller")) {
        name = name.replace("Controller", "");
    }
    // Create type
    final JavaType linkFactoryJavaType = new JavaType(controller.getPackage().getFullyQualifiedPackageName().concat(".").concat(name), controller.getModule());
    // Create identifier
    final String linkFactoryPathIdentifier = getPathResolver().getCanonicalPath(linkFactoryJavaType.getModule(), Path.SRC_MAIN_JAVA, linkFactoryJavaType);
    final String mid = PhysicalTypeIdentifier.createIdentifier(linkFactoryJavaType, getPathResolver().getPath(linkFactoryPathIdentifier));
    // Create builder
    final ClassOrInterfaceTypeDetailsBuilder typeBuilder = new ClassOrInterfaceTypeDetailsBuilder(mid, PUBLIC, linkFactoryJavaType, PhysicalTypeCategory.CLASS);
    // Add @RooLinkFactory annotation
    AnnotationMetadataBuilder annotationBuilder = new AnnotationMetadataBuilder(RooJavaType.ROO_LINK_FACTORY);
    annotationBuilder.addAttribute(new ClassAttributeValue(new JavaSymbolName("controller"), controller));
    typeBuilder.addAnnotation(annotationBuilder);
    // Write changes to disk
    getTypeManagementService().createOrUpdateTypeOnDisk(typeBuilder.build());
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) 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 22 with ClassOrInterfaceTypeDetailsBuilder

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

the class ThymeleafMVCViewResponseService method annotate.

/**
 * This operation annotates a controller with the THYMELEAF annotation
 *
 * @param controller JavaType with the controller to be annotated.
 */
@Override
public void annotate(JavaType controller) {
    Validate.notNull(controller, "ERROR: You must provide a valid controller");
    ClassOrInterfaceTypeDetails controllerDetails = getTypeLocationService().getTypeDetails(controller);
    // Check if provided controller exists on current project
    Validate.notNull(controllerDetails, "ERROR: You must provide an existing controller");
    // Check if provided controller has been annotated with @RooController
    Validate.notNull(controllerDetails.getAnnotation(RooJavaType.ROO_CONTROLLER), "ERROR: You must provide a controller annotated with @RooController");
    // Add Thymeleaf annotation
    ClassOrInterfaceTypeDetailsBuilder typeBuilder = new ClassOrInterfaceTypeDetailsBuilder(controllerDetails);
    typeBuilder.addAnnotation(new AnnotationMetadataBuilder(getAnnotation()));
    // Write changes on provided controller
    getTypeManagementService().createOrUpdateTypeOnDisk(typeBuilder.build());
}
Also used : ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 23 with ClassOrInterfaceTypeDetailsBuilder

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

the class ThymeleafMVCViewResponseService method addWebMVCThymeleafUIConfiguration.

/**
 * This method adds new WebMVCThymeleafUIConfiguration.java class inside .config
 * package of generated project
 *
 * @param module
 */
private void addWebMVCThymeleafUIConfiguration(Pom module) {
    // Obtain the class annotated with @RooWebMvcConfiguration
    Set<ClassOrInterfaceTypeDetails> webMvcConfigurationSet = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_WEB_MVC_CONFIGURATION);
    if (webMvcConfigurationSet == null || webMvcConfigurationSet.isEmpty()) {
        throw new RuntimeException(String.format("ERROR: Can't found configuration class annotated with @%s.", RooJavaType.ROO_WEB_MVC_CONFIGURATION));
    }
    ClassOrInterfaceTypeDetails webMvcConfiguration = webMvcConfigurationSet.iterator().next();
    // Prevent to include the @RooWebMvcThymeleafUIConfiguration more than once
    if (webMvcConfiguration.getAnnotation(RooJavaType.ROO_WEB_MVC_THYMELEAF_UI_CONFIGURATION) == null) {
        AnnotationMetadataBuilder thymeleaftConfigurationAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_WEB_MVC_THYMELEAF_UI_CONFIGURATION);
        ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(webMvcConfiguration);
        ;
        cidBuilder.addAnnotation(thymeleaftConfigurationAnnotation);
        getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
    }
}
Also used : ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 24 with ClassOrInterfaceTypeDetailsBuilder

use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder 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 25 with ClassOrInterfaceTypeDetailsBuilder

use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder 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)

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