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())) {
34 brianR 194
			return ttitem.getGenericItem().getValue().getItemName().getValue()+
195
			" ["+ttitem.getGenericItem().getValue().getItemID().getValue()+"]";
30 brianR 196
		}
197
		if(fieldname.equals(SBMStaticFields.PROJECTID.getValue())) {
198
			if(ttitem.getClassification() ==null || ttitem.getClassification().isNil()) return "";
199
			return ttitem.getClassification().getValue();
200
		}
201
		if(fieldname.equals(SBMStaticFields.PROJECTUUID.getValue())) {
202
			if(ttitem.getClassificationUUID()==null || ttitem.getClassificationUUID().isNil()) return "";
203
			return ttitem.getClassificationUUID().getValue();
204
		}
205
		if(fieldname.equals("DESCRIPTION")) {
206
			if(ttitem.getDescription() == null || ttitem.getDescription().isNil()) return "";
207
			return ttitem.getDescription().getValue();
208
		}
209
		if(fieldname.equals(SBMStaticFields.SUBMITTER.getValue())) {
210
			if(ttitem.getCreatedBy()==null || ttitem.getCreatedBy().isNil()) return "";
211
			return ttitem.getCreatedBy().getValue();
212
		}
213
		if(fieldname.equals(SBMStaticFields.SUBMITDATE.getValue())) {
214
			return String.valueOf(ttitem.getCreateDate().getValue().toGregorianCalendar().getTimeInMillis());
215
		}
216
		if(fieldname.equals(SBMStaticFields.LASTMODIFIER.getValue())) {
217
			if(ttitem.getModifiedBy()==null || ttitem.getModifiedBy().isNil()) return "";
218
			return ttitem.getModifiedBy().getValue();
219
		}
220
		if(fieldname.equals(SBMStaticFields.LASTMODIFIEDDATE.getValue())) {
221
			return String.valueOf(ttitem.getModifiedDate().getValue().toGregorianCalendar().getTimeInMillis());
222
		}
223
		if(fieldname.equals(SBMStaticFields.ACTIVEINACTIVE.getValue())) {
224
			return ttitem.getActiveInactive().getValue();
225
		}
226
		if(fieldname.equals(SBMStaticFields.OWNER.getValue())) {
227
			return ttitem.getOwner().getValue();
228
		}
229
		if(fieldname.equals(SBMStaticFields.ITEMURL.getValue())) {
230
			return ttitem.getUrl().getValue();
231
		}
232
		if(fieldname.equals(SBMStaticFields.UUID.getValue())) {
233
			return ttitem.getGenericItem().getValue().getItemUUID().getValue();
234
		}
235
		if(fieldname.equals(SBMStaticFields.CLOSEDATE.getValue())) {
236
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
237
			while (list.hasNext()) {
238
				NameValue field = list.next();
239
				if(field.getName().getValue().equals("CLOSEDATE")) {
240
					return field.getValue().getValue().getInternalValue().getValue();
241
				}
242
			}
243
		}
244
		if(fieldname.equals(SBMStaticFields.LASTSTATECHANGEDATE.getValue())) {
245
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
246
			while (list.hasNext()) {
247
				NameValue field = list.next();
248
				if(field.getName().getValue().equals("LASTSTATECHANGEDATE")) {
249
					return field.getValue().getValue().getInternalValue().getValue();
250
				}
251
			}
252
		}
253
		if(fieldname.equals(SBMStaticFields.SECONDARYOWNER.getValue())) {
254
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
255
			while (list.hasNext()) {
256
				NameValue field = list.next();
257
				if(field.getName().getValue().equals("SECONDARYOWNER")) {
258
					return field.getValue().getValue().getInternalValue().getValue();
259
				}
260
			}
261
		}
262
		if(fieldname.equals(SBMStaticFields.LASTSTATECHANGER.getValue())) {
263
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
264
			while (list.hasNext()) {
265
				NameValue field = list.next();
266
				if(field.getName().getValue().equals("LASTSTATECHANGER")) {
267
					return field.getValue().getValue().getDisplayValue().getValue();
268
				}
269
			}
270
		}
