Subversion Repositories XServices

Rev

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

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