/******************************************************************************************************************* * * Creates a {@link PresentationModel} for a {@link Customer} injecting a {@link Selectable} role which fires a * {@link CustomerSelectedEvent} on selection. * * @param customer the {@code Customer} * @return the {@code PresentationModel} * ******************************************************************************************************************/ @Nonnull @VisibleForTesting PresentationModel createPresentationModelFor (final @Nonnull Customer customer) { final Selectable publishEventOnSelection = () -> messageBus.publish(new CustomerSelectedEvent(customer)); return customer.as(Presentable).createPresentationModel(publishEventOnSelection); } }
/******************************************************************************************************************* * * Creates a {@link PresentationModel} for a {@link Project} injecting a {@link Selectable} role which fires a * {@link ProjectSelectedEvent} on selection. * * @param customer the {@code Project} * @return the {@code PresentationModel} * ******************************************************************************************************************/ @Nonnull @VisibleForTesting PresentationModel createPresentationModelFor (final @Nonnull Project project) { final Selectable publishEventOnSelection = () -> messageBus.publish(new ProjectSelectedEvent(project)); return project.as(Presentable).createPresentationModel(publishEventOnSelection); } }
/******************************************************************************************************************* * * 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? }
/******************************************************************************************************************* * * ******************************************************************************************************************/ @Nonnull private PresentationModel createPresentationModelFor (final @Nonnull Object object) { try { if (!(object instanceof As)) { throw new AsException(As.class); } return ((As)object).as(Presentable).createPresentationModel(); } catch (AsException e) { return new AsLookupPresentationModel(object); // FIXME: use PresentationModelFactory } } }
/******************************************************************************************************************* * * 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())); } }
/******************************************************************************************************************* * * ******************************************************************************************************************/ @Nonnull private static PresentationModel createPresentationModelFor (final @Nonnull Object object) { if (!(object instanceof As)) { return new DefaultPresentationModel(object); } else { try { return ((As)object).as(Presentable).createPresentationModel(); } catch (AsException e) { return new DefaultPresentationModel(object); } } } }
@Override public List<? extends PresentationModel> run() { final List<PresentationModel> results = new ArrayList<>(); try { @SuppressWarnings("unchecked") final SimpleComposite<T> composite = scp.datum.as(SimpleComposite.class); for (final T child : composite.findChildren().results()) { final Presentable presentable = child.as(Presentable, new NotFoundBehaviour<Presentable>() { @Override public Presentable run (final Throwable t) { return new SimpleCompositePresentable<>(child); } }); results.add(presentable.createPresentationModel(rolesOrFactories.toArray())); } } catch (AsException e) { // ok, no Composite role log.trace(">>>> no composite role for {}", scp.datum); } return results; } });
/* visible for testing */ void onSiteNodeSelected (final @ListensTo @Nonnull SiteNodeSelectedEvent selectionEvent) { log.debug("onSiteNodeSelected({})", selectionEvent); if (!selectionEvent.getSiteNode().isPresent()) { presentation.clear(); } else { final SiteNode siteNode = selectionEvent.getSiteNode().get(); final ResourceProperties properties = siteNode.getProperties(); log.debug(">>>> properties: {}", properties); presentation.populate("Viewer not implemented for " + siteNode.getFile()); presentation.populateProperties(properties.as(Presentable).createPresentationModel()); presentation.showUp(); } } }
/******************************************************************************************************************* * ******************************************************************************************************************/ @BeforeMethod public void setup() { ContextManager.Locator.set(null); context = new ClassPathXmlApplicationContext("DefaultStructureEditorPresentationControlTestBeans.xml"); underTest = context.getBean(DefaultStructureEditorPresentationControl.class); presentation = context.getBean(StructureEditorPresentation.class); siteNode = mock(SiteNode.class); properties = mock(ResourceProperties.class); presentable = mock(Presentable.class); pm = mock(PresentationModel.class); when(siteNode.getProperties()).thenReturn(properties); when(presentable.createPresentationModel(anyVararg())).thenReturn(pm); when(properties.as(eq(Presentable))).thenReturn(presentable); }
/* visible for testing */ void onOpenSite (final @ListensTo @Nonnull OpenSiteEvent event) { try { log.debug("onOpenSite({})", event); final ResourceFile root = event.getFileSystem().findFileByPath(ROOT_SITE_NODE_PATH); final SiteNode rootSiteNode = modelFactory.createSiteNode(site, root); presentation.populate(rootSiteNode.as(Presentable).createPresentationModel()); presentation.expandFirstLevel(); messageBus.publish(emptySelectionEvent()); } catch (IOException | NotFoundException e) { log.warn("", e); } } }
/******************************************************************************************************************* * * ******************************************************************************************************************/ @ThreadConfined(type=NOT_UI) protected void createPresentationModels (final @Nonnull Taxon taxon) throws IOException { log.info("createPresentationModels({})", taxon); for (final Observation observation : loadObservationSet(taxon).find(Observation).results()) { final Media media = observation.as(Media); final ActionProvider actionProvider = new SoundActionProvider(DefaultTaxonSoundFactSheetViewController.this, view, playController, media, observation); addPresentationModel(media.as(Presentable).createPresentationModel(observation, actionProvider)); } }
/******************************************************************************************************************* * * ******************************************************************************************************************/ @ThreadConfined(type=NOT_UI) protected void createPresentationModels (final @Nonnull Taxon taxon) throws NotFoundException { log.info("createPresentationModels({})", taxon); // stillImages.addAll(taxon.as(Documentable).getFactSheet().getMultiple(FOAF_IMAGE)); for (final Media stillImage : taxon.as(Documentable).getFactSheet().getMultiple(FOAF_IMAGE)) { addPresentationModel(stillImage.with(ID, replacedId(stillImage.get(ID))).as(Presentable).createPresentationModel()); } }
/* visible for testing */ void onOpenSite (final @ListensTo @Nonnull OpenSiteEvent event) { log.debug("onOpenSite({})", event); final ResourceFile root = event.getFileSystem().findFileByPath(ROOT_DOCUMENT_PATH); final Content rootContent = modelFactory.createContent().withFolder(root).build(); presentation.populate(rootContent.as(Presentable).createPresentationModel()); presentation.expandFirstLevel(); messageBus.publish(emptySelectionEvent()); } }
/******************************************************************************************************************* * ******************************************************************************************************************/ @BeforeMethod public void setup() { ContextManager.Locator.set(null); context = (ClassPathXmlApplicationContext)helper.createSpringContext(); underTest = context.getBean(DefaultContentEditorPresentationControl.class); embeddedServer = context.getBean(EmbeddedServer.class); presentation = context.getBean(ContentEditorPresentation.class); content = mock(Content.class); properties = mock(ResourceProperties.class); presentable = mock(Presentable.class); pm = mock(PresentationModel.class); propertyBinder = mock(PropertyBinder.class); when(embeddedServer.putDocument(anyString(), any(Document.class))).thenReturn(registeredUrl); when(content.getProperties()).thenReturn(properties); when(presentable.createPresentationModel(anyVararg())).thenReturn(pm); when(properties.as(eq(Presentable))).thenReturn(presentable); when(properties.as(eq(PropertyBinder))).thenReturn(propertyBinder); document = new Document().withContent("proxy for: full text") .withMimeType("text/html"); when(propertyBinder.createBoundDocument(any(Key.class), any(PropertyBinder.UpdateCallback.class))) .thenAnswer(invocation -> document); underTest.initialize(); }