Subversion Repositories XServices

Rev

Rev 30 | Rev 32 | 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;
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
 
67
	public SBMClient(TaskRepository repository) {
68
		this.repository = repository;
69
		this.of = new ObjectFactory();
70
 
71
        URL wsdlURL = Aewebservices71.WSDL_LOCATION;
72
		wsdlURL = this.getClass().getResource("/META-INF/aewebservices71.wsdl");
73
        Aewebservices71 ss = new Aewebservices71(wsdlURL, SERVICE_NAME);
74
        port = ss.getAewebservices71();
75
        ((BindingProvider)port).getRequestContext().put(
76
        		BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
77
        		repository.getRepositoryUrl());
78
	}
79
 
80
	public boolean canAuthenticate() throws CoreException {
81
		try {
82
			port.getUser(getAuth(), repository.getCredentials(AuthenticationType.REPOSITORY).getUserName());
83
		} catch (AEWebservicesFaultFault e) {
84
			new CoreException(RepositoryStatus.createLoginError(
85
					repository.getRepositoryUrl(), SBMConnectorPlugin.PLUGIN_ID));
86
			return false;
87
		}
88
		return true;
89
	}
90
 
91
	public List<TTItem> getTTItemsByTable(String tablename, String sql_where) throws CoreException {
92
		List<TTItem> list = new ArrayList<TTItem>();
93
		if(sql_where==null || sql_where.isEmpty()) sql_where = "TS_ID>0";
94
		try {
95
			list = port.getItemsByQueryWithName(
96
					getAuth(),
97
					tablename,
98
					"("+sql_where+")",
99
					"TS_SUBMITDATE desc",
100
					BigInteger.valueOf(500l), null);
101
		} catch (AEWebservicesFaultFault e) {
102
			new CoreException(
103
					RepositoryStatus.createInternalError(
104
							SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e));
105
		}
106
		return list;
107
	}
108
 
109
	public TTItem getTTItem(String itemid) {
110
		TTItem item = of.createTTItem();
111
			try {
112
				item = port.getItem(getAuth(), itemid, null);
113
			} catch (AEWebservicesFaultFault e) {
114
				new CoreException(
115
						RepositoryStatus.createInternalError(
116
								SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e));
117
			}
118
			return item;
119
	}
120
 
121
 
122
 
123
	private Auth getAuth() {
124
		Auth auth = of.createAuth();
125
		AuthenticationCredentials credentials = repository.getCredentials(AuthenticationType.REPOSITORY);
126
		auth.setUserId(of.createAuthUserId(credentials.getUserName()));
127
		auth.setPassword(of.createAuthPassword(credentials.getPassword()));
128
		return auth;
129
	}
130
 
31 brianR 131
	/**
132
	 * Gets the field value for a system generic field.
133
	 *
134
	 * @param ttitem the ttitem
135
	 * @param fieldname the fieldname
136
	 * @return the static field value
137
	 */
