use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.
the class ControllerMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
ControllerAnnotationValues controllerValues = new ControllerAnnotationValues(governorPhysicalTypeMetadata);
// Getting entity
final JavaType entity = controllerValues.getEntity();
ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
// Get entity metadata
final String entityMetadataId = JpaEntityMetadata.createIdentifier(entityDetails);
registerDependency(entityMetadataId, metadataIdentificationString);
final JpaEntityMetadata entityMetadata = getMetadataService().get(entityMetadataId);
// This metadata is not available yet.
if (entityMetadata == null) {
return null;
}
// Getting type
ControllerType type = controllerValues.getType();
// Getting related service
ClassOrInterfaceTypeDetails serviceDetails = getServiceLocator().getService(entity);
JavaType service = serviceDetails.getType();
final String serviceMetadataId = ServiceMetadata.createIdentifier(serviceDetails);
registerDependency(serviceMetadataId, metadataIdentificationString);
final ServiceMetadata serviceMetadata = getMetadataService().get(serviceMetadataId);
// This metadata is not available yet.
if (serviceMetadata == null) {
return null;
}
// Generate path
final String path = getControllerOperations().getBasePathForController(governorPhysicalTypeMetadata.getMemberHoldingTypeDetails());
final String baseUrl = getControllerOperations().getBaseUrlForController(governorPhysicalTypeMetadata.getMemberHoldingTypeDetails());
List<RelationInfoExtended> detailsFieldInfo = null;
String detailAnnotaionFieldValue = null;
Map<JavaType, ServiceMetadata> detailsServiceMetadata = null;
if (type == ControllerType.DETAIL || type == ControllerType.DETAIL_ITEM) {
final JavaType controller = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails().getName();
// Getting the relationField from @RooDetail entity
final AnnotationAttributeValue<Object> relationFieldAttr = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails().getAnnotation(RooJavaType.ROO_DETAIL).getAttribute("relationField");
Validate.notNull(relationFieldAttr, "ERROR: In %s controller, @RooDetail annotation must have relationField value", controller);
detailAnnotaionFieldValue = (String) relationFieldAttr.getValue();
Validate.isTrue(StringUtils.isNotBlank(detailAnnotaionFieldValue), "ERROR: In %s controller, @RooDetail annotation must have relationField value", controller);
// generate detail info object
detailsFieldInfo = getControllerOperations().getRelationInfoFor(entityMetadata, detailAnnotaionFieldValue);
detailsServiceMetadata = new TreeMap<JavaType, ServiceMetadata>();
for (RelationInfoExtended info : detailsFieldInfo) {
if (detailsServiceMetadata.containsKey(info.childType)) {
continue;
}
// Getting related service
ClassOrInterfaceTypeDetails detailsServiceDetails = getServiceLocator().getService(info.childType);
String detailServiceMetadataId = ServiceMetadata.createIdentifier(detailsServiceDetails);
registerDependency(detailServiceMetadataId, metadataIdentificationString);
ServiceMetadata curMetadata = getMetadataService().get(detailServiceMetadataId);
if (curMetadata == null) {
// Not ready for this metadata yet
return null;
}
detailsServiceMetadata.put(info.childType, curMetadata);
// Register dependency with related entity
registerDependency(info.childEntityMetadata.getId(), metadataIdentificationString);
}
}
Map<String, RelationInfoExtended> relationInfos = new HashMap<String, RelationInfoExtended>();
for (String fieldName : entityMetadata.getRelationInfos().keySet()) {
RelationInfoExtended info = getControllerOperations().getRelationInfoFor(entityMetadata, fieldName).get(0);
if (info.childEntityMetadata == null) {
// Not ready for this metadata yet
return null;
}
relationInfos.put(fieldName, info);
}
return new ControllerMetadata(metadataIdentificationString, aspectName, controllerValues, governorPhysicalTypeMetadata, entity, entityMetadata, service, path, baseUrl, type, serviceMetadata, detailAnnotaionFieldValue, detailsServiceMetadata, detailsFieldInfo, relationInfos);
}
use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.
the class ControllerMetadataProviderImpl method getEntityFromRelationField.
/**
* Get the entity that represents a relation field
*
* @param relationField Array that represents the relation field.
* @param entity Current entity to search the corresponding field
* @param level Current array level to search
* @return
*/
private JavaType getEntityFromRelationField(String[] relationField, JavaType entity, int level) {
JavaType entityTypeToCalculate = null;
MemberDetails memberDetails = getMemberDetails(entity);
List<FieldMetadata> fields = memberDetails.getFields();
for (FieldMetadata entityField : fields) {
if (entityField.getFieldName().getSymbolName().equals(relationField[level])) {
AnnotationMetadata oneToManyAnnotation = entityField.getAnnotation(JpaJavaType.ONE_TO_MANY);
if (oneToManyAnnotation != null && (entityField.getFieldType().getFullyQualifiedTypeName().equals(JavaType.LIST.getFullyQualifiedTypeName()) || entityField.getFieldType().getFullyQualifiedTypeName().equals(JavaType.SET.getFullyQualifiedTypeName()))) {
level++;
if (relationField.length > level) {
entityTypeToCalculate = getEntityFromRelationField(relationField, entityField.getFieldType().getParameters().get(0), level);
} else {
entityTypeToCalculate = entityField.getFieldType().getParameters().get(0);
}
break;
}
}
}
return entityTypeToCalculate;
}
use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.
the class ControllerMetadataProviderImpl method getLocalMidToRequest.
@Override
protected String getLocalMidToRequest(final ItdTypeDetails itdTypeDetails) {
// Determine the governor for this ITD, and whether any metadata is even
// hoping to hear about changes to that JavaType and its ITDs
final JavaType governor = itdTypeDetails.getName();
final String localMid = domainTypeToServiceMidMap.get(governor);
if (localMid != null) {
return localMid;
}
final MemberHoldingTypeDetails memberHoldingTypeDetails = getTypeLocationService().getTypeDetails(governor);
if (memberHoldingTypeDetails != null) {
for (final JavaType type : memberHoldingTypeDetails.getLayerEntities()) {
final String localMidType = domainTypeToServiceMidMap.get(type);
if (localMidType != null) {
return localMidType;
}
}
}
return null;
}
use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.
the class ControllerOperationsImpl method createDomainModelModule.
/**
* Create DomainModelModule.java class and adds it
* {@link RooDomainModelModule} annotation
*
* @param module
* the Pom where configuration classes should be installed
*/
private void createDomainModelModule(Pom module) {
// Create DomainModelModule.java class
JavaType domainModelModule = new JavaType(String.format("%s.config.jackson.DomainModelModule", getTypeLocationService().getTopLevelPackageForModule(module)), module.getModuleName());
Validate.notNull(domainModelModule.getModule(), "ERROR: Module name is required to generate a valid JavaType");
final String domainModelModuleIdentifier = getPathResolver().getCanonicalPath(domainModelModule.getModule(), Path.SRC_MAIN_JAVA, domainModelModule);
// Check if file already exists
if (!getFileManager().exists(domainModelModuleIdentifier)) {
// Creating class builder
final String mid = PhysicalTypeIdentifier.createIdentifier(domainModelModule, getPathResolver().getPath(domainModelModuleIdentifier));
final ClassOrInterfaceTypeDetailsBuilder typeBuilder = new ClassOrInterfaceTypeDetailsBuilder(mid, PUBLIC, domainModelModule, PhysicalTypeCategory.CLASS);
// Generating @RooDomainModelModule annotation
typeBuilder.addAnnotation(new AnnotationMetadataBuilder(RooJavaType.ROO_DOMAIN_MODEL_MODULE));
// Write new class disk
getTypeManagementService().createOrUpdateTypeOnDisk(typeBuilder.build());
}
}
use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.
the class ControllerOperationsImpl method createJsonDeserializersIfDontExists.
/**
* Create the Json Desearializer utility class (annotated
* with @RooDeserializer) for target Entity and its related entities if they
* aren't created yet.
*
* @param entity
* @param module
* @param controllerPackage
*/
private void createJsonDeserializersIfDontExists(JavaType currentEntity, String module, JavaPackage controllerPackage) {
List<JavaType> entitiesToCreateSerializers = getParentAndChildrenRelatedEntities(currentEntity);
// Check if already exists a serializer for each entity
Set<ClassOrInterfaceTypeDetails> allDeserializer = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_DESERIALIZER);
for (JavaType entity : entitiesToCreateSerializers) {
EntityDeserializerAnnotationValues values;
boolean deserializerFound = false;
for (ClassOrInterfaceTypeDetails deserializer : allDeserializer) {
values = new EntityDeserializerAnnotationValues(deserializer);
if (entity.equals(values.getEntity())) {
// Found mixing. Nothing to do.
deserializerFound = true;
}
}
if (!deserializerFound) {
// Not found deserializer. Create it
ClassOrInterfaceTypeDetails serviceDetails = getServiceLocator().getService(entity);
Validate.notNull(serviceDetails, "Can't found service for Entity %s to generate " + "Serializer. If it is a related entity with the one to generate " + "controller, it needs a service.", entity.getFullyQualifiedTypeName());
// Build @RooDeserializer
List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
annotations = new ArrayList<AnnotationMetadataBuilder>();
AnnotationMetadataBuilder deserializerAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_DESERIALIZER);
deserializerAnnotation.addClassAttribute("entity", entity);
annotations.add(deserializerAnnotation);
JavaType deserializerClass = new JavaType(String.format("%s.%sDeserializer", controllerPackage.getFullyQualifiedPackageName(), entity.getSimpleTypeName()), module);
final LogicalPath deserializerPath = getPathResolver().getPath(module, Path.SRC_MAIN_JAVA);
final String resourceIdentifierItem = getTypeLocationService().getPhysicalTypeCanonicalPath(deserializerClass, deserializerPath);
final String declaredByMetadataIdItem = PhysicalTypeIdentifier.createIdentifier(deserializerClass, getPathResolver().getPath(resourceIdentifierItem));
ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataIdItem, Modifier.PUBLIC, deserializerClass, PhysicalTypeCategory.CLASS);
cidBuilder.setAnnotations(annotations);
/*
* Moved extend to java (instead ITD) because there were
* compilation problems when Mixin
* uses @JsonDeserialize(using=EntityDeserializer.class)
* annotation (requires extend of JsonDeseralizer)
*/
cidBuilder.addExtendsTypes(JavaType.wrapperOf(JSON_OBJECT_DESERIALIZER, entity));
FieldMetadata serviceField = EntityDeserializerMetadata.getFieldFor(declaredByMetadataIdItem, serviceDetails.getType());
FieldMetadata conversionServiceField = EntityDeserializerMetadata.getFieldFor(declaredByMetadataIdItem, SpringJavaType.CONVERSION_SERVICE);
cidBuilder.addField(serviceField);
cidBuilder.addField(conversionServiceField);
ConstructorMetadata constructor = EntityDeserializerMetadata.getConstructor(declaredByMetadataIdItem, serviceField, conversionServiceField);
cidBuilder.addConstructor(constructor);
getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
}
}
}
Aggregations