Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8874cc2c108d403bbfff082b14c184d56edf0265 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*******************************************************************************
 * Copyright (c) 2004 - 2005 University Of British Columbia and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylar.tasklist.internal;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.mylar.core.MylarPlugin;
import org.eclipse.mylar.tasklist.ITaskListCategory;
import org.eclipse.mylar.tasklist.IQuery;
import org.eclipse.mylar.tasklist.IQueryHit;
import org.eclipse.mylar.tasklist.ITask;
import org.eclipse.mylar.tasklist.ITaskListExternalizer;
import org.eclipse.mylar.tasklist.Task;
import org.eclipse.mylar.tasklist.ui.actions.TaskActivateAction;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author Mik Kersten and Ken Sueda
 */
public class DefaultTaskListExternalizer implements ITaskListExternalizer {

	public static final String TAG_QUERY = "Query";
	public static final String TAG_QUERY_HIT = "QueryHit";
	public static final String MAX_HITS = "MaxHits";
	public static final String QUERY_STRING = "QueryString";
	
	public static final String LABEL = "Label";
	public static final String HANDLE = "Handle";
	public static final String TAG_CATEGORY = "Category";
	public static final String TAG_TASK = "Task";
	public static final String TAG_TASK_CATEGORY = "Task" + TAG_CATEGORY;
	
	public static final String LINK = "Link";
	public static final String ESTIMATED = "Estimated";
	public static final String ELAPSED = "Elapsed";
	public static final String ISSUEURL = "IssueURL";
	public static final String NOTES = "Notes";
	public static final String BUGZILLA = "Bugzilla";
	public static final String ACTIVE = "Active";
	public static final String COMPLETE = "Complete";
	public static final String PRIORITY = "Priority";
	public static final String PATH = "Path";
	public static final String FALSE = "false";
	public static final String TRUE = "true";
	public static final String NAME = "Name";
	public static final String END_DATE = "EndDate";
	public static final String CREATION_DATE = "CreationDate";
	public static final String REMINDER_DATE = "ReminderDate";
	public static final String REMINDED = "Reminded";

	private List<ITaskListExternalizer> externalizers = new ArrayList<ITaskListExternalizer>();
	
	void setExternalizers(List<ITaskListExternalizer> externalizers) {
		this.externalizers = externalizers;
	}
	
	public boolean canCreateElementFor(ITaskListCategory category) {
		return category instanceof TaskCategory;
	}
	
	public Element createCategoryElement(ITaskListCategory category, Document doc, Element parent) {
		if(category.isArchive())
			return parent;
		Element node = doc.createElement(getCategoryTagName());
		node.setAttribute(NAME, category.getDescription(false));
				
		for (ITask task : ((TaskCategory)category).getChildren()) {
			try {
				Element element = null;
				for (ITaskListExternalizer externalizer : externalizers) {
					if (externalizer.canCreateElementFor(task)) element = externalizer.createTaskElement(task, doc, node);
				}
				if (element == null) createTaskElement(task, doc, node);
			} catch (Exception e) {
				MylarPlugin.log(e, e.getMessage());
			}
			
		}
		parent.appendChild(node);
		return node;
	}

	public boolean canCreateElementFor(ITask task) {
		return true;
	}
	
