Subversion Repositories XServices

Rev

Rev 39 | 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
 
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
	 *
123
	 * @param repository
124
	 *            the repository
125
	 * @param ttitem
126
	 *            the ttitem
127
	 * @param monitor
128
	 *            the monitor
30 brianR 129
	 * @return the task data
39 brianR 130
	 * @throws CoreException
131
	 *             the core exception
30 brianR 132
	 */
39 brianR 133
	public TaskData convert(TaskRepository repository, TTItem ttitem,
134
			IProgressMonitor monitor, IRepositoryQuery query)
135
			throws CoreException {
31 brianR 136
		SBMClient client = SBMRepositoryConnector.getClient(repository);
30 brianR 137
		TaskData data = new TaskData(new SBMTicketAttributeMapper(repository),
39 brianR 138
				SBMConnectorPlugin.CONNECTOR_KIND, repository
139
						.getRepositoryUrl(), ttitem.getGenericItem().getValue()
140
						.getItemName().getValue()
141
						+ " ["
142
						+ ttitem.getGenericItem().getValue().getItemID()
143
								.getValue() + "]");
30 brianR 144
		initializeTaskData(repository, data, null, monitor);
39 brianR 145
		String fieldlist = "";
146
		if (repository
147
				.hasProperty(SBMRepositorySettingsPage.STRING_TABLEFIELDLIST)) {
148
			fieldlist = repository
149
					.getProperty(SBMRepositorySettingsPage.STRING_TABLEFIELDLIST);
150
		}
30 brianR 151
		// Create Fields for all the generic SBM (system) fields
39 brianR 152
		monitor.subTask("Creating generic SBM fields...");
153
		for (SBMSystemFields sysfield : SBMSystemFields.values()) {
154
			if(!sysfield.hasTaskAttribute()) {
155
				// skip the field here, because we do not have a
156
				// matching task attribute to map. The field
157
				// will be processed in the custom field section
158
				continue;
159
			}
160
			if (!fieldlist.contains("," + sysfield.getValue() + ",")) {
161
				// do not process custom field when it has not
162
				// been selected by user in repository settings page
163
				continue;
164
			}
165
			// try to initialize the SBM attribute
166
			SBMTicketAttribute f = null;
167
			TaskAttribute attr = null;
168
			try {
169
				f = SBMTicketAttribute.valueOf(sysfield.getValue());
170
			} catch (IllegalArgumentException e) {
171
				// the field is a valid sbm system field and is set up to
172
				// have an equivalent in the task attributes. It must have an
173
				// entry in the SBMTicketAttribute class !
174
				continue; // we just skip the field
175
			}
176
 
177
			if (f.getTaskName() != null) {
30 brianR 178
				attr = data.getRoot().createMappedAttribute(f.getTaskName());
179
			} else {
39 brianR 180
				// again the field is set up to have an task attribute
181
				// equivalent, but it is not set up in SBMAttributes correctly
182
				continue;
30 brianR 183
			}
39 brianR 184
 
185
			attr.getMetaData().setReadOnly(true); //we do not support writing yet
186
			attr.getMetaData().setType(f.getTaskType()); // set the mapped field type
187
			attr.getMetaData().setLabel(client.getFieldLabel(ttitem, f.getSBMName())); // get and set the field label
188
			if (f.isVisible()) {
189
				// field is not hidden, display it by setting KIND_DEFAULT
30 brianR 190
				attr.getMetaData().setKind(TaskAttribute.KIND_DEFAULT);
191
			} else {
39 brianR 192
				// field is hidden
30 brianR 193
				attr.getMetaData().setKind(null);
194
			}
34 brianR 195
 
39 brianR 196
			attr.setValue(client.getStaticFieldValue(ttitem, f.getSBMName())); // get and set the field value
30 brianR 197
		}
39 brianR 198
		monitor.worked(1);
30 brianR 199
 
39 brianR 200
		//--------------
201
		// Custom fields
202
		//--------------
203
		List<SBMField> customfields = client.getFields(client
204
				.getTableName(ttitem));
205
		for (SBMField f : customfields) {
206
			if (!fieldlist.contains("," + f.getName() + ",")) {
207
				// do not process custom field when it has not
208
				// been selected by user in repository settings page
31 brianR 209
				continue;
210
			}
39 brianR 211
			try {
212
				SBMSystemFields sysfield = SBMSystemFields.valueOf(f.getName());
213
				if( sysfield.hasTaskAttribute()) {
214
					// the field has an equivalent in the task attribute
215
					// mappings and therefore it can be skipped in the custom
216
					// fields section
217
					continue;
218
				}
219
			} catch (IllegalArgumentException e) {
220
				//field is not a system field, do nothing
221
			}
222
			monitor.subTask("Read custom field [" + f.getName() + "]");
31 brianR 223
			TaskAttribute custom = data.getRoot().createAttribute(f.getName());
224
			custom.getMetaData().setType(
39 brianR 225
					new SBMTicketAttributeMapper(repository).mapToTaskKey(f
226
							.getType()));
31 brianR 227
			custom.getMetaData().setLabel(f.getLabel());
228
			custom.getMetaData().setKind(TaskAttribute.KIND_DEFAULT);
39 brianR 229
			custom.getMetaData().setReadOnly(true);
230
 
231
			//set value/ values depending on field type
232
			// single selection
233
			if (f.getType() == SBMFieldTypes.SELECTION
234
					&& client.getFieldValue(ttitem, f.getName()) != null) {
42 brianR 235
				//List<SBMFieldValue> options = client.getValidSet(client
236
				//		.getTableName(ttitem), f.getName());
237
				//for (SBMFieldValue optionvalue : options) {
238
				//	custom.putOption(optionvalue.getInternalValue(),
239
				//			optionvalue.getValue());
240
				//}
39 brianR 241
				String val = client.getFieldValue(ttitem, f.getName())
242
						.getValue();
31 brianR 243
				custom.setValue(val);
39 brianR 244
			// Multi-Type fields
245
			} else if (f.getType() == SBMFieldTypes.MULTIPLE_SELECTION
246
					|| f.getType() == SBMFieldTypes.MULTIPLE_RELATIONAL
247
					|| f.getType() == SBMFieldTypes.MULTIPLE_GROUP
248
					|| f.getType() == SBMFieldTypes.MULTIPLE_USERGROUP) {
249
				List<SBMFieldValue> values = client.getFieldValues(ttitem, f
250
						.getName());
251
				for (SBMFieldValue val : values) {
31 brianR 252
					custom.putOption(val.getInternalValue(), val.getValue());
253
					custom.addValue(val.getValue());
39 brianR 254
				}
255
			//Date fields
256
			} else if (f.getType() == SBMFieldTypes.DATETIME) {
257
				String datestring = client.getFieldValue(ttitem, f.getName()).getValue();
258
				long timestamp;
259
				try {
260
					timestamp = DatatypeFactory.newInstance().newXMLGregorianCalendar(datestring).toGregorianCalendar().getTimeInMillis();
261
					custom.setValue(String.valueOf(timestamp));
262
				} catch (DatatypeConfigurationException e) {
263
					// TODO Auto-generated catch block
264
					e.printStackTrace();
265
				} catch (IllegalArgumentException e) {
266
					//date field is probably empty
267
					custom.setValue("");
268
				}
269
 
270
			// any other field type
271
			} else if (client.getFieldValue(ttitem, f.getName()) != null) {
272
				custom.setValue(client.getFieldValue(ttitem, f.getName())
273
						.getValue());
31 brianR 274
			}
275
		}
39 brianR 276
		monitor.worked(1);
277
 
278
		// ----------
279
		// Notes
280
		// ----------
281
		monitor.subTask("Read notes.");
282
		for (SBMNote n : client.getNotes(ttitem)) {
30 brianR 283
			TaskCommentMapper mapper = new TaskCommentMapper();
284
			mapper.setCommentId(n.getId());
285
			mapper.setNumber(Integer.valueOf(n.getId()));
286
			mapper.setText(n.getText());
287
			mapper.setCreationDate(n.getCreation());
39 brianR 288
 
289
			TaskAttribute a = data.getRoot()
290
					.createAttribute("NOTE" + n.getId());
30 brianR 291
			mapper.applyTo(a);
292
		}
39 brianR 293
		monitor.worked(1);
294
 
30 brianR 295
		data.setPartial(false);
296
		return data;
297
	}
298
}