Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5aa96d2c3ae498b0898c5d58be600833a1062f5d (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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*******************************************************************************
 * 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.mylyn.internal.tasks.ui.util;

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.mylyn.internal.tasks.core.RepositoryTaskHandleUtil;
import org.eclipse.mylyn.internal.tasks.core.TaskArchive;
import org.eclipse.mylyn.internal.tasks.core.TaskCategory;
import org.eclipse.mylyn.internal.tasks.core.TaskExternalizationException;
import org.eclipse.mylyn.internal.tasks.core.UnfiledCategory;
import org.eclipse.mylyn.monitor.core.StatusHandler;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryQuery;
import org.eclipse.mylyn.tasks.core.AbstractTask;
import org.eclipse.mylyn.tasks.core.AbstractTaskCategory;
import org.eclipse.mylyn.tasks.core.AbstractTaskContainer;
import org.eclipse.mylyn.tasks.core.AbstractTaskListFactory;
import org.eclipse.mylyn.tasks.core.TaskList;
import org.eclipse.mylyn.tasks.core.AbstractTask.PriorityLevel;
import org.eclipse.mylyn.tasks.core.AbstractTask.RepositoryTaskSyncState;
import org.eclipse.mylyn.web.core.XmlUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Subclass externalizers must override the get*TagName() methods for the types of externalized items they support to
 * ensure that their externalizer does not externalize tasks from other connectors incorrectly.
 * 
 * These tag names uniquely identify the externalizer to be used to read the task from externalized form on disk.
 * 
 * The canCreateElementFor methods specify which tasks the externalizer should write to disk.
 * 
 * The TaskList is read on startup, so externalizers extending this should not perform any slow (i.e., network)
 * operations when overriding methods.
 * 
 * @author Mik Kersten
 * @author Ken Sueda (XML serialization support)
 * @author Steffen Pingel
 */
final class DelegatingTaskExternalizer {

	static final String DEFAULT_PRIORITY = PriorityLevel.P3.toString();

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

	static final String KEY_NOTIFIED_INCOMING = "NotifiedIncoming";

	static final String KEY_NAME = "Name";

	static final String KEY_LABEL = "Label";

	static final String KEY_HANDLE = "Handle";

	static final String KEY_REPOSITORY_URL = "RepositoryUrl";

	static final String KEY_CATEGORY = "Category";

	static final String VAL_ROOT = "Root";

	static final String KEY_SUBTASK = "SubTask";

	static final String KEY_KIND = "Kind";

	static final String KEY_TASK_CATEGORY = "Task" + KEY_CATEGORY;

	static final String KEY_LINK = "Link";

	static final String KEY_PLAN = "Plan";

	static final String KEY_TIME_ESTIMATED = "Estimated";

	static final String KEY_ISSUEURL = "IssueURL";

	static final String KEY_NOTES = "Notes";

	static final String KEY_ACTIVE = "Active";

	static final String KEY_COMPLETE = "Complete";

	static final String KEY_PRIORITY = "Priority";

	static final String KEY_PATH = "Path";

	static final String VAL_FALSE = "false";

	static final String VAL_TRUE = "true";

	static final String KEY_DATE_END = "EndDate";

	static final String KEY_QUERY_HIT = "QueryHit";

	static final String KEY_DATE_CREATION = "CreationDate";

	static final String KEY_DATE_REMINDER = "ReminderDate";

	static final String KEY_DATE_DUE = "DueDate";

	static final String KEY_REMINDED = "Reminded";

	/**
	 * This element holds the date stamp recorded upon last transition to a synchronized state.
	 */
	static final String KEY_LAST_MOD_DATE = "LastModified";

	static final String KEY_DIRTY = "Dirty";

	static final String KEY_SYNC_STATE = "offlineSyncState";

	static final String KEY_OWNER = "Owner";

	static final String KEY_STALE = "Stale";

	static final String KEY_LAST_REFRESH = "LastRefreshTimeStamp";

	private List<AbstractTaskListFactory> factories = new ArrayList<AbstractTaskListFactory>();

	public void setFactories(List<AbstractTaskListFactory> externalizers) {
		this.factories = externalizers;
	}

	public Element createCategoryElement(AbstractTaskContainer category, Document doc, Element parent) {
		if (category instanceof TaskArchive) {
			return parent;
		} else if (category instanceof UnfiledCategory) {
			return parent;
		} else {
			Element node = doc.createElement(getCategoryTagName());
			node.setAttribute(DelegatingTaskExternalizer.KEY_NAME, category.getSummary());
			parent.appendChild(node);
			return node;
		}
	}

//	/**
//	 * Override to create specific elements
//	 */
//	public boolean canCreateElementFor(AbstractTask task) {
//		return false;
//	}

	public Element createTaskElement(AbstractTask task, Document doc, Element parent) {
		AbstractTaskListFactory factory = null;
		for (AbstractTaskListFactory currentFactory : factories) {
			if (currentFactory.canCreate(task)) {
				factory = currentFactory;
				break;
			}
		}

		String taskTagName;
		if (factory != null) {
			taskTagName = factory.getTaskElementName();
		} else {
			StatusHandler.log("No externalizer for task: " + task, this);
			return null;
//			taskTagName = getTaskTagName();
		}
		Element node = doc.createElement(taskTagName);
		factory.setAdditionalAttributes(task, node);

		node.setAttribute(KEY_LABEL, stripControlCharacters(task.getSummary()));
		node.setAttribute(KEY_HANDLE, task.getHandleIdentifier());
		node.setAttribute(KEY_REPOSITORY_URL, task.getRepositoryUrl());

		AbstractTaskContainer container = null;

		if (task.getParentContainers().size() > 0) {
			container = task.getParentContainers().iterator().next();
		}
		if (container != null) {
			if (container.getHandleIdentifier().equals(UnfiledCategory.HANDLE)) {
				node.setAttribute(KEY_CATEGORY, VAL_ROOT);
			} else {
				node.setAttribute(KEY_CATEGORY, container.getHandleIdentifier());
			}
		} else {
			// TODO: if/when subtasks are supported this should be handled
		}

		node.setAttribute(KEY_PRIORITY, task.getPriority());
		node.setAttribute(KEY_KIND, task.getTaskKind());

		if (task.isCompleted()) {
			node.setAttribute(KEY_COMPLETE, VAL_TRUE);
		} else {
			node.setAttribute(KEY_COMPLETE, VAL_FALSE);
		}
		if (task.isActive()) {
			node.setAttribute(KEY_ACTIVE, VAL_TRUE);
		} else {
			node.setAttribute(KEY_ACTIVE, VAL_FALSE);
		}

		if (task.getUrl() != null) {
			node.setAttribute(KEY_ISSUEURL, task.getUrl());
		}
		node.setAttribute(KEY_NOTES, stripControlCharacters(task.getNotes()));
		node.setAttribute(KEY_TIME_ESTIMATED, "" + task.getEstimateTimeHours());
		node.setAttribute(KEY_DATE_END, formatExternDate(task.getCompletionDate()));
		node.setAttribute(KEY_DATE_CREATION, formatExternDate(task.getCreationDate()));
		node.setAttribute(KEY_DATE_DUE, formatExternDate(task.getDueDate()));
		node.setAttribute(KEY_DATE_REMINDER, formatExternDate(task.getScheduledForDate()));
		if (task.isReminded()) {
			node.setAttribute(KEY_REMINDED, VAL_TRUE);
		} else {
			node.setAttribute(KEY_REMINDED, VAL_FALSE);
		}
		if (task.isStale()) {
			node.setAttribute(KEY_STALE, VAL_TRUE);
		} else {
			node.setAttribute(KEY_STALE, VAL_FALSE);
		}

		AbstractTask abstractTask = task;
		if (abstractTask.getLastReadTimeStamp() != null) {
			node.setAttribute(KEY_LAST_MOD_DATE, abstractTask.getLastReadTimeStamp());
		}

		if (abstractTask.isNotified()) {
			node.setAttribute(KEY_NOTIFIED_INCOMING, VAL_TRUE);
		} else {
			node.setAttribute(KEY_NOTIFIED_INCOMING, VAL_FALSE);
		}

		if (abstractTask.getSynchronizationState() != null) {
			node.setAttribute(KEY_SYNC_STATE, abstractTask.getSynchronizationState().toString());
		} else {
			node.setAttribute(KEY_SYNC_STATE, RepositoryTaskSyncState.SYNCHRONIZED.toString());
		}

		if (abstractTask.getOwner() != null) {
			node.setAttribute(KEY_OWNER, abstractTask.getOwner());
		}

		for (AbstractTask t : task.getChildren()) {
			createSubTaskElement(t, doc, node);
		}

		parent.appendChild(node);
		return node;
	}

	public void readSubTasks(AbstractTask task, NodeList nodes, TaskList tasklist) {
		for (int j = 0; j < nodes.getLength(); j++) {
			Node child = nodes.item(j);
			Element element = (Element) child;
			if (element.hasAttribute(KEY_HANDLE)) {
				String handle = element.getAttribute(KEY_HANDLE);
				AbstractTask subTask = tasklist.getTask(handle);
				if (subTask != null) {
					tasklist.addTask(subTask, task);
				}
			}
		}
	}

	public void createSubTaskElement(AbstractTask task, Document doc, Element parent) {
		Element node = doc.createElement(KEY_SUBTASK);
		node.setAttribute(KEY_HANDLE, task.getHandleIdentifier());
		parent.appendChild(node);
	}

	private String stripControlCharacters(String text) {
		if (text == null)
			return "";
		return XmlUtil.cleanXmlString(text);
	}

	private 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 taskList) throws TaskExternalizationException {
		boolean hasCaughtException = false;
		Element element = (Element) node;

		AbstractTaskCategory category;
		if (element.hasAttribute(DelegatingTaskExternalizer.KEY_NAME)) {
			category = new TaskCategory(element.getAttribute(DelegatingTaskExternalizer.KEY_NAME));
			taskList.internalAddCategory((TaskCategory) category);
		} else {
			// LEGACY: registry categories did not have names
			category = taskList.getArchiveContainer();
		}

		NodeList list = node.getChildNodes();
		for (int i = 0; i < list.getLength(); i++) {
			Node child = list.item(i);
			try {
				// LEGACY: categories used to contain tasks?
				category.internalAddChild(readTask(child, taskList, category, null));
			} catch (Throwable t) {
				hasCaughtException = true;
			}
		}
		if (hasCaughtException) {
			throw new TaskExternalizationException("Failed to load all tasks");
		}
	}

	public final AbstractTask readTask(Node node, TaskList taskList, AbstractTaskCategory category, AbstractTask parent)
			throws TaskExternalizationException {
		AbstractTask task = null;
		String taskId = null;
		String repositoryUrl = null;
		String summary = "";
//		boolean alreadyRead = false;

		Element element = (Element) node;
		if (element.hasAttribute(KEY_HANDLE)) {
			String handle = element.getAttribute(KEY_HANDLE);
			repositoryUrl = RepositoryTaskHandleUtil.getRepositoryUrl(handle);
			taskId = RepositoryTaskHandleUtil.getTaskId(handle);
		} else {
			throw new TaskExternalizationException("Handle not stored for repository task");
		}
		if (element.hasAttribute(KEY_LABEL)) {
			summary = element.getAttribute(KEY_LABEL);
		}

		for (AbstractTaskListFactory externalizer : factories) {
			if (node.getNodeName().equals(externalizer.getTaskElementName())) {
				task = externalizer.createTask(repositoryUrl, taskId, summary, element);
				break;
			}
		}

//		if (!alreadyRead && this.canReadTask(node)) {
//			task = this.createTask(repositoryUrl, taskId, summary, element, taskList, category, parent);
//			alreadyRead = true;
//		}
		if (task != null) {
			readTaskInfo(task, taskList, element, parent, category);
		}

		return task;
	}

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

//	/**
//	 * Override for connector-specific implementation
//	 */
//	public AbstractTask createTask(String repositoryUrl, String taskId, String summary, Element element,
//			TaskList taskList, AbstractTaskContainer category, AbstractTask parent) throws TaskExternalizationException {
//		String handle;
//		if (element.hasAttribute(KEY_HANDLE)) {
//			handle = element.getAttribute(KEY_HANDLE);
//		} else {
//			throw new TaskExternalizationException("Handle not stored for task");
//		}
//		AbstractTask task = new LocalTask(handle, summary);
//		return task;
//	}

	private void readTaskInfo(AbstractTask task, TaskList taskList, Element element, AbstractTask parent,
			AbstractTaskCategory legacyCategory) throws TaskExternalizationException {
		if (task == null) {
			return;
		}

		String categoryHandle = null;
		if (element.hasAttribute(KEY_CATEGORY)) {
			categoryHandle = element.getAttribute(KEY_CATEGORY);
			AbstractTaskCategory category = null;
			if (categoryHandle != null) {
				category = taskList.getContainerForHandle(categoryHandle);
			}

			if (category != null) {
				taskList.internalAddTask(task, category);
			} else if (parent == null) {
				taskList.internalAddRootTask(task);
			}
		} else if (legacyCategory != null && !(legacyCategory instanceof TaskArchive)) {
			task.addParentContainer(legacyCategory);
			legacyCategory.internalAddChild(task);
		} else if (legacyCategory == null && parent == null) {
			taskList.internalAddTask(task, taskList.getArchiveContainer());
		} else {
			taskList.internalAddTask(task, taskList.getArchiveContainer());
		}
		if (element.hasAttribute(KEY_PRIORITY)) {
			task.setPriority(element.getAttribute(KEY_PRIORITY));
		} else {
			task.setPriority(DEFAULT_PRIORITY);
		}

		if (element.hasAttribute(KEY_KIND)) {
			task.setTaskKind(element.getAttribute(KEY_KIND));
		}

		if (element.getAttribute(KEY_ACTIVE).compareTo(VAL_TRUE) == 0) {
			task.setActive(true);
			taskList.setActive(task, true);
		} else {
			task.setActive(false);
		}
		if (element.hasAttribute(KEY_ISSUEURL)) {
			task.setUrl(element.getAttribute(KEY_ISSUEURL));
		} else {
			task.setUrl("");
		}
		if (element.hasAttribute(KEY_NOTES)) {
			task.setNotes(element.getAttribute(KEY_NOTES));
		} else {
			task.setNotes("");
		}

		if (element.hasAttribute(KEY_TIME_ESTIMATED)) {
			String est = element.getAttribute(KEY_TIME_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(KEY_COMPLETE).compareTo(VAL_TRUE) == 0) {
			task.setCompleted(true);
		} else {
			task.setCompleted(false);
		}
		if (element.hasAttribute(KEY_DATE_END)) {
			task.setCompletionDate(getDateFromString(element.getAttribute(KEY_DATE_END)));
		} else {
			task.setCompletionDate(null);
		}
		if (element.hasAttribute(KEY_DATE_CREATION)) {
			task.setCreationDate(getDateFromString(element.getAttribute(KEY_DATE_CREATION)));
		} else {
			task.setCreationDate(Calendar.getInstance().getTime());
		}
		if (element.hasAttribute(KEY_DATE_DUE)) {
			task.setDueDate(getDateFromString(element.getAttribute(KEY_DATE_DUE)));
		} else {
			task.setDueDate(null);
		}
		if (element.hasAttribute(KEY_DATE_REMINDER)) {
			task.setScheduledForDate(getDateFromString(element.getAttribute(KEY_DATE_REMINDER)));
		} else {
			task.setScheduledForDate(null);
		}
		if (element.hasAttribute(KEY_REMINDED) && element.getAttribute(KEY_REMINDED).compareTo(VAL_TRUE) == 0) {
			task.setReminded(true);
		} else {
			task.setReminded(false);
		}
		if (element.hasAttribute(KEY_STALE) && element.getAttribute(KEY_STALE).compareTo(VAL_TRUE) == 0) {
			task.setStale(true);
		} else {
			task.setStale(false);
		}

		AbstractTask abstractTask = task;
		abstractTask.setSynchronizing(false);

		if (element.hasAttribute(DelegatingTaskExternalizer.KEY_REPOSITORY_URL)) {
			abstractTask.setRepositoryUrl(element.getAttribute(DelegatingTaskExternalizer.KEY_REPOSITORY_URL));
		}

		if (element.hasAttribute(KEY_LAST_MOD_DATE) && !element.getAttribute(KEY_LAST_MOD_DATE).equals("")) {
			abstractTask.setLastReadTimeStamp(element.getAttribute(KEY_LAST_MOD_DATE));
		}

		if (element.hasAttribute(KEY_OWNER)) {
			abstractTask.setOwner(element.getAttribute(KEY_OWNER));
		}

		if (VAL_TRUE.equals(element.getAttribute(KEY_NOTIFIED_INCOMING))) {
			abstractTask.setNotified(true);
		} else {
			abstractTask.setNotified(false);
		}

		if (element.hasAttribute(KEY_SYNC_STATE)) {
			String syncState = element.getAttribute(KEY_SYNC_STATE);
			if (syncState.compareTo(RepositoryTaskSyncState.SYNCHRONIZED.toString()) == 0) {
				abstractTask.setSynchronizationState(RepositoryTaskSyncState.SYNCHRONIZED);
			} else if (syncState.compareTo(RepositoryTaskSyncState.INCOMING.toString()) == 0) {
				abstractTask.setSynchronizationState(RepositoryTaskSyncState.INCOMING);
			} else if (syncState.compareTo(RepositoryTaskSyncState.OUTGOING.toString()) == 0) {
				abstractTask.setSynchronizationState(RepositoryTaskSyncState.OUTGOING);
			} else if (syncState.compareTo(RepositoryTaskSyncState.CONFLICT.toString()) == 0) {
				abstractTask.setSynchronizationState(RepositoryTaskSyncState.CONFLICT);
			}
		}

// NodeList list = element.getChildNodes();
// for (int j = 0; j < list.getLength(); j++) {
// Node child = list.item(j);
// task.addSubTask(readTask(child, taskList, null, task));
// }
	}

	// /**
	// * @return task ID, or null if not found
	// */
	// protected void readLegacyHandleFormat(ITask task, Element element) throws
	// TaskExternalizationException {
	// if (task instanceof AbstractTask) {
	// AbstractTask abstractTask = (AbstractTask) task;
	//			
	// if (element.hasAttribute(KEY_HANDLE)) {
	// String handle = element.getAttribute(KEY_HANDLE);
	// String repositoryUrl = RepositoryTaskHandleUtil.getRepositoryUrl(handle);
	// String taskId = RepositoryTaskHandleUtil.getTaskId(handle);
	// abstractTask.setRepositoryUrl(repositoryUrl);
	// abstractTask.setTaskId(taskId);
	// } else {
	// throw new TaskExternalizationException("Handle not stored for repository
	// task");
	// }
	// }
	// }

	// protected void readTaskData(AbstractTask task) {
	// RepositoryTaskData data =
	// taskDataManager.getRepositoryTaskData(task.getHandleIdentifier());
	// // RepositoryTaskData data =
	// //
	// TasksUiPlugin.getTaskDataManager().getTaskData(task.getHandleIdentifier());
	// task.setTaskData(data);
	//		
	// if (data != null && data.hasLocalChanges()) {
	// task.setSyncState(RepositoryTaskSyncState.OUTGOING);
	// }
	// }

	private 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) {
			StatusHandler.fail(e, "Could not parse end date", false);
		}
		return date;
	}

	private String getCategoryTagName() {
		return KEY_TASK_CATEGORY;
	}

