001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * Created on 16-Sep-2004
017: */
018: package org.geotools.data.wfs;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.NoSuchElementException;
023: import java.util.logging.Logger;
024:
025: import org.geotools.feature.Feature;
026: import org.geotools.feature.IllegalAttributeException;
027: import org.opengis.filter.Filter;
028: import org.geotools.filter.FilterFactoryFinder;
029: import org.geotools.filter.Filters;
030: import org.geotools.filter.visitor.DuplicatorFilterVisitor;
031:
032: /**
033: * This interface represents pending actions within a transaction.
034: *
035: * @author dzwiers
036: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/wfs/src/main/java/org/geotools/data/wfs/Action.java $
037: */
038: public interface Action {
039: /**
040: * Action mask for an Insert Action
041: */
042: public static final int INSERT = 1;
043: /**
044: * Action mask for an Update Action
045: */
046: public static final int UPDATE = 2;
047: /**
048: * Action mask for a Delete Action
049: */
050: public static final int DELETE = 4;
051:
052: /**
053: * @return The Type of Action ... one of the three Constants
054: */
055: public int getType();
056:
057: /**
058: * @return The Filter on which to inflict the Action
059: */
060: public Filter getFilter();
061:
062: /**
063: * @return The FeatureType name for which this Action is intended
064: */
065: public String getTypeName();
066:
067: /**
068: * Represents an Update Action
069: *
070: * @author dzwiers
071: */
072: public static class UpdateAction implements Action {
073: private final Filter filter;
074: private final Map properties;
075: private final String typeName;
076:
077: /**
078: * Makes an UpdateAction Filter is copied so any further changes will not be included in filter of action.
079: *
080: * @param typeName The TypeName
081: * @param f Filter which this update affects
082: * @param properties The properties to update. Entries must be <String, Object> where String is an attribute to
083: * update and Object is the new Value.
084: */
085: public UpdateAction(String typeName, Filter f, Map properties) {
086: DuplicatorFilterVisitor visitor = new DuplicatorFilterVisitor(
087: FilterFactoryFinder.createFilterFactory(), false);
088: Filters.accept(f, visitor);
089: filter = (Filter) visitor.getCopy();
090: this .properties = new HashMap(properties);
091: this .typeName = typeName;
092: }
093:
094: /**
095: * @return @see Action#UPDATE
096: */
097: public int getType() {
098: return UPDATE;
099: }
100:
101: /**
102: * Returns the property if found ... this method will not create a
103: * NullPointerException if properties is null.
104: *
105: * @param name String the property key
106: * @return Object The property if found, null other wise.
107: */
108: public Object getProperty(String name) {
109: return (properties == null) ? null : properties.get(name);
110: }
111:
112: /**
113: * Returns the property names if they exist ... this method will not create a
114: * NullPointerException if properties is null.
115: *
116: * @return A list of the keys.
117: */
118: public String[] getPropertyNames() {
119: return properties == null ? new String[0]
120: : (String[]) properties.keySet().toArray(
121: new String[properties.keySet().size()]);
122: }
123:
124: /**
125: * @return a clone of the properties map, null if it does not exist.
126: */
127: public Map getProperties() {
128: return properties == null ? null : new HashMap(properties);
129: }
130:
131: /**
132: * @return Filter the Filter
133: */
134: public Filter getFilter() {
135: return filter;
136: }
137:
138: /**
139: * @return String the TypeName
140: */
141: public String getTypeName() {
142: return typeName;
143: }
144:
145: public void update(Feature feature) {
146: if (!filter.evaluate(feature))
147: throw new IllegalArgumentException(
148: feature
149: + "is not affected by this update, only call update on features that"
150: + "the Action applies to!");
151: String[] propNames = getPropertyNames();
152:
153: for (int j = 0; j < propNames.length; j++) {
154: try {
155: feature.setAttribute(propNames[j],
156: getProperty(propNames[j]));
157: } catch (IllegalAttributeException e) {
158: NoSuchElementException ee = new NoSuchElementException(
159: e.getMessage());
160: ee.initCause(e);
161: throw ee;
162: }
163: }
164: }
165:
166: public String toString() {
167: return "UPDATE " + filter + " " + properties;
168: }
169: }
170:
171: /**
172: * Represents a Delete Action for a Transaction
173: *
174: * @author dzwiers
175: */
176: public static class DeleteAction implements Action {
177: private final Filter filter;
178: private final String typeName;
179:
180: /**
181: * Represents a Delete Action. Filter is copied so any further changes will not be included in filter of action.
182: *
183: * @param typeName TypeName
184: * @param f Filter of Features to Delete
185: */
186: public DeleteAction(String typeName, Filter f) {
187: DuplicatorFilterVisitor visitor = new DuplicatorFilterVisitor(
188: FilterFactoryFinder.createFilterFactory(), false);
189: Filters.accept(f, visitor);
190: filter = (Filter) visitor.getCopy();
191: this .typeName = typeName;
192: }
193:
194: /**
195: * @return @see Action#DELETE
196: */
197: public int getType() {
198: return DELETE;
199: }
200:
201: /**
202: * @return the TypeName
203: */
204: public String getTypeName() {
205: return typeName;
206: }
207:
208: /**
209: * @return the Filter
210: */
211: public Filter getFilter() {
212: return filter;
213: }
214:
215: public String toString() {
216: return "REMOVE " + filter;
217: }
218:
219: }
220:
221: /**
222: * Represents an Insert Action
223: *
224: * @author dzwiers
225: */
226: public static class InsertAction implements Action {
227: private final Feature feature;
228:
229: /**
230: * Creates an insert action for the Feature specified. The feature is copied to
231: * prevent inadvertant side effect of modifing the feature after an insert.
232: *
233: * @param f Feature to add
234: */
235: public InsertAction(Feature f) {
236: Feature feature;
237: try {
238: feature = f.getFeatureType().duplicate(f);
239: } catch (IllegalAttributeException e) {
240: org.geotools.util.logging.Logging.getLogger(
241: "org.geotools.data.wfs").warning(
242: "Failed to duplicate feature:" + f);
243: feature = f;
244: }
245: this .feature = feature;
246: }
247:
248: /**
249: * @return @see Action#INSERT
250: */
251: public int getType() {
252: return INSERT;
253: }
254:
255: /**
256: * @return The Feature to add
257: */
258: public Feature getFeature() {
259: return feature;
260: }
261:
262: /**
263: * @see org.geotools.data.wfs.Action#getTypeName()
264: */
265: public String getTypeName() {
266: return (feature == null) ? null : feature.getFeatureType()
267: .getTypeName();
268: }
269:
270: /**
271: * @see org.geotools.data.wfs.Action#getFilter()
272: */
273: public Filter getFilter() {
274: return (feature.getID() == null) ? null
275: : (FilterFactoryFinder.createFilterFactory()
276: .createFidFilter(feature.getID()));
277: }
278:
279: public String toString() {
280: return "INSERT " + feature;
281: }
282: }
283: }
|