comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/DatacageTwinPanel.java @ 1321:cfbfaadf4b6f

Remove Artifacts not needed anymore when reparameterizing w-diffs. flys-client/trunk@2962 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Thu, 13 Oct 2011 12:34:46 +0000
parents 4c716bec7822
children 1627a28c4504
comparison
equal deleted inserted replaced
1320:5733c05ce24c 1321:cfbfaadf4b6f
6 import java.util.Set; 6 import java.util.Set;
7 7
8 import com.google.gwt.core.client.GWT; 8 import com.google.gwt.core.client.GWT;
9 import com.google.gwt.user.client.rpc.AsyncCallback; 9 import com.google.gwt.user.client.rpc.AsyncCallback;
10 10
11 import com.smartgwt.client.data.Record;
12 import com.smartgwt.client.types.ListGridFieldType;
11 import com.smartgwt.client.widgets.Canvas; 13 import com.smartgwt.client.widgets.Canvas;
12 import com.smartgwt.client.widgets.Label;
13 import com.smartgwt.client.widgets.layout.HLayout; 14 import com.smartgwt.client.widgets.layout.HLayout;
14 import com.smartgwt.client.widgets.layout.VLayout; 15 import com.smartgwt.client.widgets.layout.VLayout;
15 import com.smartgwt.client.widgets.grid.ListGrid; 16 import com.smartgwt.client.widgets.grid.ListGrid;
16 import com.smartgwt.client.widgets.grid.ListGridField; 17 import com.smartgwt.client.widgets.grid.ListGridField;
17 import com.smartgwt.client.widgets.grid.ListGridRecord; 18 import com.smartgwt.client.widgets.grid.ListGridRecord;
18 import com.smartgwt.client.widgets.events.ClickEvent; 19 import com.smartgwt.client.widgets.events.ClickEvent;
20 import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
21 import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
19 22
20 import de.intevation.flys.client.shared.model.Artifact; 23 import de.intevation.flys.client.shared.model.Artifact;
24 import de.intevation.flys.client.shared.model.Collection;
21 import de.intevation.flys.client.shared.model.Data; 25 import de.intevation.flys.client.shared.model.Data;
22 import de.intevation.flys.client.shared.model.DataItem; 26 import de.intevation.flys.client.shared.model.DataItem;
23 import de.intevation.flys.client.shared.model.DefaultData; 27 import de.intevation.flys.client.shared.model.DefaultData;
24 import de.intevation.flys.client.shared.model.DefaultDataItem; 28 import de.intevation.flys.client.shared.model.DefaultDataItem;
25 import de.intevation.flys.client.shared.model.DataList; 29 import de.intevation.flys.client.shared.model.DataList;
26 import de.intevation.flys.client.shared.model.User; 30 import de.intevation.flys.client.shared.model.User;
27 31
28 import de.intevation.flys.client.client.FLYSConstants; 32 import de.intevation.flys.client.client.FLYSConstants;
29 import de.intevation.flys.client.client.event.StepForwardEvent; 33 import de.intevation.flys.client.client.event.StepForwardEvent;
30 import de.intevation.flys.client.shared.model.Recommendation; 34 import de.intevation.flys.client.shared.model.Recommendation;
31 import de.intevation.flys.client.shared.model.ToLoad;
32 import de.intevation.flys.client.shared.model.Recommendation.Facet; 35 import de.intevation.flys.client.shared.model.Recommendation.Facet;
33 import de.intevation.flys.client.shared.model.Recommendation.Filter; 36 import de.intevation.flys.client.shared.model.Recommendation.Filter;
34 37
38 import de.intevation.flys.client.client.Config;
35 import de.intevation.flys.client.client.services.LoadArtifactService; 39 import de.intevation.flys.client.client.services.LoadArtifactService;
36 import de.intevation.flys.client.client.services.LoadArtifactServiceAsync; 40 import de.intevation.flys.client.client.services.LoadArtifactServiceAsync;
37 import de.intevation.flys.client.client.Config; 41 import de.intevation.flys.client.client.services.RemoveArtifactService;
42 import de.intevation.flys.client.client.services.RemoveArtifactServiceAsync;
38 43
39 // TODO Probably better to branch off AbstractUIProvider. 44 // TODO Probably better to branch off AbstractUIProvider.
40 // TODO Merge with other datacage-widget impls. 45 // TODO Merge with other datacage-widget impls.
41 /** 46 /**
42 * Panel containing a Grid and a "next" button. The Grid is fed by a 47 * Panel containing a Grid and a "next" button. The Grid is fed by a
49 54
50 protected String dataName; 55 protected String dataName;
51 56
52 protected User user; 57 protected User user;
53 58
59 /** ListGrid that displays user-selected pairs to build differences with. */
54 protected ListGrid differencesList; 60 protected ListGrid differencesList;
55 61
62 /*
63 * List to track previously selected but now removed pairs. (Needed to
64 * be able to identify artifacts that can be removed from the collection.
65 */
66 protected List<RecommendationPairRecord> removedPairs =
67 new ArrayList<RecommendationPairRecord>();
68
69 /** Service handle to clone and add artifacts to collection. */
56 LoadArtifactServiceAsync loadArtifactService = GWT.create( 70 LoadArtifactServiceAsync loadArtifactService = GWT.create(
57 de.intevation.flys.client.client.services.LoadArtifactService.class); 71 de.intevation.flys.client.client.services.LoadArtifactService.class);
58 72
73 /** Service to remove artifacts from collection. */
74 RemoveArtifactServiceAsync removeArtifactService = GWT.create(
75 de.intevation.flys.client.client.services.RemoveArtifactService.class);
59 76
60 public DatacageTwinPanel(User user) { 77 public DatacageTwinPanel(User user) {
61 super(); 78 super();
62 this.user = user; 79 this.user = user;
63 } 80 }
168 } 185 }
169 186
170 187
171 /** 188 /**
172 * Validates the selection. 189 * Validates the selection.
190 * @return List of internationalized errror messages (if any).
173 */ 191 */
174 @Override 192 @Override
175 public List<String> validate() { 193 public List<String> validate() {
176 List<String> errors = new ArrayList<String>(); 194 List<String> errors = new ArrayList<String>();
177 if (differencesList.getRecords().length == 0) { 195 if (differencesList.getRecords().length == 0) {
195 public Canvas createWidget() { 213 public Canvas createWidget() {
196 VLayout layout = new VLayout(); 214 VLayout layout = new VLayout();
197 differencesList = new ListGrid(); 215 differencesList = new ListGrid();
198 216
199 differencesList.setCanEdit(false); 217 differencesList.setCanEdit(false);
200 differencesList.setCanRemoveRecords(true);
201 differencesList.setCanSort(false); 218 differencesList.setCanSort(false);
202 differencesList.setHeight(150); 219 differencesList.setHeight(150);
203 differencesList.setShowAllRecords(true); 220 differencesList.setShowAllRecords(true);
204 221
205 ListGridField nameField = new ListGridField("first", "Minuend"); 222 ListGridField nameField = new ListGridField("first", "Minuend");
206 ListGridField capitalField = new ListGridField("second", "Subtrahend"); 223 ListGridField capitalField = new ListGridField("second", "Subtrahend");
224 // Track removed rows, therefore more or less reimplement
225 // setCanRecomeRecords.
226 final ListGridField removeField =
227 new ListGridField("_removeRecord", "Remove Record"){{
228 setType(ListGridFieldType.ICON);
229 setIcon("[SKIN]/actions/remove.png");
230 setCanEdit(false);
231 setCanFilter(false);
232 setCanSort(false);
233 setCanGroupBy(false);
234 setCanFreeze(false);
235 setWidth(25);
236 }};
237
207 differencesList.setFields(new ListGridField[] {nameField, 238 differencesList.setFields(new ListGridField[] {nameField,
208 capitalField}); 239 capitalField, removeField});
209 240
241 differencesList.addRecordClickHandler(new RecordClickHandler() {
242 public void onRecordClick(final RecordClickEvent event) {
243 // Just handle remove-clicks
244 if(!event.getField().getName().equals(removeField.getName())) {
245 return;
246 }
247 trackRemoved(event.getRecord());
248 event.getViewer().removeData(event.getRecord());
249 }
250 });
210 layout.addMember(differencesList); 251 layout.addMember(differencesList);
211 252
212 return layout; 253 return layout;
213 } 254 }
214 255
256
257 public void trackRemoved(Record r) {
258 RecommendationPairRecord pr = (RecommendationPairRecord) r;
259 this.removedPairs.add(pr);
260 }
215 261
216 /** 262 /**
217 * Validates data, does nothing if invalid, otherwise clones new selected 263 * Validates data, does nothing if invalid, otherwise clones new selected
218 * waterlevels and add them to collection, forward the artifact. 264 * waterlevels and add them to collection, forward the artifact.
219 */ 265 */
227 return; 273 return;
228 } 274 }
229 275
230 Config config = Config.getInstance(); 276 Config config = Config.getInstance();
231 ListGridRecord[] records = differencesList.getRecords(); 277 ListGridRecord[] records = differencesList.getRecords();
232
233 // TODO Problem when same facet participates in two diffs.
234 //
235 // Resolve whether "new" ones were already cloned.
236 278
237 List<Recommendation> ar = new ArrayList<Recommendation>(); 279 List<Recommendation> ar = new ArrayList<Recommendation>();
238 List<Recommendation> all = new ArrayList<Recommendation>(); 280 List<Recommendation> all = new ArrayList<Recommendation>();
239 for (ListGridRecord record : records) { 281 for (ListGridRecord record : records) {
240 RecommendationPairRecord r = 282 RecommendationPairRecord r =
250 } 292 }
251 } 293 }
252 294
253 final Recommendation[] toClone = ar.toArray(new Recommendation[ar.size()]); 295 final Recommendation[] toClone = ar.toArray(new Recommendation[ar.size()]);
254 final Recommendation[] toUse = all.toArray(new Recommendation[all.size()]); 296 final Recommendation[] toUse = all.toArray(new Recommendation[all.size()]);
297
298 // Find out whether "old" artifacts have to be removed.
299 List<String> artifactIdsToRemove = new ArrayList<String>();
300 for (RecommendationPairRecord rp: this.removedPairs) {
301 Recommendation first = rp.getFirst();
302 Recommendation second = rp.getSecond();
303
304 for (Recommendation recommendation: toUse) {
305 if (first != null && first.getIDs().equals(recommendation.getIDs())) {
306 first = null;
307 }
308 if (second != null && second.getIDs().equals(recommendation.getIDs())) {
309 second = null;
310 }
311
312 if (first == null && second == null) {
313 break;
314 }
315 }
316 if (first != null) {
317 artifactIdsToRemove.add(first.getIDs());
318 }
319 if (second != null) {
320 artifactIdsToRemove.add(second.getIDs());
321 }
322 }
323
324 // Remove old artifacts, if any. Do this asychronously without much
325 // feedback.
326 for(final String uuid: artifactIdsToRemove) {
327 removeArtifactService.remove(this.collection,
328 uuid,
329 config.getServerUrl(),
330 config.getLocale(),
331 new AsyncCallback<Collection>() {
332 public void onFailure(Throwable caught) {
333 GWT.log("RemoveArtifact (" + uuid + ") failed.");
334 }
335 public void onSuccess(Collection collection) {
336 GWT.log("RemoveArtifact succeeded");
337 }
338 });
339 }
340
341 // Clone new ones, go forward.
255 loadArtifactService.loadMany( 342 loadArtifactService.loadMany(
256 this.collection, 343 this.collection,
257 toClone, 344 toClone,
258 "waterlevel", 345 "waterlevel",
259 config.getServerUrl(), 346 config.getServerUrl(),
271 } 358 }
272 } ); 359 } );
273 } 360 }
274 361
275 362
363 /**
364 * Create Data and DataItem from selection.
365 */
276 protected Data[] getData( 366 protected Data[] getData(
277 Recommendation[] newRecommendations, 367 Recommendation[] newRecommendations,
278 Artifact[] newArtifacts, 368 Artifact[] newArtifacts,
279 Recommendation[] oldRecommendations) 369 Recommendation[] oldRecommendations)
280 { 370 {

http://dive4elements.wald.intevation.org