Ignore:
Timestamp:
2009-09-13T19:30:36+02:00 (16 years ago)
Author:
Gubaer
Message:

new: reading open changesets from the server
new: reading user info from the server
new: any open changeset can be used when uploading
new: generic dialog for closing changesets
fixed #3427: JOSM can't keep many changesets open at once
fixed #3408: Allow continuing opened changeset even after restarting JOSM
fixed #3476: Default selection in upload dialog should be different for unclosed changesets. (Upload dialog now looks different)

Location:
trunk/src/org/openstreetmap/josm/io
Files:
4 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r2074 r2115  
    2323import java.util.Collections;
    2424import java.util.HashMap;
    25 import java.util.Properties;
    2625
    2726import javax.xml.parsers.SAXParserFactory;
     
    3231import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    3332import org.openstreetmap.josm.data.osm.visitor.CreateOsmChangeVisitor;
     33import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    3434import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    3535import org.xml.sax.Attributes;
     
    205205
    206206    /**
     207     * Makes an XML string from an OSM primitive. Uses the OsmWriter class.
     208     * @param o the OSM primitive
     209     * @param addBody true to generate the full XML, false to only generate the encapsulating tag
     210     * @return XML string
     211     */
     212    private String toXml(Changeset s) {
     213        swriter.getBuffer().setLength(0);
     214        osmWriter.header();
     215        s.visit(osmWriter);
     216        osmWriter.footer();
     217        osmWriter.out.flush();
     218        return swriter.toString();
     219    }
     220
     221    /**
    207222     * Returns the base URL for API requests, including the negotiated version number.
    208223     * @return base URL string
     
    229244     */
    230245    public void createPrimitive(OsmPrimitive osm, ProgressMonitor monitor) throws OsmTransferException {
    231         initialize(monitor);
    232246        String ret = "";
    233247        try {
     248            ensureValidChangeset();
     249            initialize(monitor);
    234250            ret = sendRequest("PUT", OsmPrimitiveType.from(osm).getAPIName()+"/create", toXml(osm, true),monitor);
    235251            osm.setOsmId(Long.parseLong(ret.trim()), 1);
     
    244260     * version.
    245261     *
    246      * @param osm the primitive
     262     * @param osm the primitive. Must not be null
    247263     * @throws OsmTransferException if something goes wrong
    248264     */
    249265    public void modifyPrimitive(OsmPrimitive osm, ProgressMonitor monitor) throws OsmTransferException {
    250         initialize(monitor);
    251         if (version.equals("0.5")) {
    252             // legacy mode does not return the new object version.
    253             sendRequest("PUT", OsmPrimitiveType.from(osm).getAPIName()+"/" + osm.getId(), toXml(osm, true),monitor);
    254         } else {
    255             String ret = null;
    256             // normal mode (0.6 and up) returns new object version.
    257             try {
     266        String ret = null;
     267        try {
     268            ensureValidChangeset();
     269            initialize(monitor);
     270            if (version.equals("0.5")) {
     271                // legacy mode does not return the new object version.
     272                sendRequest("PUT", OsmPrimitiveType.from(osm).getAPIName()+"/" + osm.getId(), toXml(osm, true),monitor);
     273            } else {
     274                // normal mode (0.6 and up) returns new object version.
    258275                ret = sendRequest("PUT", OsmPrimitiveType.from(osm).getAPIName()+"/" + osm.getId(), toXml(osm, true), monitor);
    259276                osm.setOsmId(osm.getId(), Integer.parseInt(ret.trim()));
    260             } catch(NumberFormatException e) {
    261                 throw new OsmTransferException(tr("unexpected format of new version of modified primitive ''{0}'', got ''{1}''", osm.getId(), ret));
    262277            }
     278        } catch(NumberFormatException e) {
     279            throw new OsmTransferException(tr("unexpected format of new version of modified primitive ''{0}'', got ''{1}''", osm.getId(), ret));
    263280        }
    264281    }
     
    270287     */
    271288    public void deletePrimitive(OsmPrimitive osm, ProgressMonitor monitor) throws OsmTransferException {
     289        ensureValidChangeset();
    272290        initialize(monitor);
    273291        // can't use a the individual DELETE method in the 0.6 API. Java doesn't allow
     
    280298
    281299    /**
    282      * Creates a new changeset based on the keys in <code>changeset</code>
    283      *
    284      * @param changeset the changeset to be used for uploading
     300     * Creates a new changeset based on the keys in <code>changeset</code>. If this
     301     * method succeeds, changeset.getId() replies the id the server assigned to the new
     302     * changeset
     303     *
     304     * The changeset must not be null, but its key/value-pairs may be empty.
     305     *
     306     * @param changeset the changeset toe be created. Must not be null.
    285307     * @param progressMonitor the progress monitor
    286308     * @throws OsmTransferException signifying a non-200 return code, or connection errors
    287      */
    288     public void createChangeset(Changeset changeset, ProgressMonitor progressMonitor) throws OsmTransferException {
     309     * @throws IllegalArgumentException thrown if changeset is null
     310     */
     311    public void openChangeset(Changeset changeset, ProgressMonitor progressMonitor) throws OsmTransferException {
     312        if (changeset == null)
     313            throw new IllegalArgumentException(tr("parameter ''{0}'' must not be null", "changeset"));
    289314        try {
    290315            progressMonitor.beginTask((tr("Creating changeset...")));
    291             createPrimitive(changeset, progressMonitor);
    292             this.changeset = changeset;
    293             progressMonitor.setCustomText((tr("Successfully opened changeset {0}",this.changeset.getId())));
     316            initialize(progressMonitor);
     317            String ret = "";
     318            try {
     319                ret = sendRequest("PUT", "changeset/create", toXml(changeset),progressMonitor);
     320                changeset.setId(Long.parseLong(ret.trim()));
     321                changeset.setOpen(true);
     322            } catch(NumberFormatException e){
     323                throw new OsmTransferException(tr("unexpected format of id replied by the server, got ''{0}''", ret));
     324            }
     325            progressMonitor.setCustomText((tr("Successfully opened changeset {0}",changeset.getId())));
    294326        } finally {
    295327            progressMonitor.finishTask();
     
    298330
    299331    /**
    300      * Updates the current changeset with the keys in  <code>changesetUpdate</code>.
    301      *
    302      * @param changesetUpdate the changeset to update
    303      * @param progressMonitor the progress monitor
     332     * Updates a changeset with the keys in  <code>changesetUpdate</code>. The changeset must not
     333     * be null and id > 0 must be true.
     334     *
     335     * @param changeset the changeset to update. Must not be null.
     336     * @param monitor the progress monitor. If null, uses the {@see NullProgressMonitor#INSTANCE}.
    304337     *
    305338     * @throws OsmTransferException if something goes wrong.
    306      */
    307     public void updateChangeset(Changeset changesetUpdate, ProgressMonitor progressMonitor) throws OsmTransferException {
     339     * @throws IllegalArgumentException if changeset is null
     340     * @throws IllegalArgumentException if changeset.getId() == 0
     341     *
     342     */
     343    public void updateChangeset(Changeset changeset, ProgressMonitor monitor) throws OsmTransferException {
     344        if (changeset == null)
     345            throw new IllegalArgumentException(tr("parameter ''{0}'' must not be null", "changeset"));
     346        if (monitor == null) {
     347            monitor = NullProgressMonitor.INSTANCE;
     348        }
     349        if (changeset.getId() <= 0)
     350            throw new IllegalArgumentException(tr("id of changeset > 0 required. Got {0}", changeset.getId()));
    308351        try {
    309             progressMonitor.beginTask(tr("Updating changeset..."));
    310             initialize(progressMonitor);
    311             if (this.changeset != null && this.changeset.getId() > 0) {
    312                 if (this.changeset.hasEqualSemanticAttributes(changesetUpdate)) {
    313                     progressMonitor.setCustomText(tr("Changeset {0} is unchanged. Skipping update.", changesetUpdate.getId()));
    314                     return;
    315                 }
    316                 this.changeset.setKeys(changesetUpdate.getKeys());
    317                 progressMonitor.setCustomText(tr("Updating changeset {0}...", this.changeset.getId()));
    318                 sendRequest(
    319                         "PUT",
    320                         OsmPrimitiveType.from(changesetUpdate).getAPIName() + "/" + this.changeset.getId(),
    321                         toXml(this.changeset, true),
    322                         progressMonitor
    323                 );
    324             } else
    325                 throw new OsmTransferException(tr("Failed to update changeset. Either there is no current changeset or the id of the current changeset is 0"));
     352            monitor.beginTask(tr("Updating changeset..."));
     353            initialize(monitor);
     354            monitor.setCustomText(tr("Updating changeset {0}...", changeset.getId()));
     355            sendRequest(
     356                    "PUT",
     357                    "changeset/" + this.changeset.getId(),
     358                    toXml(this.changeset),
     359                    monitor
     360            );
    326361        } finally {
    327             progressMonitor.finishTask();
    328         }
    329     }
    330 
    331     /**
    332      * Closes a changeset on the server.
    333      *
    334      * @param changesetProcessingType how changesets are currently handled
    335      * @param progressMonitor the progress monitor
     362            monitor.finishTask();
     363        }
     364    }
     365
     366
     367    /**
     368     * Closes a changeset on the server. Sets changeset.setOpen(false) if this operation
     369     * succeeds.
     370     *
     371     * @param changeset the changeset to be closed. Must not be null. changeset.getId() > 0 required.
     372     * @param monitor the progress monitor. If null, uses {@see NullProgressMonitor#INSTANCE}
    336373     *
    337374     * @throws OsmTransferException if something goes wrong.
    338      */
    339     public void stopChangeset(ChangesetProcessingType changesetProcessingType, ProgressMonitor progressMonitor) throws OsmTransferException {
    340         if (changesetProcessingType == null) {
    341             changesetProcessingType = ChangesetProcessingType.USE_NEW_AND_CLOSE;
    342         }
     375     * @throws IllegalArgumentException thrown if changeset is null
     376     * @throws IllegalArgumentException thrown if changeset.getId() <= 0
     377     */
     378    public void closeChangeset(Changeset changeset, ProgressMonitor monitor) throws OsmTransferException {
     379        if (changeset == null)
     380            throw new IllegalArgumentException(tr("parameter ''{0}'' must not be null", "changeset"));
     381        if (monitor == null) {
     382            monitor = NullProgressMonitor.INSTANCE;
     383        }
     384        if (changeset.getId() <= 0)
     385            throw new IllegalArgumentException(tr("id of changeset > 0 required. Got {0}", changeset.getId()));
    343386        try {
    344             progressMonitor.beginTask(tr("Closing changeset..."));
    345             initialize(progressMonitor);
    346             if (changesetProcessingType.isCloseAfterUpload()) {
    347                 progressMonitor.setCustomText(tr("Closing changeset {0}...", changeset.getId()));
    348                 if (this.changeset != null && this.changeset.getId() > 0) {
    349                     sendRequest("PUT", "changeset" + "/" + changeset.getId() + "/close", null, progressMonitor);
    350                     changeset = null;
    351                 }
    352             } else {
    353                 progressMonitor.setCustomText(tr("Leaving changeset {0} open...", changeset.getId()));
    354             }
     387            monitor.beginTask(tr("Closing changeset..."));
     388            initialize(monitor);
     389            sendRequest("PUT", "changeset" + "/" + changeset.getId() + "/close", null, monitor);
     390            changeset.setOpen(false);
    355391        } finally {
    356             progressMonitor.finishTask();
     392            monitor.finishTask();
    357393        }
    358394    }
     
    365401     * @throws OsmTransferException if something is wrong
    366402     */
    367     public Collection<OsmPrimitive> uploadDiff(final Collection<OsmPrimitive> list, ProgressMonitor progressMonitor) throws OsmTransferException {
    368 
    369         progressMonitor.beginTask("", list.size() * 2);
     403    public Collection<OsmPrimitive> uploadDiff(Collection<OsmPrimitive> list, ProgressMonitor progressMonitor) throws OsmTransferException {
    370404        try {
     405            progressMonitor.beginTask("", list.size() * 2);
    371406            if (changeset == null)
    372407                throw new OsmTransferException(tr("No changeset present for diff upload"));
     
    385420
    386421            String diff = duv.getDocument();
    387             try {
    388                 String diffresult = sendRequest("POST", "changeset/" + changeset.getId() + "/upload", diff,progressMonitor);
    389                 DiffResultReader.parseDiffResult(diffresult, list, processed, duv.getNewIdMap(),
    390                         progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
    391             } catch(OsmTransferException e) {
    392                 throw e;
    393             } catch(Exception e) {
    394                 throw new OsmTransferException(e);
    395             }
    396 
     422            String diffresult = sendRequest("POST", "changeset/" + changeset.getId() + "/upload", diff,progressMonitor);
     423            DiffResultReader.parseDiffResult(diffresult, list, processed, duv.getNewIdMap(),
     424                    progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
    397425            return processed;
     426        } catch(OsmTransferException e) {
     427            throw e;
     428        } catch(Exception e) {
     429            throw new OsmTransferException(e);
    398430        } finally {
    399431            progressMonitor.finishTask();
     
    536568                }
    537569                throw new OsmTransferException(e);
     570            } catch(OsmTransferException e) {
     571                throw e;
    538572            } catch (Exception e) {
    539                 if (e instanceof OsmTransferException) throw (OsmTransferException) e;
    540573                throw new OsmTransferException(e);
    541574            }
     
    552585    }
    553586
    554     /**
    555      * Replies the current changeset
    556      *
    557      * @return the current changeset
    558      */
    559     public Changeset getCurrentChangeset() {
     587
     588    /**
     589     * Ensures that the current changeset can be used for uploading data
     590     *
     591     * @throws OsmTransferException thrown if the current changeset can't be used for
     592     * uploading data
     593     */
     594    protected void ensureValidChangeset() throws OsmTransferException {
     595        if (changeset == null)
     596            throw new OsmTransferException(tr("current changeset is null. Can't upload data."));
     597        if (changeset.getId() <= 0)
     598            throw new OsmTransferException(tr("id of current changeset > required. Current id is {0}", changeset.getId()));
     599    }
     600    /**
     601     * Replies the changeset data uploads are currently directed to
     602     *
     603     * @return the changeset data uploads are currently directed to
     604     */
     605    public Changeset getChangeset() {
    560606        return changeset;
    561607    }
     608
     609    /**
     610     * Sets the changesets to which further data uploads are directed. The changeset
     611     * can be null. If it isn't null it must have been created, i.e. id > 0 is required. Furthermore,
     612     * it must be open.
     613     *
     614     * @param changeset the changeset
     615     * @throws IllegalArgumentException thrown if changeset.getId() <= 0
     616     * @throws IllegalArgumentException thrown if !changeset.isOpen()
     617     */
     618    public void setChangeset(Changeset changeset) {
     619        if (changeset == null) {
     620            this.changeset = null;
     621            return;
     622        }
     623        if (changeset.getId() <= 0)
     624            throw new IllegalArgumentException(tr("Changeset id > 0 expected. Got {0}", changeset.getId()));
     625        if (!changeset.isOpen())
     626            throw new IllegalArgumentException(tr("Open changeset expected. Got closed changeset with id {0}", changeset.getId()));
     627        this.changeset = changeset;
     628    }
     629
    562630}
  • trunk/src/org/openstreetmap/josm/io/OsmDataParsingException.java

    r2094 r2115  
    77import org.xml.sax.SAXException;
    88
    9 /**
    10  * Represents a parsing error in an OSM data file.
    11  *
    12  * Use {@see #getColumnNumber()} and {@see #getLineNumber()} to locate
    13  * the position in the file where the parsing error occured.
    14  *
    15  */
    169public class OsmDataParsingException extends SAXException {
    1710    private int columnNumber;
  • trunk/src/org/openstreetmap/josm/io/OsmReader.java

    r2094 r2115  
    3636
    3737/**
    38  * Parser for the Osm Api. Read from an input stream and constructs a dataset out of it.
     38 * Parser for the Osm Api. Read from an input stream and construct a dataset out of it.
    3939 *
    4040 */
     
    8080    }
    8181
     82    private static class OsmPrimitiveData {
     83        public long id = 0;
     84        public boolean modified = false;
     85        public boolean deleted = false;
     86        public Date timestamp = new Date();
     87        public User user = null;
     88        public boolean visible = true;
     89        public int version = 0;
     90        public LatLon latlon = new LatLon(0,0);
     91        private OsmPrimitive primitive;
     92
     93        public void copyTo(OsmPrimitive osm) {
     94            osm.setModified(modified);
     95            osm.setDeleted(deleted);
     96            //  id < 0 possible if read from a file
     97            if (id <= 0) {
     98                osm.clearOsmId();
     99            } else {
     100                osm.setOsmId(id, version);
     101            }
     102            osm.setTimestamp(timestamp);
     103            osm.user = user;
     104            osm.setVisible(visible);
     105            osm.mappaintStyle = null;
     106        }
     107
     108        public Node createNode() {
     109            Node node = new Node();
     110            node.setCoor(latlon);
     111            copyTo(node);
     112            primitive = node;
     113            return node;
     114        }
     115
     116        public Way createWay() {
     117            Way way = new Way();
     118            copyTo(way);
     119            primitive = way;
     120            return way;
     121        }
     122        public Relation createRelation() {
     123            Relation relation= new Relation();
     124            copyTo(relation);
     125            primitive = relation;
     126            return relation;
     127        }
     128
     129        public void rememberTag(String key, String value) {
     130            primitive.put(key, value);
     131        }
     132    }
    82133
    83134    /**
     
    100151     */
    101152    private Map<Long, Collection<RelationMemberData>> relations = new HashMap<Long, Collection<RelationMemberData>>();
    102 
    103153
    104154    private class Parser extends DefaultHandler {
     
    241291                current.rememberTag(key, value);
    242292            } else {
    243                 System.out.println(tr("Warning: Undefined element ''{0}'' found in input stream. Skipping.", qName));
     293                throwException(tr("Undefined element ''{0}'' found in input stream. Aborting.", qName));
    244294            }
    245295        }
     
    503553        }
    504554    }
    505 
    506     /**
    507      * Temporarily holds data for a parsed {@see OsmPrimitive} and provides
    508      * methods for creating an {@see OsmPrimitive} based on this data.
    509      */
    510     private static class OsmPrimitiveData {
    511         public long id = 0;
    512         public boolean modified = false;
    513         public boolean deleted = false;
    514         public Date timestamp = new Date();
    515         public User user = null;
    516         public boolean visible = true;
    517         public int version = 0;
    518         public LatLon latlon = new LatLon(0,0);
    519         private OsmPrimitive primitive;
    520 
    521         public void copyTo(OsmPrimitive osm) {
    522             //  id < 0 possible if read from a file
    523             if (id <= 0) {
    524                 osm.clearOsmId();
    525             } else {
    526                 osm.setOsmId(id, version);
    527             }
    528             osm.setDeleted(deleted);
    529             osm.setModified(modified);
    530             osm.setTimestamp(timestamp);
    531             osm.user = user;
    532             osm.setVisible(visible);
    533             osm.mappaintStyle = null;
    534         }
    535 
    536         public Node createNode() {
    537             Node node = new Node();
    538             node.setCoor(latlon);
    539             copyTo(node);
    540             primitive = node;
    541             return node;
    542         }
    543 
    544         public Way createWay() {
    545             Way way = new Way();
    546             copyTo(way);
    547             primitive = way;
    548             return way;
    549         }
    550         public Relation createRelation() {
    551             Relation relation= new Relation();
    552             copyTo(relation);
    553             primitive = relation;
    554             return relation;
    555         }
    556 
    557         public void rememberTag(String key, String value) {
    558             primitive.put(key, value);
    559         }
    560     }
    561555}
  • trunk/src/org/openstreetmap/josm/io/OsmServerWriter.java

    r2081 r2115  
    6565     *
    6666     * @param primitives the collection of primitives to upload
    67      * @param changeset the changeset to be used if <code>changesetProcessingType</code> indicates that
    68      *   a new changeset should be opened
    69      * @param changesetProcessingType how we handle changesets
    7067     * @param progressMonitor the progress monitor
    7168     * @throws OsmTransferException thrown if an exception occurs
    7269     */
    73     protected void uploadChangesIndividually(Collection<OsmPrimitive> primitives, Changeset changeset, ChangesetProcessingType changesetProcessingType, ProgressMonitor progressMonitor) throws OsmTransferException {
     70    protected void uploadChangesIndividually(Collection<OsmPrimitive> primitives, ProgressMonitor progressMonitor) throws OsmTransferException {
    7471        try {
    7572            progressMonitor.beginTask(tr("Starting to upload with one request per primitive ..."));
    7673            progressMonitor.setTicksCount(primitives.size());
    77             if (changesetProcessingType.isUseNew()) {
    78                 api.createChangeset(changeset,progressMonitor.createSubTaskMonitor(0, false));
    79             } else {
    80                 api.updateChangeset(changeset,progressMonitor.createSubTaskMonitor(0, false));
    81             }
    8274            uploadStartTime = System.currentTimeMillis();
    8375            for (OsmPrimitive osm : primitives) {
     
    8678                String msg = "";
    8779                switch(OsmPrimitiveType.from(osm)) {
    88                 case NODE: msg = marktr("{0}% ({1}/{2}), {3} left. Uploading node ''{4}'' (id: {5})"); break;
    89                 case WAY: msg = marktr("{0}% ({1}/{2}), {3} left. Uploading way ''{4}'' (id: {5})"); break;
    90                 case RELATION: msg = marktr("{0}% ({1}/{2}), {3} left. Uploading relation ''{4}'' (id: {5})"); break;
     80                    case NODE: msg = marktr("{0}% ({1}/{2}), {3} left. Uploading node ''{4}'' (id: {5})"); break;
     81                    case WAY: msg = marktr("{0}% ({1}/{2}), {3} left. Uploading way ''{4}'' (id: {5})"); break;
     82                    case RELATION: msg = marktr("{0}% ({1}/{2}), {3} left. Uploading relation ''{4}'' (id: {5})"); break;
    9183                }
    9284                progressMonitor.subTask(
     
    10799            throw new OsmTransferException(e);
    108100        } finally {
    109             try {
    110                 // starting the changeset may have failed, for instance because the user
    111                 // cancelled the upload task. Only close the changeset if we currently have
    112                 // an open changeset
    113 
    114                 if (api.getCurrentChangeset() != null && api.getCurrentChangeset().getId() > 0) {
    115                     api.stopChangeset(changesetProcessingType, progressMonitor.createSubTaskMonitor(0, false));
    116                 }
    117             } catch(Exception e) {
    118                 OsmChangesetCloseException closeException = new OsmChangesetCloseException(e);
    119                 closeException.setChangeset(api.getCurrentChangeset());
    120                 throw closeException;
    121             } finally {
    122                 progressMonitor.finishTask();
    123             }
     101            progressMonitor.finishTask();
    124102        }
    125103    }
     
    132110     * @throws OsmTransferException thrown if an exception occurs
    133111     */
    134     protected void uploadChangesAsDiffUpload(Collection<OsmPrimitive> primitives, Changeset changeset, ChangesetProcessingType changesetProcessingType, ProgressMonitor progressMonitor) throws OsmTransferException {
     112    protected void uploadChangesAsDiffUpload(Collection<OsmPrimitive> primitives, ProgressMonitor progressMonitor) throws OsmTransferException {
    135113        // upload everything in one changeset
    136114        //
    137115        try {
    138116            progressMonitor.beginTask(tr("Starting to upload in one request ..."));
    139             if (changesetProcessingType.isUseNew()) {
    140                 api.createChangeset(changeset,progressMonitor.createSubTaskMonitor(0, false));
    141             } else {
    142                 api.updateChangeset(changeset,progressMonitor.createSubTaskMonitor(0, false));
    143             }
    144117            processed.addAll(api.uploadDiff(primitives, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false)));
    145118        } catch(OsmTransferException e) {
     
    148121            throw new OsmTransferException(e);
    149122        } finally {
    150             try {
    151                 api.stopChangeset(changesetProcessingType, progressMonitor.createSubTaskMonitor(0, false));
    152             } catch (Exception ee) {
    153                 OsmChangesetCloseException closeException = new OsmChangesetCloseException(ee);
    154                 closeException.setChangeset(api.getCurrentChangeset());
    155                 throw closeException;
    156             } finally {
    157                 progressMonitor.finishTask();
    158             }
    159 
     123            progressMonitor.finishTask();
    160124        }
    161125    }
     
    167131     * @param primitives list of objects to send
    168132     */
    169     public void uploadOsm(String apiVersion, Collection<OsmPrimitive> primitives, Changeset changeset, ChangesetProcessingType changesetProcessingType, ProgressMonitor progressMonitor) throws OsmTransferException {
     133    public void uploadOsm(String apiVersion, Collection<OsmPrimitive> primitives, Changeset changeset, boolean closeChangesetAfterUpload, ProgressMonitor progressMonitor) throws OsmTransferException {
    170134        processed = new LinkedList<OsmPrimitive>();
    171135        progressMonitor.beginTask(tr("Uploading data ..."));
     
    184148                useDiffUpload = false;
    185149            }
    186 
     150            if (changeset == null) {
     151                changeset = new Changeset();
     152            }
     153            if (changeset.getId() == 0) {
     154                api.openChangeset(changeset,progressMonitor.createSubTaskMonitor(0, false));
     155            } else {
     156                api.updateChangeset(changeset,progressMonitor.createSubTaskMonitor(0, false));
     157            }
     158            api.setChangeset(changeset);
    187159            if (useDiffUpload) {
    188                 uploadChangesAsDiffUpload(primitives,changeset, changesetProcessingType, progressMonitor.createSubTaskMonitor(0,false));
     160                uploadChangesAsDiffUpload(primitives,progressMonitor.createSubTaskMonitor(0,false));
    189161            } else {
    190                 uploadChangesIndividually(primitives,changeset,changesetProcessingType,  progressMonitor.createSubTaskMonitor(0,false));
    191             }
     162                uploadChangesIndividually(primitives,progressMonitor.createSubTaskMonitor(0,false));
     163            }
     164        } catch(OsmTransferException e) {
     165            throw e;
     166        } catch(Exception e) {
     167            throw new OsmTransferException(e);
    192168        } finally {
    193             progressMonitor.finishTask();
     169            try {
     170                if (closeChangesetAfterUpload && api.getChangeset() != null && api.getChangeset().getId() > 0) {
     171                    api.closeChangeset(changeset,progressMonitor.createSubTaskMonitor(0, false));
     172                    api.setChangeset(null);
     173                }
     174            } catch (Exception ee) {
     175                OsmChangesetCloseException closeException = new OsmChangesetCloseException(ee);
     176                closeException.setChangeset(api.getChangeset());
     177                throw closeException;
     178            } finally {
     179                progressMonitor.finishTask();
     180            }
    194181        }
    195182    }
  • trunk/src/org/openstreetmap/josm/io/OsmWriter.java

    r2070 r2115  
    66import java.util.Map.Entry;
    77
     8import org.openstreetmap.josm.data.coor.CoordinateFormat;
    89import org.openstreetmap.josm.data.osm.Changeset;
    910import org.openstreetmap.josm.data.osm.DataSet;
     
    1415import org.openstreetmap.josm.data.osm.Relation;
    1516import org.openstreetmap.josm.data.osm.RelationMember;
    16 import org.openstreetmap.josm.data.osm.User;
     17import org.openstreetmap.josm.data.osm.Tagged;
    1718import org.openstreetmap.josm.data.osm.Way;
    1819import org.openstreetmap.josm.data.osm.visitor.Visitor;
     
    143144
    144145    public void visit(Changeset cs) {
    145         addCommon(cs, "changeset");
    146         out.println(">\n");
    147         addTags(cs, "changeset", false);
    148     }
    149 
    150     public final void footer(PrintWriter out) {
    151         out.println("</osm>");
     146        out.print("  <changeset ");
     147        out.print(" id='"+cs.getId()+"'");
     148        if (cs.getUser() != null) {
     149            out.print(" user='"+cs.getUser().getName() +"'");
     150            out.print(" uid='"+cs.getUser().getId() +"'");
     151        }
     152        if (cs.getCreatedAt() != null) {
     153            out.print(" created_at='"+DateUtils.fromDate(cs.getCreatedAt()) +"'");
     154        }
     155        if (cs.getClosedAt() != null) {
     156            out.print(" closed_at='"+DateUtils.fromDate(cs.getClosedAt()) +"'");
     157        }
     158        out.print(" open='"+ (cs.isOpen() ? "true" : "false") +"'");
     159        if (cs.getMin() != null) {
     160            out.print(" min_lon='"+ cs.getMin().lonToString(CoordinateFormat.DECIMAL_DEGREES) +"'");
     161            out.print(" min_lat='"+ cs.getMin().latToString(CoordinateFormat.DECIMAL_DEGREES) +"'");
     162        }
     163        if (cs.getMax() != null) {
     164            out.print(" max_lon='"+ cs.getMin().lonToString(CoordinateFormat.DECIMAL_DEGREES) +"'");
     165            out.print(" max_lat='"+ cs.getMin().latToString(CoordinateFormat.DECIMAL_DEGREES) +"'");
     166        }
     167        out.println(">");
     168        addTags(cs, "changeset", false); // also writes closing </changeset>
    152169    }
    153170
     
    164181    }
    165182
    166     private void addTags(OsmPrimitive osm, String tagname, boolean tagOpen) {
     183    private void addTags(Tagged osm, String tagname, boolean tagOpen) {
    167184        if (osm.hasKeys()) {
    168185            if (tagOpen) {
    169186                out.println(">");
    170187            }
    171             for (Entry<String, String> e : osm.entrySet()) {
     188            for (Entry<String, String> e : osm.getKeys().entrySet()) {
    172189                if ((osm instanceof Changeset) || !("created_by".equals(e.getKey()))) {
    173190                    out.println("    <tag k='"+ XmlWriter.encode(e.getKey()) +
Note: See TracChangeset for help on using the changeset viewer.