Skip to main content
summaryrefslogtreecommitdiffstats
blob: 070200e8bf65c173177cc9e9aad85a7f4585b8d3 (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
/*******************************************************************************
 * 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.bugzilla.ui.tasks;

import java.util.Date;

import org.eclipse.mylar.bugzilla.ui.BugzillaUiPlugin;
import org.eclipse.mylar.bugzilla.ui.tasks.BugzillaTask.BugTaskState;
import org.eclipse.mylar.core.MylarPlugin;
import org.eclipse.mylar.tasks.AbstractCategory;
import org.eclipse.mylar.tasks.ITask;
import org.eclipse.mylar.tasks.ITaskContributor;
import org.eclipse.mylar.tasks.MylarTasksPlugin;
import org.eclipse.mylar.tasks.internal.DefaultTaskListExternalizer;
import org.eclipse.mylar.tasks.internal.MylarExternalizerException;
import org.eclipse.mylar.tasks.internal.TaskList;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * The wierd thing here is that the registry gets read in as a normal
 * category, but gets written out by createRegistry
 * 
 * @author Mik Kersten and Ken Sueda
 */
public class BugzillaTaskExternalizer extends DefaultTaskListExternalizer {

	private static final String BUGZILLA = "Bugzilla";
	private static final String LAST_DATE = "LastDate";
	private static final String DIRTY = "Dirty";
	private static final String URL = "URL";
	private static final String DESCRIPTION = "Description";

	private static final String BUGZILLA_TASK_REGISTRY = "BugzillaTaskRegistry" + TAG_CATEGORY;
	private static final String TAG_BUGZILLA_CATEGORY = "BugzillaQuery" + TAG_CATEGORY;
	private static final String TAG_TASK = "BugzillaReport";
	
	@Override
	public void createRegistry(Document doc, Node parent) {
		Element node = doc.createElement(BUGZILLA_TASK_REGISTRY);
		for (BugzillaTask task : BugzillaUiPlugin.getDefault().getBugzillaTaskListManager().getBugzillaTaskRegistry().values()) {
			try {
				createTaskElement(task, doc, node);
			} catch (Exception e) {
				MylarPlugin.log(e, e.getMessage());
			}
			
		}
		parent.appendChild(node);
	} 
	
	@Override
	public boolean canReadCategory(Node node) {
		return node.getNodeName().equals(getCategoryTagName())
			|| node.getNodeName().equals(BUGZILLA_TASK_REGISTRY);
	}

	@Override
	public void readCategory(Node node, TaskList taskList)  throws MylarExternalizerException {
		Element e = (Element) node;
		if (e.getNodeName().equals(BUGZILLA_TASK_REGISTRY)) {
			readRegistry(node, taskList);
		} else {
			BugzillaQueryCategory cat = new BugzillaQueryCategory(e.getAttribute(DESCRIPTION), e.getAttribute(URL));
			taskList.addCategory(cat);
		}
	}

	public void readRegistry(Node node, TaskList taskList)  throws MylarExternalizerException {
		boolean hasCaughtException = false;
		NodeList list = node.getChildNodes();		
		for (int i = 0; i < list.getLength(); i++) {
			try {
				Node child = list.item(i);
				ITask task = readTask(child, taskList, null, null);
				if (task instanceof BugzillaTask) {
					BugzillaUiPlugin.getDefault().getBugzillaTaskListManager()
							.addToBugzillaTaskRegistry((BugzillaTask) task);
				}
			} catch (MylarExternalizerException e) {
				hasCaughtException = true;
			}
		}
		if (hasCaughtException) throw new MylarExternalizerException("Failed to restore all tasks");
	}
	
	public boolean canCreateElementFor(AbstractCategory category) {
		return category instanceof BugzillaQueryCategory;
	}
	
	public Element createCategoryElement(AbstractCategory category, Document doc, Element parent) {
		BugzillaQueryCategory queryCategory = (BugzillaQueryCategory)category;
		Element node = doc.createElement(getCategoryTagName());
		node.setAttribute(DESCRIPTION, queryCategory.getDescription(false));
		node.setAttribute(URL, queryCategory.getUrl());
		parent.appendChild(node);
		return node;
	}

	public boolean canCreateElementFor(ITask task) {
		return task instanceof BugzillaTask;
	}
	
	public Element createTaskElement(ITask task, Document doc, Element parent) {
		Element node = super.createTaskElement(task, doc, parent);
		BugzillaTask bt = (BugzillaTask) task;
		node.setAttribute(BUGZILLA, TRUE);
		if (bt.getLastRefresh() != null) {
			node.setAttribute(LAST_DATE, new Long(bt.getLastRefreshTime()
					.getTime()).toString());
		} else {
			node.setAttribute(LAST_DATE, new Long(new Date().getTime()).toString());
		}
		
		if (bt.isDirty()) {
			node.setAttribute(DIRTY, TRUE);
		} else {
			node.setAttribute(DIRTY, FALSE);
		}
//		bt.saveBugReport(false); // XXX don't think that this needs to be done, should be handled already
		return node;
	}

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

	@Override
	public ITask readTask(Node node, TaskList tlist, AbstractCategory 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 bug report");
		}
		if (element.hasAttribute(LABEL)) {
			label = element.getAttribute(LABEL);
		} else {
			throw new MylarExternalizerException("Description not stored for bug report");
		}
		BugzillaTask task = new BugzillaTask(handle, label, true);		
		readTaskInfo(task, tlist, element, category, parent);
				
		task.setState(BugTaskState.FREE);
		task.setLastRefresh(new Date(new Long(element.getAttribute("LastDate"))
				.longValue()));
		if (element.getAttribute("Dirty").compareTo("true") == 0) {
			task.setDirty(true);
		} else {
			task.setDirty(false);
		}
		try {
			if (task.readBugReport() == false) {
				MylarPlugin.log("Failed to read bug report", null);
			}
		} catch(Exception e) {
			MylarPlugin.log(e, "Failed to read bug report");
		}
		
		ITaskContributor contributor = MylarTasksPlugin.getDefault().getContributorForElement(task);
	    if(contributor != null){
    		ITask addedTask = contributor.taskAdded(task);
    		if(addedTask instanceof BugzillaTask) task = (BugzillaTask)addedTask;
    	}
		return task;
	}
	
	@Override
	public String getCategoryTagName() {
		return TAG_BUGZILLA_CATEGORY;
	}

	@Override
	public String getTaskTagName() {
		return TAG_TASK;
	}
}

Back to the top