//	public String getTaskTagName() {
//		return AbstractTaskListElementFactory.KEY_TASK;
//	}

//	public boolean canCreateElementFor(AbstractRepositoryQuery query) {
//		return false;
//	}

	public Element createQueryElement(AbstractRepositoryQuery query, Document doc, Element parent) {
		AbstractTaskListFactory factory = null;
		String queryTagName = null;
		for (AbstractTaskListFactory currentFactory : factories) {
			if (currentFactory.canCreate(query)) {
				factory = currentFactory;
				queryTagName = factory.getQueryElementName(query);
				break;
			}
		}
		if (factory == null || queryTagName == null) {
			StatusHandler.log("No externalizer for query: " + query, this);
			return null;
//			queryTagName = getQueryTagNameForElement(query);
		}

//		String queryTagName = getQueryTagNameForElement(query);

		Element node = doc.createElement(queryTagName);
		factory.setAdditionalAttributes(query, node);

		node.setAttribute(DelegatingTaskExternalizer.KEY_NAME, query.getSummary());
		node.setAttribute(AbstractTaskListFactory.KEY_QUERY_STRING, query.getUrl());
		node.setAttribute(DelegatingTaskExternalizer.KEY_REPOSITORY_URL, query.getRepositoryUrl());
		if (query.getLastSynchronizedTimeStamp() != null) {
			node.setAttribute(DelegatingTaskExternalizer.KEY_LAST_REFRESH, query.getLastSynchronizedTimeStamp());
		}
		for (AbstractTask hit : query.getChildren()) {
			try {
				createQueryHitElement(hit, doc, node);
			} catch (Exception e) {
				StatusHandler.log(e, e.getMessage());
			}
		}
		parent.appendChild(node);
		return node;
	}

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

