[ad_1]
I have a Java application that saves the path and last modification date of all files with the specified extension in the specified folder, recursivelly, in a CSV file.
I have another application that receives a keyword from the user and looks for a file which file name contains that word, opens the last modified one.
It was working as expected, but one of the users found a bug, where, with a specific file (until now) the it opens the one with the older modification date instead of the last one.
Here is the CSV file filtered with the item bugged: CSV file
This is the code where the application filters the csv data to find only the last modified file which the name contains the keyword:
public class OpenListener implements ActionListener {
private TwoComboBox frame;
private List<String[]> drawList = new ArrayList<String[]>();
public OpenListener(TwoComboBox frame, List<String[]> drawList) {
this.frame = frame;
this.drawList = drawList;
}
public void actionPerformed(ActionEvent e) {
/**
* Open the file or directory of file of the last modified file
in the folder to look with the specified file name
**/
// Get user preferences
final String fileType =
String.valueOf(frame.getCombo1().getCombo().getSelectedItem());
final String openMethod =
String.valueOf(frame.getCombo2().getCombo().getSelectedItem());
// Filter
drawList = filterDrawList(drawList, fileType.toLowerCase());
// On click
String drawPath = replaceUnmappedChars(drawList.get(0)[2]);
try {
if (openMethod.equals(SearchListener.OPEN_FOLDER_OPTION)) {
frame.dispose();
String dirPath =
(new File(drawPath)).getParentFile().getAbsolutePath();
Desktop.getDesktop().open(new File(dirPath));
} else {
frame.dispose();
Desktop.getDesktop().open(new File(drawPath));
}
} catch (IOException ex) {
showErrorDialog("'" + drawPath + "' não encontrado");
}
}
private static List<String[]> filterDrawList(
List<String[]> drawListToFilter, String fileTypeToFilter
)
{
/**
* Returns filtered draw list
**/
drawListToFilter = DTManager.filterTable(drawListToFilter,
fileTypeToFilter,
1);
return DTManager.getLastObjects(
drawListToFilter,
0,
3,
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'")
);
}
}
This is the code that filters the data:
public class DTManager{
public static List<String[]> getLastObjects(List<String[]> table,
int toFilterIndex,
int dateIndex,
DateTimeFormatter dtFormatter)
{
/**
* Returns a list with only unique items filtered by the instances with
the last date
**/
List<String[]> list = new ArrayList<String[]>();
List<String[]> currData = new ArrayList<String[]>();
LocalDateTime date;
String dateStr;
List<LocalDateTime> dates = new ArrayList<LocalDateTime>();
int lastDateIndex;
for (int i = 0; i < table.size(); i++) {
currData = filterTable(table,
table.get(i)[toFilterIndex],
toFilterIndex);
dates = new ArrayList<LocalDateTime>();
for (int j = 0; j < currData.size(); j++) {
date = LocalDateTime.parse(currData.get(j)[dateIndex],
dtFormatter);
dates.add(date);
}
lastDateIndex = dates.indexOf(Collections.max(dates));
list.add(currData.get(lastDateIndex));
table = inverseFilterTable(table,
table.get(i)[toFilterIndex],
toFilterIndex);
}
return list;
}
public static List<String[]> filterTable(List<String[]> table,
String valueToLook,
int toFilterIndex)
{
/**
* Returns a list with only items where the passed value appears in the
specified column
**/
List<String[]> list = new ArrayList<String[]>();
for (int i = 0; i < table.size(); i++) {
if (table.get(i)[toFilterIndex].contains(valueToLook)) {
list.add(table.get(i));
}
}
return list;
}
public static List<String[]> inverseFilterTable(List<String[]> table,
String valueNotToLook,
int toFilterIndex)
{
/**
* Returns a list with only items where the passed value does not
appears in the specified column
**/
List<String[]> list = new ArrayList<String[]>();
for (int i = 0; i < table.size(); i++) {
if (!table.get(i)[toFilterIndex].contains(valueNotToLook)) {
list.add(table.get(i));
}
}
return list;
}
}
And this is the method that transform the CSV data into a List<String[]>:
public List<String[]> getDB() throws FileNotFoundException, IOException{
/**
* Returns a table with the data of the CSV file
**/
List<String[]> data = new ArrayList<String[]>();
CSVParser parser = new CSVParserBuilder().withSeparator(sep).build();
CSVReader reader =
new CSVReaderBuilder(new FileReader(csvPath))
.withCSVParser(parser).build();
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
data.add(nextLine);
}
return data;
}
I’ve already removed the miliseconds from the modification datetimes, thinking this could be the problem but it was not the case. Unfortunally I’m out of ideas.
If you want adventure yourselfs:
Repository where is the app that writes the csv file data:
https://github.com/xlurio/drawings-admin/
- The csv must be created previously, it only writes it.
- Set the CSV path and the folder you want to get information on: “src/main/java/com/calegario/drawingsadmin/Settings.java”.
Repository where is the app that looks for the files:
https://github.com/xlurio/drawings-finder/
- Set the CSV path on: “src/main/java/com/calegario/drawingsfinder/Settings.java”.
Dependencies:
https://github.com/xlurio/file-database-gen/blob/main/target/filedbgen-1.0-SNAPSHOT.jar
https://github.com/xlurio/csv-database/blob/main/target/csvdb-1.0-SNAPSHOT.jar
https://github.com/xlurio/datatable-manager/blob/main/target/dtmanager-1.0-SNAPSHOT.jar
https://github.com/xlurio/default-windows/blob/main/target/defaultwins-1.0-SNAPSHOT.jar
To run the applications, you need to manually install the dependencies using maven, and then use the command “mvn clean compile assembly:single” to compile the apps.
Obs: I’m sorry if the code it’s not readable enough, I didn’t had the time to refactor it yet, but if it’s too hard I’ll do it soon as possible.
[ad_2]