Subversion Repositories XServices

Rev

Rev 34 | Rev 42 | 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
 
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
 
39 brianR 142
	public int getResultCount(String tablename, String sql) throws CoreException {
143
		List<TTItem> list = new ArrayList<TTItem>();
144
		try {
145
			list = port.getItemsByQueryWithName(
146
					getAuth(),
147
					tablename,
148
					"("+sql+")",
149
					"TS_ID desc",
150
					null,
151
					"SECTION:NONE");
152
		} catch (AEWebservicesFaultFault e) {
153
			throw new CoreException(
154
					RepositoryStatus.createInternalError(
155
							SBMConnectorPlugin.PLUGIN_ID, e.getMessage(), e));
156
		}
157
		return list.size();
158
	}
159
 
32 brianR 160
	/**
161
	 * Gets a SBM item specified by its internal identifier ([tableid:recordid])
162
	 *
163
	 * @param itemid the itemid
164
	 * @return the tT item
165
	 */
30 brianR 166
	public TTItem getTTItem(String itemid) {
39 brianR 167
		int pos1;
168
		int pos2;
169
		pos1 = itemid.lastIndexOf("[")+1;
170
		pos2 = itemid.lastIndexOf("]");
171
		itemid = itemid.substring(pos1, pos2);
30 brianR 172
		TTItem item = of.createTTItem();
173
			try {
174
				item = port.getItem(getAuth(), itemid, null);
175
			} catch (AEWebservicesFaultFault e) {
176
				new CoreException(
177
						RepositoryStatus.createInternalError(
178
								SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e));
179
			}
180
			return item;
181
	}
182
 
183
 
184
 
185
	private Auth getAuth() {
186
		Auth auth = of.createAuth();
187
		AuthenticationCredentials credentials = repository.getCredentials(AuthenticationType.REPOSITORY);
188
		auth.setUserId(of.createAuthUserId(credentials.getUserName()));
189
		auth.setPassword(of.createAuthPassword(credentials.getPassword()));
190
		return auth;
191
	}
192
 
31 brianR 193
	/**
194
	 * Gets the field value for a system generic field.
195
	 *
196
	 * @param ttitem the ttitem
197
	 * @param fieldname the fieldname
198
	 * @return the static field value
199
	 */
