Search in sources :

Example 76 with Pom

use of org.springframework.roo.project.maven.Pom in project spring-roo by spring-projects.

the class ServiceCommands method service.

@CliCommand(value = "service", help = "Creates new service interface and its implementation related to an entity, or for all the " + "entities in generated project, with some basic management methods by using Spring Data repository methods.")
public void service(@CliOption(key = "all", mandatory = false, specifiedDefaultValue = "true", unspecifiedDefaultValue = "false", help = "Indicates if developer wants to generate service interfaces and their implementations " + "for every entity of current project. " + "This option is mandatory if `--entity` is not specified. Otherwise, using `--entity` " + "will cause the parameter `--all` won't be available. " + "Default if option present: `true`; default if option not present: `false`.") boolean all, @CliOption(key = "entity", optionContext = PROJECT, mandatory = false, help = "The domain entity this service should manage. When working on a single module " + "project, simply specify the name of the entity. If you consider it necessary, you can " + "also specify the package. Ex.: `--class ~.domain.MyEntity` (where `~` is the base package). " + "When working with multiple modules, you should specify the name of the entity and the " + "module where it is. Ex.: `--class model:~.domain.MyEntity`. If the module is not specified, " + "it is assumed that the entity is in the module which has the focus. " + "Possible values are: any of the entities in the project. " + "This option is mandatory if `--all` is not specified. Otherwise, using `--all` " + "will cause the parameter `--entity` won't be available.") final JavaType domainType, @CliOption(key = "repository", optionContext = PROJECT, mandatory = true, help = "The repository this service should expose. When working on a single module project, " + "simply specify the name of the class. If you consider it necessary, you can also " + "specify the package. Ex.: `--class ~.repository.MyClass` (where `~` is the base " + "package). When working with multiple modules, you should specify the name of the class " + "and the module where it is. Ex.: `--class repository:~.MyClass`. If the module is not " + "specified, it is assumed that the class is in the module which has the focus. " + "Possible values are: any of the repositories annotated with `@RooJpaRepository` and " + "associated to the entity specified in `--entity`. " + "This option is mandatory if `--entity` has been already specified and the project is " + "multi-module. " + "This option is available only when `--entity` has been specified. " + "Default if option not present: first repository annotated with `@RooJpaRepository` and " + "associated to the entity specified in `--entity`.") final JavaType repositoryType, @CliOption(key = "interface", mandatory = true, help = "The service interface to be generated. When working on a single module project, simply " + "specify the name of the class. If you consider it necessary, you can also specify the " + "package. Ex.: `--class ~.service.api.MyClass` (where `~` is the base package). When " + "working with multiple modules, you should specify the name of the class and the module " + "where it is. Ex.: `--class service-api:~.MyClass`. If the module is not specified, it " + "is assumed that the class is in the module which has the focus. " + "This option is mandatory if `--entity` has been already specified and the project is " + "multi-module. " + "This option is available only when `--entity` has been specified. " + "Default if option not present: concatenation of entity simple name with 'Service' in " + "`~.service.api` package, or 'service-api:~.' if multi-module project.") final JavaType interfaceType, @CliOption(key = "class", mandatory = false, help = "The service implementation to be generated. When working on a single module " + "project, simply specify the name of the class. If you consider it necessary, you can " + "also specify the package. Ex.: `--class ~.service.impl.MyClass` (where `~` is the base " + "package). When working with multiple modules, you should specify the name of the class " + "and the module where it is. Ex.: `--class service-impl:~.MyClass`. If the module is not " + "specified, it is assumed that the class is in the module which has the focus. " + "This option is available only when `--entity` has been specified. " + "Default if option not present: concatenation of entity simple name with 'ServiceImpl' " + "in `~.service.impl` package, or 'service-impl:~.' if multi-module project.") final JavaType implType, @CliOption(key = "apiPackage", mandatory = false, help = "The java interface package. In multi-module project you should specify the module name" + " before the package name. Ex.: `--apiPackage service-api:org.springframework.roo` but, " + "if module name is not present, the Roo Shell focused module will be used. " + "This option is available only when `--all` parameter has been specified. " + "Default value if not present: `~.service.api` package, or 'service-api:~.' if " + "multi-module project.") JavaPackage apiPackage, @CliOption(key = "implPackage", mandatory = false, help = "The java package of the implementation classes for the interfaces. In multi-module " + "project you should specify the module name before the package name. Ex.: `--implPackage " + "service-impl:org.springframework.roo` but, if module name is not present, the Roo Shell " + "focused module will be used. " + "This option is available only when `--all` parameter has been specified. " + "Default value if not present: `~.service.impl` package, or 'service-impl:~.' if " + "multi-module project.") JavaPackage implPackage) {
    if (all) {
        // If user didn't specified some API package, use default API package
        if (apiPackage == null) {
            if (projectOperations.isMultimoduleProject()) {
                // Build default JavaPackage with module
                for (String moduleName : projectOperations.getModuleNames()) {
                    if (moduleName.equals("service-api")) {
                        Pom module = projectOperations.getPomFromModuleName(moduleName);
                        apiPackage = new JavaPackage(typeLocationService.getTopLevelPackageForModule(module), moduleName);
                        break;
                    }
                }
                // Check if repository found
                Validate.notNull(apiPackage, "Couldn't find in project a default service.api " + "package. Please, use 'apiPackage' option to specify it.");
            } else {
                // Build default JavaPackage for single module
                apiPackage = new JavaPackage(projectOperations.getFocusedTopLevelPackage().getFullyQualifiedPackageName().concat(".service.api"), projectOperations.getFocusedModuleName());
            }
        }
        // If user didn't specified some impl package, use default impl package
        if (implPackage == null) {
            if (projectOperations.isMultimoduleProject()) {
                // Build default JavaPackage with module
                for (String moduleName : projectOperations.getModuleNames()) {
                    if (moduleName.equals("service-impl")) {
                        Pom module = projectOperations.getPomFromModuleName(moduleName);
                        implPackage = new JavaPackage(typeLocationService.getTopLevelPackageForModule(module), moduleName);
                        break;
                    }
                }
                // Check if repository found
                Validate.notNull(implPackage, "Couldn't find in project a default service.impl " + "package. Please, use 'implPackage' option to specify it.");
            } else {
                // Build default JavaPackage for single module
                implPackage = new JavaPackage(projectOperations.getFocusedTopLevelPackage().getFullyQualifiedPackageName().concat(".service.impl"), projectOperations.getFocusedModuleName());
            }
        }
        serviceOperations.addAllServices(apiPackage, implPackage);
    } else {
        serviceOperations.addService(domainType, repositoryType, interfaceType, implType);
    }
}
Also used : JavaPackage(org.springframework.roo.model.JavaPackage) Pom(org.springframework.roo.project.maven.Pom) CliCommand(org.springframework.roo.shell.CliCommand)

