svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.library / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.buffer.impl / src / main / java / org / gvsig / raster / lib / buffer / impl / operations / DefaultOperationList.java @ 43803
History | View | Annotate | Download (17.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.raster.lib.buffer.api.operations.OperationListNotification; |
49 |
import org.gvsig.tools.ToolsLocator; |
50 |
import org.gvsig.tools.dispose.DisposeUtils; |
51 |
import org.gvsig.tools.dynobject.DynClass; |
52 |
import org.gvsig.tools.dynobject.DynField; |
53 |
import org.gvsig.tools.dynobject.DynField_v2; |
54 |
import org.gvsig.tools.dynobject.DynObject; |
55 |
import org.gvsig.tools.dynobject.DynStruct; |
56 |
import org.gvsig.tools.observer.impl.BaseWeakReferencingObservable; |
57 |
import org.gvsig.tools.persistence.PersistenceManager; |
58 |
import org.gvsig.tools.persistence.PersistentState; |
59 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
60 |
|
61 |
|
62 |
/**
|
63 |
* @author fdiaz
|
64 |
*
|
65 |
*/
|
66 |
public class DefaultOperationList extends BaseWeakReferencingObservable implements OperationList { |
67 |
|
68 |
private static final Logger LOG = LoggerFactory.getLogger(DefaultOperationList.class); |
69 |
|
70 |
/**
|
71 |
* Persistence definition name
|
72 |
*/
|
73 |
public static final String PERSISTENT_NAME = "OperationListPersistence"; |
74 |
/**
|
75 |
* Description of persistence definition
|
76 |
*/
|
77 |
public static final String PERSISTENT_DESCRIPTION = "Persistence definition of operation list"; |
78 |
|
79 |
private final static String OPERATIONS_PERSISTENCE_FIELD = "operations"; |
80 |
|
81 |
|
82 |
private List<OperationListEntry> operations; |
83 |
|
84 |
private DynObject defaultParameters = null; |
85 |
|
86 |
/**
|
87 |
*
|
88 |
*/
|
89 |
public DefaultOperationList() {
|
90 |
this.operations = new ArrayList<OperationListEntry>(); |
91 |
} |
92 |
|
93 |
@Override
|
94 |
public int size() { |
95 |
return this.operations.size(); |
96 |
} |
97 |
|
98 |
@Override
|
99 |
public boolean isEmpty() { |
100 |
return this.operations.isEmpty(); |
101 |
} |
102 |
|
103 |
@Override
|
104 |
public boolean contains(Object o) { |
105 |
return this.operations.contains(o); |
106 |
} |
107 |
|
108 |
@Override
|
109 |
public Iterator<OperationListEntry> iterator() { |
110 |
return this.operations.iterator(); |
111 |
} |
112 |
|
113 |
@Override
|
114 |
public Object[] toArray() { |
115 |
return this.operations.toArray(); |
116 |
} |
117 |
|
118 |
@Override
|
119 |
public <T> T[] toArray(T[] a) { |
120 |
return this.operations.toArray(a); |
121 |
} |
122 |
|
123 |
@Override
|
124 |
public boolean add(OperationListEntry e) { |
125 |
ArrayList<OperationListEntry> oldOperations = this.cloneOperations(); |
126 |
boolean result = this.operations.add(e); |
127 |
this.notifyObservers(new DefaultOperationListNotification(OperationListNotification.ADDED_ENTRY, new Object[] { |
128 |
oldOperations, this.operations }));
|
129 |
|
130 |
return result;
|
131 |
} |
132 |
|
133 |
@Override
|
134 |
public boolean remove(Object o) { |
135 |
ArrayList<OperationListEntry> oldOperations = this.cloneOperations(); |
136 |
boolean result = this.operations.remove(o); |
137 |
this.notifyObservers(new DefaultOperationListNotification(OperationListNotification.REMOVED_ENTRY, new Object[] { |
138 |
oldOperations, this.operations }));
|
139 |
return result;
|
140 |
} |
141 |
|
142 |
@Override
|
143 |
public boolean containsAll(Collection<?> c) { |
144 |
return this.operations.containsAll(c); |
145 |
} |
146 |
|
147 |
@Override
|
148 |
public boolean addAll(Collection<? extends OperationListEntry> c) { |
149 |
ArrayList<OperationListEntry> oldOperations = this.cloneOperations(); |
150 |
boolean result = this.operations.addAll(c); |
151 |
this.notifyObservers(new DefaultOperationListNotification(OperationListNotification.ADDED_ENTRIES, new Object[] { |
152 |
oldOperations, this.operations }));
|
153 |
return result;
|
154 |
} |
155 |
|
156 |
@Override
|
157 |
public boolean addAll(int index, Collection<? extends OperationListEntry> c) { |
158 |
ArrayList<OperationListEntry> oldOperations = this.cloneOperations(); |
159 |
boolean result = this.operations.addAll(index, c); |
160 |
this.notifyObservers(new DefaultOperationListNotification(OperationListNotification.ADDED_ENTRIES, new Object[] { |
161 |
oldOperations, this.operations }));
|
162 |
return result;
|
163 |
} |
164 |
|
165 |
@Override
|
166 |
public boolean removeAll(Collection<?> c) { |
167 |
ArrayList<OperationListEntry> oldOperations = this.cloneOperations(); |
168 |
boolean result = this.operations.removeAll(c); |
169 |
this.notifyObservers(new DefaultOperationListNotification(OperationListNotification.REMOVED_ENTRIES, new Object[] { |
170 |
oldOperations, this.operations }));
|
171 |
return result;
|
172 |
} |
173 |
|
174 |
@Override
|
175 |
public boolean retainAll(Collection<?> c) { |
176 |
ArrayList<OperationListEntry> oldOperations = this.cloneOperations(); |
177 |
boolean result = this.operations.retainAll(c); |
178 |
this.notifyObservers(new DefaultOperationListNotification(OperationListNotification.RETAINED_ENTRIES, new Object[] { |
179 |
oldOperations, this.operations }));
|
180 |
return result;
|
181 |
} |
182 |
|
183 |
@Override
|
184 |
public void clear() { |
185 |
ArrayList<OperationListEntry> oldOperations = this.cloneOperations(); |
186 |
this.operations.clear();
|
187 |
this.notifyObservers(new DefaultOperationListNotification(OperationListNotification.CLEAR_ENTRIES, new Object[] { |
188 |
oldOperations, this.operations }));
|
189 |
} |
190 |
|
191 |
@Override
|
192 |
public OperationListEntry get(int index) { |
193 |
return this.operations.get(index); |
194 |
} |
195 |
|
196 |
@Override
|
197 |
public OperationListEntry set(int index, OperationListEntry element) { |
198 |
ArrayList<OperationListEntry> oldOperations = this.cloneOperations(); |
199 |
OperationListEntry result = this.operations.set(index, element);
|
200 |
this.notifyObservers(new DefaultOperationListNotification(OperationListNotification.SETTED_ENTRY, new Object[] { |
201 |
oldOperations, this.operations }));
|
202 |
return result;
|
203 |
} |
204 |
|
205 |
@Override
|
206 |
public void add(int index, OperationListEntry element) { |
207 |
ArrayList<OperationListEntry> oldOperations = this.cloneOperations(); |
208 |
if(index<0){ |
209 |
index = this.operations.size()+index;
|
210 |
} |
211 |
this.operations.add(index, element);
|
212 |
this.notifyObservers(new DefaultOperationListNotification(OperationListNotification.ADDED_ENTRY, new Object[] { |
213 |
oldOperations, this.operations }));
|
214 |
} |
215 |
|
216 |
@Override
|
217 |
public OperationListEntry remove(int index) { |
218 |
ArrayList<OperationListEntry> oldOperations = this.cloneOperations(); |
219 |
OperationListEntry result = this.operations.remove(index);
|
220 |
this.notifyObservers(new DefaultOperationListNotification(OperationListNotification.REMOVED_ENTRY, new Object[] { |
221 |
oldOperations, this.operations }));
|
222 |
return result;
|
223 |
} |
224 |
|
225 |
@Override
|
226 |
public int indexOf(Object o) { |
227 |
return this.operations.indexOf(o); |
228 |
} |
229 |
|
230 |
@Override
|
231 |
public int lastIndexOf(Object o) { |
232 |
return this.operations.lastIndexOf(o); |
233 |
} |
234 |
|
235 |
@Override
|
236 |
public ListIterator<OperationListEntry> listIterator() { |
237 |
return this.operations.listIterator(); |
238 |
} |
239 |
|
240 |
@Override
|
241 |
public ListIterator<OperationListEntry> listIterator(int index) { |
242 |
return this.operations.listIterator(index); |
243 |
} |
244 |
|
245 |
@Override
|
246 |
public List<OperationListEntry> subList(int fromIndex, int toIndex) { |
247 |
return this.operations.subList(fromIndex, toIndex); |
248 |
} |
249 |
|
250 |
@Override
|
251 |
public Buffer execute(Buffer buffer) throws BufferOperationException { |
252 |
Buffer tmpBuffer1 = buffer;
|
253 |
DisposeUtils.bind(tmpBuffer1); |
254 |
Buffer tmpBuffer2 = null; |
255 |
int i=0; |
256 |
for (Iterator<OperationListEntry> iterator = operations.iterator(); iterator.hasNext();) { |
257 |
OperationListEntry operationListEntry = (OperationListEntry) iterator.next(); |
258 |
Operation operation = operationListEntry.getOperation();
|
259 |
DynObject parameters = operationListEntry.getParameters(); |
260 |
OperationFactory factory = operation.getFactory(); |
261 |
if (operationListEntry.isActive()) {
|
262 |
if (factory.isApplicable(tmpBuffer1).equals(Applicable.NO)) {
|
263 |
throw new IllegalArgumentException("The operation is not applicable to the input buffer"); |
264 |
} |
265 |
DynClass paramsDefinition = parameters.getDynClass(); |
266 |
DynField[] fields = paramsDefinition.getDynFields();
|
267 |
for (int j = 0; j < fields.length; j++) { |
268 |
DynField_v2 field = (DynField_v2) fields[j]; |
269 |
String name = operationListEntry.getLookpupParameterName(field.getName());
|
270 |
if (StringUtils.isEmpty(name)) {
|
271 |
name = (String) field.getTags().get("lookupParameter"); |
272 |
} |
273 |
if (!StringUtils.isEmpty(name)) {
|
274 |
parameters.setDynValue(field.getName(), getParameterValue(name, i)); |
275 |
} |
276 |
} |
277 |
tmpBuffer2 = operation.execute(tmpBuffer1, parameters); |
278 |
DisposeUtils.dispose(tmpBuffer1); |
279 |
tmpBuffer1 = tmpBuffer2; |
280 |
} |
281 |
i++; |
282 |
} |
283 |
return tmpBuffer1;
|
284 |
} |
285 |
|
286 |
@Override
|
287 |
public void add(Operation operation, DynObject parameters) { |
288 |
ArrayList<OperationListEntry> oldOperations = this.cloneOperations(); |
289 |
OperationManager operationManager = ((OperationManager)BufferLocator.getBufferManager()); |
290 |
OperationListEntry entry = operationManager.createOperationListEntry(operation, parameters); |
291 |
this.operations.add(entry);
|
292 |
this.notifyObservers(new DefaultOperationListNotification(OperationListNotification.ADDED_ENTRY, new Object[] { |
293 |
oldOperations, this.operations }));
|
294 |
} |
295 |
|
296 |
@Override
|
297 |
public void add(int index, Operation operation, DynObject parameters) { |
298 |
ArrayList<OperationListEntry> oldOperations = this.cloneOperations(); |
299 |
OperationManager operationManager = ((OperationManager)BufferLocator.getBufferManager()); |
300 |
OperationListEntry entry = operationManager.createOperationListEntry(operation, parameters); |
301 |
this.operations.add(index, entry);
|
302 |
this.notifyObservers(new DefaultOperationListNotification(OperationListNotification.ADDED_ENTRY, new Object[] { |
303 |
oldOperations, this.operations }));
|
304 |
} |
305 |
|
306 |
@Override
|
307 |
public Object getParameterValue(String name) { |
308 |
int size = operations.size();
|
309 |
return getParameterValue(name, size);
|
310 |
} |
311 |
|
312 |
private Object getParameterValue(String name, int index) { |
313 |
if(index<0){ |
314 |
index = this.operations.size()+index;
|
315 |
} |
316 |
ListIterator<OperationListEntry> iterator = operations.listIterator(index);
|
317 |
|
318 |
Object result = null; |
319 |
while (iterator.hasPrevious()) {
|
320 |
OperationListEntry operationListEntry = (OperationListEntry) iterator.previous(); |
321 |
if (operationListEntry != null && operationListEntry.isActive()) { |
322 |
DynObject parameters = operationListEntry.getParameters(); |
323 |
if (parameters.getDynClass().getDynField(name) != null) { |
324 |
result = parameters.getDynValue(name); |
325 |
break;
|
326 |
} |
327 |
} else {
|
328 |
LOG.warn("***** THE ENTRY IS NULL *****");
|
329 |
} |
330 |
} |
331 |
if(result == null && defaultParameters!=null){ |
332 |
if (defaultParameters.getDynClass().getDynField(name) != null) { |
333 |
result = defaultParameters.getDynValue(name); |
334 |
} |
335 |
} |
336 |
return result;
|
337 |
} |
338 |
|
339 |
@Override
|
340 |
public void saveToState(PersistentState state) throws PersistenceException { |
341 |
state.set(OPERATIONS_PERSISTENCE_FIELD, this.operations);
|
342 |
|
343 |
} |
344 |
|
345 |
@Override
|
346 |
public void loadFromState(PersistentState state) throws PersistenceException { |
347 |
this.operations = new ArrayList<OperationListEntry>(); |
348 |
|
349 |
Iterator it = state.getIterator(OPERATIONS_PERSISTENCE_FIELD);
|
350 |
while(it.hasNext()) {
|
351 |
OperationListEntry operationListEntry = (OperationListEntry) it.next(); |
352 |
this.add(operationListEntry);
|
353 |
} |
354 |
} |
355 |
|
356 |
public static void registerPersistence() { |
357 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
358 |
DynStruct definition = manager.getDefinition(PERSISTENT_NAME); |
359 |
if (definition == null) { |
360 |
definition = |
361 |
manager.addDefinition(DefaultOperationList.class, PERSISTENT_NAME, |
362 |
PERSISTENT_DESCRIPTION, null, null); |
363 |
definition.addDynFieldList(OPERATIONS_PERSISTENCE_FIELD).setMandatory(false).setClassOfItems(OperationListEntry.class);
|
364 |
} |
365 |
} |
366 |
|
367 |
public List<String> getAvailableParameterNames() { |
368 |
Set<String> result = new HashSet<String>(); |
369 |
for (Iterator<OperationListEntry> iterator = operations.iterator(); iterator.hasNext();) { |
370 |
OperationListEntry operationListEntry = (OperationListEntry) iterator.next(); |
371 |
DynObject parameters = operationListEntry.getParameters(); |
372 |
DynField[] dynFields = parameters.getDynClass().getDynFields();
|
373 |
for (int i = 0; i < dynFields.length; i++) { |
374 |
result.add(dynFields[i].getName()); |
375 |
} |
376 |
} |
377 |
if (this.defaultParameters != null) { |
378 |
DynField[] dynFields = this.defaultParameters.getDynClass().getDynFields(); |
379 |
for (int i = 0; i < dynFields.length; i++) { |
380 |
result.add(dynFields[i].getName()); |
381 |
} |
382 |
} |
383 |
return new ArrayList<String>(result); |
384 |
} |
385 |
|
386 |
@Override
|
387 |
public void setDefaultParameters(DynObject parameters) { |
388 |
this.defaultParameters = parameters;
|
389 |
} |
390 |
|
391 |
@Override
|
392 |
public void validateLookupParameters() throws InvalidLookupParametersException { |
393 |
List<Problem> problems = new ArrayList<>(); |
394 |
InvalidLookupParametersException exception = new InvalidLookupParametersException(problems);
|
395 |
|
396 |
ListIterator<OperationListEntry> iterator = operations.listIterator();
|
397 |
|
398 |
int i = operations.size();
|
399 |
while (iterator.hasPrevious()) {
|
400 |
OperationListEntry operationListEntry = (OperationListEntry) iterator.previous(); |
401 |
Operation operation = operationListEntry.getOperation();
|
402 |
DynObject parameters = operationListEntry.getParameters(); |
403 |
OperationFactory factory = operation.getFactory(); |
404 |
if (operationListEntry.isActive()) {
|
405 |
DynClass paramsDefinition = parameters.getDynClass(); |
406 |
DynField[] fields = paramsDefinition.getDynFields();
|
407 |
for (int j = 0; j < fields.length; j++) { |
408 |
DynField_v2 field = (DynField_v2) fields[j]; |
409 |
String name = operationListEntry.getLookpupParameterName(field.getName());
|
410 |
if( !StringUtils.isEmpty(name) ) {
|
411 |
name = (String) field.getTags().get("lookupParameter"); |
412 |
} |
413 |
if( !StringUtils.isEmpty(name) ) {
|
414 |
if( getParameterValue(name, i) == null){ |
415 |
Problem problem = exception.new Problem(factory.getName(), field.getName(), name); |
416 |
problems.add(problem); |
417 |
} |
418 |
} |
419 |
} |
420 |
} |
421 |
i--; |
422 |
} |
423 |
if(!problems.isEmpty()){
|
424 |
throw new InvalidLookupParametersException(problems); |
425 |
} |
426 |
return;
|
427 |
} |
428 |
|
429 |
private ArrayList<OperationListEntry> cloneOperations() { |
430 |
ArrayList<OperationListEntry> clonedOperations = null; |
431 |
if (operations != null) { |
432 |
try {
|
433 |
clonedOperations = new ArrayList(operations.size()); |
434 |
for (int i = 0; i < operations.size(); i++) { |
435 |
clonedOperations.add((OperationListEntry) operations.get(i).clone()); |
436 |
} |
437 |
} catch (CloneNotSupportedException e) { |
438 |
LOG.warn("Can't clone operation list", e);
|
439 |
} |
440 |
} |
441 |
return clonedOperations;
|
442 |
} |
443 |
|
444 |
@Override
|
445 |
public Object clone() throws CloneNotSupportedException { |
446 |
OperationList cloned = new DefaultOperationList();
|
447 |
|
448 |
if (operations != null) { |
449 |
for (int i = 0; i < operations.size(); i++) { |
450 |
cloned.add((OperationListEntry) operations.get(i).clone()); |
451 |
} |
452 |
} |
453 |
|
454 |
return cloned;
|
455 |
} |
456 |
} |