/** * Adds a command to be saved. * * @param gameID The gameID of the command. * @param command The command to be saved. * @return True if adding was successful. Else false (in this case, attempt updating the game). * @throws PersistenceException Thrown if issues occur adding the command */ public boolean AddCommand(int gameID, ICommand command) throws PersistenceException { IPersistenceProvider provider = handler.GetPlugin(); boolean success = false; try { provider.StartTransaction(); ICommandDAO commandDAO = provider.GetCommandDAO(); if (commandDAO.GetCommandCount(gameID) <= commandLength) { String serializedCommand = Serialize(command); provider.GetCommandDAO().AddCommand(gameID, serializedCommand); success = true; } else { success = false; } provider.EndTransaction(true); } catch (PersistenceException | IOException e) { provider.EndTransaction(false); throw new PersistenceException("Error saving command", e); } return success; }
/** * Get all the commands that haven't been applied. * * @return The commands for all the games. * @throws PersistenceException Thrown if issues occur getting the commands. */ public List<ICommand> GetAllCommands() throws PersistenceException { IPersistenceProvider provider = handler.GetPlugin(); List<String> commands; try { provider.StartTransaction(); commands = provider.GetCommandDAO().GetCommands(); provider.EndTransaction(true); } catch (PersistenceException e) { provider.EndTransaction(false); throw e; } List<ICommand> convertedCommands = new ArrayList<ICommand>(commands.size()); for (String command : commands) { try { ICommand convertedCommand = Deserialize(command, ICommand.class); convertedCommands.add(convertedCommand); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } return convertedCommands; }
/** * Updates the currently saved game. * * @param sgm The server game manager to update. * @throws PersistenceException Thron if errors occur updating. */ public void UpdateGame(ServerGameManager sgm) throws PersistenceException { IPersistenceProvider provider = handler.GetPlugin(); try { provider.StartTransaction(); int gameID = sgm.GetGameID(); provider.GetCommandDAO().DeleteCommands(gameID); String blob = Serialize(sgm); provider.GetGameDAO().UpdateGame(gameID, blob); provider.EndTransaction(true); } catch (PersistenceException e) { provider.EndTransaction(false); throw e; } catch (IOException e) { // TODO Auto-generated catch block provider.EndTransaction(false); throw new PersistenceException("Unable to serialize"); } }