Subversion Repositories XServices

Rev

Rev 31 | Rev 34 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 31 Rev 32
1
/*
1
/*
2
 *   Mylyn Connector for Serena Business Mashups
2
 *   Mylyn Connector for Serena Business Mashups
3
 * 	 Copyright 2010 Brian Rosenberger (Brutex Network)
3
 * 	 Copyright 2010 Brian Rosenberger (Brutex Network)
4
 *
4
 *
5
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   Licensed under the Apache License, Version 2.0 (the "License");
6
 *   you may not use this file except in compliance with 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
7
 *   You may obtain a copy of the License at
8
 *
8
 *
9
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *       http://www.apache.org/licenses/LICENSE-2.0
10
 *
10
 *
11
 *   Unless required by applicable law or agreed to in writing, software
11
 *   Unless required by applicable law or agreed to in writing, software
12
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   distributed under the License is distributed on an "AS IS" BASIS,
13
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 *   See the License for the specific language governing permissions and
14
 *   See the License for the specific language governing permissions and
15
 *   limitations under the License.
15
 *   limitations under the License.
16
 * 
16
 * 
17
 *   Serena, TeamTrack and Serena Business Mashup are 
17
 *   Serena, TeamTrack and Serena Business Mashup are 
18
 * 	 registered trademarks of SERENA Software Inc.
18
 * 	 registered trademarks of SERENA Software Inc.
19
 */
19
 */
20
 
20
 
21
package net.brutex.mylyn.sbmconnector.core;
21
package net.brutex.mylyn.sbmconnector.core;
22
 
22
 
23
import java.math.BigInteger;
23
import java.math.BigInteger;
24
import java.net.URL;
24
import java.net.URL;
25
import java.util.ArrayList;
25
import java.util.ArrayList;
26
import java.util.Date;
26
import java.util.Date;
27
import java.util.Iterator;
27
import java.util.Iterator;
28
import java.util.List;
28
import java.util.List;
29
import java.util.StringTokenizer;
29
import java.util.StringTokenizer;
30
 
30
 
31
import javax.xml.namespace.QName;
31
import javax.xml.namespace.QName;
32
import javax.xml.ws.BindingProvider;
32
import javax.xml.ws.BindingProvider;
33
 
33
 
34
import net.brutex.mylyn.sbmconnector.SBMConnectorPlugin;
34
import net.brutex.mylyn.sbmconnector.SBMConnectorPlugin;
35
import net.brutex.mylyn.sbmconnector.core.model.SBMField;
35
import net.brutex.mylyn.sbmconnector.core.model.SBMField;
36
import net.brutex.mylyn.sbmconnector.core.model.SBMFieldTypes;
36
import net.brutex.mylyn.sbmconnector.core.model.SBMFieldTypes;
37
import net.brutex.mylyn.sbmconnector.core.model.SBMFieldValue;
37
import net.brutex.mylyn.sbmconnector.core.model.SBMFieldValue;
38
import net.brutex.mylyn.sbmconnector.core.model.SBMNote;
38
import net.brutex.mylyn.sbmconnector.core.model.SBMNote;
39
import net.brutex.mylyn.sbmconnector.core.model.SBMStaticFields;
39
import net.brutex.mylyn.sbmconnector.core.model.SBMStaticFields;
40
import net.brutex.sbm.wsclient.AEWebservicesFaultFault;
40
import net.brutex.sbm.wsclient.AEWebservicesFaultFault;
41
import net.brutex.sbm.wsclient.Aewebservices71;
41
import net.brutex.sbm.wsclient.Aewebservices71;
42
import net.brutex.sbm.wsclient.Aewebservices71PortType;
42
import net.brutex.sbm.wsclient.Aewebservices71PortType;
43
import net.brutex.sbm.wsclient.Auth;
43
import net.brutex.sbm.wsclient.Auth;
44
import net.brutex.sbm.wsclient.Field;
44
import net.brutex.sbm.wsclient.Field;
45
import net.brutex.sbm.wsclient.NameValue;
45
import net.brutex.sbm.wsclient.NameValue;
46
import net.brutex.sbm.wsclient.Note;
46
import net.brutex.sbm.wsclient.Note;
47
import net.brutex.sbm.wsclient.ObjectFactory;
47
import net.brutex.sbm.wsclient.ObjectFactory;
48
import net.brutex.sbm.wsclient.TTItem;
48
import net.brutex.sbm.wsclient.TTItem;
49
import net.brutex.sbm.wsclient.TableData;
49
import net.brutex.sbm.wsclient.TableData;
50
import net.brutex.sbm.wsclient.TableType;
50
import net.brutex.sbm.wsclient.TableType;
51
import net.brutex.sbm.wsclient.Value;
51
import net.brutex.sbm.wsclient.Value;
52
 