Example 77 with Pom

use of org.springframework.roo.project.maven.Pom in project spring-roo by spring-projects.

the class RepositoryJpaOperationsImpl method addRepositoryConfigurationClass.

/**
 * Checks for all the application modules in project and adds a repository
 * configuration class, which uses the Springlets base repository class if
 * none is already specified.
 */
private void addRepositoryConfigurationClass() {
    Set<ClassOrInterfaceTypeDetails> applicationCids = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(SpringJavaType.SPRING_BOOT_APPLICATION);
    for (ClassOrInterfaceTypeDetails applicationCid : applicationCids) {
        // Obtain main application config class and its module
        Pom module = getProjectOperations().getPomFromModuleName(applicationCid.getType().getModule());
        // Create or update SpringDataJpaDetachableRepositoryConfiguration
        JavaType repositoryConfigurationClass = new JavaType(String.format("%s.config.SpringDataJpaDetachableRepositoryConfiguration", getTypeLocationService().getTopLevelPackageForModule(module)), module.getModuleName());
        Validate.notNull(repositoryConfigurationClass.getModule(), "ERROR: Module name is required to generate a valid JavaType");
        // Checks if new service interface already exists.
        final String repositoryConfigurationClassIdentifier = getPathResolver().getCanonicalPath(repositoryConfigurationClass.getModule(), Path.SRC_MAIN_JAVA, repositoryConfigurationClass);
        final String mid = PhysicalTypeIdentifier.createIdentifier(repositoryConfigurationClass, getPathResolver().getPath(repositoryConfigurationClassIdentifier));
        if (!getFileManager().exists(repositoryConfigurationClassIdentifier)) {
            // Repository config class doesn't exist. Create class builder
            final ClassOrInterfaceTypeDetailsBuilder typeBuilder = new ClassOrInterfaceTypeDetailsBuilder(mid, PUBLIC, repositoryConfigurationClass, PhysicalTypeCategory.CLASS);
            // Add @RooJpaRepositoryConfiguration
            AnnotationMetadataBuilder repositoryCondigurationAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JPA_REPOSITORY_CONFIGURATION);
            typeBuilder.addAnnotation(repositoryCondigurationAnnotation);
            // Write new class disk
            getTypeManagementService().createOrUpdateTypeOnDisk(typeBuilder.build());
        }
    }
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) JavaType(org.springframework.roo.model.JavaType) 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)

