Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.buffer / org.gvsig.raster.lib.buffer.impl / src / main / java / org / gvsig / raster / lib / buffer / impl / operations / DefaultOperationList.java @ 8682

History | View | Annotate | Download (13.6 KB)

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

    
25
import java.util.ArrayList;
26
import java.util.Collection;
27
import java.util.HashSet;
28
import java.util.Iterator;
29
import java.util.List;
30
import java.util.ListIterator;
31
import java.util.Set;
32

    
33
import org.apache.commons.lang3.StringUtils;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36

    
37
import org.gvsig.raster.lib.buffer.api.Buffer;
38
import org.gvsig.raster.lib.buffer.api.BufferLocator;
39
import org.gvsig.raster.lib.buffer.api.OperationManager;
40
import org.gvsig.raster.lib.buffer.api.exceptions.BufferOperationException;
41
import org.gvsig.raster.lib.buffer.api.operations.InvalidLookupParametersException;
42
import org.gvsig.raster.lib.buffer.api.operations.InvalidLookupParametersException.Problem;
43
import org.gvsig.raster.lib.buffer.api.operations.Operation;
44
import org.gvsig.raster.lib.buffer.api.operations.OperationFactory;
45
import org.gvsig.raster.lib.buffer.api.operations.OperationFactory.Applicable;
46
import org.gvsig.raster.lib.buffer.api.operations.OperationList;
47
import org.gvsig.raster.lib.buffer.api.operations.OperationListEntry;
48
import org.gvsig.tools.ToolsLocator;
49
import org.gvsig.tools.dynobject.DynClass;
50
import org.gvsig.tools.dynobject.DynField;
51
import org.gvsig.tools.dynobject.DynField_v2;
52
import org.gvsig.tools.dynobject.DynObject;
53
import org.gvsig.tools.dynobject.DynStruct;
54
import org.gvsig.tools.persistence.PersistenceManager;
55
import org.gvsig.tools.persistence.PersistentState;
56
import org.gvsig.tools.persistence.exception.PersistenceException;
57
import org.gvsig.tools.lang.Cloneable;
58

    
59

    
60
/**
61
 * @author fdiaz
62
 *
63
 */
