Search in sources :

Example 1 with EntityDeserializerAnnotationValues

use of org.springframework.roo.addon.web.mvc.controller.addon.config.EntityDeserializerAnnotationValues 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)

Aggregations

ArrayList (java.util.ArrayList)1 EntityDeserializerAnnotationValues (org.springframework.roo.addon.web.mvc.controller.addon.config.EntityDeserializerAnnotationValues)1 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)1 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)1 ConstructorMetadata (org.springframework.roo.classpath.details.ConstructorMetadata)1 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)1 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)1 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)1 JavaType (org.springframework.roo.model.JavaType)1 JpaJavaType (org.springframework.roo.model.JpaJavaType)1 RooJavaType (org.springframework.roo.model.RooJavaType)1 SpringJavaType (org.springframework.roo.model.SpringJavaType)1 LogicalPath (org.springframework.roo.project.LogicalPath)1