Commit 2ed91a45f83bd48e5438716980370f778f130c24
1 parent
d1c5ffd1
Exists in
master
Melhoria na importação de dataObject
Showing
1 changed file
with
62 additions
and
42 deletions
Show diff stats
cit-esi-api/src/main/java/br/com/centralit/esi/api/data/service/impl/DataObjectServiceImpl.java
| @@ -640,49 +640,9 @@ public class DataObjectServiceImpl extends GenericServiceImpl<DataObject, Long> | @@ -640,49 +640,9 @@ public class DataObjectServiceImpl extends GenericServiceImpl<DataObject, Long> | ||
| 640 | sourceObject.setGenerateController(sourceObject.getFormVersion() != null); | 640 | sourceObject.setGenerateController(sourceObject.getFormVersion() != null); |
| 641 | sourceObject.setDataSource(dataSourceService.save(sourceObject.getDataSource(), false)); | 641 | sourceObject.setDataSource(dataSourceService.save(sourceObject.getDataSource(), false)); |
| 642 | 642 | ||
| 643 | - Database database = sourceObject.getDataSource().getDatabase(); | ||
| 644 | if (sourceObject.getFields() != null) { | 643 | if (sourceObject.getFields() != null) { |
| 645 | - for (DataColumn dataColumn: sourceObject.getColumns()) { | ||
| 646 | - dataColumn.setId(null); | ||
| 647 | - if (dataColumn.getWidget() != null) { | ||
| 648 | - dataColumn.setWidget(formWidgetService.findByName(dataColumn.getWidget().getName())); | ||
| 649 | - } | ||
| 650 | - String name = dataColumn.getType().getName(); | ||
| 651 | - dataColumn.setType(dataTypeService.findByName(name)); | ||
| 652 | - if (dataColumn.getType() == null) { | ||
| 653 | - String databaseType = dataColumn.getDatabaseType() != null ? dataColumn.getDatabaseType() : name; | ||
| 654 | - for (DatabaseDomain domain : database.getTypes()) { | ||
| 655 | - if (domain.getName().equalsIgnoreCase(databaseType)) { | ||
| 656 | - dataColumn.setType(domain.getDataType()); | ||
| 657 | - break; | ||
| 658 | - } | ||
| 659 | - } | ||
| 660 | - } | ||
| 661 | - if (dataColumn.getType() == null) { | ||
| 662 | - throw new EsiBusinessException("Tipo de dados '"+name+"' não encontrado"); | ||
| 663 | - } | ||
| 664 | - | ||
| 665 | - String databaseType = null; | ||
| 666 | - boolean exists = false; | ||
| 667 | - for (DatabaseDomain domain : database.getTypes()) { | ||
| 668 | - if (domain.getDataType().getId().equals(dataColumn.getType().getId())) { | ||
| 669 | - if (databaseType == null) { | ||
| 670 | - databaseType = domain.getName(); | ||
| 671 | - } | ||
| 672 | - if (!UtilString.isNullOrEmpty(dataColumn.getDatabaseType()) && dataColumn.getDatabaseType().equalsIgnoreCase(domain.getName())) { | ||
| 673 | - exists = true; | ||
| 674 | - break; | ||
| 675 | - } | ||
| 676 | - } | ||
| 677 | - } | ||
| 678 | - if (!exists) { | ||
| 679 | - dataColumn.setDatabaseType(databaseType); | ||
| 680 | - } | ||
| 681 | - | ||
| 682 | - if (UtilString.isNullOrEmpty(dataColumn.getDatabaseType())) { | ||
| 683 | - throw new EsiBusinessException("Tipo de dados '"+name+"' não encontrado no BD"); | ||
| 684 | - } | ||
| 685 | - } | 644 | + this.verifyColumnsForImport(sourceObject); |
| 645 | + | ||
| 686 | for (Relationship relationship: sourceObject.getRelationships()) { | 646 | for (Relationship relationship: sourceObject.getRelationships()) { |
| 687 | relationship.setId(null); | 647 | relationship.setId(null); |
| 688 | relationship.setReferencedColumn(null); | 648 | relationship.setReferencedColumn(null); |
| @@ -721,5 +681,65 @@ public class DataObjectServiceImpl extends GenericServiceImpl<DataObject, Long> | @@ -721,5 +681,65 @@ public class DataObjectServiceImpl extends GenericServiceImpl<DataObject, Long> | ||
| 721 | } | 681 | } |
| 722 | } | 682 | } |
| 723 | 683 | ||
| 684 | + private void verifyColumnsForImport(DataObject sourceObject) { | ||
| 685 | + List<DataColumn> columns = null; | ||
| 686 | + Connection connection = dataSourceService.connect(sourceObject.getDataSource()); | ||
| 687 | + try { | ||
| 688 | + columns = DataObjectUtil.buildColumns(sourceObject.getDataSource(), connection, sourceObject.getSchema(), sourceObject.getName()); | ||
| 689 | + } catch (Exception e) { | ||
| 690 | + } | ||
| 691 | + | ||
| 692 | + Database database = sourceObject.getDataSource().getDatabase(); | ||
| 693 | + for (DataColumn dataColumn: sourceObject.getColumns()) { | ||
| 694 | + DataColumn DBColumn = null; | ||
| 695 | + if (columns != null) { | ||
| 696 | + for (DataColumn col : columns) { | ||
| 697 | + if (col.getName().equalsIgnoreCase(dataColumn.getName())) { | ||
| 698 | + DBColumn = col; | ||
| 699 | + break; | ||
| 700 | + } | ||
| 701 | + } | ||
| 702 | + } | ||
| 703 | + | ||
| 704 | + dataColumn.setId(null); | ||
| 705 | + String name = dataColumn.getType().getName(); | ||
| 706 | + if (DBColumn != null) { | ||
| 707 | + dataColumn.setDatabaseType(DBColumn.getDatabaseType()); | ||
| 708 | + dataColumn.setType(DBColumn.getType()); | ||
| 709 | + }else{ | ||
| 710 | + dataColumn.setType(dataTypeService.findByName(name)); | ||
| 711 | + if (dataColumn.getType() == null) { | ||
| 712 | + String databaseType = dataColumn.getDatabaseType() != null ? dataColumn.getDatabaseType() : name; | ||
| 713 | + for (DatabaseDomain domain : database.getTypes()) { | ||
| 714 | + if (domain.getName().equalsIgnoreCase(databaseType)) { | ||
| 715 | + dataColumn.setType(domain.getDataType()); | ||
| 716 | + break; | ||
| 717 | + } | ||
| 718 | + } | ||
| 719 | + } | ||
| 720 | + } | ||
| 721 | + | ||
| 722 | + if (dataColumn.getType() == null) { | ||
| 723 | + throw new EsiBusinessException("Tipo de dados '"+name+"' não encontrado"); | ||
| 724 | + } | ||
| 725 | + | ||
| 726 | + if (UtilString.isNullOrEmpty(dataColumn.getDatabaseType())) { | ||
| 727 | + for (DatabaseDomain domain : database.getTypes()) { | ||
| 728 | + if (domain.getDataType().getId().equals(dataColumn.getType().getId())) { | ||
| 729 | + dataColumn.setDatabaseType(domain.getName()); | ||
| 730 | + break; | ||
| 731 | + } | ||
| 732 | + } | ||
| 733 | + } | ||
| 734 | + | ||
| 735 | + if (UtilString.isNullOrEmpty(dataColumn.getDatabaseType())) { | ||
| 736 | + throw new EsiBusinessException("Tipo de dados '"+name+"' não encontrado no BD"); | ||
| 737 | + } | ||
| 738 | + | ||
| 739 | + if (dataColumn.getWidget() != null) { | ||
| 740 | + dataColumn.setWidget(formWidgetService.findByName(dataColumn.getWidget().getName())); | ||
| 741 | + } | ||
| 742 | + } | ||
| 743 | + } | ||
| 724 | 744 | ||
| 725 | } | 745 | } |
| 726 | \ No newline at end of file | 746 | \ No newline at end of file |