Subversion Repositories XServices

Rev

Rev 31 | 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;
35
import net.brutex.mylyn.sbmconnector.core.model.SBMNote;
36
import net.brutex.mylyn.sbmconnector.core.model.SBMStaticFields;
37
import net.brutex.sbm.wsclient.AEWebservicesFaultFault;
38
import net.brutex.sbm.wsclient.Aewebservices71;
39
import net.brutex.sbm.wsclient.Aewebservices71PortType;
40
import net.brutex.sbm.wsclient.Auth;
41
import net.brutex.sbm.wsclient.Field;
42
import net.brutex.sbm.wsclient.NameValue;
43
import net.brutex.sbm.wsclient.Note;
44
import net.brutex.sbm.wsclient.ObjectFactory;
45
import net.brutex.sbm.wsclient.TTItem;
46
import net.brutex.sbm.wsclient.TableData;
47
import net.brutex.sbm.wsclient.TableType;
48
 
49
import org.eclipse.core.runtime.CoreException;
50
import org.eclipse.mylyn.commons.net.AuthenticationCredentials;
51
import org.eclipse.mylyn.commons.net.AuthenticationType;
52
import org.eclipse.mylyn.tasks.core.RepositoryStatus;
53
import org.eclipse.mylyn.tasks.core.TaskRepository;
54
 
