Search in sources :

Example 66 with ClassOrInterfaceTypeDetailsBuilder

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

the class WebFinderOperationsImpl method buildNewSearchController.

/**
 * Build a new search controller for provided entity, with provided response type,
 * package, path prefix and query methods.
 *
 * @param entity
 * @param queryMethods
 * @param responseType
 * @param controllerPackage
 * @param pathPrefix
 * @return
 */
private ClassOrInterfaceTypeDetailsBuilder buildNewSearchController(JavaType entity, List<String> queryMethods, ControllerMVCResponseService responseType, JavaPackage controllerPackage, String pathPrefix) {
    ClassOrInterfaceTypeDetailsBuilder controllerBuilder;
    JavaType searchController = new JavaType(String.format("%s.%sSearch%sController", controllerPackage.getFullyQualifiedPackageName(), pluralService.getPlural(entity), responseType.getControllerNameModifier()), controllerPackage.getModule());
    final String physicalPath = PhysicalTypeIdentifier.createIdentifier(searchController, LogicalPath.getInstance(Path.SRC_MAIN_JAVA, controllerPackage.getModule()));
    controllerBuilder = new ClassOrInterfaceTypeDetailsBuilder(physicalPath, Modifier.PUBLIC, searchController, PhysicalTypeCategory.CLASS);
    // Create @RooController
    AnnotationMetadataBuilder controllerAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_CONTROLLER);
    controllerAnnotation.addClassAttribute("entity", entity);
    if (StringUtils.isNotBlank(pathPrefix)) {
        controllerAnnotation.addStringAttribute("pathPrefix", pathPrefix);
    }
    controllerAnnotation.addEnumAttribute("type", new EnumDetails(RooJavaType.ROO_ENUM_CONTROLLER_TYPE, new JavaSymbolName("SEARCH")));
    controllerBuilder.addAnnotation(controllerAnnotation.build());
    // Create @RooSearch
    AnnotationMetadataBuilder searchAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_SEARCH);
    List<AnnotationAttributeValue<?>> findersToAdd = new ArrayList<AnnotationAttributeValue<?>>();
    for (String finder : queryMethods) {
        findersToAdd.add(new StringAttributeValue(new JavaSymbolName("value"), finder));
    }
    searchAnnotation.addAttribute(new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("finders"), findersToAdd));
    controllerBuilder.addAnnotation(searchAnnotation);
    // Add response type annotation
    AnnotationMetadataBuilder responseTypeAnnotation = new AnnotationMetadataBuilder(responseType.getAnnotation());
    controllerBuilder.addAnnotation(responseTypeAnnotation);
    return controllerBuilder;
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) ArrayList(java.util.ArrayList) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) EnumDetails(org.springframework.roo.model.EnumDetails) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 67 with ClassOrInterfaceTypeDetailsBuilder

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

the class ThymeleafMVCViewResponseService method addMainController.

/**
 * This method add new MainController.java class annotated
 * with @RooThymeleafMainController
 *
 * This controller will be used to return index page.
 *
 * @param module
 */
private void addMainController(Pom module) {
    // Check if already exists other main controller annotated with @RooThymeleafMainController
    if (getMainController() != null) {
        return;
    }
    // If not, define new JavaType
    JavaType mainController = new JavaType(String.format("%s.web.MainController", getProjectOperations().getTopLevelPackage(module.getModuleName())), module.getModuleName());
    // Check that new JavaType doesn't exists
    ClassOrInterfaceTypeDetails mainControllerDetails = getTypeLocationService().getTypeDetails(mainController);
    if (mainControllerDetails != null) {
        AnnotationMetadata mainControllerThymeleafAnnotation = mainControllerDetails.getAnnotation(getMainControllerAnnotation());
        // Maybe, this controller already exists
        if (mainControllerThymeleafAnnotation != null) {
            return;
        } else {
            throw new RuntimeException("ERROR: You are trying to generate more than one MainController.");
        }
    }
    ClassOrInterfaceTypeDetailsBuilder cidBuilder = null;
    final LogicalPath controllerPath = getPathResolver().getPath(mainController.getModule(), Path.SRC_MAIN_JAVA);
    final String resourceIdentifier = getTypeLocationService().getPhysicalTypeCanonicalPath(mainController, controllerPath);
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(mainController, getPathResolver().getPath(resourceIdentifier));
    List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    annotations.add(new AnnotationMetadataBuilder(getMainControllerAnnotation()));
    cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, mainController, PhysicalTypeCategory.CLASS);
    cidBuilder.setAnnotations(annotations);
    getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ArrayList(java.util.ArrayList) LogicalPath(org.springframework.roo.project.LogicalPath) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 68 with ClassOrInterfaceTypeDetailsBuilder

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

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