|
21 | 21 | import java.util.List; |
22 | 22 | import java.util.Map; |
23 | 23 | import org.apache.commons.lang3.StringUtils; |
| 24 | +import org.apache.hop.core.CheckResult; |
24 | 25 | import org.apache.hop.core.Const; |
| 26 | +import org.apache.hop.core.ICheckResult; |
25 | 27 | import org.apache.hop.core.SourceToTargetMapping; |
26 | 28 | import org.apache.hop.core.exception.HopException; |
27 | 29 | import org.apache.hop.core.row.IRowMeta; |
|
34 | 36 | import org.apache.hop.testing.PipelineUnitTestSetLocation; |
35 | 37 | import org.apache.hop.ui.core.PropsUi; |
36 | 38 | import org.apache.hop.ui.core.dialog.BaseDialog; |
| 39 | +import org.apache.hop.ui.core.dialog.CheckResultDialog; |
37 | 40 | import org.apache.hop.ui.core.dialog.EnterMappingDialog; |
38 | 41 | import org.apache.hop.ui.core.dialog.ErrorDialog; |
39 | 42 | import org.apache.hop.ui.core.gui.GuiResource; |
@@ -190,11 +193,15 @@ public boolean open() { |
190 | 193 | wGetSortFields.setText( |
191 | 194 | BaseMessages.getString(PKG, "PipelineUnitTestSetLocationDialog.GetSortFields.Button")); |
192 | 195 | wGetSortFields.addListener(SWT.Selection, e -> getSortFields()); |
| 196 | + Button wValidate = new Button(shell, SWT.PUSH); |
| 197 | + wValidate.setText( |
| 198 | + BaseMessages.getString(PKG, "PipelineUnitTestSetLocationDialog.Validate.Button")); |
| 199 | + wValidate.addListener(SWT.Selection, e -> validate()); |
193 | 200 | Button wCancel = new Button(shell, SWT.PUSH); |
194 | 201 | wCancel.setText(BaseMessages.getString(PKG, "System.Button.Cancel")); |
195 | 202 | wCancel.addListener(SWT.Selection, e -> cancel()); |
196 | 203 | BaseTransformDialog.positionBottomButtons( |
197 | | - shell, new Button[] {wOk, wMapFields, wGetSortFields, wCancel}, margin, null); |
| 204 | + shell, new Button[] {wOk, wMapFields, wGetSortFields, wValidate, wCancel}, margin, null); |
198 | 205 |
|
199 | 206 | // the field mapping grid in between on the left |
200 | 207 | // |
@@ -428,6 +435,196 @@ public void getInfo(PipelineUnitTestSetLocation loc) { |
428 | 435 | } |
429 | 436 | } |
430 | 437 |
|
| 438 | + protected void validate() { |
| 439 | + List<ICheckResult> remarks = new ArrayList<>(); |
| 440 | + |
| 441 | + String transformName = wTransformName.getText(); |
| 442 | + String datasetName = wDataset.getText(); |
| 443 | + |
| 444 | + if (StringUtils.isEmpty(datasetName)) { |
| 445 | + remarks.add( |
| 446 | + new CheckResult( |
| 447 | + ICheckResult.TYPE_RESULT_ERROR, |
| 448 | + BaseMessages.getString( |
| 449 | + PKG, "PipelineUnitTestSetLocationDialog.Validate.Result.DatasetNotSelected"), |
| 450 | + null)); |
| 451 | + } |
| 452 | + if (StringUtils.isEmpty(transformName)) { |
| 453 | + remarks.add( |
| 454 | + new CheckResult( |
| 455 | + ICheckResult.TYPE_RESULT_ERROR, |
| 456 | + BaseMessages.getString( |
| 457 | + PKG, "PipelineUnitTestSetLocationDialog.Validate.Result.TransformNotSelected"), |
| 458 | + null)); |
| 459 | + } |
| 460 | + |
| 461 | + DataSet dataSet = null; |
| 462 | + if (StringUtils.isNotEmpty(datasetName)) { |
| 463 | + try { |
| 464 | + dataSet = findDataSet(datasetName); |
| 465 | + remarks.add( |
| 466 | + new CheckResult( |
| 467 | + ICheckResult.TYPE_RESULT_OK, |
| 468 | + "Dataset '" + datasetName + "' exists in metadata.", |
| 469 | + null)); |
| 470 | + } catch (Exception e) { |
| 471 | + remarks.add( |
| 472 | + new CheckResult( |
| 473 | + ICheckResult.TYPE_RESULT_ERROR, |
| 474 | + BaseMessages.getString( |
| 475 | + PKG, |
| 476 | + "PipelineUnitTestSetLocationDialog.Validate.Result.DatasetNotExist", |
| 477 | + datasetName), |
| 478 | + null)); |
| 479 | + } |
| 480 | + } |
| 481 | + |
| 482 | + // Check fields of the transform if selected |
| 483 | + IRowMeta transformRowMeta = null; |
| 484 | + if (StringUtils.isNotEmpty(transformName)) { |
| 485 | + transformRowMeta = transformFieldsMap.get(transformName); |
| 486 | + if (transformRowMeta == null) { |
| 487 | + remarks.add( |
| 488 | + new CheckResult( |
| 489 | + ICheckResult.TYPE_RESULT_ERROR, |
| 490 | + "Unable to find fields for transform " + transformName, |
| 491 | + null)); |
| 492 | + } |
| 493 | + } |
| 494 | + |
| 495 | + IRowMeta setRowMeta = null; |
| 496 | + if (dataSet != null) { |
| 497 | + try { |
| 498 | + setRowMeta = dataSet.getSetRowMeta(); |
| 499 | + } catch (Exception e) { |
| 500 | + remarks.add( |
| 501 | + new CheckResult( |
| 502 | + ICheckResult.TYPE_RESULT_ERROR, |
| 503 | + "Error reading fields metadata for dataset '" |
| 504 | + + datasetName |
| 505 | + + "': " |
| 506 | + + e.getMessage(), |
| 507 | + null)); |
| 508 | + } |
| 509 | + } |
| 510 | + |
| 511 | + // Verify field mappings |
| 512 | + PipelineUnitTestSetLocation loc = new PipelineUnitTestSetLocation(); |
| 513 | + getInfo(loc); |
| 514 | + |
| 515 | + if (loc.getFieldMappings().isEmpty()) { |
| 516 | + remarks.add( |
| 517 | + new CheckResult( |
| 518 | + ICheckResult.TYPE_RESULT_WARNING, |
| 519 | + BaseMessages.getString( |
| 520 | + PKG, "PipelineUnitTestSetLocationDialog.Validate.Result.FieldMappingEmpty"), |
| 521 | + null)); |
| 522 | + } else { |
| 523 | + for (PipelineUnitTestFieldMapping mapping : loc.getFieldMappings()) { |
| 524 | + String tField = mapping.getTransformFieldName(); |
| 525 | + String dField = mapping.getDataSetFieldName(); |
| 526 | + |
| 527 | + if (transformRowMeta != null && StringUtils.isNotEmpty(tField)) { |
| 528 | + if (transformRowMeta.indexOfValue(tField) < 0) { |
| 529 | + remarks.add( |
| 530 | + new CheckResult( |
| 531 | + ICheckResult.TYPE_RESULT_ERROR, |
| 532 | + BaseMessages.getString( |
| 533 | + PKG, |
| 534 | + "PipelineUnitTestSetLocationDialog.Validate.Result.TransformFieldNotExist", |
| 535 | + tField, |
| 536 | + transformName), |
| 537 | + null)); |
| 538 | + } |
| 539 | + } |
| 540 | + if (setRowMeta != null && StringUtils.isNotEmpty(dField)) { |
| 541 | + if (setRowMeta.indexOfValue(dField) < 0) { |
| 542 | + remarks.add( |
| 543 | + new CheckResult( |
| 544 | + ICheckResult.TYPE_RESULT_ERROR, |
| 545 | + BaseMessages.getString( |
| 546 | + PKG, |
| 547 | + "PipelineUnitTestSetLocationDialog.Validate.Result.DatasetFieldNotExist", |
| 548 | + dField, |
| 549 | + datasetName), |
| 550 | + null)); |
| 551 | + } |
| 552 | + } |
| 553 | + } |
| 554 | + } |
| 555 | + |
| 556 | + // Verify sort field order mappings |
| 557 | + if (setRowMeta != null) { |
| 558 | + for (String sortField : loc.getFieldOrder()) { |
| 559 | + if (StringUtils.isNotEmpty(sortField) && setRowMeta.indexOfValue(sortField) < 0) { |
| 560 | + remarks.add( |
| 561 | + new CheckResult( |
| 562 | + ICheckResult.TYPE_RESULT_ERROR, |
| 563 | + BaseMessages.getString( |
| 564 | + PKG, |
| 565 | + "PipelineUnitTestSetLocationDialog.Validate.Result.SortFieldNotExist", |
| 566 | + sortField, |
| 567 | + datasetName), |
| 568 | + null)); |
| 569 | + } |
| 570 | + } |
| 571 | + } |
| 572 | + |
| 573 | + // Verify CSV file exists and can read rows |
| 574 | + if (dataSet != null) { |
| 575 | + String filename = dataSet.getActualDataSetFilename(variables); |
| 576 | + try { |
| 577 | + org.apache.commons.vfs2.FileObject file = |
| 578 | + org.apache.hop.core.vfs.HopVfs.getFileObject(filename); |
| 579 | + if (!file.exists()) { |
| 580 | + remarks.add( |
| 581 | + new CheckResult( |
| 582 | + ICheckResult.TYPE_RESULT_ERROR, |
| 583 | + BaseMessages.getString( |
| 584 | + PKG, |
| 585 | + "PipelineUnitTestSetLocationDialog.Validate.Result.CsvNotExist", |
| 586 | + filename), |
| 587 | + null)); |
| 588 | + } else { |
| 589 | + remarks.add( |
| 590 | + new CheckResult( |
| 591 | + ICheckResult.TYPE_RESULT_OK, "CSV file '" + filename + "' exists.", null)); |
| 592 | + try { |
| 593 | + List<Object[]> rows = |
| 594 | + dataSet.getAllRows(variables, org.apache.hop.core.logging.LogChannel.UI); |
| 595 | + remarks.add( |
| 596 | + new CheckResult( |
| 597 | + ICheckResult.TYPE_RESULT_OK, |
| 598 | + BaseMessages.getString( |
| 599 | + PKG, |
| 600 | + "PipelineUnitTestSetLocationDialog.Validate.Result.Success", |
| 601 | + String.valueOf(rows.size())), |
| 602 | + null)); |
| 603 | + } catch (Exception e) { |
| 604 | + remarks.add( |
| 605 | + new CheckResult( |
| 606 | + ICheckResult.TYPE_RESULT_ERROR, |
| 607 | + BaseMessages.getString( |
| 608 | + PKG, |
| 609 | + "PipelineUnitTestSetLocationDialog.Validate.Result.CsvReadError", |
| 610 | + e.getMessage()), |
| 611 | + null)); |
| 612 | + } |
| 613 | + } |
| 614 | + } catch (Exception e) { |
| 615 | + remarks.add( |
| 616 | + new CheckResult( |
| 617 | + ICheckResult.TYPE_RESULT_ERROR, |
| 618 | + "Error resolving CSV file path: " + e.getMessage(), |
| 619 | + null)); |
| 620 | + } |
| 621 | + } |
| 622 | + |
| 623 | + // Show CheckResultDialog |
| 624 | + CheckResultDialog checkResultDialog = new CheckResultDialog(shell, remarks); |
| 625 | + checkResultDialog.open(); |
| 626 | + } |
| 627 | + |
431 | 628 | public void ok() { |
432 | 629 | getInfo(location); |
433 | 630 | ok = true; |
|
0 commit comments