271
 
272
		return "UNKNOWN";
273
	}
274
 
32 brianR 275
	/**
276
	 * Gets the field label. The SBM item is used to determine the table id of
277
	 * the table where this field is in.
278
	 *
279
	 * @param ttitem the ttitem
280
	 * @param fieldname the fieldname
281
	 * @return the field label
282
	 */
30 brianR 283
	public String getFieldLabel(TTItem ttitem, String fieldname) {
31 brianR 284
		refreshTables();
30 brianR 285
		String itemid = ttitem.getGenericItem().getValue().getItemID().getValue();
286
		String tableid = new StringTokenizer(itemid, ":").nextToken();
287
		for (TableData table : tables) {
288
			if (String.valueOf(table.getTableID().intValue()).equals(tableid)) {
289
				Iterator<Field> iter = table.getFieldList().iterator();
290
				while(iter.hasNext()) {
291
					Field f = iter.next();
292
					if(f.getName().getValue().equals(fieldname)) {
293
						return f.getDisplayName().getValue();
294
					}
295
				}
296
				break;
297
			}
298
		}
299
	return "label_UNKNOWN";
300
	}
301
 
31 brianR 302
	/**
303
	 * Gets the table database name.
304
	 *
305
	 * @param ttitem the ttitem
306
	 * @return the table name or null in case table is not found
307
	 */
308
	public String getTableName(TTItem ttitem) {
309
		refreshTables();
310
		String itemid = ttitem.getGenericItem().getValue().getItemID().getValue();
311
		String tableid = new StringTokenizer(itemid, ":").nextToken();
312
		for (TableData table : tables) {
313
			if (String.valueOf(table.getTableID().intValue()).equals(tableid)) {
314
				return table.getName().getValue();
315
			}
316
		}
317
		return null;
318
	}
319
 
32 brianR 320
	/**
321
	 * Gets the notes attached to a SBM item.
322
	 *
323
	 * @param ttitem the ttitem
324
	 * @return the notes
325
	 */
30 brianR 326
	public List<SBMNote> getNotes(TTItem ttitem) {
327
		List<SBMNote> notes = new ArrayList<SBMNote>();
328
		Iterator<Note> iter = ttitem.getNoteList().iterator();
329
		while(iter.hasNext()) {
330
			Note n = iter.next();
331
			SBMNote note = new SBMNote("sbm_user",
332
					n.getTitle().getValue()+"\n"+n.getNote().getValue(),
333
					n.getModificationDateTime().toGregorianCalendar().getTime(),
334
					n.getId().toString());
335
			notes.add(note);
336
		}
337
		return notes;
338
	}
339
 
31 brianR 340
 
341
	/**
342
	 * Gets the names of all available primary tables.
343
	 * A table name is a unique reference within one SBM environment, thus can be
344
	 * used as a key.
345
	 *
346
	 * @return the primary table names as a list
347
	 */
30 brianR 348
	public List<String> getPrimaryTables() {
31 brianR 349
		refreshTables();
30 brianR 350
		List<String> table_names = new ArrayList<String>();
31 brianR 351
		for (TableData table : tables) {
352
			table_names.add(table.getName().getValue());
353
		}
354
		return table_names;
355
	}
356
 
357
	/**
358
	 * Refresh table specifications from SBM web service. This
359
	 * is only done once per SBMClient instance.
360
	 */
361
	private void refreshTables() {
30 brianR 362
		if (tables.isEmpty()) {
363
			try {
31 brianR 364
				//currently we limit this to primary tables
30 brianR 365
				tables = port.getTables(getAuth(), null, TableType.PRIMARY_TABLE);
366
			} catch (AEWebservicesFaultFault e) {
367
				new CoreException(
368
						RepositoryStatus.createInternalError(
369
								SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e));
370
			}
371
		}
31 brianR 372
	}
373
 
374
	/**
375
	 * Gets the fields for a primary table
376
	 *
377
	 * @param tablename the table database name
378
	 * @return the fields, empty when table does not exist
379
	 */
