Search in sources :

Example 1 with RepositoryJpaMetadata

use of org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata in project spring-roo by spring-projects.

the class AbstractViewGenerationService method addListDeleteModalBatchView.

@Override
public void addListDeleteModalBatchView(String moduleName, JpaEntityMetadata entityMetadata, MemberDetails entity, ViewContext<T> ctx) {
    // Get the repository related with the entity to check the default return type
    RepositoryJpaMetadata repository = getRepositoryJpaLocator().getFirstRepositoryMetadata(entityMetadata.getAnnotatedEntity());
    // All views should have a repository
    Validate.notNull(repository, "ERROR: The provided entity should have an associated repository to be able " + "to generate the list view.");
    // Obtain the defaultReturnType
    JavaType defaultReturnType = repository.getDefaultReturnType();
    // The defaultReturnType must not be null. If it's not an entity projection,
    // it must be an entity
    Validate.notNull(defaultReturnType, "ERROR: The repository associated to the provided entity should define a defaultReturnType");
    // Obtain details of the provided defaultReturnType. If not exists as type, show an error
    ClassOrInterfaceTypeDetails defaultReturnTypeCid = getTypeLocationService().getTypeDetails(defaultReturnType);
    Validate.notNull(defaultReturnTypeCid, "ERROR: The provided defaultReturnType is not a valid type");
    MemberDetails defaultReturnTypeDetails = getMemberDetailsScanner().getMemberDetails(getClass().toString(), defaultReturnTypeCid);
    Validate.notNull(defaultReturnTypeDetails, "ERROR: Is not possible to obtain any detail from the " + "provided defaultReturnType.");
    List<FieldMetadata> defaultReturnTypeFields = defaultReturnTypeDetails.getFields();
    // all the entity fields will be used
    if (defaultReturnTypeFields.isEmpty()) {
        defaultReturnTypeFields = entity.getFields();
    }
    // Getting entity fields that should be included on view
    List<FieldMetadata> entityFields = getPersistentFields(defaultReturnTypeFields);
    List<FieldItem> fields = getFieldViewItems(entityMetadata, entityFields, ctx.getEntityName(), true, ctx, TABLE_SUFFIX);
    // Process elements to generate
    DOC newDoc = null;
    // Getting new viewName
    String viewName = getViewsFolder(moduleName).concat(ctx.getControllerPath()).concat("/").concat("/listDeleteModalBatch").concat(getViewsExtension());
    EntityItem entityItem = createEntityItem(entityMetadata, ctx, TABLE_SUFFIX);
    // Check if new view to generate exists or not
    if (existsFile(viewName)) {
        DOC existingDoc = loadExistingDoc(viewName);
        if (!isUserManagedDocument(existingDoc)) {
            newDoc = mergeListDeleteModalBatchView("listDeleteModalBatch", loadExistingDoc(viewName), ctx, entityItem, fields);
        }
    } else {
        ctx.addExtraParameter("entity", entityItem);
        ctx.addExtraParameter("fields", fields);
        newDoc = process("listDeleteModalBatch", ctx);
    }
    // Write newDoc on disk
    writeDoc(newDoc, viewName);
}
Also used : RepositoryJpaMetadata(org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata) Jsr303JavaType(org.springframework.roo.model.Jsr303JavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) FieldItem(org.springframework.roo.addon.web.mvc.views.components.FieldItem) EntityItem(org.springframework.roo.addon.web.mvc.views.components.EntityItem) DetailEntityItem(org.springframework.roo.addon.web.mvc.views.components.DetailEntityItem)

Example 2 with RepositoryJpaMetadata

use of org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata in project spring-roo by spring-projects.

the class AbstractViewGenerationService method addListDeleteModalDetailBatchView.

