Created
December 9, 2011 22:16
-
-
Save onlyangel/1453563 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.onlyangel.sclbits.rosh.database.adapters; | |
import java.util.Date; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.SQLException; | |
import android.database.sqlite.SQLiteDatabase; | |
import com.onlyangel.sclbits.rosh.database.RoshDatabaseHelper; | |
public class CarrosDbAdapter { | |
// Database fields | |
public static final String KEY_ROWID = "_id"; | |
public static final String KEY_NOMBRE = "nombre"; | |
public static final String KEY_MARCA = "marca"; | |
public static final String KEY_MODELO = "modelo"; | |
public static final String KEY_ANIO = "anio"; | |
public static final String KEY_KILOMETRAJE = "kilometraje"; | |
public static final String KEY_FDC = "fdc"; | |
public static final String KEY_TIPO = "tipo"; | |
public static final String KEY_COLOR = "color"; | |
private static final String DATABASE_TABLE = "carros"; | |
private Context context; | |
private SQLiteDatabase database; | |
private RoshDatabaseHelper dbHelper; | |
public CarrosDbAdapter(Context context) { | |
this.context = context; | |
} | |
public CarrosDbAdapter open() throws SQLException { | |
dbHelper = new RoshDatabaseHelper(context); | |
database = dbHelper.getWritableDatabase(); | |
return this; | |
} | |
public void close() { | |
dbHelper.close(); | |
} | |
public long createCarro(String nombre, String marca, String modelo, String anio, String kilometraje, String tipo, String color) { | |
ContentValues initialValues = createContentValues(nombre, marca, modelo, anio, kilometraje, tipo, color, true); | |
long id = database.insert(DATABASE_TABLE, null, initialValues); | |
ServiciosDbAdapter servicios = new ServiciosDbAdapter(context); | |
servicios.open(); | |
Long time = new Date().getTime(); | |
Long carroId = new Long(id); | |
servicios.createServicio(carroId, "Mantenimiento Basico", 3*2592000000l, "3000", time, kilometraje, "basico"); | |
servicios.createServicio(carroId, "Mantenimiento Avanzado", 6*2592000000l, "6000", time, kilometraje, "abanzado"); | |
servicios.createServicio(carroId, "Cambio de Aceite", 5*2592000000l, "5000", time, kilometraje, "aceite"); | |
return id; | |
} | |
public boolean updateCarro(long rowId, String nombre, String marca, String modelo, String anio, String kilometraje, String tipo, String color) { | |
ContentValues updateValues = createContentValues(nombre, marca, modelo, anio, kilometraje, tipo, color, false); | |
return database.update(DATABASE_TABLE, updateValues, KEY_ROWID + "=" + rowId, null) > 0; | |
} | |
public boolean deleteTodo(long rowId) { | |
return database.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; | |
} | |
/** | |
* Return a Cursor over the list of all todo in the database | |
* | |
* @return Cursor over all notes | |
*/ | |
public Cursor fetchAllCarros() { | |
return database.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_NOMBRE, KEY_MARCA, KEY_MODELO, KEY_ANIO, KEY_KILOMETRAJE, KEY_FDC, KEY_TIPO, KEY_COLOR }, null, null, null, | |
null, null); | |
} | |
/** | |
* Return a Cursor positioned at the defined todo | |
*/ | |
public Cursor fetchCarro(long rowId) throws SQLException { | |
Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] { | |
KEY_ROWID, KEY_NOMBRE, KEY_MARCA, KEY_MODELO, KEY_ANIO, KEY_KILOMETRAJE, KEY_FDC, KEY_TIPO, KEY_COLOR }, | |
KEY_ROWID + "=" + rowId, null, null, null, null, null); | |
if (mCursor != null) { | |
mCursor.moveToFirst(); | |
} | |
return mCursor; | |
} | |
private ContentValues createContentValues(String nombre, String marca, | |
String modelo, String anio, String kilometraje, String tipo, String color, Boolean fdc) { | |
ContentValues values = new ContentValues(); | |
values.put(KEY_ANIO, anio); | |
if (!kilometraje.equals("")){ | |
values.put(KEY_KILOMETRAJE, new Double(kilometraje)); | |
} | |
values.put(KEY_MARCA, marca); | |
values.put(KEY_MODELO, modelo); | |
values.put(KEY_NOMBRE, nombre); | |
values.put(KEY_TIPO, tipo); | |
values.put(KEY_COLOR, color); | |
if (fdc){ | |
values.put(KEY_FDC, new Date().getTime()); | |
} | |
return values; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment