Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 545e2b3fc2ccba56bacba36338ae85500de44b02 (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
/*******************************************************************************
 * Copyright (c) 2017 Red Hat Inc. 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:
 *     Red Hat - Initial Contribution
 *******************************************************************************/
package org.eclipse.linuxtools.internal.mylyn.osio.rest.ui.provisional;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map.Entry;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.linuxtools.internal.mylyn.osio.rest.core.OSIORestConfiguration;
import org.eclipse.linuxtools.internal.mylyn.osio.rest.core.OSIORestConnector;
import org.eclipse.linuxtools.internal.mylyn.osio.rest.ui.OSIORestUIPlugin;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.tasks.core.IRepositoryQuery;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.eclipse.mylyn.tasks.ui.editors.AbstractAttributeEditor;
import org.eclipse.osgi.util.NLS;

public class OSIORestSearchQueryPage extends RepositoryQuerySchemaPage {
	
	public OSIORestSearchQueryPage(String pageName, TaskRepository repository, IRepositoryQuery query,
			AbstractQueryPageSchema schema, TaskData data, QueryPageDetails pageDetails) {
		super(pageName, repository, query, schema, data, pageDetails);
		if (query != null) {
			setTitle(NLS.bind(Messages.OSIORestSearchQueryPage_PropertiesForQuery, query.getSummary()));
		} else {
			setTitle(Messages.OSIORestSearchQueryPage_PropertiesForNewQuery);
		}
	}

	@Override
	protected void doRefreshControls() {
		try {
			OSIORestConnector connectorREST = (OSIORestConnector) getConnector();
			connectorREST.getRepositoryConfiguration(getTaskRepository()).updateSpaceOptions(getTargetTaskData());

			for (Entry<String, AbstractAttributeEditor> entry : editorMap.entrySet()) {
				entry.getValue().refresh();
			}
		} catch (CoreException e) {
			StatusHandler.log(new Status(IStatus.ERROR, OSIORestUIPlugin.PLUGIN_ID,
					"OSIORestSearchQueryPage could not refresh!", e)); //$NON-NLS-1$
		}
	}

	@Override
	protected boolean hasRepositoryConfiguration() {
		return getRepositoryConfiguration() != null;
	}

	private OSIORestConfiguration getRepositoryConfiguration() {
		try {
			return ((OSIORestConnector) getConnector()).getRepositoryConfiguration(getTaskRepository());
		} catch (CoreException e) {
			StatusHandler.log(new Status(IStatus.ERROR, OSIORestUIPlugin.PLUGIN_ID,
					"OSIORestSearchQueryPage could get the RepositoryConfiguration!", e)); //$NON-NLS-1$
		}
		return null;
	}

	@Override
	protected boolean restoreState(IRepositoryQuery query) {
		if (query != null) {
			try {
				restoreStateFromUrl(query.getUrl());
				doRefreshControls();
				return true;
			} catch (UnsupportedEncodingException e) {
				// ignore
			}
		}
		return false;
	}

	private void restoreStateFromUrl(String queryUrl) throws UnsupportedEncodingException {
		queryUrl = queryUrl.substring(queryUrl.indexOf("?") + 1); //$NON-NLS-1$
		String[] options = queryUrl.split("&"); //$NON-NLS-1$
		for (String option : options) {
			String key;
			int endindex = option.indexOf("="); //$NON-NLS-1$
			if (endindex == -1) {
				key = null;
			} else {
				key = option.substring(0, option.indexOf("=")); //$NON-NLS-1$
			}
			if (key == null || key.equals("order")) { //$NON-NLS-1$
				continue;
			}
			String value = URLDecoder.decode(option.substring(option.indexOf("=") + 1), //$NON-NLS-1$
					getTaskRepository().getCharacterEncoding());
			TaskAttribute attr = getTargetTaskData().getRoot().getAttribute(key);
			if (attr != null) {
				if (getTargetTaskData().getRoot().getAttribute(key).getValue().equals("")) { //$NON-NLS-1$
					getTargetTaskData().getRoot().getAttribute(key).setValue(value);
				} else {
					getTargetTaskData().getRoot().getAttribute(key).addValue(value);
				}
			}
		}
	}

	@Override
	protected String getQueryUrl(String repositoryUrl) {
		return getQueryURL(repositoryUrl, getQueryParameters());
	}

	private StringBuilder getQueryParameters() {
		StringBuilder sb = new StringBuilder();
		for (Entry<String, AbstractAttributeEditor> entry : editorMap.entrySet()) {
			TaskAttribute attrib = getTargetTaskData().getRoot().getAttribute(entry.getKey());
			for (String string : attrib.getValues()) {
				if (string != null && !string.equals("")) { //$NON-NLS-1$
					try {
						appendToBuffer(sb, entry.getKey() + "=", //$NON-NLS-1$
								URLEncoder.encode(string.replaceAll(" ", "%20"), //$NON-NLS-1$//$NON-NLS-2$
										getTaskRepository().getCharacterEncoding()).replaceAll("%2520", "%20")); //$NON-NLS-1$ //$NON-NLS-2$
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		return sb;
	}

	private String getQueryURL(String repositoryUrl, StringBuilder params) {
		StringBuilder url = new StringBuilder(getQueryURLStart(repositoryUrl));
		url.append(params);
		return url.toString();
	}

	/**
	 * Creates the bugzilla query URL start. Example: https://bugs.eclipse.org/bugs/buglist.cgi?
	 */
	private String getQueryURLStart(String repositoryUrl) {
		return repositoryUrl + (repositoryUrl.endsWith("/") ? "" : "/") + "/search?"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$//$NON-NLS-4$
	}

	private void appendToBuffer(StringBuilder sb, String key, String value) {
		if (sb.length() > 0) {
			sb.append('&');
		}
		sb.append(key);
		sb.append(value);
	}

}

Back to the top