Subversion Repositories XServices

Rev

Rev 42 | 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
 
39 brianR 22
import java.util.Date;
23
import java.util.GregorianCalendar;
31 brianR 24
import java.util.List;
30 brianR 25
import java.util.Set;
26
 
39 brianR 27
import javax.xml.datatype.DatatypeConfigurationException;
28
import javax.xml.datatype.DatatypeFactory;
29
import javax.xml.datatype.XMLGregorianCalendar;
30
 
30 brianR 31
import net.brutex.mylyn.sbmconnector.SBMConnectorPlugin;
31 brianR 32
import net.brutex.mylyn.sbmconnector.core.model.SBMField;
33
import net.brutex.mylyn.sbmconnector.core.model.SBMFieldAttributes;
34
import net.brutex.mylyn.sbmconnector.core.model.SBMFieldTypes;
35
import net.brutex.mylyn.sbmconnector.core.model.SBMFieldValue;
30 brianR 36
import net.brutex.mylyn.sbmconnector.core.model.SBMNote;
37
import net.brutex.mylyn.sbmconnector.core.model.SBMStaticFields;
39 brianR 38
import net.brutex.mylyn.sbmconnector.core.model.SBMSystemFields;
39
import net.brutex.mylyn.sbmconnector.ui.SBMRepositorySettingsPage;
30 brianR 40
import net.brutex.sbm.wsclient.TTItem;
41
 
42
import org.eclipse.core.runtime.CoreException;
43
import org.eclipse.core.runtime.IProgressMonitor;
39 brianR 44
import org.eclipse.mylyn.tasks.core.IRepositoryQuery;
30 brianR 45
import org.eclipse.mylyn.tasks.core.ITaskMapping;
46
import org.eclipse.mylyn.tasks.core.RepositoryResponse;
47
import org.eclipse.mylyn.tasks.core.TaskRepository;
48
import org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler;
49
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
50
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;
51
import org.eclipse.mylyn.tasks.core.data.TaskCommentMapper;
52
import org.eclipse.mylyn.tasks.core.data.TaskData;
53
 
54
/**
55
 * The Class SBMTicketDataHandler.
56
 */