138
	public String getStaticFieldValue(TTItem ttitem, String fieldname) {
30 brianR 139
		if(fieldname.equals(SBMStaticFields.SUBMITDATE.getValue())) {
140
			Date date = ttitem.getCreateDate().getValue().toGregorianCalendar().getTime();
141
			return String.valueOf(date.getTime());
142
		}
143
		if(fieldname.equals(SBMStaticFields.LASTMODIFIEDDATE.getValue())) {
144
			return String.valueOf(ttitem.getModifiedDate().getValue().toGregorianCalendar().getTimeInMillis());
145
		}
146
		if(fieldname.equals("TITLE")) {
147
			if(ttitem.getTitle()==null || ttitem.getTitle().isNil()) return "";
148
			return ttitem.getTitle().getValue();
149
		}
150
		if(fieldname.equals(SBMStaticFields.ISSUEID.getValue())) {
151
			if(ttitem.getGenericItem()==null || ttitem.getGenericItem().getValue().getItemName()==null) {
152
				return "";
153
			}
154
			return ttitem.getGenericItem().getValue().getItemName().getValue();
155
		}
156
		if(fieldname.equals("ISSUETYPE")) {
157
			if(ttitem.getItemType()==null || ttitem.getItemType().isNil()) return "";
158
			return ttitem.getItemType().getValue();
159
		}
160
		if(fieldname.equals(SBMStaticFields.STATE.getValue())) {
161
			if(ttitem.getState()==null || ttitem.getState().isNil()) return "";
162
			return ttitem.getState().getValue();
163
		}
164
		if(fieldname.equals(SBMStaticFields.ID.getValue())) {
165
			return ttitem.getGenericItem().getValue().getItemID().getValue();
166
		}
167
		if(fieldname.equals(SBMStaticFields.PROJECTID.getValue())) {
168
			if(ttitem.getClassification() ==null || ttitem.getClassification().isNil()) return "";
169
			return ttitem.getClassification().getValue();
170
		}
171
		if(fieldname.equals(SBMStaticFields.PROJECTUUID.getValue())) {
172
			if(ttitem.getClassificationUUID()==null || ttitem.getClassificationUUID().isNil()) return "";
173
			return ttitem.getClassificationUUID().getValue();
174
		}
175
		if(fieldname.equals("DESCRIPTION")) {
176
			if(ttitem.getDescription() == null || ttitem.getDescription().isNil()) return "";
177
			return ttitem.getDescription().getValue();
178
		}
179
		if(fieldname.equals(SBMStaticFields.SUBMITTER.getValue())) {
180
			if(ttitem.getCreatedBy()==null || ttitem.getCreatedBy().isNil()) return "";
181
			return ttitem.getCreatedBy().getValue();
182
		}
183
		if(fieldname.equals(SBMStaticFields.SUBMITDATE.getValue())) {
184
			return String.valueOf(ttitem.getCreateDate().getValue().toGregorianCalendar().getTimeInMillis());
185
		}
186
		if(fieldname.equals(SBMStaticFields.LASTMODIFIER.getValue())) {
187
			if(ttitem.getModifiedBy()==null || ttitem.getModifiedBy().isNil()) return "";
188
			return ttitem.getModifiedBy().getValue();
189
		}
190
		if(fieldname.equals(SBMStaticFields.LASTMODIFIEDDATE.getValue())) {
191
			return String.valueOf(ttitem.getModifiedDate().getValue().toGregorianCalendar().getTimeInMillis());
192
		}
193
		if(fieldname.equals(SBMStaticFields.ACTIVEINACTIVE.getValue())) {
194
			return ttitem.getActiveInactive().getValue();
195
		}
196
		if(fieldname.equals(SBMStaticFields.OWNER.getValue())) {
197
			return ttitem.getOwner().getValue();
198
		}
199
		if(fieldname.equals(SBMStaticFields.ITEMURL.getValue())) {
200
			return ttitem.getUrl().getValue();
201
		}
202
		if(fieldname.equals(SBMStaticFields.UUID.getValue())) {
203
			return ttitem.getGenericItem().getValue().getItemUUID().getValue();
204
		}
205
		if(fieldname.equals(SBMStaticFields.CLOSEDATE.getValue())) {
206
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
207
			while (list.hasNext()) {
208
				NameValue field = list.next();
209
				if(field.getName().getValue().equals("CLOSEDATE")) {
210
					return field.getValue().getValue().getInternalValue().getValue();
211
				}
212
			}
213
		}
214
		if(fieldname.equals(SBMStaticFields.LASTSTATECHANGEDATE.getValue())) {
215
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
216
			while (list.hasNext()) {
217
				NameValue field = list.next();
218
				if(field.getName().getValue().equals("LASTSTATECHANGEDATE")) {
219
					return field.getValue().getValue().getInternalValue().getValue();
220
				}
221
			}
222
		}
223
		if(fieldname.equals(SBMStaticFields.SECONDARYOWNER.getValue())) {
224
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
225
			while (list.hasNext()) {
226
				NameValue field = list.next();
227
				if(field.getName().getValue().equals("SECONDARYOWNER")) {
228
					return field.getValue().getValue().getInternalValue().getValue();
229
				}
230
			}
231
		}
232
		if(fieldname.equals(SBMStaticFields.LASTSTATECHANGER.getValue())) {
233
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
234
			while (list.hasNext()) {
235
				NameValue field = list.next();
236
				if(field.getName().getValue().equals("LASTSTATECHANGER")) {
237
					return field.getValue().getValue().getDisplayValue().getValue();
238
				}
239
			}
240
		}
241
 
242
		return "UNKNOWN";
243
	}
244
 
245
	public String getFieldLabel(TTItem ttitem, String fieldname) {
31 brianR 246
		refreshTables();
30 brianR 247
		String itemid = ttitem.getGenericItem().getValue().getItemID().getValue();
248
		String tableid = new StringTokenizer(itemid, ":").nextToken();
249
		for (TableData table : tables) {
250
			if (String.valueOf(table.getTableID().intValue()).equals(tableid)) {
251
				Iterator<Field> iter = table.getFieldList().iterator();
252
				while(iter.hasNext()) {
253
					Field f = iter.next();
254
					if(f.getName().getValue().equals(fieldname)) {
255
						return f.getDisplayName().getValue();
256
					}
257
				}
258
				break;
259
			}
260
		}
261
	return "label_UNKNOWN";
262
	}
263
 
31 brianR 264
	/**
265
	 * Gets the table database name.
266
	 *
267
	 * @param ttitem the ttitem
268
	 * @return the table name or null in case table is not found
269
	 */
270
	public String getTableName(TTItem ttitem) {
271
		refreshTables();
272
		String itemid = ttitem.getGenericItem().getValue().getItemID().getValue();
273
		String tableid = new StringTokenizer(itemid, ":").nextToken();
274
		for (TableData table : tables) {
275
			if (String.valueOf(table.getTableID().intValue()).equals(tableid)) {
276
				return table.getName().getValue();
277
			}
278
		}
279
		return null;
280
	}
281
 
