Subversion Repositories XServices

Rev

Rev 42 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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