[2711] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[12687] | 2 | package org.openstreetmap.josm.io;
|
---|
[2711] | 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[8404] | 6 | import java.util.Locale;
|
---|
| 7 |
|
---|
[12846] | 8 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
[12620] | 9 | import org.openstreetmap.josm.tools.Logging;
|
---|
[2711] | 10 |
|
---|
[12370] | 11 | /**
|
---|
| 12 | * The chunk mode to use when uploading
|
---|
[12687] | 13 | * @since 12687 (moved from {@code gui.io} package)
|
---|
[12370] | 14 | */
|
---|
[2711] | 15 | public enum UploadStrategy {
|
---|
| 16 | /**
|
---|
| 17 | * Uploads the objects individually, one request per object
|
---|
| 18 | */
|
---|
| 19 | INDIVIDUAL_OBJECTS_STRATEGY("individualobjects"),
|
---|
| 20 | /**
|
---|
| 21 | * Upload the objects in junks of n objects using m diff uploads
|
---|
| 22 | */
|
---|
| 23 | CHUNKED_DATASET_STRATEGY("chunked"),
|
---|
| 24 | /**
|
---|
| 25 | * Upload the objects in one request using 1 diff upload
|
---|
| 26 | */
|
---|
| 27 | SINGLE_REQUEST_STRATEGY("singlerequest");
|
---|
| 28 |
|
---|
[11489] | 29 | private final String preferenceValue;
|
---|
[2711] | 30 |
|
---|
| 31 | UploadStrategy(String preferenceValue) {
|
---|
| 32 | this.preferenceValue = preferenceValue;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
[12370] | 35 | /**
|
---|
| 36 | * Reads the value from preferences
|
---|
| 37 | * @param preferenceValue The preference value
|
---|
| 38 | * @return The {@link UploadStrategy} for that preference or <code>null</code> if unknown
|
---|
| 39 | */
|
---|
[2711] | 40 | public static UploadStrategy fromPreference(String preferenceValue) {
|
---|
| 41 | if (preferenceValue == null) return null;
|
---|
[8404] | 42 | preferenceValue = preferenceValue.trim().toLowerCase(Locale.ENGLISH);
|
---|
[2711] | 43 | for (UploadStrategy strategy: values()) {
|
---|
| 44 | if (strategy.getPreferenceValue().equals(preferenceValue))
|
---|
| 45 | return strategy;
|
---|
| 46 | }
|
---|
| 47 | return null;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * Replies the value which is written to the preferences for a specific
|
---|
| 52 | * upload strategy
|
---|
[2801] | 53 | *
|
---|
[2711] | 54 | * @return the value which is written to the preferences for a specific
|
---|
| 55 | * upload strategy
|
---|
| 56 | */
|
---|
| 57 | public String getPreferenceValue() {
|
---|
| 58 | return preferenceValue;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | * the default upload strategy
|
---|
| 63 | */
|
---|
[6889] | 64 | public static final UploadStrategy DEFAULT_UPLOAD_STRATEGY = SINGLE_REQUEST_STRATEGY;
|
---|
[2711] | 65 |
|
---|
| 66 | /**
|
---|
| 67 | * Replies the upload strategy currently configured in the preferences.
|
---|
[2801] | 68 | *
|
---|
[13064] | 69 | * Checks for the preference key <pre>osm-server.upload-strategy</pre>.
|
---|
[2801] | 70 | *
|
---|
[13064] | 71 | * If missing or if the preference value is illegal, {@link #DEFAULT_UPLOAD_STRATEGY}
|
---|
[2711] | 72 | * is replied.
|
---|
[2801] | 73 | *
|
---|
[2711] | 74 | * @return the upload strategy currently configured in the preferences.
|
---|
| 75 | */
|
---|
| 76 | public static UploadStrategy getFromPreferences() {
|
---|
[12846] | 77 | String v = Config.getPref().get("osm-server.upload-strategy", null);
|
---|
[2711] | 78 | if (v == null) {
|
---|
[13064] | 79 | return DEFAULT_UPLOAD_STRATEGY;
|
---|
[2711] | 80 | }
|
---|
| 81 | UploadStrategy strategy = fromPreference(v);
|
---|
| 82 | if (strategy == null) {
|
---|
[12620] | 83 | Logging.warn(tr("Unexpected value for key ''{0}'' in preferences, got ''{1}''", "osm-server.upload-strategy", v));
|
---|
[2711] | 84 | return DEFAULT_UPLOAD_STRATEGY;
|
---|
| 85 | }
|
---|
| 86 | return strategy;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /**
|
---|
| 90 | * Saves the upload strategy <code>strategy</code> to the preferences.
|
---|
[2801] | 91 | *
|
---|
[2711] | 92 | * @param strategy the strategy to save
|
---|
| 93 | */
|
---|
| 94 | public static void saveToPreferences(UploadStrategy strategy) {
|
---|
[12846] | 95 | Config.getPref().put("osm-server.upload-strategy", strategy.getPreferenceValue());
|
---|
[2711] | 96 | }
|
---|
| 97 | }
|
---|