200
	public String getStaticFieldValue(TTItem ttitem, String fieldname) {
30 brianR 201
		if(fieldname.equals(SBMStaticFields.SUBMITDATE.getValue())) {
202
			Date date = ttitem.getCreateDate().getValue().toGregorianCalendar().getTime();
203
			return String.valueOf(date.getTime());
204
		}
205
		if(fieldname.equals(SBMStaticFields.LASTMODIFIEDDATE.getValue())) {
206
			return String.valueOf(ttitem.getModifiedDate().getValue().toGregorianCalendar().getTimeInMillis());
207
		}
208
		if(fieldname.equals("TITLE")) {
209
			if(ttitem.getTitle()==null || ttitem.getTitle().isNil()) return "";
210
			return ttitem.getTitle().getValue();
211
		}
212
		if(fieldname.equals(SBMStaticFields.ISSUEID.getValue())) {
213
			if(ttitem.getGenericItem()==null || ttitem.getGenericItem().getValue().getItemName()==null) {
214
				return "";
215
			}
216
			return ttitem.getGenericItem().getValue().getItemName().getValue();
217
		}
218
		if(fieldname.equals("ISSUETYPE")) {
219
			if(ttitem.getItemType()==null || ttitem.getItemType().isNil()) return "";
220
			return ttitem.getItemType().getValue();
221
		}
222
		if(fieldname.equals(SBMStaticFields.STATE.getValue())) {
223
			if(ttitem.getState()==null || ttitem.getState().isNil()) return "";
224
			return ttitem.getState().getValue();
225
		}
226
		if(fieldname.equals(SBMStaticFields.ID.getValue())) {
34 brianR 227
			return ttitem.getGenericItem().getValue().getItemName().getValue()+
228
			" ["+ttitem.getGenericItem().getValue().getItemID().getValue()+"]";
30 brianR 229
		}
230
		if(fieldname.equals(SBMStaticFields.PROJECTID.getValue())) {
231
			if(ttitem.getClassification() ==null || ttitem.getClassification().isNil()) return "";
232
			return ttitem.getClassification().getValue();
233
		}
234
		if(fieldname.equals(SBMStaticFields.PROJECTUUID.getValue())) {
235
			if(ttitem.getClassificationUUID()==null || ttitem.getClassificationUUID().isNil()) return "";
236
			return ttitem.getClassificationUUID().getValue();
237
		}
238
		if(fieldname.equals("DESCRIPTION")) {
239
			if(ttitem.getDescription() == null || ttitem.getDescription().isNil()) return "";
240
			return ttitem.getDescription().getValue();
241
		}
242
		if(fieldname.equals(SBMStaticFields.SUBMITTER.getValue())) {
243
			if(ttitem.getCreatedBy()==null || ttitem.getCreatedBy().isNil()) return "";
244
			return ttitem.getCreatedBy().getValue();
245
		}
246
		if(fieldname.equals(SBMStaticFields.SUBMITDATE.getValue())) {
247
			return String.valueOf(ttitem.getCreateDate().getValue().toGregorianCalendar().getTimeInMillis());
248
		}
249
		if(fieldname.equals(SBMStaticFields.LASTMODIFIER.getValue())) {
250
			if(ttitem.getModifiedBy()==null || ttitem.getModifiedBy().isNil()) return "";
251
			return ttitem.getModifiedBy().getValue();
252
		}
253
		if(fieldname.equals(SBMStaticFields.LASTMODIFIEDDATE.getValue())) {
254
			return String.valueOf(ttitem.getModifiedDate().getValue().toGregorianCalendar().getTimeInMillis());
255
		}
256
		if(fieldname.equals(SBMStaticFields.ACTIVEINACTIVE.getValue())) {
257
			return ttitem.getActiveInactive().getValue();
258
		}
259
		if(fieldname.equals(SBMStaticFields.OWNER.getValue())) {
260
			return ttitem.getOwner().getValue();
261
		}
262
		if(fieldname.equals(SBMStaticFields.ITEMURL.getValue())) {
263
			return ttitem.getUrl().getValue();
264
		}
265
		if(fieldname.equals(SBMStaticFields.UUID.getValue())) {
266
			return ttitem.getGenericItem().getValue().getItemUUID().getValue();
267
		}
268
		if(fieldname.equals(SBMStaticFields.CLOSEDATE.getValue())) {
269
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
270
			while (list.hasNext()) {
271
				NameValue field = list.next();
272
				if(field.getName().getValue().equals("CLOSEDATE")) {
273
					return field.getValue().getValue().getInternalValue().getValue();
274
				}
275
			}
276
		}
277
		if(fieldname.equals(SBMStaticFields.LASTSTATECHANGEDATE.getValue())) {
278
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
279
			while (list.hasNext()) {
280
				NameValue field = list.next();
281
				if(field.getName().getValue().equals("LASTSTATECHANGEDATE")) {
282
					return field.getValue().getValue().getInternalValue().getValue();
283
				}
284
			}
285
		}
286
		if(fieldname.equals(SBMStaticFields.SECONDARYOWNER.getValue())) {
287
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
288
			while (list.hasNext()) {
289
				NameValue field = list.next();
290
				if(field.getName().getValue().equals("SECONDARYOWNER")) {
291
					return field.getValue().getValue().getInternalValue().getValue();
292
				}
293
			}
294
		}
295
		if(fieldname.equals(SBMStaticFields.LASTSTATECHANGER.getValue())) {
296
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
297
			while (list.hasNext()) {
298
				NameValue field = list.next();
299
				if(field.getName().getValue().equals("LASTSTATECHANGER")) {
300
					return field.getValue().getValue().getDisplayValue().getValue();
301
				}
302
			}
303
		}
304
 
305
		return "UNKNOWN";
306
	}
307
 
