View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  package org.opencastproject.assetmanager.util;
22  
23  import static com.entwinemedia.fn.Stream.$;
24  import static org.opencastproject.assetmanager.api.fn.Enrichments.enrich;
25  
26  import org.opencastproject.assetmanager.api.AssetManager;
27  import org.opencastproject.assetmanager.api.Snapshot;
28  import org.opencastproject.assetmanager.api.query.AQueryBuilder;
29  import org.opencastproject.mediapackage.MediaPackage;
30  import org.opencastproject.security.api.UnauthorizedException;
31  import org.opencastproject.workflow.api.ConfiguredWorkflow;
32  import org.opencastproject.workflow.api.WorkflowDatabaseException;
33  import org.opencastproject.workflow.api.WorkflowInstance;
34  import org.opencastproject.workflow.api.WorkflowParsingException;
35  import org.opencastproject.workflow.api.WorkflowService;
36  
37  import com.entwinemedia.fn.Fn;
38  import com.entwinemedia.fn.Stream;
39  import com.entwinemedia.fn.data.Opt;
40  
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  
44  /**
45   * Utility class to apply workflows to episodes. Removed 'final class' so that we can mock it for
46   * unit tests.
47   */
48  public class Workflows {
49    /** Log facility */
50    private static final Logger logger = LoggerFactory.getLogger(Workflows.class);
51  
52    private final AssetManager am;
53    private final WorkflowService wfs;
54  
55    public Workflows(AssetManager am, WorkflowService wfs) {
56      this.am = am;
57      this.wfs = wfs;
58    }
59  
60    /**
61     * Apply a workflow to the latest version of each media package.
62     */
63    public Stream<WorkflowInstance> applyWorkflowToLatestVersion(Iterable<String> mpIds, ConfiguredWorkflow wf) {
64      return $(mpIds).bind(findLatest).map(getMediapackage).bind(applyWorkflow(wf));
65    }
66  
67    /**
68     * Apply a workflow to a media package. The function returns some workflow instance if the
69     * workflow could be started successfully, none otherwise.
70     */
71    public Fn<MediaPackage, Opt<WorkflowInstance>> applyWorkflow(final ConfiguredWorkflow wf) {
72      return new Fn<MediaPackage, Opt<WorkflowInstance>>() {
73        @Override public Opt<WorkflowInstance> apply(MediaPackage mp) {
74          try {
75            return Opt.some(wfs.start(wf.getWorkflowDefinition(), mp, wf.getParameters()));
76          } catch (WorkflowDatabaseException | WorkflowParsingException | UnauthorizedException e) {
77            logger.error("Cannot start workflow on media package " + mp.getIdentifier().toString(), e);
78            return Opt.none();
79          }
80        }
81      };
82    }
83  
84    // CHECKSTYLE:OFF
85    private final Fn<Snapshot, MediaPackage> getMediapackage = new Fn<Snapshot, MediaPackage>() {
86      @Override public MediaPackage apply(Snapshot snapshot) {
87        return snapshot.getMediaPackage();
88      }
89    };
90    // CHECKSTYLE:ON
91  
92    private final Fn<String, Iterable<Snapshot>> findLatest = new Fn<String, Iterable<Snapshot>>() {
93      @Override public Iterable<Snapshot> apply(String mpId) {
94        AQueryBuilder q = am.createQuery();
95        return enrich(q.select(q.snapshot()).where(q.mediaPackageId(mpId).and(q.version().isLatest())).run())
96            .getSnapshots();
97      }
98    };
99  }