use of org.springframework.roo.addon.layers.service.addon.ServiceMetadata 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.layers.service.addon.ServiceMetadata 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.layers.service.addon.ServiceMetadata in project spring-roo by spring-projects.
the class ThymeleafMetadata method getRemoveFromDetailsMethod.
private MethodMetadata getRemoveFromDetailsMethod() {
RelationInfoExtended 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(), AnnotationMetadataBuilder.getInstance(SpringJavaType.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(SpringJavaType.DELETE_MAPPING);
postMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
postMappingAnnotation.addStringAttribute("value", "/{" + itemsName.getSymbolName() + "}");
annotations.add(postMappingAnnotation);
this.mvcMethodNames.put(methodName.getSymbolName(), methodName.getSymbolName());
annotations.add(new AnnotationMetadataBuilder(SpringJavaType.RESPONSE_BODY));
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.addAll(removeFromMethod.getParameterNames());
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// customerService.removeFromOrders(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.layers.service.addon.ServiceMetadata 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();
}
use of org.springframework.roo.addon.layers.service.addon.ServiceMetadata in project spring-roo by spring-projects.
the class ThymeleafMetadata method getListDatatablesDetailMethod.
/**
* This method provides detail datatables list method using Thymeleaf
* response type
*
* @return MethodMetadata
*/
private MethodMetadata getListDatatablesDetailMethod() {
RelationInfo detailsInfo = controllerMetadata.getLastDetailsInfo();
final ServiceMetadata detailsServiceMetadata = controllerMetadata.getServiceMetadataForEntity(detailsInfo.childType);
final MethodMetadata findAllMethod = detailsServiceMetadata.getRefencedFieldFindAllDefinedMethod(detailsInfo.mappedBy);
final MethodMetadata countByDetailMethod = detailsServiceMetadata.getCountByReferenceFieldDefinedMethod(detailsInfo.mappedBy);
final FieldMetadata detailsServiceField = controllerMetadata.getDetailsServiceFields(detailsInfo.childType);
// Define methodName
final JavaSymbolName methodName = LIST_DATATABLES_DETAILS_METHOD_NAME;
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(new AnnotatedJavaType(findAllMethod.getParameterTypes().get(0).getJavaType(), ANN_METADATA_MODEL_ATTRIBUTE));
parameterTypes.add(DATATABLES_COLUMNS_PARAM);
parameterTypes.add(GLOBAL_SEARCH_PARAM);
parameterTypes.add(DATATABLES_PAGEABLE_PARAM);
AnnotationMetadataBuilder requestParamAnnotation = new AnnotationMetadataBuilder(REQUEST_PARAM);
requestParamAnnotation.addStringAttribute("value", "draw");
parameterTypes.add(new AnnotatedJavaType(JavaType.INT_OBJECT, requestParamAnnotation.build()));
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
final JavaSymbolName parentParamName = findAllMethod.getParameterNames().get(0);
parameterNames.add(parentParamName);
parameterNames.add(DATATABLES_COLUMNS_PARAM_NAME);
parameterNames.add(new JavaSymbolName("search"));
parameterNames.add(new JavaSymbolName("pageable"));
parameterNames.add(new JavaSymbolName("draw"));
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @GetMapping annotation
AnnotationMetadataBuilder getMappingAnnotation = new AnnotationMetadataBuilder(GET_MAPPING);
getMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
getMappingAnnotation.addEnumAttribute("produces", SPRINGLETS_DATATABLES, "MEDIA_TYPE");
getMappingAnnotation.addStringAttribute("value", "/dt");
annotations.add(getMappingAnnotation);
this.mvcMethodNames.put(methodName.getSymbolName(), methodName.getSymbolName());
// Adding @ResponseBody annotation
annotations.add(RESPONSE_BODY_ANNOTATION);
// Generating returnType
final JavaType serviceReturnType = findAllMethod.getReturnType();
final JavaType dataReturnType = JavaType.wrapperOf(SpringletsJavaType.SPRINGLETS_CONVERTED_DATATABLES_DATA, serviceReturnType.getParameters().get(0));
final JavaType returnType = JavaType.wrapperOf(RESPONSE_ENTITY, dataReturnType);
// TODO
// Add module dependency
// getTypeLocationService().addModuleDependency(this.controller.getType().getModule(),
// returnParameterTypes.get(i));
// Generate body
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
final String itemsName = StringUtils.uncapitalize(detailsInfo.fieldName);
// Page<CustomerOrder> orders =
// customerOrderService.findByCustomer(customer, globalSearch,
// pageable);
bodyBuilder.newLine();
bodyBuilder.appendFormalLine("%s %s = %s().%s(%s, search, pageable);", getNameOfJavaType(serviceReturnType), itemsName, getAccessorMethod(detailsServiceField).getMethodName(), findAllMethod.getMethodName(), parentParamName);
final String totalVarName = "total" + StringUtils.capitalize(itemsName) + "Count";
// long totalOrdersCount =
// customerOrderService.countByCustomer(customer);
bodyBuilder.appendFormalLine("%s %s = %s().%s(%s);", getNameOfJavaType(countByDetailMethod.getReturnType()), totalVarName, getAccessorMethod(detailsServiceField).getMethodName(), countByDetailMethod.getMethodName(), parentParamName);
// ConvertedDatatablesData<CustomerOrder> data = new
// ConvertedDatatablesData<CustomerOrder>(orders,
// totalOrdersCount, draw, conversionService, columns);
bodyBuilder.appendFormalLine("%1$s data = new %1$s(%2$s, %3$s, draw, %4$s(), %5$s);", getNameOfJavaType(dataReturnType), itemsName, totalVarName, getAccessorMethod(this.conversionServiceField).getMethodName(), DATATABLES_COLUMNS_PARAM_NAME);
// return ResponseEntity.ok(data);
bodyBuilder.appendFormalLine("return %s.ok(data);", getNameOfJavaType(SpringJavaType.RESPONSE_ENTITY));
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, returnType, parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
return methodBuilder.build();
}
Aggregations