Subversion Repositories XServices

Rev

Rev 52 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 brianR 1
/*
2
 *   Mylyn Connector for Serena Business Mashups
3
 * 	 Copyright 2010 Brian Rosenberger (Brutex Network)
4
 *
5
 *   Licensed under the Apache License, Version 2.0 (the "License");
6
 *   you may not use this file except in compliance with the License.
7
 *   You may obtain a copy of the License at
8
 *
9
 *       http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 *   Unless required by applicable law or agreed to in writing, software
12
 *   distributed under the License is distributed on an "AS IS" BASIS,
13
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 *   See the License for the specific language governing permissions and
15
 *   limitations under the License.
16
 *
17
 *   Serena, TeamTrack and Serena Business Mashup are
18
 * 	 registered trademarks of SERENA Software Inc.
19
 */
20
package net.brutex.mylyn.sbmconnector.core;
21
 
22
import java.util.Date;
23
import java.util.HashMap;
24
import java.util.List;
25
import java.util.Map;
26
 
27
import net.brutex.mylyn.sbmconnector.SBMConnectorPlugin;
28
import net.brutex.mylyn.sbmconnector.core.model.SBMStaticFields;
52 brianR 29
import net.brutex.mylyn.sbmconnector.ui.SBMQueryComposite;
39 brianR 30
import net.brutex.mylyn.sbmconnector.ui.SBMRepositoryQueryPage;
31
import net.brutex.mylyn.sbmconnector.ui.SBMRepositorySettingsPage;
30 brianR 32
import net.brutex.sbm.wsclient.TTItem;
33
 
34
import org.eclipse.core.runtime.CoreException;
35
import org.eclipse.core.runtime.IProgressMonitor;
36
import org.eclipse.core.runtime.IStatus;
37
import org.eclipse.core.runtime.Status;
38
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
39
import org.eclipse.mylyn.tasks.core.IRepositoryQuery;
40
import org.eclipse.mylyn.tasks.core.ITask;
41
import org.eclipse.mylyn.tasks.core.TaskRepository;
42
import org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler;
43
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
44
import org.eclipse.mylyn.tasks.core.data.TaskData;
45
import org.eclipse.mylyn.tasks.core.data.TaskDataCollector;
46
import org.eclipse.mylyn.tasks.core.data.TaskMapper;
47
import org.eclipse.mylyn.tasks.core.sync.ISynchronizationSession;
48
 
