Revision 8682 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
DefaultOperationList.java | ||
---|---|---|
24 | 24 |
|
25 | 25 |
import java.util.ArrayList; |
26 | 26 |
import java.util.Collection; |
27 |
import java.util.HashSet; |
|
27 | 28 |
import java.util.Iterator; |
28 | 29 |
import java.util.List; |
29 | 30 |
import java.util.ListIterator; |
31 |
import java.util.Set; |
|
30 | 32 |
|
33 |
import org.apache.commons.lang3.StringUtils; |
|
34 |
import org.slf4j.Logger; |
|
35 |
import org.slf4j.LoggerFactory; |
|
36 |
|
|
31 | 37 |
import org.gvsig.raster.lib.buffer.api.Buffer; |
32 | 38 |
import org.gvsig.raster.lib.buffer.api.BufferLocator; |
39 |
import org.gvsig.raster.lib.buffer.api.OperationManager; |
|
33 | 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; |
|
34 | 43 |
import org.gvsig.raster.lib.buffer.api.operations.Operation; |
35 | 44 |
import org.gvsig.raster.lib.buffer.api.operations.OperationFactory; |
36 | 45 |
import org.gvsig.raster.lib.buffer.api.operations.OperationFactory.Applicable; |
37 | 46 |
import org.gvsig.raster.lib.buffer.api.operations.OperationList; |
38 | 47 |
import org.gvsig.raster.lib.buffer.api.operations.OperationListEntry; |
39 |
import org.gvsig.raster.lib.buffer.spi.OperationListManager; |
|
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; |
|
40 | 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; |
|
41 | 58 |
|
42 | 59 |
|
43 | 60 |
/** |
... | ... | |
46 | 63 |
*/ |
47 | 64 |
public class DefaultOperationList implements OperationList { |
48 | 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 |
|
|
49 | 80 |
private List<OperationListEntry> operations; |
50 | 81 |
|
82 |
private DynObject defaultParameters = null; |
|
83 |
|
|
51 | 84 |
/** |
52 | 85 |
* |
53 | 86 |
*/ |
... | ... | |
55 | 88 |
this.operations = new ArrayList<OperationListEntry>(); |
56 | 89 |
} |
57 | 90 |
|
58 |
/* (non-Javadoc) |
|
59 |
* @see java.util.List#size() |
|
60 |
*/ |
|
61 | 91 |
@Override |
62 | 92 |
public int size() { |
63 | 93 |
return this.operations.size(); |
64 | 94 |
} |
65 | 95 |
|
66 |
/* (non-Javadoc) |
|
67 |
* @see java.util.List#isEmpty() |
|
68 |
*/ |
|
69 | 96 |
@Override |
70 | 97 |
public boolean isEmpty() { |
71 | 98 |
return this.operations.isEmpty(); |
72 | 99 |
} |
73 | 100 |
|
74 |
/* (non-Javadoc) |
|
75 |
* @see java.util.List#contains(java.lang.Object) |
|
76 |
*/ |
|
77 | 101 |
@Override |
78 | 102 |
public boolean contains(Object o) { |
79 | 103 |
return this.operations.contains(o); |
80 | 104 |
} |
81 | 105 |
|
82 |
/* (non-Javadoc) |
|
83 |
* @see java.util.List#iterator() |
|
84 |
*/ |
|
85 | 106 |
@Override |
86 | 107 |
public Iterator<OperationListEntry> iterator() { |
87 | 108 |
return this.operations.iterator(); |
88 | 109 |
} |
89 | 110 |
|
90 |
/* (non-Javadoc) |
|
91 |
* @see java.util.List#toArray() |
|
92 |
*/ |
|
93 | 111 |
@Override |
94 | 112 |
public Object[] toArray() { |
95 | 113 |
return this.operations.toArray(); |
96 | 114 |
} |
97 | 115 |
|
98 |
/* (non-Javadoc) |
|
99 |
* @see java.util.List#toArray(java.lang.Object[]) |
|
100 |
*/ |
|
101 | 116 |
@Override |
102 | 117 |
public <T> T[] toArray(T[] a) { |
103 | 118 |
return this.operations.toArray(a); |
104 | 119 |
} |
105 | 120 |
|
106 |
/* (non-Javadoc) |
|
107 |
* @see java.util.List#add(java.lang.Object) |
|
108 |
*/ |
|
109 | 121 |
@Override |
110 | 122 |
public boolean add(OperationListEntry e) { |
111 | 123 |
return this.operations.add(e); |
112 | 124 |
} |
113 | 125 |
|
114 |
/* (non-Javadoc) |
|
115 |
* @see java.util.List#remove(java.lang.Object) |
|
116 |
*/ |
|
117 | 126 |
@Override |
118 | 127 |
public boolean remove(Object o) { |
119 | 128 |
return this.operations.remove(o); |
120 | 129 |
} |
121 | 130 |
|
122 |
/* (non-Javadoc) |
|
123 |
* @see java.util.List#containsAll(java.util.Collection) |
|
124 |
*/ |
|
125 | 131 |
@Override |
126 | 132 |
public boolean containsAll(Collection<?> c) { |
127 | 133 |
return this.operations.containsAll(c); |
128 | 134 |
} |
129 | 135 |
|
130 |
/* (non-Javadoc) |
|
131 |
* @see java.util.List#addAll(java.util.Collection) |
|
132 |
*/ |
|
133 | 136 |
@Override |
134 | 137 |
public boolean addAll(Collection<? extends OperationListEntry> c) { |
135 | 138 |
return this.operations.addAll(c); |
136 | 139 |
} |
137 | 140 |
|
138 |
/* (non-Javadoc) |
|
139 |
* @see java.util.List#addAll(int, java.util.Collection) |
|
140 |
*/ |
|
141 | 141 |
@Override |
142 | 142 |
public boolean addAll(int index, Collection<? extends OperationListEntry> c) { |
143 | 143 |
return this.operations.addAll(index, c); |
144 | 144 |
} |
145 | 145 |
|
146 |
/* (non-Javadoc) |
|
147 |
* @see java.util.List#removeAll(java.util.Collection) |
|
148 |
*/ |
|
149 | 146 |
@Override |
150 | 147 |
public boolean removeAll(Collection<?> c) { |
151 | 148 |
return this.operations.removeAll(c); |
152 | 149 |
} |
153 | 150 |
|
154 |
/* (non-Javadoc) |
|
155 |
* @see java.util.List#retainAll(java.util.Collection) |
|
156 |
*/ |
|
157 | 151 |
@Override |
158 | 152 |
public boolean retainAll(Collection<?> c) { |
159 | 153 |
return this.operations.retainAll(c); |
160 | 154 |
} |
161 | 155 |
|
162 |
/* (non-Javadoc) |
|
163 |
* @see java.util.List#clear() |
|
164 |
*/ |
|
165 | 156 |
@Override |
166 | 157 |
public void clear() { |
167 | 158 |
this.operations.clear(); |
168 | 159 |
} |
169 | 160 |
|
170 |
/* (non-Javadoc) |
|
171 |
* @see java.util.List#get(int) |
|
172 |
*/ |
|
173 | 161 |
@Override |
174 | 162 |
public OperationListEntry get(int index) { |
175 | 163 |
return this.operations.get(index); |
176 | 164 |
} |
177 | 165 |
|
178 |
/* (non-Javadoc) |
|
179 |
* @see java.util.List#set(int, java.lang.Object) |
|
180 |
*/ |
|
181 | 166 |
@Override |
182 | 167 |
public OperationListEntry set(int index, OperationListEntry element) { |
183 | 168 |
return this.operations.set(index, element); |
184 | 169 |
} |
185 | 170 |
|
186 |
/* (non-Javadoc) |
|
187 |
* @see java.util.List#add(int, java.lang.Object) |
|
188 |
*/ |
|
189 | 171 |
@Override |
190 | 172 |
public void add(int index, OperationListEntry element) { |
173 |
if(index<0){ |
|
174 |
index = this.operations.size()+index; |
|
175 |
} |
|
191 | 176 |
this.operations.add(index, element); |
192 | 177 |
} |
193 | 178 |
|
194 |
/* (non-Javadoc) |
|
195 |
* @see java.util.List#remove(int) |
|
196 |
*/ |
|
197 | 179 |
@Override |
198 | 180 |
public OperationListEntry remove(int index) { |
199 | 181 |
return this.operations.remove(index); |
200 | 182 |
} |
201 | 183 |
|
202 |
/* (non-Javadoc) |
|
203 |
* @see java.util.List#indexOf(java.lang.Object) |
|
204 |
*/ |
|
205 | 184 |
@Override |
206 | 185 |
public int indexOf(Object o) { |
207 | 186 |
return this.operations.indexOf(o); |
208 | 187 |
} |
209 | 188 |
|
210 |
/* (non-Javadoc) |
|
211 |
* @see java.util.List#lastIndexOf(java.lang.Object) |
|
212 |
*/ |
|
213 | 189 |
@Override |
214 | 190 |
public int lastIndexOf(Object o) { |
215 | 191 |
return this.operations.lastIndexOf(o); |
216 | 192 |
} |
217 | 193 |
|
218 |
/* (non-Javadoc) |
|
219 |
* @see java.util.List#listIterator() |
|
220 |
*/ |
|
221 | 194 |
@Override |
222 | 195 |
public ListIterator<OperationListEntry> listIterator() { |
223 | 196 |
return this.operations.listIterator(); |
224 | 197 |
} |
225 | 198 |
|
226 |
/* (non-Javadoc) |
|
227 |
* @see java.util.List#listIterator(int) |
|
228 |
*/ |
|
229 | 199 |
@Override |
230 | 200 |
public ListIterator<OperationListEntry> listIterator(int index) { |
231 | 201 |
return this.operations.listIterator(index); |
232 | 202 |
} |
233 | 203 |
|
234 |
/* (non-Javadoc) |
|
235 |
* @see java.util.List#subList(int, int) |
|
236 |
*/ |
|
237 | 204 |
@Override |
238 | 205 |
public List<OperationListEntry> subList(int fromIndex, int toIndex) { |
239 | 206 |
return this.operations.subList(fromIndex, toIndex); |
240 | 207 |
} |
241 | 208 |
|
242 |
/* (non-Javadoc) |
|
243 |
* @see org.gvsig.raster.lib.buffer.api.operations.OperationList#execute(org.gvsig.raster.lib.buffer.api.Buffer) |
|
244 |
*/ |
|
245 | 209 |
@Override |
246 | 210 |
public Buffer execute(Buffer buffer) throws BufferOperationException { |
247 | 211 |
Buffer tmpBuffer = buffer; |
212 |
int i=0; |
|
248 | 213 |
for (Iterator<OperationListEntry> iterator = operations.iterator(); iterator.hasNext();) { |
249 | 214 |
OperationListEntry operationListEntry = (OperationListEntry) iterator.next(); |
250 | 215 |
Operation operation = operationListEntry.getOperation(); |
... | ... | |
254 | 219 |
if (factory.isApplicable(tmpBuffer).equals(Applicable.NO)) { |
255 | 220 |
throw new IllegalArgumentException("The operation is not applicable to the input buffer"); |
256 | 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 |
} |
|
257 | 234 |
tmpBuffer = operation.execute(tmpBuffer, parameters); |
258 | 235 |
} |
236 |
i++; |
|
259 | 237 |
} |
260 | 238 |
return tmpBuffer; |
261 | 239 |
} |
262 | 240 |
|
263 |
/* (non-Javadoc) |
|
264 |
* @see org.gvsig.raster.lib.buffer.api.operations.OperationList#add(org.gvsig.raster.lib.buffer.api.operations.Operation, org.gvsig.tools.dynobject.DynObject) |
|
265 |
*/ |
|
266 | 241 |
@Override |
267 | 242 |
public void add(Operation operation, DynObject parameters) { |
268 |
OperationListManager operationListManager = ((OperationListManager)BufferLocator.getBufferManager());
|
|
269 |
OperationListEntry entry = operationListManager.createOperationListEntry(operation, parameters);
|
|
243 |
OperationManager operationManager = ((OperationManager)BufferLocator.getBufferManager());
|
|
244 |
OperationListEntry entry = operationManager.createOperationListEntry(operation, parameters); |
|
270 | 245 |
this.operations.add(entry); |
271 | 246 |
} |
272 | 247 |
|
273 | 248 |
@Override |
274 | 249 |
public void add(int index, Operation operation, DynObject parameters) { |
275 |
OperationListManager operationListManager = ((OperationListManager)BufferLocator.getBufferManager());
|
|
276 |
OperationListEntry entry = operationListManager.createOperationListEntry(operation, parameters);
|
|
250 |
OperationManager operationManager = ((OperationManager)BufferLocator.getBufferManager());
|
|
251 |
OperationListEntry entry = operationManager.createOperationListEntry(operation, parameters); |
|
277 | 252 |
this.operations.add(index, entry); |
278 | 253 |
} |
279 | 254 |
|
280 | 255 |
@Override |
281 |
public Object getThrownDynValue(String name) { |
|
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 |
|
|
282 | 266 |
Object result = null; |
283 |
for (Iterator iterator = operations.iterator(); iterator.hasNext();) { |
|
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();) { |
|
284 | 318 |
OperationListEntry operationListEntry = (OperationListEntry) iterator.next(); |
285 | 319 |
DynObject parameters = operationListEntry.getParameters(); |
286 |
if (parameters.getDynClass().getDynField(name) != null) { |
|
287 |
result = parameters.getDynValue(name); |
|
320 |
DynField[] dynFields = parameters.getDynClass().getDynFields(); |
|
321 |
for (int i = 0; i < dynFields.length; i++) { |
|
322 |
result.add(dynFields[i].getName()); |
|
288 | 323 |
} |
289 | 324 |
} |
290 |
return result; |
|
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 |
} |
|
291 | 333 |
|
334 |
@Override |
|
335 |
public void setDefaultParameters(DynObject parameters) { |
|
336 |
this.defaultParameters = parameters; |
|
292 | 337 |
} |
293 | 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 |
} |
|
294 | 390 |
} |
Also available in: Unified diff