Search in sources :

Example 91 with AnnotatedJavaType

use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.

the class ThymeleafMainControllerMetadata method getAccessibilityMethod.

private MethodMetadata getAccessibilityMethod() {
    // Define methodName
    final JavaSymbolName methodName = new JavaSymbolName("accessibility");
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(SpringJavaType.MODEL));
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.add(new JavaSymbolName("model"));
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Adding annotations
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    // Adding @GetMapping annotation
    AnnotationMetadataBuilder getMapping = new AnnotationMetadataBuilder(SpringJavaType.GET_MAPPING);
    getMapping.addStringAttribute("value", "/accessibility");
    annotations.add(getMapping);
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Always save locale
    bodyBuilder.appendFormalLine("model.addAttribute(\"application_locale\", %s.getLocale().getLanguage());", getNameOfJavaType(SpringJavaType.LOCALE_CONTEXT_HOLDER));
    // return "accessibility";
    bodyBuilder.appendFormalLine("return \"accessibility\";");
    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 92 with AnnotatedJavaType

use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.

the class ThymeleafMainControllerMetadata method getIndexMethod.

/**
 * This method provides the "index" method that returns Thymeleaf view
 *
 * @return MethodMetadata
 */
private MethodMetadata getIndexMethod() {
    // Define methodName
    final JavaSymbolName methodName = new JavaSymbolName("index");
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(SpringJavaType.MODEL));
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.add(new JavaSymbolName("model"));
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Adding annotations
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    // Adding @GetMapping annotation
    AnnotationMetadataBuilder getMapping = new AnnotationMetadataBuilder(SpringJavaType.GET_MAPPING);
    getMapping.addStringAttribute("value", "/");
    annotations.add(getMapping);
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Always save locale
    bodyBuilder.appendFormalLine("model.addAttribute(\"application_locale\", %s.getLocale().getLanguage());", getNameOfJavaType(SpringJavaType.LOCALE_CONTEXT_HOLDER));
    // return "index";
    bodyBuilder.appendFormalLine("return \"index\";");
    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 93 with AnnotatedJavaType

use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.

the class ThymeleafUIConfigurationMetadata method getJavascriptThymeleafViewResolverMethod.

/**
 * Returns method which configure ThymeleafViewResolver
 *
 * @return
 */