	public Element createTaskElement(ITask task, Document doc, Element parent) {
		Element node = doc.createElement(getTaskTagName());
		node.setAttribute(PATH, task.getPath());
		node.setAttribute(LABEL, task.getDescription(false));
		node.setAttribute(HANDLE, task.getHandle());
		node.setAttribute(PRIORITY, task.getPriority());
		
		if (task.isCompleted()) {
			node.setAttribute(COMPLETE, TRUE);
		} else {
			node.setAttribute(COMPLETE, FALSE);
		}
		if (task.isActive()) {
			node.setAttribute(ACTIVE, TRUE);
		} else {
			node.setAttribute(ACTIVE, FALSE);
		}
		node.setAttribute(BUGZILLA, FALSE); // TODO: this is not great
		
		node.setAttribute(ISSUEURL, task.getIssueReportURL());
		node.setAttribute(NOTES, task.getNotes());
		node.setAttribute(ELAPSED, task.getElapsedTime());
		node.setAttribute(ESTIMATED, ""+task.getEstimateTime());
		node.setAttribute(END_DATE, task.getEndDateString());
		node.setAttribute(CREATION_DATE, task.getCreationDateString());
		node.setAttribute(REMINDER_DATE, task.getReminderDateString(false));
		if (task.hasBeenReminded()) {
			node.setAttribute(REMINDED, TRUE);
		} else {
			node.setAttribute(REMINDED, FALSE);
		}
		List<String> rl = task.getRelatedLinks().getLinks();
		int i = 0;
		for (String link : rl) {
			node.setAttribute(LINK+i, link);
			i++;
		}
		
		for (ITask t : task.getChildren()) {
			createTaskElement(t, doc, node);
		}
		parent.appendChild(node);
		return node;
	}

	public boolean canReadCategory(Node node) {
		return node.getNodeName().equals(getCategoryTagName());
	}
	
	public void readCategory(Node node, TaskList tlist)  throws MylarExternalizerException {
		boolean hasCaughtException = false;
		Element element = (Element) node;
		TaskCategory category = new TaskCategory(element.getAttribute("Name"));
		tlist.internalAddCategory(category);
		NodeList list = node.getChildNodes();
		for (int i = 0; i < list.getLength(); i++) {
			Node child = list.item(i);
			boolean read = false;
			try {
				for (ITaskListExternalizer externalizer : externalizers) {
					if (externalizer.canReadTask(child)) {
						category.internalAddTask(externalizer.readTask(child, tlist,
								category, null));
						read = true;
					}
				}
				if (!read && canReadTask(child)) {
					category.internalAddTask(readTask(child, tlist, category, null));
				}
			} catch (MylarExternalizerException e) {
				hasCaughtException = true;
			}
		}
		if (hasCaughtException) throw new MylarExternalizerException("Failed to load all tasks");
	}

	public boolean canReadTask(Node node) {
		return node.getNodeName().equals(getTaskTagName());
	}

	public ITask readTask(Node node, TaskList tlist, ITaskListCategory category, ITask parent)  throws MylarExternalizerException {
		Element element = (Element) node;
		String handle;
		String label;
		if (element.hasAttribute(HANDLE)) {
			handle = element.getAttribute(HANDLE);
		} else {
			throw new MylarExternalizerException("Handle not stored for task");			
		}
		if (element.hasAttribute(LABEL)) {
			label = element.getAttribute(LABEL);
		} else {
			label = "Description was corrupted in stored tasklist";
		}
		Task task = new Task(handle, label, false);		
		readTaskInfo(task, tlist, element, category, parent);
		return task;
	}

