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();
}
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();
}
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;
}
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;
}
Aggregations