Search in sources :

Example 76 with AnnotatedJavaType

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

the class RepositoryJpaCustomMetadata method getFindAllByIdsInGlobalSearchMethod.

/**
 * Method that generates the findAllByIdsIn method on current interface.
 *
 * @return
 */
private MethodMetadata getFindAllByIdsInGlobalSearchMethod() {
    // Define method parameter types and parameter names
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    // Identifiers parameter
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(JavaType.wrapperOf(JavaType.LIST, identifierType)));
    parameterNames.add(new JavaSymbolName("ids"));
    // Global search parameter
    parameterTypes.add(GLOBAL_SEARCH_PARAMETER);
    parameterNames.add(GOBAL_SEARCH_PARAMETER_NAME);
    // Pageable parameter
    parameterTypes.add(PAGEABLE_PARAMETER);
    parameterNames.add(PAGEABLE_PARAMETER_NAME);
    // Method name
    JavaSymbolName methodName = new JavaSymbolName("findAllByIdsIn");
    // Return type
    JavaType returnType = new JavaType("org.springframework.data.domain.Page", 0, DataType.TYPE, null, Arrays.asList(defaultReturnType));
    // Use the MethodMetadataBuilder for easy creation of MethodMetadata
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, methodName, returnType, parameterTypes, parameterNames, null);
    // Build and return a MethodMetadata
    return methodBuilder.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JavaType(org.springframework.roo.model.JavaType) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList)

Example 77 with AnnotatedJavaType

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

the class RepositoryJpaCustomMetadata method getCustomCount.

/**
 * Method that generates count methods for custom finders.
 *
 * @param formBean the object containing the properties to search to
 * @param javaSymbolName the finder name
 * @return
 */
private MethodMetadata getCustomCount(JavaType formBean, JavaSymbolName finderName) {
    // Define method parameter types and parameter names
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(formBean));
    List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.add(new JavaSymbolName("formBean"));
    // Create count method name
    String countName = finderName.getSymbolName();
    if (StringUtils.startsWith(countName, "find")) {
        countName = StringUtils.removeStart(countName, "find");
    } else if (StringUtils.startsWith(countName, "query")) {
        countName = StringUtils.removeStart(countName, "query");
    } else if (StringUtils.startsWith(countName, "read")) {
        countName = StringUtils.removeStart(countName, "read");
    }
    countName = "count".concat(countName);
    JavaSymbolName countMethodName = new JavaSymbolName(countName);
    // Use the MethodMetadataBuilder for easy creation of MethodMetadata
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, countMethodName, JavaType.LONG_PRIMITIVE, parameterTypes, parameterNames, null);
    // Build and return a MethodMetadata
    return methodBuilder.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList)

Example 78 with AnnotatedJavaType

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

the class ServiceImplMetadata method getMethod.

/**
 * Method that generates implementation of provided method
 *
 * @return MethodMetadataBuilder
 */
private MethodMetadata getMethod(final MethodMetadata methodToBeImplemented, boolean isTransactional) {
    // Check if is batch method
    boolean isBatch = false;
    for (AnnotatedJavaType parameters : methodToBeImplemented.getParameterTypes()) {
        JavaType type = parameters.getJavaType();
        isBatch = !type.getParameters().isEmpty();
        if (isBatch) {
            break;
        }
    }
    // Check what is the particular method
    boolean isDelete = methodToBeImplemented.getMethodName().getSymbolName().equals("delete");
    boolean isSaveMethod = methodToBeImplemented.equals(this.serviceMetadata.getCurrentSaveMethod());
    boolean isFindOneForUpdate = methodToBeImplemented.getMethodName().getSymbolName().equals("findOneForUpdate");
    InvocableMemberBodyBuilder bodyBuilder;
    if (isDelete) {
        bodyBuilder = builDeleteMethodBody(methodToBeImplemented, isBatch);
    } else if (isSaveMethod) {
        bodyBuilder = builSaveMethodBody(methodToBeImplemented);
    } else if (isFindOneForUpdate) {
        bodyBuilder = buildFindOneForUpdateBody(methodToBeImplemented);
    } else {
        bodyBuilder = builMethodBody(methodToBeImplemented);
    }
    return getMethod(methodToBeImplemented, isTransactional, bodyBuilder);
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) JavaType(org.springframework.roo.model.JavaType) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Example 79 with AnnotatedJavaType

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

the class ServiceImplMetadata method getEntityTypeGetterMethod.

/**
 * Builds a method which returns the class of entity JavaType.
 *
 * @return MethodMetadataBuilder
 */
