Changeset 1894 in josm for trunk


Ignore:
Timestamp:
2009-08-03T15:00:34+02:00 (15 years ago)
Author:
Gubaer
Message:

fixed #3158: modify status is not removed for partial uploads

Location:
trunk/src/org/openstreetmap/josm
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/SaveActionBase.java

    r1890 r1894  
    216216                return;
    217217            }
    218             layer.cleanData(null, false);
     218            layer.cleanupAfterSaveToDisk();
    219219        } catch (IOException e) {
    220220            e.printStackTrace();
  • trunk/src/org/openstreetmap/josm/actions/UploadAction.java

    r1885 r1894  
    235235        );
    236236        switch(ret) {
    237         case JOptionPane.CLOSED_OPTION: return;
    238         case JOptionPane.CANCEL_OPTION: return;
    239         case 0: synchronizePrimitive(id); break;
    240         case 1: synchronizeDataSet(); break;
    241         default:
    242             // should not happen
    243             throw new IllegalStateException(tr("unexpected return value. Got {0}", ret));
     237            case JOptionPane.CLOSED_OPTION: return;
     238            case JOptionPane.CANCEL_OPTION: return;
     239            case 0: synchronizePrimitive(id); break;
     240            case 1: synchronizeDataSet(); break;
     241            default:
     242                // should not happen
     243                throw new IllegalStateException(tr("unexpected return value. Got {0}", ret));
    244244        }
    245245    }
     
    274274        );
    275275        switch(ret) {
    276         case JOptionPane.CLOSED_OPTION: return;
    277         case 1: return;
    278         case 0: synchronizeDataSet(); break;
    279         default:
    280             // should not happen
    281             throw new IllegalStateException(tr("unexpected return value. Got {0}", ret));
     276            case JOptionPane.CLOSED_OPTION: return;
     277            case 1: return;
     278            case 0: synchronizeDataSet(); break;
     279            default:
     280                // should not happen
     281                throw new IllegalStateException(tr("unexpected return value. Got {0}", ret));
    282282        }
    283283    }
     
    537537            try {
    538538                writer.uploadOsm(getCurrentDataSet().version, toUpload, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
    539                 getEditLayer().cleanData(writer.processed, !toUpload.isEmpty());
    540539            } catch (Exception sxe) {
    541540                if (uploadCancelled) {
     
    550549            if (uploadCancelled)
    551550                return;
     551
     552            // we always clean the data, even in case of errors. It's possible the data was
     553            // partially uploaded
     554            //
     555            getEditLayer().cleanupAfterUpload(writer.getProcessedPrimitives());
    552556            if (lastException != null) {
    553557                handleFailedUpload(lastException);
  • trunk/src/org/openstreetmap/josm/command/Command.java

    r1856 r1894  
    8989            e.getKey().cloneFrom(e.getValue());
    9090        }
     91        getLayer().setModified(true);
    9192    }
    9293
  • trunk/src/org/openstreetmap/josm/data/UndoRedoHandler.java

    r1877 r1894  
    9999    }
    100100
    101     public void layerRemoved(Layer oldLayer) {
     101    public void clean(Layer layer) {
     102        if (layer == null)
     103            return;
    102104        boolean changed = false;
    103105        for (Iterator<Command> it = commands.iterator(); it.hasNext();) {
    104             if (it.next().invalidBecauselayerRemoved(oldLayer)) {
     106            if (it.next().invalidBecauselayerRemoved(layer)) {
    105107                it.remove();
    106108                changed = true;
     
    108110        }
    109111        for (Iterator<Command> it = redoCommands.iterator(); it.hasNext();) {
    110             if (it.next().invalidBecauselayerRemoved(oldLayer)) {
     112            if (it.next().invalidBecauselayerRemoved(layer)) {
    111113                it.remove();
    112114                changed = true;
     
    118120    }
    119121
     122    public void layerRemoved(Layer oldLayer) {
     123        clean(oldLayer);
     124    }
     125
    120126    public void layerAdded(Layer newLayer) {}
    121127    public void activeLayerChange(Layer oldLayer, Layer newLayer) {}
  • trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java

    r1890 r1894  
    6565import org.openstreetmap.josm.tools.ImageProvider;
    6666
     67import sun.security.action.GetLongAction;
     68
    6769/**
    6870 * A layer holding data from a specific dataset.
     
    317319
    318320    /**
     321     * Cleanup the layer after save to disk. Just marks the layer as unmodified.
     322     * Leaves the undo/redo stack unchanged.
     323     *
     324     */
     325    public void cleanupAfterSaveToDisk() {
     326        setModified(false);
     327    }
     328
     329    /**
    319330     * Clean out the data behind the layer. This means clearing the redo/undo lists,
    320      * really deleting all deleted objects and reset the modified flags. This is done
    321      * after a successfull upload.
     331     * really deleting all deleted objects and reset the modified flags. This should
     332     * be done after an upload, even after a partial upload.
    322333     *
    323334     * @param processed A list of all objects that were actually uploaded.
    324      *         May be <code>null</code>, which means nothing has been uploaded but
    325      *         saved to disk instead. Note that an empty collection for "processed"
    326      *      means that an upload has been attempted but failed.
    327      */
    328     public void cleanData(final Collection<OsmPrimitive> processed, boolean dataAdded) {
     335     *         May be <code>null</code>, which means nothing has been uploaded
     336     */
     337    public void cleanupAfterUpload(final Collection<OsmPrimitive> processed) {
    329338
    330339        // return immediately if an upload attempt failed
    331         if (processed != null && processed.isEmpty() && !dataAdded)
     340        if (processed == null || processed.isEmpty())
    332341            return;
    333342
    334         Main.main.undoRedo.clean();
     343        Main.main.undoRedo.clean(this);
    335344
    336345        // if uploaded, clean the modified flags as well
    337         if (processed != null) {
    338             final Set<OsmPrimitive> processedSet = new HashSet<OsmPrimitive>(processed);
    339             for (final Iterator<Node> it = data.nodes.iterator(); it.hasNext();) {
    340                 cleanIterator(it, processedSet);
    341             }
    342             for (final Iterator<Way> it = data.ways.iterator(); it.hasNext();) {
    343                 cleanIterator(it, processedSet);
    344             }
    345             for (final Iterator<Relation> it = data.relations.iterator(); it.hasNext();) {
    346                 cleanIterator(it, processedSet);
    347             }
    348         }
    349 
    350         // update the modified flag
    351         boolean b = (getAssociatedFile() != null && processed != null);
    352         if (b && !dataAdded)
    353             return; // do nothing when uploading non-harmful changes.
    354 
    355         // modified if server changed the data (esp. the id).
    356         uploadedModified = b && dataAdded;
    357         setModified(uploadedModified);
     346        final Set<OsmPrimitive> processedSet = new HashSet<OsmPrimitive>(processed);
     347        for (final Iterator<Node> it = data.nodes.iterator(); it.hasNext();) {
     348            cleanIterator(it, processedSet);
     349        }
     350        for (final Iterator<Way> it = data.ways.iterator(); it.hasNext();) {
     351            cleanIterator(it, processedSet);
     352        }
     353        for (final Iterator<Relation> it = data.relations.iterator(); it.hasNext();) {
     354            cleanIterator(it, processedSet);
     355        }
     356
     357        setModified(true);
    358358    }
    359359
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r1879 r1894  
    507507    }
    508508
     509    /**
     510     * Replies the current changeset
     511     *
     512     * @return the current changeset
     513     */
     514    public Changeset getCurrentChangeset() {
     515        return changeset;
     516    }
    509517}
  • trunk/src/org/openstreetmap/josm/io/OsmServerWriter.java

    r1879 r1894  
    55
    66import java.util.Collection;
     7import java.util.Collections;
    78import java.util.LinkedList;
    89import java.util.List;
     
    1112import org.openstreetmap.josm.Main;
    1213import org.openstreetmap.josm.actions.UploadAction;
     14import org.openstreetmap.josm.data.osm.Changeset;
    1315import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1416import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
     
    3436     * than where passed in the list to upload*.
    3537     */
    36     public Collection<OsmPrimitive> processed;
     38    private Collection<OsmPrimitive> processed;
    3739
    3840    private OsmApi api = OsmApi.getOsmApi();
     
    7981
    8082    /**
     83     * Uploads the changes individually. Invokes one API call per uploaded primitmive.
     84     *
     85     * @param primitives the collection of primitives to upload
     86     * @param progressMonitor the progress monitor
     87     * @throws OsmTransferException thrown if an exception occurs
     88     */
     89    protected void uploadChangesIndividually(Collection<OsmPrimitive> primitives, ProgressMonitor progressMonitor) throws OsmTransferException {
     90        try {
     91            progressMonitor.setTicksCount(primitives.size());
     92            api.createChangeset(getChangesetComment(), progressMonitor.createSubTaskMonitor(0, false));
     93            uploadStartTime = System.currentTimeMillis();
     94            for (OsmPrimitive osm : primitives) {
     95                int progress = progressMonitor.getTicks();
     96                String time_left_str = timeLeft(progress, primitives.size());
     97                progressMonitor.subTask(
     98                        tr("{0}% ({1}/{2}), {3} left. Uploading {4}: {5} (id: {6})",
     99                                Math.round(100.0*progress/primitives.size()), progress,
     100                                primitives.size(), time_left_str,
     101                                OsmPrimitiveType.from(osm).getLocalizedDisplayNameSingular(),
     102                                NAME_FORMATTER.getName(osm),
     103                                osm.id));
     104                makeApiRequest(osm,progressMonitor);
     105                processed.add(osm);
     106                progressMonitor.worked(1);
     107            }
     108        } catch(OsmTransferException e) {
     109            throw e;
     110        } catch(Exception e) {
     111            throw new OsmTransferException(e);
     112        } finally {
     113            try {
     114                api.stopChangeset(progressMonitor.createSubTaskMonitor(0, false));
     115            } catch(Exception e) {
     116                Changeset changeset = api.getCurrentChangeset();
     117                String changesetId = (changeset == null ? tr("unknown") : Long.toString(changeset.id));
     118                logger.warning(tr("Failed to close changeset {0}, will be closed by server after timeout. Exception was: {1}",
     119                        changesetId, e.toString()));
     120            }
     121        }
     122    }
     123
     124    /**
     125     * Upload all changes in one diff upload
     126     *
     127     * @param primitives the collection of primitives to upload
     128     * @param progressMonitor  the progress monitor
     129     * @throws OsmTransferException thrown if an exception occurs
     130     */
     131    protected void uploadChangesAsDiffUpload(Collection<OsmPrimitive> primitives, ProgressMonitor progressMonitor) throws OsmTransferException {
     132        // upload everything in one changeset
     133        //
     134        try {
     135            api.createChangeset(getChangesetComment(), progressMonitor.createSubTaskMonitor(0, false));
     136            processed.addAll(api.uploadDiff(primitives, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false)));
     137        } catch(OsmTransferException e) {
     138            throw e;
     139        } catch(Exception e) {
     140            throw new OsmTransferException(e);
     141        } finally {
     142            try {
     143                api.stopChangeset(progressMonitor.createSubTaskMonitor(0, false));
     144            } catch (Exception ee) {
     145                Changeset changeset = api.getCurrentChangeset();
     146                String changesetId = (changeset == null ? tr("unknown") : Long.toString(changeset.id));
     147                logger.warning(tr("Failed to close changeset {0}, will be closed by server after timeout. Exception was: {1}",
     148                        changesetId, ee.toString()));
     149            }
     150        }
     151    }
     152
     153    /**
    81154     * Send the dataset to the server.
    82155     *
     
    92165
    93166        try {
    94 
    95167            // check whether we can use changeset
    96168            //
     
    103175
    104176            if (useChangeset) {
    105                 // upload everything in one changeset
    106                 //
    107                 try {
    108                     api.createChangeset(getChangesetComment(), progressMonitor.createSubTaskMonitor(0, false));
    109                     processed.addAll(api.uploadDiff(primitives, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false)));
    110                 } catch(OsmTransferException e) {
    111                     throw e;
    112                 } finally {
    113                     try {
    114                         if (canUseChangeset) {
    115                             api.stopChangeset(progressMonitor.createSubTaskMonitor(0, false));
    116                         }
    117                     } catch (Exception ee) {
    118                         ee.printStackTrace();
    119                         // ignore nested exception
    120                     }
    121                 }
     177                uploadChangesAsDiffUpload(primitives, progressMonitor);
    122178            } else {
    123                 // upload changes individually (90% of code is for the status display...)
    124                 //
    125                 progressMonitor.setTicksCount(primitives.size());
    126                 api.createChangeset(getChangesetComment(), progressMonitor.createSubTaskMonitor(0, false));
    127                 uploadStartTime = System.currentTimeMillis();
    128                 for (OsmPrimitive osm : primitives) {
    129                     int progress = progressMonitor.getTicks();
    130                     String time_left_str = timeLeft(progress, primitives.size());
    131                     progressMonitor.subTask(
    132                             tr("{0}% ({1}/{2}), {3} left. Uploading {4}: {5} (id: {6})",
    133                                     Math.round(100.0*progress/primitives.size()), progress,
    134                                     primitives.size(), time_left_str,
    135                                     OsmPrimitiveType.from(osm).getLocalizedDisplayNameSingular(),
    136                                     NAME_FORMATTER.getName(osm),
    137                                     osm.id));
    138                     makeApiRequest(osm,progressMonitor);
    139                     processed.add(osm);
    140                     progressMonitor.worked(1);
    141                 }
    142                 api.stopChangeset(progressMonitor.createSubTaskMonitor(0, false));
    143             }
    144 
     179                uploadChangesIndividually(primitives, progressMonitor);
     180            }
    145181        } finally {
    146182            progressMonitor.finishTask();
     
    163199        }
    164200    }
     201
     202    /**
     203     * Replies the collection of successfully processed primitives
     204     *
     205     * @return the collection of successfully processed primitives
     206     */
     207    public Collection<OsmPrimitive> getProcessedPrimitives() {
     208        return processed;
     209    }
    165210}
Note: See TracChangeset for help on using the changeset viewer.