Subversion Repositories XServices

Rev

Rev 90 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 brianR 1
/*
2
 *   Mylyn Connector for Serena Business Mashups
3
 * 	 Copyright 2010 Brian Rosenberger (Brutex Network)
4
 *
5
 *   Licensed under the Apache License, Version 2.0 (the "License");
6
 *   you may not use this file except in compliance with the License.
7
 *   You may obtain a copy of the License at
8
 *
9
 *       http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 *   Unless required by applicable law or agreed to in writing, software
12
 *   distributed under the License is distributed on an "AS IS" BASIS,
13
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 *   See the License for the specific language governing permissions and
15
 *   limitations under the License.
16
 *
17
 *   Serena, TeamTrack and Serena Business Mashup are
18
 * 	 registered trademarks of SERENA Software Inc.
19
 */
20
package net.brutex.mylyn.sbmconnector.ui;
21
 
39 brianR 22
import java.util.List;
23
 
30 brianR 24
import net.brutex.mylyn.sbmconnector.SBMConnectorPlugin;
25
import net.brutex.mylyn.sbmconnector.core.SBMClient;
39 brianR 26
import net.brutex.mylyn.sbmconnector.core.SBMRepositoryConnector;
27
import net.brutex.mylyn.sbmconnector.core.model.SBMField;
28
import net.brutex.mylyn.sbmconnector.core.model.SBMSystemFields;
30 brianR 29
 
30
import org.eclipse.core.runtime.CoreException;
31
import org.eclipse.core.runtime.IProgressMonitor;
39 brianR 32
import org.eclipse.jface.dialogs.IMessageProvider;
30 brianR 33
import org.eclipse.mylyn.tasks.core.TaskRepository;
34
import org.eclipse.mylyn.tasks.ui.wizards.AbstractRepositorySettingsPage;
39 brianR 35
import org.eclipse.swt.SWT;
36
import org.eclipse.swt.layout.GridData;
37
import org.eclipse.swt.layout.GridLayout;
38
import org.eclipse.swt.widgets.Combo;
30 brianR 39
import org.eclipse.swt.widgets.Composite;
39 brianR 40
import org.eclipse.swt.widgets.Event;
41
import org.eclipse.swt.widgets.Label;
42
import org.eclipse.swt.widgets.Listener;
43
import org.eclipse.swt.widgets.Table;
44
import org.eclipse.swt.widgets.TableColumn;
45
import org.eclipse.swt.widgets.TableItem;
46
import org.eclipse.ui.forms.widgets.ExpandableComposite;
47
import org.eclipse.ui.forms.widgets.FormToolkit;
30 brianR 48
 