@Override
public void addListDeleteModalDetailBatchView(String moduleName, JpaEntityMetadata entityMetadata, MemberDetails entity, ControllerMetadata controllerMetadata, ViewContext<T> ctx) {
    // Get the repository related with the entity to check the default return type
    RepositoryJpaMetadata repository = getRepositoryJpaLocator().getFirstRepositoryMetadata(entityMetadata.getAnnotatedEntity());
    // All views should have a repository
    Validate.notNull(repository, "ERROR: The provided entity should have an associated repository to be able " + "to generate the list view.");
    // Obtain the defaultReturnType
    JavaType defaultReturnType = repository.getDefaultReturnType();
    // The defaultReturnType must not be null. If it's not an entity projection,
    // it must be an entity
    Validate.notNull(defaultReturnType, "ERROR: The repository associated to the provided entity should define a defaultReturnType");
    // Obtain details of the provided defaultReturnType. If not exists as type, show an error
    ClassOrInterfaceTypeDetails defaultReturnTypeCid = getTypeLocationService().getTypeDetails(defaultReturnType);
    Validate.notNull(defaultReturnTypeCid, "ERROR: The provided defaultReturnType is not a valid type");
    MemberDetails defaultReturnTypeDetails = getMemberDetailsScanner().getMemberDetails(getClass().toString(), defaultReturnTypeCid);
    Validate.notNull(defaultReturnTypeDetails, "ERROR: Is not possible to obtain any detail from the " + "provided defaultReturnType.");
    List<FieldMetadata> defaultReturnTypeFields = defaultReturnTypeDetails.getFields();
    // all the entity fields will be used
    if (defaultReturnTypeFields.isEmpty()) {
        defaultReturnTypeFields = entity.getFields();
    }
    // Getting entity fields that should be included on view
    List<FieldMetadata> entityFields = getPersistentFields(defaultReturnTypeFields);
    List<FieldItem> fields = getFieldViewItems(entityMetadata, entityFields, ctx.getEntityName(), true, ctx, TABLE_SUFFIX);
    // Process elements to generate
    DOC newDoc = null;
    // Getting new viewName
    String viewName = getViewsFolder(moduleName).concat(ctx.getControllerPath()).concat("/").concat(controllerMetadata.getDetailsPathAsString("/")).concat("/listDeleteModalBatch").concat(getViewsExtension());
    EntityItem entityItem = createEntityItem(entityMetadata, ctx, TABLE_SUFFIX);
    // Check if new view to generate exists or not
    if (existsFile(viewName)) {
        DOC existingDoc = loadExistingDoc(viewName);
        if (!isUserManagedDocument(existingDoc)) {
            newDoc = mergeListDeleteModalBatchDetailView("listDeleteModalBatchDetail", loadExistingDoc(viewName), ctx, entityItem, fields);
        }
    } else {
        ctx.addExtraParameter("entity", entityItem);
        ctx.addExtraParameter("fields", fields);
        newDoc = process("listDeleteModalBatchDetail", ctx);
    }
    // Write newDoc on disk
    writeDoc(newDoc, viewName);
}
Also used : RepositoryJpaMetadata(org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata) Jsr303JavaType(org.springframework.roo.model.Jsr303JavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) FieldItem(org.springframework.roo.addon.web.mvc.views.components.FieldItem) EntityItem(org.springframework.roo.addon.web.mvc.views.components.EntityItem) DetailEntityItem(org.springframework.roo.addon.web.mvc.views.components.DetailEntityItem)

Example 3 with RepositoryJpaMetadata

use of org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata in project spring-roo by spring-projects.

the class AbstractViewGenerationService method addListView.

