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