Example 78 with Pom

use of org.springframework.roo.project.maven.Pom in project spring-roo by spring-projects.

the class RepositoryJpaTestCreator method createIntegrationTest.

@Override
public void createIntegrationTest(JavaType type, Pom module) {
    Validate.notNull(type, "Class to produce an integration test class for is required");
    // Check if provided JavaType is a Repository
    ClassOrInterfaceTypeDetails cid = typeLocationService.getTypeDetails(type);
    Validate.notNull(cid.getAnnotation(RooJavaType.ROO_REPOSITORY_JPA), "Type must be a Roo JPA Repository type.");
    // Get the repository managed entity
    RepositoryJpaAnnotationValues repositoryAnnotationValues = new RepositoryJpaAnnotationValues(cid);
    JavaType managedEntity = repositoryAnnotationValues.getEntity();
    // Workaround to get a JavaType with not null module when recovering it
    // from a ClassAttributeValue
    managedEntity = new JavaType(managedEntity.getFullyQualifiedTypeName(), managedEntity.getArray(), managedEntity.getDataType(), managedEntity.getArgName(), managedEntity.getParameters(), typeLocationService.getTypeDetails(managedEntity).getType().getModule());
    // Create Data On Demand artifacts for managed entity
    List<DataOnDemandCreatorProvider> dodCreators = getValidDataOnDemandCreatorsForType(managedEntity);
    Validate.isTrue(!dodCreators.isEmpty(), "Couldn't find any 'DataOnDemandCreatorProvider' for JPA repositories.");
    Validate.isTrue(dodCreators.size() == 1, "More than 1 valid 'DataOnDemandCreatorProvider' found for JPA repositories. %s can't decide which one to use.", this.getClass().getName());
    DataOnDemandCreatorProvider creator = dodCreators.get(0);
    creator.createDataOnDemand(managedEntity);
    // Add module dependency with test-jar dependency
    if (projectOperations.isMultimoduleProject()) {
        String managedEntityModuleName = managedEntity.getModule();
        Pom managedEntityModule = projectOperations.getPomFromModuleName(managedEntityModuleName);
        projectOperations.addDependency(module.getModuleName(), new Dependency(managedEntityModule.getGroupId(), managedEntityModule.getArtifactId(), "${project.version}", DependencyType.valueOfTypeCode("test-jar"), DependencyScope.TEST), true, true);
    }
    // Create integration test class
    final JavaType name = new JavaType(type + "IT", module.getModuleName());
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, Path.SRC_TEST_JAVA.getModulePathId(module.getModuleName()));
    if (metadataService.get(declaredByMetadataId) != null) {
        // The file already exists
        return;
    }
    // Add @RooRepositoryJpaIntegrationTest to source file
    AnnotationMetadataBuilder rooIntegrationTestAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_REPOSITORY_JPA_INTEGRATION_TEST);
    rooIntegrationTestAnnotation.addClassAttribute("targetClass", type);
    rooIntegrationTestAnnotation.addClassAttribute("dodConfigurationClass", creator.getDataOnDemandConfiguration());
    rooIntegrationTestAnnotation.addClassAttribute("dodClass", creator.getDataOnDemand(managedEntity));
    // Create integration test class
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, name, PhysicalTypeCategory.CLASS);
    cidBuilder.addAnnotation(rooIntegrationTestAnnotation);
    // Write changes to disk
    typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
}
Also used : RepositoryJpaAnnotationValues(org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaAnnotationValues) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) Dependency(org.springframework.roo.project.Dependency) DataOnDemandCreatorProvider(org.springframework.roo.addon.test.providers.DataOnDemandCreatorProvider) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) Pom(org.springframework.roo.project.maven.Pom)

