Search in sources :

Example 1 with Function2

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");
    }
}
Also used : MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Function2(org.opencastproject.util.data.Function2) Job(org.opencastproject.job.api.Job) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) Track(org.opencastproject.mediapackage.Track)

Example 2 with Function2

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);
}
Also used : IoSupport.fileInputStream(org.opencastproject.util.IoSupport.fileInputStream) InputStream(java.io.InputStream) Function2(org.opencastproject.util.data.Function2) IOException(java.io.IOException) File(java.io.File) IoSupport.withFile(org.opencastproject.util.IoSupport.withFile) Test(org.junit.Test)

Example 3 with Function2

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());
}
Also used : Partial(org.joda.time.Partial) Function2(org.opencastproject.util.data.Function2) DateTime(org.joda.time.DateTime)

Aggregations

Function2 (org.opencastproject.util.data.Function2)3 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 DateTime (org.joda.time.DateTime)1 Partial (org.joda.time.Partial)1 Test (org.junit.Test)1 Job (org.opencastproject.job.api.Job)1 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)1 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)1 Track (org.opencastproject.mediapackage.Track)1 IoSupport.fileInputStream (org.opencastproject.util.IoSupport.fileInputStream)1 IoSupport.withFile (org.opencastproject.util.IoSupport.withFile)1 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)1