49
public class SBMRepositorySettingsPage extends AbstractRepositorySettingsPage {
50
 
39 brianR 51
	private Combo table;
52
	private Table list;
53
 
54
	private String solutionTable = "";
55
	private String solutionTableList = "";
56
	private String tableFieldList = "";
57
	private String tableFullFieldList = "";
58
 
59
	public static final String STRING_SOLUTIONTABLE = "solutionTable";
60
	public static final String STRING_SOLUTIONTABLELIST = "solutionTableList";
61
	public static final String STRING_TABLEFIELDLIST = "tableFieldList";
62
	public static final String STRING_TABLEFULLFIELDLIST = "tableFullFieldList";
63
 
30 brianR 64
	public SBMRepositorySettingsPage(String title, String description,
65
			TaskRepository taskRepository) {
66
		super(title, description, taskRepository);
39 brianR 67
		setNeedsAdvanced(false);
68
		setNeedsValidation(true);
69
		setNeedsEncoding(false);
70
		// initialize table and table list if not null
71
		if (taskRepository.hasProperty(STRING_SOLUTIONTABLE)) {
72
			this.solutionTable = taskRepository
73
					.getProperty(STRING_SOLUTIONTABLE);
74
		}
75
		if (taskRepository.hasProperty(STRING_SOLUTIONTABLELIST)) {
76
			this.solutionTableList = taskRepository
77
					.getProperty(STRING_SOLUTIONTABLELIST);
78
		}
79
		if (taskRepository.hasProperty(STRING_TABLEFIELDLIST)) {
80
			this.tableFieldList = taskRepository
81
					.getProperty(STRING_TABLEFIELDLIST);
82
		}
83
		if (taskRepository.hasProperty(STRING_TABLEFULLFIELDLIST)) {
84
			this.tableFullFieldList = taskRepository
85
					.getProperty(STRING_TABLEFULLFIELDLIST);
86
		}
30 brianR 87
	}
88
 
89
	@Override
39 brianR 90
	public void applyTo(TaskRepository repository) {
91
		super.applyTo(repository);
92
		if (!solutionTable.isEmpty()) {
93
			repository.setProperty(STRING_SOLUTIONTABLE, solutionTable);
94
			repository.setProperty(STRING_SOLUTIONTABLELIST, solutionTableList);
95
			String fieldlist = ",";
96
			String fullfieldlist = ",";
97
			for (TableItem t : list.getItems()) {
98
				if (t.getChecked())
99
					fieldlist = fieldlist + t.getText(2) + ",";
100
				fullfieldlist = fullfieldlist + t.getText(2) + ",";
101
			}
102
			repository.setProperty(STRING_TABLEFIELDLIST, fieldlist);
103
			repository.setProperty(STRING_TABLEFULLFIELDLIST, fullfieldlist);
104
		}
105
	}
30 brianR 106
 
39 brianR 107
	@Override
108
	public boolean isPageComplete() {
90 brianR 109
		if (table != null
110
				&& table.getText().length() > 4
111
				&& getUserName() != null
112
				&& getUserName().length() > 0) {
113
			return true;
39 brianR 114
		}
115
		return false;
30 brianR 116
	}
117
 
118
	@Override
39 brianR 119
	protected void createContributionControls(Composite parentControl) {
120
		ExpandableComposite expander = createSection(parentControl,
121
				"Mashup application");
122
		FormToolkit toolkit = new FormToolkit(parentControl.getDisplay());
123
		Composite control = toolkit.createComposite(expander, SWT.NONE);
124
		GridLayout gridLayout = new GridLayout();
125
		gridLayout.numColumns = 2;
126
		gridLayout.verticalSpacing = 5;
127
		gridLayout.marginWidth = 0;
128
		control.setLayout(gridLayout);
129
		control.setBackground(parentControl.getBackground());
130
 
131
		Label label = new Label(control, SWT.NONE);
132
		label.setText("Table:");
133
 
134
		table = new Combo(control, SWT.SINGLE | SWT.BORDER);
135
		Label label_list = new Label(control, SWT.NONE | SWT.TOP);
136
		label_list.setText("Fields:");
137
		list = new Table(control, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL
138
				| SWT.H_SCROLL | SWT.FULL_SELECTION | SWT.CHECK);
139
		list.setLinesVisible(true);
140
		list.setHeaderVisible(true);
141
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
142
		data.heightHint = 200;
143
		list.setLayoutData(data);
144
		String[] titles = { " ", "Display Name", "DBName", "Type" };
145
		for (int i = 0; i < titles.length; i++) {
146
			TableColumn column = new TableColumn(list, SWT.NONE);
147
			column.setText(titles[i]);
148
		}
149
		list.addListener(SWT.Selection, new Listener() {
150
			@Override
151
			public void handleEvent(Event event) {
152
				//avoid that a system field may be unchecked
153
				try {
154
					SBMSystemFields
155
							.valueOf(((TableItem) event.item).getText(2));
156
					((TableItem) event.item).setChecked(true);
157
				} catch (IllegalArgumentException e) {
158
					// do nothing
159
				}
160
			}
161
		});
162
		table.addListener(SWT.Selection, new Listener() {
163
			@Override
164
			public void handleEvent(Event arg0) {
165
				// when the table has been changed
166
				// try to re-read the field list
167
				tableFieldList = "";
168
				tableFullFieldList = "";
169
				try {
170
					SBMField[] fields = SBMRepositoryConnector.getClient(
171
						createTaskRepository()).getFields(table.getText())
172
						.toArray(new SBMField[0]);
173
				fillList(list, fields);
174
				} catch (Exception e) { }
175
				if (getWizard() != null) {
176
					solutionTable = table.getText();
177
					getWizard().getContainer().updateButtons();
178
				}
179
			}
180
 
181
		});
182
		regenerateTables();
183
		expander.setClient(control);
184
	}
185
 
186
	@Override
30 brianR 187
	public String getConnectorKind() {
188
		return SBMConnectorPlugin.CONNECTOR_KIND;
189
	}
190
 
191
	@Override
192
	protected Validator getValidator(TaskRepository repository) {
193
		return new SBMConnectionValidator(repository);
194
	}
195
 
196
	@Override
197
	protected boolean isValidUrl(String url) {
39 brianR 198
		if (url.contains("<server>"))
199
			return false;
30 brianR 200
		return true;
201
	}
39 brianR 202
 
30 brianR 203
	@Override
204
	protected void applyValidatorResult(Validator validator) {
205
		if (((SBMConnectionValidator) validator).getResult() != null) {
39 brianR 206
			setMessage("Validate first!", IMessageProvider.ERROR);
207
			setNeedsValidation(true);
208
		} else {
209
			regenerateTables();
30 brianR 210
		}
211
	}
39 brianR 212
 
213
	private void regenerateTables() {
214
		try {
215
			List<String> tables = SBMRepositoryConnector.getClient(
216
					createTaskRepository()).getPrimaryTables();
217
			for (String table_name : tables) {
218
				table.add(table_name);
219
				if (solutionTable.equals(table_name)) {
220
					table.setText(table_name);
221
				}
222
			}
223
		} catch (Exception e) {
224
			// it was not possible to read the list of tables
225
			// from the server, so just restore saved settings
226
			table.add(solutionTable);
227
			table.setText(solutionTable);
228
		}
229
		try {
230
		SBMField[] fields = SBMRepositoryConnector.getClient(
231
				createTaskRepository()).getFields(table.getText()).toArray(
232
				new SBMField[0]);
233
				fillList(list, fields);
234
		} catch (Exception e) {
235
			// it was not possible to read the fields for the given table
236
			// do nothing
237
		}
238
 
239
	}
240
 
30 brianR 241
	protected class SBMConnectionValidator extends Validator {
242
		private TaskRepository repository;
243
		private String result = null;
39 brianR 244
 
30 brianR 245
		protected SBMConnectionValidator(TaskRepository repository) {
39 brianR 246
			this.repository = repository;
30 brianR 247
		}
39 brianR 248
 
30 brianR 249
		@Override
250
		public void run(IProgressMonitor monitor) throws CoreException {
39 brianR 251
			monitor.beginTask("Prüfe Verbindung", IProgressMonitor.UNKNOWN);
30 brianR 252
			SBMClient client = new SBMClient(repository);
39 brianR 253
			if (!client.canAuthenticate()) {
30 brianR 254
				result = "Verbindung fehlgeschlagen";
39 brianR 255
			} else {
256
				result = null;
30 brianR 257
			}
258
			monitor.done();
259
		}
39 brianR 260
 
261
		public String getResult() {
262
			return result;
263
		}
30 brianR 264
	}
39 brianR 265
 
266
	private void fillList(Table list, SBMField[] fields) {
267
		list.removeAll();
268
		for (int i = 0; i < fields.length; i++) {
269
			TableItem item = new TableItem(list, 0);
270
			item.setText(0, "");
271
			item.setText(1, fields[i].getLabel());
272
			item.setText(2, fields[i].getName());
273
			item.setText(3, fields[i].getType().getValue().replaceFirst(
274
					"FLDTYPE-", ""));
275
			try {
276
				// auto-select all system fields
277
				SBMSystemFields.valueOf(fields[i].getName());
278
				item.setChecked(true);
279
			} catch (IllegalArgumentException e) {
280
				// do nothing
281
			}
282
			if (tableFieldList.contains(","+fields[i].getName()+",")) {
283
				// item has been checked before
284
				// so check it
285
				item.setChecked(true);
286
			}
287
		}
288
 
289
		for (TableColumn c : list.getColumns()) {
290
			c.pack();
291
		}
292
	}
293
 
294
	@Override
295
	protected void createAdditionalControls(Composite arg0) {
296
		// TODO Auto-generated method stub
297
 
298
	}
299
 
30 brianR 300
}