Example 79 with Pom

use of org.springframework.roo.project.maven.Pom in project spring-roo by spring-projects.

the class WebFinderCommands method getInstalledControllerMVCResponseTypes.

/**
 * This method gets all implementations of ControllerMVCResponseService interface to be able
 * to locate all installed ControllerMVCResponseService
 *
 * @return Map with responseTypes identifier and the ControllerMVCResponseService implementation
 */
public Map<String, ControllerMVCResponseService> getInstalledControllerMVCResponseTypes() {
    if (responseTypes.isEmpty()) {
        try {
            ServiceReference<?>[] references = this.context.getAllServiceReferences(ControllerMVCResponseService.class.getName(), null);
            for (ServiceReference<?> ref : references) {
                ControllerMVCResponseService responseTypeService = (ControllerMVCResponseService) this.context.getService(ref);
                boolean isInstalled = false;
                for (Pom module : getProjectOperations().getPoms()) {
                    if (responseTypeService.isInstalledInModule(module.getModuleName())) {
                        isInstalled = true;
                        break;
                    }
                }
                if (isInstalled) {
                    responseTypes.put(responseTypeService.getResponseType(), responseTypeService);
                }
            }
            return responseTypes;
        } catch (InvalidSyntaxException e) {
            LOGGER.warning("Cannot load ControllerMVCResponseService on ControllerCommands.");
            return null;
        }
    } else {
        return responseTypes;
    }
}
Also used : ControllerMVCResponseService(org.springframework.roo.addon.web.mvc.controller.addon.responses.ControllerMVCResponseService) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference) Pom(org.springframework.roo.project.maven.Pom)

Example 80 with Pom

use of org.springframework.roo.project.maven.Pom in project spring-roo by spring-projects.

the class ThymeleafControllerTestCreator method createIntegrationTest.

