Search in sources :

Example 11 with RelationInfoExtended

use of org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended in project spring-roo by spring-projects.

the class ThymeleafMetadata method getShowDetailMethod.

/**
 * This method provides the "show" detail method using Thymeleaf view
 * response type
 *
 * @return MethodMetadata
 */
private MethodMetadata getShowDetailMethod() {
    // Define methodName
    final JavaSymbolName methodName = SHOW_METHOD_NAME;
    final RelationInfoExtended info = controllerMetadata.getLastDetailsInfo();
    final JavaType parentEntity = info.entityType;
    final JavaType entity = info.childType;
    final String entityItemName = StringUtils.uncapitalize(entity.getSimpleTypeName());
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(new AnnotatedJavaType(parentEntity, ANN_METADATA_MODEL_ATTRIBUTE));
    parameterTypes.add(new AnnotatedJavaType(entity, ANN_METADATA_MODEL_ATTRIBUTE));
    parameterTypes.add(MODEL_PARAM);
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.add(new JavaSymbolName(StringUtils.uncapitalize(parentEntity.getSimpleTypeName())));
    parameterNames.add(new JavaSymbolName(entityItemName));
    parameterNames.add(MODEL_PARAM_NAME);
    // Adding annotations
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    // Adding @RequestMapping annotation
    AnnotationMetadataBuilder getMappingAnnotation = new AnnotationMetadataBuilder(GET_MAPPING);
    getMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
    annotations.add(getMappingAnnotation);
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // model.addAttribute("entity", entityParam)
    bodyBuilder.appendFormalLine("model.addAttribute(\"%s\", %s);", entityItemName, entityItemName);
    // return new ModelAndView("customerorders/details/show");
    bodyBuilder.appendFormalLine("return new %s(\"%s/%s/show\");", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW), viewsPath, controllerMetadata.getDetailsPathAsString("/"));
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, SpringJavaType.MODEL_AND_VIEW, parameterTypes, parameterNames, bodyBuilder);
    methodBuilder.setAnnotations(annotations);
    return methodBuilder.build();
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) Jsr303JavaType(org.springframework.roo.model.Jsr303JavaType) 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) RelationInfoExtended(org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 12 with RelationInfoExtended

use of org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended in project spring-roo by spring-projects.

the class ThymeleafMetadata method getCreateDetailsCompositionMethod.

/**
 * This method provides the "create" method for details association
 * relationship using Thymeleaf view response type
 *
 * @return MethodMetadata
 */
private MethodMetadata getCreateDetailsCompositionMethod() {
    RelationInfo detailsInfo = controllerMetadata.getLastDetailsInfo();
    final ServiceMetadata detailsServiceMetadata = controllerMetadata.getServiceMetadataForEntity(detailsInfo.entityType);
    final MethodMetadata addToRelationMethod = detailsServiceMetadata.getAddToRelationMethods().get(detailsInfo);
    final FieldMetadata detailsServiceField = controllerMetadata.getDetailsServiceFields(detailsInfo.entityType);
    final RelationInfoExtended info = controllerMetadata.getLastDetailsInfo();
    final JavaType parentEntity = info.entityType;
    final JavaType entity = info.childType;
    final String entityItemName = StringUtils.uncapitalize(entity.getSimpleTypeName());
    final String parentItemName = StringUtils.uncapitalize(parentEntity.getSimpleTypeName());
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(new AnnotatedJavaType(parentEntity, ANN_METADATA_MODEL_ATTRIBUTE));
    parameterTypes.add(new AnnotatedJavaType(entity, ANN_METADATA_VALID, ANN_METADATA_MODEL_ATTRIBUTE));
    parameterTypes.add(new AnnotatedJavaType(SpringJavaType.BINDING_RESULT));
    parameterTypes.add(MODEL_PARAM);
    // Define methodName
    final JavaSymbolName methodName = CREATE_METHOD_NAME;
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Adding annotations
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    // Adding @PostMapping annotation
    AnnotationMetadataBuilder postMappingAnnotation = new AnnotationMetadataBuilder(SpringJavaType.POST_MAPPING);
    postMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
    annotations.add(postMappingAnnotation);
    this.mvcMethodNames.put(methodName.getSymbolName(), methodName.getSymbolName());
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.add(new JavaSymbolName(parentItemName));
    parameterNames.add(new JavaSymbolName(entityItemName));
    parameterNames.add(new JavaSymbolName("result"));
    parameterNames.add(MODEL_PARAM_NAME);
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // if (result.hasErrors()) {
    bodyBuilder.appendFormalLine("if (result.hasErrors()) {");
    bodyBuilder.indent();
    // populateFormats(model);
    bodyBuilder.appendFormalLine("%s(model);", populateFormMethod.getMethodName());
    bodyBuilder.newLine();
    // return new ModelAndView("customerorders/details/edit");
    bodyBuilder.appendFormalLine("return new %s(\"%s/%s/create\");", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW), viewsPath, controllerMetadata.getDetailsPathAsString("/"));
    bodyBuilder.indentRemove();
    // }
    bodyBuilder.appendFormalLine("}");
    // customerOrderService.addToDetails(customerOrder,
    // Collections.singleton(orderDetail));
    bodyBuilder.appendFormalLine("%s().%s(%s,%s.singleton(%s));", getAccessorMethod(detailsServiceField).getMethodName(), addToRelationMethod.getMethodName(), parentItemName, getNameOfJavaType(JavaType.COLLECTIONS), entityItemName);
    // return new ModelAndView("redirect:" +
    // collectionLink.to("list").toUriString());
    bodyBuilder.appendFormalLine("return new %s(\"redirect:\" + %s().to(%s.LIST).toUriString());", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW), getAccessorMethod(this.collectionMethodLinkBuilderFactoryField).getMethodName(), getNameOfJavaType(relatedCollectionLinkFactory));
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, SpringJavaType.MODEL_AND_VIEW, 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) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) Jsr303JavaType(org.springframework.roo.model.Jsr303JavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) 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) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) ServiceMetadata(org.springframework.roo.addon.layers.service.addon.ServiceMetadata) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 13 with RelationInfoExtended

