1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.Locale;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
9 | import org.openstreetmap.josm.tools.Logging;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * The chunk mode to use when uploading
|
---|
13 | * @since 12687 (moved from {@code gui.io} package)
|
---|
14 | */
|
---|
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 |
|
---|
29 | private final String preferenceValue;
|
---|
30 |
|
---|
31 | UploadStrategy(String preferenceValue) {
|
---|
32 | this.preferenceValue = preferenceValue;
|
---|
33 | }
|
---|
34 |
|
---|
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 | */
|
---|
40 | public static UploadStrategy fromPreference(String preferenceValue) {
|
---|
41 | if (preferenceValue == null) return null;
|
---|
42 | preferenceValue = preferenceValue.trim().toLowerCase(Locale.ENGLISH);
|
---|
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
|
---|
53 | *
|
---|
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 | */
|
---|
64 | public static final UploadStrategy DEFAULT_UPLOAD_STRATEGY = SINGLE_REQUEST_STRATEGY;
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Replies the upload strategy currently configured in the preferences.
|
---|
68 | *
|
---|
69 | * Checks for the preference key <pre>osm-server.upload-strategy</pre>.
|
---|
70 | *
|
---|
71 | * If missing or if the preference value is illegal, {@link #DEFAULT_UPLOAD_STRATEGY}
|
---|
72 | * is replied.
|
---|
73 | *
|
---|
74 | * @return the upload strategy currently configured in the preferences.
|
---|
75 | */
|
---|
76 | public static UploadStrategy getFromPreferences() {
|
---|
77 | String v = Config.getPref().get("osm-server.upload-strategy", null);
|
---|
78 | if (v == null) {
|
---|
79 | return DEFAULT_UPLOAD_STRATEGY;
|
---|
80 | }
|
---|
81 | UploadStrategy strategy = fromPreference(v);
|
---|
82 | if (strategy == null) {
|
---|
83 | Logging.warn(tr("Unexpected value for key ''{0}'' in preferences, got ''{1}''", "osm-server.upload-strategy", v));
|
---|
84 | return DEFAULT_UPLOAD_STRATEGY;
|
---|
85 | }
|
---|
86 | return strategy;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Saves the upload strategy <code>strategy</code> to the preferences.
|
---|
91 | *
|
---|
92 | * @param strategy the strategy to save
|
---|
93 | */
|
---|
94 | public static void saveToPreferences(UploadStrategy strategy) {
|
---|
95 | Config.getPref().put("osm-server.upload-strategy", strategy.getPreferenceValue());
|
---|
96 | }
|
---|
97 | }
|
---|