|
| 1 | +package fr.free.nrw.commons.contributions |
| 2 | + |
| 3 | +import android.database.sqlite.SQLiteException |
| 4 | +import androidx.paging.DataSource |
| 5 | +import androidx.room.Dao |
| 6 | +import androidx.room.Delete |
| 7 | +import androidx.room.Insert |
| 8 | +import androidx.room.OnConflictStrategy |
| 9 | +import androidx.room.Query |
| 10 | +import androidx.room.Transaction |
| 11 | +import androidx.room.Update |
| 12 | +import io.reactivex.Completable |
| 13 | +import io.reactivex.Single |
| 14 | +import java.util.Calendar |
| 15 | + |
| 16 | +@Dao |
| 17 | +abstract class ContributionDao { |
| 18 | + @Query("SELECT * FROM contribution order by media_dateUploaded DESC") |
| 19 | + abstract fun fetchContributions(): DataSource.Factory<Int, Contribution> |
| 20 | + |
| 21 | + @Insert(onConflict = OnConflictStrategy.REPLACE) |
| 22 | + abstract fun saveSynchronous(contribution: Contribution) |
| 23 | + |
| 24 | + fun save(contribution: Contribution): Completable { |
| 25 | + return Completable |
| 26 | + .fromAction { |
| 27 | + contribution.dateModified = Calendar.getInstance().time |
| 28 | + if (contribution.dateUploadStarted == null) { |
| 29 | + contribution.dateUploadStarted = Calendar.getInstance().time |
| 30 | + } |
| 31 | + saveSynchronous(contribution) |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + @Transaction |
| 36 | + open fun deleteAndSaveContribution( |
| 37 | + oldContribution: Contribution, |
| 38 | + newContribution: Contribution |
| 39 | + ) { |
| 40 | + deleteSynchronous(oldContribution) |
| 41 | + saveSynchronous(newContribution) |
| 42 | + } |
| 43 | + |
| 44 | + @Insert(onConflict = OnConflictStrategy.REPLACE) |
| 45 | + abstract fun save(contribution: List<Contribution>): Single<List<Long>> |
| 46 | + |
| 47 | + @Delete |
| 48 | + abstract fun deleteSynchronous(contribution: Contribution) |
| 49 | + |
| 50 | + /** |
| 51 | + * Deletes contributions with specific states from the database. |
| 52 | + * |
| 53 | + * @param states The states of the contributions to delete. |
| 54 | + * @throws SQLiteException If an SQLite error occurs. |
| 55 | + */ |
| 56 | + @Query("DELETE FROM contribution WHERE state IN (:states)") |
| 57 | + @Throws(SQLiteException::class) |
| 58 | + abstract fun deleteContributionsWithStatesSynchronous(states: List<Int>) |
| 59 | + |
| 60 | + fun delete(contribution: Contribution): Completable { |
| 61 | + return Completable |
| 62 | + .fromAction { deleteSynchronous(contribution) } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Deletes contributions with specific states from the database. |
| 67 | + * |
| 68 | + * @param states The states of the contributions to delete. |
| 69 | + * @return A Completable indicating the result of the operation. |
| 70 | + */ |
| 71 | + fun deleteContributionsWithStates(states: List<Int>): Completable { |
| 72 | + return Completable |
| 73 | + .fromAction { deleteContributionsWithStatesSynchronous(states) } |
| 74 | + } |
| 75 | + |
| 76 | + @Query("SELECT * from contribution WHERE media_filename=:fileName") |
| 77 | + abstract fun getContributionWithTitle(fileName: String): List<Contribution> |
| 78 | + |
| 79 | + @Query("SELECT * from contribution WHERE pageId=:pageId") |
| 80 | + abstract fun getContribution(pageId: String): Contribution |
| 81 | + |
| 82 | + @Query("SELECT * from contribution WHERE state IN (:states) order by media_dateUploaded DESC") |
| 83 | + abstract fun getContribution(states: List<Int>): Single<List<Contribution>> |
| 84 | + |
| 85 | + /** |
| 86 | + * Gets contributions with specific states in descending order by the date they were uploaded. |
| 87 | + * |
| 88 | + * @param states The states of the contributions to fetch. |
| 89 | + * @return A DataSource factory for paginated contributions with the specified states. |
| 90 | + */ |
| 91 | + @Query("SELECT * from contribution WHERE state IN (:states) order by media_dateUploaded DESC") |
| 92 | + abstract fun getContributions( |
| 93 | + states: List<Int> |
| 94 | + ): DataSource.Factory<Int, Contribution> |
| 95 | + |
| 96 | + /** |
| 97 | + * Gets contributions with specific states in ascending order by the date the upload started. |
| 98 | + * |
| 99 | + * @param states The states of the contributions to fetch. |
| 100 | + * @return A DataSource factory for paginated contributions with the specified states. |
| 101 | + */ |
| 102 | + @Query("SELECT * from contribution WHERE state IN (:states) order by dateUploadStarted ASC") |
| 103 | + abstract fun getContributionsSortedByDateUploadStarted( |
| 104 | + states: List<Int> |
| 105 | + ): DataSource.Factory<Int, Contribution> |
| 106 | + |
| 107 | + @Query("SELECT COUNT(*) from contribution WHERE state in (:toUpdateStates)") |
| 108 | + abstract fun getPendingUploads(toUpdateStates: IntArray): Single<Int> |
| 109 | + |
| 110 | + @Query("Delete FROM contribution") |
| 111 | + @Throws(SQLiteException::class) |
| 112 | + abstract fun deleteAll() |
| 113 | + |
| 114 | + @Update |
| 115 | + abstract fun updateSynchronous(contribution: Contribution) |
| 116 | + |
| 117 | + /** |
| 118 | + * Updates the state of contributions with specific states. |
| 119 | + * |
| 120 | + * @param states The current states of the contributions to update. |
| 121 | + * @param newState The new state to set. |
| 122 | + */ |
| 123 | + @Query("UPDATE contribution SET state = :newState WHERE state IN (:states)") |
| 124 | + abstract fun updateContributionsState(states: List<Int>, newState: Int) |
| 125 | + |
| 126 | + fun update(contribution: Contribution): Completable { |
| 127 | + return Completable.fromAction { |
| 128 | + contribution.dateModified = Calendar.getInstance().time |
| 129 | + updateSynchronous(contribution) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + /** |
| 136 | + * Updates the state of contributions with specific states asynchronously. |
| 137 | + * |
| 138 | + * @param states The current states of the contributions to update. |
| 139 | + * @param newState The new state to set. |
| 140 | + * @return A Completable indicating the result of the operation. |
| 141 | + */ |
| 142 | + fun updateContributionsWithStates(states: List<Int>, newState: Int): Completable { |
| 143 | + return Completable |
| 144 | + .fromAction { |
| 145 | + updateContributionsState(states, newState) |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments