ReceiptDatabaseAdapter implements SelectionBackedDatabaseAdapter<Receipt, Trip> { @NonNull @Override public ContentValues write(@NonNull Receipt receipt, @NonNull DatabaseOperationMetadata databaseOperationMetadata) { final ContentValues values = new ContentValues(); values.put(ReceiptsTable.COLUMN_PARENT_TRIP_ID, receipt.getTrip().getId()); values.put(ReceiptsTable.COLUMN_NAME, receipt.getName().trim()); values.put(ReceiptsTable.COLUMN_CATEGORY_ID, receipt.getCategory().getId()); values.put(ReceiptsTable.COLUMN_DATE, receipt.getDate().getTime()); values.put(ReceiptsTable.COLUMN_TIMEZONE, receipt.getTimeZone().getID()); values.put(ReceiptsTable.COLUMN_COMMENT, receipt.getComment()); values.put(ReceiptsTable.COLUMN_ISO4217, receipt.getPrice().getCurrencyCode()); values.put(ReceiptsTable.COLUMN_REIMBURSABLE, receipt.isReimbursable()); values.put(ReceiptsTable.COLUMN_NOTFULLPAGEIMAGE, !receipt.isFullPage()); values.put(ReceiptsTable.COLUMN_UUID, receipt.getUuid().toString()); values.put(ReceiptsTable.COLUMN_NAME_HIDDEN_AUTO_COMPLETE, receipt.getAutoCompleteMetadata().isNameHiddenFromAutoComplete()); values.put(ReceiptsTable.COLUMN_COMMENT_HIDDEN_AUTO_COMPLETE, receipt.getAutoCompleteMetadata().isCommentHiddenFromAutoComplete()); final File file = receipt.getFile(); if (file != null) { values.put(ReceiptsTable.COLUMN_PATH, file.getName()); } else { values.put(ReceiptsTable.COLUMN_PATH, (String) null); } values.put(ReceiptsTable.COLUMN_PAYMENT_METHOD_ID, receipt.getPaymentMethod().getId()); values.put(ReceiptsTable.COLUMN_PRICE, receipt.getPrice().getPrice().doubleValue()); values.put(ReceiptsTable.COLUMN_TAX, receipt.getTax().getPrice().doubleValue()); values.put(ReceiptsTable.COLUMN_TAX2, receipt.getTax2().getPrice().doubleValue()); final BigDecimal exchangeRate = receipt.getPrice().getExchangeRate().getExchangeRate(receipt.getTrip().getDefaultCurrencyCode()); if (exchangeRate != null) { values.put(ReceiptsTable.COLUMN_EXCHANGE_RATE, exchangeRate.doubleValue()); } values.put(ReceiptsTable.COLUMN_EXTRA_EDITTEXT_1, receipt.getExtraEditText1()); values.put(ReceiptsTable.COLUMN_EXTRA_EDITTEXT_2, receipt.getExtraEditText2()); values.put(ReceiptsTable.COLUMN_EXTRA_EDITTEXT_3, receipt.getExtraEditText3()); if (databaseOperationMetadata.getOperationFamilyType() == OperationFamilyType.Sync) { values.putAll(mSyncStateAdapter.write(receipt.getSyncState())); } else { values.putAll(mSyncStateAdapter.writeUnsynced(receipt.getSyncState())); } values.put(ReceiptsTable.COLUMN_CUSTOM_ORDER_ID, receipt.getCustomOrderId()); return values; } ReceiptDatabaseAdapter(@NonNull Table<Trip> tripsTable, @NonNull Table<PaymentMethod> paymentMethodTable,
                                  @NonNull Table<Category> categoriesTable, @NonNull StorageManager storageManager);  ReceiptDatabaseAdapter(@NonNull Table<Trip> tripsTable, @NonNull Table<PaymentMethod> paymentMethodTable,
                                  @NonNull Table<Category> categoriesTable, @NonNull StorageManager storageManager,
                                  @NonNull SyncStateAdapter syncStateAdapter); @NonNull @Override Receipt read(@NonNull Cursor cursor); @NonNull @Override Receipt readForSelection(@NonNull Cursor cursor, @NonNull Trip trip, boolean isDescending); @NonNull @Override ContentValues write(@NonNull Receipt receipt, @NonNull DatabaseOperationMetadata databaseOperationMetadata); @NonNull @Override Receipt build(@NonNull Receipt receipt, int primaryKey,
                         @NonNull UUID uuid, @NonNull DatabaseOperationMetadata databaseOperationMetadata);  }
