Search in sources :

Example 21 with MethodMetadata

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

the class LinkFactoryMetadata method getToUriMethod.

/**
 * Generates a `toUri` method which generates URI's for the *Collection*
 * controller methods which are called from views.
 *
 * @param finderName
 * @param serviceFinderMethod
 * @return
 */
private MethodMetadata getToUriMethod() {
    // Define methodName
    final JavaSymbolName toUriMethodName = TO_URI_METHOD_NAME;
    // Define method argument types
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(stringArgument);
    parameterTypes.add(objectArrayArgument);
    parameterTypes.add(mapStringObjectArgument);
    // Return method if already exists
    MethodMetadata existingMethod = getGovernorMethod(toUriMethodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Define method argument names
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.add(METHOD_NAME_ARGUMENT_NAME);
    parameterNames.add(PARAMETERS_ARGUMENT_NAME);
    parameterNames.add(PATH_VARIABLES_ARGUMENT_NAME);
    // Generate body
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Include a conditional for every method
    for (MethodMetadata method : this.controllerMethods) {
        // Getting methodName
        String methodName = method.getMethodName().getSymbolName();
        // Getting methodParams
        List<String> methodParamsToNull = new ArrayList<String>();
        for (AnnotatedJavaType paramType : method.getParameterTypes()) {
            JavaType javaType = paramType.getJavaType();
            if (javaType.isPrimitive() && !javaType.isArray()) {
                if (JavaType.BOOLEAN_PRIMITIVE.equals(javaType)) {
                    methodParamsToNull.add("false");
                } else if (JavaType.INT_PRIMITIVE.equals(javaType)) {
                    methodParamsToNull.add("0");
                } else if (JavaType.LONG_PRIMITIVE.equals(javaType)) {
                    methodParamsToNull.add("0l");
                } else if (JavaType.FLOAT_PRIMITIVE.equals(javaType)) {
                    methodParamsToNull.add("0.0f");
                } else if (JavaType.DOUBLE_PRIMITIVE.equals(javaType)) {
                    methodParamsToNull.add("0.0");
                }
            } else {
                // Include a null declaration for every parameter
                methodParamsToNull.add("null");
            }
        }
        // if (METHOD_NAME_ARGUMENT_NAME.equals(methodNameConstant)) {
        bodyBuilder.appendFormalLine("if (%s.equals(%s)) {", METHOD_NAME_ARGUMENT_NAME, getConstantForMethodName(methodName).getFieldName());
        bodyBuilder.indent();
        // return MvcUriComponentsBuilder.fromMethodCall(MvcUriComponentsBuilder.on(getControllerClass()).list(null)).build();
        bodyBuilder.appendFormalLine("return %1$s.fromMethodCall(%1$s.on(%2$s()).%3$s(%4$s)).buildAndExpand(%5$s);", getNameOfJavaType(SpringletsJavaType.SPRINGLETS_MVC_URI_COMPONENTS_BUILDER), this.getControllerClassMethod.getMethodName(), methodName, StringUtils.join(methodParamsToNull, ", "), PATH_VARIABLES_ARGUMENT_NAME);
        bodyBuilder.indentRemove();
        // }
        bodyBuilder.appendFormalLine("}");
    }
    // throw new IllegalArgumentException("Invalid method name: " + METHOD_NAME_ARGUMENT_NAME);
    bodyBuilder.appendFormalLine("throw new IllegalArgumentException(\"Invalid method name: \" + %s);", METHOD_NAME_ARGUMENT_NAME);
    // Build method builder
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, toUriMethodName, SpringJavaType.URI_COMPONENTS, parameterTypes, parameterNames, bodyBuilder);
    return methodBuilder.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JavaType(org.springframework.roo.model.JavaType) 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)

Example 22 with MethodMetadata

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

the class LinkFactoryMetadata method getControllerClassMethod.

private MethodMetadata getControllerClassMethod() {
    // Define methodName
    final JavaSymbolName methodName = GET_CONTROLLER_CLASS_METHOD_NAME;
    // Define method argument types
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    // Return method if already exists
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine("return %s.class;", getNameOfJavaType(this.controller));
    // return CONTROLLER_CLASS;
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.wrapperOf(JavaType.CLASS, this.controller), null);
    // Set method body
    methodBuilder.setBodyBuilder(bodyBuilder);
    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)

