/******************************************************************************************************************* * * A facility simplified version of * {@link #toCompositePresentationModel(java.lang.Iterable, java.util.function.Function)} without a function to * supply additional roles. * * @param i the {@code Iterable} * @return the composite {@code PresentationModel} * ******************************************************************************************************************/ @Nonnull public static <T extends As> PresentationModel toCompositePresentationModel (final @Nonnull Iterable<T> i) { return toCompositePresentationModel(i, o -> new Object[0]); }
/******************************************************************************************************************* * * A facility method that creates a composite {@link PresentationModel} out of a stream of objects. For each object * in the stream, its {@code PresentationModel} is created by means of invoking its {@link Presentable} role. * Then all the {@code PresentationModel}s are aggregated into the composite. * * This method accepts an {@link Iterable}. A function which creates specific roles for each * {@code PresentationModel} can be supplied. The function can return a single role or multiple roles in form of * an {@code Object[]}. * * @param i the {@code Iterable} * @param roleCreator the function to create roles * @return the composite {@code PresentationModel} * ******************************************************************************************************************/ @Nonnull public static <T extends As> PresentationModel toCompositePresentationModel ( final @Nonnull Iterable<T> i, final @Nonnull Function<T, Object> roleCreator) { return StreamSupport.stream(i.spliterator(), false) .map(o -> o.as(Presentable).createPresentationModel(array(roleCreator.apply(o)))) .collect(PresentationModelCollectors.toCompositePresentationModel()); }
@Override @Nonnull public PresentationModel createPresentationModel (final @Nonnull Object... instanceRoles) { final Styleable styleable = new DefaultStyleable(getStyles()); return jobEvent.findChildren() .stream() .map(jobEvent -> (JobEventSpi)jobEvent) .sorted(comparing(JobEventSpi::getDateTime)) .map(jobEvent -> jobEvent.as(Presentable).createPresentationModel()) .collect(toCompositePresentationModel(aggregateBuilder().create(), styleable)); // FIXME: use SimpleCompositePresentable? }
/******************************************************************************************************************* * * {@inheritDoc} * * This method demonstrates the typical idiom for populating data: * * 1. A dao is called to provide raw data - let's say in form of collections; * 2. Objects in the collection are transformed into PresentationModels. * 3. The PresentationModels are then passed to the presentation. * ******************************************************************************************************************/ @Override public void start() { presentation.showUp(); final Collection<SimpleEntity> entities1 = dao.getSimpleEntities(); final Collection<SimpleDciEntity> entities2 = dao.getDciEntities(); final PresentationModel pm1 = entities1.stream().map(e -> pmFor(e)).collect(toCompositePresentationModel()); final PresentationModel pm2 = entities2.stream().map(e -> pmFor(e)).collect(toCompositePresentationModel()); presentation.populate(pm1, pm2); }
/******************************************************************************************************************* * * Reacts to the notification that a {@link Customer} has been selected by populating the presentation with * his projects. * * @param event the notification event * ******************************************************************************************************************/ @VisibleForTesting void onCustomerSelectedEvent (final @Nonnull @ListensTo CustomerSelectedEvent event) { log.info("onCustomerSelectedEvent({})", event); presentation.populate(event.getCustomer().findProjects() .stream() .map(project -> (ProjectSpi)project) .sorted(comparing(ProjectSpi::getName)) .map(project -> createPresentationModelFor(project)) .collect(toCompositePresentationModel())); }
/******************************************************************************************************************* * * Reacts to the notification that an {@link Accounting} has been opened by populating the presentation with * the customers. * * @param event the notification event * ******************************************************************************************************************/ @VisibleForTesting void onAccountingOpenedEvent (final @Nonnull @ListensTo AccountingOpenedEvent event) { log.info("onAccountingOpenedEvent({})", event); presentation.populate(event.getAccounting().getCustomerRegistry().findCustomers().stream() .map(customer -> (CustomerSpi)customer) .sorted(comparing(CustomerSpi::getName)) .map(customer -> createPresentationModelFor(customer)) .collect(toCompositePresentationModel())); }
/******************************************************************************************************************* * * Reacts to the notification that a {@link Project} has been selected by populating the presentation with * its job events. * * @param event the notification event * ******************************************************************************************************************/ @VisibleForTesting void onProjectSelectedEvent (final @Nonnull @ListensTo ProjectSelectedEvent event) { log.info("onProjectSelectedEvent({})", event); presentation.populate(event.getProject().findChildren() .stream() .map(jobEvent -> (JobEventSpi)jobEvent) .sorted(comparing(JobEventSpi::getDateTime)) .map(jobEvent -> jobEvent.as(Presentable).createPresentationModel()) .collect(toCompositePresentationModel())); } }