Subversion Repositories XServices

Rev

Go to most recent revision | Details | 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
 
21
package net.brutex.mylyn.sbmconnector.core;
22
 
23
import java.math.BigInteger;
24
import java.net.URL;
25
import java.util.ArrayList;
26
import java.util.Date;
27
import java.util.Iterator;
28
import java.util.List;
29
import java.util.StringTokenizer;
30
 
31
import javax.xml.namespace.QName;
32
import javax.xml.ws.BindingProvider;
33
 
34
import net.brutex.mylyn.sbmconnector.SBMConnectorPlugin;
31 brianR 35
import net.brutex.mylyn.sbmconnector.core.model.SBMField;
36
import net.brutex.mylyn.sbmconnector.core.model.SBMFieldTypes;
37
import net.brutex.mylyn.sbmconnector.core.model.SBMFieldValue;
30 brianR 38
import net.brutex.mylyn.sbmconnector.core.model.SBMNote;
39
import net.brutex.mylyn.sbmconnector.core.model.SBMStaticFields;
40
import net.brutex.sbm.wsclient.AEWebservicesFaultFault;
41
import net.brutex.sbm.wsclient.Aewebservices71;
42
import net.brutex.sbm.wsclient.Aewebservices71PortType;
43
import net.brutex.sbm.wsclient.Auth;
44
import net.brutex.sbm.wsclient.Field;
45
import net.brutex.sbm.wsclient.NameValue;
46
import net.brutex.sbm.wsclient.Note;
47
import net.brutex.sbm.wsclient.ObjectFactory;
48
import net.brutex.sbm.wsclient.TTItem;
49
import net.brutex.sbm.wsclient.TableData;
50
import net.brutex.sbm.wsclient.TableType;
31 brianR 51
import net.brutex.sbm.wsclient.Value;
30 brianR 52
 
53
import org.eclipse.core.runtime.CoreException;
54
import org.eclipse.mylyn.commons.net.AuthenticationCredentials;
55
import org.eclipse.mylyn.commons.net.AuthenticationType;
56
import org.eclipse.mylyn.tasks.core.RepositoryStatus;
57
import org.eclipse.mylyn.tasks.core.TaskRepository;
58
 
