source: josm/trunk/src/org/openstreetmap/josm/io/OsmServerChangesetReader.java@ 2667

Last change on this file since 2667 was 2613, checked in by Gubaer, 14 years ago

new: global in-memory cache for downloaded changesets
new: toggle dialog for changesets
new: downloading of changesets (currently without changeset content, will follow later)

File size: 6.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.io.InputStream;
8import java.util.ArrayList;
9import java.util.Collection;
10import java.util.Collections;
11import java.util.Iterator;
12import java.util.List;
13
14import org.openstreetmap.josm.data.osm.Changeset;
15import org.openstreetmap.josm.data.osm.DataSet;
16import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
17import org.openstreetmap.josm.gui.progress.ProgressMonitor;
18
19/**
20 * Reads the history of an {@see OsmPrimitive} from the OSM API server.
21 *
22 */
23public class OsmServerChangesetReader extends OsmServerReader {
24
25 /**
26 * constructor
27 *
28 */
29 public OsmServerChangesetReader(){
30 setDoAuthenticate(false);
31 }
32
33 /**
34 * don't use - not implemented!
35 *
36 */
37 @Override
38 public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
39 return null;
40 }
41
42 /**
43 * Queries a list
44 * @param query the query specification. Must not be null.
45 * @param monitor a progress monitor. Set to {@see NullProgressMonitor#INSTANCE} if null
46 * @return the list of changesets read from the server
47 * @throws IllegalArgumentException thrown if query is null
48 * @throws OsmTransferException
49 */
50 public List<Changeset> queryChangesets(ChangesetQuery query, ProgressMonitor monitor) throws OsmTransferException {
51 if (query == null)
52 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "query"));
53 if (monitor == null) {
54 monitor = NullProgressMonitor.INSTANCE;
55 }
56 try {
57 monitor.beginTask(tr("Reading changesets..."));
58 StringBuffer sb = new StringBuffer();
59 sb.append("changesets?").append(query.getQueryString());
60 InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
61 if (in == null)
62 return null;
63 monitor.indeterminateSubTask(tr("Downloading changesets ..."));
64 List<Changeset> changesets = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
65 return changesets;
66 } catch(OsmTransferException e) {
67 throw e;
68 } catch(IllegalDataException e) {
69 throw new OsmTransferException(e);
70 } finally {
71 monitor.finishTask();
72 }
73 }
74
75 /**
76 * Reads the changeset with id <code>id</code> from the server
77 *
78 * @param id the changeset id. id > 0 required.
79 * @param monitor the progress monitor. Set to {@see NullProgressMonitor#INSTANCE} if null
80 * @return the changeset read
81 * @throws OsmTransferException thrown if something goes wrong
82 * @throws IllegalArgumentException if id <= 0
83 */
84 public Changeset readChangeset(long id, ProgressMonitor monitor) throws OsmTransferException {
85 if (id <= 0)
86 throw new IllegalArgumentException(tr("Parameter ''{0}'' > 0 expected. Got ''{1}''.", "id", id));
87 if (monitor == null) {
88 monitor = NullProgressMonitor.INSTANCE;
89 }
90 try {
91 monitor.beginTask(tr("Reading changeset {0} ...",id));
92 StringBuffer sb = new StringBuffer();
93 sb.append("changeset/").append(id);
94 InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
95 if (in == null)
96 return null;
97 monitor.indeterminateSubTask(tr("Downloading changeset {0} ...", id));
98 List<Changeset> changesets = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
99 if (changesets == null || changesets.isEmpty())
100 return null;
101 return changesets.get(0);
102 } catch(OsmTransferException e) {
103 throw e;
104 } catch(IllegalDataException e) {
105 throw new OsmTransferException(e);
106 } finally {
107 monitor.finishTask();
108 }
109 }
110
111 /**
112 * Reads the changeset with id <code>id</code> from the server
113 *
114 * @param ids the list of ids. Ignored if null. Only load changesets for ids > 0.
115 * @param monitor the progress monitor. Set to {@see NullProgressMonitor#INSTANCE} if null
116 * @return the changeset read
117 * @throws OsmTransferException thrown if something goes wrong
118 * @throws IllegalArgumentException if id <= 0
119 */
120 public List<Changeset> readChangesets(Collection<Integer> ids, ProgressMonitor monitor) throws OsmTransferException {
121 if (ids == null)
122 return Collections.emptyList();
123 if (monitor == null) {
124 monitor = NullProgressMonitor.INSTANCE;
125 }
126 try {
127 monitor.beginTask(trn("Downloading {0} changeset ...", "Downloading {0} changesets ...",ids.size(),ids.size()));
128 monitor.setTicksCount(ids.size());
129 List<Changeset> ret = new ArrayList<Changeset>();
130 int i=0;
131 for (Iterator<Integer> it = ids.iterator(); it.hasNext(); ) {
132 int id = it.next();
133 if (id <= 0) {
134 continue;
135 }
136 i++;
137 StringBuffer sb = new StringBuffer();
138 sb.append("changeset/").append(id);
139 InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
140 if (in == null)
141 return null;
142 monitor.indeterminateSubTask(tr("({0}/{1}) Downloading changeset {0} ...", i,ids.size(), id));
143 List<Changeset> changesets = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
144 if (changesets == null || changesets.isEmpty()) {
145 continue;
146 }
147 ret.addAll(changesets);
148 monitor.worked(1);
149 }
150 return ret;
151 } catch(OsmTransferException e) {
152 throw e;
153 } catch(IllegalDataException e) {
154 throw new OsmTransferException(e);
155 } finally {
156 monitor.finishTask();
157 }
158 }
159
160 /**
161 * not implemented yet
162 *
163 * @param id
164 * @param monitor
165 * @return
166 * @throws OsmTransferException
167 */
168 public Changeset downloadChangeset(long id, ProgressMonitor monitor) throws OsmTransferException {
169 return null;
170 }
171}
Note: See TracBrowser for help on using the repository browser.