Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1ac5d28af964807f64f76529f97dd103cbc71312 (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
/*******************************************************************************
 * 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.bugzilla.core;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.mylyn.tasks.core.IRepositoryPerson;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.TaskAttachmentMapper;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;

/**
 * @author Rob Elves
 */
public class BugzillaAttributeMapper extends TaskAttributeMapper {

	private static final String DATE_FORMAT_1 = "yyyy-MM-dd HH:mm";

	private static final String DATE_FORMAT_2 = "yyyy-MM-dd HH:mm:ss";

	private static final String DATE_FORMAT_3 = "yyyy-MM-dd";

	private static final String delta_ts_format = DATE_FORMAT_2;

	private static final String creation_ts_format = DATE_FORMAT_1;

	private static final String deadline_format = DATE_FORMAT_3;

	private static final String customAttribute_format = DATE_FORMAT_2;

	/**
	 * public for testing Bugzilla 2.18 uses DATE_FORMAT_1 but later versions use DATE_FORMAT_2 Using lowest common
	 * denominator DATE_FORMAT_1
	 */
	public static final String comment_creation_ts_format = DATE_FORMAT_1;

	private static final String attachment_creation_ts_format = DATE_FORMAT_1;

	public BugzillaAttributeMapper(TaskRepository taskRepository) {
		super(taskRepository);
	}

	@Override
	public Date getDateValue(TaskAttribute attribute) {
		if (attribute == null) {
			return null;
		}
		String dateString = attribute.getValue();
		String id = attribute.getId();
		Date parsedDate = getDate(id, dateString);
		if (parsedDate == null) {
			parsedDate = super.getDateValue(attribute);
		}
		return parsedDate;
	}

	@Override
	public boolean getBooleanValue(TaskAttribute attribute) {
		if (attribute.getValue().equals("1")) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public void setBooleanValue(TaskAttribute attribute, Boolean value) {
		if (value == null) {
			attribute.setValue("0");
		} else if (value) {
			attribute.setValue("1");
		} else {
			attribute.setValue("0");
		}
	}

	private Date getDate(String attributeId, String dateString) {
		Date parsedDate = null;
		try {
			if (attributeId.equals(BugzillaAttribute.DELTA_TS.getKey())) {
				parsedDate = new SimpleDateFormat(delta_ts_format).parse(dateString);
			} else if (attributeId.equals(BugzillaAttribute.CREATION_TS.getKey())) {
				parsedDate = new SimpleDateFormat(creation_ts_format).parse(dateString);
			} else if (attributeId.equals(BugzillaAttribute.BUG_WHEN.getKey())) {
				parsedDate = new SimpleDateFormat(comment_creation_ts_format).parse(dateString);
			} else if (attributeId.equals(BugzillaAttribute.DATE.getKey())) {
				parsedDate = new SimpleDateFormat(attachment_creation_ts_format).parse(dateString);
			} else if (attributeId.equals(BugzillaAttribute.DEADLINE.getKey())) {
				parsedDate = new SimpleDateFormat(deadline_format).parse(dateString);
			} else if (attributeId.startsWith(BugzillaCustomField.CUSTOM_FIELD_PREFIX)) {
				parsedDate = new SimpleDateFormat(customAttribute_format).parse(dateString);
			}
		} catch (ParseException e) {
			return null;
		} catch (NumberFormatException e) {
			return null;
		}
		return parsedDate;
	}

	@Override
	public void setDateValue(TaskAttribute attribute, Date date) {
		if (date != null) {
			String dateString = null;
			String attributeId = attribute.getId();

			if (attributeId.equals(BugzillaAttribute.DELTA_TS.getKey())) {
				dateString = new SimpleDateFormat(delta_ts_format).format(date);
			} else if (attributeId.equals(BugzillaAttribute.CREATION_TS.getKey())) {
				dateString = new SimpleDateFormat(creation_ts_format).format(date);
			} else if (attributeId.equals(BugzillaAttribute.BUG_WHEN.getKey())) {
				dateString = new SimpleDateFormat(comment_creation_ts_format).format(date);
			} else if (attributeId.equals(BugzillaAttribute.DATE.getKey())) {
				dateString = new SimpleDateFormat(attachment_creation_ts_format).format(date);
			} else if (attributeId.equals(BugzillaAttribute.DEADLINE.getKey())) {
				dateString = new SimpleDateFormat(deadline_format).format(date);
			} else if (attributeId.startsWith(BugzillaCustomField.CUSTOM_FIELD_PREFIX)) {
				dateString = new SimpleDateFormat(customAttribute_format).format(date);
			}

			if (dateString == null) {
				super.setDateValue(attribute, date);
			} else {
				attribute.setValue(dateString);
			}

		} else {
			attribute.clearValues();
		}
	}

	@Override
	public String mapToRepositoryKey(TaskAttribute parent, String key) {
		/*if (key.equals(TaskAttribute.NEW_CC)) {
			return BugzillaReportElement.NEWCC.getKey();
		} else*/if (key.equals(TaskAttribute.COMMENT_DATE)) {
			return BugzillaAttribute.BUG_WHEN.getKey();
		} else if (key.equals(TaskAttribute.COMMENT_AUTHOR)) {
			return BugzillaAttribute.WHO.getKey();
		} else if (key.equals(TaskAttribute.COMMENT_AUTHOR_NAME)) {
			return BugzillaAttribute.WHO_NAME.getKey();
		} else if (key.equals(TaskAttribute.USER_CC)) {
			return BugzillaAttribute.CC.getKey();
		} else if (key.equals(TaskAttribute.COMMENT_TEXT)) {
			return BugzillaAttribute.THETEXT.getKey();
		} else if (key.equals(TaskAttribute.DATE_CREATION)) {
			return BugzillaAttribute.CREATION_TS.getKey();
		} else if (key.equals(TaskAttribute.DESCRIPTION)) {
			return BugzillaAttribute.LONG_DESC.getKey();
		} else if (key.equals(TaskAttribute.ATTACHMENT_ID)) {
			return BugzillaAttribute.ATTACHID.getKey();
		} else if (key.equals(TaskAttribute.ATTACHMENT_DESCRIPTION)) {
			return BugzillaAttribute.DESC.getKey();
		} else if (key.equals(TaskAttribute.ATTACHMENT_CONTENT_TYPE)) {
			return BugzillaAttribute.CTYPE.getKey();
			//return BugzillaReportElement.TYPE.getKey();*/
		} else if (key.equals(TaskAttribute.USER_ASSIGNED)) {
			return BugzillaAttribute.ASSIGNED_TO.getKey();
		} else if (key.equals(TaskAttribute.USER_ASSIGNED_NAME)) {
			return BugzillaAttribute.ASSIGNED_TO_NAME.getKey();
		} else if (key.equals(TaskAttribute.RESOLUTION)) {
			return BugzillaAttribute.RESOLUTION.getKey();
		} else if (key.equals(TaskAttribute.STATUS)) {
			return BugzillaAttribute.BUG_STATUS.getKey();
		} else if (key.equals(TaskAttribute.DATE_MODIFICATION)) {
			return BugzillaAttribute.DELTA_TS.getKey();
		} else if (key.equals(TaskAttribute.USER_REPORTER)) {
			return BugzillaAttribute.REPORTER.getKey();
		} else if (key.equals(TaskAttribute.USER_REPORTER_NAME)) {
			return BugzillaAttribute.REPORTER_NAME.getKey();
		} else if (key.equals(TaskAttribute.SUMMARY)) {
			return BugzillaAttribute.SHORT_DESC.getKey();
		} else if (key.equals(TaskAttribute.PRODUCT)) {
			return BugzillaAttribute.PRODUCT.getKey();
		} else if (key.equals(TaskAttribute.KEYWORDS)) {
			return BugzillaAttribute.KEYWORDS.getKey();
		} else if (key.equals(TaskAttribute.ATTACHMENT_DATE)) {
			return BugzillaAttribute.DATE.getKey();
		} else if (key.equals(TaskAttribute.ATTACHMENT_SIZE)) {
			return BugzillaAttribute.SIZE.getKey();
		} else if (key.equals(TaskAttribute.ADD_SELF_CC)) {
			return BugzillaAttribute.ADDSELFCC.getKey();
		} else if (key.equals(TaskAttribute.PRIORITY)) {
			return BugzillaAttribute.PRIORITY.getKey();
		} else if (key.equals(TaskAttribute.COMMENT_NEW)) {
			return BugzillaAttribute.NEW_COMMENT.getKey();
		} else if (key.equals(TaskAttribute.COMPONENT)) {
			return BugzillaAttribute.COMPONENT.getKey();
		} else if (key.equals(TaskAttribute.TASK_KEY)) {
			return BugzillaAttribute.BUG_ID.getKey();
		} else if (key.equals(TaskAttribute.DATE_DUE)) {
			return BugzillaAttribute.DEADLINE.getKey();
		}
		return super.mapToRepositoryKey(parent, key);
	}

	@Override
	public TaskAttribute getAssoctiatedAttribute(TaskAttribute taskAttribute) {
		String id = taskAttribute.getMetaData().getValue(TaskAttribute.META_ASSOCIATED_ATTRIBUTE_ID);
		if (id != null) {
			// operation associated input attributes are stored on the root attribute
			if (TaskAttribute.TYPE_OPERATION.equals(taskAttribute.getMetaData().getType())) {
				return taskAttribute.getTaskData().getRoot().getMappedAttribute(id);
			}
			return taskAttribute.getMappedAttribute(id);
		}
		return null;
	}

	@Override
	public IRepositoryPerson getRepositoryPerson(TaskAttribute taskAttribute) {

		IRepositoryPerson person = super.getRepositoryPerson(taskAttribute);
		if (person.getName() == null) {
			if (taskAttribute.getId().equals(BugzillaAttribute.ASSIGNED_TO.getKey())) {
				TaskAttribute attrAssigned = taskAttribute.getTaskData().getRoot().getAttribute(
						BugzillaAttribute.ASSIGNED_TO_NAME.getKey());
				if (attrAssigned != null) {
					person.setName(attrAssigned.getValue());
				}
			} else if (taskAttribute.getId().equals(BugzillaAttribute.REPORTER.getKey())) {
				TaskAttribute attrReporter = taskAttribute.getTaskData().getRoot().getAttribute(
						BugzillaAttribute.REPORTER_NAME.getKey());
				if (attrReporter != null) {
					person.setName(attrReporter.getValue());
				}
			} else if (taskAttribute.getId().equals(BugzillaAttribute.QA_CONTACT.getKey())) {
				TaskAttribute attrReporter = taskAttribute.getTaskData().getRoot().getAttribute(
						BugzillaAttribute.QA_CONTACT_NAME.getKey());
				if (attrReporter != null) {
					person.setName(attrReporter.getValue());
				}
			}
		}
		return person;
	}

	@Override
	public Map<String, String> getOptions(TaskAttribute attribute) {
		RepositoryConfiguration configuration = BugzillaCorePlugin.getRepositoryConfiguration(getTaskRepository().getRepositoryUrl());
		if (configuration != null) {
			TaskAttribute attributeProduct = attribute.getTaskData().getRoot().getMappedAttribute(
					BugzillaAttribute.PRODUCT.getKey());
			if (attributeProduct != null && attributeProduct.getValue().length() > 0) {
				List<String> options = configuration.getAttributeOptions(attributeProduct.getValue(), attribute);
				if (options.size() == 0 && attribute.getId().equals(BugzillaOperation.resolve.getInputId())) {
					options = configuration.getOptionValues(BugzillaAttribute.RESOLUTION, attributeProduct.getValue());
					// DUPLICATE and MOVED have special meanings so do not show as resolution
					// TODO: COPIED FUNCTIONALITY from RepositoryConfiguration.addOperation() refactor.
					if (options != null) {
						options.remove("DUPLICATE");
						options.remove("MOVED");
					}
				}
				Map<String, String> newOptions = new LinkedHashMap<String, String>();
				for (String option : options) {
					newOptions.put(option, option);
				}
				return newOptions;
			}
		}
		return super.getOptions(attribute);
	}

	@Override
	public boolean equals(TaskAttribute newAttribute, TaskAttribute oldAttribute) {
		if (oldAttribute.getId().startsWith(TaskAttribute.PREFIX_ATTACHMENT)) {
			TaskAttachmentMapper oldAttachment;
			oldAttachment = TaskAttachmentMapper.createFrom(oldAttribute);
			TaskAttachmentMapper newAttachment;
			newAttachment = TaskAttachmentMapper.createFrom(newAttribute);
			return newAttachment.equals(oldAttachment);
		}
		return super.equals(newAttribute, oldAttribute);
	}

	@Override
	public String getLabel(TaskAttribute taskAttribute) {
		if (taskAttribute.getId().startsWith(BugzillaCustomField.CUSTOM_FIELD_PREFIX)) {
			return super.getLabel(taskAttribute) + ":";
		} else {
			return super.getLabel(taskAttribute);
		}
	}

}

Back to the top