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.io.IOException;
007import java.io.StringReader;
008import java.time.Instant;
009import java.util.Collection;
010import java.util.Collections;
011import java.util.HashMap;
012import java.util.HashSet;
013import java.util.Map;
014import java.util.Optional;
015import java.util.Set;
016
017import javax.xml.parsers.ParserConfigurationException;
018
019import org.openstreetmap.josm.data.osm.Changeset;
020import org.openstreetmap.josm.data.osm.DataSet;
021import org.openstreetmap.josm.data.osm.OsmPrimitive;
022import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
023import org.openstreetmap.josm.data.osm.PrimitiveId;
024import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
025import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
026import org.openstreetmap.josm.gui.progress.ProgressMonitor;
027import org.openstreetmap.josm.tools.CheckParameterUtil;
028import org.openstreetmap.josm.tools.Utils;
029import org.openstreetmap.josm.tools.XmlParsingException;
030import org.openstreetmap.josm.tools.XmlUtils;
031import org.xml.sax.Attributes;
032import org.xml.sax.InputSource;
033import org.xml.sax.Locator;
034import org.xml.sax.SAXException;
035import org.xml.sax.helpers.DefaultHandler;
036
037/**
038 * Helper class to process the OSM API server response to a "diff" upload.
039 * <p>
040 * New primitives (uploaded with negative id) will be assigned a positive id, etc.
041 * The goal is to have a clean state, just like a fresh download (assuming no
042 * concurrent uploads by other users have happened in the meantime).
043 * @see <a href="https://wiki.openstreetmap.org/wiki/API_v0.6#Response_10">API 0.6 diff upload response</a>
044 */
045public class DiffResultProcessor {
046
047    static class DiffResultEntry {
048        long newId;
049        int newVersion;
050    }
051
052    /**
053     * mapping from old id to new id and version, the result of parsing the diff result
054     * replied by the server
055     */
056    private final Map<PrimitiveId, DiffResultEntry> diffResults = new HashMap<>();
057    /**
058     * the set of processed primitives *after* the new id, the new version and the new changeset id is set
059     */
060    private final Set<OsmPrimitive> processed;
061    /**
062     * the collection of primitives being uploaded
063     */
064    private final Collection<? extends OsmPrimitive> primitives;
065
066    /**
067     * Creates a diff result reader
068     *
069     * @param primitives the collection of primitives which have been uploaded. If null,
070     * assumes an empty collection.
071     */
072    public DiffResultProcessor(Collection<? extends OsmPrimitive> primitives) {
073        this.primitives = Optional.ofNullable(primitives).orElseGet(Collections::emptyList);
074        this.processed = new HashSet<>();
075    }
076
077    /**
078     * Parse the response from a diff upload to the OSM API.
079     *
080     * @param diffUploadResponse the response. Must not be null.
081     * @param progressMonitor a progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null
082     * @throws IllegalArgumentException if diffUploadRequest is null
083     * @throws XmlParsingException if the diffUploadRequest can't be parsed successfully
084     *
085     */
086    public void parse(String diffUploadResponse, ProgressMonitor progressMonitor) throws XmlParsingException {
087        if (progressMonitor == null) {
088            progressMonitor = NullProgressMonitor.INSTANCE;
089        }
090        CheckParameterUtil.ensureParameterNotNull(diffUploadResponse, "diffUploadResponse");
091        try {
092            progressMonitor.beginTask(tr("Parsing response from server..."));
093            InputSource inputSource = new InputSource(new StringReader(diffUploadResponse));
094            XmlUtils.parseSafeSAX(inputSource, new Parser());
095        } catch (XmlParsingException e) {
096            throw e;
097        } catch (IOException | ParserConfigurationException | SAXException e) {
098            throw new XmlParsingException(e);
099        } finally {
100            progressMonitor.finishTask();
101        }
102    }
103
104    /**
105     * Postprocesses the diff result read and parsed from the server.
106     * <p>
107     * Uploaded objects are assigned their new id (if they got assigned a new
108     * id by the server), their new version (if the version was incremented),
109     * and the id of the changeset to which they were uploaded.
110     *
111     * @param cs the current changeset. Ignored if null.
112     * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
113     * @return the collection of processed primitives
114     */
115    protected Set<OsmPrimitive> postProcess(Changeset cs, ProgressMonitor monitor) {
116        if (monitor == null) {
117            monitor = NullProgressMonitor.INSTANCE;
118        }
119        DataSet ds = null;
120        if (!primitives.isEmpty()) {
121            ds = primitives.iterator().next().getDataSet();
122        }
123        boolean readOnly = false;
124        if (ds != null) {
125            readOnly = ds.isLocked();
126            if (readOnly) {
127                ds.unlock();
128            }
129            ds.beginUpdate();
130        }
131        try {
132            monitor.beginTask(tr("Postprocessing uploaded data..."));
133            monitor.setTicksCount(primitives.size());
134            monitor.setTicks(0);
135            for (OsmPrimitive p : primitives) {
136                monitor.worked(1);
137                DiffResultEntry entry = diffResults.get(p.getPrimitiveId());
138                if (entry == null) {
139                    continue;
140                }
141                processed.add(p);
142                if (!p.isDeleted()) {
143                    boolean isNew = p.isNew();
144                    p.setOsmId(entry.newId, entry.newVersion);
145                    if (isNew) {
146                        p.setReferrersDownloaded(true);
147                    }
148                    p.setVisible(true);
149                } else {
150                    p.setVisible(false);
151                }
152                if (cs != null && !cs.isNew()) {
153                    p.setChangesetId(cs.getId());
154                    p.setUser(cs.getUser());
155                    // TODO is there a way to obtain the timestamp for non-closed changesets?
156                    Instant instant = Utils.firstNonNull(cs.getClosedAt(), Instant.now());
157                    p.setInstant(instant);
158                }
159            }
160            return processed;
161        } finally {
162            if (ds != null) {
163                ds.endUpdate();
164                if (readOnly) {
165                    ds.lock();
166                }
167            }
168            monitor.finishTask();
169        }
170    }
171
172    private final class Parser extends DefaultHandler {
173        private Locator locator;
174
175        @Override
176        public void setDocumentLocator(Locator locator) {
177            this.locator = locator;
178        }
179
180        void throwException(String msg) throws XmlParsingException {
181            throw new XmlParsingException(msg).rememberLocation(locator);
182        }
183
184        @Override
185        public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
186            try {
187                switch (qName) {
188                case "diffResult":
189                    // the root element, ignore
190                    break;
191                case "node":
192                case "way":
193                case "relation":
194                    PrimitiveId id = new SimplePrimitiveId(
195                            Long.parseLong(atts.getValue("old_id")),
196                            OsmPrimitiveType.fromApiTypeName(qName)
197                    );
198                    DiffResultEntry entry = new DiffResultEntry();
199                    if (atts.getValue("new_id") != null) {
200                        entry.newId = Long.parseLong(atts.getValue("new_id"));
201                    }
202                    if (atts.getValue("new_version") != null) {
203                        entry.newVersion = Integer.parseInt(atts.getValue("new_version"));
204                    }
205                    diffResults.put(id, entry);
206                    break;
207                default:
208                    throwException(tr("Unexpected XML element with name ''{0}''", qName));
209                }
210            } catch (NumberFormatException e) {
211                throw new XmlParsingException(e).rememberLocation(locator);
212            }
213        }
214    }
215
216    final Map<PrimitiveId, DiffResultEntry> getDiffResults() {
217        return new HashMap<>(diffResults);
218    }
219}