@Test public void testGenerateContextForModelWithFilters() { FormElementFilter nameFilter = new FormElementFilter("name", o -> true); FormElementFilter lastNameFilter = new FormElementFilter("lastName", o -> false); FormElementFilter addressStreetFilter = new FormElementFilter("address.street", o -> true); FormElementFilter addressNumFilter = new FormElementFilter("address.number", o -> false); StaticModelFormRenderingContext context = dynamicFormModelGenerator.getContextForModel(model, nameFilter, lastNameFilter, addressStreetFilter, addressNumFilter); assertEquals(3, context.getAvailableForms().size()); FormDefinition rootForm = context.getRootForm(); assertNotNull(rootForm); assertEquals(rootForm.getFields().size(), rootForm.getLayoutTemplate().getRows().size()); assertNotNull(rootForm.getFieldByBinding("name")); assertNull(rootForm.getFieldByBinding("lastName")); FormDefinition addressForm = context.getAvailableForms().get(Address.class.getName()); assertNotNull(addressForm); assertEquals(addressForm.getFields().size(), addressForm.getLayoutTemplate().getRows().size()); assertNotNull(addressForm.getFieldByBinding("street")); assertNull(addressForm.getFieldByBinding("number")); }
public StaticModelFormRenderingContext getContextForModel(Object model, FormElementFilter... filters) { PortablePreconditions.checkNotNull("model", model); Optional<FormElementFilter[]> optional = Optional.ofNullable(filters); Stream<FormElementFilter> streamFilter = optional.map(value -> Stream.of(value)).orElseGet(Stream::empty); List<FormElementFilter> rootFormElemenFilters = new ArrayList<>(); List<FormElementFilter> nestedFormFilters = new ArrayList<>(); streamFilter.forEach(filter -> { if(!filter.getElementName().contains(".")) { rootFormElemenFilters.add(filter); } else { nestedFormFilters.add(filter); } }); FormDefinition formDefinition = formBuildingService.generateFormForModel(model, rootFormElemenFilters.stream().toArray(FormElementFilter[]::new)); if (formDefinition == null) { return null; } StaticModelFormRenderingContext context = new StaticModelFormRenderingContext(String.valueOf(System.currentTimeMillis())); context.setModel(model); context.setRootForm(formDefinition); if (context.getModel() != null) { initNestedFormSettings(formDefinition, context.getModel(), context, nestedFormFilters); } else { initNestedFormSettings(formDefinition,null, context, nestedFormFilters); } return context; }
DynamicFormModelGenerator { public StaticModelFormRenderingContext getContextForModel(Object model, FormElementFilter... filters) { PortablePreconditions.checkNotNull("model", model); Optional<FormElementFilter[]> optional = Optional.ofNullable(filters); Stream<FormElementFilter> streamFilter = optional.map(value -> Stream.of(value)).orElseGet(Stream::empty); List<FormElementFilter> rootFormElemenFilters = new ArrayList<>(); List<FormElementFilter> nestedFormFilters = new ArrayList<>(); streamFilter.forEach(filter -> { if(!filter.getElementName().contains(".")) { rootFormElemenFilters.add(filter); } else { nestedFormFilters.add(filter); } }); FormDefinition formDefinition = formBuildingService.generateFormForModel(model, rootFormElemenFilters.stream().toArray(FormElementFilter[]::new)); if (formDefinition == null) { return null; } StaticModelFormRenderingContext context = new StaticModelFormRenderingContext(String.valueOf(System.currentTimeMillis())); context.setModel(model); context.setRootForm(formDefinition); if (context.getModel() != null) { initNestedFormSettings(formDefinition, context.getModel(), context, nestedFormFilters); } else { initNestedFormSettings(formDefinition,null, context, nestedFormFilters); } return context; } }
DynamicFormModelGenerator { public StaticModelFormRenderingContext getContextForModel(Object model, FormElementFilter... filters) { PortablePreconditions.checkNotNull("model", model); Optional<FormElementFilter[]> optional = Optional.ofNullable(filters); Stream<FormElementFilter> streamFilter = optional.map(value -> Stream.of(value)).orElseGet(Stream::empty); List<FormElementFilter> rootFormElemenFilters = new ArrayList<>(); List<FormElementFilter> nestedFormFilters = new ArrayList<>(); streamFilter.forEach(filter -> { if(!filter.getElementName().contains(".")) { rootFormElemenFilters.add(filter); } else { nestedFormFilters.add(filter); } }); FormDefinition formDefinition = formBuildingService.generateFormForModel(model, rootFormElemenFilters.stream().toArray(FormElementFilter[]::new)); if (formDefinition == null) { return null; } StaticModelFormRenderingContext context = new StaticModelFormRenderingContext(String.valueOf(System.currentTimeMillis())); context.setModel(model); context.setRootForm(formDefinition); if (context.getModel() != null) { initNestedFormSettings(formDefinition, context.getModel(), context, nestedFormFilters); } else { initNestedFormSettings(formDefinition,null, context, nestedFormFilters); } return context; } @Inject  DynamicFormModelGenerator(FormBuildingService formBuildingService,
                                     PropertyValueExtractor propertyValueExtractor); }
