OaiPmhUpdatedEventHandler implements ManagedService { public void handleEvent(AssetManagerItem.TakeSnapshot snapshotItem) { if (!propagateEpisode) { logger.trace("Skipping automatic propagation of episode meta data to OAI-PMH since it is turned off."); return; } logger.debug("Handling update event for media package {}", snapshotItem.getId()); final User prevUser = securityService.getUser(); final Organization prevOrg = securityService.getOrganization(); try { securityService.setUser(SecurityUtil.createSystemUser(systemAccount, prevOrg)); String versionStr = Long.toString(snapshotItem.getVersion()); AQueryBuilder q = assetManager.createQuery(); AResult snapshotQueryResult = q.select(q.snapshot()) .where(q.organizationId().eq(prevOrg.getId()) .and(q.mediaPackageId(snapshotItem.getId()) .and(q.version().eq(assetManager.toVersion(versionStr).get())))).run(); Opt<ARecord> snapshotRecordOpt = snapshotQueryResult.getRecords().head(); if (snapshotRecordOpt.isSome()) { Snapshot snapshot = snapshotRecordOpt.get().getSnapshot().get(); MediaPackage snapshotMp = snapshot.getMediaPackage(); SimpleElementSelector mpeSelector = new SimpleElementSelector(); for (String flavor : flavors) { mpeSelector.addFlavor(flavor); } for (String tag : tags) { mpeSelector.addTag(tag); } Collection<MediaPackageElement> elementsToUpdate = mpeSelector.select(snapshotMp, true); if (elementsToUpdate == null || elementsToUpdate.isEmpty()) { logger.debug("The media package {} does not contain any elements matching the given flavors and tags", snapshotMp.getIdentifier().toString()); return; } SearchResult result = oaiPmhPersistence.search( QueryBuilder.query().mediaPackageId(snapshotMp).isDeleted(false).build()); for (SearchResultItem searchResultItem : result.getItems()) { try { Job job = oaiPmhPublicationService .updateMetadata(snapshotMp, searchResultItem.getRepository(), flavors, tags, false); } catch (Exception e) { logger.error("Unable to update OAI-PMH publication for the media package {} in repository {}", snapshotItem.getId(), searchResultItem.getRepository(), e); } } } } finally { securityService.setOrganization(prevOrg); securityService.setUser(prevUser); } }  @Override void updated(Dictionary<String, ?> dictionary); void handleEvent(AssetManagerItem.TakeSnapshot snapshotItem); void setAssetManager(AssetManager assetManager); void setOaiPmhPersistence(OaiPmhDatabase oaiPmhPersistence); void setOaiPmhPublicationService(OaiPmhPublicationService oaiPmhPublicationService); void setSecurityService(SecurityService securityService);  }
@Test public void testHandleEvent() throws Exception { Catalog episodeCatalog = CatalogImpl.newInstance(); episodeCatalog.setURI(URI.create("/episode.xml")); episodeCatalog.setFlavor(MediaPackageElementFlavor.parseFlavor("dublincore/episode")); episodeCatalog.addTag("archive"); MediaPackage updatedMp = createMediaPackage(episodeCatalog); mockAssetManager(updatedMp); mockSecurityService(); mockOaiPmhDatabase(); Capture<MediaPackage> mpCapture = Capture.newInstance(); Capture<String> repositoryCapture = Capture.newInstance(); Capture<Set<String>> flavorsCapture = Capture.newInstance(); Capture<Set> tagsCapture = Capture.newInstance(); expect(oaiPmhPublicationService.updateMetadata(capture(mpCapture), capture(repositoryCapture), capture(flavorsCapture), capture(tagsCapture), anyBoolean())) .andAnswer(() -> mock(Job.class)).times(1); replayAll(); cut.handleEvent(createSnapshot(updatedMp)); assertEquals(updatedMp.getIdentifier().toString(), mpCapture.getValue().getIdentifier().toString()); assertEquals(OAIPMH_REPOSITORY, repositoryCapture.getValue()); Assert.assertNotNull(flavorsCapture.getValue()); Assert.assertTrue(flavorsCapture.getValue().contains("dublincore/*")); Assert.assertTrue(flavorsCapture.getValue().contains("security/*")); Assert.assertTrue(tagsCapture.getValue().contains("archive")); Assert.assertTrue(orgIdCapture.hasCaptured()); Assert.assertEquals(new DefaultOrganization().getId(), orgIdCapture.getValue()); Assert.assertTrue(mpIdCapture.hasCaptured()); Assert.assertEquals(updatedMp.getIdentifier().toString(), mpIdCapture.getValue()); Assert.assertTrue(snapshotVersionCapture.hasCaptured()); Assert.assertEquals("3", snapshotVersionCapture.getValue()); }
@Test public void testEpisodeNotKnownByOaiPmh() throws Exception { Catalog episodeCatalog = CatalogImpl.newInstance(); episodeCatalog.setURI(URI.create("/episode.xml")); episodeCatalog.setFlavor(MediaPackageElementFlavor.parseFlavor("dublincore/episode")); episodeCatalog.addTag("archive"); MediaPackage updatedMp = createMediaPackage(episodeCatalog); mockAssetManager(updatedMp); mockSecurityService(); SearchResult searchResultMock = mock(MockType.NICE, SearchResult.class); expect(searchResultMock.getItems()).andReturn(Collections.EMPTY_LIST).anyTimes(); queryCapture = Capture.newInstance(); expect(oaiPmhDatabaseMock.search(capture(queryCapture))).andReturn(searchResultMock); replayAll(); cut.handleEvent(createSnapshot(updatedMp)); verifyAll(); }
@Test public void testNoElementsWithGivenFlavorAndTags() throws Exception { Catalog episodeCatalog = CatalogImpl.newInstance(); episodeCatalog.setURI(URI.create("/episode.xml")); episodeCatalog.setFlavor(MediaPackageElementFlavor.parseFlavor("dublincore/episode")); MediaPackage updatedMp = createMediaPackage(); mockAssetManager(updatedMp); mockSecurityService(); replayAll(); cut.handleEvent(createSnapshot(updatedMp)); verifyAll(); }
@Test public void testNoElementsForPublishing() throws Exception { MediaPackage updatedMp = createMediaPackage(); mockAssetManager(updatedMp); mockSecurityService(); replayAll(); cut.handleEvent(createSnapshot(updatedMp)); verifyAll(); }