49
public class SBMRepositoryConnector extends AbstractRepositoryConnector {
50
 
51
	private SBMTicketDataHandler datahandler;
52
	private static Map<TaskRepository, SBMClient> clients = new HashMap<TaskRepository, SBMClient>();
53
 
54
	public SBMRepositoryConnector() {
55
		this.datahandler = new SBMTicketDataHandler(this);
56
	}
57
 
58
	public static synchronized SBMClient getClient(TaskRepository repository) {
59
		SBMClient client = clients.get(repository);
60
		if (client == null) {
61
			client = new SBMClient(repository);
62
			clients.put(repository, client);
63
		}
64
		return client;
65
	}
66
 
67
	@Override
68
	public boolean canCreateNewTask(TaskRepository repository) {
69
		return false;
70
	}
71
 
72
	@Override
73
	public boolean canCreateTaskFromKey(TaskRepository repository) {
74
		return false;
75
	}
76
 
77
	@Override
78
	public boolean canSynchronizeTask(TaskRepository taskRepository, ITask task) {
79
		return true;
80
	}
39 brianR 81
 
30 brianR 82
	@Override
83
	public String getConnectorKind() {
84
		return SBMConnectorPlugin.CONNECTOR_KIND;
85
	}
86
 
87
	@Override
88
	public String getLabel() {
89
		return SBMConnectorPlugin.LABEL;
90
	}
91
 
92
	@Override
93
	public String getRepositoryUrlFromTaskUrl(String taskFullUrl) {
94
		// TODO Auto-generated method stub
95
		return null;
96
	}
97
 
98
	@Override
99
	public TaskData getTaskData(TaskRepository taskRepository, String taskId,
100
			IProgressMonitor monitor) throws CoreException {
101
		monitor.beginTask("Loading SBM Ticket", IProgressMonitor.UNKNOWN);
102
		TaskData data = null;
103
		try {
104
			SBMClient client = getClient(taskRepository);
105
			TTItem item = client.getTTItem(taskId);
39 brianR 106
			data = datahandler.convert(taskRepository, item, monitor, null);
30 brianR 107
 
108
		} finally {
109
			monitor.done();
110
		}
111
		return data;
112
	}
113
 
114
	@Override
115
	public String getTaskIdFromTaskUrl(String taskFullUrl) {
116
		// TODO Auto-generated method stub
117
		return null;
118
	}
119
 
120
	@Override
121
	public String getTaskUrl(String repositoryUrl, String taskId) {
122
		// TODO Auto-generated method stub
52 brianR 123
		return "http://something/I/havent/implemented/yet?"+taskId;
30 brianR 124
	}
125
 
126
	@Override
127
	public boolean hasTaskChanged(TaskRepository taskRepository, ITask task,
128
			TaskData taskData) {
129
		TaskAttribute attribute = taskData.getRoot().getAttribute(
130
				SBMStaticFields.LASTMODIFIEDDATE.getValue());
131
		if (attribute != null) {
132
			Date dataModificationDate = taskData.getAttributeMapper()
133
					.getDateValue(attribute);
134
			if (dataModificationDate != null) {
135
				Date taskModificationDate = task.getModificationDate();
136
				if (taskModificationDate != null) {
137
					return !taskModificationDate.equals(dataModificationDate);
138
				}
139
			}
140
		}
141
		return true;
142
	}
143
 
144
	@Override
145
	public IStatus performQuery(TaskRepository repository,
146
			IRepositoryQuery query, TaskDataCollector collector,
147
			ISynchronizationSession session, IProgressMonitor monitor) {
52 brianR 148
 
149
		SBMClient client = new SBMClient(repository);
150
		// do we use a report or a query?
151
		if (query.getAttribute(SBMRepositoryQueryPage.SQL_QUERY_NAME).equals(
152
				query.getAttribute(SBMRepositoryQueryPage.REPORT_OR_QUERY))) {
39 brianR 153
			monitor.subTask("Calculating query result size ...");
52 brianR 154
			try {
155
 
156
				List<String> idlist = client.getResultCount(
157
						getQueryTable(repository),
158
						query.getAttribute("sql_where"));
159
				int countItem = idlist.size();
160
				monitor.beginTask("", countItem);
161
				monitor.subTask("Loading " + countItem + " items ...");
162
 
163
				// List<TTItem> list = client.getTTItemsByTable(
164
				// getQueryTable(repository), query.getAttribute("sql_where"),
165
				// true);
166
				monitor.worked(1);
167
				int i = 1;
168
				for (String id : idlist) {
169
					if (id != null) {
170
						monitor.subTask("Loading item " + i + "/" + countItem
171
								+ " ...");
172
						TTItem ttitem = client.getTTItem("[" + id + "]");
173
						TaskData data = datahandler.convert(repository, ttitem,
174
								monitor, query);
175
						collector.accept(data);
176
					}
177
					monitor.worked(1);
178
					if (monitor.isCanceled())
179
						return Status.CANCEL_STATUS;
180
					i++;
42 brianR 181
				}
52 brianR 182
			} catch (CoreException e) {
183
				IStatus status = new Status(IStatus.ERROR,
184
						SBMConnectorPlugin.PLUGIN_ID, e.getMessage(), e);
185
				// StatusManager.getManager().handle(status,
186
				// StatusManager.BLOCK);
187
				return status;
188
			} finally {
189
				monitor.done();
30 brianR 190
			}
52 brianR 191
		} else {
192
			// use report
193
			monitor.beginTask("Loading Report", 0);
194
			try {
195
				List<TTItem> list = client.getTTItemsByReport(query
196
						.getAttribute(SBMRepositoryQueryPage.REPORT));
197
				for(TTItem i:list) {
198
					TaskData data = datahandler.convert(repository, i,
199
							monitor, query);
200
					collector.accept(data);
201
				}
202
			} catch (CoreException e) {
203
				IStatus status = new Status(IStatus.ERROR,
204
						SBMConnectorPlugin.PLUGIN_ID, e.getMessage(), e);
205
				// StatusManager.getManager().handle(status,
206
				// StatusManager.BLOCK);
207
				return status;
208
			} finally {
209
				monitor.done();
210
			}
30 brianR 211
		}
52 brianR 212
 
30 brianR 213
		return Status.OK_STATUS;
214
	}
215
 
216
	@Override
217
	public void updateRepositoryConfiguration(TaskRepository taskRepository,
218
			IProgressMonitor monitor) throws CoreException {
219
		// TODO Auto-generated method stub
220
 
221
	}
222
 
223
	@Override
224
	public void updateTaskFromTaskData(TaskRepository taskRepository,
225
			ITask task, TaskData taskData) {
226
		getTaskMapping(taskData).applyTo(task);
227
	}
228
 
229
	@Override
230
	public TaskMapper getTaskMapping(TaskData taskData) {
231
		return new TaskMapper(taskData);
232
	}
39 brianR 233
 
30 brianR 234
	@Override
235
	public AbstractTaskDataHandler getTaskDataHandler() {
236
		return datahandler;
237
	}
238
 
239
	public boolean canDeleteTask(TaskRepository repository, ITask task) {
240
		return false;
241
	}
39 brianR 242
 
31 brianR 243
	@Override
244
	public boolean hasRepositoryDueDate(TaskRepository taskRepository,
245
			ITask task, TaskData taskData) {
246
		// TODO Auto-generated method stub
39 brianR 247
		return false;
31 brianR 248
	}
30 brianR 249
 
39 brianR 250
	private String getQueryTable(TaskRepository repository) {
251
		return repository
252
				.getProperty(SBMRepositorySettingsPage.STRING_SOLUTIONTABLE);
253
	}
254
 
52 brianR 255
 
30 brianR 256
}