use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.
the class ThymeleafMainControllerMetadata method getJavascriptTemplatesmethod.
/*
* =====================================================================================
*/
/**
* @return
*/
private MethodMetadata getJavascriptTemplatesmethod() {
// Define methodName
final JavaSymbolName methodName = new JavaSymbolName("javascriptTemplates");
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
// Create @PathVariable("templeate") String template parameter
AnnotationMetadataBuilder pathVarialbeAnnotation = new AnnotationMetadataBuilder(SpringJavaType.PATH_VARIABLE);
pathVarialbeAnnotation.addStringAttribute("value", "template");
AnnotatedJavaType templateParameter = new AnnotatedJavaType(JavaType.STRING, pathVarialbeAnnotation.build());
parameterTypes.add(templateParameter);
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(new JavaSymbolName("template"));
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @RequestMapping annotation
AnnotationMetadataBuilder requestMapping = new AnnotationMetadataBuilder(SpringJavaType.REQUEST_MAPPING);
requestMapping.addStringAttribute("value", "/js/{template}.js");
getNameOfJavaType(SpringJavaType.REQUEST_METHOD);
requestMapping.addEnumAttribute("method", SpringJavaType.REQUEST_METHOD, "GET");
annotations.add(requestMapping);
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// if (StringUtils.hasLength(template)) {
// return template.concat(".js");
// }
bodyBuilder.appendFormalLine("if (%s.hasLength(template)) {", getNameOfJavaType(SpringJavaType.STRING_UTILS));
bodyBuilder.indent();
bodyBuilder.appendFormalLine("return template.concat(\".js\");");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
// throw new NotFoundException("File not found")
bodyBuilder.appendFormalLine("throw new %s(\"File not found\");", getNameOfJavaType(SpringletsJavaType.SPRINGLETS_NOT_FOUND_EXCEPTION));
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.STRING, parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
return methodBuilder.build();
}
use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.
the class ControllerOperationsImpl method getRooDetailAnnotation.
/**
* Method that returns @RooDetail annotation
*
* @param relationField
* Field that set the relationship
* @param viewsList
* Separated comma list that defines the parent views where the
* new detail will be displayed.
* @return
*/
private AnnotationMetadataBuilder getRooDetailAnnotation(final String relationField, final String viewsList) {
AnnotationMetadataBuilder annotationDetail = new AnnotationMetadataBuilder(RooJavaType.ROO_DETAIL);
annotationDetail.addStringAttribute("relationField", relationField);
// Including views attribute if needed
if (StringUtils.isNotEmpty(viewsList)) {
String[] views = viewsList.split(",");
List<StringAttributeValue> viewsValues = new ArrayList<StringAttributeValue>();
for (String view : views) {
viewsValues.add(new StringAttributeValue(new JavaSymbolName("value"), view));
}
ArrayAttributeValue<StringAttributeValue> viewsAttr = new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("views"), viewsValues);
annotationDetail.addAttribute(viewsAttr);
}
return annotationDetail;
}
use of org.springframework.roo.model.JavaSymbolName 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.model.JavaSymbolName 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.model.JavaSymbolName 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());
}
Aggregations