	protected void readTaskInfo(ITask task, TaskList tlist, Element element, ITaskListCategory category, ITask parent)  throws MylarExternalizerException{
		if (element.hasAttribute(PRIORITY)) {
			task.setPriority(element.getAttribute(PRIORITY));
		} else {
			task.setPriority("P3");
		}
		if (element.hasAttribute(PATH)) {
			task.setPath(element.getAttribute(PATH));
		} else {
			task.setPath(task.getHandle());
		}		
		
		if (element.getAttribute(ACTIVE).compareTo(TRUE) == 0) {
			task.setActive(true, false);
			tlist.setActive(task, true, false);
			new TaskActivateAction(task).run();
		} else {
			task.setActive(false, false);
		}	
		if (element.hasAttribute(ISSUEURL)) {
			task.setIssueReportURL(element.getAttribute(ISSUEURL));
		} else {
			task.setIssueReportURL("");
		}
		if (element.hasAttribute(NOTES)) {
			task.setNotes(element.getAttribute(NOTES));
		} else {
			task.setNotes("");
		}
		if (element.hasAttribute(ELAPSED)) {
			task.setElapsedTime(element.getAttribute(ELAPSED));			
		} else {
			task.setElapsedTime("");
		}
		if (element.hasAttribute(ESTIMATED)) {
			String est = element.getAttribute(ESTIMATED);
			try {
				int estimate = Integer.parseInt(est);
				task.setEstimatedTime(estimate);
			} catch (Exception e) {
				task.setEstimatedTime(0);
			}					
		} else {
			task.setEstimatedTime(0);
		}
		// NOTE: do not change the order of complete and end date!!
		if (element.getAttribute(COMPLETE).compareTo(TRUE) == 0) {
			task.setCompleted(true);
		} else {
			task.setCompleted(false);
		}
		if (element.hasAttribute(END_DATE)) {
			task.setEndDate(element.getAttribute(END_DATE));
		} else {
			task.setEndDate("");
		}	
		if (element.hasAttribute(CREATION_DATE)) {
			task.setCreationDate(element.getAttribute(CREATION_DATE));
		} else {
			task.setCreationDate("");
		}
		if (element.hasAttribute(REMINDER_DATE)) {
			task.setReminderDate(element.getAttribute(REMINDER_DATE));
		} else {
			task.setReminderDate("");
		}	
		if (element.hasAttribute(REMINDED) && element.getAttribute(REMINDED).compareTo(TRUE) == 0) {
			task.setReminded(true);
		} else {
			task.setReminded(false);
		}	
		int i = 0;
		while (element.hasAttribute(LINK+i)) {
			task.getRelatedLinks().add(element.getAttribute(LINK+i));
			i++;
		}
		if (category != null) {
			task.internalSetCategory((TaskCategory) category);
		} else {
			task.internalSetCategory(null);
		}
		task.setParent(parent);
		NodeList list = element.getChildNodes();
		for (int j = 0; j < list.getLength(); j++) {
			Node child = list.item(j);
			task.addSubTask(readTask(child, tlist, null, task));
		}
	}

	public String getCategoryTagName() {
		return TAG_TASK_CATEGORY;
	}

	public String getTaskTagName() {
		return TAG_TASK;
	}

	public void createRegistry(Document doc, Node parent) {
		// nothing to do
	}
	
	public boolean canCreateElementFor(IQuery category) {
		return true;
	}

	public Element createQueryElement(IQuery query, Document doc, Element parent) {
		Element node = doc.createElement(getQueryTagNameForElement(query));
		node.setAttribute(NAME, query.getDescription(false));
		node.setAttribute(MAX_HITS, query.getMaxHits()+"");
		node.setAttribute(QUERY_STRING, query.getQueryString());
		for(IQueryHit hit: query.getChildren()){
			try {
				Element element = null;
				for (ITaskListExternalizer externalizer : externalizers) {
					if (externalizer.canCreateElementFor(hit)) element = externalizer.createQueryHitElement(hit, doc, node);
				}
				if (element == null) createQueryHitElement(hit, doc, node);
			} catch (Exception e) {
				MylarPlugin.log(e, e.getMessage());
			}
		}
		parent.appendChild(node);
		return node;
	}

	public boolean canReadQuery(Node node) {
		return false;
	}

	public void readQuery(Node node, TaskList tlist) throws MylarExternalizerException {
		// doesn't know how to read any queries
		
	}

	public String getQueryTagNameForElement(IQuery query) {
		return "";
	}

	public String getQueryHitTagName() {
		return TAG_QUERY_HIT;
	}

	public boolean canCreateElementFor(IQueryHit queryHit) {
		return true;
	}

	public Element createQueryHitElement(IQueryHit queryHit, Document doc, Element parent) {
		Element node = doc.createElement(getQueryHitTagName());
		node.setAttribute(NAME, queryHit.getDescription(false));
		node.setAttribute(HANDLE, queryHit.getHandle());
		node.setAttribute(PRIORITY, queryHit.getPriority());
		if (queryHit.isCompleted()) {
			node.setAttribute(COMPLETE, TRUE);
		} else {
			node.setAttribute(COMPLETE, FALSE);
		}
		parent.appendChild(node);
		return null;
	}

	public boolean canReadQueryHit(Node node) {
		return false;
	}

	public void readQueryHit(Node node, TaskList tlist, IQuery query) throws MylarExternalizerException {
		// doesn't know how to read a query hit		
	}
}

Back to the top