Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1357a1df3c48412d0164a9759242382a6033ee36 (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
/*******************************************************************************
 * Copyright (c) 2015 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.util.Map;

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.Product;
import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.SortableActiveEntry;
import org.eclipse.mylyn.tasks.core.RepositoryStatus;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;

public class BugzillaRestTaskAttributeMapper extends TaskAttributeMapper {

	private final BugzillaRestConnector connector;

	public BugzillaRestTaskAttributeMapper(TaskRepository taskRepository, BugzillaRestConnector connector) {
		super(taskRepository);
		this.connector = connector;
	}

	@Override
	public Map<String, String> getOptions(@NonNull TaskAttribute attribute) {
		if (attribute.getId().equals(BugzillaRestCreateTaskSchema.getDefault().TARGET_MILESTONE.getKey())
				|| attribute.getId().equals(BugzillaRestCreateTaskSchema.getDefault().VERSION.getKey())
				|| attribute.getId().equals(BugzillaRestCreateTaskSchema.getDefault().COMPONENT.getKey())) {
			TaskAttribute productAttribute = attribute.getParentAttribute()
					.getAttribute(BugzillaRestCreateTaskSchema.getDefault().PRODUCT.getKey());
			BugzillaRestConfiguration repositoryConfiguration;
			try {
				repositoryConfiguration = connector.getRepositoryConfiguration(this.getTaskRepository());
				// TODO: change this when we have offline cache for the repository configuration so we build the options in an temp var
				if (repositoryConfiguration != null) {
					if (!productAttribute.getValue().equals("")) { //$NON-NLS-1$
						boolean found = false;
						attribute.clearOptions();
						for (String productName : productAttribute.getValues()) {
							Product actualProduct = repositoryConfiguration.getProductWithName(productName);
							if (attribute.getId()
									.equals(BugzillaRestCreateTaskSchema.getDefault().COMPONENT.getKey())) {
								internalSetAttributeOptions4Product(attribute, actualProduct.getComponents());

							} else if (attribute.getId()
									.equals(BugzillaRestCreateTaskSchema.getDefault().TARGET_MILESTONE.getKey())) {
								internalSetAttributeOptions4Product(attribute, actualProduct.getMilestones());
							} else if (attribute.getId()
									.equals(BugzillaRestCreateTaskSchema.getDefault().VERSION.getKey())) {
								internalSetAttributeOptions4Product(attribute, actualProduct.getVersions());
							}
						}
					}
				}
			} catch (CoreException e) {
				StatusHandler.log(new RepositoryStatus(getTaskRepository(), IStatus.ERROR, BugzillaRestCore.ID_PLUGIN,
						0, "Failed to obtain repository configuration", e)); //$NON-NLS-1$
			}
		}
		return super.getOptions(attribute);
	}

	private void internalSetAttributeOptions4Product(TaskAttribute taskAttribute,
			SortableActiveEntry[] actualProductEntry) {
		boolean found = false;
		String actualValue = taskAttribute.getValue();
		for (SortableActiveEntry SortableActiveEntry : actualProductEntry) {
			if (SortableActiveEntry.isActive()) {
				// TODO: remove when we have offline cache for the repository configuration
				taskAttribute.putOption(SortableActiveEntry.getName(), SortableActiveEntry.getName());
				found |= actualValue.equals(SortableActiveEntry.getName());
			}
		}
		if (!found) {
			taskAttribute.setValue(""); //$NON-NLS-1$
		}
	}

	@Override
	public String mapToRepositoryKey(@NonNull TaskAttribute parent, @NonNull String key) {
		if (key.equals(TaskAttribute.TASK_KEY)) {
			return BugzillaRestTaskSchema.getDefault().BUG_ID.getKey();
		} else {
			return super.mapToRepositoryKey(parent, key);
		}
	}

	public void updateNewAttachmentAttribute(TaskAttribute attachmentAttribute) {
		BugzillaRestConfiguration repositoryConfiguration;
		try {
			repositoryConfiguration = connector.getRepositoryConfiguration(this.getTaskRepository());
			repositoryConfiguration.updateAttachmentFlags(attachmentAttribute);
		} catch (CoreException e) {
			StatusHandler.log(
					new Status(IStatus.ERROR, BugzillaRestCore.ID_PLUGIN, "Eerror in updateNewAttachmentAttribute", e)); //$NON-NLS-1$
		}

	}

}

Back to the top