Skip to content

Commit

Permalink
differential customerdb.read based on last_changed date
Browse files Browse the repository at this point in the history
  • Loading branch information
schorschii committed Nov 1, 2022
1 parent 66f7017 commit a998906
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void run() {
// get extra from parent intent
Intent intent = getIntent();
mCurrentAppointmentId = intent.getLongExtra("appointment-id", -1);
mCurrentAppointment = mDb.getAppointmentById(mCurrentAppointmentId);
mCurrentAppointment = mDb.getAppointmentById(mCurrentAppointmentId, false);
if(mCurrentAppointment != null) {
fillFields(mCurrentAppointment);
getSupportActionBar().setTitle(getResources().getString(R.string.edit_appointment));
Expand Down
24 changes: 17 additions & 7 deletions app/src/main/java/de/georgsieber/customerdb/CustomerDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,15 @@ private void upgradeDatabase() {
}


CustomerCalendar getCalendarById(long id, boolean showRemoved) {
List<CustomerCalendar> calendars = getCalendars(showRemoved);
for(CustomerCalendar c : calendars) {
if(c.mId == id) {
return c;
}
}
return null;
}
List<CustomerCalendar> getCalendars(boolean showRemoved) {
String sql = "SELECT id, title, color, notes, last_modified, removed FROM calendar WHERE removed = 0";
if(showRemoved) sql = "SELECT id, title, color, notes, last_modified, removed FROM calendar";
Expand Down Expand Up @@ -353,9 +362,9 @@ void removeCalendar(CustomerCalendar c) {
stmt2.execute();
}

CustomerAppointment getAppointmentById(long id) {
CustomerAppointment getAppointmentById(long id, boolean showRemoved) {
Cursor cursor = db.rawQuery(
"SELECT id, calendar_id, title, notes, time_start, time_end, fullday, customer, customer_id, location, last_modified, removed FROM appointment WHERE removed = 0 AND id = ?",
"SELECT id, calendar_id, title, notes, time_start, time_end, fullday, customer, customer_id, location, last_modified, removed FROM appointment WHERE id = ?",
new String[]{ Long.toString(id) }
);
try {
Expand All @@ -380,7 +389,7 @@ CustomerAppointment getAppointmentById(long id) {
if(cursor.getString(10) != null && (!cursor.getString(10).equals("")))
lastModified = parseDate(cursor.getString(10));
} catch (ParseException ignored) {}
return new CustomerAppointment(
CustomerAppointment appointment = new CustomerAppointment(
cursor.getLong(0),
cursor.getLong(1),
cursor.getString(2),
Expand All @@ -394,6 +403,7 @@ CustomerAppointment getAppointmentById(long id) {
lastModified,
cursor.getInt(11)
);
if(showRemoved || appointment.mRemoved == 0) return appointment;
} while (cursor.moveToNext());
}
} catch (SQLiteException e) {
Expand Down Expand Up @@ -1048,9 +1058,9 @@ public Customer getCustomerByNumber(String number) {
return null;
}

public Customer getCustomerById(long id, boolean showDeleted, boolean withFiles) {
public Customer getCustomerById(long id, boolean showRemoved, boolean withFiles) {
// Do not fetch files for all customers! We'll fetch files only for the one ID match!
List<Customer> customers = getCustomers(null, showDeleted, false, null);
List<Customer> customers = getCustomers(null, showRemoved, false, null);
for(Customer c : customers) {
if(c.mId == id) {
if(withFiles) {
Expand All @@ -1065,8 +1075,8 @@ public Customer getCustomerById(long id, boolean showDeleted, boolean withFiles)
Voucher getVoucherById(long id) {
return getVoucherById(id, false);
}
Voucher getVoucherById(long id, boolean showDeleted) {
List<Voucher> vouchers = getVouchers(null, showDeleted, null);
Voucher getVoucherById(long id, boolean showRemoved) {
List<Voucher> vouchers = getVouchers(null, showRemoved, null);
for(Voucher v : vouchers) {
if(v.mId == id) {
return v;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected void sync(final Date diffSince) {
public void run() {
try {
putCustomers(diffSince);
readCustomers();
readCustomers(diffSince);
if(mReadyListener != null) mReadyListener.ready(null);
} catch(Exception e) {
if(mReadyListener != null) mReadyListener.ready(e);
Expand Down Expand Up @@ -195,10 +195,11 @@ private void putCustomers(Date diffSince) throws Exception {
}
}

private void readCustomers() throws Exception {
private void readCustomers(Date diffSince) throws Exception {
MainActivity activity = mMainActivityReference.get();
try {
JSONObject jparams = new JSONObject();
jparams.put("diff_since", CustomerDatabase.dateToString(diffSince));
jparams.put("playstore_token", mPurchaseToken);
jparams.put("username", mUsername);
jparams.put("password", mPassword);
Expand All @@ -213,6 +214,15 @@ private void readCustomers() throws Exception {
//Log.e("API", jroot.toString());
//Log.e("API", result);

try {
JSONObject jresult = new JSONObject(result);
if(jresult.isNull("result") || !jresult.isNull("error")) {
throw new Exception(jresult.getString("error"));
}
} catch(JSONException e) {
throw new Exception(result);
}

try {
JSONObject jresult = new JSONObject(result);
JSONObject jresults = jresult.getJSONObject("result");
Expand All @@ -222,10 +232,6 @@ private void readCustomers() throws Exception {
JSONArray jappointments = jresults.getJSONArray("appointments");

activity.mDb.beginTransaction();
activity.mDb.truncateCustomers();
activity.mDb.truncateVouchers();
activity.mDb.truncateCalendars();
activity.mDb.truncateAppointments();

for(int i = 0; i < jcustomers.length(); i++) {
JSONObject jo = jcustomers.getJSONObject(i);
Expand All @@ -237,7 +243,12 @@ private void readCustomers() throws Exception {
String value = jo.getString(key);
c.putCustomerAttribute(key, value);
}
if(c.mId > 0) activity.mDb.addCustomer(c);
if(c.mId <= 0) continue;
if(activity.mDb.getCustomerById(c.mId, true, false) == null) {
activity.mDb.addCustomer(c);
} else {
activity.mDb.updateCustomer(c);
}
}
for(int i = 0; i < jcalendars.length(); i++) {
JSONObject jo = jcalendars.getJSONObject(i);
Expand All @@ -249,7 +260,12 @@ private void readCustomers() throws Exception {
String value = jo.getString(key);
c.putAttribute(key, value);
}
if(c.mId > 0) activity.mDb.addCalendar(c);
if(c.mId <= 0) continue;
if(activity.mDb.getCalendarById(c.mId, true) == null) {
activity.mDb.addCalendar(c);
} else {
activity.mDb.updateCalendar(c);
}
}
for(int i = 0; i < jappointments.length(); i++) {
JSONObject jo = jappointments.getJSONObject(i);
Expand All @@ -261,7 +277,12 @@ private void readCustomers() throws Exception {
String value = jo.getString(key);
a.putAttribute(key, value);
}
if(a.mId > 0) activity.mDb.addAppointment(a);
if(a.mId <= 0) continue;
if(activity.mDb.getAppointmentById(a.mId, true) == null) {
activity.mDb.addAppointment(a);
} else {
activity.mDb.updateAppointment(a);
}
}
for(int i = 0; i < jvouchers.length(); i++) {
JSONObject jo = jvouchers.getJSONObject(i);
Expand All @@ -273,7 +294,12 @@ private void readCustomers() throws Exception {
String value = jo.getString(key);
v.putVoucherAttribute(key, value);
}
if(v.mId > 0) activity.mDb.addVoucher(v);
if(v.mId <= 0) continue;
if(activity.mDb.getVoucherById(v.mId, true) == null) {
activity.mDb.addVoucher(v);
} else {
activity.mDb.updateVoucher(v);
}
}

activity.mDb.commitTransaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1913,7 +1913,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(newAppointments.size() > 0) {
int counter = 0;
for(CustomerAppointment ca : newAppointments) {
if(ca.mId < 1 || mDb.getAppointmentById(ca.mId) != null) {
if(ca.mId < 1 || mDb.getAppointmentById(ca.mId, true) != null) {
ca.mId = Customer.generateID(counter);
}
ca.mCalendarId = mCurrentCalendarImportSelectedId;
Expand Down

0 comments on commit a998906

Please sign in to comment.