use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata in project spring-roo by spring-projects.
the class JpaDataOnDemandCreator method getEntityAndRelatedEntitiesList.
/**
* Searches the related entities of provided entity and returns a
* {@link List} with all the related entities plus the provided entity.
*
* @param entity
* the entity JavaType to search for its related entities.
* @return a List with all the related entities.
*/
private List<JavaType> getEntityAndRelatedEntitiesList(JavaType entity) {
ClassOrInterfaceTypeDetails entityDetails = getEntityDetails(entity);
JpaEntityMetadata entityMetadata = metadataService.get(JpaEntityMetadata.createIdentifier(entityDetails));
List<JavaType> entitiesToCreateFactories = new ArrayList<JavaType>();
entitiesToCreateFactories.add(entity);
// Get related child entities
for (RelationInfo info : entityMetadata.getRelationInfos().values()) {
// Add to list
if (!entitiesToCreateFactories.contains(info.childType)) {
entitiesToCreateFactories.add(info.childType);
}
}
return entitiesToCreateFactories;
}
use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata in project spring-roo by spring-projects.
the class JpaOperationsImpl method getJpaEntityMetadata.
/**
* Gets JpaEntityMetadata by a javaType
* @param domainType
* @return
*/
private JpaEntityMetadata getJpaEntityMetadata(ClassOrInterfaceTypeDetails domainTypeDetails) {
final String entityMetadataId = JpaEntityMetadata.createIdentifier(domainTypeDetails);
JpaEntityMetadata entityMetadata = getMetadataService().get(entityMetadataId);
return entityMetadata;
}
use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata in project spring-roo by spring-projects.
the class JpaOperationsImpl method getFieldChildPartOfRelation.
@Override
public List<Pair<FieldMetadata, RelationInfo>> getFieldChildPartOfRelation(ClassOrInterfaceTypeDetails entityCdi) {
JavaType domainType = entityCdi.getType();
JpaEntityMetadata entityMetadata = getJpaEntityMetadata(entityCdi);
Validate.notNull(entityMetadata, "%s should be a Jpa Entity", domainType);
Map<String, FieldMetadata> relations = entityMetadata.getRelationsAsChild();
List<Pair<FieldMetadata, RelationInfo>> childRelations = new ArrayList<Pair<FieldMetadata, RelationInfo>>();
JpaEntityMetadata parent;
JavaType parentType;
RelationInfo info;
for (Entry<String, FieldMetadata> fieldEntry : relations.entrySet()) {
parentType = fieldEntry.getValue().getFieldType().getBaseType();
parent = getJpaEntityMetadata(parentType);
Validate.notNull(parent, "Can't get information about Entity %s which is declared as parent in %s.%s field", parentType, domainType, fieldEntry.getKey());
info = parent.getRelationInfosByMappedBy(domainType, fieldEntry.getKey());
if (info != null) {
childRelations.add(Pair.of(fieldEntry.getValue(), info));
}
}
return childRelations;
}
use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata in project spring-roo by spring-projects.
the class JpaUnitTestMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
final JpaUnitTestAnnotationValues annotationValues = new JpaUnitTestAnnotationValues(governorPhysicalTypeMetadata);
JavaType targetType = annotationValues.getTargetClass();
Validate.notNull(targetType, targetType.getSimpleTypeName().concat(" doesn't exist in the project."));
// Obtain target type child related entities
final ClassOrInterfaceTypeDetails cid = getTypeLocationService().getTypeDetails(targetType);
JpaEntityMetadata entityMetadata = getMetadataService().get(JpaEntityMetadata.createIdentifier(cid));
List<JavaType> relatedEntities = new ArrayList<JavaType>();
// Add the current entity first
relatedEntities.add(targetType);
Map<String, RelationInfo> relationInfos = entityMetadata.getRelationInfos();
for (RelationInfo relation : relationInfos.values()) {
// Get child entity type
JavaType childType = relation.childType;
if (!relatedEntities.contains(childType)) {
relatedEntities.add(childType);
}
}
// Get all entity factories for this entity and child related entities
Set<ClassOrInterfaceTypeDetails> entityFactories = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_JPA_ENTITY_FACTORY);
Map<JavaType, JavaType> entityAndItsFactoryMap = new TreeMap<JavaType, JavaType>();
for (ClassOrInterfaceTypeDetails entityFactory : entityFactories) {
JavaType entity = (JavaType) entityFactory.getAnnotation(RooJavaType.ROO_JPA_ENTITY_FACTORY).getAttribute("entity").getValue();
if (entity != null && relatedEntities.contains(entity)) {
entityAndItsFactoryMap.put(entity, entityFactory.getType());
}
}
return new JpaUnitTestMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, relationInfos.values(), entityAndItsFactoryMap);
}
use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata 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);
}
Aggregations