use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class ControllerOperationsImpl method getRooControllerAnnotation.
/**
* Method that returns @RooController annotation
*
* @param entity
* Entity over which create the controller
* @param pathPrefix
* Prefix to use in RequestMapping
* @param controllerType
* Indicates the controller type
* @return
*/
private AnnotationMetadataBuilder getRooControllerAnnotation(final JavaType entity, final String pathPrefix, final ControllerType controllerType) {
final List<AnnotationAttributeValue<?>> rooControllerAttributes = new ArrayList<AnnotationAttributeValue<?>>();
rooControllerAttributes.add(new ClassAttributeValue(new JavaSymbolName("entity"), entity));
if (StringUtils.isNotEmpty(pathPrefix)) {
rooControllerAttributes.add(new StringAttributeValue(new JavaSymbolName("pathPrefix"), pathPrefix));
}
rooControllerAttributes.add(new EnumAttributeValue(new JavaSymbolName("type"), new EnumDetails(RooJavaType.ROO_ENUM_CONTROLLER_TYPE, new JavaSymbolName(controllerType.name()))));
return new AnnotationMetadataBuilder(RooJavaType.ROO_CONTROLLER, rooControllerAttributes);
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class ControllerOperationsImpl method exportOperation.
/**
* Generate the operations selected in the controller indicated
*
* @param controller
* Controller where the operations will be created
* @param operations
* Service operations names that will be created
*/
public void exportOperation(JavaType controller, List<String> operations) {
ClassOrInterfaceTypeDetails controllerDetails = getTypeLocationService().getTypeDetails(controller);
// Check if provided controller exists on current project
Validate.notNull(controllerDetails, "ERROR: You must provide an existing controller");
// Check if provided controller has been annotated with @RooController
Validate.notNull(controllerDetails.getAnnotation(RooJavaType.ROO_CONTROLLER), "ERROR: You must provide a controller annotated with @RooController");
// Check parameter operations
Validate.notEmpty(operations, "INFO: Don't exist operations to publish");
ClassOrInterfaceTypeDetailsBuilder controllerBuilder = new ClassOrInterfaceTypeDetailsBuilder(controllerDetails);
AnnotationMetadata operationsAnnotation = controllerDetails.getAnnotation(RooJavaType.ROO_OPERATIONS);
// Create an array with new attributes array
List<StringAttributeValue> operationsToAdd = new ArrayList<StringAttributeValue>();
if (operationsAnnotation == null) {
// Add Operations annotation
AnnotationMetadataBuilder opAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_OPERATIONS);
controllerBuilder.addAnnotation(opAnnotation);
// set operations from command
for (String operation : operations) {
operationsToAdd.add(new StringAttributeValue(new JavaSymbolName("value"), operation));
}
opAnnotation.addAttribute(new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("operations"), operationsToAdd));
// Write changes on provided controller
getTypeManagementService().createOrUpdateTypeOnDisk(controllerBuilder.build());
} else {
List<String> operationsNames = new ArrayList<String>();
boolean operationsAdded = false;
AnnotationAttributeValue<Object> attributeOperations = operationsAnnotation.getAttribute("operations");
if (attributeOperations != null) {
List<StringAttributeValue> existingOperations = (List<StringAttributeValue>) attributeOperations.getValue();
Iterator<StringAttributeValue> it = existingOperations.iterator();
// new ones
while (it.hasNext()) {
StringAttributeValue attributeValue = (StringAttributeValue) it.next();
operationsToAdd.add(attributeValue);
operationsNames.add(attributeValue.getValue());
}
// Add new finders to new attributes array
for (String operation : operations) {
if (!operationsNames.contains(operation)) {
operationsToAdd.add(new StringAttributeValue(new JavaSymbolName("value"), operation));
operationsAdded = true;
}
}
if (operationsAdded) {
AnnotationMetadataBuilder opAnnotation = new AnnotationMetadataBuilder(operationsAnnotation);
opAnnotation.addAttribute(new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("operations"), operationsToAdd));
controllerBuilder.updateTypeAnnotation(opAnnotation);
// Write changes on provided controller
getTypeManagementService().createOrUpdateTypeOnDisk(controllerBuilder.build());
}
}
}
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class ControllerOperationsImpl method createLinkFactoryClass.
/**
* Creates a new class which supports its associated controller building
* URL's for its methods
*
* @param controller
* the JavaType of the associated controller
*/
@Override
public void createLinkFactoryClass(JavaType controller) {
// Create name
String name = controller.getSimpleTypeName().concat("LinkFactory");
if (name.contains("Controller")) {
name = name.replace("Controller", "");
}
// Create type
final JavaType linkFactoryJavaType = new JavaType(controller.getPackage().getFullyQualifiedPackageName().concat(".").concat(name), controller.getModule());
// Create identifier
final String linkFactoryPathIdentifier = getPathResolver().getCanonicalPath(linkFactoryJavaType.getModule(), Path.SRC_MAIN_JAVA, linkFactoryJavaType);
final String mid = PhysicalTypeIdentifier.createIdentifier(linkFactoryJavaType, getPathResolver().getPath(linkFactoryPathIdentifier));
// Create builder
final ClassOrInterfaceTypeDetailsBuilder typeBuilder = new ClassOrInterfaceTypeDetailsBuilder(mid, PUBLIC, linkFactoryJavaType, PhysicalTypeCategory.CLASS);
// Add @RooLinkFactory annotation
AnnotationMetadataBuilder annotationBuilder = new AnnotationMetadataBuilder(RooJavaType.ROO_LINK_FACTORY);
annotationBuilder.addAttribute(new ClassAttributeValue(new JavaSymbolName("controller"), controller));
typeBuilder.addAnnotation(annotationBuilder);
// Write changes to disk
getTypeManagementService().createOrUpdateTypeOnDisk(typeBuilder.build());
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class ThymeleafMetadata 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);
JavaSymbolName methodName = DELETE_METHOD_NAME;
JavaSymbolName itemsName = detailsInfo.fieldMetadata.getFieldName();
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(new AnnotatedJavaType(removeFromMethod.getParameterTypes().get(0).getJavaType(), ANN_METADATA_MODEL_ATTRIBUTE));
parameterTypes.add(new AnnotatedJavaType(detailsInfo.childType, ANN_METADATA_MODEL_ATTRIBUTE));
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());
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.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.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class ThymeleafMetadata method getListDatatablesByIdsInDetailMethod.
/**
* This method provides detail datatablesByIdsIn list method using Thymeleaf
* response type
*
* @return MethodMetadata
*/
private MethodMetadata getListDatatablesByIdsInDetailMethod() {
RelationInfo detailsInfo = controllerMetadata.getLastDetailsInfo();
final ServiceMetadata detailsServiceMetadata = controllerMetadata.getServiceMetadataForEntity(detailsInfo.childType);
final MethodMetadata findAllByIdsInMethod = detailsServiceMetadata.getCurrentFindAllByIdsInWithGlobalSearchMethod();
if (findAllByIdsInMethod == null) {
return null;
}
final FieldMetadata detailsServiceField = controllerMetadata.getDetailsServiceFields(detailsInfo.childType);
// Define methodName
final JavaSymbolName methodName = LIST_DATATABLES_BY_IDS_IN_DETAILS_METHOD_NAME;
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
AnnotationMetadataBuilder requestParamIdsAnnotation = new AnnotationMetadataBuilder(REQUEST_PARAM);
requestParamIdsAnnotation.addStringAttribute("value", "ids");
parameterTypes.add(new AnnotatedJavaType(JavaType.wrapperOf(JavaType.LIST, findAllByIdsInMethod.getParameterTypes().get(0).getJavaType().getBaseType()), requestParamIdsAnnotation.build()));
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>();
parameterNames.add(new JavaSymbolName("ids"));
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", "/dtByIdsIn");
annotations.add(getMappingAnnotation);
this.mvcMethodNames.put(methodName.getSymbolName(), methodName.getSymbolName());
// Adding @ResponseBody annotation
annotations.add(RESPONSE_BODY_ANNOTATION);
// Generating returnType
final JavaType serviceReturnType = findAllByIdsInMethod.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(ids, search, pageable);", getNameOfJavaType(serviceReturnType), itemsName, getAccessorMethod(detailsServiceField).getMethodName(), findAllByIdsInMethod.getMethodName());
final String totalVarName = "total" + StringUtils.capitalize(itemsName) + "Count";
// long totalOrdersCount =
// customerOrderService.countByCustomer(customer);
bodyBuilder.appendFormalLine("long %s = %s.getTotalElements();", totalVarName, itemsName);
// 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