Skip to main content
summaryrefslogtreecommitdiffstats
blob: e6546ef7809e038c13682a94462d82dfa3750f6d (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
/*******************************************************************************
 * Copyright (c) 2004, 2008 Tasktop Technologies 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:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.core;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.internal.tasks.core.deprecated.AbstractAttributeFactory;
import org.eclipse.mylyn.internal.tasks.core.deprecated.AbstractLegacyRepositoryConnector;
import org.eclipse.mylyn.internal.tasks.core.deprecated.RepositoryTaskAttribute;
import org.eclipse.mylyn.internal.tasks.core.deprecated.RepositoryTaskData;
import org.eclipse.mylyn.tasks.core.IRepositoryManager;

/**
 * @deprecated Do not use. This class is pending for removal: see bug 237552.
 */
@Deprecated
public class TaskDataStorageManager {

	private final IRepositoryManager taskRepositoryManager;

	private final ITaskDataStorage storage;

	private int nextNewId = 1;

	public TaskDataStorageManager(IRepositoryManager taskRepositoryManager, ITaskDataStorage storage) {
		this.taskRepositoryManager = taskRepositoryManager;
		this.storage = storage;
	}

	/**
	 * Add a RepositoryTaskData to the offline reports file.
	 */
	public void setNewTaskData(RepositoryTaskData data) {
		if (data == null || data.getRepositoryUrl() == null || data.getTaskId() == null) {
			return;
		}

		TaskDataState state = retrieveState(data);
		if (state != null) {
			state.setNewTaskData(data);
		} else {
			state = new TaskDataState(data.getRepositoryUrl(), data.getTaskId());
			state.setNewTaskData(data);
		}
		saveState(state);
	}

	/**
	 * @since 2.3
	 */
	public void setNewTaskData(String repositoryUrl, String taskId, RepositoryTaskData data) {
		TaskDataState state = retrieveState(repositoryUrl, taskId);
		if (state != null) {
			state.setNewTaskData(data);
		} else {
			state = new TaskDataState(repositoryUrl, taskId);
			state.setNewTaskData(data);
		}
		saveState(state);
	}

	public void setOldTaskData(RepositoryTaskData data) {
		if (data == null || data.getRepositoryUrl() == null || data.getTaskId() == null) {
			return;
		}
		TaskDataState state = retrieveState(data);
		if (state != null) {
			state.setOldTaskData(data);
		} else {
			StatusHandler.log(new Status(IStatus.WARNING, ITasksCoreConstants.ID_PLUGIN,
					"Attempt to save old data when no new data exists", new Exception()));
		}
		saveState(state);
	}

	/**
	 * Returns the most recent copy of the task data.
	 * 
	 * @return offline task data, null if no data found
	 */
	public RepositoryTaskData getNewTaskData(String repositoryUrl, String id) {
		if (repositoryUrl == null || id == null) {
			return null;
		}
		TaskDataState state = retrieveState(repositoryUrl, id);
		if (state != null) {
			return state.getNewTaskData();
		}
		return null;
	}

	/**
	 * Returns the old copy if exists, null otherwise.
	 */
	public RepositoryTaskData getOldTaskData(String repositoryUrl, String id) {
		if (repositoryUrl == null || id == null) {
			return null;
		}
		TaskDataState state = retrieveState(repositoryUrl, id);
		if (state != null) {
			return state.getOldTaskData();
		}
		return null;
	}

	/**
	 * @return Get the next available temporary taskId. This taskId is given to new unsubmitted repository tasks.
	 *         Incremented each time this method is called.
	 */
	public synchronized String getNewRepositoryTaskId() {
		// TODO: generate based on values of unsubmitted offline report ids
		nextNewId++;
		if (nextNewId == Integer.MAX_VALUE) {
			nextNewId = 1;
		}
		return "" + nextNewId;
	}

	/**
	 * 
	 * @return editable copy of task data with any edits applied
	 */
	public RepositoryTaskData getEditableCopy(String repositoryUrl, String id) {
		if (repositoryUrl == null || id == null) {
			return null;
		}
		TaskDataState state = retrieveState(repositoryUrl, id);
		RepositoryTaskData clone = null;
		if (state != null) {
			if (state.getNewTaskData() != null) {
				try {
					clone = (RepositoryTaskData) ObjectCloner.deepCopy(state.getNewTaskData());
					updateAttributeFactory(clone);
				} catch (Exception e) {
					StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN,
							"Error constructing modifiable task", e));
					return null;
				}
			}
			if (clone != null) {
				for (RepositoryTaskAttribute attribute : state.getEdits()) {
					if (attribute == null) {
						continue;
					}
					RepositoryTaskAttribute existing = clone.getAttribute(attribute.getId());
					if (existing != null) {
						existing.clearValues();
						List<String> options = existing.getOptions();

						for (String value : attribute.getValues()) {
							if (options.size() > 0) {
								if (options.contains(value)) {
									existing.addValue(value);
								}
							} else {
								existing.addValue(value);
							}
						}

					} else {
						clone.addAttribute(attribute.getId(), attribute);
					}
				}
			}
		}
		return clone;

	}

	// TODO review: the state of the elements of changedAttribues could change between this call and the time state is written to disk, might need to make a full copy  
	public void saveEdits(String repositoryUrl, String id, Set<RepositoryTaskAttribute> changedAttributes) {
		TaskDataState state = retrieveState(repositoryUrl, id);
		if (state != null) {
			Set<RepositoryTaskAttribute> edits = state.getEdits();
			if (edits == null) {
				// Copy here?
				state.setEdits(changedAttributes);
			} else {
				edits.removeAll(changedAttributes);
				edits.addAll(changedAttributes);
			}
			try {
				storage.put(state);
			} catch (Exception e) {
				// FIXME what exception is caught here?
				StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, "Error saving edits", e));
			}
		}

	}

	public Set<RepositoryTaskAttribute> getEdits(String repositoryUrl, String id) {
		if (repositoryUrl == null || id == null) {
			return Collections.emptySet();
		}
		TaskDataState state = retrieveState(repositoryUrl, id);
		if (state != null) {
			if (state.getEdits() != null) {
				return Collections.unmodifiableSet(state.getEdits());
			}
		}
		return Collections.emptySet();
	}

	public void discardEdits(String repositoryUrl, String id) {
		if (repositoryUrl == null || id == null) {
			return;
		}
		TaskDataState state = retrieveState(repositoryUrl, id);
		if (state != null) {
			state.discardEdits();
			try {
				storage.put(state);
			} catch (Exception e) {
				// FIXME what exception is caught here?
				StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, "Error discarding edits", e));
			}
		}
	}

	public void remove(String repositoryUrl, String id) {
		if (repositoryUrl == null || id == null) {
			return;
		}
		storage.remove(repositoryUrl, id);
	}

	/**
	 * DESTROY ALL OFFLINE TASK DATA Public for testing only Forces a reset of all data maps Does not signal data
	 * changed (doesn't request save)
	 */
	public void clear() {
		nextNewId = 0;
		storage.clear();
	}

	/**
	 * After deserialization process the attributeFactory needs to be reset on each RepositoryTaskData.
	 */
	private void updateAttributeFactory(RepositoryTaskData taskData) {
		if (taskData == null) {
			return;
		}
		taskData.refresh();
		AbstractLegacyRepositoryConnector connector = (AbstractLegacyRepositoryConnector) taskRepositoryManager.getRepositoryConnector(taskData.getConnectorKind());
		if (connector != null && connector.getLegacyTaskDataHandler() != null) {
			AbstractAttributeFactory factory = connector.getLegacyTaskDataHandler().getAttributeFactory(taskData);
			if (factory != null) {
				taskData.setAttributeFactory(factory);
			}
		}
	}

	// XXX: review if task data cloning can be done without using serialization
	// Reference:
	// http://www.javaworld.com/javaworld/javatips/jw-javatip76.html?page=2
	public static class ObjectCloner {

		private ObjectCloner() {
			// can not instantiate
		}

		static public Object deepCopy(Object oldObj) throws Exception {
			ObjectOutputStream outputStream = null;
			ObjectInputStream inputStream = null;
			try {
				ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
				outputStream = new ObjectOutputStream(byteArrayOutputStream);

				outputStream.writeObject(oldObj);
				outputStream.flush();
				ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
						byteArrayOutputStream.toByteArray());
				inputStream = new ObjectInputStream(byteArrayInputStream);
				return inputStream.readObject();
			} catch (Exception e) {
				throw (e);
			} finally {
				if (outputStream != null) {
					outputStream.close();
				}
				if (inputStream != null) {
					inputStream.close();
				}
			}
		}

	}

	public void start() {
		try {
			storage.start();
		} catch (Exception e) {
			StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, "Offline storage start failed",
					e));
		}
	}

	public void stop() {
		try {
			storage.stop();
		} catch (Exception e) {
			StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, "Offline storage stop failed", e));
		}
	}

	public void saveNow() {
		storage.flush();
	}

	private TaskDataState retrieveState(RepositoryTaskData data) {
		return retrieveState(data.getRepositoryUrl(), data.getTaskId());
	}

	private TaskDataState retrieveState(String repositoryUrl, String id) {
		TaskDataState state = null;
		try {
			state = storage.get(repositoryUrl, id);
			if (state != null) {
				// TODO: Get rid of attribute factory on containers!!!
				if (state.getNewTaskData() != null) {
					updateAttributeFactory(state.getNewTaskData());
				}
				if (state.getOldTaskData() != null) {
					updateAttributeFactory(state.getOldTaskData());
				}
			}
		} catch (Exception e) {
			// FIXME what Exception is caught here?
			StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, "Error saving offline data", e));
		}
		return state;
	}

	private void saveState(TaskDataState state) {
		storage.put(state);
	}

}

Back to the top