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