use of org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.PartTree in project spring-roo by spring-projects.
the class ServiceMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
final ServiceAnnotationValues annotationValues = new ServiceAnnotationValues(governorPhysicalTypeMetadata);
// Getting entity
JavaType entity = annotationValues.getEntity();
Validate.notNull(entity, "ERROR: You should specify a valid entity on @RooService annotation");
final JavaType identifierType = getPersistenceMemberLocator().getIdentifierType(entity);
Validate.notNull(identifierType, "ERROR: You should specify a valid entity on @RooService annotation");
final JpaEntityMetadata entityMetadata = getEntityMetadata(entity);
if (entityMetadata == null) {
return null;
}
registerDependency(entityMetadata.getId(), metadataIdentificationString);
// Getting associated repository
ClassOrInterfaceTypeDetails repositoryDetails = getRepositoryJpaLocator().getRepository(entity);
// Check if we have a valid repository
Validate.notNull(repositoryDetails, String.format("ERROR: You must generate some @RooJpaRepository for entity '%s' to be able to generate services", entity.getSimpleTypeName()));
// Get repository metadata
final String repositoryMetadataKey = RepositoryJpaMetadata.createIdentifier(repositoryDetails);
registerDependency(repositoryMetadataKey, metadataIdentificationString);
final RepositoryJpaMetadata repositoryMetadata = getMetadataService().get(repositoryMetadataKey);
List<MethodMetadata> finders = new ArrayList<MethodMetadata>();
List<MethodMetadata> countMethods = new ArrayList<MethodMetadata>();
if (repositoryMetadata == null) {
// Can't generate metadata yet
return null;
}
// Add dependencies between modules
for (MethodMetadata finder : repositoryMetadata.getFindersGenerated()) {
// Add to service finders list
finders.add(finder);
registerDependencyModulesOfFinder(governorPhysicalTypeMetadata, finder);
}
// Get count methods
countMethods.addAll(repositoryMetadata.getCountMethods());
// Get finders and its associated count method from repository metadata
Map<JavaSymbolName, MethodMetadata> repositoryFindersAndCounts = repositoryMetadata.getFinderMethodsAndCounts();
// Getting methods declared on related RepositoryJpaCustomMetadata
final JavaType customRepository = repositoryMetadata.getCustomRepository();
final ClassOrInterfaceTypeDetails customRepositoryDetails = getTypeLocationService().getTypeDetails(customRepository);
final String customRepositoryMetadataKey = RepositoryJpaCustomMetadata.createIdentifier(customRepositoryDetails);
final RepositoryJpaCustomMetadata repositoryCustomMetadata = getMetadataService().get(customRepositoryMetadataKey);
// Return null if repository dependency is not satisfied
if (repositoryCustomMetadata == null) {
return null;
}
// Get finders and its associated count method from custom repository metadata
Map<JavaSymbolName, MethodMetadata> repositoryCustomFindersAndCounts = repositoryCustomMetadata.getFinderMethodsAndCounts();
// Check if we have a valid custom repository
Validate.notNull(repositoryCustomMetadata, String.format("ERROR: Can't found a class @RooJpaRepositoryCustom for entity '%s' to be able to generate services", entity.getSimpleTypeName()));
registerDependency(customRepositoryMetadataKey, metadataIdentificationString);
final Map<FieldMetadata, MethodMetadata> countByReferencedFieldMethods = new HashMap<FieldMetadata, MethodMetadata>(repositoryMetadata.getCountMethodByReferencedFields());
// Add custom finders to finders list and add dependencies between modules
for (Pair<MethodMetadata, PartTree> finderInfo : repositoryCustomMetadata.getCustomFinderMethods()) {
// Add to service finders list
finders.add(finderInfo.getKey());
registerDependencyModulesOfFinder(governorPhysicalTypeMetadata, finderInfo.getKey());
}
// Add custom count methods to count method list
for (Pair<MethodMetadata, PartTree> countInfo : repositoryCustomMetadata.getCustomCountMethods()) {
countMethods.add(countInfo.getKey());
}
// Get related entities metadata
final Map<JavaType, JpaEntityMetadata> relatedEntities = new HashMap<JavaType, JpaEntityMetadata>();
// As parent
JavaType childEntity;
JpaEntityMetadata childEntityMetadata;
List<RelationInfo> relatedInfosWithServiceLayer = new ArrayList<RelationInfo>();
for (RelationInfo info : entityMetadata.getRelationInfos().values()) {
childEntity = info.fieldMetadata.getFieldType().getBaseType();
if (relatedEntities.containsKey(childEntity)) {
continue;
}
// No need to add relation methods if related entity has not service
ClassOrInterfaceTypeDetails childService = getServiceLocator().getFirstService(childEntity);
if (childService != null) {
relatedInfosWithServiceLayer.add(info);
childEntityMetadata = getEntityMetadata(childEntity);
if (childEntityMetadata == null) {
// We need child metadata. Return null waiting next metadata iteration
return null;
}
registerDependency(childEntityMetadata.getId(), metadataIdentificationString);
relatedEntities.put(childEntity, childEntityMetadata);
}
}
// As child
JavaType parentEntity;
JpaEntityMetadata parentEntityMetadata;
for (FieldMetadata fieldAsChild : entityMetadata.getRelationsAsChild().values()) {
parentEntity = fieldAsChild.getFieldType().getBaseType();
if (relatedEntities.containsKey(parentEntity)) {
continue;
}
parentEntityMetadata = getEntityMetadata(parentEntity);
if (parentEntityMetadata == null) {
// We need parent metadata. Return null waiting next metadata iteration
return null;
}
registerDependency(parentEntityMetadata.getId(), metadataIdentificationString);
relatedEntities.put(parentEntity, parentEntityMetadata);
}
return new ServiceMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, entity, identifierType, entityMetadata, repositoryMetadata, finders, repositoryCustomMetadata.getCurrentFindAllGlobalSearchMethod(), repositoryCustomMetadata.getCurrentFindAllByIdsInGlobalSearchMethod(), repositoryCustomMetadata.getReferencedFieldsFindAllMethods(), countByReferencedFieldMethods, countMethods, relatedEntities, repositoryFindersAndCounts, repositoryCustomFindersAndCounts, relatedInfosWithServiceLayer);
}
Aggregations