@Override
public void addListView(String moduleName, JpaEntityMetadata entityMetadata, MemberDetails entity, List<T> detailsControllers, ViewContext<T> ctx) {
    // Get the repository related with the entity to check the default return type
    RepositoryJpaMetadata repository = getRepositoryJpaLocator().getFirstRepositoryMetadata(entityMetadata.getAnnotatedEntity());
    // All views should have a repository
    Validate.notNull(repository, "ERROR: The provided entity should have an associated repository to be able " + "to generate the list view.");
    // Obtain the defaultReturnType
    JavaType defaultReturnType = repository.getDefaultReturnType();
    // The defaultReturnType must not be null. If it's not an entity projection,
    // it must be an entity
    Validate.notNull(defaultReturnType, "ERROR: The repository associated to the provided entity should define a defaultReturnType");
    // Obtain details of the provided defaultReturnType. If not exists as type, show an error
    ClassOrInterfaceTypeDetails defaultReturnTypeCid = getTypeLocationService().getTypeDetails(defaultReturnType);
    Validate.notNull(defaultReturnTypeCid, "ERROR: The provided defaultReturnType is not a valid type");
    MemberDetails defaultReturnTypeDetails = getMemberDetailsScanner().getMemberDetails(getClass().toString(), defaultReturnTypeCid);
    Validate.notNull(defaultReturnTypeDetails, "ERROR: Is not possible to obtain any detail from the " + "provided defaultReturnType.");
    List<FieldMetadata> defaultReturnTypeFields = defaultReturnTypeDetails.getFields();
    // all the entity fields will be used
    if (defaultReturnTypeFields.isEmpty()) {
        defaultReturnTypeFields = entity.getFields();
    }
    // Getting entity fields that should be included on view
    List<FieldMetadata> entityFields = getPersistentFields(defaultReturnTypeFields);
    List<FieldItem> fields = getFieldViewItems(entityMetadata, entityFields, ctx.getEntityName(), true, ctx, TABLE_SUFFIX);
    // Process elements to generate
    DOC newDoc = null;
    // Getting new viewName
    String viewName = getViewsFolder(moduleName).concat(ctx.getControllerPath()).concat("/").concat("/list").concat(getViewsExtension());
    EntityItem entityItem = createEntityItem(entityMetadata, ctx, TABLE_SUFFIX);
    List<List<DetailEntityItem>> detailsLevels = new ArrayList<List<DetailEntityItem>>();
    if (detailsControllers != null && !detailsControllers.isEmpty()) {
        List<DetailEntityItem> details = new ArrayList<DetailEntityItem>();
        for (T detailController : detailsControllers) {
            DetailEntityItem detailItem = createDetailEntityItem(detailController, entity, entityMetadata, ctx.getEntityName(), ctx, DETAIL_SUFFIX, entityItem);
            details.add(detailItem);
        }
        // Sort details by path
        Collections.sort(details, new Comparator<DetailEntityItem>() {

            @Override
            public int compare(DetailEntityItem o1, DetailEntityItem o2) {
                return o1.getPathString().compareTo(o2.getPathString());
            }
        });
        // levels
        for (DetailEntityItem detail : details) {
            // Create group until item level
            while (detailsLevels.size() < detail.getLevel()) {
                detailsLevels.add(new ArrayList<DetailEntityItem>());
            }
            // Include detail in its group
            detailsLevels.get(detail.getLevel() - 1).add(detail);
            if (detail.getLevel() < 1) {
                // Nothing more to do with detail
                continue;
            }
            // look for parent
            for (DetailEntityItem parent : details) {
                if (detail.isTheParentEntity(parent)) {
                    // set parent
                    detail.setParentEntity(parent);
                    break;
                }
            }
        }
    }
    // Check if new view to generate exists or not
    if (existsFile(viewName)) {
        DOC existingDoc = loadExistingDoc(viewName);
        if (!isUserManagedDocument(existingDoc)) {
            newDoc = mergeListView("list", existingDoc, ctx, entityItem, fields, detailsLevels);
        }
    } else {
        ctx.addExtraParameter("entity", entityItem);
        ctx.addExtraParameter("fields", fields);
        ctx.addExtraParameter("detailsLevels", detailsLevels);
        newDoc = process("list", ctx);
    }
    // Write newDoc on disk
    writeDoc(newDoc, viewName);
}
Also used : RepositoryJpaMetadata(org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) Jsr303JavaType(org.springframework.roo.model.Jsr303JavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) DetailEntityItem(org.springframework.roo.addon.web.mvc.views.components.DetailEntityItem) List(java.util.List) ArrayList(java.util.ArrayList) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) FieldItem(org.springframework.roo.addon.web.mvc.views.components.FieldItem) EntityItem(org.springframework.roo.addon.web.mvc.views.components.EntityItem) DetailEntityItem(org.springframework.roo.addon.web.mvc.views.components.DetailEntityItem)

