Subversion Repositories XServices

Rev

Rev 39 | Go to most recent revision | 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() {
109
		if (table != null && table.getText().length() > 4) {
110
			return super.isPageComplete();
111
		}
112
		return false;
30 brianR 113
	}
114
 
115
	@Override
39 brianR 116
	protected void createContributionControls(Composite parentControl) {
117
		ExpandableComposite expander = createSection(parentControl,
118
				"Mashup application");
119
		FormToolkit toolkit = new FormToolkit(parentControl.getDisplay());
120
		Composite control = toolkit.createComposite(expander, SWT.NONE);
121
		GridLayout gridLayout = new GridLayout();
122
		gridLayout.numColumns = 2;
123
		gridLayout.verticalSpacing = 5;
124
		gridLayout.marginWidth = 0;
125
		control.setLayout(gridLayout);
126
		control.setBackground(parentControl.getBackground());
127
 
128
		Label label = new Label(control, SWT.NONE);
129
		label.setText("Table:");
130
 
131
		table = new Combo(control, SWT.SINGLE | SWT.BORDER);
132
		Label label_list = new Label(control, SWT.NONE | SWT.TOP);
133
		label_list.setText("Fields:");
134
		list = new Table(control, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL
135
				| SWT.H_SCROLL | SWT.FULL_SELECTION | SWT.CHECK);
136
		list.setLinesVisible(true);
137
		list.setHeaderVisible(true);
138
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
139
		data.heightHint = 200;
140
		list.setLayoutData(data);
141
		String[] titles = { " ", "Display Name", "DBName", "Type" };
142
		for (int i = 0; i < titles.length; i++) {
143
			TableColumn column = new TableColumn(list, SWT.NONE);
144
			column.setText(titles[i]);
145
		}
146
		list.addListener(SWT.Selection, new Listener() {
147
			@Override
148
			public void handleEvent(Event event) {
149
				//avoid that a system field may be unchecked
150
				try {
151
					SBMSystemFields
152
							.valueOf(((TableItem) event.item).getText(2));
153
					((TableItem) event.item).setChecked(true);
154
				} catch (IllegalArgumentException e) {
155
					// do nothing
156
				}
157
			}
158
		});
159
		table.addListener(SWT.Selection, new Listener() {
160
			@Override
161
			public void handleEvent(Event arg0) {
162
				// when the table has been changed
163
				// try to re-read the field list
164
				tableFieldList = "";
165
				tableFullFieldList = "";
166
				try {
167
					SBMField[] fields = SBMRepositoryConnector.getClient(
168
						createTaskRepository()).getFields(table.getText())
169
						.toArray(new SBMField[0]);
170
				fillList(list, fields);
171
				} catch (Exception e) { }
172
				if (getWizard() != null) {
173
					solutionTable = table.getText();
174
					getWizard().getContainer().updateButtons();
175
				}
176
			}
177
 
178
		});
179
		regenerateTables();
180
		expander.setClient(control);
181
	}
182
 
183
	@Override
30 brianR 184
	public String getConnectorKind() {
185
		return SBMConnectorPlugin.CONNECTOR_KIND;
186
	}
187
 
188
	@Override
189
	protected Validator getValidator(TaskRepository repository) {
190
		return new SBMConnectionValidator(repository);
191
	}
192
 
193
	@Override
194
	protected boolean isValidUrl(String url) {
39 brianR 195
		if (url.contains("<server>"))
196
			return false;
30 brianR 197
		return true;
198
	}
39 brianR 199
 
30 brianR 200
	@Override
201
	protected void applyValidatorResult(Validator validator) {
202
		if (((SBMConnectionValidator) validator).getResult() != null) {
39 brianR 203
			setMessage("Validate first!", IMessageProvider.ERROR);
204
			setNeedsValidation(true);
205
		} else {
206
			regenerateTables();
30 brianR 207
		}
208
	}
39 brianR 209
 
210
	private void regenerateTables() {
211
		try {
212
			List<String> tables = SBMRepositoryConnector.getClient(
213
					createTaskRepository()).getPrimaryTables();
214
			for (String table_name : tables) {
215
				table.add(table_name);
216
				if (solutionTable.equals(table_name)) {
217
					table.setText(table_name);
218
				}
219
			}
220
		} catch (Exception e) {
221
			// it was not possible to read the list of tables
222
			// from the server, so just restore saved settings
223
			table.add(solutionTable);
224
			table.setText(solutionTable);
225
		}
226
		try {
227
		SBMField[] fields = SBMRepositoryConnector.getClient(
228
				createTaskRepository()).getFields(table.getText()).toArray(
229
				new SBMField[0]);
230
				fillList(list, fields);
231
		} catch (Exception e) {
232
			// it was not possible to read the fields for the given table
233
			// do nothing
234
		}
235
 
236
	}
237
 
30 brianR 238
	protected class SBMConnectionValidator extends Validator {
239
		private TaskRepository repository;
240
		private String result = null;
39 brianR 241
 
30 brianR 242
		protected SBMConnectionValidator(TaskRepository repository) {
39 brianR 243
			this.repository = repository;
30 brianR 244
		}
39 brianR 245
 
30 brianR 246
		@Override
247
		public void run(IProgressMonitor monitor) throws CoreException {
39 brianR 248
			monitor.beginTask("Prüfe Verbindung", IProgressMonitor.UNKNOWN);
30 brianR 249
			SBMClient client = new SBMClient(repository);
39 brianR 250
			if (!client.canAuthenticate()) {
30 brianR 251
				result = "Verbindung fehlgeschlagen";
39 brianR 252
			} else {
253
				result = null;
30 brianR 254
			}
255
			monitor.done();
256
		}
39 brianR 257
 
258
		public String getResult() {
259
			return result;
260
		}
30 brianR 261
	}
39 brianR 262
 
263
	private void fillList(Table list, SBMField[] fields) {
264
		list.removeAll();
265
		for (int i = 0; i < fields.length; i++) {
266
			TableItem item = new TableItem(list, 0);
267
			item.setText(0, "");
268
			item.setText(1, fields[i].getLabel());
269
			item.setText(2, fields[i].getName());
270
			item.setText(3, fields[i].getType().getValue().replaceFirst(
271
					"FLDTYPE-", ""));
272
			try {
273
				// auto-select all system fields
274
				SBMSystemFields.valueOf(fields[i].getName());
275
				item.setChecked(true);
276
			} catch (IllegalArgumentException e) {
277
				// do nothing
278
			}
279
			if (tableFieldList.contains(","+fields[i].getName()+",")) {
280
				// item has been checked before
281
				// so check it
282
				item.setChecked(true);
283
			}
284
		}
285
 
286
		for (TableColumn c : list.getColumns()) {
287
			c.pack();
288
		}
289
	}
290
 
291
	@Override
292
	protected void createAdditionalControls(Composite arg0) {
293
		// TODO Auto-generated method stub
294
 
295
	}
296
 
30 brianR 297
}