30 brianR 282
	public List<SBMNote> getNotes(TTItem ttitem) {
283
		List<SBMNote> notes = new ArrayList<SBMNote>();
284
		Iterator<Note> iter = ttitem.getNoteList().iterator();
285
		while(iter.hasNext()) {
286
			Note n = iter.next();
287
			SBMNote note = new SBMNote("sbm_user",
288
					n.getTitle().getValue()+"\n"+n.getNote().getValue(),
289
					n.getModificationDateTime().toGregorianCalendar().getTime(),
290
					n.getId().toString());
291
			notes.add(note);
292
		}
293
		return notes;
294
	}
295
 
31 brianR 296
 
297
	/**
298
	 * Gets the names of all available primary tables.
299
	 * A table name is a unique reference within one SBM environment, thus can be
300
	 * used as a key.
301
	 *
302
	 * @return the primary table names as a list
303
	 */
30 brianR 304
	public List<String> getPrimaryTables() {
31 brianR 305
		refreshTables();
30 brianR 306
		List<String> table_names = new ArrayList<String>();
31 brianR 307
		for (TableData table : tables) {
308
			table_names.add(table.getName().getValue());
309
		}
310
		return table_names;
311
	}
312
 
313
	/**
314
	 * Refresh table specifications from SBM web service. This
315
	 * is only done once per SBMClient instance.
316
	 */
317
	private void refreshTables() {
30 brianR 318
		if (tables.isEmpty()) {
319
			try {
31 brianR 320
				//currently we limit this to primary tables
30 brianR 321
				tables = port.getTables(getAuth(), null, TableType.PRIMARY_TABLE);
322
			} catch (AEWebservicesFaultFault e) {
323
				new CoreException(
324
						RepositoryStatus.createInternalError(
325
								SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e));
326
			}
327
		}
31 brianR 328
	}
329
 
330
	/**
331
	 * Gets the fields for a primary table
332
	 *
333
	 * @param tablename the table database name
334
	 * @return the fields, empty when table does not exist
335
	 */
336
	public List<SBMField> getFields(String tablename) {
337
		refreshTables();
338
		List<SBMField> fields = new ArrayList<SBMField>();
30 brianR 339
		for (TableData table : tables) {
31 brianR 340
			if(table.getName().getValue().equals(tablename)) {
341
				Iterator<Field> iter = table.getFieldList().iterator();
342
				while(iter.hasNext()) {
343
					Field f = iter.next();
344
					SBMField nf = new SBMField(
345
							SBMFieldTypes.fromValue(f.getFieldType().value()),
346
							tablename,
347
							f.getDisplayName().getValue(),
348
							f.getName().getValue());
349
					fields.add(nf);
350
				}
351
				break;
352
			}
30 brianR 353
		}
31 brianR 354
		return fields;
30 brianR 355
	}
31 brianR 356
 
357
	/**
358
	 * Gets the field value for custom defined field.
359
	 * (those from &lt;extendedFieldList&gt;)
360
	 *
361
	 * @param ttitem the ttitem
362
	 * @param fieldname the fieldname
363
	 * @return the field value or null if the field is not found
364
	 */
365
	public SBMFieldValue getFieldValue(TTItem ttitem, String fieldname) {
366
		SBMFieldValue value;
367
		Iterator<NameValue> fs = ttitem.getExtendedFieldList().iterator();
368
		while(fs.hasNext()) {
369
			NameValue nv = fs.next();
370
			if(nv.getName().getValue().equals(fieldname)) {
371
				if (nv.getValue()!=null && !nv.getValue().isNil()) {
372
					value = new SBMFieldValue(
373
							nv.getValue().getValue().getInternalValue().getValue(),
374
							nv.getValue().getValue().getDisplayValue().getValue());
375
					return value;
376
				}
377
			}
378
		}
379
		return null;
380
	}
381
 
382
	/**
383
	 * Gets the field values for custom defined, multi type field.
384
	 * (those from &lt;extendedFieldList&gt;)
385
	 *
386
	 * @param ttitem the ttitem
387
	 * @param fieldname the fieldname
388
	 * @return the list of field values
389
	 */
390
	public List<SBMFieldValue> getFieldValues(TTItem ttitem, String fieldname) {
391
		List<SBMFieldValue> values = new ArrayList<SBMFieldValue>();
392
		Iterator<NameValue> fs = ttitem.getExtendedFieldList().iterator();
393
		while(fs.hasNext()) {
394
			NameValue nv = fs.next();
395
			if(nv.getName().getValue().equals(fieldname)) {
396
				if (nv.getValues()!=null && !nv.getValues().isEmpty()) {
397
					Iterator<Value> nvv = nv.getValues().iterator();
398
					while(nvv.hasNext()) {
399
						Value nvv_value = nvv.next();
400
						SBMFieldValue value = new SBMFieldValue(
401
							nvv_value.getInternalValue().getValue(),
402
							nvv_value.getDisplayValue().getValue());
403
						values.add(value);
404
					}
405
					return values;
406
				}
407
			}
408
		}
409
		return values;
410
	}
30 brianR 411
}