001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.sources;
003
004import java.util.Map;
005
006import javax.json.stream.JsonCollectors;
007
008import org.openstreetmap.josm.data.StructUtils.StructEntry;
009import org.openstreetmap.josm.data.imagery.DefaultLayer;
010import org.openstreetmap.josm.data.imagery.Shape;
011import org.openstreetmap.josm.tools.Utils;
012
013/**
014 * A generic SourcePreferenceEntry that is used for storing data in JOSM preferences.
015 * This is intended to be removed, at some point. User beware.
016 *
017 * @author Taylor Smock
018 *
019 * @param <T> The type of SourceInfo
020 */
021public class SourcePreferenceEntry<T extends SourceInfo<?, ?, ?, ?>> {
022    /** The name of the source */
023    @StructEntry public String name;
024    /** A *unique* id for the source */
025    @StructEntry public String id;
026    /** The type of the source (e.g., WMS, WMTS, etc.) */
027    @StructEntry public String type;
028    /** The URL for the source (base url) */
029    @StructEntry public String url;
030    /** The EULA for the source */
031    @StructEntry public String eula;
032    /** The attribution text for the source */
033    @StructEntry public String attribution_text;
034    /** The attribution URL for the source */
035    @StructEntry public String attribution_url;
036    /** The permission reference url (i.e., how do we know we have permission?) */
037    @StructEntry public String permission_reference_url;
038    /** The logo to be used for the source */
039    @StructEntry public String logo_image;
040    /** The logo url */
041    @StructEntry public String logo_url;
042    /** The TOU text */
043    @StructEntry public String terms_of_use_text;
044    /** The URL for the TOU */
045    @StructEntry public String terms_of_use_url;
046    /** The country code for the source (usually ISO 3166-1 alpha-2) */
047    @StructEntry public String country_code = "";
048    /** The date for the source */
049    @StructEntry public String date;
050    /** The cookies required to get the source */
051    @StructEntry public String cookies;
052    /** The bounds of the source */
053    @StructEntry public String bounds;
054    /** The shape of the source (mostly used for visual aid purposes) */
055    @StructEntry public String shapes;
056    /** The icon for the source (not necessarily the same as the logo) */
057    @StructEntry public String icon;
058    /** The description of the source */
059    @StructEntry public String description;
060    /** The default layers for the source, if any (mostly useful for imagery) */
061    @StructEntry public String default_layers;
062    /** Any custom HTTP headers */
063    @StructEntry public Map<String, String> customHttpHeaders;
064    /** The category string for the source */
065    @StructEntry public String category;
066
067    /**
068     * Constructs a new empty {@code SourcePreferenceEntry}.
069     */
070    public SourcePreferenceEntry() {
071        // Do nothing
072    }
073
074    /**
075     * Constructs a new {@code SourcePreferenceEntry} from a given {@code SourceInfo}.
076     * @param i The corresponding source info
077     */
078    public SourcePreferenceEntry(T i) {
079        name = i.getName();
080        id = i.getId();
081        type = i.sourceType.getTypeString();
082        url = i.getUrl();
083        eula = i.eulaAcceptanceRequired;
084        attribution_text = i.attributionText;
085        attribution_url = i.attributionLinkURL;
086        permission_reference_url = i.permissionReferenceURL;
087        date = i.date;
088        logo_image = i.attributionImage;
089        logo_url = i.attributionImageURL;
090        terms_of_use_text = i.termsOfUseText;
091        terms_of_use_url = i.termsOfUseURL;
092        country_code = i.countryCode;
093        cookies = i.getCookies();
094        icon = Utils.intern(i.icon);
095        description = i.description;
096        category = i.category != null ? i.category.getCategoryString() : null;
097        if (i.bounds != null) {
098            bounds = i.bounds.encodeAsString(",");
099            StringBuilder shapesString = new StringBuilder();
100            for (Shape s : i.bounds.getShapes()) {
101                if (shapesString.length() > 0) {
102                    shapesString.append(';');
103                }
104                shapesString.append(s.encodeAsString(","));
105            }
106            if (shapesString.length() > 0) {
107                shapes = shapesString.toString();
108            }
109        }
110
111        if (!i.defaultLayers.isEmpty()) {
112            default_layers = i.defaultLayers.stream().map(DefaultLayer::toJson).collect(JsonCollectors.toJsonArray()).toString();
113        }
114        customHttpHeaders = i.customHttpHeaders;
115    }
116
117    @Override
118    public String toString() {
119        StringBuilder s = new StringBuilder(getClass().getSimpleName()).append(" [name=").append(name);
120        if (id != null) {
121            s.append(" id=").append(id);
122        }
123        s.append(']');
124        return s.toString();
125    }
126}