Example 4 with RepositoryJpaMetadata

use of org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata in project spring-roo by spring-projects.

the class RepositoryJpaIntegrationTestMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final RepositoryJpaIntegrationTestAnnotationValues annotationValues = new RepositoryJpaIntegrationTestAnnotationValues(governorPhysicalTypeMetadata);
    // Find SpringJpaDataDetachableConfiguration class
    Set<JavaType> jpaConfigurationClasses = getTypeLocationService().findTypesWithAnnotation(RooJavaType.ROO_JPA_REPOSITORY_CONFIGURATION);
    Validate.isTrue(!jpaConfigurationClasses.isEmpty(), "Couldn't find the 'SpringDataJpaDetachableRepositoryConfiguration' on the project for '%s'", this.getClass().getName());
    JavaType jpaDetachableRepositoryClass = jpaConfigurationClasses.iterator().next();
    // Get repository metadata
    JavaType repositoryInterface = annotationValues.getTargetClass();
    ClassOrInterfaceTypeDetails repositoryDetails = getTypeLocationService().getTypeDetails(repositoryInterface);
    Validate.notNull(repositoryDetails.getAnnotation(RooJavaType.ROO_REPOSITORY_JPA), "Couldn't find @RooJpaRepository in '%s'", repositoryInterface.getSimpleTypeName());
    String repositoryMetadataId = RepositoryJpaMetadata.createIdentifier(repositoryDetails);
    RepositoryJpaMetadata repositoryMetadata = getMetadataService().get(repositoryMetadataId);
    if (repositoryMetadata == null) {
        return null;
    }
    // Get entity identifier info
    JavaType entity = repositoryMetadata.getEntity();
    JavaType identifierType = getPersistenceMemberLocator().getIdentifierType(entity);
    JavaSymbolName identifierAccessorMethodName = getPersistenceMemberLocator().getIdentifierAccessor(entity).getMethodName();
    // Get entity metadata
    ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    String jpaEntityMetadataId = JpaEntityMetadata.createIdentifier(entityDetails);
    JpaEntityMetadata jpaEntityMetadata = getMetadataService().get(jpaEntityMetadataId);
    if (jpaEntityMetadata == null) {
        return null;
    }
    // Get entity read only
    boolean isReadOnly = jpaEntityMetadata.isReadOnly();
    // Get entity plural
    String entityPlural = getPluralService().getPlural(entity);
    // Get repository default return type
    JavaType defaultReturnType = repositoryMetadata.getDefaultReturnType();
    return new RepositoryJpaIntegrationTestMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, jpaDetachableRepositoryClass, identifierType, identifierAccessorMethodName, entityPlural, entity, defaultReturnType, isReadOnly);
}
Also used : RepositoryJpaMetadata(org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata) JavaType(org.springframework.roo.model.JavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)

Example 5 with RepositoryJpaMetadata

use of org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata 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

RepositoryJpaMetadata (org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata)12 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)12 JavaType (org.springframework.roo.model.JavaType)11 RooJavaType (org.springframework.roo.model.RooJavaType)11 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)7 DetailEntityItem (org.springframework.roo.addon.web.mvc.views.components.DetailEntityItem)6 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)6 JdkJavaType (org.springframework.roo.model.JdkJavaType)6 JpaJavaType (org.springframework.roo.model.JpaJavaType)6 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)6 SpringJavaType (org.springframework.roo.model.SpringJavaType)6 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)6 EntityItem (org.springframework.roo.addon.web.mvc.views.components.EntityItem)5 FieldItem (org.springframework.roo.addon.web.mvc.views.components.FieldItem)5 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)2 PartTree (org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.PartTree)2