59
public class SBMClient {
60
 
61
	private Aewebservices71PortType port;
62
	private static final QName SERVICE_NAME = new QName("http://localhost:80/gsoap/aewebservices71.wsdl", "aewebservices71");
63
	private TaskRepository repository;
64
	private ObjectFactory of;
65
	private List<TableData> tables = new ArrayList<TableData>();
66
 
32 brianR 67
	/**
68
	 * Instantiates a new SBM client.
69
	 * Creates new instance of the aewebservices71 {@link net.brutex.sbm.wsclient.ObjectFactory} and
70
	 * initializes web service endpoint from repository url.
71
	 *
72
	 * @param repository the repository
73
	 */
30 brianR 74
	public SBMClient(TaskRepository repository) {
75
		this.repository = repository;
76
		this.of = new ObjectFactory();
77
 
78
        URL wsdlURL = Aewebservices71.WSDL_LOCATION;
79
		wsdlURL = this.getClass().getResource("/META-INF/aewebservices71.wsdl");
80
        Aewebservices71 ss = new Aewebservices71(wsdlURL, SERVICE_NAME);
81
        port = ss.getAewebservices71();
82
        ((BindingProvider)port).getRequestContext().put(
83
        		BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
84
        		repository.getRepositoryUrl());
85
	}
86
 
32 brianR 87
	/**
88
	 * Can authenticate checks if this SBMClient instance has proper authentication details
89
	 * set in its related repository. The check is done by invoking the GetUser web service.
90
	 *
91
	 * @return true, if successful
92
	 * @throws CoreException the core exception
93
	 */
30 brianR 94
	public boolean canAuthenticate() throws CoreException {
95
		try {
96
			port.getUser(getAuth(), repository.getCredentials(AuthenticationType.REPOSITORY).getUserName());
97
		} catch (AEWebservicesFaultFault e) {
98
			new CoreException(RepositoryStatus.createLoginError(
99
					repository.getRepositoryUrl(), SBMConnectorPlugin.PLUGIN_ID));
100
			return false;
101
		}
102
		return true;
103
	}
104
 
32 brianR 105
	/**
106
	 * Gets the SBM items from a table. The result size is limited to 500 and the sorting is done
107
	 * by submit date descending.
108
	 *
109
	 * @param tablename the tablename
110
	 * @param sql_where the sql_where
111
	 * @return the tT items by table
112
	 * @throws CoreException the core exception
113
	 */
30 brianR 114
	public List<TTItem> getTTItemsByTable(String tablename, String sql_where) throws CoreException {
115
		List<TTItem> list = new ArrayList<TTItem>();
116
		if(sql_where==null || sql_where.isEmpty()) sql_where = "TS_ID>0";
117
		try {
118
			list = port.getItemsByQueryWithName(
119
					getAuth(),
120
					tablename,
121
					"("+sql_where+")",
122
					"TS_SUBMITDATE desc",
123
					BigInteger.valueOf(500l), null);
124
		} catch (AEWebservicesFaultFault e) {
125
			new CoreException(
126
					RepositoryStatus.createInternalError(
127
							SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e));
128
		}
129
		return list;
130
	}
131
 
32 brianR 132
	/**
133
	 * Gets a SBM item specified by its internal identifier ([tableid:recordid])
134
	 *
135
	 * @param itemid the itemid
136
	 * @return the tT item
137
	 */
30 brianR 138
	public TTItem getTTItem(String itemid) {
139
		TTItem item = of.createTTItem();
140
			try {
141
				item = port.getItem(getAuth(), itemid, null);
142
			} catch (AEWebservicesFaultFault e) {
143
				new CoreException(
144
						RepositoryStatus.createInternalError(
145
								SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e));
146
			}
147
			return item;
148
	}
149
 
150
 
151
 
152
	private Auth getAuth() {
153
		Auth auth = of.createAuth();
154
		AuthenticationCredentials credentials = repository.getCredentials(AuthenticationType.REPOSITORY);
155
		auth.setUserId(of.createAuthUserId(credentials.getUserName()));
156
		auth.setPassword(of.createAuthPassword(credentials.getPassword()));
157
		return auth;
158
	}
159
 
31 brianR 160
	/**
161
	 * Gets the field value for a system generic field.
162
	 *
163
	 * @param ttitem the ttitem
164
	 * @param fieldname the fieldname
165
	 * @return the static field value
166
	 */
167
	public String getStaticFieldValue(TTItem ttitem, String fieldname) {
30 brianR 168
		if(fieldname.equals(SBMStaticFields.SUBMITDATE.getValue())) {
169
			Date date = ttitem.getCreateDate().getValue().toGregorianCalendar().getTime();
170
			return String.valueOf(date.getTime());
171
		}
172
		if(fieldname.equals(SBMStaticFields.LASTMODIFIEDDATE.getValue())) {
173
			return String.valueOf(ttitem.getModifiedDate().getValue().toGregorianCalendar().getTimeInMillis());
174
		}
175
		if(fieldname.equals("TITLE")) {
176
			if(ttitem.getTitle()==null || ttitem.getTitle().isNil()) return "";
177
			return ttitem.getTitle().getValue();
178
		}
179
		if(fieldname.equals(SBMStaticFields.ISSUEID.getValue())) {
180
			if(ttitem.getGenericItem()==null || ttitem.getGenericItem().getValue().getItemName()==null) {
181
				return "";
182
			}
183
			return ttitem.getGenericItem().getValue().getItemName().getValue();
184
		}
185
		if(fieldname.equals("ISSUETYPE")) {
186
			if(ttitem.getItemType()==null || ttitem.getItemType().isNil()) return "";
187
			return ttitem.getItemType().getValue();
188
		}
189
		if(fieldname.equals(SBMStaticFields.STATE.getValue())) {
190
			if(ttitem.getState()==null || ttitem.getState().isNil()) return "";
191
			return ttitem.getState().getValue();
192
		}
193
		if(fieldname.equals(SBMStaticFields.ID.getValue())) {
194
			return ttitem.getGenericItem().getValue().getItemID().getValue();
195
		}
196
		if(fieldname.equals(SBMStaticFields.PROJECTID.getValue())) {
197
			if(ttitem.getClassification() ==null || ttitem.getClassification().isNil()) return "";
198
			return ttitem.getClassification().getValue();
199
		}
200
		if(fieldname.equals(SBMStaticFields.PROJECTUUID.getValue())) {
201
			if(ttitem.getClassificationUUID()==null || ttitem.getClassificationUUID().isNil()) return "";
202
			return ttitem.getClassificationUUID().getValue();
203
		}
204
		if(fieldname.equals("DESCRIPTION")) {
205
			if(ttitem.getDescription() == null || ttitem.getDescription().isNil()) return "";
206
			return ttitem.getDescription().getValue();
207
		}
208
		if(fieldname.equals(SBMStaticFields.SUBMITTER.getValue())) {
209
			if(ttitem.getCreatedBy()==null || ttitem.getCreatedBy().isNil()) return "";
210
			return ttitem.getCreatedBy().getValue();
211
		}
212
		if(fieldname.equals(SBMStaticFields.SUBMITDATE.getValue())) {
213
			return String.valueOf(ttitem.getCreateDate().getValue().toGregorianCalendar().getTimeInMillis());
214
		}
215
		if(fieldname.equals(SBMStaticFields.LASTMODIFIER.getValue())) {
216
			if(ttitem.getModifiedBy()==null || ttitem.getModifiedBy().isNil()) return "";
217
			return ttitem.getModifiedBy().getValue();
218
		}
219
		if(fieldname.equals(SBMStaticFields.LASTMODIFIEDDATE.getValue())) {
220
			return String.valueOf(ttitem.getModifiedDate().getValue().toGregorianCalendar().getTimeInMillis());
221
		}
222
		if(fieldname.equals(SBMStaticFields.ACTIVEINACTIVE.getValue())) {
223
			return ttitem.getActiveInactive().getValue();
224
		}
225
		if(fieldname.equals(SBMStaticFields.OWNER.getValue())) {
226
			return ttitem.getOwner().getValue();
227
		}
228
		if(fieldname.equals(SBMStaticFields.ITEMURL.getValue())) {
229
			return ttitem.getUrl().getValue();
230
		}
231
		if(fieldname.equals(SBMStaticFields.UUID.getValue())) {
232
			return ttitem.getGenericItem().getValue().getItemUUID().getValue();
233
		}
234
		if(fieldname.equals(SBMStaticFields.CLOSEDATE.getValue())) {
235
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
236
			while (list.hasNext()) {
237
				NameValue field = list.next();
238
				if(field.getName().getValue().equals("CLOSEDATE")) {
239
					return field.getValue().getValue().getInternalValue().getValue();
240
				}
241
			}
242
		}
243
		if(fieldname.equals(SBMStaticFields.LASTSTATECHANGEDATE.getValue())) {
244
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
245
			while (list.hasNext()) {
246
				NameValue field = list.next();
247
				if(field.getName().getValue().equals("LASTSTATECHANGEDATE")) {
248
					return field.getValue().getValue().getInternalValue().getValue();
249
				}
250
			}
251
		}
252
		if(fieldname.equals(SBMStaticFields.SECONDARYOWNER.getValue())) {
253
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
254
			while (list.hasNext()) {
255
				NameValue field = list.next();
256
				if(field.getName().getValue().equals("SECONDARYOWNER")) {
257
					return field.getValue().getValue().getInternalValue().getValue();
258
				}
259
			}
260
		}
261
		if(fieldname.equals(SBMStaticFields.LASTSTATECHANGER.getValue())) {
262
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
263
			while (list.hasNext()) {
264
				NameValue field = list.next();
265
				if(field.getName().getValue().equals("LASTSTATECHANGER")) {
266
					return field.getValue().getValue().getDisplayValue().getValue();
267
				}
268
			}
269
		}
270
 
271
		return "UNKNOWN";
272
	}
273
 
32 brianR 274
	/**
275
	 * Gets the field label. The SBM item is used to determine the table id of
276
	 * the table where this field is in.
277
	 *
278
	 * @param ttitem the ttitem
279
	 * @param fieldname the fieldname
280
	 * @return the field label
281
	 */
30 brianR 282
	public String getFieldLabel(TTItem ttitem, String fieldname) {
31 brianR 283
		refreshTables();
30 brianR 284
		String itemid = ttitem.getGenericItem().getValue().getItemID().getValue();
285
		String tableid = new StringTokenizer(itemid, ":").nextToken();
286
		for (TableData table : tables) {
287
			if (String.valueOf(table.getTableID().intValue()).equals(tableid)) {
288
				Iterator<Field> iter = table.getFieldList().iterator();
289
				while(iter.hasNext()) {
290
					Field f = iter.next();
291
					if(f.getName().getValue().equals(fieldname)) {
292
						return f.getDisplayName().getValue();
293
					}
294
				}
295
				break;
296
			}
297
		}
298
	return "label_UNKNOWN";
299
	}
300
 
31 brianR 301
	/**
302
	 * Gets the table database name.
303
	 *
304
	 * @param ttitem the ttitem
305
	 * @return the table name or null in case table is not found
306
	 */
307
	public String getTableName(TTItem ttitem) {
308
		refreshTables();
309
		String itemid = ttitem.getGenericItem().getValue().getItemID().getValue();
310
		String tableid = new StringTokenizer(itemid, ":").nextToken();
311
		for (TableData table : tables) {
312
			if (String.valueOf(table.getTableID().intValue()).equals(tableid)) {
313
				return table.getName().getValue();
314
			}
315
		}
316
		return null;
317
	}
318
 
32 brianR 319
	/**
320
	 * Gets the notes attached to a SBM item.
321
	 *
322
	 * @param ttitem the ttitem
323
	 * @return the notes
324
	 */
30 brianR 325
	public List<SBMNote> getNotes(TTItem ttitem) {
326
		List<SBMNote> notes = new ArrayList<SBMNote>();
327
		Iterator<Note> iter = ttitem.getNoteList().iterator();
328
		while(iter.hasNext()) {
329
			Note n = iter.next();
330
			SBMNote note = new SBMNote("sbm_user",
331
					n.getTitle().getValue()+"\n"+n.getNote().getValue(),
332
					n.getModificationDateTime().toGregorianCalendar().getTime(),
333
					n.getId().toString());
334
			notes.add(note);
335
		}
336
		return notes;
337
	}
338
 
31 brianR 339
 
340
	/**
341
	 * Gets the names of all available primary tables.
342
	 * A table name is a unique reference within one SBM environment, thus can be
343
	 * used as a key.
344
	 *
345
	 * @return the primary table names as a list
346
	 */
30 brianR 347
	public List<String> getPrimaryTables() {
31 brianR 348
		refreshTables();
30 brianR 349
		List<String> table_names = new ArrayList<String>();
31 brianR 350
		for (TableData table : tables) {
351
			table_names.add(table.getName().getValue());
352
		}
353
		return table_names;
354
	}
355
 
356
	/**
357
	 * Refresh table specifications from SBM web service. This
358
	 * is only done once per SBMClient instance.
359
	 */
360
	private void refreshTables() {
30 brianR 361
		if (tables.isEmpty()) {
362
			try {
31 brianR 363
				//currently we limit this to primary tables
30 brianR 364
				tables = port.getTables(getAuth(), null, TableType.PRIMARY_TABLE);
365
			} catch (AEWebservicesFaultFault e) {
366
				new CoreException(
367
						RepositoryStatus.createInternalError(
368
								SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e));
369
			}
370
		}
31 brianR 371
	}
372
 
373
	/**
374
	 * Gets the fields for a primary table
375
	 *
376
	 * @param tablename the table database name
377
	 * @return the fields, empty when table does not exist
378
	 */
379
	public List<SBMField> getFields(String tablename) {
380
		refreshTables();
381
		List<SBMField> fields = new ArrayList<SBMField>();
30 brianR 382
		for (TableData table : tables) {
31 brianR 383
			if(table.getName().getValue().equals(tablename)) {
384
				Iterator<Field> iter = table.getFieldList().iterator();
385
				while(iter.hasNext()) {
386
					Field f = iter.next();
387
					SBMField nf = new SBMField(
388
							SBMFieldTypes.fromValue(f.getFieldType().value()),
389
							tablename,
390
							f.getDisplayName().getValue(),
391
							f.getName().getValue());
392
					fields.add(nf);
393
				}
394
				break;
395
			}
30 brianR 396
		}
31 brianR 397
		return fields;
30 brianR 398
	}
31 brianR 399
 
400
	/**
401
	 * Gets the field value for custom defined field.
402
	 * (those from &lt;extendedFieldList&gt;)
403
	 *
404
	 * @param ttitem the ttitem
405
	 * @param fieldname the fieldname
406
	 * @return the field value or null if the field is not found
407
	 */
408
	public SBMFieldValue getFieldValue(TTItem ttitem, String fieldname) {
409
		SBMFieldValue value;
410
		Iterator<NameValue> fs = ttitem.getExtendedFieldList().iterator();
411
		while(fs.hasNext()) {
412
			NameValue nv = fs.next();
413
			if(nv.getName().getValue().equals(fieldname)) {
414
				if (nv.getValue()!=null && !nv.getValue().isNil()) {
415
					value = new SBMFieldValue(
416
							nv.getValue().getValue().getInternalValue().getValue(),
417
							nv.getValue().getValue().getDisplayValue().getValue());
418
					return value;
419
				}
420
			}
421
		}
422
		return null;
423
	}
424
 
425
	/**
426
	 * Gets the field values for custom defined, multi type field.
427
	 * (those from &lt;extendedFieldList&gt;)
428
	 *
429
	 * @param ttitem the ttitem
430
	 * @param fieldname the fieldname
431
	 * @return the list of field values
432
	 */
433
	public List<SBMFieldValue> getFieldValues(TTItem ttitem, String fieldname) {
434
		List<SBMFieldValue> values = new ArrayList<SBMFieldValue>();
435
		Iterator<NameValue> fs = ttitem.getExtendedFieldList().iterator();
436
		while(fs.hasNext()) {
437
			NameValue nv = fs.next();
438
			if(nv.getName().getValue().equals(fieldname)) {
439
				if (nv.getValues()!=null && !nv.getValues().isEmpty()) {
440
					Iterator<Value> nvv = nv.getValues().iterator();
441
					while(nvv.hasNext()) {
442
						Value nvv_value = nvv.next();
443
						SBMFieldValue value = new SBMFieldValue(
444
							nvv_value.getInternalValue().getValue(),
445
							nvv_value.getDisplayValue().getValue());
446
						values.add(value);
447
					}
448
					return values;
449
				}
450
			}
451
		}
452
		return values;
453
	}
30 brianR 454
}