View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.surf.task;
20  
21  import java.util.ArrayList;
22  import java.util.Date;
23  import java.util.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /**
29   * Abstract implementation of a task which may be useful to developers who
30   * wish to write custom tasks.
31   * 
32   * @author muzquiano
33   */
34  public abstract class AbstractTask implements Task
35  {
36      protected static Log logger = LogFactory.getLog(AbstractTask.class);
37      
38      protected boolean isError = false;
39      protected boolean isSuccess = false;
40      protected boolean isFinished = false;
41      protected boolean isRunning = false;
42      protected boolean isCancelled = false;
43      protected Throwable throwable = null;
44      protected String id = null;
45  	protected String name = null;
46  	protected String description = null;
47  	protected String status;
48  	protected int progress;
49  	protected int progressSize;
50  	protected List<String> history;
51  	protected String creator;
52  	protected Date startTime;
53  	protected Date endTime;
54  
55  	/**
56  	 * Instantiates a new abstract task.
57  	 * 
58  	 * @param name the name
59  	 */
60  	public AbstractTask(String name)
61  	{
62  		this.name = name;
63  		this.id = new org.springframework.extensions.surf.util.ObjectGUID().toString();
64  
65  		this.history = new ArrayList<String>();
66  		this.description = "";
67  		this.status = "";
68  		this.creator = "unknown";
69  		this.startTime = new Date();
70  
71  		progress = 0;
72  		progressSize = 10;
73  	}
74  
75  	/* (non-Javadoc)
76  	 * @see org.alfresco.web.framework.Task#increment()
77  	 */
78  	public void increment()
79  	{
80  		progress++;
81  	}
82  
83  	/* (non-Javadoc)
84  	 * @see org.alfresco.web.framework.Task#getId()
85  	 */
86  	public String getId()
87  	{
88  		return id;
89  	}
90  
91  	/* (non-Javadoc)
92  	 * @see org.alfresco.web.framework.Task#getName()
93  	 */
94  	public String getName()
95  	{
96  		return name;
97  	}
98  
99  	/* (non-Javadoc)
100 	 * @see org.alfresco.web.framework.Task#setDescription(java.lang.String)
101 	 */
102 	public void setDescription(String description)
103 	{
104 		this.description = description;
105 	}
106 
107 	/* (non-Javadoc)
108 	 * @see org.alfresco.web.framework.Task#getDescription()
109 	 */
110 	public String getDescription()
111 	{
112 		return description;
113 	}
114 
115 	/* (non-Javadoc)
116 	 * @see org.alfresco.web.framework.Task#setCreator(java.lang.String)
117 	 */
118 	public void setCreator(String creator)
119 	{
120 		this.creator = creator;
121 	}
122 
123 	/* (non-Javadoc)
124 	 * @see org.alfresco.web.framework.Task#getCreator()
125 	 */
126 	public String getCreator()
127 	{
128 		return this.creator;
129 	}
130 
131 	/* (non-Javadoc)
132 	 * @see org.alfresco.web.framework.Task#execute()
133 	 */
134 	public abstract void execute()
135 		throws Throwable;
136 
137 	/* (non-Javadoc)
138 	 * @see org.alfresco.web.framework.Task#cancel()
139 	 */
140 	public abstract void cancel();
141 
142 	/* (non-Javadoc)
143 	 * @see org.alfresco.web.framework.Task#isError()
144 	 */
145 	public boolean isError()
146 	{
147 		return isError;
148 	}
149 
150 	/* (non-Javadoc)
151 	 * @see org.alfresco.web.framework.Task#isSuccess()
152 	 */
153 	public boolean isSuccess()
154 	{
155 		return isSuccess;
156 	}
157 
158 	/* (non-Javadoc)
159 	 * @see org.alfresco.web.framework.Task#isFinished()
160 	 */
161 	public boolean isFinished()
162 	{
163 		return isFinished;
164 	}
165 
166 	/* (non-Javadoc)
167 	 * @see org.alfresco.web.framework.Task#isRunning()
168 	 */
169 	public boolean isRunning()
170 	{
171 		return isRunning;
172 	}
173 
174 	/* (non-Javadoc)
175 	 * @see org.alfresco.web.framework.Task#isCancelled()
176 	 */
177 	public boolean isCancelled()
178 	{
179 		return isCancelled;
180 	}
181 
182 	/* (non-Javadoc)
183 	 * @see org.alfresco.web.framework.Task#getStartTime()
184 	 */
185 	public Date getStartTime()
186 	{
187 		return this.startTime;
188 	}
189 
190 	/* (non-Javadoc)
191 	 * @see org.alfresco.web.framework.Task#getEndTime()
192 	 */
193 	public Date getEndTime()
194     {
195         return this.endTime;
196     }
197 
198 	/* (non-Javadoc)
199 	 * @see org.alfresco.web.framework.Task#getThrowable()
200 	 */
201 	public Throwable getThrowable()
202 	{
203 		return throwable;
204 	}
205 
206 	/* (non-Javadoc)
207 	 * @see org.alfresco.web.framework.Task#getStatus()
208 	 */
209 	public String getStatus()
210 	{
211 		return this.status;
212 	}
213 
214 	/* (non-Javadoc)
215 	 * @see org.alfresco.web.framework.Task#setStatus(java.lang.String)
216 	 */
217 	public void setStatus(String status)
218 	{
219 		this.status = status;
220 
221 		String pattern = "yyyy, MMMMM, d hh:mm";
222 		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(pattern);
223 		String result = formatter.format(new Date());
224 		String text = "[" + result + "] " + status;
225 
226 		history.add(text);
227 	}
228 
229 	/* (non-Javadoc)
230 	 * @see org.alfresco.web.framework.Task#getProgress()
231 	 */
232 	public int getProgress()
233 	{
234 		return this.progress;
235 	}
236 
237 	/* (non-Javadoc)
238 	 * @see org.alfresco.web.framework.Task#getProgressSize()
239 	 */
240 	public int getProgressSize()
241 	{
242 		return this.progressSize;
243 	}
244 
245 	/* (non-Javadoc)
246 	 * @see org.alfresco.web.framework.Task#getHistory()
247 	 */
248 	public List<String> getHistory()
249 	{
250 		return history;
251 	}
252 }