/** * Returns a set of all Cannons from the database * * @return */ public ArrayList<Cannon> getAll() { final String SQLGet = "SELECT * FROM cannons"; ArrayList<Cannon> result = new ArrayList<>(); Cursor cursor = db.rawQuery(SQLGet, new String[] {}); try { cursor.moveToFirst(); while (!cursor.isAfterLast()) { Cannon cannon = new Cannon(); cannon.setAttachPoint(new Coordinate(cursor.getString(1))); cannon.setEmitPoint(new Coordinate(cursor.getString(2))); String attackImagePath = cursor.getString(3); int attackImageID = ContentManager.getInstance().loadImage(attackImagePath); if (attackImageID == -1) { Log.i("modelPopulate", "Failed to load image " + attackImagePath); throw new Exception("Cannon failed to populate"); } int attackImageHeight = cursor.getInt(4); int attackImageWidth = cursor.getInt(5); String attackSoundPath = cursor.getString(6); ViewableObject attackViewable = new ViewableObject(attackImagePath, attackImageWidth, attackImageHeight, attackImageID); int attackSoundID = ContentManager.getInstance().loadSound(attackSoundPath); int damage = cursor.getInt(7); cannon.setLaserShot(new Laser(attackViewable, attackSoundPath, attackSoundID, damage)); String imagePath = cursor.getString(8); int imageID = ContentManager.getInstance().loadImage(imagePath); if (imageID == -1) { Log.i("modelPopulate", "Failed to load image " + imagePath); throw new Exception("Cannon failed to populate"); } int imageWidth = cursor.getInt(9); int imageHeight = cursor.getInt(10); cannon.setMainViewableInfo(new ViewableObject(imagePath, imageWidth, imageHeight, imageID)); result.add(cannon); cursor.moveToNext(); } } catch (Exception e) { Log.i("modelPopulate", e.getMessage()); } finally { cursor.close(); } return result; }
/** * Takes <code>cannon</code> and inserts it into the proper table * * @param cannon */ public void addCannon(Cannon cannon) { ContentValues values = new ContentValues(); values.put("attachPoint", cannon.getAttachPoint().toString()); values.put("emitPoint", cannon.getEmitPoint().toString()); values.put("attackImage", cannon.getLaserShot().getAttackViewableInfo().getImage()); values.put("attackImageHeight", cannon.getLaserShot().getAttackViewableInfo().getImageHeight()); values.put("attackImageWidth", cannon.getLaserShot().getAttackViewableInfo().getImageWidth()); values.put("attackSound", cannon.getLaserShot().getAttackSound()); values.put("damage", cannon.getLaserShot().getDamage()); values.put("image", cannon.getMainViewableInfo().getImage()); values.put("imageWidth", cannon.getMainViewableInfo().getImageWidth()); values.put("imageHeight", cannon.getMainViewableInfo().getImageHeight()); long result = db.insert("cannons", null, values); // if(result == -1) Log.i("JsonDomParserExample", "Failed to add cannon to db."); // else Log.i("JsonDomParserExample", "Successfully added cannon to db"); }