use of org.springframework.roo.addon.layers.service.addon.ServiceMetadata in project spring-roo by spring-projects.
the class JSONMetadata method getDeleteDetailMethod.
private MethodMetadata getDeleteDetailMethod() {
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 = new JavaSymbolName("delete");
JavaSymbolName itemsName = detailsInfo.fieldMetadata.getFieldName();
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(new AnnotatedJavaType(removeFromMethod.getParameterTypes().get(0).getJavaType(), ANN_MODEL_ATTRIBUTE));
AnnotationMetadata modelAttributAnnotation = AnnotationMetadataBuilder.getInstance(SpringJavaType.MODEL_ATTRIBUTE);
parameterTypes.add(new AnnotatedJavaType(detailsInfo.childType, modelAttributAnnotation));
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.add(removeFromMethod.getParameterNames().get(0));
parameterNames.add(itemsName);
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// customerService.addToOrders(customer, order.getId());
bodyBuilder.appendFormalLine("%s().%s(%s,%s.singleton(%s.%s()));", getAccessorMethod(detailsServiceField).getMethodName(), removeFromMethod.getMethodName(), removeFromMethod.getParameterNames().get(0), getNameOfJavaType(JavaType.COLLECTIONS), itemsName, detailsInfo.childEntityMetadata.getCurrentIdentifierAccessor().getMethodName());
// 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 JSONMetadata method getListDetailsMethod.
private MethodMetadata getListDetailsMethod() {
RelationInfo detailsInfo = controllerMetadata.getLastDetailsInfo();
final ServiceMetadata detailsServiceMetadata = controllerMetadata.getServiceMetadataForEntity(detailsInfo.childType);
final MethodMetadata findAllMethod = detailsServiceMetadata.getRefencedFieldFindAllDefinedMethod(detailsInfo.mappedBy);
final FieldMetadata detailsServiceField = controllerMetadata.getDetailsServiceFields(detailsInfo.childType);
// Define methodName
final JavaSymbolName methodName = new JavaSymbolName("list" + StringUtils.capitalize(detailsInfo.fieldName));
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(new AnnotatedJavaType(findAllMethod.getParameterTypes().get(0).getJavaType(), ANN_MODEL_ATTRIBUTE));
parameterTypes.add(new AnnotatedJavaType(SpringletsJavaType.SPRINGLETS_GLOBAL_SEARCH));
parameterTypes.add(new AnnotatedJavaType(SpringJavaType.PAGEABLE));
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(GLOBAL_SEARCH_NAME);
parameterNames.add(PAGEABLE_PARAM_NAME);
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @GetMapping annotation
AnnotationMetadataBuilder getMappingAnnotation = new AnnotationMetadataBuilder(GET_MAPPING);
getMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
annotations.add(getMappingAnnotation);
// Generating returnType
JavaType serviceReturnType = findAllMethod.getReturnType();
JavaType returnType = JavaType.wrapperOf(RESPONSE_ENTITY, serviceReturnType);
// TODO
// Add module dependency
// getTypeLocationService().addModuleDependency(this.controller.getType().getModule(),
// returnParameterTypes.get(i));
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
final String itemNames = StringUtils.uncapitalize(detailsInfo.fieldName);
// Page<Customer> customers = customerService.findAll(globalSearch, pageable);
bodyBuilder.newLine();
bodyBuilder.appendFormalLine("%s %s = %s().%s(%s, %s, %s);", getNameOfJavaType(serviceReturnType), itemNames, getAccessorMethod(detailsServiceField).getMethodName(), findAllMethod.getMethodName(), parentParamName, GLOBAL_SEARCH_NAME, PAGEABLE_PARAM_NAME);
// return ResponseEntity.ok(customers);
bodyBuilder.appendFormalLine(String.format("return %s.ok(%s);", getNameOfJavaType(RESPONSE_ENTITY), itemNames));
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, returnType, 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 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.layers.service.addon.ServiceMetadata in project spring-roo by spring-projects.
the class JSONMetadata method getUpdateDetailMethod.
private MethodMetadata getUpdateDetailMethod() {
// Define methodName
final JavaSymbolName methodName = new JavaSymbolName("update");
final RelationInfoExtended info = controllerMetadata.getLastDetailsInfo();
final JavaType parentEntity = info.entityType;
final JavaType entity = info.childType;
final JpaEntityMetadata entityMetadata = info.childEntityMetadata;
final FieldMetadata entityIdentifier = entityMetadata.getCurrentIndentifierField();
final String entityItemName = StringUtils.uncapitalize(entity.getSimpleTypeName());
final ServiceMetadata serviceMetadata = controllerMetadata.getServiceMetadataForEntity(entity);
// Define parameters
final List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(new AnnotatedJavaType(parentEntity, ANN_MODEL_ATTRIBUTE));
parameterTypes.add(new AnnotatedJavaType(entity, ANN_MODEL_ATTRIBUTE));
parameterTypes.add(new AnnotatedJavaType(entity, ANN_METADATA_VALID, ANN_METADATA_REQUEST_BODY));
parameterTypes.add(new AnnotatedJavaType(SpringJavaType.BINDING_RESULT));
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
final String storedName = "stored".concat(entity.getSimpleTypeName());
parameterNames.add(new JavaSymbolName(StringUtils.uncapitalize(parentEntity.getSimpleTypeName())));
parameterNames.add(new JavaSymbolName(storedName));
parameterNames.add(new JavaSymbolName(entityItemName));
parameterNames.add(new JavaSymbolName("result"));
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @PutMapping annotation
AnnotationMetadataBuilder putMappingAnnotation = new AnnotationMetadataBuilder(PUT_MAPPING);
putMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
annotations.add(putMappingAnnotation);
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// if (result.hasErrors()) {
// return new ResponseEntity.status(HttpStatus.CONFLICT).body(result);
// }
bodyBuilder.newLine();
bodyBuilder.appendFormalLine("if (result.hasErrors()) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("return %s.status(%s.CONFLICT).body(result);", getNameOfJavaType(RESPONSE_ENTITY), getNameOfJavaType(SpringJavaType.HTTP_STATUS));
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
// orderDetail.setId(storedOrderDetails.getId());
bodyBuilder.appendFormalLine("%s.set%s(%s.get%s());", entityItemName, entityIdentifier.getFieldName().getSymbolNameCapitalisedFirstLetter(), storedName, entityIdentifier.getFieldName().getSymbolNameCapitalisedFirstLetter());
// customerService.save(customer);
bodyBuilder.appendFormalLine("%s().%s(%s);", getAccessorMethod(controllerMetadata.getLastDetailServiceField()).getMethodName(), serviceMetadata.getCurrentSaveMethod().getMethodName(), entityItemName);
// 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 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();
}
Aggregations