32 brianR 308
	/**
309
	 * Gets the field label. The SBM item is used to determine the table id of
310
	 * the table where this field is in.
311
	 *
312
	 * @param ttitem the ttitem
313
	 * @param fieldname the fieldname
314
	 * @return the field label
315
	 */
30 brianR 316
	public String getFieldLabel(TTItem ttitem, String fieldname) {
31 brianR 317
		refreshTables();
30 brianR 318
		String itemid = ttitem.getGenericItem().getValue().getItemID().getValue();
319
		String tableid = new StringTokenizer(itemid, ":").nextToken();
320
		for (TableData table : tables) {
321
			if (String.valueOf(table.getTableID().intValue()).equals(tableid)) {
322
				Iterator<Field> iter = table.getFieldList().iterator();
323
				while(iter.hasNext()) {
324
					Field f = iter.next();
325
					if(f.getName().getValue().equals(fieldname)) {
326
						return f.getDisplayName().getValue();
327
					}
328
				}
329
				break;
330
			}
331
		}
39 brianR 332
	return fieldname; //field has not been found
30 brianR 333
	}
334
 
31 brianR 335
	/**
336
	 * Gets the table database name.
337
	 *
338
	 * @param ttitem the ttitem
339
	 * @return the table name or null in case table is not found
340
	 */
341
	public String getTableName(TTItem ttitem) {
342
		refreshTables();
343
		String itemid = ttitem.getGenericItem().getValue().getItemID().getValue();
344
		String tableid = new StringTokenizer(itemid, ":").nextToken();
345
		for (TableData table : tables) {
346
			if (String.valueOf(table.getTableID().intValue()).equals(tableid)) {
347
				return table.getName().getValue();
348
			}
349
		}
350
		return null;
351
	}
352
 
32 brianR 353
	/**
354
	 * Gets the notes attached to a SBM item.
355
	 *
356
	 * @param ttitem the ttitem
357
	 * @return the notes
358
	 */
30 brianR 359
	public List<SBMNote> getNotes(TTItem ttitem) {
360
		List<SBMNote> notes = new ArrayList<SBMNote>();
361
		Iterator<Note> iter = ttitem.getNoteList().iterator();
362
		while(iter.hasNext()) {
363
			Note n = iter.next();
364
			SBMNote note = new SBMNote("sbm_user",
365
					n.getTitle().getValue()+"\n"+n.getNote().getValue(),
366
					n.getModificationDateTime().toGregorianCalendar().getTime(),
367
					n.getId().toString());
368
			notes.add(note);
369
		}
370
		return notes;
371
	}
372
 
31 brianR 373
 
374
	/**
375
	 * Gets the names of all available primary tables.
376
	 * A table name is a unique reference within one SBM environment, thus can be
377
	 * used as a key.
378
	 *
379
	 * @return the primary table names as a list
380
	 */
30 brianR 381
	public List<String> getPrimaryTables() {
31 brianR 382
		refreshTables();
30 brianR 383
		List<String> table_names = new ArrayList<String>();
31 brianR 384
		for (TableData table : tables) {
385
			table_names.add(table.getName().getValue());
386
		}
387
		return table_names;
388
	}
389
 
390
	/**
391
	 * Refresh table specifications from SBM web service. This
392
	 * is only done once per SBMClient instance.
393
	 */
394
	private void refreshTables() {
30 brianR 395
		if (tables.isEmpty()) {
396
			try {
31 brianR 397
				//currently we limit this to primary tables
30 brianR 398
				tables = port.getTables(getAuth(), null, TableType.PRIMARY_TABLE);
399
			} catch (AEWebservicesFaultFault e) {
400
				new CoreException(
401
						RepositoryStatus.createInternalError(
402
								SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e));
403
			}
404
		}
31 brianR 405
	}
406
 
407
	/**
408
	 * Gets the fields for a primary table
409
	 *
410
	 * @param tablename the table database name
411
	 * @return the fields, empty when table does not exist
412
	 */
