1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.Main;
|
---|
7 |
|
---|
8 | public enum UploadStrategy {
|
---|
9 | /**
|
---|
10 | * Uploads the objects individually, one request per object
|
---|
11 | */
|
---|
12 | INDIVIDUAL_OBJECTS_STRATEGY("individualobjects"),
|
---|
13 | /**
|
---|
14 | * Upload the objects in junks of n objects using m diff uploads
|
---|
15 | */
|
---|
16 | CHUNKED_DATASET_STRATEGY("chunked"),
|
---|
17 | /**
|
---|
18 | * Upload the objects in one request using 1 diff upload
|
---|
19 | */
|
---|
20 | SINGLE_REQUEST_STRATEGY("singlerequest");
|
---|
21 |
|
---|
22 | private String preferenceValue;
|
---|
23 |
|
---|
24 | UploadStrategy(String preferenceValue) {
|
---|
25 | this.preferenceValue = preferenceValue;
|
---|
26 | }
|
---|
27 |
|
---|
28 | public static UploadStrategy fromPreference(String preferenceValue) {
|
---|
29 | if (preferenceValue == null) return null;
|
---|
30 | preferenceValue = preferenceValue.trim().toLowerCase();
|
---|
31 | for (UploadStrategy strategy: values()) {
|
---|
32 | if (strategy.getPreferenceValue().equals(preferenceValue))
|
---|
33 | return strategy;
|
---|
34 | }
|
---|
35 | return null;
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Replies the value which is written to the preferences for a specific
|
---|
40 | * upload strategy
|
---|
41 | *
|
---|
42 | * @return the value which is written to the preferences for a specific
|
---|
43 | * upload strategy
|
---|
44 | */
|
---|
45 | public String getPreferenceValue() {
|
---|
46 | return preferenceValue;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * the default upload strategy
|
---|
51 | */
|
---|
52 | public final static UploadStrategy DEFAULT_UPLOAD_STRATEGY = SINGLE_REQUEST_STRATEGY;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Replies the upload strategy currently configured in the preferences.
|
---|
56 | *
|
---|
57 | * First checks for the preference key <pre>osm-server.upload-strategy</pre>. If not
|
---|
58 | * present, checks for the legacy preference key <pre>osm-server.atomic-upload</pre>.
|
---|
59 | *
|
---|
60 | * If both are missing or if the preference value is illegal, {@see #DEFAULT_UPLOAD_STRATEGY}
|
---|
61 | * is replied.
|
---|
62 | *
|
---|
63 | * @return the upload strategy currently configured in the preferences.
|
---|
64 | */
|
---|
65 | public static UploadStrategy getFromPreferences() {
|
---|
66 | String v = Main.pref.get("osm-server.upload-strategy");
|
---|
67 | if (v == null) {
|
---|
68 | // legacy support. Until 12/2009 we had osm-server.atomic-upload only.
|
---|
69 | // If we still find "osm-server.atomic-upload" we use it and remove it.
|
---|
70 | // When the preferences are saved the next time, "osm-server.upload-strategy"
|
---|
71 | // will be inserted.
|
---|
72 | v = Main.pref.get("osm-server.atomic-upload");
|
---|
73 | if (v != null) {
|
---|
74 | Main.pref.removeFromCollection("osm-server.atomic-upload", v);
|
---|
75 | }
|
---|
76 | v = v.trim().toLowerCase();
|
---|
77 | if (v.equals("true"))
|
---|
78 | return SINGLE_REQUEST_STRATEGY;
|
---|
79 | else if (v.equals("false"))
|
---|
80 | return INDIVIDUAL_OBJECTS_STRATEGY;
|
---|
81 | else
|
---|
82 | return DEFAULT_UPLOAD_STRATEGY;
|
---|
83 | }
|
---|
84 | UploadStrategy strategy = fromPreference(v);
|
---|
85 | if (strategy == null) {
|
---|
86 | System.err.println(tr("Warning: unexpected value for key ''{0}'' in preferences, got ''{1}''", "osm-server.upload-strategy", v ));
|
---|
87 | return DEFAULT_UPLOAD_STRATEGY;
|
---|
88 | }
|
---|
89 | return strategy;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Saves the upload strategy <code>strategy</code> to the preferences.
|
---|
94 | *
|
---|
95 | * @param strategy the strategy to save
|
---|
96 | */
|
---|
97 | public static void saveToPreferences(UploadStrategy strategy) {
|
---|
98 | Main.pref.put("osm-server.upload-strategy", strategy.getPreferenceValue());
|
---|
99 | }
|
---|
100 | }
|
---|