use of org.opencastproject.util.data.Function2 in project opencast by opencast.
the class ImageToVideoWorkflowOperationHandler method imageToVideo.
private WorkflowOperationResult imageToVideo(MediaPackage mp, WorkflowInstance wi) throws Exception {
// read cfg
final List<String> sourceTags = getCfg(wi, OPT_SOURCE_TAGS).map(asList).getOrElse(nil(String.class));
final Option<MediaPackageElementFlavor> sourceFlavor = getCfg(wi, OPT_SOURCE_FLAVOR).map(MediaPackageElementFlavor.parseFlavor);
if (sourceFlavor.isNone() && sourceTags.isEmpty()) {
logger.warn("No source tags or flavor are given to determine the image to use");
return createResult(mp, Action.SKIP);
}
final List<String> targetTags = getCfg(wi, OPT_TARGET_TAGS).map(asList).getOrElse(nil(String.class));
final Option<MediaPackageElementFlavor> targetFlavor = getCfg(wi, OPT_TARGET_FLAVOR).map(MediaPackageElementFlavor.parseFlavor);
final double duration = getCfg(wi, OPT_DURATION).bind(Strings.toDouble).getOrElse(this.<Double>cfgKeyMissing(OPT_DURATION));
final String profile = getCfg(wi, OPT_PROFILE).getOrElse(this.<String>cfgKeyMissing(OPT_PROFILE));
// run image to video jobs
final List<Job> jobs = Monadics.<MediaPackageElement>mlist(mp.getAttachments()).filter(sourceFlavor.map(Filters.matchesFlavor).getOrElse(Booleans.<MediaPackageElement>yes())).filter(Filters.hasTagAny(sourceTags)).map(Misc.<MediaPackageElement, Attachment>cast()).map(imageToVideo(profile, duration)).value();
if (JobUtil.waitForJobs(serviceRegistry, jobs).isSuccess()) {
for (final Job job : jobs) {
if (job.getPayload().length() > 0) {
Track track = (Track) MediaPackageElementParser.getFromXml(job.getPayload());
track.setURI(workspace.moveTo(track.getURI(), mp.getIdentifier().toString(), track.getIdentifier(), FilenameUtils.getName(track.getURI().toString())));
// Adjust the target tags
for (String tag : targetTags) {
track.addTag(tag);
}
// Adjust the target flavor.
for (MediaPackageElementFlavor flavor : targetFlavor) {
track.setFlavor(flavor);
}
// store new tracks to mediaPackage
mp.add(track);
logger.debug("Image to video operation completed");
} else {
logger.info("Image to video operation unsuccessful, no payload returned: {}", job);
return createResult(mp, Action.SKIP);
}
}
return createResult(mp, Action.CONTINUE, mlist(jobs).foldl(0L, new Function2<Long, Job, Long>() {
@Override
public Long apply(Long max, Job job) {
return Math.max(max, job.getQueueTime());
}
}));
} else {
throw new WorkflowOperationException("The image to video encoding jobs did not return successfully");
}
}
use of org.opencastproject.util.data.Function2 in project opencast by opencast.
the class IoSupportTest method testWithFile.
@Test
public void testWithFile() throws Exception {
final File f1 = new File(this.getClass().getResource("/dublincore.xml").toURI());
final Option<String> r1 = withFile(f1, new Function2.X<InputStream, File, String>() {
@Override
public String xapply(InputStream in, File file) throws IOException {
return IOUtils.readLines(in).get(0);
}
});
assertEquals(some("<?xml version=\"1.0\"?>"), r1);
final File f2 = new File("i-do-not-exist");
final Option<String> r2 = withFile(f2, new Function2.X<InputStream, File, String>() {
@Override
public String xapply(InputStream in, File file) throws IOException {
return IOUtils.readLines(in).get(0);
}
});
assertEquals(none(String.class), r2);
}
use of org.opencastproject.util.data.Function2 in project opencast by opencast.
the class SchedulerServiceDatabaseImpl method setDateForQuarterQuery.
/**
* Add the correct start and end value for the given quarter count query
* <p/>
* Please note that the start instant is inclusive while the end instant is exclusive.
*
* @param query
* The query where the parameters have to be added
* @return the same query instance
*/
private Query setDateForQuarterQuery(Query query) {
final DateTime today = new DateTime().withTimeAtStartOfDay();
final Partial partialToday = partialize(today);
final DateTime quarterBeginning = mlist(quarterBeginnings).foldl(quarterBeginnings.get(0), new Function2<Partial, Partial, Partial>() {
@Override
public Partial apply(Partial sum, Partial quarterBeginning) {
return partialToday.isAfter(quarterBeginning) ? quarterBeginning : sum;
}
}).toDateTime(today);
return query.setParameter("start", quarterBeginning.toDate()).setParameter("end", quarterBeginning.plusMonths(3).toDate());
}
Aggregations