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  
22  package org.opencastproject.workflow.api;
23  
24  import org.opencastproject.mediapackage.MediaPackage;
25  import org.opencastproject.security.api.UnauthorizedException;
26  import org.opencastproject.util.NotFoundException;
27  import org.opencastproject.workflow.api.WorkflowInstance.WorkflowState;
28  
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Optional;
32  
33  /**
34   * Manages {@link WorkflowDefinition}s and {@link WorkflowInstance}s.
35   */
36  public interface WorkflowService {
37    /**
38     * The service registration property we use to identify which workflow operation a {@link WorkflowOperationHandler}
39     * should handle.
40     */
41    String WORKFLOW_OPERATION_PROPERTY = "workflow.operation";
42  
43    /** Identifier for workflow jobs */
44    String JOB_TYPE = "org.opencastproject.workflow";
45  
46    /**
47     * Adds a workflow listener to be notified when workflows are updated.
48     *
49     * @param listener
50     *          the workflow listener to add
51     */
52    void addWorkflowListener(WorkflowListener listener);
53  
54    /**
55     * Removes a workflow listener.
56     *
57     * @param listener
58     *          the workflow listener to remove
59     */
60    void removeWorkflowListener(WorkflowListener listener);
61  
62    /**
63     * Returns the {@link WorkflowDefinition} identified by <code>name</code>.
64     *
65     * @param id
66     *          the workflow definition id
67     * @return the workflow
68     * @throws WorkflowDatabaseException
69     *           if there is a problem accessing the workflow definition
70     * @throws NotFoundException
71     *           if there is no registered workflow definition with this identifier
72     *
73     */
74    WorkflowDefinition getWorkflowDefinitionById(String id) throws WorkflowDatabaseException, NotFoundException;
75  
76    /**
77     * Gets a {@link WorkflowInstance} by its ID.
78     *
79     * @return the workflow instance
80     * @throws WorkflowDatabaseException
81     *           if there is a problem accessing the workflow instance from persistence
82     * @throws NotFoundException
83     *           if there is no workflow instance with this identifier
84     * @throws UnauthorizedException
85     *           if the current user does not have read permissions on the workflow instance's mediapackage.
86     */
87    WorkflowInstance getWorkflowById(long workflowId) throws WorkflowDatabaseException, NotFoundException,
88            UnauthorizedException;
89  
90    /**
91     * Creates a new workflow instance and starts the workflow.
92     *
93     * @param workflowDefinition
94     *          the workflow definition
95     * @param mediaPackage
96     *          the mediapackage to process
97     * @param properties
98     *          any properties to apply to the workflow definition
99     * @return The new workflow instance
100    * @throws WorkflowDatabaseException
101    *           if there is a problem storing the workflow instance in persistence
102    * @throws WorkflowParsingException
103    *           if there is a problem parsing or serializing workflow entities
104    */
105   WorkflowInstance start(WorkflowDefinition workflowDefinition, MediaPackage mediaPackage,
106           Map<String, String> properties) throws WorkflowDatabaseException, WorkflowParsingException,
107           UnauthorizedException;
108 
109   /**
110    * Creates a new workflow instance and starts the workflow.
111    *
112    * @param workflowDefinition
113    *          the workflow definition
114    * @param mediaPackage
115    *          the mediapackage to process
116    * @param parentWorkflowId
117    *          An existing workflow to associate with the new workflow instance
118    * @param properties
119    *          any properties to apply to the workflow definition
120    * @return The new workflow instance
121    * @throws NotFoundException
122    *           if the parent workflow does not exist
123    * @throws WorkflowDatabaseException
124    *           if there is a problem storing the workflow instance in persistence
125    * @throws WorkflowParsingException
126    *           if there is a problem parsing or serializing workflow entities
127    * @throws IllegalStateException
128    *           if there is currently a workflow active on the media package
129    * @throws UnauthorizedException
130    *           if the current user does not have {@link org.opencastproject.security.api.Permissions.Action#WRITE}
131    *           on the media package.
132    */
133   WorkflowInstance start(WorkflowDefinition workflowDefinition, MediaPackage mediaPackage, Long parentWorkflowId,
134           Map<String, String> properties) throws WorkflowDatabaseException, WorkflowParsingException,
135           UnauthorizedException, NotFoundException, IllegalStateException;
136 
137   /**
138    * Creates a new workflow instance and starts the workflow.
139    *
140    * @param workflowDefinition
141    *          the workflow definition
142    * @param mediaPackage
143    *          the mediapackage to process
144    * @return The new workflow instance
145    * @throws WorkflowDatabaseException
146    *           if there is a problem storing the workflow instance in persistence
147    * @throws WorkflowParsingException
148    *           if there is a problem parsing or serializing workflow entities
149    */
150   WorkflowInstance start(WorkflowDefinition workflowDefinition, MediaPackage mediaPackage)
151           throws WorkflowDatabaseException, WorkflowParsingException, UnauthorizedException;
152 
153   /**
154    * Gets the total number of workflows that have been created to date.
155    *
156    * @return The number of workflow instances, regardless of their state
157    * @throws WorkflowDatabaseException
158    *           if there is a problem accessing the workflow instances in persistence
159    */
160   long countWorkflowInstances() throws WorkflowDatabaseException;
161 
162   /**
163    * Gets the total number of workflows that have been created to date and that match the workflow state
164    * which might be <code>null</code>.
165    *
166    * @param state
167    *          the workflow state
168    * @return The number of workflow instances, regardless of their state
169    * @throws WorkflowDatabaseException
170    *           if there is a problem accessing the workflow instances in persistence
171    */
172   long countWorkflowInstances(WorkflowState state) throws WorkflowDatabaseException;
173 
174   /**
175    * Stops a running workflow instance.
176    *
177    * @param workflowInstanceId
178    *          the workflow instance identifier
179    * @return the workflow instance
180    * @throws NotFoundException
181    *           if no running workflow with this identifier exists
182    * @throws WorkflowException
183    *           if there is a problem processing the workflow
184    * @throws UnauthorizedException
185    *           if the current user does not have read permissions on the workflow instance's mediapackage.
186    */
187   WorkflowInstance stop(long workflowInstanceId) throws WorkflowException, NotFoundException, UnauthorizedException;
188 
189   /**
190    * Permanently removes a workflow instance. Only workflow instances with state {@link WorkflowState#SUCCEEDED},
191    * {@link WorkflowState#STOPPED} or {@link WorkflowState#FAILED} may be removed.
192    *
193    * @param workflowInstanceId
194    *          the workflow instance identifier
195    * @throws WorkflowDatabaseException
196    *           if there is a problem writing to the database
197    * @throws NotFoundException
198    *           if no workflow instance with the given identifier could be found
199    * @throws UnauthorizedException
200    *           if the current user does not have write permissions on the workflow instance
201    * @throws WorkflowStateException
202    *           if the workflow instance is in a disallowed state
203    */
204   void remove(long workflowInstanceId) throws WorkflowDatabaseException, WorkflowParsingException, NotFoundException,
205           UnauthorizedException, WorkflowStateException;
206 
207   /**
208    * Permanently removes a workflow instance. Option to remove a workflow instance regardless of its status.
209    *
210    * @param workflowInstanceId
211    *          the workflow instance identifier
212    * @param force
213    *          remove the workflow instance no matter the status
214    * @throws WorkflowDatabaseException
215    *           if there is a problem writing to the database
216    * @throws NotFoundException
217    *           if no workflow instance with the given identifier could be found
218    * @throws UnauthorizedException
219    *           if the current user does not have write permissions on the workflow instance
220    * @throws WorkflowStateException
221    *           if the workflow instance is in a disallowed state
222    */
223   void remove(long workflowInstanceId, boolean force) throws WorkflowDatabaseException, WorkflowParsingException,
224           NotFoundException, UnauthorizedException, WorkflowStateException;
225 
226   /**
227    * Temporarily suspends a started workflow instance.
228    *
229    * @param workflowInstanceId
230    *          the workflow instance identifier
231    * @return the workflow instance
232    * @throws NotFoundException
233    *           if no running workflow with this identifier exists
234    * @throws WorkflowException
235    *           if there is a problem processing the workflow
236    * @throws UnauthorizedException
237    *           if the current user does not have read permissions on the workflow instance's mediapackage.
238    */
239   WorkflowInstance suspend(long workflowInstanceId) throws WorkflowException, NotFoundException, UnauthorizedException;
240 
241   /**
242    * Resumes a suspended workflow instance.
243    *
244    * @param workflowInstanceId
245    *          the workflow instance identifier
246    * @return the workflow instance
247    * @throws NotFoundException
248    *           if no paused workflow with this identifier exists
249    * @throws WorkflowException
250    *           if there is a problem processing the workflow
251    * @throws IllegalStateException
252    *           if the workflow with this identifier is not in the paused state
253    * @throws UnauthorizedException
254    *           if the current user does not have read permissions on the workflow instance's mediapackage.
255    */
256   WorkflowInstance resume(long workflowInstanceId) throws NotFoundException, WorkflowException, IllegalStateException,
257           UnauthorizedException;
258 
259   /**
260    * Resumes a suspended workflow instance, applying new properties to the workflow.
261    *
262    * @param workflowInstanceId
263    *          the workflow to resume
264    * @param properties
265    *          the properties to apply to the resumed workflow
266    * @return the workflow instance
267    * @throws NotFoundException
268    *           if no workflow with this identifier exists
269    * @throws WorkflowException
270    *           if there is a problem processing the workflow
271    * @throws IllegalStateException
272    *           if the workflow with this identifier is not in the paused state
273    * @throws UnauthorizedException
274    *           if the current user does not have read permissions on the workflow instance's mediapackage.
275    */
276   WorkflowInstance resume(long workflowInstanceId, Map<String, String> properties) throws NotFoundException,
277           WorkflowException, IllegalStateException, UnauthorizedException;
278 
279   /**
280    * Updates the given workflow instance with regard to the media package, the properties and the operations involved.
281    *
282    * @param workflowInstance
283    *          the workflow instance
284    * @throws WorkflowDatabaseException
285    *           if there is a database problem processing the workflow
286    * @throws UnauthorizedException
287    *           if the current user does not have read permissions on the workflow instance's mediapackage.
288    */
289   void update(WorkflowInstance workflowInstance) throws WorkflowDatabaseException, UnauthorizedException;
290 
291   /**
292    * Gets the list of available workflow definitions. In order to be "available", a workflow definition must be
293    * registered and must have registered workflow operation handlers for each of the workflow definition's operations.
294    *
295    * @return The list of currently available workflow definitions, sorted by title
296    * @throws WorkflowDatabaseException
297    *           if there is a problem storing the registered workflow definitions
298    */
299   List<WorkflowDefinition> listAvailableWorkflowDefinitions() throws WorkflowDatabaseException;
300 
301   /**
302    *
303    * Starts a cleanup of workflow instances with a given lifetime and a specific state
304    *
305    * @param lifetime
306    *          minimum lifetime of the workflow instances
307    * @param state
308    *          state of the workflow instances
309    */
310   void cleanupWorkflowInstances(int lifetime, WorkflowInstance.WorkflowState state) throws WorkflowDatabaseException,
311           UnauthorizedException;
312 
313   /**
314    * @return All configured workflow state mappings
315    */
316   Map<String, Map<String, String>> getWorkflowStateMappings();
317 
318   /**
319    * Checks if there is at least one workflow currently running on the given mediapackage
320    *
321    * @param mediaPackageId
322    *          the identifier of the mediapackage
323    * @return Whether there is a workflow active on the mediapackage
324    * @throws WorkflowDatabaseException
325    */
326   boolean mediaPackageHasActiveWorkflows(String mediaPackageId) throws WorkflowDatabaseException;
327 
328   /**
329    * Checks if there is at least one workflow currently running started by the given user
330    *
331    * @param userId
332    *          the identifier of the user
333    * @return Whether there is a workflow active started by the user
334    * @throws WorkflowDatabaseException
335    */
336   boolean userHasActiveWorkflows(String userId) throws WorkflowDatabaseException;
337 
338   /**
339    * Returns all workflows associated with the given mediapackage
340    * Current user needs permission to the mediapackage
341    *
342    * @param mediaPackageId
343    *          the identifier of the mediapackage
344    * @return a {@link List} of {@link WorkflowInstance}
345    * @throws WorkflowDatabaseException
346    */
347   List<WorkflowInstance> getWorkflowInstancesByMediaPackage(String mediaPackageId)
348           throws WorkflowDatabaseException, UnauthorizedException;
349 
350   /**
351    * Returns the {@link WorkflowInstance} currently running on the given mediaPackage
352    *
353    * @param mediaPackageId
354    *          the identifier of the mediaPackage
355    * @param action
356    *          necessary permissions for the workflowInstance
357    * @return An {@link Optional} containing the currently running {@link WorkflowInstance}
358    * @throws WorkflowException
359    * @throws WorkflowDatabaseException
360    * @throws UnauthorizedException
361    */
362   Optional<WorkflowInstance> getRunningWorkflowInstanceByMediaPackage(String mediaPackageId, String action)
363           throws WorkflowException, UnauthorizedException, WorkflowDatabaseException;
364 
365 }