use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo in project spring-roo by spring-projects.
the class JSONMetadata method getAddToDetailsBatchMethod.
private MethodMetadata getAddToDetailsBatchMethod() {
RelationInfo detailsInfo = controllerMetadata.getLastDetailsInfo();
final ServiceMetadata detailsServiceMetadata = controllerMetadata.getServiceMetadataForEntity(detailsInfo.entityType);
final MethodMetadata addToMethod = detailsServiceMetadata.getAddToRelationMethods().get(detailsInfo);
final FieldMetadata detailsServiceField = controllerMetadata.getDetailsServiceFields(detailsInfo.entityType);
// Define methodName
final JavaSymbolName methodName = new JavaSymbolName(addToMethod.getMethodName().getSymbolName() + "Batch");
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(new AnnotatedJavaType(addToMethod.getParameterTypes().get(0).getJavaType(), ANN_MODEL_ATTRIBUTE));
parameterTypes.add(new AnnotatedJavaType(addToMethod.getParameterTypes().get(1).getJavaType(), ANN_REQUEST_BODY));
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @PostMapping annotation
AnnotationMetadataBuilder postMappingAnnotation = new AnnotationMetadataBuilder(POST_MAPPING);
postMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
postMappingAnnotation.addStringAttribute("value", "/batch");
annotations.add(postMappingAnnotation);
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.addAll(addToMethod.getParameterNames());
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// customerService.addToOrders(customer, order);
bodyBuilder.appendFormalLine("%s().%s(%s,%s);", getAccessorMethod(detailsServiceField).getMethodName(), addToMethod.getMethodName(), addToMethod.getParameterNames().get(0), addToMethod.getParameterNames().get(1));
// return ResponseEntity.ok().build();
bodyBuilder.appendFormalLine("return %s.ok().build();", getNameOfJavaType(RESPONSE_ENTITY));
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.wrapperWilcard(RESPONSE_ENTITY), parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
return methodBuilder.build();
}
use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo in project spring-roo by spring-projects.
the class JSONMetadata method getRemoveFromDetailsMethod.
private MethodMetadata getRemoveFromDetailsMethod() {
RelationInfo detailsInfo = controllerMetadata.getLastDetailsInfo();
final ServiceMetadata detailsServiceMetadata = controllerMetadata.getServiceMetadataForEntity(detailsInfo.entityType);
final MethodMetadata removeFromMethod = detailsServiceMetadata.getRemoveFromRelationMethods().get(detailsInfo);
final FieldMetadata detailsServiceField = controllerMetadata.getDetailsServiceFields(detailsInfo.entityType);
// Define methodName
final JavaSymbolName methodName = removeFromMethod.getMethodName();
JavaSymbolName itemsName = removeFromMethod.getParameterNames().get(1);
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(new AnnotatedJavaType(removeFromMethod.getParameterTypes().get(0).getJavaType(), ANN_MODEL_ATTRIBUTE));
AnnotationMetadataBuilder pathVariableAnnotation = new AnnotationMetadataBuilder(SpringJavaType.PATH_VARIABLE);
pathVariableAnnotation.addStringAttribute("value", itemsName.getSymbolName());
parameterTypes.add(new AnnotatedJavaType(removeFromMethod.getParameterTypes().get(1).getJavaType().getParameters().get(0), pathVariableAnnotation.build()));
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @DeleteMapping annotation
AnnotationMetadataBuilder postMappingAnnotation = new AnnotationMetadataBuilder(DELETE_MAPPING);
postMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
annotations.add(postMappingAnnotation);
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.addAll(removeFromMethod.getParameterNames());
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// customerService.addToOrders(customer, Collections.singleton(order));
bodyBuilder.appendFormalLine("%s().%s(%s,%s.singleton(%s));", getAccessorMethod(detailsServiceField).getMethodName(), removeFromMethod.getMethodName(), removeFromMethod.getParameterNames().get(0), getNameOfJavaType(JavaType.COLLECTIONS), itemsName);
// return ResponseEntity.ok().build();
bodyBuilder.appendFormalLine("return %s.ok().build();", getNameOfJavaType(RESPONSE_ENTITY));
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.wrapperWilcard(RESPONSE_ENTITY), parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
return methodBuilder.build();
}
use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo in project spring-roo by spring-projects.
the class JSONMetadata method getRemoveFromDetailsBatchMethod.
private MethodMetadata getRemoveFromDetailsBatchMethod() {
RelationInfo detailsInfo = controllerMetadata.getLastDetailsInfo();
final ServiceMetadata detailsServiceMetadata = controllerMetadata.getServiceMetadataForEntity(detailsInfo.entityType);
final MethodMetadata removeFromDetailsMethod = detailsServiceMetadata.getRemoveFromRelationMethods().get(detailsInfo);
final FieldMetadata detailsServiceField = controllerMetadata.getDetailsServiceFields(detailsInfo.entityType);
// Define methodName
final JavaSymbolName methodName = new JavaSymbolName(removeFromDetailsMethod.getMethodName().getSymbolName() + "Batch");
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(new AnnotatedJavaType(removeFromDetailsMethod.getParameterTypes().get(0).getJavaType(), ANN_MODEL_ATTRIBUTE));
parameterTypes.add(new AnnotatedJavaType(removeFromDetailsMethod.getParameterTypes().get(1).getJavaType(), ANN_REQUEST_BODY));
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @DeleteMapping annotation
AnnotationMetadataBuilder postMappingAnnotation = new AnnotationMetadataBuilder(DELETE_MAPPING);
postMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
postMappingAnnotation.addStringAttribute("value", "/batch");
annotations.add(postMappingAnnotation);
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.addAll(removeFromDetailsMethod.getParameterNames());
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// customerService.removeFromOrders(customer, orders);
bodyBuilder.appendFormalLine("%s().%s(%s,%s);", getAccessorMethod(detailsServiceField).getMethodName(), removeFromDetailsMethod.getMethodName(), removeFromDetailsMethod.getParameterNames().get(0), removeFromDetailsMethod.getParameterNames().get(1));
// return ResponseEntity.ok().build();
bodyBuilder.appendFormalLine("return %s.ok().build();", getNameOfJavaType(RESPONSE_ENTITY));
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.wrapperWilcard(RESPONSE_ENTITY), parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
return methodBuilder.build();
}
use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo in project spring-roo by spring-projects.
the class JSONMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
final JSONAnnotationValues annotationValues = new JSONAnnotationValues(governorPhysicalTypeMetadata);
ClassOrInterfaceTypeDetails controllerDetail = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
// Getting controller metadata
final String controllerMetadataKey = ControllerMetadata.createIdentifier(controllerDetail);
final ControllerMetadata controllerMetadata = getMetadataService().get(controllerMetadataKey);
// This metadata is not available yet.
if (controllerMetadata == null) {
return null;
}
// Getting entity and check if is a readOnly entity or not
final JavaType entity = controllerMetadata.getEntity();
JavaType itemController = null;
if (controllerMetadata.getType() != ControllerType.ITEM) {
// Locate ItemController
Collection<ClassOrInterfaceTypeDetails> itemControllers = getControllerLocator().getControllers(entity, ControllerType.ITEM, RooJavaType.ROO_JSON);
if (itemControllers.isEmpty()) {
// We can't create metadata "Jet"
return null;
} else {
// Get controller with the same package
JavaPackage controllerPackage = controllerDetail.getType().getPackage();
for (ClassOrInterfaceTypeDetails controller : itemControllers) {
if (controllerPackage.equals(controller.getType().getPackage())) {
itemController = controller.getType();
break;
}
}
Validate.notNull(itemController, "ERROR: Can't find ITEM-type controller related to controller '%s'", controllerDetail.getType().getFullyQualifiedTypeName());
}
}
Validate.notNull(entity, "ERROR: You should provide a valid entity for controller '%s'", controllerDetail.getType().getFullyQualifiedTypeName());
final ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
Validate.notNull(entityDetails, "ERROR: Can't load details of %s", entity.getFullyQualifiedTypeName());
final JpaEntityMetadata entityMetadata = getMetadataService().get(JpaEntityMetadata.createIdentifier(entityDetails));
Validate.notNull(entityMetadata, "ERROR: Can't get Jpa Entity metada of %s", entity.getFullyQualifiedTypeName());
// Get entity plural
final String entityPlural = getPluralService().getPlural(entity);
final String entityIdentifierPlural = getPluralService().getPlural(entityMetadata.getCurrentIndentifierField().getFieldName());
// Getting service and its metadata
final JavaType service = controllerMetadata.getService();
ClassOrInterfaceTypeDetails serviceDetails = getTypeLocationService().getTypeDetails(service);
final String serviceMetadataKey = ServiceMetadata.createIdentifier(serviceDetails);
registerDependency(serviceMetadataKey, metadataIdentificationString);
final ServiceMetadata serviceMetadata = getMetadataService().get(serviceMetadataKey);
// Prepare information about ONE-TO-ONE relations
final List<Pair<RelationInfo, JpaEntityMetadata>> compositionRelationOneToOne = new ArrayList<Pair<RelationInfo, JpaEntityMetadata>>();
ClassOrInterfaceTypeDetails childEntityDetails;
JpaEntityMetadata childEntityMetadata;
for (RelationInfo info : entityMetadata.getRelationInfos().values()) {
if (info.cardinality == Cardinality.ONE_TO_ONE && info.type == JpaRelationType.COMPOSITION) {
childEntityDetails = getTypeLocationService().getTypeDetails(info.childType);
childEntityMetadata = getMetadataService().get(JpaEntityMetadata.createIdentifier(childEntityDetails));
compositionRelationOneToOne.add(Pair.of(info, childEntityMetadata));
}
}
Map<String, MethodMetadata> findersToAdd = new HashMap<String, MethodMetadata>();
// Getting annotated finders
final SearchAnnotationValues searchAnnotationValues = new SearchAnnotationValues(governorPhysicalTypeMetadata);
// Add finders only if controller is of search type
if (controllerMetadata.getType() == ControllerType.SEARCH && searchAnnotationValues != null && searchAnnotationValues.getFinders() != null) {
List<String> finders = new ArrayList<String>(Arrays.asList(searchAnnotationValues.getFinders()));
// Search indicated finders in its related service
for (MethodMetadata serviceFinder : serviceMetadata.getFinders()) {
String finderName = serviceFinder.getMethodName().getSymbolName();
if (finders.contains(finderName)) {
findersToAdd.put(finderName, serviceFinder);
// Add dependencies between modules
List<JavaType> types = new ArrayList<JavaType>();
types.add(serviceFinder.getReturnType());
types.addAll(serviceFinder.getReturnType().getParameters());
for (AnnotatedJavaType parameter : serviceFinder.getParameterTypes()) {
types.add(parameter.getJavaType());
types.addAll(parameter.getJavaType().getParameters());
}
for (JavaType parameter : types) {
getTypeLocationService().addModuleDependency(governorPhysicalTypeMetadata.getType().getModule(), parameter);
}
finders.remove(finderName);
}
}
// Check all finders have its service method
if (!finders.isEmpty()) {
throw new IllegalArgumentException(String.format("ERROR: Service %s does not have these finder methods: %s ", service.getFullyQualifiedTypeName(), StringUtils.join(finders, ", ")));
}
}
return new JSONMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, controllerMetadata, serviceMetadata, entityMetadata, entityPlural, entityIdentifierPlural, compositionRelationOneToOne, itemController, findersToAdd);
}
use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo in project spring-roo by spring-projects.
the class ThymeleafMetadata method getCreateDetailsCompositionMethod.
/**
* This method provides the "create" method for details association
* relationship using Thymeleaf view response type
*
* @return MethodMetadata
*/
private MethodMetadata getCreateDetailsCompositionMethod() {
RelationInfo detailsInfo = controllerMetadata.getLastDetailsInfo();
final ServiceMetadata detailsServiceMetadata = controllerMetadata.getServiceMetadataForEntity(detailsInfo.entityType);
final MethodMetadata addToRelationMethod = detailsServiceMetadata.getAddToRelationMethods().get(detailsInfo);
final FieldMetadata detailsServiceField = controllerMetadata.getDetailsServiceFields(detailsInfo.entityType);
final RelationInfoExtended info = controllerMetadata.getLastDetailsInfo();
final JavaType parentEntity = info.entityType;
final JavaType entity = info.childType;
final String entityItemName = StringUtils.uncapitalize(entity.getSimpleTypeName());
final String parentItemName = StringUtils.uncapitalize(parentEntity.getSimpleTypeName());
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(new AnnotatedJavaType(parentEntity, ANN_METADATA_MODEL_ATTRIBUTE));
parameterTypes.add(new AnnotatedJavaType(entity, ANN_METADATA_VALID, ANN_METADATA_MODEL_ATTRIBUTE));
parameterTypes.add(new AnnotatedJavaType(SpringJavaType.BINDING_RESULT));
parameterTypes.add(MODEL_PARAM);
// Define methodName
final JavaSymbolName methodName = CREATE_METHOD_NAME;
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @PostMapping annotation
AnnotationMetadataBuilder postMappingAnnotation = new AnnotationMetadataBuilder(SpringJavaType.POST_MAPPING);
postMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
annotations.add(postMappingAnnotation);
this.mvcMethodNames.put(methodName.getSymbolName(), methodName.getSymbolName());
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(new JavaSymbolName(parentItemName));
parameterNames.add(new JavaSymbolName(entityItemName));
parameterNames.add(new JavaSymbolName("result"));
parameterNames.add(MODEL_PARAM_NAME);
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// if (result.hasErrors()) {
bodyBuilder.appendFormalLine("if (result.hasErrors()) {");
bodyBuilder.indent();
// populateFormats(model);
bodyBuilder.appendFormalLine("%s(model);", populateFormMethod.getMethodName());
bodyBuilder.newLine();
// return new ModelAndView("customerorders/details/edit");
bodyBuilder.appendFormalLine("return new %s(\"%s/%s/create\");", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW), viewsPath, controllerMetadata.getDetailsPathAsString("/"));
bodyBuilder.indentRemove();
// }
bodyBuilder.appendFormalLine("}");
// customerOrderService.addToDetails(customerOrder,
// Collections.singleton(orderDetail));
bodyBuilder.appendFormalLine("%s().%s(%s,%s.singleton(%s));", getAccessorMethod(detailsServiceField).getMethodName(), addToRelationMethod.getMethodName(), parentItemName, getNameOfJavaType(JavaType.COLLECTIONS), entityItemName);
// return new ModelAndView("redirect:" +
// collectionLink.to("list").toUriString());
bodyBuilder.appendFormalLine("return new %s(\"redirect:\" + %s().to(%s.LIST).toUriString());", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW), getAccessorMethod(this.collectionMethodLinkBuilderFactoryField).getMethodName(), getNameOfJavaType(relatedCollectionLinkFactory));
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, SpringJavaType.MODEL_AND_VIEW, parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
return methodBuilder.build();
}
Aggregations