@Test public void writeUnsynced() throws Exception { final String sync = "sync"; final ContentValues syncValues = new ContentValues(); syncValues.put(sync, sync); when(mSyncStateAdapter.writeUnsynced(mSyncState)).thenReturn(syncValues); final ContentValues contentValues = mReceiptDatabaseAdapter.write(mReceipt, new DatabaseOperationMetadata()); assertEquals(RECEIPT_FILE.getName(), contentValues.getAsString(ReceiptsTable.COLUMN_PATH)); assertEquals(NAME, contentValues.getAsString(ReceiptsTable.COLUMN_NAME)); assertTrue(PARENT_TRIP_ID == contentValues.getAsInteger(ReceiptsTable.COLUMN_PARENT_TRIP_ID)); assertEquals(CATEGORY_ID, (int) contentValues.getAsInteger(ReceiptsTable.COLUMN_CATEGORY_ID)); assertEquals(PRICE, contentValues.getAsDouble(ReceiptsTable.COLUMN_PRICE), 0.0001d); assertEquals(TAX, contentValues.getAsDouble(ReceiptsTable.COLUMN_TAX), 0.0001d); assertEquals(TAX2, contentValues.getAsDouble(ReceiptsTable.COLUMN_TAX2), 0.0001d); assertEquals(EXCHANGE_RATE_FOR_USD, contentValues.getAsDouble(ReceiptsTable.COLUMN_EXCHANGE_RATE), 0.0001d); assertEquals(DATE, (long) contentValues.getAsLong(ReceiptsTable.COLUMN_DATE)); assertEquals(TIMEZONE, contentValues.getAsString(ReceiptsTable.COLUMN_TIMEZONE)); assertEquals(COMMENT, contentValues.getAsString(ReceiptsTable.COLUMN_COMMENT)); assertEquals(REIMBURSABLE, contentValues.getAsBoolean(ReceiptsTable.COLUMN_REIMBURSABLE)); assertEquals(CURRENCY_CODE, contentValues.getAsString(ReceiptsTable.COLUMN_ISO4217)); assertEquals(PAYMENT_METHOD_ID, (int) contentValues.getAsInteger(ReceiptsTable.COLUMN_PAYMENT_METHOD_ID)); assertEquals(!FULL_PAGE, contentValues.getAsBoolean(ReceiptsTable.COLUMN_NOTFULLPAGEIMAGE)); assertEquals(NAME_HIDDEN_FROM_AUTO_COMPLETE, contentValues.getAsBoolean(ReceiptsTable.COLUMN_NAME_HIDDEN_AUTO_COMPLETE)); assertEquals(COMMENT_HIDDEN_FROM_AUTO_COMPLETE, contentValues.getAsBoolean(ReceiptsTable.COLUMN_COMMENT_HIDDEN_AUTO_COMPLETE)); assertEquals(EXTRA1, contentValues.getAsString(ReceiptsTable.COLUMN_EXTRA_EDITTEXT_1)); assertEquals(EXTRA2, contentValues.getAsString(ReceiptsTable.COLUMN_EXTRA_EDITTEXT_2)); assertEquals(EXTRA3, contentValues.getAsString(ReceiptsTable.COLUMN_EXTRA_EDITTEXT_3)); assertEquals(CUSTOM_ORDER_ID, (long) contentValues.getAsLong(ReceiptsTable.COLUMN_CUSTOM_ORDER_ID)); assertEquals(RECEIPT_UUID.toString(), contentValues.getAsString(ReceiptsTable.COLUMN_UUID)); assertEquals(sync, contentValues.getAsString(sync)); assertFalse(contentValues.containsKey(ReceiptsTable.COLUMN_ID)); }
@Test public void write() throws Exception { final String sync = "sync"; final ContentValues syncValues = new ContentValues(); syncValues.put(sync, sync); when(mSyncStateAdapter.write(mSyncState)).thenReturn(syncValues); final ContentValues contentValues = mReceiptDatabaseAdapter.write(mReceipt, new DatabaseOperationMetadata(OperationFamilyType.Sync)); assertEquals(RECEIPT_FILE.getName(), contentValues.getAsString(ReceiptsTable.COLUMN_PATH)); assertEquals(NAME, contentValues.getAsString(ReceiptsTable.COLUMN_NAME)); assertTrue(PARENT_TRIP_ID == contentValues.getAsInteger(ReceiptsTable.COLUMN_PARENT_TRIP_ID)); assertEquals(CATEGORY_ID, (int) contentValues.getAsInteger(ReceiptsTable.COLUMN_CATEGORY_ID)); assertEquals(PRICE, contentValues.getAsDouble(ReceiptsTable.COLUMN_PRICE), 0.0001d); assertEquals(TAX, contentValues.getAsDouble(ReceiptsTable.COLUMN_TAX), 0.0001d); assertEquals(TAX2, contentValues.getAsDouble(ReceiptsTable.COLUMN_TAX2), 0.0001d); assertEquals(EXCHANGE_RATE_FOR_USD, contentValues.getAsDouble(ReceiptsTable.COLUMN_EXCHANGE_RATE), 0.0001d); assertEquals(DATE, (long) contentValues.getAsLong(ReceiptsTable.COLUMN_DATE)); assertEquals(TIMEZONE, contentValues.getAsString(ReceiptsTable.COLUMN_TIMEZONE)); assertEquals(COMMENT, contentValues.getAsString(ReceiptsTable.COLUMN_COMMENT)); assertEquals(REIMBURSABLE, contentValues.getAsBoolean(ReceiptsTable.COLUMN_REIMBURSABLE)); assertEquals(CURRENCY_CODE, contentValues.getAsString(ReceiptsTable.COLUMN_ISO4217)); assertEquals(PAYMENT_METHOD_ID, (int) contentValues.getAsInteger(ReceiptsTable.COLUMN_PAYMENT_METHOD_ID)); assertEquals(!FULL_PAGE, contentValues.getAsBoolean(ReceiptsTable.COLUMN_NOTFULLPAGEIMAGE)); assertEquals(NAME_HIDDEN_FROM_AUTO_COMPLETE, contentValues.getAsBoolean(ReceiptsTable.COLUMN_NAME_HIDDEN_AUTO_COMPLETE)); assertEquals(COMMENT_HIDDEN_FROM_AUTO_COMPLETE, contentValues.getAsBoolean(ReceiptsTable.COLUMN_COMMENT_HIDDEN_AUTO_COMPLETE)); assertEquals(EXTRA1, contentValues.getAsString(ReceiptsTable.COLUMN_EXTRA_EDITTEXT_1)); assertEquals(EXTRA2, contentValues.getAsString(ReceiptsTable.COLUMN_EXTRA_EDITTEXT_2)); assertEquals(EXTRA3, contentValues.getAsString(ReceiptsTable.COLUMN_EXTRA_EDITTEXT_3)); assertEquals(CUSTOM_ORDER_ID, (long) contentValues.getAsLong(ReceiptsTable.COLUMN_CUSTOM_ORDER_ID)); assertEquals(RECEIPT_UUID.toString(), contentValues.getAsString(ReceiptsTable.COLUMN_UUID)); assertEquals(sync, contentValues.getAsString(sync)); assertFalse(contentValues.containsKey(ReceiptsTable.COLUMN_ID)); }