Statistics
| Revision:

gvsig-projects-pool / org.gvsig.online / trunk / org.gvsig.online / org.gvsig.online.lib / org.gvsig.online.lib.impl / src / main / java / org / gvsig / online / lib / impl / workspace / ChangesImpl.java @ 9512

History | View | Annotate | Download (12.1 KB)

1
/*
2
 * gvSIG. Desktop Geographic Information System.
3
 * 
4
 * Copyright (C) 2007-2020 gvSIG Association.
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License 
17
 * along with this program. If not, see <https://www.gnu.org/licenses/>. 
18
 * 
19
 * For any additional information, do not hesitate to contact us
20
 * at info AT gvsig.com, or visit our website www.gvsig.com.
21
 */
22
package org.gvsig.online.lib.impl.workspace;
23

    
24
import java.util.Iterator;
25
import java.util.List;
26
import java.util.function.Predicate;
27
import javax.swing.event.ChangeListener;
28
import org.gvsig.expressionevaluator.ExpressionBuilder;
29
import org.gvsig.expressionevaluator.ExpressionUtils;
30
import org.gvsig.fmap.dal.feature.EditableFeature;
31
import org.gvsig.fmap.dal.feature.Feature;
32
import org.gvsig.fmap.dal.feature.FeatureSet;
33
import org.gvsig.fmap.dal.feature.FeatureStore;
34
import org.gvsig.fmap.dal.feature.paging.FeaturePagingHelper;
35
import org.gvsig.online.lib.api.workingcopy.OnlineChange;
36
import org.gvsig.online.lib.api.workingcopy.OnlineEntity;
37
import static org.gvsig.online.lib.impl.workspace.tables.EntitiesTable.COD_ENTITY;
38
import org.gvsig.tools.ToolsLocator;
39
import org.gvsig.tools.dispose.DisposeUtils;
40
import org.gvsig.tools.dispose.impl.AbstractDisposable;
41
import org.gvsig.tools.exception.BaseException;
42
import org.gvsig.tools.swing.api.ChangeListenerHelper;
43
import org.gvsig.tools.swing.api.ToolsSwingLocator;
44
import org.gvsig.tools.task.SimpleTaskStatus;
45
import org.gvsig.tools.util.GetItemWithSize64;
46
import org.slf4j.Logger;
47
import org.slf4j.LoggerFactory;
48
import org.gvsig.online.lib.api.workingcopy.OnlineChanges;
49

    
50
/**
51
 *
52
 * @author gvSIG Team
53
 * @param <CHANGE>
54
 */
55
@SuppressWarnings("UseSpecificCatch")
56
public abstract class ChangesImpl<CHANGE extends OnlineChange> extends AbstractDisposable implements OnlineChanges<CHANGE> {
57
    
58
    private static final Logger LOGGER = LoggerFactory.getLogger(ChangesImpl.class);
59
    
60
    final FeaturePagingHelper changes;
61
    ChangeListenerHelper changeListernerHelper; 
62
    private final String selectedFieldName;
63

    
64
    public ChangesImpl(GetItemWithSize64<Feature> changes, String selectedFieldName) {
65
        super();
66
        this.changes = (FeaturePagingHelper) changes;
67
        DisposeUtils.bind(this.changes);
68
        this.changeListernerHelper = ToolsSwingLocator.getToolsSwingManager().createChangeListenerHelper();
69
        this.selectedFieldName = selectedFieldName;
70
        
71
    }
72

    
73
    abstract protected CHANGE createChange(Feature f);
74

    
75
    abstract protected void updateChange(FeatureStore store, CHANGE change);
76

    
77
    @Override
78
    public void addSelected(long selected) {
79
        try {
80
            FeatureStore store = this.changes.getFeatureStore();
81
            store.edit(FeatureStore.MODE_PASS_THROUGH);
82
            Feature feat = this.changes.getFeatureAt(selected);
83
            CHANGE row = this.createChange(feat);
84
            row.setSelected(true);
85
            updateChange(store, row);
86
            store.finishEditingQuietly();
87
            this.changes.reload(); //FIXME: modificar el paging helper para que no haga falta esto
88
            this.fireChanges();
89
        } catch (Exception ex) {
90
            LOGGER.warn("Can't add to selection.", ex);
91
        }
92
    }
93

    
94
    @Override
95
    public void removeSelected(long selected) {
96
        try {
97
            FeatureStore store = this.changes.getFeatureStore();
98
            store.edit(FeatureStore.MODE_PASS_THROUGH);
99
            Feature feat = this.changes.getFeatureAt(selected);
100
            CHANGE row = this.createChange(feat);
101
            row.setSelected(false);
102
            updateChange(store, row);
103
            store.finishEditingQuietly();
104
            this.changes.reload(); //FIXME: modificar el paging helper para que no haga falta esto
105
            this.fireChanges();
106
        } catch (Exception ex) {
107
            LOGGER.warn("Can't add to selection.", ex);
108
        }
109
    }
110

    
111
    @Override
112
    public void setSelected(long selected) {
113
        try {
114
            FeatureStore store = this.changes.getFeatureStore();
115
            store.edit(FeatureStore.MODE_PASS_THROUGH);
116
            store.update(this.selectedFieldName, false);
117

    
118
            Feature sourceFeat = this.changes.getFeatureAt(selected);
119
            CHANGE row = this.createChange(sourceFeat);
120
            row.setSelected(true);
121
            updateChange(store, row);
122

    
123
            //TODO: Para quitar el reload de abajo
124
//            EditableFeature targetFeat = change.createFeature(store);
125
//            targetFeat.setUpdatable(true);
126
//            this.changes.update(targetFeature);
127

    
128
            store.finishEditingQuietly();
129
            this.changes.reload(); //FIXME: modificar el paging helper para que no haga falta esto
130
            this.fireChanges();
131
        } catch (Exception ex) {
132
            LOGGER.warn("Can't process.", ex);
133
        }
134
    }
135

    
136
    @Override
137
    public void clearSelection() {
138
        SimpleTaskStatus status = ToolsLocator.getTaskStatusManager()
139
                .createDefaultSimpleTaskStatus("_Clear_changes_selection");
140
        status.setAutoremove(true);
141
        status.add();
142
        try {
143
            status.setIndeterminate();
144
            status.message("_Updating_selection");
145
            FeatureStore store = this.changes.getFeatureStore();
146
            store.edit(FeatureStore.MODE_PASS_THROUGH);
147
            store.update(this.selectedFieldName, false);
148
            status.message("_Finising_process");
149
            store.finishEditing();
150
            status.message("_Reloading_changes");
151
            this.changes.reload();
152
            status.message("_Refresing");
153
            this.fireChanges();
154
            status.terminate();
155
        } catch (Exception ex) {
156
            LOGGER.warn("Can't process.", ex);
157
            status.message("Can't process changes ("+ex.getMessage()+")");
158
            status.abort();
159
        }
160
    }
161

    
162
    @Override
163
    public void setSelectionAll() {
164
        SimpleTaskStatus status = ToolsLocator.getTaskStatusManager()
165
                .createDefaultSimpleTaskStatus("_Selecting_all_changes");
166
        status.setAutoremove(true);
167
        status.add();
168
        try {
169
            status.setIndeterminate();
170
            status.message("_Updating_selection");
171
            FeatureStore store = this.changes.getFeatureStore();
172
            store.edit(FeatureStore.MODE_PASS_THROUGH);
173
            store.update(this.selectedFieldName, true);
174
            status.message("_Finising_process");
175
            store.finishEditingQuietly();
176
            status.message("_Reloading_changes");
177
            this.changes.reload();
178
            status.message("_Refresing");
179
            this.fireChanges();
180
            status.terminate();
181
        } catch (Exception ex) {
182
            LOGGER.warn("Can't process.", ex);
183
            status.message("Can't process changes ("+ex.getMessage()+")");
184
            status.abort();
185
        }
186
    }
187
    
188
    @Override
189
    public void process(Iterator<Long> rows, Predicate<CHANGE> action) {
190
        SimpleTaskStatus status = ToolsLocator.getTaskStatusManager()
191
                .createDefaultSimpleTaskStatus("_Processing_changes");
192
        status.setAutoremove(true);
193
        status.add();
194
        try {
195
            status.setRangeOfValues(0, this.size64());
196
            status.message("_Processing");
197
            FeatureStore store = this.changes.getFeatureStore();
198
            FeatureSet set = this.changes.getFeatureSet();
199
            store.edit(FeatureStore.MODE_PASS_THROUGH);
200
            while(rows.hasNext()){
201
                Long row = rows.next();
202
                Feature sourceFeat = this.changes.getFeatureAt(row);
203
                CHANGE change = this.createChange(sourceFeat);
204
                if(action.test(change)){
205
                    EditableFeature targetFeat = change.createFeature(store);
206
                    targetFeat.setUpdatable(true);
207
                    set.update(targetFeat);
208
//                    updateChange(store, change);
209
                }
210
                status.incrementCurrentValue();
211
            }
212
            status.message("_Finising_process");
213
            store.finishEditingQuietly();
214
            status.message("_Reloading_changes");
215
            this.changes.reload();
216
            status.message("_Refresing");
217
            this.fireChanges();
218
            status.terminate();
219
        } catch (Exception ex) {
220
            LOGGER.warn("Can't process.", ex);
221
            status.message("Can't process changes ("+ex.getMessage()+")");
222
            status.abort();
223
        }
224
    }
225

    
226
    
227
    @Override
228
    public boolean isSelectedIndex(long index) {
229
        try {
230
            Feature feat = this.changes.getFeatureAt(index);
231
            return feat.getBoolean(this.selectedFieldName);
232
        } catch (BaseException ex) {
233
            LOGGER.warn("Can't check if is selected.", ex);
234
            throw new RuntimeException("Can't check if is selected.", ex);
235
        }
236
    }
237

    
238
    @Override
239
    public boolean isSelectionEmpty() {
240
        try {
241
            FeatureStore store = this.changes.getFeatureStore();
242
            Feature foundFeat = store.findFirst("\""+this.selectedFieldName+"\"");
243
            return foundFeat == null;
244
        } catch (BaseException ex) {
245
            LOGGER.warn("Can't check if is empty selection.", ex);
246
            throw new RuntimeException("Can't check if is selected.", ex);
247
        }
248
    }
249

    
250
    @Override
251
    public boolean isSelectionFromEntitiesEmpty(List<OnlineEntity> entities) {
252
        try {
253
            if(entities.isEmpty()){
254
                return false;
255
            }
256
            
257
            ExpressionBuilder expBuilder = ExpressionUtils.createExpressionBuilder();
258
            for (OnlineEntity entity : entities) {
259
                if(entity != null){
260
                    expBuilder.or(
261
                            expBuilder.eq(
262
                                expBuilder.column(COD_ENTITY),
263
                                expBuilder.constant(entity.getEntityCode())
264
                            )
265
                    );
266
                }
267
            }
268
            ExpressionBuilder.Value or = expBuilder.value();
269
            expBuilder.set(null);
270
            expBuilder.and(expBuilder.column(this.selectedFieldName));
271
            expBuilder.and(expBuilder.group(or));
272
            
273
            FeatureStore store = this.changes.getFeatureStore();
274
            Feature foundFeat = store.findFirst(expBuilder.toString());
275
            return foundFeat == null;
276
        } catch (BaseException ex) {
277
            LOGGER.warn("Can't check if is empty selection.", ex);
278
            throw new RuntimeException("Can't check if is selected.", ex);
279
        }
280
    }
281

    
282
    @Override
283
    public long size64() {
284
        return changes.size64();
285
    }
286
    
287
    @Override
288
    public boolean isEmpty(){
289
        return changes.isEmpty();
290
    }
291

    
292
    @Override
293
    public CHANGE get64(long position) {
294
        Feature f = changes.get64(position);
295
        if (f == null) {
296
            return null;
297
        }
298
        return (CHANGE) this.createChange(f);
299
    }
300

    
301
    private void fireChanges() {
302
        changeListernerHelper.fireEvent();
303
    }
304

    
305
    @Override
306
    public void addChangeListener(ChangeListener listener) {
307
        changeListernerHelper.addChangeListener(listener);
308
    }
309

    
310
    @Override
311
    public ChangeListener[] getChangeListeners() {
312
        return changeListernerHelper.getChangeListeners();
313
    }
314

    
315
    @Override
316
    public void removeChangeListener(ChangeListener listener) {
317
        changeListernerHelper.removeChangeListener(listener);
318
    }
319

    
320
    @Override
321
    public void removeAllChangeListener() {
322
        changeListernerHelper.removeAllChangeListener();
323
    }
324

    
325
    @Override
326
    public boolean hasChangeListeners() {
327
        return changeListernerHelper.hasChangeListeners();
328
    }
329

    
330
    @Override
331
    protected void doDispose() throws BaseException {
332
        DisposeUtils.disposeQuietly(this.changes);
333
    }
334

    
335
    
336
    
337
}