Subversion Repositories XServices

Rev

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

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