package xyz.veronie.bgg.ui.handlers; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.HashSet; import java.util.List; import java.util.Set; import org.eclipse.core.runtime.Path; import org.eclipse.e4.core.di.annotations.CanExecute; import org.eclipse.e4.core.di.annotations.Execute; import org.eclipse.e4.core.services.events.IEventBroker; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import xyz.veronie.bgg.result.BggApi; import xyz.veronie.bgg.result.Thing; import xyz.veronie.bgg.result.ThingProvider; import xyz.veronie.bgg.types.EventConstants; public class ImportResultTxtHandler { private Shell shell; @Execute public void execute(Shell shell, ThingProvider thingProvider, BggApi bggApi, IEventBroker eventBroker) { this.shell = shell; FileDialog dialog = new FileDialog(shell, SWT.OPEN); dialog.setText("Select a result.txt from bgg1tool"); String[] exfilters = { "*.txt" }; dialog.setFilterExtensions(exfilters); String resultPath = dialog.open(); if(resultPath != null && !resultPath.isEmpty()) { Set thingIds = parseResultTxtIds(resultPath); if(thingIds != null && !thingIds.isEmpty()) { String listName = new Path(resultPath).lastSegment() + " (unsaved)"; eventBroker.send(EventConstants.TOPIC_STATUS, "Fetching entries from BGG..."); List things = bggApi.getThings(thingIds); thingProvider.importList(things, listName); eventBroker.send(EventConstants.TOPIC_RESULT_CHANGED, listName); eventBroker.send(EventConstants.TOPIC_STATUS, "Fetched " + Integer.toString(things.size()) + " things."); } } } private Set parseResultTxtIds(String resultPath) { FileReader input = null; String myLine = null; int lineNo = 0; // count overall lines int errNo = 0; // count unparsable lines int dupNo = 0; // count duplicates boolean header = false; try { input = new FileReader(resultPath); BufferedReader bufferedReader = new BufferedReader(input); Set ids = new HashSet(); while ( (myLine = bufferedReader.readLine()) != null) { lineNo++; String[] tokens = myLine.split(","); if(tokens.length > 0) { if(tokens[0].equals("id")) { header = true; continue; // header line } try { boolean exists = !ids.add(Integer.parseInt(tokens[0])); if(exists) { System.out.println("DEBUG: duplicate id: " + tokens[0]); dupNo++; } } catch (NumberFormatException e) { errNo++; System.out.println("WARN: Could not parse id from line " + Integer.toString(lineNo) + ", line starts with '" + tokens[0] + "'"); } } } bufferedReader.close(); if(ids != null) { MessageBox msgBox = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK); StringBuilder msg = new StringBuilder(); int thingsNo = lineNo - errNo - dupNo; if(header) thingsNo -= 1; msg.append("Parsing successful. Found ").append(Integer.toString(thingsNo)).append(" things."); if(errNo > 0) { msg.append("\n\rSkipped ").append(Integer.toString(errNo)).append(" line(s)."); } if(dupNo > 0) { msg.append("\n\r").append(Integer.toString(dupNo)).append(" duplicate ids"); } msg.append("\n\r"); msgBox.setMessage(msg.toString()); msgBox.open(); } System.out.println("TRACE: " + ids); return ids; } catch (FileNotFoundException e) { MessageBox msgBox = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK); msgBox.setMessage("Could not open file '" + resultPath + "'."); msgBox.open(); e.printStackTrace(); } catch (IOException e) { MessageBox msgBox = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK); String msg = "Error while parsing '" + resultPath + "'.\r\n" + "It must have comma separated lines starting with an integer number.\r\n"; msgBox.setMessage(msg); msgBox.open(); e.printStackTrace(); } return null; } @CanExecute public boolean canExecute() { return true; } }