use of org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended in project spring-roo by spring-projects.

the class ThymeleafMetadataProviderImpl method createMetadataInstance.

@Override
protected ThymeleafMetadata createMetadataInstance(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final ControllerMetadata controllerMetadata, final ServiceMetadata serviceMetadata, final JpaEntityMetadata entityMetadata, final String entityPlural, final String entityIdentifierPlural, final List<Pair<RelationInfo, JpaEntityMetadata>> compositionRelationOneToOne, final JavaType itemController, final JavaType collectionController, final List<FieldMetadata> dateTimeFields, final List<FieldMetadata> enumFields, final Map<String, MethodMetadata> findersToAdd, final Map<JavaType, List<FieldMetadata>> formBeansDateTimeFields, final Map<JavaType, List<FieldMetadata>> formBeansEnumFields, final JavaType detailsItemController, final JavaType detailsCollectionController) {
    final ThymeleafAnnotationValues annotationValues = new ThymeleafAnnotationValues(governorPhysicalTypeMetadata);
    // Get related controller for select2 request
    JavaType relatedCollectionController = null;
    JavaType relatedItemController = null;
    RelationInfoExtended info = controllerMetadata.getLastDetailsInfo();
    if (controllerMetadata.getType() == ControllerType.DETAIL && info.type == JpaRelationType.AGGREGATION) {
        Collection<ClassOrInterfaceTypeDetails> controllers = getControllerLocator().getControllers(info.childType, ControllerType.COLLECTION, RooJavaType.ROO_THYMELEAF);
        final String prefix = controllerMetadata.getAnnotationValues().getPathPrefix();
        for (ClassOrInterfaceTypeDetails controller : controllers) {
            // use annotation values to avoid cyclic dependencies
            ControllerAnnotationValues valuesToCheck = new ControllerAnnotationValues(controller);
            if (StringUtils.equals(prefix, valuesToCheck.getPathPrefix())) {
                relatedCollectionController = controller.getType();
                break;
            }
        }
        Validate.notNull(relatedCollectionController, "Can't found Collection-type controller of entity '%s' and pathPrefix '%s' required by '%s' controller", info.childType, prefix, controllerMetadata.getDestination());
        controllers = getControllerLocator().getControllers(info.childType, ControllerType.ITEM, RooJavaType.ROO_THYMELEAF);
        for (ClassOrInterfaceTypeDetails controller : controllers) {
            // use annotation values to avoid cyclic dependencies
            ControllerAnnotationValues valuesToCheck = new ControllerAnnotationValues(controller);
            if (StringUtils.equals(prefix, valuesToCheck.getPathPrefix())) {
                relatedItemController = controller.getType();
                break;
            }
        }
        Validate.notNull(relatedItemController, "Can't found Item-type controller of entity '%s' and pathPrefix '%s' required by '%s' controller", info.childType, prefix, controllerMetadata.getDestination());
    }
    Validate.notNull(getJasperReportsMap(), "Couldn't find class `JasperReportsExporter` in project.");
    // Get valid entity fields (those suitable for reports)
    JavaType defaultReturnType = serviceMetadata.getCurrentFindAllWithGlobalSearchMethod().getReturnType().getParameters().get(0);
    List<FieldMetadata> entityFields = getReturnTypeValidFields(getMemberDetailsScanner().getMemberDetails(this.getClass().getName(), getTypeLocationService().getTypeDetails(defaultReturnType)).getFields());
    // Getting the related linkFactory for this controller
    Set<ClassOrInterfaceTypeDetails> linkFactories = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_LINK_FACTORY);
    JavaType relatedCollectionLinkFactory = null;
    JavaType relatedItemLinkFactory = null;
    JavaType collectionControllerToCheck = collectionController;
    if (collectionControllerToCheck == null) {
        collectionControllerToCheck = governorPhysicalTypeMetadata.getType();
    }
    JavaType itemControllerToCheck = itemController;
    if (itemControllerToCheck == null) {
        itemControllerToCheck = governorPhysicalTypeMetadata.getType();
    }
    for (ClassOrInterfaceTypeDetails linkFactory : linkFactories) {
        LinkFactoryAnnotationValues linkAnnotationValues = new LinkFactoryAnnotationValues(linkFactory);
        // Getting link factory of the collection controller
        if (linkAnnotationValues.getController().equals(collectionControllerToCheck)) {
            relatedCollectionLinkFactory = linkFactory.getType();
        } else if (linkAnnotationValues.getController().equals(itemControllerToCheck)) {
            relatedItemLinkFactory = linkFactory.getType();
        }
    }
    final ThymeleafMetadata metadata = new ThymeleafMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, controllerMetadata, serviceMetadata, entityMetadata, entityPlural, entityIdentifierPlural, compositionRelationOneToOne, itemController, collectionController, dateTimeFields, enumFields, findersToAdd, formBeansDateTimeFields, formBeansEnumFields, detailsItemController, detailsCollectionController, relatedCollectionController, relatedItemController, entityFields, getJasperReportsMap(), relatedCollectionLinkFactory, relatedItemLinkFactory);
    return metadata;
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ControllerAnnotationValues(org.springframework.roo.addon.web.mvc.controller.addon.ControllerAnnotationValues) RelationInfoExtended(org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended) LinkFactoryAnnotationValues(org.springframework.roo.addon.web.mvc.thymeleaf.addon.link.factory.LinkFactoryAnnotationValues) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)

