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 org.opencastproject.assetmanager.api.fn.Enrichments.enrich;
24  
25  import org.opencastproject.assetmanager.api.AssetManager;
26  import org.opencastproject.assetmanager.api.Snapshot;
27  import org.opencastproject.assetmanager.api.query.AQueryBuilder;
28  import org.opencastproject.mediapackage.MediaPackage;
29  import org.opencastproject.security.api.UnauthorizedException;
30  import org.opencastproject.workflow.api.ConfiguredWorkflow;
31  import org.opencastproject.workflow.api.WorkflowDatabaseException;
32  import org.opencastproject.workflow.api.WorkflowInstance;
33  import org.opencastproject.workflow.api.WorkflowParsingException;
34  import org.opencastproject.workflow.api.WorkflowService;
35  
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  import java.util.ArrayList;
40  import java.util.List;
41  import java.util.Optional;
42  
43  /**
44   * Utility class to apply workflows to episodes. Removed 'final class' so that we can mock it for
45   * unit tests.
46   */
47  public class Workflows {
48    /** Log facility */
49    private static final Logger logger = LoggerFactory.getLogger(Workflows.class);
50  
51    private final AssetManager am;
52    private final WorkflowService wfs;
53  
54    public Workflows(AssetManager am, WorkflowService wfs) {
55      this.am = am;
56      this.wfs = wfs;
57    }
58  
59    /**
60     * Apply a workflow to the latest version of each media package.
61     */
62    public List<WorkflowInstance> applyWorkflowToLatestVersion(Iterable<String> mpIds, ConfiguredWorkflow wf) {
63      List<WorkflowInstance> result = new ArrayList<>();
64  
65      for (String mpId : mpIds) {
66        List<Snapshot> snapshots = findLatestSnapshots(mpId);
67        for (Snapshot snapshot : snapshots) {
68          MediaPackage mp = snapshot.getMediaPackage();
69          Optional<WorkflowInstance> optWorkflow = applyWorkflow(wf, mp);
70          optWorkflow.ifPresent(result::add);
71        }
72      }
73  
74      return result;
75    }
76  
77    /**
78     * Apply a workflow to a media package. The function returns some workflow instance if the
79     * workflow could be started successfully, none otherwise.
80     */
81    private Optional<WorkflowInstance> applyWorkflow(ConfiguredWorkflow wf, MediaPackage mp) {
82      try {
83        WorkflowInstance instance = wfs.start(wf.getWorkflowDefinition(), mp, wf.getParameters());
84        return Optional.of(instance);
85      } catch (WorkflowDatabaseException | WorkflowParsingException | UnauthorizedException e) {
86        logger.error("Cannot start workflow on media package {}", mp.getIdentifier(), e);
87        return Optional.empty();
88      }
89    }
90  
91    private List<Snapshot> findLatestSnapshots(String mpId) {
92      AQueryBuilder q = am.createQuery();
93      Iterable<Snapshot> snapshots = enrich(q.select(q.snapshot())
94          .where(q.mediaPackageId(mpId).and(q.version().isLatest())).run())
95          .getSnapshots();
96  
97      List<Snapshot> result = new ArrayList<>();
98      for (Snapshot snapshot : snapshots) {
99        result.add(snapshot);
100     }
101     return result;
102   }
103 }