Search in sources :

Example 61 with JavaSymbolName

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();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 62 with JavaSymbolName

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;
}
Also used : ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 63 with JavaSymbolName

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);
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) ArrayList(java.util.ArrayList) EnumAttributeValue(org.springframework.roo.classpath.details.annotations.EnumAttributeValue) EnumDetails(org.springframework.roo.model.EnumDetails) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 64 with JavaSymbolName

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());
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) List(java.util.List) ArrayList(java.util.ArrayList) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 65 with JavaSymbolName

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());
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

JavaSymbolName (org.springframework.roo.model.JavaSymbolName)317 ArrayList (java.util.ArrayList)186 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)155 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)143 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)142 JavaType (org.springframework.roo.model.JavaType)133 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)121 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)119 SpringJavaType (org.springframework.roo.model.SpringJavaType)61 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)59 JdkJavaType (org.springframework.roo.model.JdkJavaType)59 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)48 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)47 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)45 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)39 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)36 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)29 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)29 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)27 EnumDetails (org.springframework.roo.model.EnumDetails)25