Skip to main content
summaryrefslogtreecommitdiffstats
blob: 85156437b4a10d396dce34b6ab02a559a2825b33 (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
/*******************************************************************************
 * Copyright (c) 2013 Frank Becker 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:
 *     Frank Becker - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.bugzilla.rest.core;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.Component;
import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.Field;
import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.FieldValues;
import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.FlagType;
import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.FlagTypes;
import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.Parameters;
import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.Product;
import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.SortableActiveEntry;
import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.StatusTransition;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.eclipse.mylyn.tasks.core.data.TaskOperation;

import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Ordering;

public class BugzillaRestConfiguration implements Serializable {

	private static final BugzillaRestCreateTaskSchema SCHEMA = BugzillaRestCreateTaskSchema.getDefault();

	private static final long serialVersionUID = 4173223872076958202L;

	private final String repositoryId;

	private Map<String, Field> fields;

	private Map<String, Product> products;

	private Parameters parameters;

	public BugzillaRestConfiguration(String repositoryId) {
		this.repositoryId = repositoryId;
	}

	public String getRepositoryId() {
		return repositoryId;
	}

	void setFields(Map<String, Field> fields) {
		Function<Field, String> getName = new Function<Field, String>() {
			public String apply(Field item) {
				return item.getName();
			}
		};
		Function<String, String> comparatorFunction = Functions.compose(getName, Functions.forMap(fields));
		Ordering<String> comparator = Ordering.natural().onResultOf(comparatorFunction);
		this.fields = ImmutableSortedMap.copyOf(fields, comparator);
	}

	public Map<String, Field> getFields() {
		return fields;
	}

	public Field getFieldWithName(String fieldName) {
		return fields.get(fieldName);
	}

	void setProducts(Map<String, Product> products) {
		Function<Product, String> getName = new Function<Product, String>() {
			public String apply(Product item) {
				return item.getName();
			}
		};
		Function<String, String> comparatorFunction = Functions.compose(getName, Functions.forMap(products));
		Ordering<String> comparator = Ordering.natural().onResultOf(comparatorFunction);
		this.products = ImmutableSortedMap.copyOf(products, comparator);
	}

	public Map<String, Product> getProducts() {
		return products;
	}

	public Product getProductWithName(String productName) {
		return products.get(productName);
	}

	void setParameters(Parameters parameters) {
		this.parameters = parameters;
	}

	public Parameters getParameters() {
		return parameters;
	}

	private Component getProductComponentWithName(@NonNull Product product, String name) {
		return product.getComponentWithName(name);
	}

	public void updateInitialTaskData(TaskData data) throws CoreException {
		setProductOptions(data);
		updateProductOptions(data);
		for (String key : data.getRoot().getAttributes().keySet()) {
			if (key.equals(BugzillaRestTaskSchema.getDefault().ADD_SELF_CC.getKey())
					|| key.equals(BugzillaRestTaskSchema.getDefault().NEW_COMMENT.getKey())
					|| key.equals(BugzillaRestTaskSchema.getDefault().DUPE_OF.getKey())
					|| key.equals(TaskAttribute.OPERATION)
					|| key.equals(BugzillaRestTaskSchema.getDefault().DATE_MODIFICATION.getKey())
					|| key.equals(BugzillaRestTaskSchema.getDefault().RESET_ASSIGNED_TO.getKey())) {
				continue;
			}
			TaskAttribute attribute = data.getRoot().getAttribute(key);
			if (!key.equals(SCHEMA.PRODUCT.getKey())) {
				String configName = mapTaskAttributeKey2ConfigurationFields(key);
				if ("addCC".equals(configName) || "removeCC".equals(configName)
						|| "reset_qa_contact".equals(configName)) {
					continue;
				}
				Field configField = getFieldWithName(configName);
				if (configField == null) {
					continue;
				}
				if (configName.equals("component") || configName.equals("version")
						|| configName.equals("target_milestone")) {
					if (attribute.getOptions().size() == 1 && attribute.getValue().isEmpty()) {
						attribute.setValue((String) attribute.getOptions().values().toArray()[0]);
					}
				} else {
					FieldValues[] val = configField.getValues();
					if (val != null && val.length > 0) {
						for (FieldValues fieldValues : val) {
							if (configName.equals("bug_status")) {
								if (fieldValues.getName() == null) {
									for (StatusTransition bugzillaRestBugStatusTransition : fieldValues
											.getCanChangeTo()) {
										attribute.putOption(bugzillaRestBugStatusTransition.getName(),
												bugzillaRestBugStatusTransition.getName());
									}
								}
							} else {
								attribute.putOption(fieldValues.getName(), fieldValues.getName());
							}

						}
					}
				}
			}
			if (attribute.getValue().isEmpty()) {
				String newValue = getValueFromParameter(key);
				if (newValue != null) {
					attribute.setValue(getValueFromParameter(key));
				}
			}

		}
		for (Field Field : fields.values()) {
			if (Field.isCustom() && Field.isOnBugEntry()) {
				TaskAttribute attribute = data.getRoot().createAttribute(Field.getName());
				if (attribute != null) {
					attribute.getMetaData().defaults().setLabel(Field.getDisplayName());
					attribute.getMetaData().setKind(TaskAttribute.KIND_DEFAULT);
					String type = getAttributeTypFromFieldTyp(Field.getType());
					attribute.getMetaData().setType(type);
					if (type.equals(TaskAttribute.TYPE_SINGLE_SELECT)) {
						attribute.getMetaData().setRequired(true);
					}

					FieldValues[] values1 = Field.getValues();
					if (values1 != null) {
						for (FieldValues FieldValues : values1) {
							attribute.putOption(FieldValues.getName(), FieldValues.getName());
						}
					}
					attribute.getMetaData().setReadOnly(false);
				}
			}
		}
	}

	private String getValueFromParameter(String attributeId) {
		if (attributeId.equals(TaskAttribute.PRIORITY)) {
			return getParameters().getDefaultpriority();
		} else if (attributeId.equals(TaskAttribute.SEVERITY)) {
			return getParameters().getDefaultseverity();
		} else if (attributeId.equals("platform")) {
			if (getParameters().getDefaultplatform() == null || getParameters().getDefaultplatform().isEmpty()) {
				return "All";
			} else {
				return getParameters().getDefaultplatform();
			}
		} else if (attributeId.equals("os")) {
			if (getParameters().getDefaultopsys() == null || getParameters().getDefaultopsys().isEmpty()) {
				return "All";
			} else {
				return getParameters().getDefaultopsys();
			}
		}
		return "";
	}

	private String getAttributeTypFromFieldTyp(int fieldTyp) throws CoreException {
		switch (fieldTyp) {
		case 1://Free Text
			return TaskAttribute.TYPE_SHORT_TEXT;
		case 2: //DropDown
			return TaskAttribute.TYPE_SINGLE_SELECT;
		case 3: //Multiple-Selection Box
			return TaskAttribute.TYPE_MULTI_SELECT;
		case 4: //Large Text Box
			return TaskAttribute.TYPE_LONG_TEXT;
		case 5: //Date/Time
			return TaskAttribute.TYPE_DATETIME;
		case 6: //Bug Id
			return TaskAttribute.TYPE_INTEGER;
		case 7: //Bug URLs
			return TaskAttribute.TYPE_URL;
		default:
			Status status = new Status(IStatus.INFO, BugzillaRestCore.ID_PLUGIN,
					"unknown custom field type " + fieldTyp);
			StatusHandler.log(status);
			throw new CoreException(status);
		}
	}

	private String mapTaskAttributeKey2ConfigurationFields(String taskAttributeKey) {
		String resultString;
		if (taskAttributeKey.equals("task.common.summary")) {
			resultString = "short_desc";
		} else if (taskAttributeKey.equals(TaskAttribute.PRODUCT) //
				|| taskAttributeKey.equals(TaskAttribute.RESOLUTION) //
				|| taskAttributeKey.equals(TaskAttribute.PRIORITY) //
				|| taskAttributeKey.equals(TaskAttribute.COMPONENT) //
				|| taskAttributeKey.equals(TaskAttribute.VERSION)) {
			resultString = taskAttributeKey.substring(12);
		} else if (taskAttributeKey.equals(TaskAttribute.STATUS)) {
			resultString = "bug_status";
		} else if (taskAttributeKey.equals(TaskAttribute.USER_ASSIGNED)) {
			resultString = "assigned_to";
		} else if (taskAttributeKey.equals(TaskAttribute.USER_CC)) {
			resultString = "cc";
		} else if (taskAttributeKey.equals(TaskAttribute.DESCRIPTION)) {
			resultString = "longdesc";
		} else if (taskAttributeKey.equals("description_is_private")) {
			resultString = "longdescs.isprivate";
		} else if (taskAttributeKey.equals("os")) {
			resultString = "op_sys";
		} else if (taskAttributeKey.equals("platform")) {
			resultString = "rep_platform";
		} else if (taskAttributeKey.equals(TaskAttribute.SEVERITY)) {
			resultString = "bug_severity";
		} else if (taskAttributeKey.equals("comment")) {
			resultString = "longdesc";
		} else if (taskAttributeKey.equals("blocks")) {
			resultString = "blocked";
		} else if (taskAttributeKey.equals("depends_on")) {
			resultString = "dependson";
		} else {

			resultString = taskAttributeKey;
		}
		return resultString;
	}

	private void setAttributeOptionsForProduct(TaskAttribute taskAttribute, Product actualProduct) {
		taskAttribute.clearOptions();
		if (taskAttribute.getId().equals(SCHEMA.TARGET_MILESTONE.getKey())) {
			internalSetAttributeOptions(taskAttribute, actualProduct.getMilestones());
		} else if (taskAttribute.getId().equals(SCHEMA.VERSION.getKey())) {
			internalSetAttributeOptions(taskAttribute, actualProduct.getVersions());
		} else if (taskAttribute.getId().equals(SCHEMA.COMPONENT.getKey())) {
			internalSetAttributeOptions(taskAttribute, actualProduct.getComponents());
		}
	}

	private void internalSetAttributeOptions(TaskAttribute taskAttribute, SortableActiveEntry[] actualProductEntry) {
		boolean found = false;
		String actualValue = taskAttribute.getValue();
		for (SortableActiveEntry SortableActiveEntry : actualProductEntry) {
			if (SortableActiveEntry.isActive()) {
				taskAttribute.putOption(SortableActiveEntry.getName(), SortableActiveEntry.getName());
				if (!found) {
					found = actualValue.equals(SortableActiveEntry.getName());
				}
			}
		}
		if (!found) {
			taskAttribute.setValue(""); //$NON-NLS-1$
		}
	}

	public boolean setProductOptions(@NonNull TaskData taskData) {
		TaskAttribute attributeProduct = taskData.getRoot().getMappedAttribute(SCHEMA.PRODUCT.getKey());
		if (attributeProduct != null) {
			SortedSet<String> products = new TreeSet<String>();
			Field configFieldComponent = getFieldWithName("component"); //$NON-NLS-1$
			FieldValues[] val = configFieldComponent.getValues();
			if (val != null && val.length > 0) {
				for (FieldValues fieldValues : val) {
					for (String visibilityValue : fieldValues.getVisibilityValues()) {
						if (!products.contains(visibilityValue)) {
							products.add(visibilityValue);
						}
					}
				}
				attributeProduct.clearOptions();
				for (String productName : products) {
					attributeProduct.putOption(productName, productName);
				}
			}

			return true;
		}
		return false;
	}

	public boolean updateProductOptions(@NonNull TaskData taskData) {
		if (taskData == null) {
			return false;
		}
		TaskAttribute attributeProduct = taskData.getRoot().getMappedAttribute(SCHEMA.PRODUCT.getKey());
		if (attributeProduct == null) {
			return false;
		}
		if (!attributeProduct.getValue().isEmpty()) {
			Product actualProduct = getProductWithName(attributeProduct.getValue());

			TaskAttribute attributeComponent = taskData.getRoot().getMappedAttribute(SCHEMA.COMPONENT.getKey());
			if (attributeComponent != null) {
				setAttributeOptionsForProduct(attributeComponent, actualProduct);
			}
			TaskAttribute attributeVersion = taskData.getRoot().getMappedAttribute(SCHEMA.VERSION.getKey());
			if (attributeVersion != null) {
				setAttributeOptionsForProduct(attributeVersion, actualProduct);
			}
			TaskAttribute attributeTargetMilestone = taskData.getRoot()
					.getMappedAttribute(SCHEMA.TARGET_MILESTONE.getKey());
			if (attributeTargetMilestone != null) {
				setAttributeOptionsForProduct(attributeTargetMilestone, actualProduct);
			}
		} else {
			for (Product product : getProducts().values()) {
				attributeProduct.putOption(product.getName(), product.getName());
			}
			TaskAttribute attributeComponent = taskData.getRoot().getMappedAttribute(SCHEMA.COMPONENT.getKey());
			if (attributeComponent != null) {
				setAllAttributeOptions(attributeComponent, getFieldWithName("component")); //$NON-NLS-1$
			}
			TaskAttribute attributeVersion = taskData.getRoot().getMappedAttribute(SCHEMA.VERSION.getKey());
			if (attributeVersion != null) {
				setAllAttributeOptions(attributeVersion, getFieldWithName("version")); //$NON-NLS-1$
			}
			TaskAttribute attributeTargetMilestone = taskData.getRoot()
					.getMappedAttribute(SCHEMA.TARGET_MILESTONE.getKey());
			if (attributeTargetMilestone != null) {
				setAllAttributeOptions(attributeTargetMilestone, getFieldWithName("target_milestone")); //$NON-NLS-1$
			}
		}
		return true;
	}

	private void setAllAttributeOptions(TaskAttribute updateAttribute, Field configField) {
		FieldValues[] val = configField.getValues();
		if (val != null && val.length > 0) {
			for (FieldValues fieldValues : val) {
				updateAttribute.putOption(fieldValues.getName(), fieldValues.getName());
			}
		}
	}

	public void addValidOperations(TaskData bugReport) {
		TaskAttribute attributeStatus = bugReport.getRoot().getMappedAttribute(TaskAttribute.STATUS);
		String attributeStatusValue = attributeStatus.getValue();
		TaskAttribute operationAttribute = bugReport.getRoot().getAttribute(TaskAttribute.OPERATION);
		if (operationAttribute == null) {
			operationAttribute = bugReport.getRoot().createAttribute(TaskAttribute.OPERATION);
		}
		TaskAttribute attribute = bugReport.getRoot()
				.createAttribute(TaskAttribute.PREFIX_OPERATION + attributeStatusValue);
		if (attributeStatusValue.equals("RESOLVED")) {
			attribute.getMetaData().putValue(TaskAttribute.META_ASSOCIATED_ATTRIBUTE_ID,
					BugzillaRestTaskSchema.getDefault().RESOLUTION.getKey());
		}

		TaskOperation.applyTo(attribute, attributeStatusValue, attributeStatusValue);
		// set as default
		TaskOperation.applyTo(operationAttribute, attributeStatusValue, attributeStatusValue);
		Field status = getFieldWithName("bug_status");
		for (FieldValues fieldValues : status.getValues()) {
			if (((attributeStatusValue == null || attributeStatusValue.isEmpty()) && fieldValues.getName() == null)
					|| (attributeStatusValue != null && attributeStatusValue.equals(fieldValues.getName()))) {
				for (StatusTransition statusTransition : fieldValues.getCanChangeTo()) {
					attribute = bugReport.getRoot()
							.createAttribute(TaskAttribute.PREFIX_OPERATION + statusTransition.name);
					TaskOperation.applyTo(attribute, statusTransition.name, statusTransition.name);
					if (statusTransition.name != null && statusTransition.name.equals("RESOLVED")) {
						TaskAttribute attrResolvedInput = attribute.getTaskData()
								.getRoot()
								.createAttribute("resolutionInput");
						attrResolvedInput.getMetaData().setType(TaskAttribute.TYPE_SINGLE_SELECT);
						attribute.getMetaData().putValue(TaskAttribute.META_ASSOCIATED_ATTRIBUTE_ID, "resolutionInput");
						Field resolution = getFieldWithName("resolution");
						for (FieldValues resolutionValues : resolution.getValues()) {
							if (resolutionValues.getName().compareTo("DUPLICATE") != 0) {
								attrResolvedInput.putOption(resolutionValues.getName(), resolutionValues.getName());
							}
						}
					}
				}
			}
		}
		attribute = bugReport.getRoot().createAttribute(TaskAttribute.PREFIX_OPERATION + "duplicate");
		TaskOperation.applyTo(attribute, "duplicate", "Mark as Duplicate");
		TaskAttribute attrResolvedInput = attribute.getTaskData()
				.getRoot()
				.getAttribute(BugzillaRestTaskSchema.getDefault().DUPE_OF.getKey());
		if (attrResolvedInput == null) {
			attrResolvedInput = attribute.getTaskData()
					.getRoot()
					.createAttribute(BugzillaRestTaskSchema.getDefault().DUPE_OF.getKey());
		}
		attrResolvedInput.getMetaData().setType(TaskAttribute.TYPE_TASK_DEPENDENCY);
		attribute.getMetaData().putValue(TaskAttribute.META_ASSOCIATED_ATTRIBUTE_ID, attrResolvedInput.getId());
	}

	public boolean updateFlags(@NonNull TaskData taskData) {
		List<String> existingFlags = new ArrayList<String>();
		TaskAttribute attributeProduct = taskData.getRoot().getMappedAttribute(SCHEMA.PRODUCT.getKey());
		TaskAttribute attributeComponent = taskData.getRoot().getMappedAttribute(SCHEMA.COMPONENT.getKey());
		Product actualProduct = getProductWithName(attributeProduct.getValue());
		Component actualComponent = getProductComponentWithName(actualProduct, attributeComponent.getValue());
		FlagTypes flagTypes = actualComponent.getFlagTypes();

		for (TaskAttribute attribute : taskData.getRoot().getAttributes().values()) {
			if (attribute.getId().startsWith(TaskAttribute.PREFIX_ATTACHMENT)
					|| attribute.getId().equals(TaskAttribute.NEW_ATTACHMENT)) {
				List<String> existingAttachmentFlags = new ArrayList<String>();
				for (TaskAttribute attachmentAttribute : attribute.getAttributes().values()) {
					updateFlag(flagTypes.getAttachment(), existingAttachmentFlags, attachmentAttribute);
				}
				addMissingFlagsInternal(attribute, flagTypes.getAttachment(), existingAttachmentFlags);
			} else {
				updateFlag(flagTypes.getBug(), existingFlags, attribute);
			}
		}
		addMissingFlagsInternal(taskData.getRoot(), flagTypes.getBug(), existingFlags);

		return false;
	}

	private void updateFlag(FlagType[] flagTypes, List<String> existingAttachmentFlags, TaskAttribute flagAttribute) {
		if (flagAttribute.getId().startsWith(IBugzillaRestConstants.KIND_FLAG)) {
			TaskAttribute stateAttribute = flagAttribute.getAttribute("state"); //$NON-NLS-1$))
			stateAttribute.putOption("", ""); //$NON-NLS-1$ //$NON-NLS-2$
			String flagName = stateAttribute.getMetaData().getLabel();
			if (!existingAttachmentFlags.contains(flagName)) {
				existingAttachmentFlags.add(flagName);
			}
			for (FlagType flagType : flagTypes) {
				if (flagType.getName().equals(flagName)) {
					if (flagType.isRequestable()) {
						stateAttribute.putOption("?", "?"); //$NON-NLS-1$ //$NON-NLS-2$
					}
					updateRequestee(flagAttribute, flagType);
					break;
				}
			}
			stateAttribute.putOption("-", "-"); //$NON-NLS-1$ //$NON-NLS-2$
			stateAttribute.putOption("+", "+"); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}

	public void addMissingFlags(TaskData taskData) {
		List<String> existingFlags = new ArrayList<String>();
		TaskAttribute attributeProduct = taskData.getRoot().getMappedAttribute(SCHEMA.PRODUCT.getKey());
		TaskAttribute attributeComponent = taskData.getRoot().getMappedAttribute(SCHEMA.COMPONENT.getKey());
		if (attributeProduct.getValue().equals("") || attributeComponent.getValue().equals("")) { //$NON-NLS-1$ //$NON-NLS-2$
			return;
		}
		Product actualProduct = getProductWithName(attributeProduct.getValue());
		Component actualComponent = getProductComponentWithName(actualProduct, attributeComponent.getValue());
		FlagTypes flagTypes = actualComponent.getFlagTypes();
		addMissingFlagsInternal(taskData.getRoot(), flagTypes.getBug(), existingFlags);
	}

	private void addMissingFlagsInternal(TaskAttribute rootTaskAttribute, FlagType[] flagTypes,
			List<String> existingFlags) {
		for (FlagType flagType : flagTypes) {
			if (existingFlags.contains(flagType.getName()) && !flagType.isMultiplicable()) {
				continue;
			}
			BugzillaRestFlagMapper mapper = new BugzillaRestFlagMapper();
			mapper.setRequestee(""); //$NON-NLS-1$
			mapper.setSetter(""); //$NON-NLS-1$
			mapper.setState(" "); //$NON-NLS-1$
			mapper.setName(flagType.getName());
			mapper.setNumber(0);
			mapper.setDescription(flagType.getDescription());
			mapper.setTypeId(flagType.getId());
			TaskAttribute attribute = rootTaskAttribute
					.createAttribute(IBugzillaRestConstants.KIND_FLAG_TYPE + flagType.getId());
			mapper.applyTo(attribute);
			TaskAttribute stateAttribute = attribute.getAttribute("state"); //$NON-NLS-1$))
			stateAttribute.putOption("", ""); //$NON-NLS-1$ //$NON-NLS-2$
			if (flagType.isRequestable()) {
				stateAttribute.putOption("?", "?"); //$NON-NLS-1$ //$NON-NLS-2$
			}
			updateRequestee(attribute, flagType);
			attribute.getMetaData().putValue(TaskAttribute.META_DESCRIPTION, flagType.getDescription());
			stateAttribute.putOption("-", "-"); //$NON-NLS-1$ //$NON-NLS-2$
			stateAttribute.putOption("+", "+"); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}

	public void updateRequestee(TaskAttribute attribute, FlagType flagType) {
		TaskAttribute requestee = attribute.getAttribute("requestee"); //$NON-NLS-1$
		if (requestee == null) {
			requestee = attribute.createMappedAttribute("requestee"); //$NON-NLS-1$
			requestee.getMetaData().defaults().setType(TaskAttribute.TYPE_PERSON);
			requestee.setValue(""); //$NON-NLS-1$
		}
		requestee.getMetaData().setReadOnly(!flagType.isRequesteeble());
	}

	public void updateAttachmentFlags(@NonNull TaskAttribute attribute) {
		TaskAttribute attributeProduct = attribute.getParentAttribute().getMappedAttribute(SCHEMA.PRODUCT.getKey());
		TaskAttribute attributeComponent = attribute.getParentAttribute().getMappedAttribute(SCHEMA.COMPONENT.getKey());
		Product actualProduct = getProductWithName(attributeProduct.getValue());
		Component actualComponent = getProductComponentWithName(actualProduct, attributeComponent.getValue());
		FlagTypes flagTypes = actualComponent.getFlagTypes();

		List<String> existingAttachmentFlags = new ArrayList<String>();
		for (TaskAttribute attachmentAttribute : attribute.getAttributes().values()) {
			updateFlag(flagTypes.getAttachment(), existingAttachmentFlags, attachmentAttribute);
		}
		addMissingFlagsInternal(attribute, flagTypes.getAttachment(), existingAttachmentFlags);
	}

}

Back to the top