DynamicFormModelGenerator { public StaticModelFormRenderingContext getContextForModel(Object model, FormElementFilter... filters) { PortablePreconditions.checkNotNull("model", model); Optional<FormElementFilter[]> optional = Optional.ofNullable(filters); Stream<FormElementFilter> streamFilter = optional.map(value -> Stream.of(value)).orElseGet(Stream::empty); List<FormElementFilter> rootFormElemenFilters = new ArrayList<>(); List<FormElementFilter> nestedFormFilters = new ArrayList<>(); streamFilter.forEach(filter -> { if(!filter.getElementName().contains(".")) { rootFormElemenFilters.add(filter); } else { nestedFormFilters.add(filter); } }); FormDefinition formDefinition = formBuildingService.generateFormForModel(model, rootFormElemenFilters.stream().toArray(FormElementFilter[]::new)); if (formDefinition == null) { return null; } StaticModelFormRenderingContext context = new StaticModelFormRenderingContext(String.valueOf(System.currentTimeMillis())); context.setModel(model); context.setRootForm(formDefinition); if (context.getModel() != null) { initNestedFormSettings(formDefinition, context.getModel(), context, nestedFormFilters); } else { initNestedFormSettings(formDefinition,null, context, nestedFormFilters); } return context; } @Inject  DynamicFormModelGenerator(FormBuildingService formBuildingService,
                                     PropertyValueExtractor propertyValueExtractor); StaticModelFormRenderingContext getContextForModel(Object model, FormElementFilter... filters); }
DynamicFormModelGenerator { public StaticModelFormRenderingContext getContextForModel(Object model, FormElementFilter... filters) { PortablePreconditions.checkNotNull("model", model); Optional<FormElementFilter[]> optional = Optional.ofNullable(filters); Stream<FormElementFilter> streamFilter = optional.map(value -> Stream.of(value)).orElseGet(Stream::empty); List<FormElementFilter> rootFormElemenFilters = new ArrayList<>(); List<FormElementFilter> nestedFormFilters = new ArrayList<>(); streamFilter.forEach(filter -> { if(!filter.getElementName().contains(".")) { rootFormElemenFilters.add(filter); } else { nestedFormFilters.add(filter); } }); FormDefinition formDefinition = formBuildingService.generateFormForModel(model, rootFormElemenFilters.stream().toArray(FormElementFilter[]::new)); if (formDefinition == null) { return null; } StaticModelFormRenderingContext context = new StaticModelFormRenderingContext(String.valueOf(System.currentTimeMillis())); context.setModel(model); context.setRootForm(formDefinition); if (context.getModel() != null) { initNestedFormSettings(formDefinition, context.getModel(), context, nestedFormFilters); } else { initNestedFormSettings(formDefinition,null, context, nestedFormFilters); } return context; } @Inject  DynamicFormModelGenerator(FormBuildingService formBuildingService,
                                     PropertyValueExtractor propertyValueExtractor); StaticModelFormRenderingContext getContextForModel(Object model, FormElementFilter... filters);  }