55
public class SBMClient {
56
 
57
	private Aewebservices71PortType port;
58
	private static final QName SERVICE_NAME = new QName("http://localhost:80/gsoap/aewebservices71.wsdl", "aewebservices71");
59
	private TaskRepository repository;
60
	private ObjectFactory of;
61
	private List<TableData> tables = new ArrayList<TableData>();
62
 
63
	public SBMClient(TaskRepository repository) {
64
		this.repository = repository;
65
		this.of = new ObjectFactory();
66
 
67
        URL wsdlURL = Aewebservices71.WSDL_LOCATION;
68
		wsdlURL = this.getClass().getResource("/META-INF/aewebservices71.wsdl");
69
        Aewebservices71 ss = new Aewebservices71(wsdlURL, SERVICE_NAME);
70
        port = ss.getAewebservices71();
71
        ((BindingProvider)port).getRequestContext().put(
72
        		BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
73
        		repository.getRepositoryUrl());
74
	}
75
 
76
	public boolean canAuthenticate() throws CoreException {
77
		try {
78
			port.getUser(getAuth(), repository.getCredentials(AuthenticationType.REPOSITORY).getUserName());
79
		} catch (AEWebservicesFaultFault e) {
80
			new CoreException(RepositoryStatus.createLoginError(
81
					repository.getRepositoryUrl(), SBMConnectorPlugin.PLUGIN_ID));
82
			return false;
83
		}
84
		return true;
85
	}
86
 
87
	public List<TTItem> getTTItemsByTable(String tablename, String sql_where) throws CoreException {
88
		List<TTItem> list = new ArrayList<TTItem>();
89
		if(sql_where==null || sql_where.isEmpty()) sql_where = "TS_ID>0";
90
		try {
91
			list = port.getItemsByQueryWithName(
92
					getAuth(),
93
					tablename,
94
					"("+sql_where+")",
95
					"TS_SUBMITDATE desc",
96
					BigInteger.valueOf(500l), null);
97
		} catch (AEWebservicesFaultFault e) {
98
			new CoreException(
99
					RepositoryStatus.createInternalError(
100
							SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e));
101
		}
102
		return list;
103
	}
104
 
105
	public TTItem getTTItem(String itemid) {
106
		TTItem item = of.createTTItem();
107
			try {
108
				item = port.getItem(getAuth(), itemid, null);
109
			} catch (AEWebservicesFaultFault e) {
110
				new CoreException(
111
						RepositoryStatus.createInternalError(
112
								SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e));
113
			}
114
			return item;
115
	}
116
 
117
 
118
 
119
	private Auth getAuth() {
120
		Auth auth = of.createAuth();
121
		AuthenticationCredentials credentials = repository.getCredentials(AuthenticationType.REPOSITORY);
122
		auth.setUserId(of.createAuthUserId(credentials.getUserName()));
123
		auth.setPassword(of.createAuthPassword(credentials.getPassword()));
124
		return auth;
125
	}
126
 
127
	public String getFieldValue(TTItem ttitem, String fieldname) {
128
		if(fieldname.equals(SBMStaticFields.SUBMITDATE.getValue())) {
129
			Date date = ttitem.getCreateDate().getValue().toGregorianCalendar().getTime();
130
			return String.valueOf(date.getTime());
131
		}
132
		if(fieldname.equals(SBMStaticFields.LASTMODIFIEDDATE.getValue())) {
133
			return String.valueOf(ttitem.getModifiedDate().getValue().toGregorianCalendar().getTimeInMillis());
134
		}
135
		if(fieldname.equals("TITLE")) {
136
			if(ttitem.getTitle()==null || ttitem.getTitle().isNil()) return "";
137
			return ttitem.getTitle().getValue();
138
		}
139
		if(fieldname.equals(SBMStaticFields.ISSUEID.getValue())) {
140
			if(ttitem.getGenericItem()==null || ttitem.getGenericItem().getValue().getItemName()==null) {
141
				return "";
142
			}
143
			return ttitem.getGenericItem().getValue().getItemName().getValue();
144
		}
145
		if(fieldname.equals("ISSUETYPE")) {
146
			if(ttitem.getItemType()==null || ttitem.getItemType().isNil()) return "";
147
			return ttitem.getItemType().getValue();
148
		}
149
		if(fieldname.equals(SBMStaticFields.STATE.getValue())) {
150
			if(ttitem.getState()==null || ttitem.getState().isNil()) return "";
151
			return ttitem.getState().getValue();
152
		}
153
		if(fieldname.equals(SBMStaticFields.ID.getValue())) {
154
			return ttitem.getGenericItem().getValue().getItemID().getValue();
155
		}
156
		if(fieldname.equals(SBMStaticFields.PROJECTID.getValue())) {
157
			if(ttitem.getClassification() ==null || ttitem.getClassification().isNil()) return "";
158
			return ttitem.getClassification().getValue();
159
		}
160
		if(fieldname.equals(SBMStaticFields.PROJECTUUID.getValue())) {
161
			if(ttitem.getClassificationUUID()==null || ttitem.getClassificationUUID().isNil()) return "";
162
			return ttitem.getClassificationUUID().getValue();
163
		}
164
		if(fieldname.equals("DESCRIPTION")) {
165
			if(ttitem.getDescription() == null || ttitem.getDescription().isNil()) return "";
166
			return ttitem.getDescription().getValue();
167
		}
168
		if(fieldname.equals(SBMStaticFields.SUBMITTER.getValue())) {
169
			if(ttitem.getCreatedBy()==null || ttitem.getCreatedBy().isNil()) return "";
170
			return ttitem.getCreatedBy().getValue();
171
		}
172
		if(fieldname.equals(SBMStaticFields.SUBMITDATE.getValue())) {
173
			return String.valueOf(ttitem.getCreateDate().getValue().toGregorianCalendar().getTimeInMillis());
174
		}
175
		if(fieldname.equals(SBMStaticFields.LASTMODIFIER.getValue())) {
176
			if(ttitem.getModifiedBy()==null || ttitem.getModifiedBy().isNil()) return "";
177
			return ttitem.getModifiedBy().getValue();
178
		}
179
		if(fieldname.equals(SBMStaticFields.LASTMODIFIEDDATE.getValue())) {
180
			return String.valueOf(ttitem.getModifiedDate().getValue().toGregorianCalendar().getTimeInMillis());
181
		}
182
		if(fieldname.equals(SBMStaticFields.ACTIVEINACTIVE.getValue())) {
183
			return ttitem.getActiveInactive().getValue();
184
		}
185
		if(fieldname.equals(SBMStaticFields.OWNER.getValue())) {
186
			return ttitem.getOwner().getValue();
187
		}
188
		if(fieldname.equals(SBMStaticFields.ITEMURL.getValue())) {
189
			return ttitem.getUrl().getValue();
190
		}
191
		if(fieldname.equals(SBMStaticFields.UUID.getValue())) {
192
			return ttitem.getGenericItem().getValue().getItemUUID().getValue();
193
		}
194
		if(fieldname.equals(SBMStaticFields.CLOSEDATE.getValue())) {
195
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
196
			while (list.hasNext()) {
197
				NameValue field = list.next();
198
				if(field.getName().getValue().equals("CLOSEDATE")) {
199
					return field.getValue().getValue().getInternalValue().getValue();
200
				}
201
			}
202
		}
203
		if(fieldname.equals(SBMStaticFields.LASTSTATECHANGEDATE.getValue())) {
204
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
205
			while (list.hasNext()) {
206
				NameValue field = list.next();
207
				if(field.getName().getValue().equals("LASTSTATECHANGEDATE")) {
208
					return field.getValue().getValue().getInternalValue().getValue();
209
				}
210
			}
211
		}
212
		if(fieldname.equals(SBMStaticFields.SECONDARYOWNER.getValue())) {
213
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
214
			while (list.hasNext()) {
215
				NameValue field = list.next();
216
				if(field.getName().getValue().equals("SECONDARYOWNER")) {
217
					return field.getValue().getValue().getInternalValue().getValue();
218
				}
219
			}
220
		}
221
		if(fieldname.equals(SBMStaticFields.LASTSTATECHANGER.getValue())) {
222
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
223
			while (list.hasNext()) {
224
				NameValue field = list.next();
225
				if(field.getName().getValue().equals("LASTSTATECHANGER")) {
226
					return field.getValue().getValue().getDisplayValue().getValue();
227
				}
228
			}
229
		}
230
 
231
		return "UNKNOWN";
232
	}
233
 
234
	//Todo caching
235
	public String getFieldLabel(TTItem ttitem, String fieldname) {
236
 
237
		String itemid = ttitem.getGenericItem().getValue().getItemID().getValue();
238
		String tableid = new StringTokenizer(itemid, ":").nextToken();
239
		if (tables.isEmpty()) {
240
			try {
241
				tables = port.getTables(getAuth(), null, TableType.PRIMARY_TABLE);
242
			} catch (AEWebservicesFaultFault e) {
243
				new CoreException(
244
						RepositoryStatus.createInternalError(
245
								SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e));
246
			}
247
		}
248
		for (TableData table : tables) {
249
			if (String.valueOf(table.getTableID().intValue()).equals(tableid)) {
250
				Iterator<Field> iter = table.getFieldList().iterator();
251
				while(iter.hasNext()) {
252
					Field f = iter.next();
253
					if(f.getName().getValue().equals(fieldname)) {
254
						return f.getDisplayName().getValue();
255
					}
256
				}
257
				break;
258
			}
259
		}
260
	return "label_UNKNOWN";
261
	}
262
 
263
	public List<SBMNote> getNotes(TTItem ttitem) {
264
		List<SBMNote> notes = new ArrayList<SBMNote>();
265
		Iterator<Note> iter = ttitem.getNoteList().iterator();
266
		while(iter.hasNext()) {
267
			Note n = iter.next();
268
			SBMNote note = new SBMNote("sbm_user",
269
					n.getTitle().getValue()+"\n"+n.getNote().getValue(),
270
					n.getModificationDateTime().toGregorianCalendar().getTime(),
271
					n.getId().toString());
272
			notes.add(note);
273
		}
274
		return notes;
275
	}
276
 
277
	public List<String> getPrimaryTables() {
278
		List<String> table_names = new ArrayList<String>();
279
		if (tables.isEmpty()) {
280
			try {
281
				tables = port.getTables(getAuth(), null, TableType.PRIMARY_TABLE);
282
			} catch (AEWebservicesFaultFault e) {
283
				new CoreException(
284
						RepositoryStatus.createInternalError(
285
								SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e));
286
			}
287
		}
288
		for (TableData table : tables) {
289
			table_names.add(table.getName().getValue());
290
		}
291
		return table_names;
292
	}
293
}