diff --git a/MDM-LCAG-JavaFunctions/src/main/java/application/FileActions.java b/MDM-LCAG-JavaFunctions/src/main/java/application/FileActions.java index 58c377f..21b8e1b 100644 --- a/MDM-LCAG-JavaFunctions/src/main/java/application/FileActions.java +++ b/MDM-LCAG-JavaFunctions/src/main/java/application/FileActions.java @@ -13,7 +13,10 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; +import java.util.ArrayList; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.naming.ConfigurationException; import javax.swing.BorderFactory; @@ -31,6 +34,8 @@ import javax.swing.KeyStroke; import javax.swing.SwingUtilities; import javax.swing.filechooser.FileNameExtensionFilter; +import org.apache.commons.io.FilenameUtils; + import de.lcag.common.ExcelWorkbook; import de.lcag.common.LcagProperties; import de.lcag.common.MappingTable; @@ -46,7 +51,8 @@ public class FileActions extends JFrame { private JLabel masterFileLabel, clientFileLabel, configFileLabel; private JButton selectMasterFile, selectClientFile, selectConfigFile; - private Path masterFile, clientFile, configFile; + private Path configFile; + private String masterFile, clientFile; private JMenuBar menuBar; private JMenu actionsMenu; private JMenuItem mergeAction; @@ -54,6 +60,8 @@ public class FileActions extends JFrame { private File configFileDirectoryPath = null; private LcagProperties config = null; private File masterDirecory = null; + private File clientDirecory = null; + private static List SUPPORTED_FILE_TYPES = List.of("xls", "xlsx"); public FileActions() { setTitle("Compare 2 Excel Files"); @@ -70,7 +78,7 @@ public class FileActions extends JFrame { // Add an empty border to create a gap on the right side of leftPanel leftButtonPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10)); // right gap: 10px - rightTextPanel.setPreferredSize(new Dimension(500, FRAME_HEIGHT)); + rightTextPanel.setPreferredSize(new Dimension(1000, FRAME_HEIGHT)); mainPanel.add(leftButtonPanel, BorderLayout.WEST); mainPanel.add(rightTextPanel, BorderLayout.EAST); @@ -95,42 +103,40 @@ public class FileActions extends JFrame { if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { String clientDirString = null; - configFile = selectFile(fileChooser); + configFile = Path.of(selectFile(fileChooser)); config = new LcagProperties(configFile.toString()); configFileLabel.setText(String.format(" %s: %s", CONFIG_FILE_LABEL, configFile)); - masterFile = config.getOptionalFilePathProperty("main_file"); - clientFile = config.getOptionalFilePathProperty("compare_file"); - - if (clientFile.isAbsolute()) { - clientDirString = clientFile.getParent().toString(); - } else { - // client file is relative expect it is same directory as master - Path dirPath = masterFile.getParent(); - - clientDirString = "same directory as 1st file"; - clientFile = dirPath.resolve(clientFile); - } - - // After user selected a config file enable buttons to set files to compare (can - // overwrite files listed in config file): - selectMasterFile.setEnabled(true); - - if (clientFile != null) { - String fileName = clientFile.getFileName().toString(); - clientFileLabel - .setText(String.format("Selected: %s
in: %s", fileName, clientDirString)); - selectClientFile.setEnabled(true); - } + masterFile = config.getOptionalProperty("main_file"); if (masterFile != null) { - String fileName = masterFile.getFileName().toString(); - masterDirecory = new File(masterFile.getParent().toString()); - masterFileLabel.setText(String.format("Selected: %s
in: %s", fileName, - masterFile.getParent().toString())); + String masterFilePath = LcagFileTools.getPath(masterFile); + masterFile = LcagFileTools.getName(masterFile); + masterDirecory = new File(masterFilePath); + masterFileLabel.setText(String.format("Selected: %s
in: %s", masterFile, + masterDirecory.getAbsolutePath())); + selectMasterFile.setEnabled(true); + } + + clientFile = config.getOptionalProperty("compare_file"); + if (clientFile != null) { + String clientFilePath = LcagFileTools.getPath(clientFile); + clientFile = LcagFileTools.getName(clientFile); + clientDirecory = new File(clientFilePath); + + if (clientDirecory.isAbsolute()) { + clientDirString = clientFilePath; + } else { + clientDirString = "same directory as 1st file"; + } + clientFileLabel.setText( + String.format("Selected: %s
in: %s ", clientFile, clientDirString)); selectClientFile.setEnabled(true); } + // After user selected a config file enable buttons to set files to compare (can + // overwrite files listed in config file): + } }); @@ -142,15 +148,15 @@ public class FileActions extends JFrame { contextMenuMasterFile.add(renameFile1); contextMenuClientFile.add(renameFile2); - masterFileLabel = new JLabel(String.format(" %s: %s", MASTER_FILE_LABEL, - masterFile == null ? "Not selected" : masterFile.getFileName())); + masterFileLabel = new JLabel( + String.format(" %s: %s", MASTER_FILE_LABEL, masterFile == null ? "Not selected" : masterFile)); selectMasterFile = new JButton("Select " + MASTER_FILE_LABEL); leftButtonPanel.add(selectMasterFile); rightTextPanel.add(masterFileLabel); - clientFileLabel = new JLabel(String.format(" %s: %s", "Excel to compare", - clientFile == null ? "Not selected" : clientFile.getFileName())); + clientFileLabel = new JLabel( + String.format(" %s: %s", "Excel to compare", clientFile == null ? "Not selected" : clientFile)); selectClientFile = new JButton("Select Excel to Compare"); @@ -182,16 +188,26 @@ public class FileActions extends JFrame { selectMasterFile.setEnabled(false); selectMasterFile.addActionListener(e -> { JFileChooser fileChooser = new JFileChooser(); - FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel-Files", "xls", "xlsx"); + FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel-Files", + SUPPORTED_FILE_TYPES.toArray(new String[0])); fileChooser.setFileFilter(filter); + if (masterFile != null) { - masterDirecory = new File(masterFile.getParent().toString()); + Path masterFilePath = Path.of(masterFile); + masterDirecory = new File(masterFilePath.getParent().toString()); fileChooser.setCurrentDirectory(masterDirecory); + + masterDirecory = new File(masterFilePath.getParent().toString()); + masterFileLabel.setText(String.format("Selected: %s
in: %s", masterFile, + masterFilePath.getParent().toString())); + selectClientFile.setEnabled(true); } + if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { + Path masterFilePath = Path.of(masterFile); masterFile = selectFile(fileChooser); - masterDirecory = new File(masterFile.getParent().toString()); - masterFileLabel.setText(String.format(" %s: %s", MASTER_FILE_LABEL, masterFile.getFileName())); + masterDirecory = new File(masterFilePath.getParent().toString()); + masterFileLabel.setText(String.format(" %s: %s", MASTER_FILE_LABEL, masterFilePath.getFileName())); selectClientFile.setEnabled(true); } }); @@ -206,7 +222,7 @@ public class FileActions extends JFrame { fileChooser.setFileFilter(filter); if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { clientFile = selectFile(fileChooser); - clientFileLabel.setText("Datei 2: " + clientFile.getFileName()); + clientFileLabel.setText("Datei 2: " + clientFile); } } }); @@ -235,27 +251,85 @@ public class FileActions extends JFrame { setVisible(true); } - private Path selectFile(JFileChooser fileChooser) { + private String selectFile(JFileChooser fileChooser) { File result = fileChooser.getSelectedFile(); - return Path.of(result.getAbsolutePath()); + return FilenameUtils.separatorsToUnix(result.getAbsolutePath()); } - private void mergeFiles(LcagProperties pConfig, Path pMasterFilePath, Path pClientFilePath) { - if (pConfig != null) { + private void mergeFiles(LcagProperties pConfig, String pMasterFilePathPattern, String pClientFilePathPattern) { + Pattern masterPattern = Pattern.compile(pMasterFilePathPattern); + List masterFiles = new ArrayList<>(); + String masterDirectoryPathStr = masterDirecory.getAbsolutePath(); + + try { + Path masterDirectoryPath = Path.of(masterDirectoryPathStr); + masterFiles = Files.walk(masterDirectoryPath).filter(p -> { + String filePath = FilenameUtils.separatorsToUnix(p.toString()); + String fileName = LcagFileTools.getName(p.toString()); + + Matcher matcher = masterPattern.matcher(fileName); + boolean matches = matcher.matches(); + return matches; + }).toList(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + if (pConfig == null) { + JOptionPane.showMessageDialog(this, "Pls select at least a config file!", "Error", + JOptionPane.ERROR_MESSAGE); + + return; + } + + for (Path aMasterFilePath : masterFiles) { + String masterFileName = aMasterFilePath.getFileName().toString(); + String fileType = LcagFileTools.getExtension(masterFileName); + + if (!SUPPORTED_FILE_TYPES.contains(fileType)) { + System.err.printf("Comparision of file type %s not supported for %s\n", fileType, masterFileName); + continue; + } + + String s = FilenameUtils.separatorsToUnix(aMasterFilePath.normalize().toString()); + + String clientFilePathStr = clientDirecory.getAbsolutePath() + "/" + + masterFileName.replaceFirst(pMasterFilePathPattern, pClientFilePathPattern); + Path clientFilePath = Path.of(clientFilePathStr); + String clientDirString = null; + + if (clientFilePath.isAbsolute()) { + clientDirString = clientFilePath.getParent().toString(); + } else { + // client file is relative expect it is same directory as master + Path dirPath = aMasterFilePath.getParent(); + + clientDirString = "same directory as 1st file"; + clientFilePath = dirPath.resolve(clientFilePath); + } + + if (clientFilePath != null) { + String fileName = clientFilePath.getFileName().toString(); + clientFileLabel + .setText(String.format("Selected: %s
in: %s", fileName, clientDirString)); + selectClientFile.setEnabled(true); + } + String keyFieldString = pConfig.getProperty("list_of_keys"); List keyFields = LcagStringTools.asList(keyFieldString, ",\\s*"); - if (pMasterFilePath != null && pClientFilePath != null) + if (aMasterFilePath != null && clientFilePath != null) { + try { - String masterFileName = pMasterFilePath.getFileName().toString(); - Path resultFilePath = LcagFileTools.appendStringToFilePath(pMasterFilePath, "-merged"); + Path resultFilePath = LcagFileTools.appendStringToFilePath(aMasterFilePath, "-merged"); String fieldMapping = pConfig.getProperty("column.mapping.main2compare"); - Files.copy(pMasterFilePath, resultFilePath, StandardCopyOption.REPLACE_EXISTING); + Files.copy(aMasterFilePath, resultFilePath, StandardCopyOption.REPLACE_EXISTING); ExcelWorkbook excel1 = new ExcelWorkbook(resultFilePath.toString()); - ExcelWorkbook excel2 = new ExcelWorkbook(pClientFilePath.toString()); + ExcelWorkbook excel2 = new ExcelWorkbook(clientFilePath.toString()); excel1.setKeyColumns(keyFields); excel1.read(); @@ -271,14 +345,14 @@ public class FileActions extends JFrame { } WorkbookComparisonResult compareResult = excel1.compareTo(excel2); - + if (compareResult.tablesAreEquals()) { - System.out.printf("RESULT: Tables %s and %s are equal.\n", resultFilePath, pClientFilePath); + System.out.printf("RESULT: Tables %s and %s are equal.\n", resultFilePath, clientFilePath); } else { ExcelWorkbook integratedTable = (ExcelWorkbook) compareResult.integrateDifferencesIntoMaster(); - + integratedTable.save(); - + Desktop.getDesktop().open(resultFilePath.toFile()); } } catch (IOException ex) { @@ -288,9 +362,7 @@ public class FileActions extends JFrame { // TODO Auto-generated catch block e.printStackTrace(); } - } else { - JOptionPane.showMessageDialog(this, "Pls select at least a config file!", "Error", - JOptionPane.ERROR_MESSAGE); + } } } @@ -305,7 +377,7 @@ public class FileActions extends JFrame { return result; } - private Path renameFile(Path pFile, JLabel label, String prefix) { + private Path renameFile(String pFile, JLabel label, String prefix) { Path newPath = null; if (pFile == null) return newPath; @@ -314,16 +386,17 @@ public class FileActions extends JFrame { String newName = JOptionPane.showInputDialog(this, "Enter new name:", file.getName()); if (!LcagStringTools.isEmpty(newName)) { - newPath = pFile.getParent().resolve(newName); + Path filePath = Path.of(pFile); + newPath = filePath.getParent().resolve(newName); File newFile = new File(newPath.toString()); if (file.renameTo(newFile)) { file = newFile; label.setText(prefix + ": " + newFile.getName()); if (prefix.equals("Datei 1")) - this.masterFile = newPath; + this.masterFile = newPath.toString(); else - this.clientFile = newPath; + this.clientFile = newPath.toString(); } else { JOptionPane.showMessageDialog(this, "Umbenennung fehlgeschlagen!", "Fehler", JOptionPane.ERROR_MESSAGE); } diff --git a/MDM-LCAG-JavaFunctions/src/test/resources/CompareTables/testCompareShipmentPairs.properties b/MDM-LCAG-JavaFunctions/src/test/resources/CompareTables/testCompareShipmentPairs.properties new file mode 100644 index 0000000..21091fb --- /dev/null +++ b/MDM-LCAG-JavaFunctions/src/test/resources/CompareTables/testCompareShipmentPairs.properties @@ -0,0 +1,7 @@ +# Configuration file for V:/EBX/workspace/MDM-LCAG-JavaFunctions/src/main/java/application/FileActions.java + +main_file = s:/WS/eclipse-2024-git/MDM-LCAG-JavaFunctions/MDM-LCAG-JavaFunctions/src/test/resources/CompareTables/MDM_compare_FilePairs/([0-9\-]*)_storeShipment_request.xml$ +compare_file = $1_getShipment_reply.xml +list_of_keys = Variant + +#column.mapping.main2compare=Code->Code;ValidFrom->ValidFrom