Skip to main content
summaryrefslogtreecommitdiffstats
blob: 97ea66e6749a25fbaa27ddceb98aacd7b41f4b64 (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
/*******************************************************************************
 * Copyright (c) 2004, 2015 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.tasks.core.data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.mylyn.tasks.core.IRepositoryPerson;
import org.eclipse.mylyn.tasks.core.ITask.PriorityLevel;
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
import org.eclipse.mylyn.tasks.core.ITaskComment;
import org.eclipse.mylyn.tasks.core.TaskRepository;

/**
 * @author Steffen Pingel
 * @since 3.0
 */
public class TaskAttributeMapper {

	private final TaskRepository taskRepository;

	public TaskAttributeMapper(@NonNull TaskRepository taskRepository) {
		Assert.isNotNull(taskRepository);
		this.taskRepository = taskRepository;
	}

	@NonNull
	public TaskAttribute createTaskAttachment(@NonNull TaskData taskData) {
		TaskAttribute taskAttribute = taskData.getRoot()
				.createAttribute(mapToRepositoryKey(taskData.getRoot(), TaskAttribute.NEW_ATTACHMENT));
//		TaskAttachmentMapper mapper = TaskAttachmentMapper.createFrom(taskAttribute);
//		mapper.setContentType("");
//		mapper.setFileName("");
//		mapper.setContentType("");
//		mapper.applyTo(taskAttribute);
		return taskAttribute;
	}

	public boolean equals(@NonNull TaskAttribute newAttribute, @NonNull TaskAttribute oldAttribute) {
		if (TaskAttribute.TYPE_COMMENT.equals(newAttribute.getMetaData().getType())) {
			if (newAttribute.getValues().equals(oldAttribute.getValues())) {
				return true;
			}
			// the comment mapping accidentally changed throughout the Mylyn 3.7 cycle therefore some
			// cases need to be considered equal even though attribute values differ
			if (oldAttribute != null) {
				TaskAttribute commentIdAttribute = oldAttribute.getAttribute("task.common.comment.id"); //$NON-NLS-1$
				// ID not present
				if ((commentIdAttribute == null || commentIdAttribute.getValue().equals("")) //$NON-NLS-1$
						&& newAttribute.getValue().equals("")) { //$NON-NLS-1$
					return true;
				}
				// ID previously stored in a sub attribute
				if (commentIdAttribute != null && commentIdAttribute.getValue().equals(newAttribute.getValue())) {
					return true;
				}
			}
		}
		return newAttribute.getValues().equals(oldAttribute.getValues());
	}

	@Nullable
	public TaskAttribute getAssoctiatedAttribute(@NonNull TaskAttribute taskAttribute) {
		String id = taskAttribute.getMetaDatum(TaskAttribute.META_ASSOCIATED_ATTRIBUTE_ID);
		if (id != null) {
			// look up as nested attribute first
			TaskAttribute associatedAttribute = taskAttribute.getAttribute(id);
			if (associatedAttribute != null) {
				return associatedAttribute;
			}
			// fall back to root
			return taskAttribute.getTaskData().getRoot().getAttribute(id);
		}
		return null;
	}

	@Nullable
	public TaskAttribute getAssoctiatedAttribute(@NonNull TaskOperation taskOperation) {
		TaskAttribute taskAttribute = taskOperation.getTaskAttribute();
		if (taskAttribute != null) {
			return getAssoctiatedAttribute(taskAttribute);
		}
		return null;
	}

	@NonNull
	public List<TaskAttribute> getAttributesByType(@NonNull TaskData taskData, @NonNull String type) {
		Assert.isNotNull(taskData);
		Assert.isNotNull(type);
		List<TaskAttribute> result = new ArrayList<TaskAttribute>();
		for (TaskAttribute taskAttribute : taskData.getRoot().getAttributes().values()) {
			if (type.equals(taskAttribute.getMetaData().getType())) {
				result.add(taskAttribute);
			}
		}
		return result;
	}

	public boolean getBooleanValue(@NonNull TaskAttribute attribute) {
		String booleanString = attribute.getValue();
		if (booleanString != null && booleanString.length() > 0) {
			return Boolean.parseBoolean(booleanString);
		}
		return false;
	}

	/**
	 * Connectors should ensure that this method returns dates at midnight in the local time zone when the
	 * {@link TaskAttribute#META_ATTRIBUTE_PRECISION precision} is {@link TimeUnit#DAYS} or coarser. This is because
	 * {@link Date Dates} are automatically displayed in the local time zone. This is not a concern when the precision
	 * is finer than {@link TimeUnit#DAYS}, because in that case the value has a time component which can meaningfully
	 * be converted to local time.
	 */
	@Nullable
	public Date getDateValue(@NonNull TaskAttribute attribute) {
		String dateString = attribute.getValue();
		try {
			if (dateString != null && dateString.length() > 0) {
				return new Date(Long.parseLong(dateString));
			}
		} catch (NumberFormatException e) {
			// ignore
		}
		return null;
	}

	/**
	 * @deprecated Not used, see {@link TaskAttributeMetaData#setDefaultOption(String)}
	 */
	@Nullable
	@Deprecated
	public String getDefaultOption(@NonNull TaskAttribute taskAttribute) {
		return taskAttribute.getMetaData().getDefaultOption();
	}

	/**
	 * @since 3.5
	 */
	@Nullable
	public Double getDoubleValue(@NonNull TaskAttribute attribute) {
		String doubleString = attribute.getValue();
		try {
			if (doubleString != null) {
				return Double.parseDouble(doubleString);
			}
		} catch (NumberFormatException e) {
			// ignore
		}
		return null;
	}

	@Nullable
	public Integer getIntegerValue(@NonNull TaskAttribute attribute) {
		String integerString = attribute.getValue();
		try {
			if (integerString != null) {
				return Integer.parseInt(integerString);
			}
		} catch (NumberFormatException e) {
			// ignore
		}
		return null;
	}

	@Nullable
	public String getLabel(@NonNull TaskAttribute taskAttribute) {
		return taskAttribute.getMetaData().getLabel();
	}

	@Nullable
	public Long getLongValue(@NonNull TaskAttribute attribute) {
		String longString = attribute.getValue();
		try {
			if (longString != null) {
				return Long.parseLong(longString);
			}
		} catch (NumberFormatException e) {
			// ignore
		}
		return null;
	}

	/**
	 * Returns labelByValue.
	 */
	@NonNull
	public Map<String, String> getOptions(@NonNull TaskAttribute attribute) {
		return attribute.getOptions();
	}

	@NonNull
	public IRepositoryPerson getRepositoryPerson(@NonNull TaskAttribute taskAttribute) {
		IRepositoryPerson person = taskRepository.createPerson(taskAttribute.getValue());
		TaskAttribute child = taskAttribute.getMappedAttribute(TaskAttribute.PERSON_NAME);
		if (child != null) {
			person.setName(getValue(child));
		}
		return person;
	}

	@NonNull
	public List<TaskOperation> getTaskOperations(@NonNull TaskAttribute operationsAttribute) {
		Assert.isNotNull(operationsAttribute);
		TaskData taskData = operationsAttribute.getTaskData();
		List<TaskOperation> result = new ArrayList<TaskOperation>();
		for (TaskAttribute taskAttribute : taskData.getRoot().getAttributes().values()) {
			if (TaskAttribute.TYPE_OPERATION.equals(taskAttribute.getMetaData().getType())
					&& !taskAttribute.getId().equals(mapToRepositoryKey(taskData.getRoot(), TaskAttribute.OPERATION))) {
				result.add(TaskOperation.createFrom(taskAttribute));
			}
		}
		return result;
	}

	@NonNull
	public TaskOperation getTaskOperation(@NonNull TaskAttribute taskAttribute) {
		Assert.isNotNull(taskAttribute);
		return TaskOperation.createFrom(taskAttribute);
	}

	@NonNull
	public TaskRepository getTaskRepository() {
		return taskRepository;
	}

	/**
	 * @return empty String if not available
	 */
	@Nullable
	public String getValue(@NonNull TaskAttribute taskAttribute) {
		return taskAttribute.getValue();
	}

	@NonNull
	public String getValueLabel(@NonNull TaskAttribute taskAttribute) {
		List<String> labels = getValueLabels(taskAttribute);
		StringBuilder sb = new StringBuilder();
		String sep = ""; //$NON-NLS-1$
		for (String value : labels) {
			sb.append(sep).append(value);
			sep = ", "; //$NON-NLS-1$
		}
		return sb.toString();
	}

	@NonNull
	public List<String> getValueLabels(@NonNull TaskAttribute taskAttribute) {
		List<String> values = taskAttribute.getValues();
		Map<String, String> options = getOptions(taskAttribute);
		List<String> result = new ArrayList<String>(values.size());
		for (String value : values) {
			String option = options.get(value);
			if (option != null) {
				value = option;
			} else if (taskAttribute.getOption(value) != null) {
				value = taskAttribute.getOption(value);
			}
			result.add(value);
		}
		return result;
	}

	@NonNull
	public List<String> getValues(@NonNull TaskAttribute attribute) {
		return new ArrayList<String>(attribute.getValues());
	}

	public boolean hasValue(@NonNull TaskAttribute attribute) {
		return attribute.getValues().size() > 0;
	}

	public String mapToRepositoryKey(@NonNull TaskAttribute parent, @NonNull String key) {
		return key;
	}

	public void setBooleanValue(@NonNull TaskAttribute attribute, @NonNull Boolean value) {
		attribute.setValue(Boolean.toString(value));
	}

	/**
	 * Connectors should ensure that this method accepts dates at midnight in the local time zone when the
	 * {@link TaskAttribute#META_ATTRIBUTE_PRECISION precision} is {@link TimeUnit#DAYS} or coarser.
	 */
	public void setDateValue(@NonNull TaskAttribute attribute, @Nullable Date date) {
		if (date != null) {
			attribute.setValue(Long.toString(date.getTime()));
		} else {
			attribute.clearValues();
		}
	}

	/**
	 * @since 3.5
	 */
	public void setDoubleValue(@NonNull TaskAttribute attribute, @Nullable Double value) {
		if (value != null) {
			attribute.setValue(value.toString());
		} else {
			attribute.clearValues();
		}
	}

	public void setIntegerValue(@NonNull TaskAttribute attribute, @Nullable Integer value) {
		if (value != null) {
			attribute.setValue(value.toString());
		} else {
			attribute.clearValues();
		}
	}

	public void setLongValue(@NonNull TaskAttribute attribute, @Nullable Long value) {
		if (value != null) {
			attribute.setValue(value.toString());
		} else {
			attribute.clearValues();
		}
	}

	public void setRepositoryPerson(@NonNull TaskAttribute taskAttribute, @NonNull IRepositoryPerson person) {
		setValue(taskAttribute, person.getPersonId());
		if (person.getName() != null) {
			TaskAttribute child = taskAttribute.createAttribute(TaskAttribute.PERSON_NAME);
			setValue(child, person.getName());
		}
	}

	public void setTaskOperation(@NonNull TaskAttribute taskAttribute, @NonNull TaskOperation taskOperation) {
		Assert.isNotNull(taskAttribute);
		Assert.isNotNull(taskOperation);
		TaskOperation.applyTo(taskAttribute, taskOperation.getOperationId(), taskOperation.getLabel());
	}

	public void setValue(@NonNull TaskAttribute attribute, @NonNull String value) {
		attribute.setValue(value);
	}

	public void setValues(@NonNull TaskAttribute attribute, @NonNull List<String> values) {
		attribute.setValues(values);
	}

	public void updateTaskAttachment(@NonNull ITaskAttachment taskAttachment, @NonNull TaskAttribute taskAttribute) {
		TaskAttachmentMapper.createFrom(taskAttribute).applyTo(taskAttachment);
	}

	public void updateTaskComment(@NonNull ITaskComment taskComment, @NonNull TaskAttribute taskAttribute) {
		TaskCommentMapper.createFrom(taskAttribute).applyTo(taskComment);
	}

	/**
	 * Connectors may override this method to specify the mapping from the repository's priority options to
	 * {@link PriorityLevel}
	 *
	 * @return the {@link PriorityLevel} corresponding to the given option for the given priority attribute
	 * @since 3.20
	 */
	public PriorityLevel getPriorityLevel(TaskAttribute priorityAttribute, String priorityOption) {
		return PriorityLevel.fromString(priorityOption);
	}

}

Back to the top