64
public class DefaultOperationList implements OperationList {
65

    
66
    private static final Logger LOG = LoggerFactory.getLogger(DefaultOperationList.class);
67

    
68
    /**
69
     * Persistence definition name
70
     */
71
    public static final String PERSISTENT_NAME = "OperationListPersistence";
72
    /**
73
     * Description of persistence definition
74
     */
75
    public static final String PERSISTENT_DESCRIPTION = "Persistence definition of operation list";
76

    
77
    private final static String OPERATIONS_PERSISTENCE_FIELD = "operations";
78

    
79

    
80
    private List<OperationListEntry> operations;
81

    
82
    private DynObject defaultParameters = null;
83

    
84
    /**
85
     *
86
     */
87
    public DefaultOperationList() {
88
        this.operations = new ArrayList<OperationListEntry>();
89
    }
90

    
91
    @Override
92
    public int size() {
93
        return this.operations.size();
94
    }
95

    
96
    @Override
97
    public boolean isEmpty() {
98
        return this.operations.isEmpty();
99
    }
100

    
101
    @Override
102
    public boolean contains(Object o) {
103
        return this.operations.contains(o);
104
    }
105

    
106
    @Override
107
    public Iterator<OperationListEntry> iterator() {
108
        return this.operations.iterator();
109
    }
110

    
111
    @Override
112
    public Object[] toArray() {
113
        return this.operations.toArray();
114
    }
115

    
116
    @Override
117
    public <T> T[] toArray(T[] a) {
118
        return this.operations.toArray(a);
119
    }
120

    
121
    @Override
122
    public boolean add(OperationListEntry e) {
123
        return this.operations.add(e);
124
    }
125

    
126
    @Override
127
    public boolean remove(Object o) {
128
        return this.operations.remove(o);
129
    }
130

    
131
    @Override
132
    public boolean containsAll(Collection<?> c) {
133
        return this.operations.containsAll(c);
134
    }
135

    
136
    @Override
137
    public boolean addAll(Collection<? extends OperationListEntry> c) {
138
        return this.operations.addAll(c);
139
    }
140

    
141
    @Override
142
    public boolean addAll(int index, Collection<? extends OperationListEntry> c) {
143
        return this.operations.addAll(index, c);
144
    }
145

    
146
    @Override
147
    public boolean removeAll(Collection<?> c) {
148
        return this.operations.removeAll(c);
149
    }
150

    
151
    @Override
152
    public boolean retainAll(Collection<?> c) {
153
        return this.operations.retainAll(c);
154
    }
155

    
156
    @Override
157
    public void clear() {
158
        this.operations.clear();
159
    }
160

    
161
    @Override
162
    public OperationListEntry get(int index) {
163
        return this.operations.get(index);
164
    }
165

    
166
    @Override
167
    public OperationListEntry set(int index, OperationListEntry element) {
168
        return this.operations.set(index, element);
169
    }
170

    
171
    @Override
172
    public void add(int index, OperationListEntry element) {
173
        if(index<0){
174
            index = this.operations.size()+index;
175
        }
176
        this.operations.add(index, element);
177
    }
178

    
179
    @Override
180
    public OperationListEntry remove(int index) {
181
        return this.operations.remove(index);
182
    }
183

    
184
    @Override
185
    public int indexOf(Object o) {
186
        return this.operations.indexOf(o);
187
    }
188

    
189
    @Override
190
    public int lastIndexOf(Object o) {
191
        return this.operations.lastIndexOf(o);
192
    }
193

    
194
    @Override
195
    public ListIterator<OperationListEntry> listIterator() {
196
        return this.operations.listIterator();
197
    }
198

    
199
    @Override
200
    public ListIterator<OperationListEntry> listIterator(int index) {
201
        return this.operations.listIterator(index);
202
    }
203

    
204
    @Override
205
    public List<OperationListEntry> subList(int fromIndex, int toIndex) {
206
        return this.operations.subList(fromIndex, toIndex);
207
    }
208

    
209
    @Override
210
    public Buffer execute(Buffer buffer) throws BufferOperationException {
211
        Buffer tmpBuffer = buffer;
212
        int i=0;
213
        for (Iterator<OperationListEntry> iterator = operations.iterator(); iterator.hasNext();) {
214
            OperationListEntry operationListEntry = (OperationListEntry) iterator.next();
215
            Operation operation = operationListEntry.getOperation();
216
            DynObject parameters = operationListEntry.getParameters();
217
            OperationFactory factory = operation.getFactory();
218
            if (operationListEntry.isActive()) {
219
                if (factory.isApplicable(tmpBuffer).equals(Applicable.NO)) {
220
                    throw new IllegalArgumentException("The operation is not applicable to the input buffer");
221
                }
222
                DynClass paramsDefinition = parameters.getDynClass();
223
                DynField[] fields = paramsDefinition.getDynFields();
224
                for (int j = 0; j < fields.length; j++) {
225
                    DynField_v2 field = (DynField_v2) fields[j];
226
                    String name = operationListEntry.getLookpupParameterName(field.getName());
227
                    if( !StringUtils.isEmpty(name) ) {
228
                        name = (String) field.getTags().get("lookupParameter");
229
                    }
230
                    if( !StringUtils.isEmpty(name) ) {
231
                        parameters.setDynValue(field.getName(), getParameterValue(name, i));
232
                    }
233
                }
234
                tmpBuffer = operation.execute(tmpBuffer, parameters);
235
            }
236
            i++;
237
        }
238
        return tmpBuffer;
239
    }
240

    
241
    @Override
242
    public void add(Operation operation, DynObject parameters) {
243
        OperationManager operationManager = ((OperationManager)BufferLocator.getBufferManager());
244
        OperationListEntry entry = operationManager.createOperationListEntry(operation, parameters);
245
        this.operations.add(entry);
246
    }
247

    
248
    @Override
249
    public void add(int index, Operation operation, DynObject parameters) {
250
        OperationManager operationManager = ((OperationManager)BufferLocator.getBufferManager());
251
        OperationListEntry entry = operationManager.createOperationListEntry(operation, parameters);
252
        this.operations.add(index, entry);
253
    }
254

    
255
    @Override
256
    public Object getParameterValue(String name) {
257
        return getParameterValue(name, operations.size());
258
    }
259

    
260
    private Object getParameterValue(String name, int index) {
261
        if(index<0){
262
            index = this.operations.size()+index;
263
        }
264
        ListIterator<OperationListEntry> iterator = operations.listIterator(index);
265

    
266
        Object result = null;
267
        while (iterator.hasPrevious()) {
268
            OperationListEntry operationListEntry = (OperationListEntry) iterator.previous();
269
            if (operationListEntry != null && operationListEntry.isActive()) {
270
                DynObject parameters = operationListEntry.getParameters();
271
                if (parameters.getDynClass().getDynField(name) != null) {
272
                    result = parameters.getDynValue(name);
273
                    break;
274
                }
275
            } else {
276
                LOG.warn("***** THE ENTRY IS NULL *****");
277
            }
278
        }
279
        if(result == null && defaultParameters!=null){
280
            if (defaultParameters.getDynClass().getDynField(name) != null) {
281
                result = defaultParameters.getDynValue(name);
282
            }
283
        }
284
        return result;
285
    }
286

    
287
    @Override
288
    public void saveToState(PersistentState state) throws PersistenceException {
289
        state.set(OPERATIONS_PERSISTENCE_FIELD, this.operations);
290

    
291
    }
292

    
293
    @Override
294
    public void loadFromState(PersistentState state) throws PersistenceException {
295
        this.operations = new ArrayList<OperationListEntry>();
296

    
297
        Iterator it = state.getIterator(OPERATIONS_PERSISTENCE_FIELD);
298
        while(it.hasNext()) {
299
            OperationListEntry operationListEntry = (OperationListEntry) it.next();
300
            this.add(operationListEntry);
301
        }
302
    }
303

    
304
    public static void registerPersistence() {
305
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
306
        DynStruct definition = manager.getDefinition(PERSISTENT_NAME);
307
        if (definition == null) {
308
            definition =
309
                manager.addDefinition(DefaultOperationList.class, PERSISTENT_NAME,
310
                    PERSISTENT_DESCRIPTION, null, null);
311
            definition.addDynFieldList(OPERATIONS_PERSISTENCE_FIELD).setMandatory(false).setClassOfItems(OperationListEntry.class);
312
        }
313
    }
314

    
315
    public List<String> getAvailableParameterNames() {
316
        Set<String> result = new HashSet<String>();
317
        for (Iterator<OperationListEntry> iterator = operations.iterator(); iterator.hasNext();) {
318
            OperationListEntry operationListEntry = (OperationListEntry) iterator.next();
319
            DynObject parameters = operationListEntry.getParameters();
320
            DynField[] dynFields = parameters.getDynClass().getDynFields();
321
            for (int i = 0; i < dynFields.length; i++) {
322
                result.add(dynFields[i].getName());
323
            }
324
        }
325
        if (this.defaultParameters != null) {
326
            DynField[] dynFields = this.defaultParameters.getDynClass().getDynFields();
327
            for (int i = 0; i < dynFields.length; i++) {
328
                result.add(dynFields[i].getName());
329
            }
330
        }
331
        return new ArrayList<String>(result);
332
    }
333

    
334
    @Override
335
    public void setDefaultParameters(DynObject parameters) {
336
        this.defaultParameters = parameters;
337
    }
338

    
339
    @Override
340
    public void validateLookupParameters() throws InvalidLookupParametersException {
341
        List<Problem> problems = new ArrayList<>();
342
        InvalidLookupParametersException exception = new InvalidLookupParametersException(problems);
343

    
344
        ListIterator<OperationListEntry> iterator = operations.listIterator();
345

    
346
        int i = operations.size();
347
        while (iterator.hasPrevious()) {
348
            OperationListEntry operationListEntry = (OperationListEntry) iterator.previous();
349
            Operation operation = operationListEntry.getOperation();
350
            DynObject parameters = operationListEntry.getParameters();
351
            OperationFactory factory = operation.getFactory();
352
            if (operationListEntry.isActive()) {
353
                DynClass paramsDefinition = parameters.getDynClass();
354
                DynField[] fields = paramsDefinition.getDynFields();
355
                for (int j = 0; j < fields.length; j++) {
356
                    DynField_v2 field = (DynField_v2) fields[j];
357
                    String name = operationListEntry.getLookpupParameterName(field.getName());
358
                    if( !StringUtils.isEmpty(name) ) {
359
                        name = (String) field.getTags().get("lookupParameter");
360
                    }
361
                    if( !StringUtils.isEmpty(name) ) {
362
                        if( getParameterValue(name, i) == null){
363
                            Problem problem = exception.new Problem(factory.getName(), field.getName(), name);
364
                            problems.add(problem);
365
                        }
366
                    }
367
                }
368
            }
369
            i--;
370
        }
371
        if(!problems.isEmpty()){
372
            throw new InvalidLookupParametersException(problems);
373
        }
374
        return;
375
    }
376

    
377
    @Override
378
    public Object clone() throws CloneNotSupportedException {
379
        OperationList cloned = new DefaultOperationList();
380

    
381
        if (operations != null) {
382
            ArrayList<OperationListEntry> cloneOperations = new ArrayList(operations.size());
383
            for (int i = 0; i < operations.size(); i++) {
384
                cloned.add((OperationListEntry) operations.get(i).clone());
385
            }
386
        }
387

    
388
        return cloned;
389
    }
390
}