Search in sources :

Example 16 with ClassOrInterfaceTypeDetailsBuilder

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

the class ControllerOperationsImpl method createDomainModelModule.

/**
 * Create DomainModelModule.java class and adds it
 * {@link RooDomainModelModule} annotation
 *
 * @param module
 *            the Pom where configuration classes should be installed
 */
private void createDomainModelModule(Pom module) {
    // Create DomainModelModule.java class
    JavaType domainModelModule = new JavaType(String.format("%s.config.jackson.DomainModelModule", getTypeLocationService().getTopLevelPackageForModule(module)), module.getModuleName());
    Validate.notNull(domainModelModule.getModule(), "ERROR: Module name is required to generate a valid JavaType");
    final String domainModelModuleIdentifier = getPathResolver().getCanonicalPath(domainModelModule.getModule(), Path.SRC_MAIN_JAVA, domainModelModule);
    // Check if file already exists
    if (!getFileManager().exists(domainModelModuleIdentifier)) {
        // Creating class builder
        final String mid = PhysicalTypeIdentifier.createIdentifier(domainModelModule, getPathResolver().getPath(domainModelModuleIdentifier));
        final ClassOrInterfaceTypeDetailsBuilder typeBuilder = new ClassOrInterfaceTypeDetailsBuilder(mid, PUBLIC, domainModelModule, PhysicalTypeCategory.CLASS);
        // Generating @RooDomainModelModule annotation
        typeBuilder.addAnnotation(new AnnotationMetadataBuilder(RooJavaType.ROO_DOMAIN_MODEL_MODULE));
        // Write new class 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) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 17 with ClassOrInterfaceTypeDetailsBuilder

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

the class ControllerOperationsImpl method createJsonDeserializersIfDontExists.

/**
 * Create the Json Desearializer utility class (annotated
 * with @RooDeserializer) for target Entity and its related entities if they
 * aren't created yet.
 *
 * @param entity
 * @param module
 * @param controllerPackage
 */
private void createJsonDeserializersIfDontExists(JavaType currentEntity, String module, JavaPackage controllerPackage) {
    List<JavaType> entitiesToCreateSerializers = getParentAndChildrenRelatedEntities(currentEntity);
    // Check if already exists a serializer for each entity
    Set<ClassOrInterfaceTypeDetails> allDeserializer = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_DESERIALIZER);
    for (JavaType entity : entitiesToCreateSerializers) {
        EntityDeserializerAnnotationValues values;
        boolean deserializerFound = false;
        for (ClassOrInterfaceTypeDetails deserializer : allDeserializer) {
            values = new EntityDeserializerAnnotationValues(deserializer);
            if (entity.equals(values.getEntity())) {
                // Found mixing. Nothing to do.
                deserializerFound = true;
            }
        }
        if (!deserializerFound) {
            // Not found deserializer. Create it
            ClassOrInterfaceTypeDetails serviceDetails = getServiceLocator().getService(entity);
            Validate.notNull(serviceDetails, "Can't found service for Entity %s to generate " + "Serializer. If it is a related entity with the one to generate " + "controller, it needs a service.", entity.getFullyQualifiedTypeName());
            // Build @RooDeserializer
            List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
            annotations = new ArrayList<AnnotationMetadataBuilder>();
            AnnotationMetadataBuilder deserializerAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_DESERIALIZER);
            deserializerAnnotation.addClassAttribute("entity", entity);
            annotations.add(deserializerAnnotation);
            JavaType deserializerClass = new JavaType(String.format("%s.%sDeserializer", controllerPackage.getFullyQualifiedPackageName(), entity.getSimpleTypeName()), module);
            final LogicalPath deserializerPath = getPathResolver().getPath(module, Path.SRC_MAIN_JAVA);
            final String resourceIdentifierItem = getTypeLocationService().getPhysicalTypeCanonicalPath(deserializerClass, deserializerPath);
            final String declaredByMetadataIdItem = PhysicalTypeIdentifier.createIdentifier(deserializerClass, getPathResolver().getPath(resourceIdentifierItem));
            ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataIdItem, Modifier.PUBLIC, deserializerClass, PhysicalTypeCategory.CLASS);
            cidBuilder.setAnnotations(annotations);
            /*
         * Moved extend to java (instead ITD) because there were
         * compilation problems when Mixin
         * uses @JsonDeserialize(using=EntityDeserializer.class)
         * annotation (requires extend of JsonDeseralizer)
         */
            cidBuilder.addExtendsTypes(JavaType.wrapperOf(JSON_OBJECT_DESERIALIZER, entity));
            FieldMetadata serviceField = EntityDeserializerMetadata.getFieldFor(declaredByMetadataIdItem, serviceDetails.getType());
            FieldMetadata conversionServiceField = EntityDeserializerMetadata.getFieldFor(declaredByMetadataIdItem, SpringJavaType.CONVERSION_SERVICE);
            cidBuilder.addField(serviceField);
            cidBuilder.addField(conversionServiceField);
            ConstructorMetadata constructor = EntityDeserializerMetadata.getConstructor(declaredByMetadataIdItem, serviceField, conversionServiceField);
            cidBuilder.addConstructor(constructor);
            getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
        }
    }
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) LogicalPath(org.springframework.roo.project.LogicalPath) 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) EntityDeserializerAnnotationValues(org.springframework.roo.addon.web.mvc.controller.addon.config.EntityDeserializerAnnotationValues) ConstructorMetadata(org.springframework.roo.classpath.details.ConstructorMetadata) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 18 with ClassOrInterfaceTypeDetailsBuilder

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

