protected ItdTypeDetailsProvidingMetadataItem getMetadata(
final String metadataIdentificationString,
final JavaType aspectName,
final PhysicalTypeMetadata governorPhysicalTypeMetadata,
final String itdFilename) {
final ClassOrInterfaceTypeDetails serviceClass = governorPhysicalTypeMetadata
.getMemberHoldingTypeDetails();
if (serviceClass == null) {
return null;
}
ServiceInterfaceMetadata serviceInterfaceMetadata = null;
ClassOrInterfaceTypeDetails serviceInterface = null;
for (final JavaType implementedType : serviceClass.getImplementsTypes()) {
final ClassOrInterfaceTypeDetails potentialServiceInterfaceTypeDetails = typeLocationService
.getTypeDetails(implementedType);
if (potentialServiceInterfaceTypeDetails != null) {
final LogicalPath path = PhysicalTypeIdentifier
.getPath(potentialServiceInterfaceTypeDetails
.getDeclaredByMetadataId());
final String implementedTypeId = ServiceInterfaceMetadata
.createIdentifier(implementedType, path);
if ((serviceInterfaceMetadata = (ServiceInterfaceMetadata) metadataService
.get(implementedTypeId)) != null) {
// Found the metadata for the service interface
serviceInterface = potentialServiceInterfaceTypeDetails;
break;
}
}
}
if (serviceInterface == null || serviceInterfaceMetadata == null
|| !serviceInterfaceMetadata.isValid()) {
return null;
}
// Register this provider for changes to the service interface // TODO
// move this down in case we return null early below?
metadataDependencyRegistry.registerDependency(
serviceInterfaceMetadata.getId(), metadataIdentificationString);
final ServiceAnnotationValues serviceAnnotationValues = serviceInterfaceMetadata
.getServiceAnnotationValues();
final JavaType[] domainTypes = serviceAnnotationValues.getDomainTypes();
if (domainTypes == null) {
return null;
}
/*
* For each domain type, collect (1) the plural and (2) the additions to
* make to the service class for calling a lower layer when implementing
* each service layer method. We use LinkedHashMaps for the latter
* nested map to ensure repeatable order of code generation.
*/
final Map<JavaType, String> domainTypePlurals = new HashMap<JavaType, String>();
final Map<JavaType, JavaType> domainTypeToIdTypeMap = new HashMap<JavaType, JavaType>();
// Collect the additions for each method for each supported domain type
final Map<JavaType, Map<ServiceLayerMethod, MemberTypeAdditions>> allCrudAdditions = new LinkedHashMap<JavaType, Map<ServiceLayerMethod, MemberTypeAdditions>>();
for (final JavaType domainType : domainTypes) {
final JavaType idType = persistenceMemberLocator
.getIdentifierType(domainType);
if (idType == null) {
return null;
}
domainTypeToIdTypeMap.put(domainType, idType);
// Collect the plural for this domain type
final ClassOrInterfaceTypeDetails domainTypeDetails = typeLocationService
.getTypeDetails(domainType);
if (domainTypeDetails == null) {
return null;
}
final LogicalPath path = PhysicalTypeIdentifier
.getPath(domainTypeDetails.getDeclaredByMetadataId());
final String pluralId = PluralMetadata.createIdentifier(domainType,
path);
final PluralMetadata pluralMetadata = (PluralMetadata) metadataService
.get(pluralId);
if (pluralMetadata == null) {
return null;
}
domainTypePlurals.put(domainType, pluralMetadata.getPlural());
// Maintain a list of entities that are being handled by this layer
managedEntityTypes.put(domainType, metadataIdentificationString);
// Collect the additions the service class needs in order to invoke
// each service layer method
final Map<ServiceLayerMethod, MemberTypeAdditions> methodAdditions = new LinkedHashMap<ServiceLayerMethod, MemberTypeAdditions>();
for (final ServiceLayerMethod method : ServiceLayerMethod.values()) {
final Collection<MethodParameter> methodParameters = MethodParameter
.asList(method.getParameters(domainType, idType));
final MemberTypeAdditions memberTypeAdditions = layerService
.getMemberTypeAdditions(metadataIdentificationString,
method.getKey(), domainType, idType,
LAYER_POSITION, methodParameters);
if (memberTypeAdditions != null) {
// A lower layer implements this method
methodAdditions.put(method, memberTypeAdditions);
}
}
allCrudAdditions.put(domainType, methodAdditions);
// Register this provider for changes to the domain type or its
// plural
metadataDependencyRegistry.registerDependency(
domainTypeDetails.getDeclaredByMetadataId(),
metadataIdentificationString);
metadataDependencyRegistry.registerDependency(pluralId,
metadataIdentificationString);
}