private MethodMetadata getJavascriptThymeleafViewResolverMethod() {
    // Define methodName
    final JavaSymbolName methodName = new JavaSymbolName("javascriptThymeleafViewResolver");
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Adding annotations
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    // add @Bean
    annotations.add(new AnnotationMetadataBuilder(SpringJavaType.BEAN));
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    /*
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(this.templateEngine);
        resolver.setCharacterEncoding(UTF8);
        resolver.setContentType("application/javascript");
        resolver.setViewNames(new String[] {"*.js"});
        resolver.setCache(this.properties.isCache());
        return resolver;
     */
    bodyBuilder.appendFormalLine("%1$s resolver = new %1$s();", getNameOfJavaType(THYMELEAF_VIEW_RESOLVER));
    bodyBuilder.appendFormalLine("resolver.setTemplateEngine(%s());", getAccessorMethod(this.templateEngineField).getMethodName());
    bodyBuilder.appendFormalLine("resolver.setCharacterEncoding(\"UTF-8\");");
    bodyBuilder.appendFormalLine("resolver.setContentType(\"application/javascript\");");
    bodyBuilder.appendFormalLine("resolver.setViewNames(new String[] {\"*.js\"});");
    bodyBuilder.appendFormalLine("resolver.setCache(%s().isCache());", getAccessorMethod(this.thymeleafPropertiesField).getMethodName());
    bodyBuilder.appendFormalLine("return resolver;");
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, THYMELEAF_VIEW_RESOLVER, 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 94 with AnnotatedJavaType

use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.

the class ThymeleafUIConfigurationMetadata method getJavascriptTemplateResolverMethod.

/**
 * Returns method which configure SpringResourceTemplateResolve
 *
 * @return
 */
private MethodMetadata getJavascriptTemplateResolverMethod() {
    // Define methodName
    final JavaSymbolName methodName = new JavaSymbolName("javascriptTemplateResolver");
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Adding annotations
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    // add @Bean
    annotations.add(new AnnotationMetadataBuilder(SpringJavaType.BEAN));
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    /*
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setApplicationContext(getApplicationContext());
        resolver.setPrefix("classpath:/templates/fragments/js/");
        resolver.setTemplateMode(TemplateMode.JAVASCRIPT);
        resolver.setCharacterEncoding(UTF8);
        resolver.setCheckExistence(true);
        resolver.setCacheable(this.properties.isCache());
        return resolver;
     */
    bodyBuilder.appendFormalLine("%1$s resolver = new %1$s();", getNameOfJavaType(SPRING_RESOURCE_TEMPLATE_RESOLVER));
    bodyBuilder.appendFormalLine("resolver.setApplicationContext(%s());", getAccessorMethod(this.applicationContextAwareField).getMethodName());
    bodyBuilder.appendFormalLine("resolver.setPrefix(\"classpath:/templates/fragments/js/\");");
    bodyBuilder.appendFormalLine("resolver.setTemplateMode(%s.JAVASCRIPT);", getNameOfJavaType(TEMPLATE_MODE));
    bodyBuilder.appendFormalLine("resolver.setCharacterEncoding(\"UTF-8\");");
    bodyBuilder.appendFormalLine("resolver.setCheckExistence(true);");
    bodyBuilder.appendFormalLine("resolver.setCacheable(%s().isCache());", getAccessorMethod(this.thymeleafPropertiesField).getMethodName());
    bodyBuilder.appendFormalLine("return resolver;");
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, SPRING_RESOURCE_TEMPLATE_RESOLVER, 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 95 with AnnotatedJavaType

use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.

the class ControllerOperationsImpl method getAllMethodsToPublish.

/**
 * Get all the methods that can be published from the service or the
 * controller established by parameter
 *
 * @param currentService
 *            Service from which obtain methods
 * @param currentController
 *            Controller from which obtain methods
 * @return methods names list
 */
public List<String> getAllMethodsToPublish(String currentService, String currentController) {
    // Generating all possible values
    List<String> serviceMethodsToPublish = new ArrayList<String>();
    List<ClassOrInterfaceTypeDetails> servicesToPublish = new ArrayList<ClassOrInterfaceTypeDetails>();
    if (StringUtils.isEmpty(currentService)) {
        // Get controllers
        Collection<ClassOrInterfaceTypeDetails> controllers = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_CONTROLLER);
        for (ClassOrInterfaceTypeDetails controller : controllers) {
            String name = getClasspathOperations().replaceTopLevelPackageString(controller, currentController);
            if (currentController.equals(name)) {
                // Get the entity associated
                AnnotationMetadata controllerAnnotation = controller.getAnnotation(RooJavaType.ROO_CONTROLLER);
                JavaType entity = (JavaType) controllerAnnotation.getAttribute("entity").getValue();
                // Search the service related with the entity
                Set<ClassOrInterfaceTypeDetails> services = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_SERVICE);
                Iterator<ClassOrInterfaceTypeDetails> itServices = services.iterator();
                while (itServices.hasNext()) {
                    ClassOrInterfaceTypeDetails existingService = itServices.next();
                    AnnotationAttributeValue<Object> entityAttr = existingService.getAnnotation(RooJavaType.ROO_SERVICE).getAttribute("entity");
                    if (entityAttr != null && entityAttr.getValue().equals(entity)) {
                        servicesToPublish.add(existingService);
                    }
                }
                break;
            }
        }
    } else {
        // Get the services
        Set<ClassOrInterfaceTypeDetails> services = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_SERVICE);
        for (ClassOrInterfaceTypeDetails service : services) {
            String name = getClasspathOperations().replaceTopLevelPackageString(service, currentService);
            if (currentService.equals(name)) {
                servicesToPublish.add(service);
                break;
            }
        }
    }
    for (ClassOrInterfaceTypeDetails serviceToPublish : servicesToPublish) {
        // Getting service metadata
        final ServiceMetadata serviceMetadata = getMetadataService().get(ServiceMetadata.createIdentifier(serviceToPublish));
        // Get all methods generated by Roo
        List<MethodMetadata> methodsImplementedByRoo = new ArrayList<MethodMetadata>();
        methodsImplementedByRoo.addAll(serviceMetadata.getNotTransactionalDefinedMethods());
        methodsImplementedByRoo.addAll(serviceMetadata.getTransactionalDefinedMethods());
        // Get all methods and compare them with the generated by Roo
        List<MethodMetadata> methods = serviceToPublish.getMethods();
        boolean notGeneratedByRoo;
        for (MethodMetadata method : methods) {
            notGeneratedByRoo = true;
            Iterator<MethodMetadata> iterMethodsImplRoo = methodsImplementedByRoo.iterator();
            while (iterMethodsImplRoo.hasNext() && notGeneratedByRoo) {
                MethodMetadata methodImplementedByRoo = iterMethodsImplRoo.next();
                // If name is equals check the parameters
                if (method.getMethodName().equals(methodImplementedByRoo.getMethodName())) {
                    // order
                    if (method.getParameterTypes().size() == methodImplementedByRoo.getParameterTypes().size()) {
                        Iterator<AnnotatedJavaType> iterParameterTypesMethodRoo = methodImplementedByRoo.getParameterTypes().iterator();
                        boolean allParametersAreEquals = true;
                        for (AnnotatedJavaType parameterType : method.getParameterTypes()) {
                            AnnotatedJavaType parameterTypeMethodRoo = iterParameterTypesMethodRoo.next();
                            if (!parameterType.getJavaType().equals(parameterTypeMethodRoo.getJavaType())) {
                                allParametersAreEquals = false;
                                break;
                            }
                        }
                        if (allParametersAreEquals) {
                            notGeneratedByRoo = false;
                        }
                    }
                }
            }
            // If is not generated by Roo add to list of the elements
            if (notGeneratedByRoo) {
                StringBuffer methodNameBuffer = new StringBuffer("");
                if (StringUtils.isEmpty(currentService)) {
                    methodNameBuffer.append(getClasspathOperations().replaceTopLevelPackage(serviceToPublish)).append(".");
                }
                methodNameBuffer.append(method.getMethodName().getSymbolName());
                List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
                methodNameBuffer = methodNameBuffer.append("(");
                for (int i = 0; i < parameterTypes.size(); i++) {
                    String paramType = parameterTypes.get(i).getJavaType().getSimpleTypeName();
                    methodNameBuffer = methodNameBuffer.append(paramType).append(",");
                }
                String methodName;
                if (!parameterTypes.isEmpty()) {
                    methodName = methodNameBuffer.toString().substring(0, methodNameBuffer.toString().length() - 1).concat(")");
                } else {
                    methodName = methodNameBuffer.append(")").toString();
                }
                serviceMethodsToPublish.add(methodName);
            }
        }
    }
    return serviceMethodsToPublish;
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) 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) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) ServiceMetadata(org.springframework.roo.addon.layers.service.addon.ServiceMetadata)

Aggregations

AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)130 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)117 ArrayList (java.util.ArrayList)116 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)108 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)99 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)81 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)71 JavaType (org.springframework.roo.model.JavaType)63 SpringJavaType (org.springframework.roo.model.SpringJavaType)46 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)41 JdkJavaType (org.springframework.roo.model.JdkJavaType)28 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)25 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)23 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)18 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)16 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)15 RelationInfoExtended (org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended)12 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)10 Pair (org.apache.commons.lang3.tuple.Pair)7 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)7