//	public AbstractRepositoryQuery readQuery(Node node, TaskList tlist) throws TaskExternalizationException {
//		// doesn't know how to read any queries
//		return null;
//	}

//	public String getQueryTagNameForElement(AbstractRepositoryQuery query) {
//		return AbstractTaskListElementFactory.KEY_QUERY;
//	}

//	private String getQueryHitTagName() {
//		return AbstractTaskListElementFactory.KEY_QUERY_HIT;
//	}

	public Element createQueryHitElement(AbstractTask queryHit, Document doc, Element parent) {
		Element node = doc.createElement(KEY_QUERY_HIT);
		node.setAttribute(KEY_HANDLE, queryHit.getHandleIdentifier());
		parent.appendChild(node);
		return node;
	}

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

	public final void readQueryHit(Element element, TaskList taskList, AbstractRepositoryQuery query)
			throws TaskExternalizationException {

		if (element.hasAttribute(KEY_HANDLE)) {
			String handle = element.getAttribute(KEY_HANDLE);
			AbstractTask hit = taskList.getTask(handle);
			if (hit != null) {
				taskList.addTask(hit, query);
			}
		} else {
			throw new TaskExternalizationException("Handle not stored for repository task");
		}
	}

	public List<AbstractTaskListFactory> getDelegateExternalizers() {
		return factories;
	}
}

Back to the top