Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0c01ae38b0a53b8e8400a106e28468fd73027a6e (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
/*******************************************************************************
 * Copyright (c) 2004, 2009 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.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.Assert;

/**
 * Encapsulates attributes for task data.
 * 
 * @author Rob Elves
 * @author Steffen Pingel
 * @since 3.0
 * @noextend This class is not intended to be subclassed by clients.
 */
public final class TaskAttribute {

	/**
	 * Boolean attribute. If true, repository user needs to be added to the cc list.
	 */
	public static final String ADD_SELF_CC = "task.common.addselfcc"; //$NON-NLS-1$

	public static final String ATTACHMENT_AUTHOR = "task.common.attachment.author"; //$NON-NLS-1$

	public static final String ATTACHMENT_CONTENT_TYPE = "task.common.attachment.ctype"; //$NON-NLS-1$

	public static final String ATTACHMENT_DATE = "task.common.attachment.date"; //$NON-NLS-1$

	public static final String ATTACHMENT_DESCRIPTION = "task.common.attachment.description"; //$NON-NLS-1$

	public static final String ATTACHMENT_FILENAME = "filename"; //$NON-NLS-1$

	public static final String ATTACHMENT_ID = "task.common.attachment.id"; //$NON-NLS-1$

	public static final String ATTACHMENT_IS_DEPRECATED = "task.common.attachment.deprecated"; //$NON-NLS-1$

	public static final String ATTACHMENT_IS_PATCH = "task.common.attachment.patch"; //$NON-NLS-1$

	public static final String ATTACHMENT_SIZE = "task.common.attachment.size"; //$NON-NLS-1$

	public static final String ATTACHMENT_URL = "task.common.attachment.url"; //$NON-NLS-1$

	public static final String COMMENT_ATTACHMENT_ID = "task.common.comment.attachment.id"; //$NON-NLS-1$

	public static final String COMMENT_AUTHOR = "task.common.comment.author"; //$NON-NLS-1$

	@Deprecated
	public static final String COMMENT_AUTHOR_NAME = "task.common.comment.author.name"; //$NON-NLS-1$

	public static final String COMMENT_DATE = "task.common.comment.date"; //$NON-NLS-1$

	public static final String COMMENT_HAS_ATTACHMENT = "task.common.comment.attachment"; //$NON-NLS-1$

	public static final String COMMENT_NEW = "task.common.comment.new"; //$NON-NLS-1$

	/**
	 * @since 3.0
	 */
	public static final String COMMENT_NUMBER = "task.common.comment.number"; //$NON-NLS-1$

	public static final String COMMENT_TEXT = "task.common.comment.text"; //$NON-NLS-1$

	public static final String COMMENT_URL = "task.common.comment.url"; //$NON-NLS-1$

	/**
	 * @since 3.0
	 */
	public static final String COMPONENT = "task.common.component"; //$NON-NLS-1$

	/**
	 * @since 3.0
	 */
	public static final String DATE_COMPLETION = "task.common.date.completed"; //$NON-NLS-1$

	public static final String DATE_CREATION = "task.common.date.created"; //$NON-NLS-1$

	/**
	 * @since 3.0
	 */
	public static final String DATE_DUE = "task.common.date.due"; //$NON-NLS-1$

	public static final String DATE_MODIFICATION = "task.common.date.modified"; //$NON-NLS-1$

	public static final String DESCRIPTION = "task.common.description"; //$NON-NLS-1$

	public static final String KEYWORDS = "task.common.keywords"; //$NON-NLS-1$

	public static final String KIND_DEFAULT = "task.common.kind.default"; //$NON-NLS-1$

	public static final String KIND_OPERATION = "task.common.kind.operation"; //$NON-NLS-1$

	public static final String KIND_PEOPLE = "task.common.kind.people"; //$NON-NLS-1$

	//public static final String META_SHOW_IN_ATTRIBUTES_SECTION = "task.meta.showInTaskEditorAttributesSection";

	public static final String META_ASSOCIATED_ATTRIBUTE_ID = "task.meta.associated.attribute"; //$NON-NLS-1$

	public static final String META_ATTRIBUTE_KIND = "task.meta.attributeKind"; //$NON-NLS-1$

	public static final String META_ATTRIBUTE_TYPE = "task.meta.type"; //$NON-NLS-1$

	public static final String META_DEFAULT_OPTION = "task.meta.defaultOption"; //$NON-NLS-1$

//	public static final String META_DETAIL_LEVEL = "task.meta.detailLevel";

	public static final String META_LABEL = "task.meta.label"; //$NON-NLS-1$

	public static final String META_READ_ONLY = "task.meta.readOnly"; //$NON-NLS-1$

	public static final String NEW_ATTACHMENT = "task.common.new.attachment"; //$NON-NLS-1$

	// XXX merge with USER_CC
	//public static final String NEW_CC = "task.common.newcc";

	public static final String OPERATION = "task.common.operation"; //$NON-NLS-1$

	public static final String PERSON_NAME = "task.common.person.name"; //$NON-NLS-1$

	public static final String PREFIX_ATTACHMENT = "task.common.attachment-"; //$NON-NLS-1$

	public static final String PREFIX_COMMENT = "task.common.comment-"; //$NON-NLS-1$

	// XXX merge with USER_CC
	//public static final String REMOVE_CC = "task.common.removecc";

	public static final String PREFIX_OPERATION = "task.common.operation-"; //$NON-NLS-1$

	public static final String PRIORITY = "task.common.priority"; //$NON-NLS-1$

	public static final String PRODUCT = "task.common.product"; //$NON-NLS-1$

	public static final String RESOLUTION = "task.common.resolution"; //$NON-NLS-1$

	public static final String STATUS = "task.common.status"; //$NON-NLS-1$

	public static final String SUMMARY = "task.common.summary"; //$NON-NLS-1$

	public static final String TASK_KEY = "task.common.key"; //$NON-NLS-1$

	public static final String TASK_KIND = "task.common.kind"; //$NON-NLS-1$

	/**
	 * @since 3.0
	 */
	public static final String TASK_URL = "task.common.url"; //$NON-NLS-1$

	/**
	 * @since 3.0
	 */
	public static final String TYPE_ATTACHMENT = "attachment"; //$NON-NLS-1$

	/**
	 * @since 3.0
	 */
	public static final String TYPE_BOOLEAN = "boolean"; //$NON-NLS-1$

	/**
	 * @since 3.0
	 */
	public static final String TYPE_COMMENT = "comment"; //$NON-NLS-1$

	/**
	 * @since 3.0
	 */
	public static final String TYPE_CONTAINER = "container"; //$NON-NLS-1$

	/**
	 * @since 3.0
	 */
	public static final String TYPE_DATE = "date"; //$NON-NLS-1$

	/**
	 * @since 3.1
	 */
	public static final String TYPE_DATETIME = "dateTime"; //$NON-NLS-1$

	/**
	 * @since 3.0
	 */
	public static final String TYPE_INTEGER = "integer"; //$NON-NLS-1$

	/**
	 * @since 3.1
	 */
	public static final String TYPE_LONG = "long"; //$NON-NLS-1$

	/**
	 * @since 3.0
	 */
	public static final String TYPE_LONG_RICH_TEXT = "longRichText"; //$NON-NLS-1$

	/**
	 * @since 3.0
	 */
	public static final String TYPE_LONG_TEXT = "longText"; //$NON-NLS-1$

	/**
	 * @since 3.0
	 */
	public static final String TYPE_MULTI_SELECT = "multiSelect"; //$NON-NLS-1$

	public static final String TYPE_OPERATION = "operation"; //$NON-NLS-1$

	/**
	 * @since 3.0
	 */
	public static final String TYPE_PERSON = "person"; //$NON-NLS-1$

	/**
	 * @since 3.0
	 */
	public static final String TYPE_SHORT_RICH_TEXT = "shortRichText"; //$NON-NLS-1$

	/**
	 * @since 3.0
	 */
	public static final String TYPE_SHORT_TEXT = "shortText"; //$NON-NLS-1$

	/**
	 * @since 3.0
	 */
	public static final String TYPE_SINGLE_SELECT = "singleSelect"; //$NON-NLS-1$

	/**
	 * @since 3.0
	 */
	public static final String TYPE_TASK_DEPENDENCY = "taskDepenedency"; //$NON-NLS-1$

	public static final String TYPE_URL = "url"; //$NON-NLS-1$

	public static final String USER_ASSIGNED = "task.common.user.assigned"; //$NON-NLS-1$

	@Deprecated
	public static final String USER_ASSIGNED_NAME = "task.common.user.assigned.name"; //$NON-NLS-1$

	public static final String USER_CC = "task.common.user.cc"; //$NON-NLS-1$

	public static final String USER_REPORTER = "task.common.user.reporter"; //$NON-NLS-1$

	@Deprecated
	public static final String USER_REPORTER_NAME = "task.common.user.reporter.name"; //$NON-NLS-1$

	/**
	 * @since 3.2
	 */
	public static final String SEVERITY = "task.common.severity"; //$NON-NLS-1$

	/**
	 * @since 3.2
	 */
	public static final String VERSION = "task.common.version"; //$NON-NLS-1$

	private Map<String, TaskAttribute> attributeById;

	private final String attributeId;

	private Map<String, String> metaData;

	private Map<String, String> optionByKey;

	private final TaskAttribute parentAttribute;

	private final TaskData taskData;

	/**
	 * Attribute's values (selected or added)
	 */
	private final List<String> values;

	public TaskAttribute(TaskAttribute parentAttribute, String attributeId) {
		Assert.isNotNull(parentAttribute);
		Assert.isNotNull(attributeId);
		this.parentAttribute = parentAttribute;
		this.attributeId = attributeId;
		this.taskData = parentAttribute.getTaskData();
		this.values = new ArrayList<String>(1);
		parentAttribute.add(this);
	}

	/**
	 * Constructor for the root node.
	 */
	TaskAttribute(TaskData taskData) {
		Assert.isNotNull(taskData);
		this.parentAttribute = null;
		this.taskData = taskData;
		this.attributeId = "root"; //$NON-NLS-1$
		this.values = new ArrayList<String>(1);
	}

	private void add(TaskAttribute attribute) {
		if (attributeById == null) {
			attributeById = new LinkedHashMap<String, TaskAttribute>();
		}
		attributeById.put(attribute.getId(), attribute);
	}

	public void addValue(String value) {
		Assert.isNotNull(value);
		values.add(value);
	}

	public void clearAttributes() {
		attributeById = null;
	}

	void clearMetaDataMap() {
		metaData = null;
	}

	public void clearOptions() {
		optionByKey = null;
	}

	public void clearValues() {
		values.clear();
	}

	public TaskAttribute createAttribute(String attributeId) {
		return new TaskAttribute(this, attributeId);
	}

	public void deepAddCopy(TaskAttribute source) {
		TaskAttribute target = createAttribute(source.getId());
		target.values.addAll(source.values);
		if (source.metaData != null) {
			target.metaData = new LinkedHashMap<String, String>(source.metaData);
		}
		if (source.optionByKey != null) {
			target.optionByKey = new LinkedHashMap<String, String>(source.optionByKey);
		}
		if (source.attributeById != null) {
			for (TaskAttribute child : source.attributeById.values()) {
				target.deepAddCopy(child);
			}
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		final TaskAttribute other = (TaskAttribute) obj;
		if (attributeId == null) {
			if (other.attributeId != null) {
				return false;
			}
		} else if (!attributeId.equals(other.attributeId)) {
			return false;
		}
		return true;
	}

	public TaskAttribute getAttribute(String attributeId) {
		Assert.isNotNull(attributeId);
		return (attributeById != null) ? attributeById.get(attributeId) : null;
	}

	public Map<String, TaskAttribute> getAttributes() {
		if (attributeById != null) {
			return Collections.unmodifiableMap(attributeById);
		} else {
			return Collections.emptyMap();
		}
	}

	public String getId() {
		return attributeId;
	}

	public TaskAttribute getMappedAttribute(String attributeId) {
		Assert.isNotNull(attributeId);
		return (attributeById != null) ? attributeById.get(getTaskData().getAttributeMapper().mapToRepositoryKey(this,
				attributeId)) : null;
	}

	public TaskAttribute getMappedAttribute(String[] path) {
		TaskAttribute attribute = this;
		for (String id : path) {
			attribute = attribute.getMappedAttribute(id);
			if (attribute == null) {
				break;
			}
		}
		return attribute;
	}

	String getMetaDatum(String key) {
		return (metaData != null) ? metaData.get(key) : null;
	}

	Map<String, String> getMetaDataMap() {
		if (metaData != null) {
			return Collections.unmodifiableMap(metaData);
		} else {
			return Collections.emptyMap();
		}
	}

	public String getOption(String key) {
		return (optionByKey != null) ? optionByKey.get(key) : null;
	}

	public Map<String, String> getOptions() {
		if (optionByKey != null) {
			return Collections.unmodifiableMap(optionByKey);
		} else {
			return Collections.emptyMap();
		}
	}

	public TaskAttribute getParentAttribute() {
		return parentAttribute;
	}

	public String[] getPath() {
		List<String> path = new ArrayList<String>();
		TaskAttribute attribute = this;
		while (attribute.getParentAttribute() != null) {
			path.add(attribute.getId());
			attribute = attribute.getParentAttribute();
		}
		Collections.reverse(path);
		return path.toArray(new String[0]);
	}

	public TaskAttributeMetaData getMetaData() {
		return new TaskAttributeMetaData(this);
	}

	public TaskData getTaskData() {
		return taskData;
	}

	public String getValue() {
		if (values.size() > 0) {
			return values.get(0);
		} else {
			return ""; //$NON-NLS-1$
		}
	}

	public List<String> getValues() {
		return Collections.unmodifiableList(values);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((attributeId == null) ? 0 : attributeId.hashCode());
		return result;
	}

	void putMetaDatum(String key, String value) {
		Assert.isNotNull(key);
		Assert.isNotNull(value);
		if (metaData == null) {
			metaData = new LinkedHashMap<String, String>();
		}
		metaData.put(key, value);
	}

	/**
	 * Adds an attribute option value
	 * 
	 * @param readableValue
	 *            The value displayed on the screen
	 * @param parameterValue
	 *            The option value used when sending the form to the server
	 */
	public void putOption(String key, String value) {
		Assert.isNotNull(key);
		Assert.isNotNull(value);
		if (optionByKey == null) {
			optionByKey = new LinkedHashMap<String, String>();
		}
		optionByKey.put(key, value);
	}

	public void removeAttribute(String attributeId) {
		if (attributeById != null) {
			attributeById.remove(attributeId);
		}
	}

	void removeMetaDatum(String metaDataId) {
		if (metaData != null) {
			metaData.remove(metaDataId);
		}
	}

	public void removeValue(String value) {
		values.remove(value);
	}

	public void setValue(String value) {
		Assert.isNotNull(value);
		if (values.size() > 0) {
			values.clear();
		}
		values.add(value);
	}

	public void setValues(List<String> values) {
		this.values.clear();
		this.values.addAll(values);
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		toString(sb, ""); //$NON-NLS-1$
		return sb.toString();
	}

	private void toString(StringBuilder sb, String prefix) {
		sb.append(prefix);
		sb.append("TaskAttribute[id="); //$NON-NLS-1$
		sb.append(attributeId);
		sb.append(",values="); //$NON-NLS-1$
		sb.append(values);
		sb.append(",options="); //$NON-NLS-1$
		sb.append(optionByKey);
		sb.append(",metaData="); //$NON-NLS-1$
		sb.append(metaData);
		sb.append("]"); //$NON-NLS-1$
		if (attributeById != null) {
			for (TaskAttribute child : attributeById.values()) {
				sb.append("\n"); //$NON-NLS-1$
				child.toString(sb, prefix + " "); //$NON-NLS-1$
			}
		}
	}

	public TaskAttribute createMappedAttribute(String attributeId) {
		Assert.isNotNull(attributeId);
		String mappedAttributeId = getTaskData().getAttributeMapper().mapToRepositoryKey(this, attributeId);
		Assert.isNotNull(mappedAttributeId);
		return new TaskAttribute(this, mappedAttributeId);
	}
}

Back to the top