Example 14 with RelationInfoExtended

use of org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended in project spring-roo by spring-projects.

the class AbstractViewGenerationService method createDetailEntityItem.

/**
 * Create a new instance of {@link DetailEntityItem}. Implementation can
 * override this method to include it own information or extend defaults.
 *
 * @param detailController
 * @param detailSuffix
 * @param ctx
 * @param string
 * @param entityMetadata
 * @param entity
 * @return
 */
protected DetailEntityItem createDetailEntityItem(T detailController, MemberDetails entityMembers, JpaEntityMetadata entityMetadata, String entityName, ViewContext<T> ctx, String detailSuffix, EntityItem rootEntity) {
    ControllerMetadata controllerMetadata = detailController.getControllerMetadata();
    RelationInfoExtended last = controllerMetadata.getLastDetailsInfo();
    ClassOrInterfaceTypeDetails childEntityDetails = getTypeLocationService().getTypeDetails(last.childType);
    JpaEntityMetadata childEntityMetadata = last.childEntityMetadata;
    String controllerPath = getControllerOperations().getBasePathForController(controllerMetadata.getDestination());
    DetailEntityItem detailItem = new DetailEntityItem(childEntityMetadata, controllerMetadata, controllerPath, detailSuffix, rootEntity);
    // Saving necessary configuration
    detailItem.addConfigurationElement("referencedFieldType", last.childType.getSimpleTypeName());
    // Getting identifier field
    detailItem.addConfigurationElement("identifierField", childEntityMetadata.getCurrentIndentifierField().getFieldName().getSymbolName());
    // Getting referencedfield label plural
    detailItem.addConfigurationElement("referencedFieldLabel", FieldItem.buildLabel(entityName, last.fieldName));
    // Getting all referenced fields
    List<FieldMetadata> referencedFields = null;
    // Get the repository related with the child entity to check the default return type
    RepositoryJpaMetadata repository = getRepositoryJpaLocator().getFirstRepositoryMetadata(childEntityMetadata.getAnnotatedEntity());
    // All views should have a repository
    Validate.notNull(repository, "ERROR: The provided child entity should have an associated repository to be able " + "to generate the list view.");
    // Obtain the defaultReturnType
    JavaType defaultReturnType = repository.getDefaultReturnType();
    // The defaultReturnType must not be null. If it's not an entity projection,
    // it must be an entity
    Validate.notNull(defaultReturnType, "ERROR: The repository associated to the provided entity should define a defaultReturnType");
    // Obtain details of the provided defaultReturnType. If not exists as type, show an error
    ClassOrInterfaceTypeDetails defaultReturnTypeCid = getTypeLocationService().getTypeDetails(defaultReturnType);
    Validate.notNull(defaultReturnTypeCid, "ERROR: The provided defaultReturnType is not a valid type");
    MemberDetails defaultReturnTypeDetails = getMemberDetailsScanner().getMemberDetails(getClass().toString(), defaultReturnTypeCid);
    Validate.notNull(defaultReturnTypeDetails, "ERROR: Is not possible to obtain any detail from the " + "provided defaultReturnType.");
    List<FieldMetadata> defaultReturnTypeFields = defaultReturnTypeDetails.getFields();
    if (defaultReturnTypeFields.isEmpty()) {
        referencedFields = getEditableFields(getMemberDetailsScanner().getMemberDetails(getClass().toString(), childEntityDetails).getFields());
    } else {
        referencedFields = getEditableFields(defaultReturnTypeFields);
    }
    detailItem.addConfigurationElement("referenceFieldFields", getFieldViewItems(childEntityMetadata, referencedFields, entityName + "." + last.fieldName, true, ctx, StringUtils.EMPTY));
    detailItem.addConfigurationElement("fields", getFieldViewItems(childEntityMetadata, referencedFields, detailItem.getEntityName(), true, ctx, StringUtils.EMPTY));
    return detailItem;
}
Also used : RepositoryJpaMetadata(org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata) ControllerMetadata(org.springframework.roo.addon.web.mvc.controller.addon.ControllerMetadata) Jsr303JavaType(org.springframework.roo.model.Jsr303JavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) DetailEntityItem(org.springframework.roo.addon.web.mvc.views.components.DetailEntityItem) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) RelationInfoExtended(org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails)

Aggregations

RelationInfoExtended (org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended)14 ArrayList (java.util.ArrayList)12 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)12 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)12 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)11 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)11 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)11 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)11 JavaType (org.springframework.roo.model.JavaType)10 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)9 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)8 JdkJavaType (org.springframework.roo.model.JdkJavaType)8 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)8 SpringJavaType (org.springframework.roo.model.SpringJavaType)8 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)8 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)3 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)3 JpaJavaType (org.springframework.roo.model.JpaJavaType)3 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)2 ControllerMetadata (org.springframework.roo.addon.web.mvc.controller.addon.ControllerMetadata)2