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