the class ControllerOperationsImpl method createJsonMixinIfDontExists.

/**
 * Create the Json Mixin utility class (annotated with @RooJsonMixin) for
 * target Entity if it isn't created yet.
 *
 * @param entity
 * @param entityMetadata
 * @param requiresDeserializer
 * @param module
 * @param controllerPackage
 */
private void createJsonMixinIfDontExists(JavaType entity, JpaEntityMetadata entityMetadata, String module, JavaPackage controllerPackage) {
    Set<ClassOrInterfaceTypeDetails> allJsonMixin = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_JSON_MIXIN);
    JSONMixinAnnotationValues values;
    for (ClassOrInterfaceTypeDetails mixin : allJsonMixin) {
        values = new JSONMixinAnnotationValues(mixin);
        if (entity.equals(values.getEntity())) {
            // Found mixing. Nothing to do.
            return;
        }
    }
    // Not found. Create class
    List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    annotations = new ArrayList<AnnotationMetadataBuilder>();
    AnnotationMetadataBuilder mixinAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JSON_MIXIN);
    mixinAnnotation.addClassAttribute("entity", entity);
    annotations.add(mixinAnnotation);
    JavaType mixinClass = new JavaType(String.format("%s.%sJsonMixin", controllerPackage.getFullyQualifiedPackageName(), entity.getSimpleTypeName()), module);
    final LogicalPath mixinPath = getPathResolver().getPath(module, Path.SRC_MAIN_JAVA);
    final String resourceIdentifierItem = getTypeLocationService().getPhysicalTypeCanonicalPath(mixinClass, mixinPath);
    final String declaredByMetadataIdItem = PhysicalTypeIdentifier.createIdentifier(mixinClass, getPathResolver().getPath(resourceIdentifierItem));
    ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataIdItem, Modifier.PUBLIC + Modifier.ABSTRACT, mixinClass, PhysicalTypeCategory.CLASS);
    cidBuilder.setAnnotations(annotations);
    getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.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) JSONMixinAnnotationValues(org.springframework.roo.addon.web.mvc.controller.addon.config.JSONMixinAnnotationValues) ArrayList(java.util.ArrayList) LogicalPath(org.springframework.roo.project.LogicalPath) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 19 with ClassOrInterfaceTypeDetailsBuilder

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

the class ControllerOperationsImpl method createDetailClass.