@Override
public void createIntegrationTest(JavaType type, Pom module) {
    Validate.notNull(type, "Class to produce an integration test class for is required");
    // Check if provided JavaType is a Thymeleaf Controller
    ClassOrInterfaceTypeDetails cid = typeLocationService.getTypeDetails(type);
    Validate.notNull(cid.getAnnotation(RooJavaType.ROO_CONTROLLER), "Type must be a Roo controller.");
    Validate.notNull(cid.getAnnotation(RooJavaType.ROO_THYMELEAF), "Type must be a Roo Thymeleaf controller.");
    // Add springlets-boot-starter-test dependency
    projectOperations.addProperty("", SPRINGLETS_VERSION_PROPERTY);
    projectOperations.addDependency(module.getModuleName(), SPRINGLETS_BOOT_STARTER_TEST_DEPENDENCY);
    // Get the controller managed entity
    ControllerAnnotationValues controllerAnnotationValues = new ControllerAnnotationValues(cid);
    JavaType managedEntity = controllerAnnotationValues.getEntity();
    // Workaround to get a JavaType with not null module when recovering it
    // from a ClassAttributeValue
    managedEntity = new JavaType(managedEntity.getFullyQualifiedTypeName(), managedEntity.getArray(), managedEntity.getDataType(), managedEntity.getArgName(), managedEntity.getParameters(), typeLocationService.getTypeDetails(managedEntity).getType().getModule());
    // Create Data On Demand artifacts for managed entity
    List<DataOnDemandCreatorProvider> dodCreators = getValidDataOnDemandCreatorsForType(managedEntity);
    Validate.isTrue(!dodCreators.isEmpty(), "Couldn't find any 'DataOnDemandCreatorProvider' for Thymeleaf controllers.");
    Validate.isTrue(dodCreators.size() == 1, "More than 1 valid 'DataOnDemandCreatorProvider' found for Thymeleaf controllers. %s can't decide which one to use.", this.getClass().getName());
    DataOnDemandCreatorProvider creator = dodCreators.get(0);
    creator.createDataOnDemand(managedEntity);
    // Add module dependency with test-jar dependency
    if (projectOperations.isMultimoduleProject()) {
        String managedEntityModuleName = managedEntity.getModule();
        Pom managedEntityModule = projectOperations.getPomFromModuleName(managedEntityModuleName);
        projectOperations.addDependency(module.getModuleName(), new Dependency(managedEntityModule.getGroupId(), managedEntityModule.getArtifactId(), "${project.version}", DependencyType.valueOfTypeCode("test-jar"), DependencyScope.TEST), true, true);
    }
    // Create integration test class
    final JavaType name = new JavaType(type + "IT", module.getModuleName());
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, Path.SRC_TEST_JAVA.getModulePathId(module.getModuleName()));
    if (metadataService.get(declaredByMetadataId) != null) {
        // The file already exists
        return;
    }
    // Add @RooThymeleafControllerIntegrationTest to source file
    AnnotationMetadataBuilder rooIntegrationTestAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_THYMELEAF_CONTROLLER_INTEGRATION_TEST);
    rooIntegrationTestAnnotation.addClassAttribute("targetClass", type);
    // Create integration test class
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, name, PhysicalTypeCategory.CLASS);
    cidBuilder.addAnnotation(rooIntegrationTestAnnotation);
    // Write changes to disk
    typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ControllerAnnotationValues(org.springframework.roo.addon.web.mvc.controller.addon.ControllerAnnotationValues) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) Dependency(org.springframework.roo.project.Dependency) DataOnDemandCreatorProvider(org.springframework.roo.addon.test.providers.DataOnDemandCreatorProvider) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) Pom(org.springframework.roo.project.maven.Pom)

Aggregations

Pom (org.springframework.roo.project.maven.Pom)86 Element (org.w3c.dom.Element)19 JavaType (org.springframework.roo.model.JavaType)16 Document (org.w3c.dom.Document)16 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)10 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)9 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)9 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)9 Dependency (org.springframework.roo.project.Dependency)9 RooJavaType (org.springframework.roo.model.RooJavaType)7 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)5 ServiceReference (org.osgi.framework.ServiceReference)5 ControllerMVCResponseService (org.springframework.roo.addon.web.mvc.controller.addon.responses.ControllerMVCResponseService)5 JavaPackage (org.springframework.roo.model.JavaPackage)5 List (java.util.List)4 Plugin (org.springframework.roo.project.Plugin)4 Completion (org.springframework.roo.shell.Completion)4 HashMap (java.util.HashMap)3 DataOnDemandCreatorProvider (org.springframework.roo.addon.test.providers.DataOnDemandCreatorProvider)3