57
public class SBMTicketDataHandler extends AbstractTaskDataHandler {
58
 
59
	/** The connector. */
60
	private final SBMRepositoryConnector connector;
39 brianR 61
 
30 brianR 62
	/**
63
	 * Instantiates a new SBM ticket data handler.
39 brianR 64
	 *
65
	 * @param connector
66
	 *            the connector
30 brianR 67
	 */
68
	public SBMTicketDataHandler(SBMRepositoryConnector connector) {
69
		this.connector = connector;
70
	}
39 brianR 71
 
72
	/*
73
	 * (non-Javadoc)
74
	 *
75
	 * @see
76
	 * org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler#getAttributeMapper
77
	 * (org.eclipse.mylyn.tasks.core.TaskRepository)
30 brianR 78
	 */
79
	@Override
80
	public TaskAttributeMapper getAttributeMapper(TaskRepository taskRepository) {
81
		return new SBMTicketAttributeMapper(taskRepository);
82
	}
83
 
39 brianR 84
	/*
85
	 * (non-Javadoc)
86
	 *
87
	 * @see
88
	 * org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler#initializeTaskData
89
	 * (org.eclipse.mylyn.tasks.core.TaskRepository,
90
	 * org.eclipse.mylyn.tasks.core.data.TaskData,
91
	 * org.eclipse.mylyn.tasks.core.ITaskMapping,
92
	 * org.eclipse.core.runtime.IProgressMonitor)
30 brianR 93
	 */
94
	@Override
39 brianR 95
	public boolean initializeTaskData(TaskRepository repository,
96
			TaskData taskData, ITaskMapping initializationData,
97
			IProgressMonitor monitor) throws CoreException {
98
		monitor.subTask("Initialising task ...");
30 brianR 99
		taskData.setPartial(true);
100
		return true;
101
	}
102
 
39 brianR 103
	/*
104
	 * (non-Javadoc)
105
	 *
106
	 * @see
107
	 * org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler#postTaskData
108
	 * (org.eclipse.mylyn.tasks.core.TaskRepository,
109
	 * org.eclipse.mylyn.tasks.core.data.TaskData, java.util.Set,
110
	 * org.eclipse.core.runtime.IProgressMonitor)
30 brianR 111
	 */
112
	@Override
113
	public RepositoryResponse postTaskData(TaskRepository repository,
114
			TaskData taskData, Set<TaskAttribute> oldAttributes,
115
			IProgressMonitor monitor) throws CoreException {
116
		// TODO Auto-generated method stub
117
		return null;
118
	}
39 brianR 119
 
30 brianR 120
	/**
121
	 * Convert.
39 brianR 122
	 *
52 brianR 123
	 * @param repository	the repository
39 brianR 124
	 * @param ttitem
125
	 *            the ttitem
126
	 * @param monitor
127
	 *            the monitor
30 brianR 128
	 * @return the task data
39 brianR 129
	 * @throws CoreException
130
	 *             the core exception
30 brianR 131
	 */
39 brianR 132
	public TaskData convert(TaskRepository repository, TTItem ttitem,
133
			IProgressMonitor monitor, IRepositoryQuery query)
134
			throws CoreException {
31 brianR 135
		SBMClient client = SBMRepositoryConnector.getClient(repository);
30 brianR 136
		TaskData data = new TaskData(new SBMTicketAttributeMapper(repository),
39 brianR 137
				SBMConnectorPlugin.CONNECTOR_KIND, repository
138
						.getRepositoryUrl(), ttitem.getGenericItem().getValue()
139
						.getItemName().getValue()
140
						+ " ["
141
						+ ttitem.getGenericItem().getValue().getItemID()
142
								.getValue() + "]");
30 brianR 143
		initializeTaskData(repository, data, null, monitor);
39 brianR 144
		String fieldlist = "";
145
		if (repository
146
				.hasProperty(SBMRepositorySettingsPage.STRING_TABLEFIELDLIST)) {
147
			fieldlist = repository
148
					.getProperty(SBMRepositorySettingsPage.STRING_TABLEFIELDLIST);
149
		}
30 brianR 150
		// Create Fields for all the generic SBM (system) fields
39 brianR 151
		monitor.subTask("Creating generic SBM fields...");
152
		for (SBMSystemFields sysfield : SBMSystemFields.values()) {
153
			if(!sysfield.hasTaskAttribute()) {
154
				// skip the field here, because we do not have a
155
				// matching task attribute to map. The field
156
				// will be processed in the custom field section
157
				continue;
158
			}
159
			if (!fieldlist.contains("," + sysfield.getValue() + ",")) {
160
				// do not process custom field when it has not
161
				// been selected by user in repository settings page
162
				continue;
163
			}
164
			// try to initialize the SBM attribute
165
			SBMTicketAttribute f = null;
166
			TaskAttribute attr = null;
167
			try {
168
				f = SBMTicketAttribute.valueOf(sysfield.getValue());
169
			} catch (IllegalArgumentException e) {
170
				// the field is a valid sbm system field and is set up to
171
				// have an equivalent in the task attributes. It must have an
172
				// entry in the SBMTicketAttribute class !
173
				continue; // we just skip the field
174
			}
175
 
176
			if (f.getTaskName() != null) {
30 brianR 177
				attr = data.getRoot().createMappedAttribute(f.getTaskName());
178
			} else {
39 brianR 179
				// again the field is set up to have an task attribute
180
				// equivalent, but it is not set up in SBMAttributes correctly
181
				continue;
30 brianR 182
			}
39 brianR 183
 
184
			attr.getMetaData().setReadOnly(true); //we do not support writing yet
185
			attr.getMetaData().setType(f.getTaskType()); // set the mapped field type
186
			attr.getMetaData().setLabel(client.getFieldLabel(ttitem, f.getSBMName())); // get and set the field label
187
			if (f.isVisible()) {
188
				// field is not hidden, display it by setting KIND_DEFAULT
30 brianR 189
				attr.getMetaData().setKind(TaskAttribute.KIND_DEFAULT);
190
			} else {
39 brianR 191
				// field is hidden
30 brianR 192
				attr.getMetaData().setKind(null);
193
			}
34 brianR 194
 
39 brianR 195
			attr.setValue(client.getStaticFieldValue(ttitem, f.getSBMName())); // get and set the field value
30 brianR 196
		}
39 brianR 197
		monitor.worked(1);
30 brianR 198
 
39 brianR 199
		//--------------
200
		// Custom fields
201
		//--------------
202
		List<SBMField> customfields = client.getFields(client
203
				.getTableName(ttitem));
204
		for (SBMField f : customfields) {
205
			if (!fieldlist.contains("," + f.getName() + ",")) {
206
				// do not process custom field when it has not
207
				// been selected by user in repository settings page
31 brianR 208
				continue;
209
			}
39 brianR 210
			try {
211
				SBMSystemFields sysfield = SBMSystemFields.valueOf(f.getName());
212
				if( sysfield.hasTaskAttribute()) {
213
					// the field has an equivalent in the task attribute
214
					// mappings and therefore it can be skipped in the custom
215
					// fields section
216
					continue;
217
				}
218
			} catch (IllegalArgumentException e) {
219
				//field is not a system field, do nothing
220
			}
221
			monitor.subTask("Read custom field [" + f.getName() + "]");
31 brianR 222
			TaskAttribute custom = data.getRoot().createAttribute(f.getName());
223
			custom.getMetaData().setType(
39 brianR 224
					new SBMTicketAttributeMapper(repository).mapToTaskKey(f
225
							.getType()));
31 brianR 226
			custom.getMetaData().setLabel(f.getLabel());
227
			custom.getMetaData().setKind(TaskAttribute.KIND_DEFAULT);
39 brianR 228
			custom.getMetaData().setReadOnly(true);
229
 
230
			//set value/ values depending on field type
231
			// single selection
232
			if (f.getType() == SBMFieldTypes.SELECTION
233
					&& client.getFieldValue(ttitem, f.getName()) != null) {
42 brianR 234
				//List<SBMFieldValue> options = client.getValidSet(client
235
				//		.getTableName(ttitem), f.getName());
236
				//for (SBMFieldValue optionvalue : options) {
237
				//	custom.putOption(optionvalue.getInternalValue(),
238
				//			optionvalue.getValue());
239
				//}
39 brianR 240
				String val = client.getFieldValue(ttitem, f.getName())
241
						.getValue();
31 brianR 242
				custom.setValue(val);
39 brianR 243
			// Multi-Type fields
244
			} else if (f.getType() == SBMFieldTypes.MULTIPLE_SELECTION
245
					|| f.getType() == SBMFieldTypes.MULTIPLE_RELATIONAL
246
					|| f.getType() == SBMFieldTypes.MULTIPLE_GROUP
247
					|| f.getType() == SBMFieldTypes.MULTIPLE_USERGROUP) {
248
				List<SBMFieldValue> values = client.getFieldValues(ttitem, f
249
						.getName());
250
				for (SBMFieldValue val : values) {
31 brianR 251
					custom.putOption(val.getInternalValue(), val.getValue());
252
					custom.addValue(val.getValue());
39 brianR 253
				}
254
			//Date fields
255
			} else if (f.getType() == SBMFieldTypes.DATETIME) {
256
				String datestring = client.getFieldValue(ttitem, f.getName()).getValue();
257
				long timestamp;
258
				try {
259
					timestamp = DatatypeFactory.newInstance().newXMLGregorianCalendar(datestring).toGregorianCalendar().getTimeInMillis();
260
					custom.setValue(String.valueOf(timestamp));
261
				} catch (DatatypeConfigurationException e) {
262
					// TODO Auto-generated catch block
263
					e.printStackTrace();
264
				} catch (IllegalArgumentException e) {
265
					//date field is probably empty
266
					custom.setValue("");
267
				}
268
 
269
			// any other field type
270
			} else if (client.getFieldValue(ttitem, f.getName()) != null) {
271
				custom.setValue(client.getFieldValue(ttitem, f.getName())
272
						.getValue());
31 brianR 273
			}
274
		}
39 brianR 275
		monitor.worked(1);
276
 
277
		// ----------
278
		// Notes
279
		// ----------
280
		monitor.subTask("Read notes.");
281
		for (SBMNote n : client.getNotes(ttitem)) {
30 brianR 282
			TaskCommentMapper mapper = new TaskCommentMapper();
283
			mapper.setCommentId(n.getId());
284
			mapper.setNumber(Integer.valueOf(n.getId()));
285
			mapper.setText(n.getText());
286
			mapper.setCreationDate(n.getCreation());
39 brianR 287
 
288
			TaskAttribute a = data.getRoot()
289
					.createAttribute("NOTE" + n.getId());
30 brianR 290
			mapper.applyTo(a);
291
		}
39 brianR 292
		monitor.worked(1);
293
 
30 brianR 294
		data.setPartial(false);
295
		return data;
296
	}
297
}