380
	public List<SBMField> getFields(String tablename) {
381
		refreshTables();
382
		List<SBMField> fields = new ArrayList<SBMField>();
30 brianR 383
		for (TableData table : tables) {
31 brianR 384
			if(table.getName().getValue().equals(tablename)) {
385
				Iterator<Field> iter = table.getFieldList().iterator();
386
				while(iter.hasNext()) {
387
					Field f = iter.next();
388
					SBMField nf = new SBMField(
389
							SBMFieldTypes.fromValue(f.getFieldType().value()),
390
							tablename,
391
							f.getDisplayName().getValue(),
392
							f.getName().getValue());
393
					fields.add(nf);
394
				}
395
				break;
396
			}
30 brianR 397
		}
31 brianR 398
		return fields;
30 brianR 399
	}
31 brianR 400
 
401
	/**
402
	 * Gets the field value for custom defined field.
403
	 * (those from &lt;extendedFieldList&gt;)
404
	 *
405
	 * @param ttitem the ttitem
406
	 * @param fieldname the fieldname
407
	 * @return the field value or null if the field is not found
408
	 */
409
	public SBMFieldValue getFieldValue(TTItem ttitem, String fieldname) {
410
		SBMFieldValue value;
411
		Iterator<NameValue> fs = ttitem.getExtendedFieldList().iterator();
412
		while(fs.hasNext()) {
413
			NameValue nv = fs.next();
414
			if(nv.getName().getValue().equals(fieldname)) {
415
				if (nv.getValue()!=null && !nv.getValue().isNil()) {
416
					value = new SBMFieldValue(
417
							nv.getValue().getValue().getInternalValue().getValue(),
418
							nv.getValue().getValue().getDisplayValue().getValue());
419
					return value;
420
				}
421
			}
422
		}
423
		return null;
424
	}
425
 
426
	/**
427
	 * Gets the field values for custom defined, multi type field.
428
	 * (those from &lt;extendedFieldList&gt;)
429
	 *
430
	 * @param ttitem the ttitem
431
	 * @param fieldname the fieldname
432
	 * @return the list of field values
433
	 */
434
	public List<SBMFieldValue> getFieldValues(TTItem ttitem, String fieldname) {
435
		List<SBMFieldValue> values = new ArrayList<SBMFieldValue>();
436
		Iterator<NameValue> fs = ttitem.getExtendedFieldList().iterator();
437
		while(fs.hasNext()) {
438
			NameValue nv = fs.next();
439
			if(nv.getName().getValue().equals(fieldname)) {
440
				if (nv.getValues()!=null && !nv.getValues().isEmpty()) {
441
					Iterator<Value> nvv = nv.getValues().iterator();
442
					while(nvv.hasNext()) {
443
						Value nvv_value = nvv.next();
444
						SBMFieldValue value = new SBMFieldValue(
445
							nvv_value.getInternalValue().getValue(),
446
							nvv_value.getDisplayValue().getValue());
447
						values.add(value);
448
					}
449
					return values;
450
				}
451
			}
452
		}
453
		return values;
454
	}
34 brianR 455
 
456
	public List<SBMFieldValue> getValidSet(String tablename, String fieldname) {
457
		List<SBMFieldValue> list = new ArrayList<SBMFieldValue>();
458
		List<TTItem> ttlist = new ArrayList<TTItem>();
459
		String sql = "TS_ID in (select max(TS_ID) from "+tablename+" group by ts_"+fieldname+")";
460
		try {
461
			ttlist = getTTItemsByTable(tablename, sql);
462
		} catch (CoreException e) {
463
			new CoreException(
464
					RepositoryStatus.createInternalError(
465
							SBMConnectorPlugin.PLUGIN_ID, e.getMessage(), e));
466
		}
467
		for(TTItem ttitem : ttlist) {
468
			list.add(getFieldValue(ttitem, fieldname));
469
		}
470
 
471
		return list;
472
	}
30 brianR 473
}