summaryrefslogtreecommitdiffstats
path: root/api/src/main/java/org/openslx/taskmanager/api/TaskStatus.java
blob: edf7bcef840bc6cbcad6c82cccd27d273cf81205 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package org.openslx.taskmanager.api;

/**
 * This is what is returned on a status request.
 * To return custom data for your task, call {@link AbstractTask#setStatusObject(Object)} from your
 * Task.
 * This class is serialized entirely, not using the Exposed annotation.
 */
public final class TaskStatus
{

	public enum StatusCode
	{
		TASK_WAITING,
		TASK_PROCESSING,
		TASK_FINISHED,
		TASK_ERROR,
		TASK_CANCELLING,
		TASK_CANCELLED,
		NOT_CANCELLABLE,
		NO_SUCH_INSTANCE,
		NO_SUCH_TASK,
		NO_SUCH_CONSTRUCTOR,
		DUPLICATE_ID,
		PARENT_FAILED,
		JSON_ERROR,
	}

	/**
	 * Overall status of the task. Only set by base methods of the AbstractTask class.
	 */
	protected volatile StatusCode statusCode;
	/**
	 * Custom data a task might want to return on status requests.
	 */
	private volatile Object data = null;

	@SuppressWarnings( "unused" )
	private final String id;

	/*
	 * Static members
	 */

	/**
	 * Create a single "duplicate id" status we return if trying to launch a task with an id already
	 * in use.
	 */
	public static final TaskStatus ts_duplicateId = new TaskStatus( StatusCode.DUPLICATE_ID );
	/**
	 * Create a single "no such constructor" status we return if a task could be found, but not
	 * instantiated.
	 */
	public static final TaskStatus ts_noSuchConstructor = new TaskStatus( StatusCode.NO_SUCH_CONSTRUCTOR );
	/**
	 * Create a single "no such task" status we return if a task should be invoked that
	 * doesn't actually exist.
	 */
	public static final TaskStatus ts_noSuchTask = new TaskStatus( StatusCode.NO_SUCH_TASK );
	/**
	 * Create a single "no such task" status we return if an action on a task instance is requested
	 * that doesn't actually exist.
	 */
	public static final TaskStatus ts_noSuchInstance = new TaskStatus( StatusCode.NO_SUCH_INSTANCE );
	/**
	 * Create a single "parent failed" status we return as status for a task which depends on another
	 * task, and that other task failed to execute, or failed during execution.
	 */
	public static final TaskStatus ts_parentFailed = new TaskStatus( StatusCode.PARENT_FAILED );
	/**
	 * Create a single "task waiting" status we return as status for a task that is waiting for
	 * execution.
	 */
	public static final TaskStatus ts_waiting = new TaskStatus( StatusCode.TASK_WAITING );
	/**
	 * Create a single "task error" status we can use everywhere.
	 */
	public static final TaskStatus ts_error = new TaskStatus( StatusCode.TASK_ERROR );
	/**
	 * Create a single "json error" status we can use everywhere.
	 */
	public static final TaskStatus ts_jsonError = new TaskStatus( StatusCode.JSON_ERROR );
	
	/**
	 * Create a single "not cancellable" status we can use everywhere.
	 */
	public static final TaskStatus ts_notCancellable = new TaskStatus( StatusCode.NOT_CANCELLABLE );

	/**
	 * Create new TaskStatus with given initial status code
	 * and id.
	 * 
	 * @param status The status code to initialize the TaskStatus with
	 * @param id id of task this status belongs to
	 */
	public TaskStatus( final StatusCode status, final String id )
	{
		this.statusCode = status;
		this.id = id;
	}

	/**
	 * Create new TaskStatus with given initial status code.
	 * 
	 * @param status The status code to initialize the TaskStatus with
	 */
	public TaskStatus( final StatusCode status )
	{
		this( status, null );
	}

	/**
	 * Get the status code of this TaskStatus
	 * 
	 * @return
	 */
	public final StatusCode getStatusCode()
	{
		return this.statusCode;
	}

	/**
	 * Set the custom status data.
	 * 
	 * @param obj custom status object
	 */
	protected void setStatusObject( Object obj )
	{
		this.data = obj;
	}
	
	public String getStatusObjectClassName()
	{
		if ( this.data == null ) return "(null)";
		return this.data.getClass().getSimpleName();
	}

}