001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/ogcwebservices/wfs/LockFeatureHandler.java $
002 /*----------------------------------------------------------------------------
003 This file is part of deegree, http://deegree.org/
004 Copyright (C) 2001-2009 by:
005 Department of Geography, University of Bonn
006 and
007 lat/lon GmbH
008
009 This library is free software; you can redistribute it and/or modify it under
010 the terms of the GNU Lesser General Public License as published by the Free
011 Software Foundation; either version 2.1 of the License, or (at your option)
012 any later version.
013 This library is distributed in the hope that it will be useful, but WITHOUT
014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016 details.
017 You should have received a copy of the GNU Lesser General Public License
018 along with this library; if not, write to the Free Software Foundation, Inc.,
019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020
021 Contact information:
022
023 lat/lon GmbH
024 Aennchenstr. 19, 53177 Bonn
025 Germany
026 http://lat-lon.de/
027
028 Department of Geography, University of Bonn
029 Prof. Dr. Klaus Greve
030 Postfach 1147, 53001 Bonn
031 Germany
032 http://www.geographie.uni-bonn.de/deegree/
033
034 e-mail: info@deegree.org
035 ----------------------------------------------------------------------------*/
036 package org.deegree.ogcwebservices.wfs;
037
038 import java.util.ArrayList;
039 import java.util.HashMap;
040 import java.util.List;
041 import java.util.Map;
042 import java.util.Set;
043
044 import org.deegree.datatypes.QualifiedName;
045 import org.deegree.framework.log.ILogger;
046 import org.deegree.framework.log.LoggerFactory;
047 import org.deegree.i18n.Messages;
048 import org.deegree.io.datastore.Datastore;
049 import org.deegree.io.datastore.DatastoreException;
050 import org.deegree.io.datastore.FeatureId;
051 import org.deegree.io.datastore.LockManager;
052 import org.deegree.io.datastore.schema.MappedFeatureType;
053 import org.deegree.model.feature.Feature;
054 import org.deegree.model.feature.FeatureCollection;
055 import org.deegree.model.feature.schema.FeatureType;
056 import org.deegree.ogcwebservices.OGCWebServiceException;
057 import org.deegree.ogcwebservices.wfs.operation.FeatureResult;
058 import org.deegree.ogcwebservices.wfs.operation.GetFeature;
059 import org.deegree.ogcwebservices.wfs.operation.GetFeatureWithLock;
060 import org.deegree.ogcwebservices.wfs.operation.Lock;
061 import org.deegree.ogcwebservices.wfs.operation.LockFeature;
062 import org.deegree.ogcwebservices.wfs.operation.LockFeatureResponse;
063 import org.deegree.ogcwebservices.wfs.operation.Query;
064
065 /**
066 * Handles {@link LockFeature} requests to the {@link WFService}.
067 *
068 * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
069 * @author last edited by: $Author: mschneider $
070 *
071 * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
072 */
073 class LockFeatureHandler {
074
075 private static final ILogger LOG = LoggerFactory.getLogger( LockFeatureHandler.class );
076
077 // shared instance to handle GetFeature requests (for GetFeatureWithLock)
078 private GetFeatureHandler getFeatureHandler;
079
080 private WFService wfs;
081
082 /**
083 * Creates a new instance of <code>LockFeatureHandler</code>. Only called by the associated {@link WFService}
084 * (once).
085 *
086 * @param wfs
087 * associated WFService
088 */
089 LockFeatureHandler( WFService wfs ) {
090 this.wfs = wfs;
091 this.getFeatureHandler = new GetFeatureHandler( wfs );
092 }
093
094 /**
095 * Handles a {@link LockFeature} request.
096 *
097 * @param request
098 * <code>LockFeature</code> request to perform
099 * @return response to the request
100 * @throws OGCWebServiceException
101 */
102 LockFeatureResponse handleRequest( LockFeature request )
103 throws OGCWebServiceException {
104
105 LockFeatureResponse response = null;
106
107 try {
108 List<FeatureId> fidsToLock = new ArrayList<FeatureId>();
109 Map<Datastore, List<Lock>> dsToLocksMap = buildDsToLocksMap( request );
110 for ( Datastore ds : dsToLocksMap.keySet() ) {
111 fidsToLock.addAll( ds.determineFidsToLock( dsToLocksMap.get( ds ) ) );
112 }
113 org.deegree.io.datastore.Lock lock = LockManager.getInstance().acquireLock( request, fidsToLock );
114 Set<String> lockedFeatures = lock.getLockedFids();
115 String[] lockedFids = new String[lockedFeatures.size()];
116 String[] notLockedFids = new String[fidsToLock.size() - lockedFeatures.size()];
117 int lockedIdx = 0, notLockedIdx = 0;
118 for ( FeatureId fid : fidsToLock ) {
119 String fidAsString = fid.getAsString();
120 if ( lockedFeatures.contains( fidAsString ) ) {
121 lockedFids[lockedIdx++] = fidAsString;
122 } else {
123 notLockedFids[notLockedIdx++] = fidAsString;
124 }
125 }
126 response = new LockFeatureResponse( request, lock.getId(), lockedFids, notLockedFids );
127 } catch ( DatastoreException e ) {
128 LOG.logDebug( e.getMessage(), e );
129 throw new OGCWebServiceException( this.getClass().getName(), e.getMessage() );
130 }
131
132 return response;
133 }
134
135 /**
136 * Handles a {@link GetFeatureWithLock} request.
137 * <p>
138 * This is performed using the following strategy:
139 * <ul>
140 * <li>Perform the request as a standard {@link GetFeature} request.</li>
141 * <li>Create a corresponding {@link LockFeature} request from the {@link GetFeatureWithLock} request.</li>
142 * <li>Set the "lockId" attribute in the result feature collection.</li>
143 * <li>Remove all features from the feature collection that could not be locked.</li>
144 * </ul>
145 *
146 * @param request
147 * <code>GetFeatureWithLock</code> request to perform
148 * @return response to the request
149 * @throws OGCWebServiceException
150 */
151 FeatureResult handleRequest( GetFeatureWithLock request )
152 throws OGCWebServiceException {
153
154 FeatureResult response = this.getFeatureHandler.handleRequest( request );
155
156 List<Lock> locks = new ArrayList<Lock>( request.getQuery().length );
157 for ( Query query : request.getQuery() ) {
158 Lock lock = new Lock( null, query.getTypeNames()[0], query.getFilter() );
159 locks.add( lock );
160 }
161 LockFeature lockRequest = LockFeature.create( WFService.VERSION, null, null, request.getExpiry(),
162 request.getLockAction(), locks );
163 LockFeatureResponse lockResponse = handleRequest( lockRequest );
164
165 // set "lockId" parameter in result feature collection
166 FeatureCollection fc = (FeatureCollection) response.getResponse();
167 fc.setAttribute( "lockId", lockResponse.getLockId() );
168
169 // remove all features from the result feature collection that could not be locked (and
170 // count removed features)
171 int removed = 0;
172 for ( String notLockedFid : lockResponse.getFeaturesNotLocked() ) {
173 Feature feature = fc.getFeature( notLockedFid );
174 if ( feature != null ) {
175 fc.remove( feature );
176 removed++;
177 }
178 }
179
180 // correct "numberOfFeatures" attribute (and make it work even for resultType=HITS)
181 int before = Integer.parseInt( fc.getAttribute( "numberOfFeatures" ) );
182 fc.setAttribute( "numberOfFeatures", "" + ( before - removed ) );
183
184 return response;
185 }
186
187 /**
188 * Groups all {@link Lock}s contained in the given {@link LockFeature} request by the responsible {@link Datastore},
189 * i.e. that serves the {@link FeatureType} to be locked.
190 *
191 * @param request
192 * @return keys: responsible <code>Datastore</code>, values: List of <code>Lock</code>s
193 * @throws OGCWebServiceException
194 */
195 private Map<Datastore, List<Lock>> buildDsToLocksMap( LockFeature request )
196 throws OGCWebServiceException {
197
198 Map<Datastore, List<Lock>> dsToLocksMap = new HashMap<Datastore, List<Lock>>();
199 List<Lock> locks = request.getLocks();
200
201 for ( Lock lock : locks ) {
202 QualifiedName ftName = lock.getTypeName();
203 MappedFeatureType ft = this.wfs.getMappedFeatureType( ftName );
204
205 if ( ft == null ) {
206 String msg = Messages.getMessage( "WFS_FEATURE_TYPE_UNKNOWN", ftName );
207 throw new OGCWebServiceException( this.getClass().getName(), msg );
208 }
209 if ( ft.isAbstract() ) {
210 String msg = Messages.getMessage( "WFS_FEATURE_TYPE_ABSTRACT", ftName );
211 throw new OGCWebServiceException( this.getClass().getName(), msg );
212 }
213 if ( !ft.isVisible() ) {
214 String msg = Messages.getMessage( "WFS_FEATURE_TYPE_INVISIBLE", ftName );
215 throw new OGCWebServiceException( this.getClass().getName(), msg );
216 }
217
218 Datastore ds = ft.getGMLSchema().getDatastore();
219 List<Lock> dsLocks = dsToLocksMap.get( ds );
220 if ( dsLocks == null ) {
221 dsLocks = new ArrayList<Lock>();
222 dsToLocksMap.put( ds, dsLocks );
223 }
224 dsLocks.add( lock );
225 }
226 return dsToLocksMap;
227 }
228 }