From a9989066449eb59b8754727f1275604b564300c6 Mon Sep 17 00:00:00 2001 From: Georg Sieber Date: Tue, 1 Nov 2022 21:03:31 +0100 Subject: [PATCH] differential customerdb.read based on last_changed date --- .../CalendarAppointmentEditActivity.java | 2 +- .../customerdb/CustomerDatabase.java | 24 +++++++--- .../customerdb/CustomerDatabaseApi.java | 46 +++++++++++++++---- .../georgsieber/customerdb/MainActivity.java | 2 +- 4 files changed, 55 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/de/georgsieber/customerdb/CalendarAppointmentEditActivity.java b/app/src/main/java/de/georgsieber/customerdb/CalendarAppointmentEditActivity.java index ca32f35..a29d4b3 100644 --- a/app/src/main/java/de/georgsieber/customerdb/CalendarAppointmentEditActivity.java +++ b/app/src/main/java/de/georgsieber/customerdb/CalendarAppointmentEditActivity.java @@ -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)); diff --git a/app/src/main/java/de/georgsieber/customerdb/CustomerDatabase.java b/app/src/main/java/de/georgsieber/customerdb/CustomerDatabase.java index 77678f8..3d3744f 100644 --- a/app/src/main/java/de/georgsieber/customerdb/CustomerDatabase.java +++ b/app/src/main/java/de/georgsieber/customerdb/CustomerDatabase.java @@ -281,6 +281,15 @@ private void upgradeDatabase() { } + CustomerCalendar getCalendarById(long id, boolean showRemoved) { + List calendars = getCalendars(showRemoved); + for(CustomerCalendar c : calendars) { + if(c.mId == id) { + return c; + } + } + return null; + } List 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"; @@ -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 { @@ -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), @@ -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) { @@ -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 customers = getCustomers(null, showDeleted, false, null); + List customers = getCustomers(null, showRemoved, false, null); for(Customer c : customers) { if(c.mId == id) { if(withFiles) { @@ -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 vouchers = getVouchers(null, showDeleted, null); + Voucher getVoucherById(long id, boolean showRemoved) { + List vouchers = getVouchers(null, showRemoved, null); for(Voucher v : vouchers) { if(v.mId == id) { return v; diff --git a/app/src/main/java/de/georgsieber/customerdb/CustomerDatabaseApi.java b/app/src/main/java/de/georgsieber/customerdb/CustomerDatabaseApi.java index f10be92..e868cc1 100644 --- a/app/src/main/java/de/georgsieber/customerdb/CustomerDatabaseApi.java +++ b/app/src/main/java/de/georgsieber/customerdb/CustomerDatabaseApi.java @@ -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); @@ -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); @@ -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"); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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(); diff --git a/app/src/main/java/de/georgsieber/customerdb/MainActivity.java b/app/src/main/java/de/georgsieber/customerdb/MainActivity.java index 69b4d89..240e299 100644 --- a/app/src/main/java/de/georgsieber/customerdb/MainActivity.java +++ b/app/src/main/java/de/georgsieber/customerdb/MainActivity.java @@ -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;