Ignore:
Timestamp:
2018-02-24T18:58:28+01:00 (7 years ago)
Author:
Don-vip
Message:

fix #8039, fix #10456: final fixes for the read-only/locked layers:

  • rename "read-only" to "locked" (in XML and Java classes/interfaces)
  • add a new download policy (true/never) to allow private layers forbidding only to download data, but allowing everything else

This leads to:

  • normal layers: download allowed, modifications allowed, upload allowed
  • private layers: download allowed or not (download=true/never), modifications allowed, upload allowed or not (upload=true/discouraged/never)
  • locked layers: nothing allowed, the data cannot be modified in any way
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/DataSet.java

    r13434 r13453  
    104104 * @author imi
    105105 */
    106 public final class DataSet extends QuadBucketPrimitiveStore implements Data, ProjectionChangeListener, ReadOnly {
     106public final class DataSet extends QuadBucketPrimitiveStore implements Data, ProjectionChangeListener, Lockable {
     107
     108    /**
     109     * Download policy.
     110     *
     111     * Determines if download from the OSM server is intended, discouraged, or disabled / blocked.
     112     * @see UploadPolicy
     113     * @since 13453
     114     */
     115    public enum DownloadPolicy {
     116        /**
     117         * Normal dataset, download intended.
     118         */
     119        NORMAL("true"),
     120        /**
     121         * Download blocked.
     122         * Download options completely disabled. Intended for private layers, see #8039.
     123         */
     124        BLOCKED("never");
     125
     126        final String xmlFlag;
     127
     128        DownloadPolicy(String xmlFlag) {
     129            this.xmlFlag = xmlFlag;
     130        }
     131
     132        /**
     133         * Get the corresponding value of the <code>upload='...'</code> XML-attribute
     134         * in the .osm file.
     135         * @return value of the <code>download</code> attribute
     136         */
     137        public String getXmlFlag() {
     138            return xmlFlag;
     139        }
     140
     141        /**
     142         * Returns the {@code DownloadPolicy} for the given <code>upload='...'</code> XML-attribute
     143         * @param xmlFlag <code>download='...'</code> XML-attribute to convert
     144         * @return {@code DownloadPolicy} value
     145         * @throws IllegalArgumentException for invalid values
     146         */
     147        public static DownloadPolicy of(String xmlFlag) {
     148            for (DownloadPolicy policy : values()) {
     149                if (policy.getXmlFlag().equalsIgnoreCase(xmlFlag)) {
     150                    return policy;
     151                }
     152            }
     153            throw new IllegalArgumentException(xmlFlag);
     154        }
     155    }
    107156
    108157    /**
    109158     * Upload policy.
    110159     *
    111      * Determines if upload to the OSM server is intended, discouraged, or
    112      * disabled / blocked.
     160     * Determines if upload to the OSM server is intended, discouraged, or disabled / blocked.
     161     * @see DownloadPolicy
    113162     */
    114163    public enum UploadPolicy {
     
    173222
    174223    private final Storage<OsmPrimitive> allPrimitives = new Storage<>(new Storage.PrimitiveIdHash(), true);
    175     private final Map<PrimitiveId, OsmPrimitive> primitivesMap = allPrimitives.foreignKey(new Storage.PrimitiveIdHash());
     224    private final Map<PrimitiveId, OsmPrimitive> primitivesMap = allPrimitives
     225            .foreignKey(new Storage.PrimitiveIdHash());
    176226    private final CopyOnWriteArrayList<DataSetListener> listeners = new CopyOnWriteArrayList<>();
    177227
     
    187237
    188238    private String name;
     239    private DownloadPolicy downloadPolicy;
    189240    private UploadPolicy uploadPolicy;
    190241    /** Flag used to know if the dataset should not be editable */
     
    250301                primMap.put(w, newWay);
    251302                List<Node> newNodes = new ArrayList<>();
    252                 for (Node n: w.getNodes()) {
     303                for (Node n : w.getNodes()) {
    253304                    newNodes.add((Node) primMap.get(n));
    254305                }
     
    268319                Relation newRelation = (Relation) primMap.get(r);
    269320                List<RelationMember> newMembers = new ArrayList<>();
    270                 for (RelationMember rm: r.getMembers()) {
     321                for (RelationMember rm : r.getMembers()) {
    271322                    newMembers.add(new RelationMember(rm.getRole(), primMap.get(rm.getMember())));
    272323                }
     
    378429        checkModifiable();
    379430        this.version = version;
     431    }
     432
     433    /**
     434     * Get the download policy.
     435     * @return the download policy
     436     * @see #setDownloadPolicy(DownloadPolicy)
     437     * @since 13453
     438     */
     439    public DownloadPolicy getDownloadPolicy() {
     440        return this.downloadPolicy;
     441    }
     442
     443    /**
     444     * Sets the download policy.
     445     * @param downloadPolicy the download policy
     446     * @see #getUploadPolicy()
     447     * @since 13453
     448     */
     449    public void setDownloadPolicy(DownloadPolicy downloadPolicy) {
     450        this.downloadPolicy = downloadPolicy;
    380451    }
    381452
     
    529600     */
    530601    public Collection<OsmPrimitive> allNonDeletedPhysicalPrimitives() {
    531         return getPrimitives(primitive -> !primitive.isDeleted() && !primitive.isIncomplete() && !(primitive instanceof Relation));
     602        return getPrimitives(
     603                primitive -> !primitive.isDeleted() && !primitive.isIncomplete() && !(primitive instanceof Relation));
    532604    }
    533605
     
    565637            if (getPrimitiveById(primitive) != null)
    566638                throw new DataIntegrityProblemException(
    567                         tr("Unable to add primitive {0} to the dataset because it is already included", primitive.toString()));
     639                        tr("Unable to add primitive {0} to the dataset because it is already included",
     640                                primitive.toString()));
    568641
    569642            allPrimitives.add(primitive);
     
    690763     */
    691764    public Collection<OsmPrimitive> getSelectedNodesAndWays() {
    692         return new SubclassFilteredCollection<>(getSelected(), primitive -> primitive instanceof Node || primitive instanceof Way);
     765        return new SubclassFilteredCollection<>(getSelected(),
     766                primitive -> primitive instanceof Node || primitive instanceof Way);
    693767    }
    694768
     
    10061080        OsmPrimitive result = getPrimitiveById(primitiveId);
    10071081        if (result == null && primitiveId != null) {
    1008             Logging.warn(tr("JOSM expected to find primitive [{0} {1}] in dataset but it is not there. Please report this "
    1009                     + "at {2}. This is not a critical error, it should be safe to continue in your work.",
     1082            Logging.warn(tr(
     1083                    "JOSM expected to find primitive [{0} {1}] in dataset but it is not there. Please report this "
     1084                            + "at {2}. This is not a critical error, it should be safe to continue in your work.",
    10101085                    primitiveId.getType(), Long.toString(primitiveId.getUniqueId()), Main.getJOSMWebsite()));
    10111086            Logging.error(new Exception());
     
    11151190     */
    11161191    public boolean isModified() {
    1117         for (OsmPrimitive p: allPrimitives) {
     1192        for (OsmPrimitive p : allPrimitives) {
    11181193            if (p.isModified())
    11191194                return true;
     
    11281203     */
    11291204    public boolean requiresUploadToServer() {
    1130         for (OsmPrimitive p: allPrimitives) {
     1205        for (OsmPrimitive p : allPrimitives) {
    11311206            if (APIOperation.of(p) != null)
    11321207                return true;
     
    11991274                try {
    12001275                    if (eventsToFire.size() < MAX_SINGLE_EVENTS) {
    1201                         for (AbstractDatasetChangedEvent event: eventsToFire) {
     1276                        for (AbstractDatasetChangedEvent event : eventsToFire) {
    12021277                            fireEventToListeners(event);
    12031278                        }
     
    12191294
    12201295    private void fireEventToListeners(AbstractDatasetChangedEvent event) {
    1221         for (DataSetListener listener: listeners) {
     1296        for (DataSetListener listener : listeners) {
    12221297            event.fire(listener);
    12231298        }
     
    12601335
    12611336    void fireChangesetIdChanged(OsmPrimitive primitive, int oldChangesetId, int newChangesetId) {
    1262         fireEvent(new ChangesetIdChangedEvent(this, Collections.singletonList(primitive), oldChangesetId, newChangesetId));
     1337        fireEvent(new ChangesetIdChangedEvent(this, Collections.singletonList(primitive), oldChangesetId,
     1338                newChangesetId));
    12631339    }
    12641340
     
    12831359     */
    12841360    public void invalidateEastNorthCache() {
    1285         if (Main.getProjection() == null) return; // sanity check
     1361        if (Main.getProjection() == null)
     1362            return; // sanity check
    12861363        beginUpdate();
    12871364        try {
    1288             for (Node n: getNodes()) {
     1365            for (Node n : getNodes()) {
    12891366                n.invalidateEastNorthCache();
    12901367            }
     
    13261403        try {
    13271404            clearSelection();
    1328             for (OsmPrimitive primitive:allPrimitives) {
     1405            for (OsmPrimitive primitive : allPrimitives) {
    13291406                primitive.setDataset(null);
    13301407            }
     
    13431420    public void deleteInvisible() {
    13441421        checkModifiable();
    1345         for (OsmPrimitive primitive:allPrimitives) {
     1422        for (OsmPrimitive primitive : allPrimitives) {
    13461423            if (!primitive.isVisible()) {
    13471424                primitive.setDeleted(true);
     
    14541531
    14551532    @Override
    1456     public void setReadOnly() {
     1533    public void lock() {
    14571534        if (!isReadOnly.compareAndSet(false, true)) {
    14581535            Logging.warn("Trying to set readOnly flag on a readOnly dataset ", getName());
     
    14611538
    14621539    @Override
    1463     public void unsetReadOnly() {
     1540    public void unlock() {
    14641541        if (!isReadOnly.compareAndSet(true, false)) {
    14651542            Logging.warn("Trying to unset readOnly flag on a non-readOnly dataset ", getName());
     
    14681545
    14691546    @Override
    1470     public boolean isReadOnly() {
     1547    public boolean isLocked() {
    14711548        return isReadOnly.get();
    14721549    }
     
    14771554     */
    14781555    private void checkModifiable() {
    1479         if (isReadOnly()) {
     1556        if (isLocked()) {
    14801557            throw new IllegalStateException("DataSet is read-only");
    14811558        }
Note: See TracChangeset for help on using the changeset viewer.