Skip to main content
summaryrefslogtreecommitdiffstats
blob: 18eea60aebecda7e4b6d719f3a4357bc8268d00f (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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*******************************************************************************
 * Copyright (c) 2004 - 2006 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.internal.tasklist;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.eclipse.mylar.internal.core.util.MylarStatusHandler;
import org.eclipse.mylar.tasklist.IQueryHit;
import org.eclipse.mylar.tasklist.ITask;
import org.eclipse.mylar.tasklist.ITaskCategory;
import org.eclipse.mylar.tasklist.ITaskListExternalizer;
import org.eclipse.mylar.tasklist.IRepositoryQuery;
import org.eclipse.mylar.tasklist.ITaskRepositoryClient;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author Mik Kersten
 * @author Ken Sueda (XML serialization support)
 */
public class DelegatingLocalTaskExternalizer implements ITaskListExternalizer {

	private static final String DEFAULT_PRIORITY = "P3";

	private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.S z";

	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 REPOSITORY_URL = "RepositoryUrl";

	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 PLAN = "Plan";

	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";

	public static final String LABEL_AUTOMATIC = "<automatic>";

	private List<ITaskListExternalizer> delegateExternalizers = new ArrayList<ITaskListExternalizer>();

	/**
	 * TODO: make local task externalizer use repository.
	 */
	public ITaskRepositoryClient getRepositoryClient() {
		return null;
	}
	
	/**
	 * Set these on the TaskListWriter instead
	 */
	void setDelegateExternalizers(List<ITaskListExternalizer> externalizers) {
		this.delegateExternalizers = externalizers;
	}

	public boolean canCreateElementFor(ITaskCategory category) {
		return category instanceof TaskCategory;
	}

	public Element createCategoryElement(ITaskCategory category, Document doc, Element parent) {
		if (category.isArchive())
			return parent;
		Element node = doc.createElement(getCategoryTagName());
		node.setAttribute(NAME, category.getDescription());

		for (ITask task : ((TaskCategory) category).getChildren()) {
			try {
				Element element = null;
				for (ITaskListExternalizer externalizer : delegateExternalizers) {
					if (externalizer.canCreateElementFor(task)) {
						element = externalizer.createTaskElement(task, doc, node);
					}
				}
				if (element == null)
					createTaskElement(task, doc, node);
			} catch (Exception e) {
				MylarStatusHandler.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.getRemoteContextPath());
		node.setAttribute(LABEL, task.getDescription());
		node.setAttribute(HANDLE, task.getHandleIdentifier());
		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: get rid of this

		if (task.getUrl() != null) {
			node.setAttribute(ISSUEURL, task.getUrl());
		}
		// if (task.getRepositoryUrl() != null) {
		// node.setAttribute(REPOSITORY_URL, task.getRepositoryUrl());
		// }
		node.setAttribute(NOTES, task.getNotes());
		node.setAttribute(ELAPSED, "" + task.getElapsedTime());
		node.setAttribute(ESTIMATED, "" + task.getEstimateTimeHours());
		node.setAttribute(END_DATE, formatExternDate(task.getCompletionDate()));
		node.setAttribute(CREATION_DATE, formatExternDate(task.getCreationDate()));
		node.setAttribute(REMINDER_DATE, formatExternDate(task.getReminderDate()));
		if (task.hasBeenReminded()) {
			node.setAttribute(REMINDED, TRUE);
		} else {
			node.setAttribute(REMINDED, FALSE);
		}
		List<String> rl = task.getRelatedLinks();
		int i = 0;
		for (String link : rl) {
			node.setAttribute(LINK + i, link);
			i++;
		}
		List<String> plans = task.getPlans();
		int currPlan = 0;
		for (String plan : plans) {
			node.setAttribute(PLAN + currPlan, plan);
			currPlan++;
		}

		for (ITask t : task.getChildren()) {
			createTaskElement(t, doc, node);
		}
		parent.appendChild(node);
		return node;
	}

	protected String formatExternDate(Date date) {
		if (date == null)
			return "";
		String f = DATE_FORMAT;
		SimpleDateFormat format = new SimpleDateFormat(f, Locale.ENGLISH);
		return format.format(date);
	}

	public boolean canReadCategory(Node node) {
		return node.getNodeName().equals(getCategoryTagName());
	}

	public void readCategory(Node node, TaskList tlist) throws TaskListExternalizerException {
		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 : delegateExternalizers) {
					if (externalizer.canReadTask(child)) {
						ITask task = externalizer.readTask(child, tlist, category, null);
						category.addTask(task);
						if (!category.isArchive())
							task.setCategory(category);
						read = true;
					}
				}
				if (!read && canReadTask(child)) {
					category.internalAddTask(readTask(child, tlist, category, null));
				}
			} catch (TaskListExternalizerException e) {
				hasCaughtException = true;
			}
		}
		if (hasCaughtException)
			throw new TaskListExternalizerException("Failed to load all tasks");
	}

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

	public ITask readTask(Node node, TaskList tlist, ITaskCategory category, ITask parent)
			throws TaskListExternalizerException {
		Element element = (Element) node;
		String handle;
		String label;
		if (element.hasAttribute(HANDLE)) {
			handle = element.getAttribute(HANDLE);
		} else {
			throw new TaskListExternalizerException("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, ITaskCategory category, ITask parent)
			throws TaskListExternalizerException {
		if (element.hasAttribute(PRIORITY)) {
			task.setPriority(element.getAttribute(PRIORITY));
		} else {
			task.setPriority(DEFAULT_PRIORITY);
		}
		// if (element.hasAttribute(PATH)) {
		// task.setRemoteContextPath(element.getAttribute(PATH));
		// } else {
		// task.setRemoteContextPath(task.getHandleIdentifier());
		// }

		if (element.getAttribute(ACTIVE).compareTo(TRUE) == 0) {
			task.setActive(true);
			tlist.setActive(task, true);
		} else {
			task.setActive(false);
		}
		if (element.hasAttribute(ISSUEURL)) {
			task.setUrl(element.getAttribute(ISSUEURL));
		} else {
			task.setUrl("");
		}
		if (element.hasAttribute(NOTES)) {
			task.setNotes(element.getAttribute(NOTES));
		} else {
			task.setNotes("");
		}
		if (element.hasAttribute(ELAPSED)) {
			long elapsed = 0;
			try {
				long read = Long.parseLong(element.getAttribute(ELAPSED));
				if (read > 0)
					elapsed = read;
			} catch (NumberFormatException e) {
				// ignore
			}
			task.setElapsedTime(elapsed);
		} else {
			task.setElapsedTime(0);
		}
		if (element.hasAttribute(ESTIMATED)) {
			String est = element.getAttribute(ESTIMATED);
			try {
				int estimate = Integer.parseInt(est);
				task.setEstimatedTimeHours(estimate);
			} catch (Exception e) {
				task.setEstimatedTimeHours(0);
			}
		} else {
			task.setEstimatedTimeHours(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.setCompletionDate(getDateFromString(element.getAttribute(END_DATE)));
			// task.setEndDate(element.getAttribute(END_DATE));
		} else {
			task.setCompletionDate(null);
		}
		if (element.hasAttribute(CREATION_DATE)) {
			task.setCreationDate(getDateFromString(element.getAttribute(CREATION_DATE)));
			// task.setCreationDate(element.getAttribute(CREATION_DATE));
		} else {
			task.setCreationDate(Calendar.getInstance().getTime());
		}
		if (element.hasAttribute(REMINDER_DATE)) {
			task.setReminderDate(getDateFromString(element.getAttribute(REMINDER_DATE)));
			// task.setReminderDate(element.getAttribute(REMINDER_DATE));
		} else {
			task.setReminderDate(null);
		}
		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++;
		}
		int ii = 0;
		while (element.hasAttribute(PLAN + ii)) {
			task.getPlans().add(element.getAttribute(PLAN + i));
			ii++;
		}
		if (category != null) {
			task.setCategory((TaskCategory) category);
		} else {
			task.setCategory(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));
		}
	}

	protected Date getDateFromString(String dateString) {
		Date date = null;
		if ("".equals(dateString))
			return null;
		String formatString = DATE_FORMAT;
		SimpleDateFormat format = new SimpleDateFormat(formatString, Locale.ENGLISH);
		try {
			date = format.parse(dateString);
		} catch (ParseException e) {
			MylarStatusHandler.fail(e, "Could not parse end date", false);
		}
		return date;
	}

	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(IRepositoryQuery category) {
		return true;
	}

	public Element createQueryElement(IRepositoryQuery query, Document doc, Element parent) {
		String queryTagName = getQueryTagNameForElement(query);
		Element node = doc.createElement(queryTagName);
		node.setAttribute(NAME, query.getDescription());
		node.setAttribute(MAX_HITS, query.getMaxHits() + "");
		node.setAttribute(QUERY_STRING, query.getQueryUrl());
		node.setAttribute(REPOSITORY_URL, query.getRepositoryUrl());

		for (IQueryHit hit : query.getHits()) {
			try {
				Element element = null;
				for (ITaskListExternalizer externalizer : delegateExternalizers) {
					if (externalizer.canCreateElementFor(hit))
						element = externalizer.createQueryHitElement(hit, doc, node);
				}
				if (element == null)
					createQueryHitElement(hit, doc, node);
			} catch (Exception e) {
				MylarStatusHandler.log(e, e.getMessage());
			}
		}
		parent.appendChild(node);
		return node;
	}

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

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

	}

	public String getQueryTagNameForElement(IRepositoryQuery 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());
		node.setAttribute(HANDLE, queryHit.getHandleIdentifier());
		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, IRepositoryQuery query) throws TaskListExternalizerException {
		// doesn't know how to read a query hit
	}

	public List<ITaskListExternalizer> getDelegateExternalizers() {
		return delegateExternalizers;
	}
}

Back to the top