private MethodMetadata getEntityTypeGetterMethod() {
    List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    MethodMetadata existingMethod = getGovernorMethod(GET_ENTITY_TYPE_METHOD_NAME, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // return ENTITY_TYPE.class;
    bodyBuilder.appendFormalLine("return %s.class;", this.entity.getNameIncludingTypeParameters(false, importResolver));
    // Use the MethodMetadataBuilder for easy creation of MethodMetadata
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, GET_ENTITY_TYPE_METHOD_NAME, JavaType.wrapperOf(JavaType.CLASS, this.entity), parameterTypes, parameterNames, bodyBuilder);
    // Build and return a MethodMetadata instance
    return methodBuilder.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Example 80 with AnnotatedJavaType

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

the class ServiceImplMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final ServiceImplAnnotationValues annotationValues = new ServiceImplAnnotationValues(governorPhysicalTypeMetadata);
    // Getting service interface
    JavaType serviceInterface = annotationValues.getService();
    ClassOrInterfaceTypeDetails serviceInterfaceDetails = getTypeLocationService().getTypeDetails(serviceInterface);
    final ServiceMetadata serviceMetadata = getServiceMetadata(metadataIdentificationString, serviceInterfaceDetails);
    AnnotationMetadata serviceAnnotation = serviceInterfaceDetails.getAnnotation(RooJavaType.ROO_SERVICE);
    Validate.notNull(serviceAnnotation, "ERROR: Provided service should be annotated with @RooService");
    JavaType entity = (JavaType) serviceAnnotation.getAttribute("entity").getValue();
    ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    final String entityMetadataId = JpaEntityMetadata.createIdentifier(entityDetails.getType(), PhysicalTypeIdentifier.getPath(entityDetails.getDeclaredByMetadataId()));
    final JpaEntityMetadata entityMetadata = (JpaEntityMetadata) getMetadataService().get(entityMetadataId);
    // Add dependencies between modules
    if (serviceMetadata != null) {
        for (MethodMetadata method : serviceMetadata.getAllMethods()) {
            List<JavaType> types = new ArrayList<JavaType>();
            types.add(method.getReturnType());
            types.addAll(method.getReturnType().getParameters());
            for (AnnotatedJavaType parameter : method.getParameterTypes()) {
                types.add(AnnotatedJavaType.convertFromAnnotatedJavaType(parameter));
                types.addAll(AnnotatedJavaType.convertFromAnnotatedJavaType(parameter).getParameters());
            }
            for (JavaType parameter : types) {
                getTypeLocationService().addModuleDependency(governorPhysicalTypeMetadata.getType().getModule(), parameter);
            }
        }
    }
    // Getting associated repository
    ClassOrInterfaceTypeDetails repositoryDetails = getRepositoryJpaLocator().getRepository(entity);
    final String repositoryMetadataId = RepositoryJpaMetadata.createIdentifier(repositoryDetails.getType(), PhysicalTypeIdentifier.getPath(repositoryDetails.getDeclaredByMetadataId()));
    final RepositoryJpaMetadata repositoryMetadata = (RepositoryJpaMetadata) getMetadataService().get(repositoryMetadataId);
    // Locate related services API types required by relations
    Map<JavaType, ServiceMetadata> requiredServicesByEntity = new HashMap<JavaType, ServiceMetadata>();
    ClassOrInterfaceTypeDetails relatedService;
    for (RelationInfo info : entityMetadata.getRelationInfos().values()) {
        if (info.cardinality != Cardinality.ONE_TO_ONE && !requiredServicesByEntity.containsKey(info.childType)) {
            relatedService = getServiceLocator().getFirstService(info.childType);
            if (relatedService != null) {
                requiredServicesByEntity.put(info.childType, getServiceMetadata(metadataIdentificationString, relatedService));
            }
        }
    }
    // Get child relations info
    List<Pair<FieldMetadata, RelationInfo>> childRelationsInfo = getJpaOperations().getFieldChildPartOfRelation(entityDetails);
    return new ServiceImplMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, serviceInterface, repositoryDetails.getType(), repositoryMetadata, entity, entityMetadata, serviceMetadata, requiredServicesByEntity, childRelationsInfo);
}
Also used : RepositoryJpaMetadata(org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) RelationInfo(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) Pair(org.apache.commons.lang3.tuple.Pair)

Aggregations

AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)130 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)117 ArrayList (java.util.ArrayList)116 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)108 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)99 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)81 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)71 JavaType (org.springframework.roo.model.JavaType)63 SpringJavaType (org.springframework.roo.model.SpringJavaType)46 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)41 JdkJavaType (org.springframework.roo.model.JdkJavaType)28 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)25 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)23 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)18 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)16 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)15 RelationInfoExtended (org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended)12 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)10 Pair (org.apache.commons.lang3.tuple.Pair)7 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)7