Search in sources :

Example 91 with JavaType

use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.

the class ControllerMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    ControllerAnnotationValues controllerValues = new ControllerAnnotationValues(governorPhysicalTypeMetadata);
    // Getting entity
    final JavaType entity = controllerValues.getEntity();
    ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    // Get entity metadata
    final String entityMetadataId = JpaEntityMetadata.createIdentifier(entityDetails);
    registerDependency(entityMetadataId, metadataIdentificationString);
    final JpaEntityMetadata entityMetadata = getMetadataService().get(entityMetadataId);
    // This metadata is not available yet.
    if (entityMetadata == null) {
        return null;
    }
    // Getting type
    ControllerType type = controllerValues.getType();
    // Getting related service
    ClassOrInterfaceTypeDetails serviceDetails = getServiceLocator().getService(entity);
    JavaType service = serviceDetails.getType();
    final String serviceMetadataId = ServiceMetadata.createIdentifier(serviceDetails);
    registerDependency(serviceMetadataId, metadataIdentificationString);
    final ServiceMetadata serviceMetadata = getMetadataService().get(serviceMetadataId);
    // This metadata is not available yet.
    if (serviceMetadata == null) {
        return null;
    }
    // Generate path
    final String path = getControllerOperations().getBasePathForController(governorPhysicalTypeMetadata.getMemberHoldingTypeDetails());
    final String baseUrl = getControllerOperations().getBaseUrlForController(governorPhysicalTypeMetadata.getMemberHoldingTypeDetails());
    List<RelationInfoExtended> detailsFieldInfo = null;
    String detailAnnotaionFieldValue = null;
    Map<JavaType, ServiceMetadata> detailsServiceMetadata = null;
    if (type == ControllerType.DETAIL || type == ControllerType.DETAIL_ITEM) {
        final JavaType controller = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails().getName();
        // Getting the relationField from @RooDetail entity
        final AnnotationAttributeValue<Object> relationFieldAttr = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails().getAnnotation(RooJavaType.ROO_DETAIL).getAttribute("relationField");
        Validate.notNull(relationFieldAttr, "ERROR: In %s controller, @RooDetail annotation must have relationField value", controller);
        detailAnnotaionFieldValue = (String) relationFieldAttr.getValue();
        Validate.isTrue(StringUtils.isNotBlank(detailAnnotaionFieldValue), "ERROR: In %s controller, @RooDetail annotation must have relationField value", controller);
        // generate detail info object
        detailsFieldInfo = getControllerOperations().getRelationInfoFor(entityMetadata, detailAnnotaionFieldValue);
        detailsServiceMetadata = new TreeMap<JavaType, ServiceMetadata>();
        for (RelationInfoExtended info : detailsFieldInfo) {
            if (detailsServiceMetadata.containsKey(info.childType)) {
                continue;
            }
            // Getting related service
            ClassOrInterfaceTypeDetails detailsServiceDetails = getServiceLocator().getService(info.childType);
            String detailServiceMetadataId = ServiceMetadata.createIdentifier(detailsServiceDetails);
            registerDependency(detailServiceMetadataId, metadataIdentificationString);
            ServiceMetadata curMetadata = getMetadataService().get(detailServiceMetadataId);
            if (curMetadata == null) {
                // Not ready for this metadata yet
                return null;
            }
            detailsServiceMetadata.put(info.childType, curMetadata);
            // Register dependency with related entity
            registerDependency(info.childEntityMetadata.getId(), metadataIdentificationString);
        }
    }
    Map<String, RelationInfoExtended> relationInfos = new HashMap<String, RelationInfoExtended>();
    for (String fieldName : entityMetadata.getRelationInfos().keySet()) {
        RelationInfoExtended info = getControllerOperations().getRelationInfoFor(entityMetadata, fieldName).get(0);
        if (info.childEntityMetadata == null) {
            // Not ready for this metadata yet
            return null;
        }
        relationInfos.put(fieldName, info);
    }
    return new ControllerMetadata(metadataIdentificationString, aspectName, controllerValues, governorPhysicalTypeMetadata, entity, entityMetadata, service, path, baseUrl, type, serviceMetadata, detailAnnotaionFieldValue, detailsServiceMetadata, detailsFieldInfo, relationInfos);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ControllerType(org.springframework.roo.addon.web.mvc.controller.annotations.ControllerType) RooJavaType(org.springframework.roo.model.RooJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) ServiceMetadata(org.springframework.roo.addon.layers.service.addon.ServiceMetadata)

