TODO: update table properly, don't freeze while parsing xml...pull/3/head
| @@ -36,6 +36,5 @@ public class LocalDbAdapterService { | |||
| public List<String> loadThingListNames() throws SQLException { | |||
| return sqliteController.getThingLists(); | |||
| } | |||
| } | |||
| @@ -266,4 +266,5 @@ public class SqliteController { | |||
| } | |||
| } | |||
| @@ -62,9 +62,20 @@ public class BggApi { | |||
| } | |||
| } | |||
| /// retrieve a list of things from BGG with the given ids | |||
| public ArrayList<Thing> getThings(List<Integer> ids) { | |||
| // TODO: retrieve a list of things from BGG with the given ids | |||
| return new ArrayList<Thing>(); | |||
| StringBuilder urlStr = new StringBuilder(); | |||
| urlStr.append(BASE_URL + "thing?id="); | |||
| String sep = ""; | |||
| for (Integer integer : ids) { | |||
| urlStr.append(sep + integer.toString()); | |||
| if(sep.isEmpty()) { | |||
| sep = ","; | |||
| } | |||
| } | |||
| return getThings(urlStr.toString()); | |||
| } | |||
| @@ -209,7 +220,7 @@ public class BggApi { | |||
| if (nNode.getNodeType() == Node.ELEMENT_NODE) { | |||
| Element eElement = (Element) nNode; | |||
| // thing | |||
| // thing when fetching by user | |||
| if( eElement.hasAttribute("objecttype") | |||
| && eElement.getAttribute("objecttype").equals("thing")) | |||
| { | |||
| @@ -221,12 +232,45 @@ public class BggApi { | |||
| getValue(eElement, "image"), | |||
| getValue(eElement, "thumbnail"), | |||
| getValue(eElement, "comment"), | |||
| Integer.parseInt(getValue(eElement, "numplays")) | |||
| Integer.parseInt(getValue(eElement, "numplays")) | |||
| ); | |||
| Thing thing = new Thing(id, tmd); | |||
| Thing thing = new Thing(id, tmd); | |||
| things.add(thing); | |||
| } | |||
| } | |||
| // when fetching things by id, type is boardgame | |||
| else if(eElement.hasAttribute("type") && | |||
| eElement.getAttribute("type").equals("boardgame")) | |||
| { | |||
| Integer id = Integer.parseInt(eElement.getAttribute("id")); | |||
| if(id != 0) { | |||
| String name = ""; | |||
| NodeList nameList = eElement.getElementsByTagName("name"); | |||
| for(int n = 0; n < nameList.getLength(); n++) { | |||
| Node nameNode = nameList.item(n); | |||
| if (nNode.getNodeType() == Node.ELEMENT_NODE) { | |||
| Element nameElement = (Element) nameNode; | |||
| if(nameElement.hasAttribute("type") | |||
| && nameElement.getAttribute("type").equals("primary")) | |||
| { | |||
| name = nameElement.getAttribute("value"); | |||
| break; | |||
| } | |||
| } | |||
| } | |||
| ThingMetaData tmd = new ThingMetaData( | |||
| id, | |||
| name, | |||
| getValue(eElement, "image"), | |||
| getValue(eElement, "thumbnail"), | |||
| getValue(eElement, "comment"), | |||
| null | |||
| ); | |||
| Thing thing = new Thing(id, tmd); | |||
| things.add(thing); | |||
| } | |||
| } | |||
| // family has "type" | |||
| else if(eElement.hasAttribute("type") | |||
| && eElement.getAttribute("type").equals("boardgamefamily")) | |||
| @@ -317,6 +361,7 @@ public class BggApi { | |||
| return null; | |||
| } | |||
| private void checkForErrors(Document doc) throws IllegalArgumentException { | |||
| NodeList nList = doc.getElementsByTagName("error"); | |||
| if(nList.getLength() > 0) { | |||
| @@ -97,6 +97,14 @@ public class ThingProvider { | |||
| // TODO: handle unsaved current list | |||
| this.things = localDbAdapterService.loadThingList(name); | |||
| } | |||
| /// store things as a new list | |||
| public void storeThings(List<Thing> importThings, String listName) throws SQLException { | |||
| if(importThings != null && !importThings.isEmpty()) { | |||
| this.things = importThings; | |||
| localDbAdapterService.storeThingList(importThings, listName); | |||
| } | |||
| } | |||
| public int numberOfThings() { | |||
| return things.size(); | |||
| @@ -2,9 +2,13 @@ package xyz.veronie.bgg.ui.dialogs; | |||
| import java.util.List; | |||
| import javax.inject.Inject; | |||
| import org.eclipse.core.commands.ParameterizedCommand; | |||
| import org.eclipse.e4.core.commands.ECommandService; | |||
| import org.eclipse.e4.core.commands.EHandlerService; | |||
| import org.eclipse.e4.core.di.annotations.Optional; | |||
| import org.eclipse.e4.ui.di.UIEventTopic; | |||
| import org.eclipse.jface.dialogs.Dialog; | |||
| import org.eclipse.jface.dialogs.IDialogConstants; | |||
| import org.eclipse.jface.viewers.ArrayContentProvider; | |||
| @@ -20,6 +24,7 @@ import org.eclipse.swt.events.MouseEvent; | |||
| import org.eclipse.swt.graphics.Point; | |||
| import org.eclipse.swt.layout.GridData; | |||
| import org.eclipse.swt.layout.GridLayout; | |||
| import org.eclipse.swt.widgets.Button; | |||
| import org.eclipse.swt.widgets.Composite; | |||
| import org.eclipse.swt.widgets.Control; | |||
| import org.eclipse.swt.widgets.Label; | |||
| @@ -28,26 +33,38 @@ import org.eclipse.swt.widgets.Table; | |||
| import org.eclipse.swt.widgets.TableColumn; | |||
| import org.eclipse.wb.swt.SWTResourceManager; | |||
| import xyz.veronie.bgg.types.EventConstants; | |||
| import xyz.veronie.bgg.ui.helpers.BatColors; | |||
| import org.eclipse.swt.widgets.Button; | |||
| @SuppressWarnings("restriction") | |||
| public class LoadGameListDialog extends Dialog { | |||
| private String selectedName; | |||
| private List<String> thingLists; | |||
| private ECommandService commandService; | |||
| private EHandlerService handlerService; | |||
| /** | |||
| * Create the dialog. | |||
| * @param parentShell | |||
| * @param handlerService | |||
| * @param commandService | |||
| */ | |||
| public LoadGameListDialog(Shell parentShell, List<String> thingLists) { | |||
| public LoadGameListDialog(Shell parentShell, List<String> thingLists, | |||
| ECommandService commandService, EHandlerService handlerService) | |||
| { | |||
| super(parentShell); | |||
| this.thingLists = thingLists; | |||
| this.commandService = commandService; | |||
| this.handlerService = handlerService; | |||
| setShellStyle(SWT.BORDER | SWT.RESIZE | SWT.APPLICATION_MODAL); | |||
| } | |||
| /** | |||
| * Create contents of the dialog. | |||
| * | |||
| * @param parent | |||
| */ | |||
| @Override | |||
| @@ -130,7 +147,7 @@ public class LoadGameListDialog extends Dialog { | |||
| }); | |||
| btnImport.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1)); | |||
| btnImport.setBounds(0, 0, 75, 25); | |||
| btnImport.setText("Import from BggTool1 result.txt..."); | |||
| btnImport.setText("Import from bgg1tool result.txt..."); | |||
| return container; | |||
| } | |||
| @@ -142,12 +159,12 @@ public class LoadGameListDialog extends Dialog { | |||
| } | |||
| private void importFromBggTool1Result() { | |||
| // ECommandService commandService, EHandlerService handlerService | |||
| // ParameterizedCommand cmd = | |||
| // commandService.createCommand("xyz.veronie.bgg.ui.command.importResultTxt", null); | |||
| // if (handlerService.canExecute(cmd)){ | |||
| // handlerService.executeHandler(cmd); | |||
| // } | |||
| ParameterizedCommand cmd = | |||
| commandService.createCommand("xyz.veronie.bgg.ui.command.importResultTxt", null); | |||
| if (handlerService.canExecute(cmd)){ | |||
| handlerService.executeHandler(cmd); | |||
| } | |||
| closeDialogOk(); | |||
| } | |||
| /** | |||
| @@ -173,4 +190,15 @@ public class LoadGameListDialog extends Dialog { | |||
| public String getSelectedName() { | |||
| return selectedName; | |||
| } | |||
| // TODO: is not called | |||
| @Inject | |||
| @Optional | |||
| private void subscribeTopicThingsSaved | |||
| (@UIEventTopic(EventConstants.TOPIC_RESULT_CHANGED) | |||
| String listName) { | |||
| System.out.println("LoadGameListDialog: TOPIC_RESULT_CHANGED"); | |||
| closeDialogOk(); | |||
| } | |||
| } | |||
| @@ -6,6 +6,8 @@ import java.util.List; | |||
| import javax.inject.Inject; | |||
| import org.eclipse.e4.core.commands.ECommandService; | |||
| import org.eclipse.e4.core.commands.EHandlerService; | |||
| import org.eclipse.e4.core.di.annotations.CanExecute; | |||
| import org.eclipse.e4.core.di.annotations.Execute; | |||
| import org.eclipse.e4.core.services.events.IEventBroker; | |||
| @@ -18,13 +20,14 @@ import xyz.veronie.bgg.result.ThingProvider; | |||
| import xyz.veronie.bgg.types.EventConstants; | |||
| import xyz.veronie.bgg.ui.dialogs.LoadGameListDialog; | |||
| @SuppressWarnings("restriction") | |||
| public class HandleLoadGamelist { | |||
| @Inject | |||
| ThingProvider thingProvider; | |||
| @Execute | |||
| public void execute(Shell shell, IEventBroker eventBroker) { | |||
| public void execute(Shell shell, IEventBroker eventBroker, ECommandService commandService, EHandlerService handlerService) { | |||
| try { | |||
| List<String> thingLists = thingProvider.getThingListNames(); | |||
| @@ -36,7 +39,7 @@ public class HandleLoadGamelist { | |||
| return; | |||
| } | |||
| LoadGameListDialog loadDialog = new LoadGameListDialog(shell, thingLists); | |||
| LoadGameListDialog loadDialog = new LoadGameListDialog(shell, thingLists, commandService, handlerService); | |||
| loadDialog.open(); | |||
| int returnCode = loadDialog.getReturnCode(); | |||
| @@ -5,23 +5,32 @@ import java.io.BufferedReader; | |||
| import java.io.FileNotFoundException; | |||
| import java.io.FileReader; | |||
| import java.io.IOException; | |||
| import java.sql.SQLException; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| 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.jface.dialogs.Dialog; | |||
| 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; | |||
| import xyz.veronie.bgg.ui.dialogs.SaveGameListDialog; | |||
| public class ImportResultTxtHandler { | |||
| private Shell shell; | |||
| @Execute | |||
| public void execute(Shell shell) { | |||
| public void execute(Shell shell, ThingProvider thingProvider, BggApi bggApi, IEventBroker eventBroker) { | |||
| this.shell = shell; | |||
| FileDialog dialog = new FileDialog(shell, SWT.OPEN); | |||
| @@ -32,7 +41,27 @@ public class ImportResultTxtHandler { | |||
| if(resultPath != null && !resultPath.isEmpty()) { | |||
| List<Integer> thingIds = parseResultTxtIds(resultPath); | |||
| if(thingIds != null && !thingIds.isEmpty()) { | |||
| SaveGameListDialog saveDialog = new SaveGameListDialog(shell); | |||
| saveDialog.open(); | |||
| int returnCode = saveDialog.getReturnCode(); | |||
| if(returnCode == Dialog.OK) { | |||
| String listName = saveDialog.getEntryText(); | |||
| try { | |||
| eventBroker.send(EventConstants.TOPIC_STATUS, "Fetching entries from BGG..."); | |||
| List<Thing> things = bggApi.getThings(thingIds); | |||
| thingProvider.storeThings(things, listName); | |||
| eventBroker.send(EventConstants.TOPIC_RESULT_CHANGED, listName); | |||
| eventBroker.send(EventConstants.TOPIC_STATUS, "Fetched " + Integer.toString(things.size()) + " things."); | |||
| } catch (SQLException e) { | |||
| MessageBox msgErrorBox = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK); | |||
| msgErrorBox.setMessage("Could not save imported result.txt as game list."); | |||
| msgErrorBox.open(); | |||
| e.printStackTrace(); | |||
| } | |||
| } | |||
| } | |||
| } | |||
| } | |||
| @@ -40,6 +69,8 @@ public class ImportResultTxtHandler { | |||
| private List<Integer> parseResultTxtIds(String resultPath) { | |||
| FileReader input = null; | |||
| String myLine = null; | |||
| int lineNo = 0; | |||
| int errNo = 0; | |||
| try { | |||
| input = new FileReader(resultPath); | |||
| @@ -47,14 +78,38 @@ public class ImportResultTxtHandler { | |||
| List<Integer> ids = new ArrayList<Integer>(); | |||
| while ( (myLine = bufferedReader.readLine()) != null) | |||
| { | |||
| { | |||
| String[] tokens = myLine.split(","); | |||
| if(tokens.length > 1) { | |||
| ids.add(Integer.parseInt(tokens[0])); | |||
| if(tokens[0].equals("id")) continue; // header line | |||
| try { | |||
| ids.add(Integer.parseInt(tokens[0])); | |||
| lineNo++; | |||
| } | |||
| 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(); | |||
| msg.append("Parsing successful. Found ").append(Integer.toString(lineNo)).append(" things."); | |||
| if(errNo > 0) { | |||
| msg.append("Skipped ").append(Integer.toString(errNo)).append(" line(s).\n\r"); | |||
| } else { | |||
| msg.append("\n\r"); | |||
| } | |||
| msg.append("Next, you will be asked to save the imported list as a new game list."); | |||
| msgBox.setMessage(msg.toString()); | |||
| msgBox.open(); | |||
| } | |||
| System.out.println("TRACE: " + ids); | |||
| return ids; | |||
| @@ -71,17 +126,7 @@ public class ImportResultTxtHandler { | |||
| msgBox.open(); | |||
| e.printStackTrace(); | |||
| } | |||
| catch (NumberFormatException 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"; | |||
| if(myLine != null) { | |||
| msg.concat("The offending line reads\r\n" + myLine); | |||
| } | |||
| msgBox.setMessage(msg); | |||
| msgBox.open(); | |||
| e.printStackTrace(); | |||
| } | |||
| return null; | |||
| } | |||