database.schema is the source file, databaseinfo.java is generated
Right now in the middle of generating the Parcelable stuff, so the code does not actually compile.
database.schema is the source file, databaseinfo.java is generated
Right now in the middle of generating the Parcelable stuff, so the code does not actually compile.
Enum VisitType | |
Val clean default | |
Val skipped_clean | |
EndEnum | |
Enum CustomerType | |
Val active default | |
Val inactive | |
EndEnum | |
Table Rounds Round | |
Col name text notnull | |
Col color color notnull | |
Fake customer_count int | |
Fake customer_due_count int | |
Fake customer_debt real | |
EndTable | |
Table Customers Customer | |
Col round_id foreign_id Rounds | |
Col address text notnull | |
Col charge real notnull | |
Col contact uri | |
Col frequency_quantity int notnull | |
Col frequency_units enum uk.co.samatkins.windowcleaningassistant.DateRange.Unit | |
Col notes text | |
Col sort_order int notnull | |
Col type enum CustomerType | |
Fake debt real | |
Fake due_visit bool | |
Fake next_due timestamp | |
Fake last_visit_date timestamp | |
Fake round_name text | |
Fake round_color color | |
EndTable | |
Table Visits Visit | |
Col customer_id foreign_id Customers | |
Col charge real notnull | |
Col date timestamp notnull | |
Col paid bool notnull | |
Col paid_date timestamp nullable | |
Col notes text | |
Col type enum VisitType | |
EndTable |
package uk.co.samatkins.windowcleaningassistant; | |
import android.content.ContentValues; | |
import android.database.Cursor; | |
import android.net.Uri; | |
import android.os.Parcel; | |
import android.os.Parcelable; | |
import android.provider.BaseColumns; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import java.lang.Override; | |
import java.lang.String; | |
import java.util.ArrayList; | |
import java.util.Calendar; | |
import java.util.List; | |
/** | |
* Contract class that defines all the database constants | |
* @author Sam | |
* Testing: http://sqlfiddle.com/#!5/7f11b/17 | |
*/ | |
public final class DatabaseInfo { | |
public static final String[] TABLES = { | |
RoundsTable.TABLE_NAME, | |
CustomersTable.TABLE_NAME, | |
VisitsTable.TABLE_NAME, | |
}; | |
private DatabaseInfo() { | |
} | |
public static class BaseTable implements BaseColumns { | |
} | |
public abstract static class Record implements Parcelable { | |
public long id; | |
@Override | |
public int describeContents() { | |
return 0; | |
} | |
} | |
public enum VisitType { | |
CLEAN, | |
SKIPPED_CLEAN; | |
public static final VisitType DEFAULT = CLEAN; | |
/** | |
* Gets a VisitType for the String. | |
* @param s Name of enum | |
* @return Either the VisitType with the given name, or DEFAULT if nothing matches or s is null. | |
*/ | |
@NonNull | |
public static VisitType fromString(@Nullable String s) { | |
VisitType result; | |
try { | |
result = valueOf(s); | |
} catch (IllegalArgumentException | NullPointerException e) { | |
result = DEFAULT; | |
} | |
return result; | |
} | |
} | |
public enum CustomerType { | |
ACTIVE, | |
INACTIVE; | |
public static final CustomerType DEFAULT = ACTIVE; | |
/** | |
* Gets a CustomerType for the String. | |
* @param s Name of enum | |
* @return Either the CustomerType with the given name, or DEFAULT if nothing matches or s is null. | |
*/ | |
@NonNull | |
public static CustomerType fromString(@Nullable String s) { | |
CustomerType result; | |
try { | |
result = valueOf(s); | |
} catch (IllegalArgumentException | NullPointerException e) { | |
result = DEFAULT; | |
} | |
return result; | |
} | |
} | |
public abstract static class RoundsTable extends BaseTable { | |
public static final String TABLE_NAME = "rounds"; | |
public static final String COLUMN_NAME = "name"; | |
public static final String COLUMN_COLOR = "color"; | |
public static final String COLUMN_CUSTOMER_COUNT = "customer_count"; | |
public static final String COLUMN_CUSTOMER_DUE_COUNT = "customer_due_count"; | |
public static final String COLUMN_CUSTOMER_DEBT = "customer_debt"; | |
public static final String[] ALL_REAL_COLUMNS = { | |
_ID, | |
COLUMN_NAME, | |
COLUMN_COLOR, | |
}; | |
public static final String SQL_CREATE = "CREATE TABLE rounds (\n" | |
+ "_id INTEGER PRIMARY KEY,\n" | |
+ "name TEXT NOT NULL,\n" | |
+ "color INTEGER NOT NULL);"; | |
@NonNull | |
public static String col(@NonNull String column) { | |
return TABLE_NAME + '.' + column; | |
} | |
} | |
public abstract static class CustomersTable extends BaseTable { | |
public static final String TABLE_NAME = "customers"; | |
public static final String COLUMN_ROUND_ID = "round_id"; | |
public static final String COLUMN_ADDRESS = "address"; | |
public static final String COLUMN_CHARGE = "charge"; | |
public static final String COLUMN_CONTACT = "contact"; | |
public static final String COLUMN_FREQUENCY_QUANTITY = "frequency_quantity"; | |
public static final String COLUMN_FREQUENCY_UNITS = "frequency_units"; | |
public static final String COLUMN_NOTES = "notes"; | |
public static final String COLUMN_SORT_ORDER = "sort_order"; | |
public static final String COLUMN_TYPE = "type"; | |
public static final String COLUMN_DEBT = "debt"; | |
public static final String COLUMN_DUE_VISIT = "due_visit"; | |
public static final String COLUMN_NEXT_DUE = "next_due"; | |
public static final String COLUMN_LAST_VISIT_DATE = "last_visit_date"; | |
public static final String COLUMN_ROUND_NAME = "round_name"; | |
public static final String COLUMN_ROUND_COLOR = "round_color"; | |
public static final String[] ALL_REAL_COLUMNS = { | |
_ID, | |
COLUMN_ROUND_ID, | |
COLUMN_ADDRESS, | |
COLUMN_CHARGE, | |
COLUMN_CONTACT, | |
COLUMN_FREQUENCY_QUANTITY, | |
COLUMN_FREQUENCY_UNITS, | |
COLUMN_NOTES, | |
COLUMN_SORT_ORDER, | |
COLUMN_TYPE, | |
}; | |
public static final String SQL_CREATE = "CREATE TABLE customers (\n" | |
+ "_id INTEGER PRIMARY KEY,\n" | |
+ "round_id INTEGER NOT NULL,\n" | |
+ "address TEXT NOT NULL,\n" | |
+ "charge REAL NOT NULL,\n" | |
+ "contact TEXT,\n" | |
+ "frequency_quantity INTEGER NOT NULL,\n" | |
+ "frequency_units TEXT NOT NULL,\n" | |
+ "notes TEXT,\n" | |
+ "sort_order INTEGER NOT NULL,\n" | |
+ "type TEXT NOT NULL,\n" | |
+ "FOREIGN KEY(round_id) REFERENCES rounds(_id));\n" | |
+ "CREATE INDEX index_customers_round_id ON customers(round_id);"; | |
@NonNull | |
public static String col(@NonNull String column) { | |
return TABLE_NAME + '.' + column; | |
} | |
} | |
public abstract static class VisitsTable extends BaseTable { | |
public static final String TABLE_NAME = "visits"; | |
public static final String COLUMN_CUSTOMER_ID = "customer_id"; | |
public static final String COLUMN_CHARGE = "charge"; | |
public static final String COLUMN_DATE = "date"; | |
public static final String COLUMN_PAID = "paid"; | |
public static final String COLUMN_PAID_DATE = "paid_date"; | |
public static final String COLUMN_NOTES = "notes"; | |
public static final String COLUMN_TYPE = "type"; | |
public static final String[] ALL_REAL_COLUMNS = { | |
_ID, | |
COLUMN_CUSTOMER_ID, | |
COLUMN_CHARGE, | |
COLUMN_DATE, | |
COLUMN_PAID, | |
COLUMN_PAID_DATE, | |
COLUMN_NOTES, | |
COLUMN_TYPE, | |
}; | |
public static final String SQL_CREATE = "CREATE TABLE visits (\n" | |
+ "_id INTEGER PRIMARY KEY,\n" | |
+ "customer_id INTEGER NOT NULL,\n" | |
+ "charge REAL NOT NULL,\n" | |
+ "date INTEGER NOT NULL,\n" | |
+ "paid INTEGER NOT NULL,\n" | |
+ "paid_date INTEGER,\n" | |
+ "notes TEXT,\n" | |
+ "type TEXT NOT NULL,\n" | |
+ "FOREIGN KEY(customer_id) REFERENCES customers(_id));\n" | |
+ "CREATE INDEX index_visits_customer_id ON visits(customer_id);"; | |
@NonNull | |
public static String col(@NonNull String column) { | |
return TABLE_NAME + '.' + column; | |
} | |
} | |
public static class Round extends Record { | |
public String name; | |
public int color; | |
public int customer_count; | |
public int customer_due_count; | |
public double customer_debt; | |
public Round() { | |
} | |
private Round(Parcel in) { | |
this.name = in.readValue(getClass().getClassLoader()); | |
this.color = in.readValue(getClass().getClassLoader()); | |
} | |
public static Round fromCursor(Cursor cursor) { | |
int ci_id = cursor.getColumnIndex("_id"); | |
int ci_name = cursor.getColumnIndex(RoundsTable.COLUMN_NAME); | |
int ci_color = cursor.getColumnIndex(RoundsTable.COLUMN_COLOR); | |
int ci_customer_count = cursor.getColumnIndex(RoundsTable.COLUMN_CUSTOMER_COUNT); | |
int ci_customer_due_count = cursor.getColumnIndex(RoundsTable.COLUMN_CUSTOMER_DUE_COUNT); | |
int ci_customer_debt = cursor.getColumnIndex(RoundsTable.COLUMN_CUSTOMER_DEBT); | |
Round item = new Round(); | |
if (ci_id != -1) { | |
item.id = cursor.getLong(ci_id); | |
} | |
if (ci_name != -1) { | |
item.name = cursor.getString(ci_name); | |
} | |
if (ci_color != -1) { | |
item.color = cursor.getInt(ci_color); | |
} | |
if (ci_customer_count != -1) { | |
item.customer_count = cursor.getInt(ci_customer_count); | |
} | |
if (ci_customer_due_count != -1) { | |
item.customer_due_count = cursor.getInt(ci_customer_due_count); | |
} | |
if (ci_customer_debt != -1) { | |
item.customer_debt = cursor.getFloat(ci_customer_debt); | |
} | |
return item; | |
} | |
public static List<Round> listFromCursor(Cursor cursor) { | |
List<Round> list = new ArrayList<>(cursor.getCount()); | |
if (cursor.moveToFirst()) { | |
int ci_id = cursor.getColumnIndex("_id"); | |
int ci_name = cursor.getColumnIndex(RoundsTable.COLUMN_NAME); | |
int ci_color = cursor.getColumnIndex(RoundsTable.COLUMN_COLOR); | |
int ci_customer_count = cursor.getColumnIndex(RoundsTable.COLUMN_CUSTOMER_COUNT); | |
int ci_customer_due_count = cursor.getColumnIndex(RoundsTable.COLUMN_CUSTOMER_DUE_COUNT); | |
int ci_customer_debt = cursor.getColumnIndex(RoundsTable.COLUMN_CUSTOMER_DEBT); | |
while (!cursor.isAfterLast()) { | |
Round item = new Round(); | |
if (ci_id != -1) { | |
item.id = cursor.getLong(ci_id); | |
} | |
if (ci_name != -1) { | |
item.name = cursor.getString(ci_name); | |
} | |
if (ci_color != -1) { | |
item.color = cursor.getInt(ci_color); | |
} | |
if (ci_customer_count != -1) { | |
item.customer_count = cursor.getInt(ci_customer_count); | |
} | |
if (ci_customer_due_count != -1) { | |
item.customer_due_count = cursor.getInt(ci_customer_due_count); | |
} | |
if (ci_customer_debt != -1) { | |
item.customer_debt = cursor.getFloat(ci_customer_debt); | |
} | |
list.add(item); | |
cursor.moveToNext(); | |
} | |
} | |
return list; | |
} | |
public ContentValues toContentValues() { | |
/* We never need to include the _ID as we always either insert a new row, | |
or update by including the ID in the URI. */; | |
ContentValues cv = new ContentValues(2); | |
cv.put(RoundsTable.COLUMN_NAME, this.name); | |
cv.put(RoundsTable.COLUMN_COLOR, this.color); | |
return cv; | |
} | |
@Override | |
public void writeToParcel(Parcel out, int flags) { | |
out.writeValue(id); | |
out.writeValue(name); | |
out.writeValue(color); | |
} | |
} | |
public static class Customer extends Record { | |
public long round_id; | |
public String address; | |
public double charge; | |
public Uri contact; | |
public int frequency_quantity; | |
public DateRange.Unit frequency_units; | |
public String notes; | |
public int sort_order; | |
public CustomerType type; | |
public double debt; | |
public boolean due_visit; | |
public Calendar next_due; | |
public Calendar last_visit_date; | |
public String round_name; | |
public int round_color; | |
public Customer() { | |
} | |
private Customer(Parcel in) { | |
this.round_id = in.readValue(getClass().getClassLoader()); | |
this.address = in.readValue(getClass().getClassLoader()); | |
this.charge = in.readValue(getClass().getClassLoader()); | |
this.contact = in.readValue(getClass().getClassLoader()); | |
this.frequency_quantity = in.readValue(getClass().getClassLoader()); | |
this.frequency_units = in.readValue(getClass().getClassLoader()); | |
this.notes = in.readValue(getClass().getClassLoader()); | |
this.sort_order = in.readValue(getClass().getClassLoader()); | |
this.type = in.readValue(getClass().getClassLoader()); | |
} | |
public static Customer fromCursor(Cursor cursor) { | |
int ci_id = cursor.getColumnIndex("_id"); | |
int ci_round_id = cursor.getColumnIndex(CustomersTable.COLUMN_ROUND_ID); | |
int ci_address = cursor.getColumnIndex(CustomersTable.COLUMN_ADDRESS); | |
int ci_charge = cursor.getColumnIndex(CustomersTable.COLUMN_CHARGE); | |
int ci_contact = cursor.getColumnIndex(CustomersTable.COLUMN_CONTACT); | |
int ci_frequency_quantity = cursor.getColumnIndex(CustomersTable.COLUMN_FREQUENCY_QUANTITY); | |
int ci_frequency_units = cursor.getColumnIndex(CustomersTable.COLUMN_FREQUENCY_UNITS); | |
int ci_notes = cursor.getColumnIndex(CustomersTable.COLUMN_NOTES); | |
int ci_sort_order = cursor.getColumnIndex(CustomersTable.COLUMN_SORT_ORDER); | |
int ci_type = cursor.getColumnIndex(CustomersTable.COLUMN_TYPE); | |
int ci_debt = cursor.getColumnIndex(CustomersTable.COLUMN_DEBT); | |
int ci_due_visit = cursor.getColumnIndex(CustomersTable.COLUMN_DUE_VISIT); | |
int ci_next_due = cursor.getColumnIndex(CustomersTable.COLUMN_NEXT_DUE); | |
int ci_last_visit_date = cursor.getColumnIndex(CustomersTable.COLUMN_LAST_VISIT_DATE); | |
int ci_round_name = cursor.getColumnIndex(CustomersTable.COLUMN_ROUND_NAME); | |
int ci_round_color = cursor.getColumnIndex(CustomersTable.COLUMN_ROUND_COLOR); | |
Customer item = new Customer(); | |
if (ci_id != -1) { | |
item.id = cursor.getLong(ci_id); | |
} | |
if (ci_round_id != -1) { | |
item.round_id = cursor.getInt(ci_round_id); | |
} | |
if (ci_address != -1) { | |
item.address = cursor.getString(ci_address); | |
} | |
if (ci_charge != -1) { | |
item.charge = cursor.getFloat(ci_charge); | |
} | |
if (ci_contact != -1) { | |
String s_contact = cursor.getString(ci_contact); | |
if (s_contact != null && !s_contact.isEmpty()) { | |
item.contact = Uri.parse(s_contact); | |
} | |
} | |
if (ci_frequency_quantity != -1) { | |
item.frequency_quantity = cursor.getInt(ci_frequency_quantity); | |
} | |
if (ci_frequency_units != -1) { | |
item.frequency_units = DateRange.Unit.fromString(cursor.getString(ci_frequency_units)); | |
} | |
if (ci_notes != -1) { | |
item.notes = cursor.getString(ci_notes); | |
} | |
if (ci_sort_order != -1) { | |
item.sort_order = cursor.getInt(ci_sort_order); | |
} | |
if (ci_type != -1) { | |
item.type = CustomerType.fromString(cursor.getString(ci_type)); | |
} | |
if (ci_debt != -1) { | |
item.debt = cursor.getFloat(ci_debt); | |
} | |
if (ci_due_visit != -1) { | |
item.due_visit = (cursor.getInt(ci_due_visit) != 0); | |
} | |
if (ci_next_due != -1) { | |
item.next_due = Calendar.getInstance(); | |
item.next_due.setTimeInMillis(cursor.getLong(ci_next_due) * 1000L); | |
} | |
if (ci_last_visit_date != -1) { | |
item.last_visit_date = Calendar.getInstance(); | |
item.last_visit_date.setTimeInMillis(cursor.getLong(ci_last_visit_date) * 1000L); | |
} | |
if (ci_round_name != -1) { | |
item.round_name = cursor.getString(ci_round_name); | |
} | |
if (ci_round_color != -1) { | |
item.round_color = cursor.getInt(ci_round_color); | |
} | |
return item; | |
} | |
public static List<Customer> listFromCursor(Cursor cursor) { | |
List<Customer> list = new ArrayList<>(cursor.getCount()); | |
if (cursor.moveToFirst()) { | |
int ci_id = cursor.getColumnIndex("_id"); | |
int ci_round_id = cursor.getColumnIndex(CustomersTable.COLUMN_ROUND_ID); | |
int ci_address = cursor.getColumnIndex(CustomersTable.COLUMN_ADDRESS); | |
int ci_charge = cursor.getColumnIndex(CustomersTable.COLUMN_CHARGE); | |
int ci_contact = cursor.getColumnIndex(CustomersTable.COLUMN_CONTACT); | |
int ci_frequency_quantity = cursor.getColumnIndex(CustomersTable.COLUMN_FREQUENCY_QUANTITY); | |
int ci_frequency_units = cursor.getColumnIndex(CustomersTable.COLUMN_FREQUENCY_UNITS); | |
int ci_notes = cursor.getColumnIndex(CustomersTable.COLUMN_NOTES); | |
int ci_sort_order = cursor.getColumnIndex(CustomersTable.COLUMN_SORT_ORDER); | |
int ci_type = cursor.getColumnIndex(CustomersTable.COLUMN_TYPE); | |
int ci_debt = cursor.getColumnIndex(CustomersTable.COLUMN_DEBT); | |
int ci_due_visit = cursor.getColumnIndex(CustomersTable.COLUMN_DUE_VISIT); | |
int ci_next_due = cursor.getColumnIndex(CustomersTable.COLUMN_NEXT_DUE); | |
int ci_last_visit_date = cursor.getColumnIndex(CustomersTable.COLUMN_LAST_VISIT_DATE); | |
int ci_round_name = cursor.getColumnIndex(CustomersTable.COLUMN_ROUND_NAME); | |
int ci_round_color = cursor.getColumnIndex(CustomersTable.COLUMN_ROUND_COLOR); | |
while (!cursor.isAfterLast()) { | |
Customer item = new Customer(); | |
if (ci_id != -1) { | |
item.id = cursor.getLong(ci_id); | |
} | |
if (ci_round_id != -1) { | |
item.round_id = cursor.getInt(ci_round_id); | |
} | |
if (ci_address != -1) { | |
item.address = cursor.getString(ci_address); | |
} | |
if (ci_charge != -1) { | |
item.charge = cursor.getFloat(ci_charge); | |
} | |
if (ci_contact != -1) { | |
String s_contact = cursor.getString(ci_contact); | |
if (s_contact != null && !s_contact.isEmpty()) { | |
item.contact = Uri.parse(s_contact); | |
} | |
} | |
if (ci_frequency_quantity != -1) { | |
item.frequency_quantity = cursor.getInt(ci_frequency_quantity); | |
} | |
if (ci_frequency_units != -1) { | |
item.frequency_units = DateRange.Unit.fromString(cursor.getString(ci_frequency_units)); | |
} | |
if (ci_notes != -1) { | |
item.notes = cursor.getString(ci_notes); | |
} | |
if (ci_sort_order != -1) { | |
item.sort_order = cursor.getInt(ci_sort_order); | |
} | |
if (ci_type != -1) { | |
item.type = CustomerType.fromString(cursor.getString(ci_type)); | |
} | |
if (ci_debt != -1) { | |
item.debt = cursor.getFloat(ci_debt); | |
} | |
if (ci_due_visit != -1) { | |
item.due_visit = (cursor.getInt(ci_due_visit) != 0); | |
} | |
if (ci_next_due != -1) { | |
item.next_due = Calendar.getInstance(); | |
item.next_due.setTimeInMillis(cursor.getLong(ci_next_due) * 1000L); | |
} | |
if (ci_last_visit_date != -1) { | |
item.last_visit_date = Calendar.getInstance(); | |
item.last_visit_date.setTimeInMillis(cursor.getLong(ci_last_visit_date) * 1000L); | |
} | |
if (ci_round_name != -1) { | |
item.round_name = cursor.getString(ci_round_name); | |
} | |
if (ci_round_color != -1) { | |
item.round_color = cursor.getInt(ci_round_color); | |
} | |
list.add(item); | |
cursor.moveToNext(); | |
} | |
} | |
return list; | |
} | |
public ContentValues toContentValues() { | |
/* We never need to include the _ID as we always either insert a new row, | |
or update by including the ID in the URI. */; | |
ContentValues cv = new ContentValues(9); | |
cv.put(CustomersTable.COLUMN_ROUND_ID, this.round_id); | |
cv.put(CustomersTable.COLUMN_ADDRESS, this.address); | |
cv.put(CustomersTable.COLUMN_CHARGE, this.charge); | |
if (this.contact != null) { | |
cv.put(CustomersTable.COLUMN_CONTACT, this.contact.toString()); | |
} | |
cv.put(CustomersTable.COLUMN_FREQUENCY_QUANTITY, this.frequency_quantity); | |
if (this.frequency_units != null) { | |
cv.put(CustomersTable.COLUMN_FREQUENCY_UNITS, this.frequency_units.name()); | |
} | |
cv.put(CustomersTable.COLUMN_NOTES, this.notes); | |
cv.put(CustomersTable.COLUMN_SORT_ORDER, this.sort_order); | |
if (this.type != null) { | |
cv.put(CustomersTable.COLUMN_TYPE, this.type.name()); | |
} | |
return cv; | |
} | |
@Override | |
public void writeToParcel(Parcel out, int flags) { | |
out.writeValue(id); | |
out.writeValue(round_id); | |
out.writeValue(address); | |
out.writeValue(charge); | |
out.writeValue(contact); | |
out.writeValue(frequency_quantity); | |
out.writeValue(frequency_units); | |
out.writeValue(notes); | |
out.writeValue(sort_order); | |
out.writeValue(type); | |
} | |
} | |
public static class Visit extends Record { | |
public long customer_id; | |
public double charge; | |
public Calendar date; | |
public boolean paid; | |
public Calendar paid_date; | |
public String notes; | |
public VisitType type; | |
public Visit() { | |
} | |
private Visit(Parcel in) { | |
this.customer_id = in.readValue(getClass().getClassLoader()); | |
this.charge = in.readValue(getClass().getClassLoader()); | |
this.date = in.readValue(getClass().getClassLoader()); | |
this.paid = in.readValue(getClass().getClassLoader()); | |
this.paid_date = in.readValue(getClass().getClassLoader()); | |
this.notes = in.readValue(getClass().getClassLoader()); | |
this.type = in.readValue(getClass().getClassLoader()); | |
} | |
public static Visit fromCursor(Cursor cursor) { | |
int ci_id = cursor.getColumnIndex("_id"); | |
int ci_customer_id = cursor.getColumnIndex(VisitsTable.COLUMN_CUSTOMER_ID); | |
int ci_charge = cursor.getColumnIndex(VisitsTable.COLUMN_CHARGE); | |
int ci_date = cursor.getColumnIndex(VisitsTable.COLUMN_DATE); | |
int ci_paid = cursor.getColumnIndex(VisitsTable.COLUMN_PAID); | |
int ci_paid_date = cursor.getColumnIndex(VisitsTable.COLUMN_PAID_DATE); | |
int ci_notes = cursor.getColumnIndex(VisitsTable.COLUMN_NOTES); | |
int ci_type = cursor.getColumnIndex(VisitsTable.COLUMN_TYPE); | |
Visit item = new Visit(); | |
if (ci_id != -1) { | |
item.id = cursor.getLong(ci_id); | |
} | |
if (ci_customer_id != -1) { | |
item.customer_id = cursor.getInt(ci_customer_id); | |
} | |
if (ci_charge != -1) { | |
item.charge = cursor.getFloat(ci_charge); | |
} | |
if (ci_date != -1) { | |
item.date = Calendar.getInstance(); | |
item.date.setTimeInMillis(cursor.getLong(ci_date) * 1000L); | |
} | |
if (ci_paid != -1) { | |
item.paid = (cursor.getInt(ci_paid) != 0); | |
} | |
if (ci_paid_date != -1) { | |
item.paid_date = Calendar.getInstance(); | |
item.paid_date.setTimeInMillis(cursor.getLong(ci_paid_date) * 1000L); | |
} | |
if (ci_notes != -1) { | |
item.notes = cursor.getString(ci_notes); | |
} | |
if (ci_type != -1) { | |
item.type = VisitType.fromString(cursor.getString(ci_type)); | |
} | |
return item; | |
} | |
public static List<Visit> listFromCursor(Cursor cursor) { | |
List<Visit> list = new ArrayList<>(cursor.getCount()); | |
if (cursor.moveToFirst()) { | |
int ci_id = cursor.getColumnIndex("_id"); | |
int ci_customer_id = cursor.getColumnIndex(VisitsTable.COLUMN_CUSTOMER_ID); | |
int ci_charge = cursor.getColumnIndex(VisitsTable.COLUMN_CHARGE); | |
int ci_date = cursor.getColumnIndex(VisitsTable.COLUMN_DATE); | |
int ci_paid = cursor.getColumnIndex(VisitsTable.COLUMN_PAID); | |
int ci_paid_date = cursor.getColumnIndex(VisitsTable.COLUMN_PAID_DATE); | |
int ci_notes = cursor.getColumnIndex(VisitsTable.COLUMN_NOTES); | |
int ci_type = cursor.getColumnIndex(VisitsTable.COLUMN_TYPE); | |
while (!cursor.isAfterLast()) { | |
Visit item = new Visit(); | |
if (ci_id != -1) { | |
item.id = cursor.getLong(ci_id); | |
} | |
if (ci_customer_id != -1) { | |
item.customer_id = cursor.getInt(ci_customer_id); | |
} | |
if (ci_charge != -1) { | |
item.charge = cursor.getFloat(ci_charge); | |
} | |
if (ci_date != -1) { | |
item.date = Calendar.getInstance(); | |
item.date.setTimeInMillis(cursor.getLong(ci_date) * 1000L); | |
} | |
if (ci_paid != -1) { | |
item.paid = (cursor.getInt(ci_paid) != 0); | |
} | |
if (ci_paid_date != -1) { | |
item.paid_date = Calendar.getInstance(); | |
item.paid_date.setTimeInMillis(cursor.getLong(ci_paid_date) * 1000L); | |
} | |
if (ci_notes != -1) { | |
item.notes = cursor.getString(ci_notes); | |
} | |
if (ci_type != -1) { | |
item.type = VisitType.fromString(cursor.getString(ci_type)); | |
} | |
list.add(item); | |
cursor.moveToNext(); | |
} | |
} | |
return list; | |
} | |
public ContentValues toContentValues() { | |
/* We never need to include the _ID as we always either insert a new row, | |
or update by including the ID in the URI. */; | |
ContentValues cv = new ContentValues(7); | |
cv.put(VisitsTable.COLUMN_CUSTOMER_ID, this.customer_id); | |
cv.put(VisitsTable.COLUMN_CHARGE, this.charge); | |
if (this.date != null) { | |
cv.put(VisitsTable.COLUMN_DATE, this.date.getTime().getTime() / 1000L); | |
} | |
cv.put(VisitsTable.COLUMN_PAID, this.paid); | |
if (this.paid_date != null) { | |
cv.put(VisitsTable.COLUMN_PAID_DATE, this.paid_date.getTime().getTime() / 1000L); | |
} | |
cv.put(VisitsTable.COLUMN_NOTES, this.notes); | |
if (this.type != null) { | |
cv.put(VisitsTable.COLUMN_TYPE, this.type.name()); | |
} | |
return cv; | |
} | |
@Override | |
public void writeToParcel(Parcel out, int flags) { | |
out.writeValue(id); | |
out.writeValue(customer_id); | |
out.writeValue(charge); | |
out.writeValue(date); | |
out.writeValue(paid); | |
out.writeValue(paid_date); | |
out.writeValue(notes); | |
out.writeValue(type); | |
} | |
} | |
} |