001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.util.Locale;
007
008import org.openstreetmap.josm.spi.preferences.Config;
009import org.openstreetmap.josm.tools.Logging;
010
011/**
012 * The chunk mode to use when uploading
013 * @since 12687 (moved from {@code gui.io} package)
014 */
015public enum UploadStrategy {
016    /**
017     * Uploads the objects individually, one request per object
018     */
019    INDIVIDUAL_OBJECTS_STRATEGY("individualobjects"),
020    /**
021     * Upload the objects in junks of n objects using m diff uploads
022     */
023    CHUNKED_DATASET_STRATEGY("chunked"),
024    /**
025     * Upload the objects in one request using 1 diff upload
026     */
027    SINGLE_REQUEST_STRATEGY("singlerequest");
028
029    private final String preferenceValue;
030
031    UploadStrategy(String preferenceValue) {
032        this.preferenceValue = preferenceValue;
033    }
034
035    /**
036     * Reads the value from preferences
037     * @param preferenceValue The preference value
038     * @return The {@link UploadStrategy} for that preference or <code>null</code> if unknown
039     */
040    public static UploadStrategy fromPreference(String preferenceValue) {
041        if (preferenceValue == null) return null;
042        preferenceValue = preferenceValue.trim().toLowerCase(Locale.ENGLISH);
043        for (UploadStrategy strategy: values()) {
044            if (strategy.getPreferenceValue().equals(preferenceValue))
045                return strategy;
046        }
047        return null;
048    }
049
050    /**
051     * Replies the value which is written to the preferences for a specific
052     * upload strategy
053     *
054     * @return the value which is written to the preferences for a specific
055     * upload strategy
056     */
057    public String getPreferenceValue() {
058        return preferenceValue;
059    }
060
061    /**
062     * the default upload strategy
063     */
064    public static final UploadStrategy DEFAULT_UPLOAD_STRATEGY = SINGLE_REQUEST_STRATEGY;
065
066    /**
067     * Replies the upload strategy currently configured in the preferences.
068     *
069     * Checks for the preference key <pre>osm-server.upload-strategy</pre>.
070     *
071     * If missing or if the preference value is illegal, {@link #DEFAULT_UPLOAD_STRATEGY}
072     * is replied.
073     *
074     * @return the upload strategy currently configured in the preferences.
075     */
076    public static UploadStrategy getFromPreferences() {
077        String v = Config.getPref().get("osm-server.upload-strategy", null);
078        if (v == null) {
079            return DEFAULT_UPLOAD_STRATEGY;
080        }
081        UploadStrategy strategy = fromPreference(v);
082        if (strategy == null) {
083            Logging.warn(tr("Unexpected value for key ''{0}'' in preferences, got ''{1}''", "osm-server.upload-strategy", v));
084            return DEFAULT_UPLOAD_STRATEGY;
085        }
086        return strategy;
087    }
088
089    /**
090     * Saves the upload strategy <code>strategy</code> to the preferences.
091     *
092     * @param strategy the strategy to save
093     */
094    public static void saveToPreferences(UploadStrategy strategy) {
095        Config.getPref().put("osm-server.upload-strategy", strategy != null ? strategy.getPreferenceValue() : null);
096    }
097}