Example 23 with MethodMetadata

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

the class AbstractViewGeneratorMetadataProvider method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    // Use provided MVCViewGenerationService to generate views
    MVCViewGenerationService<T> viewGenerationService = getViewGenerationService();
    ClassOrInterfaceTypeDetails controllerDetail = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
    // Getting controller metadata
    final String controllerMetadataKey = ControllerMetadata.createIdentifier(controllerDetail);
    final ControllerMetadata controllerMetadata = getMetadataService().get(controllerMetadataKey);
    if (controllerMetadata == null) {
        return null;
    }
    // Getting entity and check if is a readOnly entity or not
    final JavaType entity = controllerMetadata.getEntity();
    JavaType viewType = viewGenerationService.getType();
    JavaType itemController = null;
    JavaType collectionController = null;
    final JavaPackage controllerPackage = controllerDetail.getType().getPackage();
    final String pathPrefix = controllerMetadata.getAnnotationValues().getPathPrefix();
    if (controllerMetadata.getType() != ControllerType.ITEM) {
        // Locate ItemController
        Collection<ClassOrInterfaceTypeDetails> itemControllers = getControllerLocator().getControllers(entity, ControllerType.ITEM, viewType);
        if (itemControllers.isEmpty()) {
            // We can't create metadata "Jet"
            return null;
        } else {
            // Get controller with the same package
            itemController = filterControllerByPackageAndPrefix(itemControllers, controllerPackage, pathPrefix);
            Validate.notNull(itemControllers, "ERROR: Can't find ITEM-type controller related to controller '%s'", controllerDetail.getType().getFullyQualifiedTypeName());
        }
    }
    if (controllerMetadata.getType() != ControllerType.COLLECTION) {
        // Locate ItemController
        Collection<ClassOrInterfaceTypeDetails> collectionControllers = getControllerLocator().getControllers(entity, ControllerType.COLLECTION, viewType);
        // Get controller with the same package
        collectionController = filterControllerByPackageAndPrefix(collectionControllers, controllerPackage, pathPrefix);
        Validate.notNull(collectionController, "ERROR: Can't find Collection-type controller related to controller '%s'", controllerDetail.getType().getFullyQualifiedTypeName());
    }
    JavaType detailItemController = null;
    JavaType detailCollectionController = null;
    if (controllerMetadata.getType() == ControllerType.DETAIL) {
        if (controllerMetadata.getLastDetailsInfo().type == JpaRelationType.AGGREGATION) {
            // Locate controller item which handles details elements
            JavaType detailEntity = controllerMetadata.getLastDetailEntity();
            Collection<ClassOrInterfaceTypeDetails> detailsControllersToCheck = getControllerLocator().getControllers(detailEntity, ControllerType.ITEM, viewType);
            for (ClassOrInterfaceTypeDetails controller : detailsControllersToCheck) {
                if (controllerPackage.equals(controller.getType().getPackage())) {
                    detailItemController = controller.getType();
                    break;
                }
            }
            Validate.notNull(detailItemController, "ERROR: Can't find Item-type controller for details entity '%s' related to controller '%s'", detailEntity.getFullyQualifiedTypeName(), controllerDetail.getType().getFullyQualifiedTypeName());
            // Locate controller collection which handles details elements
            detailsControllersToCheck = getControllerLocator().getControllers(detailEntity, ControllerType.COLLECTION, viewType);
            for (ClassOrInterfaceTypeDetails controller : detailsControllersToCheck) {
                if (controllerPackage.equals(controller.getType().getPackage())) {
                    detailCollectionController = controller.getType();
                    break;
                }
            }
            Validate.notNull(detailItemController, "ERROR: Can't find Collection-type controller for details entity '%s' related to controller '%s'", detailEntity.getFullyQualifiedTypeName(), controllerDetail.getType().getFullyQualifiedTypeName());
        } else {
            // ** COMPOSITION **
            // Locate controller item which handles details elements
            Collection<ClassOrInterfaceTypeDetails> detailsControllersToCheck = getControllerLocator().getControllers(entity, ControllerType.DETAIL_ITEM, viewType);
            for (ClassOrInterfaceTypeDetails controller : detailsControllersToCheck) {
                if (controllerPackage.equals(controller.getType().getPackage())) {
                    DetailAnnotationValues annotationValues = new DetailAnnotationValues(controller);
                    if (controllerMetadata.getDetailAnnotaionFieldValue().equals(annotationValues.getRelationField())) {
                        detailItemController = controller.getType();
                    }
                    break;
                }
            }
            Validate.notNull(detailItemController, "ERROR: Can't find Detail-Item-type controller for details entity '%s' related to controller '%s' (relation '%s')", entity.getFullyQualifiedTypeName(), controllerDetail.getType().getFullyQualifiedTypeName(), controllerMetadata.getDetailAnnotaionFieldValue());
        }
    }
    if (controllerMetadata.getType() == ControllerType.DETAIL_ITEM) {
        // Locate controller item which handles details elements
        Collection<ClassOrInterfaceTypeDetails> detailsControllersToCheck = getControllerLocator().getControllers(entity, ControllerType.DETAIL, viewType);
        for (ClassOrInterfaceTypeDetails controller : detailsControllersToCheck) {
            if (controllerPackage.equals(controller.getType().getPackage())) {
                DetailAnnotationValues annotationValues = new DetailAnnotationValues(controller);
                if (controllerMetadata.getDetailAnnotaionFieldValue().equals(annotationValues.getRelationField())) {
                    detailCollectionController = controller.getType();
                }
                break;
            }
        }
        Validate.notNull(detailCollectionController, "ERROR: Can't find Detail-type controller for details entity '%s' related to controller '%s' (relation '%s')", entity.getFullyQualifiedTypeName(), controllerDetail.getType().getFullyQualifiedTypeName(), controllerMetadata.getDetailAnnotaionFieldValue());
    }
    Validate.notNull(entity, "ERROR: You should provide a valid entity for controller '%s'", controllerDetail.getType().getFullyQualifiedTypeName());
    final ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    Validate.notNull(entityDetails, "ERROR: Can't load details of %s", entity.getFullyQualifiedTypeName());
    final JpaEntityMetadata entityMetadata = getMetadataService().get(JpaEntityMetadata.createIdentifier(entityDetails));
    Validate.notNull(entityMetadata, "ERROR: Can't get Jpa Entity metada of %s", entity.getFullyQualifiedTypeName());
    MemberDetails entityMemberDetails = getMemberDetails(entityDetails);
    // Get entity plural
    final String entityPlural = getPluralService().getPlural(entity);
    final String entityIdentifierPlural = getPluralService().getPlural(entityMetadata.getCurrentIndentifierField().getFieldName());
    // Getting service and its metadata
    final JavaType service = controllerMetadata.getService();
    ClassOrInterfaceTypeDetails serviceDetails = getTypeLocationService().getTypeDetails(service);
    final String serviceMetadataKey = ServiceMetadata.createIdentifier(serviceDetails);
    registerDependency(serviceMetadataKey, metadataIdentificationString);
    final ServiceMetadata serviceMetadata = getMetadataService().get(serviceMetadataKey);
    // Prepare information about ONE-TO-ONE relations
    final List<Pair<RelationInfo, JpaEntityMetadata>> compositionRelationOneToOne = new ArrayList<Pair<RelationInfo, JpaEntityMetadata>>();
    ClassOrInterfaceTypeDetails childEntityDetails;
    JpaEntityMetadata childEntityMetadata;
    for (RelationInfo info : entityMetadata.getRelationInfos().values()) {
        if (info.cardinality == Cardinality.ONE_TO_ONE) {
            childEntityDetails = getTypeLocationService().getTypeDetails(info.childType);
            childEntityMetadata = getMetadataService().get(JpaEntityMetadata.createIdentifier(childEntityDetails));
            compositionRelationOneToOne.add(Pair.of(info, childEntityMetadata));
        }
    }
    List<FieldMetadata> dateTimeFields;
    List<FieldMetadata> enumFields;
    if (controllerMetadata.getType() == ControllerType.DETAIL || controllerMetadata.getType() == ControllerType.DETAIL_ITEM) {
        ClassOrInterfaceTypeDetails childCid = getTypeLocationService().getTypeDetails(controllerMetadata.getLastDetailEntity());
        MemberDetails childMemberDetails = getMemberDetails(childCid);
        dateTimeFields = getDateTimeFields(childMemberDetails);
        enumFields = getEnumFields(childMemberDetails);
    } else {
        dateTimeFields = getDateTimeFields(entityMemberDetails);
        enumFields = getEnumFields(entityMemberDetails);
    }
    Map<String, MethodMetadata> findersToAdd = new HashMap<String, MethodMetadata>();
    Map<JavaType, List<FieldMetadata>> formBeansDateTimeFields = new HashMap<JavaType, List<FieldMetadata>>();
    Map<JavaType, List<FieldMetadata>> formBeansEnumFields = new HashMap<JavaType, List<FieldMetadata>>();
    // Getting annotated finders
    final SearchAnnotationValues searchAnnotationValues = new SearchAnnotationValues(governorPhysicalTypeMetadata);
    // Add finders only if controller is of search type
    Map<String, JavaType> finderReturnTypes = new HashMap<String, JavaType>();
    Map<String, JavaType> finderFormBeans = new HashMap<String, JavaType>();
    if (controllerMetadata.getType() == ControllerType.SEARCH && searchAnnotationValues != null && searchAnnotationValues.getFinders() != null) {
        List<String> finders = new ArrayList<String>(Arrays.asList(searchAnnotationValues.getFinders()));
        // Search indicated finders in its related service
        for (MethodMetadata serviceFinder : serviceMetadata.getFinders()) {
            String finderName = serviceFinder.getMethodName().getSymbolName();
            if (finders.contains(finderName)) {
                findersToAdd.put(finderName, serviceFinder);
                // FormBean parameters is always the first finder parameter
                JavaType formBean = serviceFinder.getParameterTypes().get(0).getJavaType();
                // Save the associated formBean to the current finder
                finderFormBeans.put(finderName, formBean);
                // Getting the returnType for this finder
                JavaType returnType = serviceFinder.getReturnType();
                // Save the associated returnType to the current finder
                finderReturnTypes.put(finderName, returnType);
                // Get dateTime and Enum of formBean
                MemberDetails formBeanDetails = getMemberDetails(formBean);
                List<FieldMetadata> formBeanDateTimeFields = getDateTimeFields(formBeanDetails);
                List<FieldMetadata> formBeanEnumFields = getEnumFields(formBeanDetails);
                if (!formBeanDateTimeFields.isEmpty()) {
                    formBeansDateTimeFields.put(formBean, formBeanDateTimeFields);
                }
                if (!formBeanEnumFields.isEmpty()) {
                    formBeansEnumFields.put(formBean, formBeanEnumFields);
                }
                // Add dependencies between modules
                List<JavaType> types = new ArrayList<JavaType>();
                types.add(serviceFinder.getReturnType());
                types.addAll(serviceFinder.getReturnType().getParameters());
                for (AnnotatedJavaType parameter : serviceFinder.getParameterTypes()) {
                    types.add(parameter.getJavaType());
                    types.addAll(parameter.getJavaType().getParameters());
                }
                for (JavaType parameter : types) {
                    getTypeLocationService().addModuleDependency(governorPhysicalTypeMetadata.getType().getModule(), parameter);
                }
                finders.remove(finderName);
            }
        }
        // Check all finders have its service method
        if (!finders.isEmpty()) {
            throw new IllegalArgumentException(String.format("ERROR: Service %s does not have these finder methods: %s ", service.getFullyQualifiedTypeName(), StringUtils.join(finders, ", ")));
        }
    }
    T viewMetadata = createMetadataInstance(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, controllerMetadata, serviceMetadata, entityMetadata, entityPlural, entityIdentifierPlural, compositionRelationOneToOne, itemController, collectionController, dateTimeFields, enumFields, findersToAdd, formBeansDateTimeFields, formBeansEnumFields, detailItemController, detailCollectionController);
    // Fill view context
    ViewContext ctx = viewGenerationService.createViewContext(controllerMetadata, entity, entityMetadata, viewMetadata);
    // Checking if Spring Security has been installed
    if (getProjectOperations().isFeatureInstalled(FeatureNames.SECURITY)) {
        ctx.setSecurityEnabled(true);
    }
    final String module = controllerDetail.getType().getModule();
    switch(controllerMetadata.getType()) {
        case COLLECTION:
            // Obtain the details controllers to use only them that includes "list" value in the
            // views parameter of @RooDetail annotation. If @RooDetail doesn't include views
            // parameter, include it.
            List<T> detailsControllersForListView = getDetailsControllers(controllerMetadata, controllerPackage, entity, viewType, "list");
            // Add list view
            if (viewMetadata.shouldGenerateView("list")) {
                viewGenerationService.addListView(module, entityMetadata, entityMemberDetails, detailsControllersForListView, ctx);
            }
            if (!entityMetadata.isReadOnly()) {
                // If not readOnly, add create view
                if (viewMetadata.shouldGenerateView("create")) {
                    viewGenerationService.addCreateView(module, entityMetadata, entityMemberDetails, ctx);
                }
                if (viewMetadata.shouldGenerateView("listDeleteModal")) {
                    // If not readOnly, add the modal dialogs for delete and delete batch
                    viewGenerationService.addListDeleteModalView(module, entityMetadata, entityMemberDetails, ctx);
                    if (viewMetadata.shouldGenerateView("listDeleteModalBatch")) {
                        viewGenerationService.addListDeleteModalBatchView(module, entityMetadata, entityMemberDetails, ctx);
                    }
                }
            }
            break;
        case ITEM:
            // Obtain the details controllers to use only them that includes "show" value in the
            // views parameter of @RooDetail annotation.
            List<T> detailsControllersForShowView = getDetailsControllers(controllerMetadata, controllerPackage, entity, viewType, "show");
            // Add show view
            if (viewMetadata.shouldGenerateView("show")) {
                viewGenerationService.addShowView(module, entityMetadata, entityMemberDetails, detailsControllersForShowView, ctx);
            }
            if (viewMetadata.shouldGenerateView("showInLine")) {
                // Add showInline view
                viewGenerationService.addShowInlineView(module, entityMetadata, entityMemberDetails, ctx);
            }
            if (!entityMetadata.isReadOnly() && viewMetadata.shouldGenerateView("edit")) {
                // If not readOnly, add update view
                viewGenerationService.addUpdateView(module, entityMetadata, entityMemberDetails, ctx);
            }
            break;
        case DETAIL:
            viewGenerationService.addDetailsViews(module, entityMetadata, entityMemberDetails, controllerMetadata, viewMetadata, ctx);
            // Add this metadata as upstream dependency for parent controllers
            // for updating views of parent controllers
            JavaType parentEntity = entityMetadata.getAnnotatedEntity();
            List<ClassOrInterfaceTypeDetails> parentControllers = new ArrayList<ClassOrInterfaceTypeDetails>();
            parentControllers.addAll(getControllerLocator().getControllers(parentEntity, ControllerType.COLLECTION, viewType));
            parentControllers.addAll(getControllerLocator().getControllers(parentEntity, ControllerType.ITEM, viewType));
            parentControllers.addAll(getControllerLocator().getControllers(parentEntity, ControllerType.SEARCH, viewType));
            for (ClassOrInterfaceTypeDetails parentController : parentControllers) {
                String viewMetadatIdentifier = createLocalIdentifier(parentController);
                registerDependency(metadataIdentificationString, viewMetadatIdentifier);
            }
            break;
        case DETAIL_ITEM:
            viewGenerationService.addDetailsItemViews(module, entityMetadata, entityMemberDetails, controllerMetadata, viewMetadata, ctx);
            RelationInfoExtended last = controllerMetadata.getLastDetailsInfo();
            ClassOrInterfaceTypeDetails childCid = getTypeLocationService().getTypeDetails(last.childType);
            MemberDetails detailMemberDetails = getMemberDetails(childCid);
            // Update i18n labels of detail entity
            if (shouldGenerateI18nLabels()) {
                Map<String, String> labels = viewGenerationService.getI18nLabels(detailMemberDetails, last.childType, last.childEntityMetadata, controllerMetadata, module, ctx);
                getI18nOperations().addOrUpdateLabels(module, labels);
            }
            break;
        case SEARCH:
            // in @RooSearch annotation
            if (searchAnnotationValues != null && searchAnnotationValues.getFinders() != null) {
                List<String> finders = new ArrayList<String>(Arrays.asList(searchAnnotationValues.getFinders()));
                // Generating views for all finders
                for (String finderName : finders) {
                    // Getting the formBean for this finder
                    JavaType formBean = finderFormBeans.get(finderName);
                    viewGenerationService.addFinderFormView(module, entityMetadata, viewMetadata, formBean, finderName, ctx);
                    // Getting the returnType for this finder
                    JavaType returnType = finderReturnTypes.get(finderName);
                    if (!returnType.getParameters().isEmpty()) {
                        returnType = returnType.getParameters().get(0);
                    }
                    // Obtain the details controllers to use only them that includes this finder value in the
                    // views parameter of @RooDetail annotation.
                    List<T> detailsControllersForFinderListView = getDetailsControllers(controllerMetadata, controllerPackage, entity, viewType, finderName);
                    viewGenerationService.addFinderListView(module, entityMetadata, entityMemberDetails, viewMetadata, formBean, returnType, finderName, detailsControllersForFinderListView, ctx);
                }
            }
            break;
        default:
            throw new IllegalArgumentException();
    }
    // be included on it. Must be fixed on future versions.
    if (shouldUpdateMenu()) {
        viewGenerationService.updateMenuView(module, ctx);
    }
    if (shouldGenerateI18nLabels()) {
        // Update i18n labels
        Map<String, String> labels = viewGenerationService.getI18nLabels(entityMemberDetails, entity, entityMetadata, controllerMetadata, module, ctx);
        getI18nOperations().addOrUpdateLabels(module, labels);
        // Add labels for child composite entity as well
        for (Pair<RelationInfo, JpaEntityMetadata> compositionRelation : compositionRelationOneToOne) {
            MemberDetails childMemberDetails = getMemberDetailsScanner().getMemberDetails(this.getClass().getName(), getTypeLocationService().getTypeDetails(compositionRelation.getKey().childType));
            Map<String, String> i18nLabels = viewGenerationService.getI18nLabels(childMemberDetails, compositionRelation.getKey().childType, compositionRelation.getValue(), null, module, ctx);
            getI18nOperations().addOrUpdateLabels(module, i18nLabels);
        }
    }
    // Register dependency between JavaBeanMetadata and this one
    final String javaBeanMetadataKey = JavaBeanMetadata.createIdentifier(entityDetails);
    registerDependency(javaBeanMetadataKey, metadataIdentificationString);
    // Register dependency between JpaEntityMetadata and this one
    final String jpaEntityMetadataKey = JpaEntityMetadata.createIdentifier(entityDetails);
    registerDependency(jpaEntityMetadataKey, metadataIdentificationString);
    return viewMetadata;
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) SearchAnnotationValues(org.springframework.roo.addon.web.mvc.controller.addon.finder.SearchAnnotationValues) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DetailAnnotationValues(org.springframework.roo.addon.web.mvc.controller.addon.DetailAnnotationValues) JavaPackage(org.springframework.roo.model.JavaPackage) ArrayList(java.util.ArrayList) List(java.util.List) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) Pair(org.apache.commons.lang3.tuple.Pair) ControllerMetadata(org.springframework.roo.addon.web.mvc.controller.addon.ControllerMetadata) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) RelationInfo(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo) RelationInfoExtended(org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) ServiceMetadata(org.springframework.roo.addon.layers.service.addon.ServiceMetadata)

Example 24 with MethodMetadata

use of org.springframework.roo.classpath.details.MethodMetadata 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 25 with MethodMetadata

use of org.springframework.roo.classpath.details.MethodMetadata 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();
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) RelationInfoExtended(org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) ServiceMetadata(org.springframework.roo.addon.layers.service.addon.ServiceMetadata) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)149 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)119 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)115 ArrayList (java.util.ArrayList)111 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)110 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)92 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)73 JavaType (org.springframework.roo.model.JavaType)72 SpringJavaType (org.springframework.roo.model.SpringJavaType)40 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)39 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)38 JdkJavaType (org.springframework.roo.model.JdkJavaType)28 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)27 RooJavaType (org.springframework.roo.model.RooJavaType)22 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)18 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)18 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)17 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)16 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)16 JpaJavaType (org.springframework.roo.model.JpaJavaType)15