/
ckanDialog.java
executable file
·467 lines (386 loc) · 15.8 KB
/
ckanDialog.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package org.ckan;
import java.text.Normalizer;
import java.text.Normalizer.Form;
import java.util.Locale;
import java.util.regex.Pattern;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.pentaho.di.core.Const;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.ui.trans.step.BaseStepDialog;
import org.pentaho.di.trans.step.BaseStepMeta;
import org.pentaho.di.trans.step.StepDialogInterface;
public class ckanDialog extends BaseStepDialog implements StepDialogInterface
{
private ckanMeta input;
private Group ckanConnection;
private FormData fdckanConnection;
private Label wlDomain;
private Text wDomain;
private FormData fdlDomain, fdDomain;
private Label wlApiKey;
private Text wApiKey;
private FormData fdlApiKey, fdApiKey;
private Group ResourceDetails;
private FormData fdResourceDetails;
private Label wlPackageId;
private Text wPackageId;
private FormData fdlPackageId, fdPackageId;
private Label wlTitle;
private Text wTitle;
private FormData fdlTitle, fdTitle;
private Label wlDescription;
private Text wDescription;
private FormData fdlDescription, fdDescription;
private Label wlResourceId;
private Text wResourceId;
private FormData fdlResourceId, fdResourceId;
private Group AdvancedSettings;
private FormData fdAdvancedSettings;
private Label wlBatchSize;
private Text wBatchSize;
private FormData fdlBatchSize, fdBatchSize;
private Label wlPrimaryKey;
private Text wPrimaryKey;
private FormData fdlPrimaryKey, fdPrimaryKey;
private static final Pattern NONLATIN = Pattern.compile("[^\\w-]");
private static final Pattern WHITESPACE = Pattern.compile("[\\s]");
public ckanDialog(Shell parent, Object in, TransMeta transMeta, String sname)
{
super(parent, (BaseStepMeta)in, transMeta, sname);
input=(ckanMeta)in;
}
public String open()
{
Shell parent = getParent();
Display display = parent.getDisplay();
shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MIN | SWT.MAX);
props.setLook( shell );
setShellImage(shell, input);
ModifyListener lsMod = new ModifyListener() {
public void modifyText(ModifyEvent e) {
input.setChanged();
}
};
changed = input.hasChanged();
FormLayout formLayout = new FormLayout ();
formLayout.marginWidth = Const.FORM_MARGIN;
formLayout.marginHeight = Const.FORM_MARGIN;
shell.setLayout(formLayout);
shell.setText(Messages.getString("ckanDialog.Shell.Title")); //$NON-NLS-1$
int middle = props.getMiddlePct()-10;
int margin = Const.MARGIN;
// Stepname line
wlStepname=new Label(shell, SWT.RIGHT);
wlStepname.setText(Messages.getString("ckanDialog.StepName.Label")); //$NON-NLS-1$
props.setLook( wlStepname );
fdlStepname=new FormData();
fdlStepname.left = new FormAttachment(0, 0);
fdlStepname.right= new FormAttachment(middle, -margin);
fdlStepname.top = new FormAttachment(0, margin);
wlStepname.setLayoutData(fdlStepname);
wStepname=new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
wStepname.setText(stepname);
props.setLook( wStepname );
wStepname.addModifyListener(lsMod);
fdStepname=new FormData();
fdStepname.left = new FormAttachment(middle, 0);
fdStepname.right= new FormAttachment(100, 0);
fdStepname.top = new FormAttachment(0, margin);
wStepname.setLayoutData(fdStepname);
// CKAN Connection Group
ckanConnection = new Group(shell, SWT.NONE);
ckanConnection.setText("CKAN Connection");
fdckanConnection=new FormData();
fdckanConnection.left = new FormAttachment(0, 0);
fdckanConnection.right= new FormAttachment(100, 0);
fdckanConnection.top = new FormAttachment(wStepname, margin+20);
ckanConnection.setLayoutData(fdckanConnection);
formLayout = new FormLayout ();
formLayout.marginWidth = Const.FORM_MARGIN;
formLayout.marginHeight = Const.FORM_MARGIN;
ckanConnection.setLayout(formLayout);
// Domain line
wlDomain=new Label(ckanConnection, SWT.RIGHT);
wlDomain.setText(Messages.getString("ckanDialog.Domain.Label")); //$NON-NLS-1$
fdlDomain=new FormData();
fdlDomain.left = new FormAttachment(0, 0);
fdlDomain.right= new FormAttachment(middle, -margin);
fdlDomain.top = new FormAttachment(0, margin);
wlDomain.setLayoutData(fdlDomain);
wDomain=new Text(ckanConnection, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
wDomain.addModifyListener(lsMod);
fdDomain=new FormData();
fdDomain.left = new FormAttachment(middle, 0);
fdDomain.right= new FormAttachment(100, 0);
fdDomain.top = new FormAttachment(0, margin);
wDomain.setLayoutData(fdDomain);
// ApiKey line
wlApiKey=new Label(ckanConnection, SWT.RIGHT);
wlApiKey.setText(Messages.getString("ckanDialog.ApiKey.Label")); //$NON-NLS-1$
fdlApiKey=new FormData();
fdlApiKey.left = new FormAttachment(0, 0);
fdlApiKey.right= new FormAttachment(middle, -margin);
fdlApiKey.top = new FormAttachment(wDomain, margin);
wlApiKey.setLayoutData(fdlApiKey);
wApiKey=new Text(ckanConnection, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
wApiKey.addModifyListener(lsMod);
fdApiKey=new FormData();
fdApiKey.left = new FormAttachment(middle, 0);
fdApiKey.right= new FormAttachment(100, 0);
fdApiKey.top = new FormAttachment(wDomain, margin);
wApiKey.setLayoutData(fdApiKey);
// Resource Details Group
ResourceDetails = new Group(shell, SWT.NONE);
ResourceDetails.setText("Resource Details");
fdResourceDetails=new FormData();
fdResourceDetails.left = new FormAttachment(0, 0);
fdResourceDetails.right= new FormAttachment(100, 0);
fdResourceDetails.top = new FormAttachment(ckanConnection, margin+20);
ResourceDetails.setLayoutData(fdResourceDetails);
formLayout = new FormLayout ();
formLayout.marginWidth = Const.FORM_MARGIN;
formLayout.marginHeight = Const.FORM_MARGIN;
ResourceDetails.setLayout(formLayout);
// PackageId line
wlPackageId=new Label(ResourceDetails, SWT.RIGHT);
wlPackageId.setText(Messages.getString("ckanDialog.PackageId.Label")); //$NON-NLS-1$
fdlPackageId=new FormData();
fdlPackageId.left = new FormAttachment(0, 0);
fdlPackageId.right= new FormAttachment(middle, -margin);
fdlPackageId.top = new FormAttachment(0, margin);
wlPackageId.setLayoutData(fdlPackageId);
wPackageId=new Text(ResourceDetails, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
wPackageId.addModifyListener(lsMod);
fdPackageId=new FormData();
fdPackageId.left = new FormAttachment(middle, 0);
fdPackageId.right= new FormAttachment(100, 0);
fdPackageId.top = new FormAttachment(0, margin);
wPackageId.setLayoutData(fdPackageId);
// Title line
wlTitle=new Label(ResourceDetails, SWT.RIGHT);
wlTitle.setText(Messages.getString("ckanDialog.Title.Label")); //$NON-NLS-1$
fdlTitle=new FormData();
fdlTitle.left = new FormAttachment(0, 0);
fdlTitle.right= new FormAttachment(middle, -margin);
fdlTitle.top = new FormAttachment(wPackageId, margin);
wlTitle.setLayoutData(fdlTitle);
wTitle=new Text(ResourceDetails, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
wTitle.addModifyListener(lsMod);
fdTitle=new FormData();
fdTitle.left = new FormAttachment(middle, 0);
fdTitle.right= new FormAttachment(100, 0);
fdTitle.top = new FormAttachment(wPackageId, margin);
wTitle.setLayoutData(fdTitle);
// Description line
wlDescription=new Label(ResourceDetails, SWT.RIGHT);
wlDescription.setText(Messages.getString("ckanDialog.Description.Label")); //$NON-NLS-1$
fdlDescription=new FormData();
fdlDescription.left = new FormAttachment(0, 0);
fdlDescription.right= new FormAttachment(middle, -margin);
fdlDescription.top = new FormAttachment(wTitle, margin);
wlDescription.setLayoutData(fdlDescription);
wDescription=new Text(ResourceDetails, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
wDescription.addModifyListener(lsMod);
fdDescription=new FormData();
fdDescription.left = new FormAttachment(middle, 0);
fdDescription.right= new FormAttachment(100, 0);
fdDescription.top = new FormAttachment(wTitle, margin);
wDescription.setLayoutData(fdDescription);
// ResourceId line
wlResourceId=new Label(ResourceDetails, SWT.RIGHT);
wlResourceId.setText(Messages.getString("ckanDialog.ResourceId.Label")); //$NON-NLS-1$
fdlResourceId=new FormData();
fdlResourceId.left = new FormAttachment(0, 0);
fdlResourceId.right= new FormAttachment(middle, -margin);
fdlResourceId.top = new FormAttachment(wDescription, margin);
wlResourceId.setLayoutData(fdlResourceId);
wResourceId=new Text(ResourceDetails, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
wResourceId.addModifyListener(lsMod);
fdResourceId=new FormData();
fdResourceId.left = new FormAttachment(middle, 0);
fdResourceId.right= new FormAttachment(100, 0);
fdResourceId.top = new FormAttachment(wDescription, margin);
wResourceId.setLayoutData(fdResourceId);
// Advanced Settings Group
AdvancedSettings = new Group(shell, SWT.NONE);
AdvancedSettings.setText("Advanced Settings");
fdAdvancedSettings=new FormData();
fdAdvancedSettings.left = new FormAttachment(0, 0);
fdAdvancedSettings.right= new FormAttachment(100, 0);
fdAdvancedSettings.top = new FormAttachment(ResourceDetails, margin+20);
AdvancedSettings.setLayoutData(fdAdvancedSettings);
formLayout = new FormLayout ();
formLayout.marginWidth = Const.FORM_MARGIN;
formLayout.marginHeight = Const.FORM_MARGIN;
AdvancedSettings.setLayout(formLayout);
// BatchSize line
wlBatchSize=new Label(AdvancedSettings, SWT.RIGHT);
wlBatchSize.setText(Messages.getString("ckanDialog.BatchSize.Label")); //$NON-NLS-1$
fdlBatchSize=new FormData();
fdlBatchSize.left = new FormAttachment(0, 0);
fdlBatchSize.right= new FormAttachment(middle, -margin);
fdlBatchSize.top = new FormAttachment(0, margin);
wlBatchSize.setLayoutData(fdlBatchSize);
wBatchSize=new Text(AdvancedSettings, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
wBatchSize.addModifyListener(lsMod);
fdBatchSize=new FormData();
fdBatchSize.left = new FormAttachment(middle, 0);
fdBatchSize.right= new FormAttachment(100, 0);
fdBatchSize.top = new FormAttachment(0, margin);
wBatchSize.setLayoutData(fdBatchSize);
// PrimaryKey line
wlPrimaryKey=new Label(AdvancedSettings, SWT.RIGHT);
wlPrimaryKey.setText(Messages.getString("ckanDialog.PrimaryKey.Label")); //$NON-NLS-1$
fdlPrimaryKey=new FormData();
fdlPrimaryKey.left = new FormAttachment(0, 0);
fdlPrimaryKey.right= new FormAttachment(middle, -margin);
fdlPrimaryKey.top = new FormAttachment(wBatchSize, margin);
wlPrimaryKey.setLayoutData(fdlPrimaryKey);
wPrimaryKey=new Text(AdvancedSettings, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
wPrimaryKey.addModifyListener(lsMod);
fdPrimaryKey=new FormData();
fdPrimaryKey.left = new FormAttachment(middle, 0);
fdPrimaryKey.right= new FormAttachment(100, 0);
fdPrimaryKey.top = new FormAttachment(wBatchSize, margin);
wPrimaryKey.setLayoutData(fdPrimaryKey);
// Some buttons
wOK=new Button(shell, SWT.PUSH);
wOK.setText(Messages.getString("System.Button.OK")); //$NON-NLS-1$
wCancel=new Button(shell, SWT.PUSH);
wCancel.setText(Messages.getString("System.Button.Cancel")); //$NON-NLS-1$
BaseStepDialog.positionBottomButtons(shell, new Button[] {wOK, wCancel}, margin, AdvancedSettings);
// Add listeners
lsCancel = new Listener() {
public void handleEvent(Event e) { cancel(); }
};
lsOK = new Listener() {
public void handleEvent(Event e) { ok(); }
};
wCancel.addListener(SWT.Selection, lsCancel);
wOK.addListener(SWT.Selection, lsOK);
// Default action listeners
lsDef = new SelectionAdapter() {
public void widgetDefaultSelected(SelectionEvent e) { ok(); }
};
wStepname.addSelectionListener(lsDef);
wDomain.addSelectionListener(lsDef);
wApiKey.addSelectionListener(lsDef);
wPackageId.addSelectionListener(lsDef);
wTitle.addSelectionListener(lsDef);
wDescription.addSelectionListener(lsDef);
wResourceId.addSelectionListener(lsDef);
wBatchSize.addSelectionListener(lsDef);
wPrimaryKey.addSelectionListener(lsDef);
// Detect X or ALT-F4 or something that kills this window...
shell.addShellListener(new ShellAdapter() { public void shellClosed(ShellEvent e) { cancel(); } } );
// Set the shell size, based upon previous time...
setSize();
// Populate and open dialog
getData();
input.setChanged(changed);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
return stepname;
}
// Read data from input (TextFileInputInfo)
public void getData()
{
wStepname.selectAll();
if (input.getDomain() != null) {
wDomain.setText(input.getDomain());
}
if (input.getApiKey() != null) {
wApiKey.setText(input.getApiKey());
}
if (input.getPackageId() != null) {
wPackageId.setText(input.getPackageId());
}
if (input.getResourceTitle() != null) {
wTitle.setText(input.getResourceTitle());
}
if (input.getResourceDescription() != null) {
wDescription.setText(input.getResourceDescription());
}
else {
wDescription.setText("");
}
if (input.getResourceId() != null) {
wResourceId.setText(input.getResourceId());
}
if (input.getBatchSize() != null) {
wBatchSize.setText(input.getBatchSize());
}
if (input.getPrimaryKey() != null) {
wPrimaryKey.setText(input.getPrimaryKey());
}
}
private void cancel()
{
stepname=null;
input.setChanged(changed);
dispose();
}
private void ok()
{
stepname = wStepname.getText(); // return value
String domain = wDomain.getText().trim();
if (!domain.startsWith("http://") && !domain.startsWith("https://")) {
domain = "http://" + domain;
}
while (domain.endsWith("/")) {
domain = domain.substring(0,domain.length()-1);
}
String apiKey = wApiKey.getText().trim();
String packageId = wPackageId.getText().trim();
if (packageId.length() != 0) {
packageId = toSlug(packageId);
}
String title = wTitle.getText().trim();
String description = wDescription.getText().trim();
String resourceId = wResourceId.getText().trim();
String batchSize = wBatchSize.getText().trim();
try {
int validSize = Integer.parseInt(batchSize);
if ( !(validSize > 0) ) {
batchSize = "5000";
}
} catch (NumberFormatException e) {
batchSize = "5000";
}
String primaryKey = wPrimaryKey.getText().trim();
input.setDomain(domain);
input.setApiKey(apiKey);
input.setPackageId(packageId);
input.setResourceTitle(title);
input.setResourceDescription(description);
input.setResourceId(resourceId);
input.setBatchSize(batchSize);
input.setPrimaryKey(primaryKey);
dispose();
}
public String toSlug(String input) {
String nowhitespace = WHITESPACE.matcher(input).replaceAll("-");
String normalized = Normalizer.normalize(nowhitespace, Form.NFD);
String slug = NONLATIN.matcher(normalized).replaceAll("");
return slug.toLowerCase(Locale.ENGLISH).replaceAll("-{2,}","-").replaceAll("^-|-$","");
}
}