|
| 1 | +package fr.free.nrw.commons.contributions; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.net.Uri; |
| 5 | +import android.os.Parcel; |
| 6 | + |
| 7 | +import androidx.annotation.NonNull; |
| 8 | +import androidx.annotation.StringDef; |
| 9 | +import androidx.room.Entity; |
| 10 | +import androidx.room.PrimaryKey; |
| 11 | + |
| 12 | +import org.apache.commons.lang3.StringUtils; |
| 13 | + |
| 14 | +import java.lang.annotation.Retention; |
| 15 | +import java.util.Date; |
| 16 | +import java.util.Locale; |
| 17 | + |
| 18 | +import fr.free.nrw.commons.CommonsApplication; |
| 19 | +import fr.free.nrw.commons.Media; |
| 20 | +import fr.free.nrw.commons.filepicker.UploadableFile; |
| 21 | +import fr.free.nrw.commons.settings.Prefs; |
| 22 | +import fr.free.nrw.commons.utils.ConfigUtils; |
| 23 | + |
| 24 | +import static java.lang.annotation.RetentionPolicy.SOURCE; |
| 25 | + |
| 26 | +@Entity(tableName = "contribution") |
| 27 | +public class Contribution extends Media { |
| 28 | + |
| 29 | + //{{According to Exif data|2009-01-09}} |
| 30 | + private static final String TEMPLATE_DATE_ACC_TO_EXIF = "{{According to Exif data|%s}}"; |
| 31 | + |
| 32 | + //2009-01-09 → 9 January 2009 |
| 33 | + private static final String TEMPLATE_DATA_OTHER_SOURCE = "%s"; |
| 34 | + |
| 35 | + // No need to be bitwise - they're mutually exclusive |
| 36 | + public static final int STATE_COMPLETED = -1; |
| 37 | + public static final int STATE_FAILED = 1; |
| 38 | + public static final int STATE_QUEUED = 2; |
| 39 | + public static final int STATE_IN_PROGRESS = 3; |
| 40 | + |
| 41 | + @Retention(SOURCE) |
| 42 | + @StringDef({SOURCE_CAMERA, SOURCE_GALLERY, SOURCE_EXTERNAL}) |
| 43 | + public @interface FileSource {} |
| 44 | + |
| 45 | + public static final String SOURCE_CAMERA = "camera"; |
| 46 | + public static final String SOURCE_GALLERY = "gallery"; |
| 47 | + public static final String SOURCE_EXTERNAL = "external"; |
| 48 | + @PrimaryKey (autoGenerate = true) |
| 49 | + @NonNull |
| 50 | + public long _id; |
| 51 | + public Uri contentUri; |
| 52 | + public String source; |
| 53 | + public String editSummary; |
| 54 | + public int state; |
| 55 | + public long transferred; |
| 56 | + public String decimalCoords; |
| 57 | + public boolean isMultiple; |
| 58 | + public String wikiDataEntityId; |
| 59 | + public String wikiItemName; |
| 60 | + private String p18Value; |
| 61 | + public Uri contentProviderUri; |
| 62 | + public String dateCreatedSource; |
| 63 | + |
| 64 | + public Contribution(Uri localUri, String imageUrl, String filename, String description, long dataLength, |
| 65 | + Date dateCreated, Date dateUploaded, String creator, String editSummary, String decimalCoords) { |
| 66 | + super(localUri, imageUrl, filename, description, dataLength, dateCreated, dateUploaded, creator); |
| 67 | + this.decimalCoords = decimalCoords; |
| 68 | + this.editSummary = editSummary; |
| 69 | + this.dateCreatedSource = ""; |
| 70 | + } |
| 71 | + |
| 72 | + public Contribution(Uri localUri, String imageUrl, String filename, String description, long dataLength, |
| 73 | + Date dateCreated, Date dateUploaded, String creator, String editSummary, String decimalCoords, int state) { |
| 74 | + super(localUri, imageUrl, filename, description, dataLength, dateCreated, dateUploaded, creator); |
| 75 | + this.decimalCoords = decimalCoords; |
| 76 | + this.editSummary = editSummary; |
| 77 | + this.dateCreatedSource = ""; |
| 78 | + this.state=state; |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + public void setDateCreatedSource(String dateCreatedSource) { |
| 84 | + this.dateCreatedSource = dateCreatedSource; |
| 85 | + } |
| 86 | + |
| 87 | + public boolean getMultiple() { |
| 88 | + return isMultiple; |
| 89 | + } |
| 90 | + |
| 91 | + public void setMultiple(boolean multiple) { |
| 92 | + isMultiple = multiple; |
| 93 | + } |
| 94 | + |
| 95 | + public long getTransferred() { |
| 96 | + return transferred; |
| 97 | + } |
| 98 | + |
| 99 | + public void setTransferred(long transferred) { |
| 100 | + this.transferred = transferred; |
| 101 | + } |
| 102 | + |
| 103 | + public String getEditSummary() { |
| 104 | + return editSummary != null ? editSummary : CommonsApplication.DEFAULT_EDIT_SUMMARY; |
| 105 | + } |
| 106 | + |
| 107 | + public Uri getContentUri() { |
| 108 | + return contentUri; |
| 109 | + } |
| 110 | + |
| 111 | + public void setContentUri(Uri contentUri) { |
| 112 | + this.contentUri = contentUri; |
| 113 | + } |
| 114 | + |
| 115 | + public int getState() { |
| 116 | + return state; |
| 117 | + } |
| 118 | + |
| 119 | + public void setState(int state) { |
| 120 | + this.state = state; |
| 121 | + } |
| 122 | + |
| 123 | + public void setDateUploaded(Date date) { |
| 124 | + this.dateUploaded = date; |
| 125 | + } |
| 126 | + |
| 127 | + public String getPageContents(Context applicationContext) { |
| 128 | + StringBuilder buffer = new StringBuilder(); |
| 129 | + buffer |
| 130 | + .append("== {{int:filedesc}} ==\n") |
| 131 | + .append("{{Information\n") |
| 132 | + .append("|description=").append(getDescription()).append("\n") |
| 133 | + .append("|source=").append("{{own}}\n") |
| 134 | + .append("|author=[[User:").append(creator).append("|").append(creator).append("]]\n"); |
| 135 | + |
| 136 | + String templatizedCreatedDate = getTemplatizedCreatedDate(); |
| 137 | + if (!StringUtils.isBlank(templatizedCreatedDate)) { |
| 138 | + buffer.append("|date=").append(templatizedCreatedDate); |
| 139 | + } |
| 140 | + |
| 141 | + buffer.append("}}").append("\n"); |
| 142 | + |
| 143 | + //Only add Location template (e.g. {{Location|37.51136|-77.602615}} ) if coords is not null |
| 144 | + if (decimalCoords != null) { |
| 145 | + buffer.append("{{Location|").append(decimalCoords).append("}}").append("\n"); |
| 146 | + } |
| 147 | + |
| 148 | + buffer.append("== {{int:license-header}} ==\n") |
| 149 | + .append(licenseTemplateFor(getLicense())).append("\n\n") |
| 150 | + .append("{{Uploaded from Mobile|platform=Android|version=") |
| 151 | + .append(ConfigUtils.getVersionNameWithSha(applicationContext)).append("}}\n"); |
| 152 | + if(categories!=null&&categories.size()!=0) { |
| 153 | + for (int i = 0; i < categories.size(); i++) { |
| 154 | + String category = categories.get(i); |
| 155 | + buffer.append("\n[[Category:").append(category).append("]]"); |
| 156 | + } |
| 157 | + } |
| 158 | + else |
| 159 | + buffer.append("{{subst:unc}}"); |
| 160 | + return buffer.toString(); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Returns upload date in either TEMPLATE_DATE_ACC_TO_EXIF or TEMPLATE_DATA_OTHER_SOURCE |
| 165 | + * @return |
| 166 | + */ |
| 167 | + private String getTemplatizedCreatedDate() { |
| 168 | + if (dateCreated != null) { |
| 169 | + java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd"); |
| 170 | + if (UploadableFile.DateTimeWithSource.EXIF_SOURCE.equals(dateCreatedSource)) { |
| 171 | + return String.format(Locale.ENGLISH, TEMPLATE_DATE_ACC_TO_EXIF, dateFormat.format(dateCreated)) + "\n"; |
| 172 | + } else { |
| 173 | + return String.format(Locale.ENGLISH, TEMPLATE_DATA_OTHER_SOURCE, dateFormat.format(dateCreated)) + "\n"; |
| 174 | + } |
| 175 | + } |
| 176 | + return ""; |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public void setFilename(String filename) { |
| 181 | + this.filename = filename; |
| 182 | + } |
| 183 | + |
| 184 | + public void setImageUrl(String imageUrl) { |
| 185 | + this.imageUrl = imageUrl; |
| 186 | + } |
| 187 | + |
| 188 | + public Contribution() { |
| 189 | + |
| 190 | + } |
| 191 | + |
| 192 | + public String getSource() { |
| 193 | + return source; |
| 194 | + } |
| 195 | + |
| 196 | + public void setSource(String source) { |
| 197 | + this.source = source; |
| 198 | + } |
| 199 | + |
| 200 | + @NonNull |
| 201 | + private String licenseTemplateFor(String license) { |
| 202 | + switch (license) { |
| 203 | + case Prefs.Licenses.CC_BY_3: |
| 204 | + return "{{self|cc-by-3.0}}"; |
| 205 | + case Prefs.Licenses.CC_BY_4: |
| 206 | + return "{{self|cc-by-4.0}}"; |
| 207 | + case Prefs.Licenses.CC_BY_SA_3: |
| 208 | + return "{{self|cc-by-sa-3.0}}"; |
| 209 | + case Prefs.Licenses.CC_BY_SA_4: |
| 210 | + return "{{self|cc-by-sa-4.0}}"; |
| 211 | + case Prefs.Licenses.CC0: |
| 212 | + return "{{self|cc-zero}}"; |
| 213 | + } |
| 214 | + |
| 215 | + throw new RuntimeException("Unrecognized license value: " + license); |
| 216 | + } |
| 217 | + |
| 218 | + public String getWikiDataEntityId() { |
| 219 | + return wikiDataEntityId; |
| 220 | + } |
| 221 | + |
| 222 | + public String getWikiItemName() { |
| 223 | + return wikiItemName; |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * When the corresponding wikidata entity is known as in case of nearby uploads, it can be set |
| 228 | + * using the setter method |
| 229 | + * @param wikiDataEntityId wikiDataEntityId |
| 230 | + */ |
| 231 | + public void setWikiDataEntityId(String wikiDataEntityId) { |
| 232 | + this.wikiDataEntityId = wikiDataEntityId; |
| 233 | + } |
| 234 | + |
| 235 | + public void setWikiItemName(String wikiItemName) { |
| 236 | + this.wikiItemName = wikiItemName; |
| 237 | + } |
| 238 | + |
| 239 | + public String getP18Value() { |
| 240 | + return p18Value; |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * When the corresponding image property of wiki entity is known as in case of nearby uploads, |
| 245 | + * it can be set using the setter method |
| 246 | + * @param p18Value p18 value, image property of the wikidata item |
| 247 | + */ |
| 248 | + public void setP18Value(String p18Value) { |
| 249 | + this.p18Value = p18Value; |
| 250 | + } |
| 251 | + |
| 252 | + public void setContentProviderUri(Uri contentProviderUri) { |
| 253 | + this.contentProviderUri = contentProviderUri; |
| 254 | + } |
| 255 | + |
| 256 | + @Override |
| 257 | + public int describeContents() { |
| 258 | + return 0; |
| 259 | + } |
| 260 | + |
| 261 | + @Override |
| 262 | + public void writeToParcel(Parcel dest, int flags) { |
| 263 | + super.writeToParcel(dest, flags); |
| 264 | + dest.writeLong(this._id); |
| 265 | + dest.writeParcelable(this.contentUri, flags); |
| 266 | + dest.writeString(this.source); |
| 267 | + dest.writeString(this.editSummary); |
| 268 | + dest.writeInt(this.state); |
| 269 | + dest.writeLong(this.transferred); |
| 270 | + dest.writeString(this.decimalCoords); |
| 271 | + dest.writeByte(this.isMultiple ? (byte) 1 : (byte) 0); |
| 272 | + dest.writeString(this.wikiDataEntityId); |
| 273 | + dest.writeString(this.wikiItemName); |
| 274 | + dest.writeString(this.p18Value); |
| 275 | + dest.writeParcelable(this.contentProviderUri, flags); |
| 276 | + dest.writeString(this.dateCreatedSource); |
| 277 | + } |
| 278 | + |
| 279 | + protected Contribution(Parcel in) { |
| 280 | + super(in); |
| 281 | + this._id = in.readLong(); |
| 282 | + this.contentUri = in.readParcelable(Uri.class.getClassLoader()); |
| 283 | + this.source = in.readString(); |
| 284 | + this.editSummary = in.readString(); |
| 285 | + this.state = in.readInt(); |
| 286 | + this.transferred = in.readLong(); |
| 287 | + this.decimalCoords = in.readString(); |
| 288 | + this.isMultiple = in.readByte() != 0; |
| 289 | + this.wikiDataEntityId = in.readString(); |
| 290 | + this.wikiItemName = in.readString(); |
| 291 | + this.p18Value = in.readString(); |
| 292 | + this.contentProviderUri = in.readParcelable(Uri.class.getClassLoader()); |
| 293 | + this.dateCreatedSource = in.readString(); |
| 294 | + } |
| 295 | + |
| 296 | + public static final Creator<Contribution> CREATOR = new Creator<Contribution>() { |
| 297 | + @Override |
| 298 | + public Contribution createFromParcel(Parcel source) { |
| 299 | + return new Contribution(source); |
| 300 | + } |
| 301 | + |
| 302 | + @Override |
| 303 | + public Contribution[] newArray(int size) { |
| 304 | + return new Contribution[size]; |
| 305 | + } |
| 306 | + }; |
| 307 | +} |
0 commit comments