52
 
53
import org.eclipse.core.runtime.CoreException;
53
import org.eclipse.core.runtime.CoreException;
54
import org.eclipse.mylyn.commons.net.AuthenticationCredentials;
54
import org.eclipse.mylyn.commons.net.AuthenticationCredentials;
55
import org.eclipse.mylyn.commons.net.AuthenticationType;
55
import org.eclipse.mylyn.commons.net.AuthenticationType;
56
import org.eclipse.mylyn.tasks.core.RepositoryStatus;
56
import org.eclipse.mylyn.tasks.core.RepositoryStatus;
57
import org.eclipse.mylyn.tasks.core.TaskRepository;
57
import org.eclipse.mylyn.tasks.core.TaskRepository;
58
 
58
 
59
public class SBMClient {
59
public class SBMClient {
60
 
60
 
61
	private Aewebservices71PortType port;
61
	private Aewebservices71PortType port;
62
	private static final QName SERVICE_NAME = new QName("http://localhost:80/gsoap/aewebservices71.wsdl", "aewebservices71");
62
	private static final QName SERVICE_NAME = new QName("http://localhost:80/gsoap/aewebservices71.wsdl", "aewebservices71");
63
	private TaskRepository repository;
63
	private TaskRepository repository;
64
	private ObjectFactory of;
64
	private ObjectFactory of;
65
	private List<TableData> tables = new ArrayList<TableData>();
65
	private List<TableData> tables = new ArrayList<TableData>();
-
 
66
	
-
 
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
66
	
73
	 */
67
	public SBMClient(TaskRepository repository) {
74
	public SBMClient(TaskRepository repository) {
68
		this.repository = repository;
75
		this.repository = repository;
69
		this.of = new ObjectFactory();
76
		this.of = new ObjectFactory();
70
		
77
		
71
        URL wsdlURL = Aewebservices71.WSDL_LOCATION;
78
        URL wsdlURL = Aewebservices71.WSDL_LOCATION;
72
		wsdlURL = this.getClass().getResource("/META-INF/aewebservices71.wsdl");
79
		wsdlURL = this.getClass().getResource("/META-INF/aewebservices71.wsdl");
73
        Aewebservices71 ss = new Aewebservices71(wsdlURL, SERVICE_NAME);
80
        Aewebservices71 ss = new Aewebservices71(wsdlURL, SERVICE_NAME);
74
        port = ss.getAewebservices71();
81
        port = ss.getAewebservices71();
75
        ((BindingProvider)port).getRequestContext().put(
82
        ((BindingProvider)port).getRequestContext().put(
76
        		BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
83
        		BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
77
        		repository.getRepositoryUrl());
84
        		repository.getRepositoryUrl());
78
	}
85
	}
-
 
86
	
-
 
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
79
	
93
	 */
80
	public boolean canAuthenticate() throws CoreException {
94
	public boolean canAuthenticate() throws CoreException {
81
		try {
95
		try {
82
			port.getUser(getAuth(), repository.getCredentials(AuthenticationType.REPOSITORY).getUserName());
96
			port.getUser(getAuth(), repository.getCredentials(AuthenticationType.REPOSITORY).getUserName());
83
		} catch (AEWebservicesFaultFault e) {
97
		} catch (AEWebservicesFaultFault e) {
84
			new CoreException(RepositoryStatus.createLoginError(
98
			new CoreException(RepositoryStatus.createLoginError(
85
					repository.getRepositoryUrl(), SBMConnectorPlugin.PLUGIN_ID));
99
					repository.getRepositoryUrl(), SBMConnectorPlugin.PLUGIN_ID));
86
			return false;
100
			return false;
87
		}
101
		}
88
		return true;
102
		return true;
89
	}
103
	}
-
 
104
	
-
 
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
90
	
113
	 */
91
	public List<TTItem> getTTItemsByTable(String tablename, String sql_where) throws CoreException {
114
	public List<TTItem> getTTItemsByTable(String tablename, String sql_where) throws CoreException {
92
		List<TTItem> list = new ArrayList<TTItem>();
115
		List<TTItem> list = new ArrayList<TTItem>();
93
		if(sql_where==null || sql_where.isEmpty()) sql_where = "TS_ID>0";
116
		if(sql_where==null || sql_where.isEmpty()) sql_where = "TS_ID>0";
94
		try {
117
		try {
95
			list = port.getItemsByQueryWithName(
118
			list = port.getItemsByQueryWithName(
96
					getAuth(), 
119
					getAuth(), 
97
					tablename, 
120
					tablename, 
98
					"("+sql_where+")",
121
					"("+sql_where+")",
99
					"TS_SUBMITDATE desc",
122
					"TS_SUBMITDATE desc",
100
					BigInteger.valueOf(500l), null);
123
					BigInteger.valueOf(500l), null);
101
		} catch (AEWebservicesFaultFault e) {
124
		} catch (AEWebservicesFaultFault e) {
102
			new CoreException(
125
			new CoreException(
103
					RepositoryStatus.createInternalError(
126
					RepositoryStatus.createInternalError(
104
							SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e));
127
							SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e));
105
		}
128
		}
106
		return list;
129
		return list;
107
	}
130
	}
-
 
131
	
-
 
132
	/**
-
 
133
	 * Gets a SBM item specified by its internal identifier ([tableid:recordid])
-
 
134
	 *
-
 
135
	 * @param itemid the itemid
-
 
136
	 * @return the tT item
108
	
137
	 */
109
	public TTItem getTTItem(String itemid) {
138
	public TTItem getTTItem(String itemid) {
110
		TTItem item = of.createTTItem();
139
		TTItem item = of.createTTItem();
111
			try {
140
			try {
112
				item = port.getItem(getAuth(), itemid, null);
141
				item = port.getItem(getAuth(), itemid, null);
113
			} catch (AEWebservicesFaultFault e) {
142
			} catch (AEWebservicesFaultFault e) {
114
				new CoreException(
143
				new CoreException(
115
						RepositoryStatus.createInternalError(
144
						RepositoryStatus.createInternalError(
116
								SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e));
145
								SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e));
117
			}
146
			}
118
			return item;
147
			return item;
119
	}
148
	}
120
	
149
	
121
 
150
 
122
	
151
	
123
	private Auth getAuth() {
152
	private Auth getAuth() {
124
		Auth auth = of.createAuth();
153
		Auth auth = of.createAuth();
125
		AuthenticationCredentials credentials = repository.getCredentials(AuthenticationType.REPOSITORY);
154
		AuthenticationCredentials credentials = repository.getCredentials(AuthenticationType.REPOSITORY);
126
		auth.setUserId(of.createAuthUserId(credentials.getUserName()));
155
		auth.setUserId(of.createAuthUserId(credentials.getUserName()));
127
		auth.setPassword(of.createAuthPassword(credentials.getPassword()));	
156
		auth.setPassword(of.createAuthPassword(credentials.getPassword()));	
128
		return auth;
157
		return auth;
129
	}
158
	}
130
 
159
 
131
	/**
160
	/**
132
	 * Gets the field value for a system generic field.
161
	 * Gets the field value for a system generic field.
133
	 *
162
	 *
134
	 * @param ttitem the ttitem
163
	 * @param ttitem the ttitem
135
	 * @param fieldname the fieldname
164
	 * @param fieldname the fieldname
136
	 * @return the static field value
165
	 * @return the static field value
137
	 */
166
	 */
138
	public String getStaticFieldValue(TTItem ttitem, String fieldname) {
167
	public String getStaticFieldValue(TTItem ttitem, String fieldname) {
139
		if(fieldname.equals(SBMStaticFields.SUBMITDATE.getValue())) {
168
		if(fieldname.equals(SBMStaticFields.SUBMITDATE.getValue())) {
140
			Date date = ttitem.getCreateDate().getValue().toGregorianCalendar().getTime();
169
			Date date = ttitem.getCreateDate().getValue().toGregorianCalendar().getTime();
141
			return String.valueOf(date.getTime());
170
			return String.valueOf(date.getTime());
142
		}
171
		}
143
		if(fieldname.equals(SBMStaticFields.LASTMODIFIEDDATE.getValue())) {
172
		if(fieldname.equals(SBMStaticFields.LASTMODIFIEDDATE.getValue())) {
144
			return String.valueOf(ttitem.getModifiedDate().getValue().toGregorianCalendar().getTimeInMillis());
173
			return String.valueOf(ttitem.getModifiedDate().getValue().toGregorianCalendar().getTimeInMillis());
145
		}
174
		}
146
		if(fieldname.equals("TITLE")) {
175
		if(fieldname.equals("TITLE")) {
147
			if(ttitem.getTitle()==null || ttitem.getTitle().isNil()) return "";
176
			if(ttitem.getTitle()==null || ttitem.getTitle().isNil()) return "";
148
			return ttitem.getTitle().getValue();
177
			return ttitem.getTitle().getValue();
149
		}
178
		}
150
		if(fieldname.equals(SBMStaticFields.ISSUEID.getValue())) {
179
		if(fieldname.equals(SBMStaticFields.ISSUEID.getValue())) {
151
			if(ttitem.getGenericItem()==null || ttitem.getGenericItem().getValue().getItemName()==null) {
180
			if(ttitem.getGenericItem()==null || ttitem.getGenericItem().getValue().getItemName()==null) {
152
				return "";
181
				return "";
153
			}
182
			}
154
			return ttitem.getGenericItem().getValue().getItemName().getValue();
183
			return ttitem.getGenericItem().getValue().getItemName().getValue();
155
		}
184
		}
156
		if(fieldname.equals("ISSUETYPE")) {
185
		if(fieldname.equals("ISSUETYPE")) {
157
			if(ttitem.getItemType()==null || ttitem.getItemType().isNil()) return "";
186
			if(ttitem.getItemType()==null || ttitem.getItemType().isNil()) return "";
158
			return ttitem.getItemType().getValue();
187
			return ttitem.getItemType().getValue();
159
		}
188
		}
160
		if(fieldname.equals(SBMStaticFields.STATE.getValue())) {
189
		if(fieldname.equals(SBMStaticFields.STATE.getValue())) {
161
			if(ttitem.getState()==null || ttitem.getState().isNil()) return "";
190
			if(ttitem.getState()==null || ttitem.getState().isNil()) return "";
162
			return ttitem.getState().getValue();
191
			return ttitem.getState().getValue();
163
		}
192
		}
164
		if(fieldname.equals(SBMStaticFields.ID.getValue())) {
193
		if(fieldname.equals(SBMStaticFields.ID.getValue())) {
165
			return ttitem.getGenericItem().getValue().getItemID().getValue();
194
			return ttitem.getGenericItem().getValue().getItemID().getValue();
166
		}
195
		}
167
		if(fieldname.equals(SBMStaticFields.PROJECTID.getValue())) {
196
		if(fieldname.equals(SBMStaticFields.PROJECTID.getValue())) {
168
			if(ttitem.getClassification() ==null || ttitem.getClassification().isNil()) return "";
197
			if(ttitem.getClassification() ==null || ttitem.getClassification().isNil()) return "";
169
			return ttitem.getClassification().getValue();
198
			return ttitem.getClassification().getValue();
170
		}
199
		}
171
		if(fieldname.equals(SBMStaticFields.PROJECTUUID.getValue())) {
200
		if(fieldname.equals(SBMStaticFields.PROJECTUUID.getValue())) {
172
			if(ttitem.getClassificationUUID()==null || ttitem.getClassificationUUID().isNil()) return "";
201
			if(ttitem.getClassificationUUID()==null || ttitem.getClassificationUUID().isNil()) return "";
173
			return ttitem.getClassificationUUID().getValue();
202
			return ttitem.getClassificationUUID().getValue();
174
		}
203
		}
175
		if(fieldname.equals("DESCRIPTION")) {
204
		if(fieldname.equals("DESCRIPTION")) {
176
			if(ttitem.getDescription() == null || ttitem.getDescription().isNil()) return "";
205
			if(ttitem.getDescription() == null || ttitem.getDescription().isNil()) return "";
177
			return ttitem.getDescription().getValue();
206
			return ttitem.getDescription().getValue();
178
		} 
207
		} 
179
		if(fieldname.equals(SBMStaticFields.SUBMITTER.getValue())) {
208
		if(fieldname.equals(SBMStaticFields.SUBMITTER.getValue())) {
180
			if(ttitem.getCreatedBy()==null || ttitem.getCreatedBy().isNil()) return "";
209
			if(ttitem.getCreatedBy()==null || ttitem.getCreatedBy().isNil()) return "";
181
			return ttitem.getCreatedBy().getValue();
210
			return ttitem.getCreatedBy().getValue();
182
		}
211
		}
183
		if(fieldname.equals(SBMStaticFields.SUBMITDATE.getValue())) {
212
		if(fieldname.equals(SBMStaticFields.SUBMITDATE.getValue())) {
184
			return String.valueOf(ttitem.getCreateDate().getValue().toGregorianCalendar().getTimeInMillis());
213
			return String.valueOf(ttitem.getCreateDate().getValue().toGregorianCalendar().getTimeInMillis());
185
		}
214
		}
186
		if(fieldname.equals(SBMStaticFields.LASTMODIFIER.getValue())) {
215
		if(fieldname.equals(SBMStaticFields.LASTMODIFIER.getValue())) {
187
			if(ttitem.getModifiedBy()==null || ttitem.getModifiedBy().isNil()) return "";
216
			if(ttitem.getModifiedBy()==null || ttitem.getModifiedBy().isNil()) return "";
188
			return ttitem.getModifiedBy().getValue();
217
			return ttitem.getModifiedBy().getValue();
189
		}
218
		}
190
		if(fieldname.equals(SBMStaticFields.LASTMODIFIEDDATE.getValue())) {
219
		if(fieldname.equals(SBMStaticFields.LASTMODIFIEDDATE.getValue())) {
191
			return String.valueOf(ttitem.getModifiedDate().getValue().toGregorianCalendar().getTimeInMillis());
220
			return String.valueOf(ttitem.getModifiedDate().getValue().toGregorianCalendar().getTimeInMillis());
192
		}
221
		}
193
		if(fieldname.equals(SBMStaticFields.ACTIVEINACTIVE.getValue())) {
222
		if(fieldname.equals(SBMStaticFields.ACTIVEINACTIVE.getValue())) {
194
			return ttitem.getActiveInactive().getValue();
223
			return ttitem.getActiveInactive().getValue();
195
		}
224
		}
196
		if(fieldname.equals(SBMStaticFields.OWNER.getValue())) {
225
		if(fieldname.equals(SBMStaticFields.OWNER.getValue())) {
197
			return ttitem.getOwner().getValue();
226
			return ttitem.getOwner().getValue();
198
		}
227
		}
199
		if(fieldname.equals(SBMStaticFields.ITEMURL.getValue())) {
228
		if(fieldname.equals(SBMStaticFields.ITEMURL.getValue())) {
200
			return ttitem.getUrl().getValue();
229
			return ttitem.getUrl().getValue();
201
		}
230
		}
202
		if(fieldname.equals(SBMStaticFields.UUID.getValue())) {
231
		if(fieldname.equals(SBMStaticFields.UUID.getValue())) {
203
			return ttitem.getGenericItem().getValue().getItemUUID().getValue();
232
			return ttitem.getGenericItem().getValue().getItemUUID().getValue();
204
		}
233
		}
205
		if(fieldname.equals(SBMStaticFields.CLOSEDATE.getValue())) {
234
		if(fieldname.equals(SBMStaticFields.CLOSEDATE.getValue())) {
206
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
235
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
207
			while (list.hasNext()) {
236
			while (list.hasNext()) {
208
				NameValue field = list.next();
237
				NameValue field = list.next();
209
				if(field.getName().getValue().equals("CLOSEDATE")) {
238
				if(field.getName().getValue().equals("CLOSEDATE")) {
210
					return field.getValue().getValue().getInternalValue().getValue();
239
					return field.getValue().getValue().getInternalValue().getValue();
211
				}
240
				}
212
			}
241
			}
213
		}
242
		}
214
		if(fieldname.equals(SBMStaticFields.LASTSTATECHANGEDATE.getValue())) {
243
		if(fieldname.equals(SBMStaticFields.LASTSTATECHANGEDATE.getValue())) {
215
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
244
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
216
			while (list.hasNext()) {
245
			while (list.hasNext()) {
217
				NameValue field = list.next();
246
				NameValue field = list.next();
218
				if(field.getName().getValue().equals("LASTSTATECHANGEDATE")) {
247
				if(field.getName().getValue().equals("LASTSTATECHANGEDATE")) {
219
					return field.getValue().getValue().getInternalValue().getValue();
248
					return field.getValue().getValue().getInternalValue().getValue();
220
				}
249
				}
221
			}
250
			}
222
		}
251
		}
223
		if(fieldname.equals(SBMStaticFields.SECONDARYOWNER.getValue())) {
252
		if(fieldname.equals(SBMStaticFields.SECONDARYOWNER.getValue())) {
224
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
253
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
225
			while (list.hasNext()) {
254
			while (list.hasNext()) {
226
				NameValue field = list.next();
255
				NameValue field = list.next();
227
				if(field.getName().getValue().equals("SECONDARYOWNER")) {
256
				if(field.getName().getValue().equals("SECONDARYOWNER")) {
228
					return field.getValue().getValue().getInternalValue().getValue();
257
					return field.getValue().getValue().getInternalValue().getValue();
229
				}
258
				}
230
			}
259
			}
231
		}
260
		}
232
		if(fieldname.equals(SBMStaticFields.LASTSTATECHANGER.getValue())) {
261
		if(fieldname.equals(SBMStaticFields.LASTSTATECHANGER.getValue())) {
233
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
262
			Iterator<NameValue> list = ttitem.getExtendedFieldList().iterator();
234
			while (list.hasNext()) {
263
			while (list.hasNext()) {
235
				NameValue field = list.next();
264
				NameValue field = list.next();
236
				if(field.getName().getValue().equals("LASTSTATECHANGER")) {
265
				if(field.getName().getValue().equals("LASTSTATECHANGER")) {
237
					return field.getValue().getValue().getDisplayValue().getValue();
266
					return field.getValue().getValue().getDisplayValue().getValue();
238
				}
267
				}
239
			}
268
			}
240
		}
269
		}
241
 
270
 
242
		return "UNKNOWN";
271
		return "UNKNOWN";
243
	}
272
	}
-
 
273
	
-
 
274
	/**
-
 
275
	 * Gets the field label. The SBM item is used to determine the table id of 
-
 
276
	 * the table where this field is in.
-
 
277
	 *
-
 
278
	 * @param ttitem the ttitem
-
 
279
	 * @param fieldname the fieldname
-
 
280
	 * @return the field label
244
	
281
	 */
245
	public String getFieldLabel(TTItem ttitem, String fieldname) {
282
	public String getFieldLabel(TTItem ttitem, String fieldname) {
246
		refreshTables();
283
		refreshTables();
247
		String itemid = ttitem.getGenericItem().getValue().getItemID().getValue();
284
		String itemid = ttitem.getGenericItem().getValue().getItemID().getValue();
248
		String tableid = new StringTokenizer(itemid, ":").nextToken();
285
		String tableid = new StringTokenizer(itemid, ":").nextToken();
249
		for (TableData table : tables) {
286
		for (TableData table : tables) {
250
			if (String.valueOf(table.getTableID().intValue()).equals(tableid)) {
287
			if (String.valueOf(table.getTableID().intValue()).equals(tableid)) {
251
				Iterator<Field> iter = table.getFieldList().iterator();
288
				Iterator<Field> iter = table.getFieldList().iterator();
252
				while(iter.hasNext()) {
289
				while(iter.hasNext()) {
253
					Field f = iter.next();
290
					Field f = iter.next();
254
					if(f.getName().getValue().equals(fieldname)) {
291
					if(f.getName().getValue().equals(fieldname)) {
255
						return f.getDisplayName().getValue();
292
						return f.getDisplayName().getValue();
256
					}
293
					}
257
				}
294
				}
258
				break;
295
				break;
259
			}
296
			}
260
		}
297
		}
261
	return "label_UNKNOWN";
298
	return "label_UNKNOWN";
262
	}
299
	}
263
	
300
	
264
	/**
301
	/**
265
	 * Gets the table database name.
302
	 * Gets the table database name.
266
	 *
303
	 *
267
	 * @param ttitem the ttitem
304
	 * @param ttitem the ttitem
268
	 * @return the table name or null in case table is not found
305
	 * @return the table name or null in case table is not found
269
	 */
306
	 */
270
	public String getTableName(TTItem ttitem) {
307
	public String getTableName(TTItem ttitem) {
271
		refreshTables();
308
		refreshTables();
272
		String itemid = ttitem.getGenericItem().getValue().getItemID().getValue();
309
		String itemid = ttitem.getGenericItem().getValue().getItemID().getValue();
273
		String tableid = new StringTokenizer(itemid, ":").nextToken();
310
		String tableid = new StringTokenizer(itemid, ":").nextToken();
274
		for (TableData table : tables) {
311
		for (TableData table : tables) {
275
			if (String.valueOf(table.getTableID().intValue()).equals(tableid)) {
312
			if (String.valueOf(table.getTableID().intValue()).equals(tableid)) {
276
				return table.getName().getValue();
313
				return table.getName().getValue();
277
			}
314
			}
278
		}
315
		}
279
		return null;
316
		return null;
280
	}
317
	}
-
 
318
	
-
 
319
	/**
-
 
320
	 * Gets the notes attached to a SBM item.
-
 
321
	 *
-
 
322
	 * @param ttitem the ttitem
-
 
323
	 * @return the notes
281
	
324
	 */
282
	public List<SBMNote> getNotes(TTItem ttitem) {
325
	public List<SBMNote> getNotes(TTItem ttitem) {
283
		List<SBMNote> notes = new ArrayList<SBMNote>();
326
		List<SBMNote> notes = new ArrayList<SBMNote>();
284
		Iterator<Note> iter = ttitem.getNoteList().iterator();
327
		Iterator<Note> iter = ttitem.getNoteList().iterator();
285
		while(iter.hasNext()) {
328
		while(iter.hasNext()) {
286
			Note n = iter.next();
329
			Note n = iter.next();
287
			SBMNote note = new SBMNote("sbm_user", 
330
			SBMNote note = new SBMNote("sbm_user", 
288
					n.getTitle().getValue()+"\n"+n.getNote().getValue(),
331
					n.getTitle().getValue()+"\n"+n.getNote().getValue(),
289
					n.getModificationDateTime().toGregorianCalendar().getTime(),
332
					n.getModificationDateTime().toGregorianCalendar().getTime(),
290
					n.getId().toString());
333
					n.getId().toString());
291
			notes.add(note);
334
			notes.add(note);
292
		}
335
		}
293
		return notes;
336
		return notes;
294
	}
337
	}
295
 
338
 
296
	
339
	
297
	/**
340
	/**
298
	 * Gets the names of all available primary tables.
341
	 * Gets the names of all available primary tables.
299
	 * A table name is a unique reference within one SBM environment, thus can be
342
	 * A table name is a unique reference within one SBM environment, thus can be
300
	 * used as a key.
343
	 * used as a key.
301
	 *
344
	 *
302
	 * @return the primary table names as a list
345
	 * @return the primary table names as a list
303
	 */
346
	 */
304
	public List<String> getPrimaryTables() {
347
	public List<String> getPrimaryTables() {
305
		refreshTables();
348
		refreshTables();
306
		List<String> table_names = new ArrayList<String>();
349
		List<String> table_names = new ArrayList<String>();
307
		for (TableData table : tables) {
350
		for (TableData table : tables) {
308
			table_names.add(table.getName().getValue());
351
			table_names.add(table.getName().getValue());
309
		}
352
		}
310
		return table_names;
353
		return table_names;
311
	}
354
	}
312
	
355
	
313
	/**
356
	/**
314
	 * Refresh table specifications from SBM web service. This
357
	 * Refresh table specifications from SBM web service. This
315
	 * is only done once per SBMClient instance.
358
	 * is only done once per SBMClient instance.
316
	 */
359
	 */
317
	private void refreshTables() {
360
	private void refreshTables() {
318
		if (tables.isEmpty()) {
361
		if (tables.isEmpty()) {
319
			try {
362
			try {
320
				//currently we limit this to primary tables
363
				//currently we limit this to primary tables
321
				tables = port.getTables(getAuth(), null, TableType.PRIMARY_TABLE);
364
				tables = port.getTables(getAuth(), null, TableType.PRIMARY_TABLE);
322
			} catch (AEWebservicesFaultFault e) {
365
			} catch (AEWebservicesFaultFault e) {
323
				new CoreException(
366
				new CoreException(
324
						RepositoryStatus.createInternalError(
367
						RepositoryStatus.createInternalError(
325
								SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e));
368
								SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e));
326
			}
369
			}
327
		}
370
		}
328
	}
371
	}
329
	
372
	
330
	/**
373
	/**
331
	 * Gets the fields for a primary table
374
	 * Gets the fields for a primary table
332
	 *
375
	 *
333
	 * @param tablename the table database name
376
	 * @param tablename the table database name
334
	 * @return the fields, empty when table does not exist
377
	 * @return the fields, empty when table does not exist
335
	 */
378
	 */
336
	public List<SBMField> getFields(String tablename) {
379
	public List<SBMField> getFields(String tablename) {
337
		refreshTables();
380
		refreshTables();
338
		List<SBMField> fields = new ArrayList<SBMField>();
381
		List<SBMField> fields = new ArrayList<SBMField>();
339
		for (TableData table : tables) {
382
		for (TableData table : tables) {
340
			if(table.getName().getValue().equals(tablename)) {
383
			if(table.getName().getValue().equals(tablename)) {
341
				Iterator<Field> iter = table.getFieldList().iterator();
384
				Iterator<Field> iter = table.getFieldList().iterator();
342
				while(iter.hasNext()) {
385
				while(iter.hasNext()) {
343
					Field f = iter.next();
386
					Field f = iter.next();
344
					SBMField nf = new SBMField(
387
					SBMField nf = new SBMField(
345
							SBMFieldTypes.fromValue(f.getFieldType().value()),
388
							SBMFieldTypes.fromValue(f.getFieldType().value()),
346
							tablename,
389
							tablename,
347
							f.getDisplayName().getValue(),
390
							f.getDisplayName().getValue(),
348
							f.getName().getValue());
391
							f.getName().getValue());
349
					fields.add(nf);
392
					fields.add(nf);
350
				}
393
				}
351
				break;
394
				break;
352
			}
395
			}
353
		}
396
		}
354
		return fields;
397
		return fields;
355
	}
398
	}
356
	
399
	
357
	/**
400
	/**
358
	 * Gets the field value for custom defined field.
401
	 * Gets the field value for custom defined field.
359
	 * (those from &lt;extendedFieldList&gt;)
402
	 * (those from &lt;extendedFieldList&gt;)
360
	 *
403
	 *
361
	 * @param ttitem the ttitem
404
	 * @param ttitem the ttitem
362
	 * @param fieldname the fieldname
405
	 * @param fieldname the fieldname
363
	 * @return the field value or null if the field is not found
406
	 * @return the field value or null if the field is not found
364
	 */
407
	 */
365
	public SBMFieldValue getFieldValue(TTItem ttitem, String fieldname) {
408
	public SBMFieldValue getFieldValue(TTItem ttitem, String fieldname) {
366
		SBMFieldValue value;
409
		SBMFieldValue value;
367
		Iterator<NameValue> fs = ttitem.getExtendedFieldList().iterator();
410
		Iterator<NameValue> fs = ttitem.getExtendedFieldList().iterator();
368
		while(fs.hasNext()) {
411
		while(fs.hasNext()) {
369
			NameValue nv = fs.next();
412
			NameValue nv = fs.next();
370
			if(nv.getName().getValue().equals(fieldname)) {
413
			if(nv.getName().getValue().equals(fieldname)) {
371
				if (nv.getValue()!=null && !nv.getValue().isNil()) {
414
				if (nv.getValue()!=null && !nv.getValue().isNil()) {
372
					value = new SBMFieldValue(
415
					value = new SBMFieldValue(
373
							nv.getValue().getValue().getInternalValue().getValue(),
416
							nv.getValue().getValue().getInternalValue().getValue(),
374
							nv.getValue().getValue().getDisplayValue().getValue());
417
							nv.getValue().getValue().getDisplayValue().getValue());
375
					return value;
418
					return value;
376
				}
419
				}
377
			}
420
			}
378
		}
421
		}
379
		return null;
422
		return null;
380
	}
423
	}
381
	
424
	
382
	/**
425
	/**
383
	 * Gets the field values for custom defined, multi type field.
426
	 * Gets the field values for custom defined, multi type field.
384
	 * (those from &lt;extendedFieldList&gt;)
427
	 * (those from &lt;extendedFieldList&gt;)
385
	 *
428
	 *
386
	 * @param ttitem the ttitem
429
	 * @param ttitem the ttitem
387
	 * @param fieldname the fieldname
430
	 * @param fieldname the fieldname
388
	 * @return the list of field values
431
	 * @return the list of field values
389
	 */
432
	 */
390
	public List<SBMFieldValue> getFieldValues(TTItem ttitem, String fieldname) {
433
	public List<SBMFieldValue> getFieldValues(TTItem ttitem, String fieldname) {
391
		List<SBMFieldValue> values = new ArrayList<SBMFieldValue>();
434
		List<SBMFieldValue> values = new ArrayList<SBMFieldValue>();
392
		Iterator<NameValue> fs = ttitem.getExtendedFieldList().iterator();
435
		Iterator<NameValue> fs = ttitem.getExtendedFieldList().iterator();
393
		while(fs.hasNext()) {
436
		while(fs.hasNext()) {
394
			NameValue nv = fs.next();
437
			NameValue nv = fs.next();
395
			if(nv.getName().getValue().equals(fieldname)) {
438
			if(nv.getName().getValue().equals(fieldname)) {
396
				if (nv.getValues()!=null && !nv.getValues().isEmpty()) {
439
				if (nv.getValues()!=null && !nv.getValues().isEmpty()) {
397
					Iterator<Value> nvv = nv.getValues().iterator();
440
					Iterator<Value> nvv = nv.getValues().iterator();
398
					while(nvv.hasNext()) {
441
					while(nvv.hasNext()) {
399
						Value nvv_value = nvv.next();			
442
						Value nvv_value = nvv.next();			
400
						SBMFieldValue value = new SBMFieldValue(
443
						SBMFieldValue value = new SBMFieldValue(
401
							nvv_value.getInternalValue().getValue(),
444
							nvv_value.getInternalValue().getValue(),
402
							nvv_value.getDisplayValue().getValue());
445
							nvv_value.getDisplayValue().getValue());
403
						values.add(value);
446
						values.add(value);
404
					}
447
					}
405
					return values;
448
					return values;
406
				}
449
				}
407
			}
450
			}
408
		}
451
		}
409
		return values;
452
		return values;
410
	}
453
	}
411
}
454
}