413
	public List<SBMField> getFields(String tablename) {
414
		refreshTables();
415
		List<SBMField> fields = new ArrayList<SBMField>();
30 brianR 416
		for (TableData table : tables) {
31 brianR 417
			if(table.getName().getValue().equals(tablename)) {
418
				Iterator<Field> iter = table.getFieldList().iterator();
419
				while(iter.hasNext()) {
420
					Field f = iter.next();
421
					SBMField nf = new SBMField(
422
							SBMFieldTypes.fromValue(f.getFieldType().value()),
423
							tablename,
424
							f.getDisplayName().getValue(),
425
							f.getName().getValue());
426
					fields.add(nf);
427
				}
428
				break;
429
			}
30 brianR 430
		}
31 brianR 431
		return fields;
30 brianR 432
	}
31 brianR 433
 
434
	/**
435
	 * Gets the field value for custom defined field.
436
	 * (those from &lt;extendedFieldList&gt;)
437
	 *
438
	 * @param ttitem the ttitem
439
	 * @param fieldname the fieldname
440
	 * @return the field value or null if the field is not found
441
	 */
442
	public SBMFieldValue getFieldValue(TTItem ttitem, String fieldname) {
443
		SBMFieldValue value;
444
		Iterator<NameValue> fs = ttitem.getExtendedFieldList().iterator();
445
		while(fs.hasNext()) {
446
			NameValue nv = fs.next();
447
			if(nv.getName().getValue().equals(fieldname)) {
448
				if (nv.getValue()!=null && !nv.getValue().isNil()) {
449
					value = new SBMFieldValue(
450
							nv.getValue().getValue().getInternalValue().getValue(),
451
							nv.getValue().getValue().getDisplayValue().getValue());
452
					return value;
453
				}
454
			}
455
		}
456
		return null;
457
	}
458
 
459
	/**
460
	 * Gets the field values for custom defined, multi type field.
461
	 * (those from &lt;extendedFieldList&gt;)
462
	 *
463
	 * @param ttitem the ttitem
464
	 * @param fieldname the fieldname
465
	 * @return the list of field values
466
	 */
467
	public List<SBMFieldValue> getFieldValues(TTItem ttitem, String fieldname) {
468
		List<SBMFieldValue> values = new ArrayList<SBMFieldValue>();
469
		Iterator<NameValue> fs = ttitem.getExtendedFieldList().iterator();
470
		while(fs.hasNext()) {
471
			NameValue nv = fs.next();
472
			if(nv.getName().getValue().equals(fieldname)) {
473
				if (nv.getValues()!=null && !nv.getValues().isEmpty()) {
474
					Iterator<Value> nvv = nv.getValues().iterator();
475
					while(nvv.hasNext()) {
476
						Value nvv_value = nvv.next();
477
						SBMFieldValue value = new SBMFieldValue(
478
							nvv_value.getInternalValue().getValue(),
479
							nvv_value.getDisplayValue().getValue());
480
						values.add(value);
481
					}
482
					return values;
483
				}
484
			}
485
		}
486
		return values;
487
	}
34 brianR 488
 
489
	public List<SBMFieldValue> getValidSet(String tablename, String fieldname) {
39 brianR 490
		if(validsets.containsKey(tablename+":"+fieldname)) return validsets.get(tablename+":"+fieldname);
34 brianR 491
		List<SBMFieldValue> list = new ArrayList<SBMFieldValue>();
492
		List<TTItem> ttlist = new ArrayList<TTItem>();
493
		String sql = "TS_ID in (select max(TS_ID) from "+tablename+" group by ts_"+fieldname+")";
494
		try {
495
			ttlist = getTTItemsByTable(tablename, sql);
496
		} catch (CoreException e) {
497
			new CoreException(
498
					RepositoryStatus.createInternalError(
499
							SBMConnectorPlugin.PLUGIN_ID, e.getMessage(), e));
500
		}
501
		for(TTItem ttitem : ttlist) {
502
			list.add(getFieldValue(ttitem, fieldname));
503
		}
39 brianR 504
		validsets.put(tablename+":"+fieldname, list);
34 brianR 505
		return list;
506
	}
30 brianR 507
}