Example 92 with JavaType

use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.

the class ControllerMetadataProviderImpl method getEntityFromRelationField.

/**
 * Get the entity that represents a relation field
 *
 * @param relationField Array that represents the relation field.
 * @param entity Current entity to search the corresponding field
 * @param level Current array level to search
 * @return
 */
private JavaType getEntityFromRelationField(String[] relationField, JavaType entity, int level) {
    JavaType entityTypeToCalculate = null;
    MemberDetails memberDetails = getMemberDetails(entity);
    List<FieldMetadata> fields = memberDetails.getFields();
    for (FieldMetadata entityField : fields) {
        if (entityField.getFieldName().getSymbolName().equals(relationField[level])) {
            AnnotationMetadata oneToManyAnnotation = entityField.getAnnotation(JpaJavaType.ONE_TO_MANY);
            if (oneToManyAnnotation != null && (entityField.getFieldType().getFullyQualifiedTypeName().equals(JavaType.LIST.getFullyQualifiedTypeName()) || entityField.getFieldType().getFullyQualifiedTypeName().equals(JavaType.SET.getFullyQualifiedTypeName()))) {
                level++;
                if (relationField.length > level) {
                    entityTypeToCalculate = getEntityFromRelationField(relationField, entityField.getFieldType().getParameters().get(0), level);
                } else {
                    entityTypeToCalculate = entityField.getFieldType().getParameters().get(0);
                }
                break;
            }
        }
    }
    return entityTypeToCalculate;
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 93 with JavaType

use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.

the class ControllerMetadataProviderImpl method getLocalMidToRequest.

@Override
protected String getLocalMidToRequest(final ItdTypeDetails itdTypeDetails) {
    // Determine the governor for this ITD, and whether any metadata is even
    // hoping to hear about changes to that JavaType and its ITDs
    final JavaType governor = itdTypeDetails.getName();
    final String localMid = domainTypeToServiceMidMap.get(governor);
    if (localMid != null) {
        return localMid;
    }
    final MemberHoldingTypeDetails memberHoldingTypeDetails = getTypeLocationService().getTypeDetails(governor);
    if (memberHoldingTypeDetails != null) {
        for (final JavaType type : memberHoldingTypeDetails.getLayerEntities()) {
            final String localMidType = domainTypeToServiceMidMap.get(type);
            if (localMidType != null) {
                return localMidType;
            }
        }
    }
    return null;
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) MemberHoldingTypeDetails(org.springframework.roo.classpath.details.MemberHoldingTypeDetails)

Example 94 with JavaType

use of org.springframework.roo.model.JavaType 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 95 with JavaType

use of org.springframework.roo.model.JavaType 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

JavaType (org.springframework.roo.model.JavaType)411 RooJavaType (org.springframework.roo.model.RooJavaType)212 ArrayList (java.util.ArrayList)142 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)133 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)129 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)114 JdkJavaType (org.springframework.roo.model.JdkJavaType)110 SpringJavaType (org.springframework.roo.model.SpringJavaType)101 JpaJavaType (org.springframework.roo.model.JpaJavaType)83 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)78 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)76 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)72 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)70 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)65 LogicalPath (org.springframework.roo.project.LogicalPath)62 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)60 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)59 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)38 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)35 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)30