001    // License: GPL. For details, see LICENSE file.
002    package org.openstreetmap.josm.gui.io;
003    
004    import static org.openstreetmap.josm.tools.I18n.tr;
005    
006    import org.openstreetmap.josm.Main;
007    
008    public enum UploadStrategy {
009        /**
010         * Uploads the objects individually, one request per object
011         */
012        INDIVIDUAL_OBJECTS_STRATEGY("individualobjects"),
013        /**
014         * Upload the objects in junks of n objects using m diff uploads
015         */
016        CHUNKED_DATASET_STRATEGY("chunked"),
017        /**
018         * Upload the objects in one request using 1 diff upload
019         */
020        SINGLE_REQUEST_STRATEGY("singlerequest");
021    
022        private String preferenceValue;
023    
024        UploadStrategy(String preferenceValue) {
025            this.preferenceValue = preferenceValue;
026        }
027    
028        public static UploadStrategy fromPreference(String preferenceValue) {
029            if (preferenceValue == null) return null;
030            preferenceValue = preferenceValue.trim().toLowerCase();
031            for (UploadStrategy strategy: values()) {
032                if (strategy.getPreferenceValue().equals(preferenceValue))
033                    return strategy;
034            }
035            return null;
036        }
037    
038        /**
039         * Replies the value which is written to the preferences for a specific
040         * upload strategy
041         *
042         * @return the value which is written to the preferences for a specific
043         * upload strategy
044         */
045        public String getPreferenceValue() {
046            return preferenceValue;
047        }
048    
049        /**
050         * the default upload strategy
051         */
052        public final static UploadStrategy DEFAULT_UPLOAD_STRATEGY = SINGLE_REQUEST_STRATEGY;
053    
054        /**
055         * Replies the upload strategy currently configured in the preferences.
056         *
057         * First checks for the preference key <pre>osm-server.upload-strategy</pre>. If not
058         * present, checks for the legacy preference key <pre>osm-server.atomic-upload</pre>.
059         *
060         * If both are missing or if the preference value is illegal, {@link #DEFAULT_UPLOAD_STRATEGY}
061         * is replied.
062         *
063         * @return the upload strategy currently configured in the preferences.
064         */
065        public static UploadStrategy getFromPreferences() {
066            String v = Main.pref.get("osm-server.upload-strategy", null);
067            if (v == null) {
068                // legacy support. Until 12/2009 we had osm-server.atomic-upload only.
069                // If we still find "osm-server.atomic-upload" we use it and remove it.
070                // When the preferences are saved the next time, "osm-server.upload-strategy"
071                // will be inserted.
072                v = Main.pref.get("osm-server.atomic-upload", null);
073                if (v != null) {
074                    Main.pref.removeFromCollection("osm-server.atomic-upload", v);
075                } else {
076                    v = "";
077                }
078                v = v.trim().toLowerCase();
079                if (v.equals("true"))
080                    return SINGLE_REQUEST_STRATEGY;
081                else if (v.equals("false"))
082                    return INDIVIDUAL_OBJECTS_STRATEGY;
083                else
084                    return DEFAULT_UPLOAD_STRATEGY;
085            }
086            UploadStrategy strategy = fromPreference(v);
087            if (strategy == null) {
088                System.err.println(tr("Warning: unexpected value for key ''{0}'' in preferences, got ''{1}''", "osm-server.upload-strategy", v ));
089                return DEFAULT_UPLOAD_STRATEGY;
090            }
091            return strategy;
092        }
093    
094        /**
095         * Saves the upload strategy <code>strategy</code> to the preferences.
096         *
097         * @param strategy the strategy to save
098         */
099        public static void saveToPreferences(UploadStrategy strategy) {
100            Main.pref.put("osm-server.upload-strategy", strategy.getPreferenceValue());
101        }
102    }