private boolean createDetailClass(String field, String controllerName, ControllerType type, JavaType entity, ControllerMVCResponseService responseType, JavaPackage controllerPackage, String pathPrefixController, String viewsList) {
    JavaType detailController = new JavaType(String.format("%s.%s", controllerPackage.getFullyQualifiedPackageName(), controllerName), controllerPackage.getModule());
    ClassOrInterfaceTypeDetails detailControllerDetails = getTypeLocationService().getTypeDetails(detailController);
    if (detailControllerDetails != null) {
        LOGGER.log(Level.INFO, String.format("ERROR: Class '%s' already exists inside your generated project.", detailController.getFullyQualifiedTypeName()));
        return false;
    }
    List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    annotations.add(getRooControllerAnnotation(entity, pathPrefixController, type));
    annotations.add(getRooDetailAnnotation(field, viewsList));
    // Add responseType annotation. Don't use responseTypeService
    // annotate to
    // prevent multiple
    // updates of the .java file. Annotate operation will be used during
    // controller update.
    annotations.add(new AnnotationMetadataBuilder(responseType.getAnnotation()));
    final LogicalPath detailControllerPathItem = getPathResolver().getPath(detailController.getModule(), Path.SRC_MAIN_JAVA);
    final String resourceIdentifierItem = getTypeLocationService().getPhysicalTypeCanonicalPath(detailController, detailControllerPathItem);
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(detailController, getPathResolver().getPath(resourceIdentifierItem));
    ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, detailController, PhysicalTypeCategory.CLASS);
    cidBuilder.setAnnotations(annotations);
    getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
    // Create LinkFactory class
    if (responseType.getName().equals("THYMELEAF")) {
        createLinkFactoryClass(cidBuilder.getName());
    }
    if (getProjectOperations().isMultimoduleProject()) {
    // TODO
    // // Getting related service
    // JavaType relatedEntityService = null;
    // Set<ClassOrInterfaceTypeDetails> services =
    // getTypeLocationService()
    // .findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_SERVICE);
    // Iterator<ClassOrInterfaceTypeDetails> itServices =
    // services.iterator();
    // 
    // while (itServices.hasNext()) {
    // ClassOrInterfaceTypeDetails existingService = itServices.next();
    // AnnotationAttributeValue<Object> entityAttr =
    // existingService.getAnnotation(RooJavaType.ROO_SERVICE).getAttribute("entity");
    // JavaType entityJavaType = (JavaType) entityAttr.getValue();
    // String entityField = relationFieldObject.get(field);
    // if (entityJavaType.getSimpleTypeName().equals(entityField)) {
    // relatedEntityService = existingService.getType();
    // break;
    // }
    // }
    // 
    // getProjectOperations().addModuleDependency(detailController.getModule(),
    // relatedEntityService.getModule());
    }
    return true;
}
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) ArrayList(java.util.ArrayList) LogicalPath(org.springframework.roo.project.LogicalPath) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 20 with ClassOrInterfaceTypeDetailsBuilder

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

the class ControllerOperationsImpl method exportOperation.

/**
 * Generate the operations selected in the controller indicated
 *
 * @param controller
 *            Controller where the operations will be created
 * @param operations
 *            Service operations names that will be created
 */
public void exportOperation(JavaType controller, List<String> operations) {
    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");
    // Check parameter operations
    Validate.notEmpty(operations, "INFO: Don't exist operations to publish");
    ClassOrInterfaceTypeDetailsBuilder controllerBuilder = new ClassOrInterfaceTypeDetailsBuilder(controllerDetails);
    AnnotationMetadata operationsAnnotation = controllerDetails.getAnnotation(RooJavaType.ROO_OPERATIONS);
    // Create an array with new attributes array
    List<StringAttributeValue> operationsToAdd = new ArrayList<StringAttributeValue>();
    if (operationsAnnotation == null) {
        // Add Operations annotation
        AnnotationMetadataBuilder opAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_OPERATIONS);
        controllerBuilder.addAnnotation(opAnnotation);
        // set operations from command
        for (String operation : operations) {
            operationsToAdd.add(new StringAttributeValue(new JavaSymbolName("value"), operation));
        }
        opAnnotation.addAttribute(new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("operations"), operationsToAdd));
        // Write changes on provided controller
        getTypeManagementService().createOrUpdateTypeOnDisk(controllerBuilder.build());
    } else {
        List<String> operationsNames = new ArrayList<String>();
        boolean operationsAdded = false;
        AnnotationAttributeValue<Object> attributeOperations = operationsAnnotation.getAttribute("operations");
        if (attributeOperations != null) {
            List<StringAttributeValue> existingOperations = (List<StringAttributeValue>) attributeOperations.getValue();
            Iterator<StringAttributeValue> it = existingOperations.iterator();
            // new ones
            while (it.hasNext()) {
                StringAttributeValue attributeValue = (StringAttributeValue) it.next();
                operationsToAdd.add(attributeValue);
                operationsNames.add(attributeValue.getValue());
            }
            // Add new finders to new attributes array
            for (String operation : operations) {
                if (!operationsNames.contains(operation)) {
                    operationsToAdd.add(new StringAttributeValue(new JavaSymbolName("value"), operation));
                    operationsAdded = true;
                }
            }
            if (operationsAdded) {
                AnnotationMetadataBuilder opAnnotation = new AnnotationMetadataBuilder(operationsAnnotation);
                opAnnotation.addAttribute(new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("operations"), operationsToAdd));
                controllerBuilder.updateTypeAnnotation(opAnnotation);
                // Write changes on provided controller
                getTypeManagementService().createOrUpdateTypeOnDisk(controllerBuilder.build());
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) List(java.util.List) ArrayList(java.util.ArrayList) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) 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