Added support of regex pattern in file names
This commit is contained in:
@@ -13,7 +13,10 @@ import java.io.IOException;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardCopyOption;
|
import java.nio.file.StandardCopyOption;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import javax.naming.ConfigurationException;
|
import javax.naming.ConfigurationException;
|
||||||
import javax.swing.BorderFactory;
|
import javax.swing.BorderFactory;
|
||||||
@@ -31,6 +34,8 @@ import javax.swing.KeyStroke;
|
|||||||
import javax.swing.SwingUtilities;
|
import javax.swing.SwingUtilities;
|
||||||
import javax.swing.filechooser.FileNameExtensionFilter;
|
import javax.swing.filechooser.FileNameExtensionFilter;
|
||||||
|
|
||||||
|
import org.apache.commons.io.FilenameUtils;
|
||||||
|
|
||||||
import de.lcag.common.ExcelWorkbook;
|
import de.lcag.common.ExcelWorkbook;
|
||||||
import de.lcag.common.LcagProperties;
|
import de.lcag.common.LcagProperties;
|
||||||
import de.lcag.common.MappingTable;
|
import de.lcag.common.MappingTable;
|
||||||
@@ -46,7 +51,8 @@ public class FileActions extends JFrame {
|
|||||||
|
|
||||||
private JLabel masterFileLabel, clientFileLabel, configFileLabel;
|
private JLabel masterFileLabel, clientFileLabel, configFileLabel;
|
||||||
private JButton selectMasterFile, selectClientFile, selectConfigFile;
|
private JButton selectMasterFile, selectClientFile, selectConfigFile;
|
||||||
private Path masterFile, clientFile, configFile;
|
private Path configFile;
|
||||||
|
private String masterFile, clientFile;
|
||||||
private JMenuBar menuBar;
|
private JMenuBar menuBar;
|
||||||
private JMenu actionsMenu;
|
private JMenu actionsMenu;
|
||||||
private JMenuItem mergeAction;
|
private JMenuItem mergeAction;
|
||||||
@@ -54,6 +60,8 @@ public class FileActions extends JFrame {
|
|||||||
private File configFileDirectoryPath = null;
|
private File configFileDirectoryPath = null;
|
||||||
private LcagProperties config = null;
|
private LcagProperties config = null;
|
||||||
private File masterDirecory = null;
|
private File masterDirecory = null;
|
||||||
|
private File clientDirecory = null;
|
||||||
|
private static List<String> SUPPORTED_FILE_TYPES = List.of("xls", "xlsx");
|
||||||
|
|
||||||
public FileActions() {
|
public FileActions() {
|
||||||
setTitle("Compare 2 Excel Files");
|
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
|
// 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
|
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(leftButtonPanel, BorderLayout.WEST);
|
||||||
mainPanel.add(rightTextPanel, BorderLayout.EAST);
|
mainPanel.add(rightTextPanel, BorderLayout.EAST);
|
||||||
|
|
||||||
@@ -95,42 +103,40 @@ public class FileActions extends JFrame {
|
|||||||
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
|
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
|
||||||
String clientDirString = null;
|
String clientDirString = null;
|
||||||
|
|
||||||
configFile = selectFile(fileChooser);
|
configFile = Path.of(selectFile(fileChooser));
|
||||||
config = new LcagProperties(configFile.toString());
|
config = new LcagProperties(configFile.toString());
|
||||||
|
|
||||||
configFileLabel.setText(String.format(" %s: %s", CONFIG_FILE_LABEL, configFile));
|
configFileLabel.setText(String.format(" %s: %s", CONFIG_FILE_LABEL, configFile));
|
||||||
|
|
||||||
masterFile = config.getOptionalFilePathProperty("main_file");
|
masterFile = config.getOptionalProperty("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("<html>Selected: %s<br>in: %s</html>", fileName, clientDirString));
|
|
||||||
selectClientFile.setEnabled(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (masterFile != null) {
|
if (masterFile != null) {
|
||||||
String fileName = masterFile.getFileName().toString();
|
String masterFilePath = LcagFileTools.getPath(masterFile);
|
||||||
masterDirecory = new File(masterFile.getParent().toString());
|
masterFile = LcagFileTools.getName(masterFile);
|
||||||
masterFileLabel.setText(String.format("<html>Selected: %s<br>in: %s</html>", fileName,
|
masterDirecory = new File(masterFilePath);
|
||||||
masterFile.getParent().toString()));
|
masterFileLabel.setText(String.format("<html>Selected: %s<br>in: %s</html>", 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("<html>Selected: %s<br>in: %s </html>", clientFile, clientDirString));
|
||||||
selectClientFile.setEnabled(true);
|
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);
|
contextMenuMasterFile.add(renameFile1);
|
||||||
contextMenuClientFile.add(renameFile2);
|
contextMenuClientFile.add(renameFile2);
|
||||||
|
|
||||||
masterFileLabel = new JLabel(String.format(" %s: %s", MASTER_FILE_LABEL,
|
masterFileLabel = new JLabel(
|
||||||
masterFile == null ? "Not selected" : masterFile.getFileName()));
|
String.format(" %s: %s", MASTER_FILE_LABEL, masterFile == null ? "Not selected" : masterFile));
|
||||||
|
|
||||||
selectMasterFile = new JButton("Select " + MASTER_FILE_LABEL);
|
selectMasterFile = new JButton("Select " + MASTER_FILE_LABEL);
|
||||||
leftButtonPanel.add(selectMasterFile);
|
leftButtonPanel.add(selectMasterFile);
|
||||||
rightTextPanel.add(masterFileLabel);
|
rightTextPanel.add(masterFileLabel);
|
||||||
|
|
||||||
clientFileLabel = new JLabel(String.format(" %s: %s", "Excel to compare",
|
clientFileLabel = new JLabel(
|
||||||
clientFile == null ? "Not selected" : clientFile.getFileName()));
|
String.format(" %s: %s", "Excel to compare", clientFile == null ? "Not selected" : clientFile));
|
||||||
|
|
||||||
selectClientFile = new JButton("Select Excel to Compare");
|
selectClientFile = new JButton("Select Excel to Compare");
|
||||||
|
|
||||||
@@ -182,16 +188,26 @@ public class FileActions extends JFrame {
|
|||||||
selectMasterFile.setEnabled(false);
|
selectMasterFile.setEnabled(false);
|
||||||
selectMasterFile.addActionListener(e -> {
|
selectMasterFile.addActionListener(e -> {
|
||||||
JFileChooser fileChooser = new JFileChooser();
|
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);
|
fileChooser.setFileFilter(filter);
|
||||||
|
|
||||||
if (masterFile != null) {
|
if (masterFile != null) {
|
||||||
masterDirecory = new File(masterFile.getParent().toString());
|
Path masterFilePath = Path.of(masterFile);
|
||||||
|
masterDirecory = new File(masterFilePath.getParent().toString());
|
||||||
fileChooser.setCurrentDirectory(masterDirecory);
|
fileChooser.setCurrentDirectory(masterDirecory);
|
||||||
|
|
||||||
|
masterDirecory = new File(masterFilePath.getParent().toString());
|
||||||
|
masterFileLabel.setText(String.format("<html>Selected: %s<br>in: %s</html>", masterFile,
|
||||||
|
masterFilePath.getParent().toString()));
|
||||||
|
selectClientFile.setEnabled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
|
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
|
||||||
|
Path masterFilePath = Path.of(masterFile);
|
||||||
masterFile = selectFile(fileChooser);
|
masterFile = selectFile(fileChooser);
|
||||||
masterDirecory = new File(masterFile.getParent().toString());
|
masterDirecory = new File(masterFilePath.getParent().toString());
|
||||||
masterFileLabel.setText(String.format(" %s: %s", MASTER_FILE_LABEL, masterFile.getFileName()));
|
masterFileLabel.setText(String.format(" %s: %s", MASTER_FILE_LABEL, masterFilePath.getFileName()));
|
||||||
selectClientFile.setEnabled(true);
|
selectClientFile.setEnabled(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -206,7 +222,7 @@ public class FileActions extends JFrame {
|
|||||||
fileChooser.setFileFilter(filter);
|
fileChooser.setFileFilter(filter);
|
||||||
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
|
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
|
||||||
clientFile = selectFile(fileChooser);
|
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);
|
setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Path selectFile(JFileChooser fileChooser) {
|
private String selectFile(JFileChooser fileChooser) {
|
||||||
File result = fileChooser.getSelectedFile();
|
File result = fileChooser.getSelectedFile();
|
||||||
return Path.of(result.getAbsolutePath());
|
return FilenameUtils.separatorsToUnix(result.getAbsolutePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void mergeFiles(LcagProperties pConfig, Path pMasterFilePath, Path pClientFilePath) {
|
private void mergeFiles(LcagProperties pConfig, String pMasterFilePathPattern, String pClientFilePathPattern) {
|
||||||
if (pConfig != null) {
|
Pattern masterPattern = Pattern.compile(pMasterFilePathPattern);
|
||||||
|
List<Path> 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("<html>Selected: %s<br>in: %s</html>", fileName, clientDirString));
|
||||||
|
selectClientFile.setEnabled(true);
|
||||||
|
}
|
||||||
|
|
||||||
String keyFieldString = pConfig.getProperty("list_of_keys");
|
String keyFieldString = pConfig.getProperty("list_of_keys");
|
||||||
|
|
||||||
List<String> keyFields = LcagStringTools.asList(keyFieldString, ",\\s*");
|
List<String> keyFields = LcagStringTools.asList(keyFieldString, ",\\s*");
|
||||||
|
|
||||||
if (pMasterFilePath != null && pClientFilePath != null)
|
if (aMasterFilePath != null && clientFilePath != null) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String masterFileName = pMasterFilePath.getFileName().toString();
|
Path resultFilePath = LcagFileTools.appendStringToFilePath(aMasterFilePath, "-merged");
|
||||||
Path resultFilePath = LcagFileTools.appendStringToFilePath(pMasterFilePath, "-merged");
|
|
||||||
String fieldMapping = pConfig.getProperty("column.mapping.main2compare");
|
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 excel1 = new ExcelWorkbook(resultFilePath.toString());
|
||||||
ExcelWorkbook excel2 = new ExcelWorkbook(pClientFilePath.toString());
|
ExcelWorkbook excel2 = new ExcelWorkbook(clientFilePath.toString());
|
||||||
|
|
||||||
excel1.setKeyColumns(keyFields);
|
excel1.setKeyColumns(keyFields);
|
||||||
excel1.read();
|
excel1.read();
|
||||||
@@ -273,7 +347,7 @@ public class FileActions extends JFrame {
|
|||||||
WorkbookComparisonResult compareResult = excel1.compareTo(excel2);
|
WorkbookComparisonResult compareResult = excel1.compareTo(excel2);
|
||||||
|
|
||||||
if (compareResult.tablesAreEquals()) {
|
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 {
|
} else {
|
||||||
ExcelWorkbook integratedTable = (ExcelWorkbook) compareResult.integrateDifferencesIntoMaster();
|
ExcelWorkbook integratedTable = (ExcelWorkbook) compareResult.integrateDifferencesIntoMaster();
|
||||||
|
|
||||||
@@ -288,9 +362,7 @@ public class FileActions extends JFrame {
|
|||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Path renameFile(Path pFile, JLabel label, String prefix) {
|
private Path renameFile(String pFile, JLabel label, String prefix) {
|
||||||
Path newPath = null;
|
Path newPath = null;
|
||||||
if (pFile == null)
|
if (pFile == null)
|
||||||
return newPath;
|
return newPath;
|
||||||
@@ -314,16 +386,17 @@ public class FileActions extends JFrame {
|
|||||||
String newName = JOptionPane.showInputDialog(this, "Enter new name:", file.getName());
|
String newName = JOptionPane.showInputDialog(this, "Enter new name:", file.getName());
|
||||||
|
|
||||||
if (!LcagStringTools.isEmpty(newName)) {
|
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());
|
File newFile = new File(newPath.toString());
|
||||||
if (file.renameTo(newFile)) {
|
if (file.renameTo(newFile)) {
|
||||||
file = newFile;
|
file = newFile;
|
||||||
label.setText(prefix + ": " + newFile.getName());
|
label.setText(prefix + ": " + newFile.getName());
|
||||||
if (prefix.equals("Datei 1"))
|
if (prefix.equals("Datei 1"))
|
||||||
this.masterFile = newPath;
|
this.masterFile = newPath.toString();
|
||||||
else
|
else
|
||||||
this.clientFile = newPath;
|
this.clientFile = newPath.toString();
|
||||||
} else {
|
} else {
|
||||||
JOptionPane.showMessageDialog(this, "Umbenennung fehlgeschlagen!", "Fehler", JOptionPane.ERROR_MESSAGE);
|
JOptionPane.showMessageDialog(this, "Umbenennung fehlgeschlagen!", "Fehler", JOptionPane.ERROR_MESSAGE);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
Reference in New Issue
Block a user