Search in sources :

Example 76 with ClassOrInterfaceTypeDetails

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

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

the class ThymeleafMVCViewResponseService method hasResponseType.

/**
 * This operation will check if some controller has the @RooThymeleaf annotation
 *
 * @param controller JavaType with controller to check
 * @return true if provided controller has the THYMELEAF responseType.
 *        If not, return false.
 */
@Override
public boolean hasResponseType(JavaType controller) {
    Validate.notNull(controller, "ERROR: You must provide a valid controller");
    ClassOrInterfaceTypeDetails controllerDetails = getTypeLocationService().getTypeDetails(controller);
    if (controllerDetails == null) {
        return false;
    }
    return controllerDetails.getAnnotation(getAnnotation()) != null;
}
Also used : ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)

Example 78 with ClassOrInterfaceTypeDetails

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

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

the class WebFinderCommands method getEntityValues.

@CliOptionAutocompleteIndicator(param = "entity", command = "web mvc finder", help = "--entity parameter should be completed with classes annotated with @RooJpaEntity.")
public List<String> getEntityValues(ShellContext context) {
    // Get current value of class
    String currentText = context.getParameters().get("entity");
    // Create results to return
    List<String> results = new ArrayList<String>();
    for (JavaType entity : getTypeLocationService().findTypesWithAnnotation(RooJavaType.ROO_JPA_ENTITY)) {
        ClassOrInterfaceTypeDetails repository = repositoryJpaLocator.getFirstRepository(entity);
        if (repository == null) {
            continue;
        }
        AnnotationMetadata repositoryAnnotation = repository.getAnnotation(RooJavaType.ROO_REPOSITORY_JPA);
        if (repositoryAnnotation.getAttribute("finders") == null) {
            continue;
        }
        String name = replaceTopLevelPackageString(entity, currentText);
        if (!results.contains(name)) {
            results.add(name);
        }
    }
    if (results.isEmpty()) {
        results.add("");
    }
    return results;
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ArrayList(java.util.ArrayList) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) CliOptionAutocompleteIndicator(org.springframework.roo.shell.CliOptionAutocompleteIndicator)

Example 80 with ClassOrInterfaceTypeDetails

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

Aggregations

ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)232 JavaType (org.springframework.roo.model.JavaType)114 ArrayList (java.util.ArrayList)87 RooJavaType (org.springframework.roo.model.RooJavaType)86 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)52 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)43 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)42 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)40 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)39 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)36 JpaJavaType (org.springframework.roo.model.JpaJavaType)34 SpringJavaType (org.springframework.roo.model.SpringJavaType)28 CliOptionAutocompleteIndicator (org.springframework.roo.shell.CliOptionAutocompleteIndicator)24 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)23 JdkJavaType (org.springframework.roo.model.JdkJavaType)22 Test (org.junit.Test)20 File (java.io.File)19 List (java.util.List)19 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)19 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)17