001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcwebservices/wfs/operation/LockFeatureDocument.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.operation;
037    
038    import java.io.IOException;
039    import java.net.URL;
040    import java.util.ArrayList;
041    import java.util.List;
042    
043    import org.deegree.datatypes.QualifiedName;
044    import org.deegree.datatypes.time.TimeDuration;
045    import org.deegree.framework.xml.XMLParsingException;
046    import org.deegree.framework.xml.XMLTools;
047    import org.deegree.i18n.Messages;
048    import org.deegree.model.filterencoding.AbstractFilter;
049    import org.deegree.model.filterencoding.Filter;
050    import org.deegree.ogcwebservices.InvalidParameterValueException;
051    import org.deegree.ogcwebservices.wfs.operation.LockFeature.ALL_SOME_TYPE;
052    import org.w3c.dom.Element;
053    import org.xml.sax.SAXException;
054    
055    /**
056     * Parser for "wfs:LockFeature" requests.
057     *
058     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
059     * @author last edited by: $Author: mschneider $
060     *
061     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
062     */
063    public class LockFeatureDocument extends AbstractWFSRequestDocument {
064    
065        private static final long serialVersionUID = 7168101158239058596L;
066    
067        private static final String XML_TEMPLATE = "LockFeatureTemplate.xml";
068    
069        /**
070         * Creates a skeleton document that contains the root element and the namespace bindings only.
071         *
072         * @throws IOException
073         * @throws SAXException
074         */
075        public void createEmptyDocument()
076                                throws IOException, SAXException {
077            URL url = LockFeatureDocument.class.getResource( XML_TEMPLATE );
078            if ( url == null ) {
079                throw new IOException( "The resource '" + XML_TEMPLATE + " could not be found." );
080            }
081            load( url );
082        }
083    
084        /**
085         * Parses the underlying "wfs:LockFeature" document into a {@link LockFeature} object.
086         *
087         * @param id
088         * @return corresponding <code>LockFeature</code> object
089         * @throws XMLParsingException
090         * @throws InvalidParameterValueException
091         */
092        public LockFeature parse( String id )
093                                throws XMLParsingException, InvalidParameterValueException {
094    
095            checkServiceAttribute();
096            String version = checkVersionAttribute();
097    
098            Element root = this.getRootElement();
099            String handle = XMLTools.getNodeAsString( root, "@handle", nsContext, null );
100    
101            long expiry = parseExpiry( root );
102    
103            String lockActionString = XMLTools.getNodeAsString( root, "@lockAction", nsContext, "ALL" );
104            ALL_SOME_TYPE lockAction = ALL_SOME_TYPE.ALL;
105            try {
106                lockAction = LockFeature.validateLockAction( lockActionString );
107            } catch ( InvalidParameterValueException e ) {
108                throw new XMLParsingException( e.getMessage() );
109            }
110    
111            List<Element> lockElements = XMLTools.getRequiredElements( root, "wfs:Lock", nsContext );
112            List<Lock> locks = new ArrayList<Lock>( lockElements.size() );
113            for ( Element lockElement : lockElements ) {
114                locks.add( parseLock( lockElement ) );
115            }
116            return new LockFeature( version, id, handle, expiry, lockAction, locks );
117        }
118    
119        /**
120         * Parses the value of the expiry-attribute of the given node.
121         * <p>
122         * Handles both the WFS 1.1.0 and the WFS 1.2.0 formats:
123         * <ul>
124         * <li>WFS 1.1.0: specified as an xsd:positiveInteger (minutes)</li>
125         * <li>WFS 1.2.0: specified as an xsd:duration (identified by the leading P in the string)</li>
126         * </ul>
127         *
128         * @param root
129         * @return duration (in milliseconds)
130         * @throws XMLParsingException
131         */
132        static long parseExpiry( Element root )
133                                throws XMLParsingException {
134    
135            long millis = 0;
136            String expiry = XMLTools.getNodeAsString( root, "@expiry", nsContext, LockFeature.DEFAULT_EXPIRY );
137    
138            if ( expiry.length() < 1 ) {
139                String msg = "Attribute 'expiry' is empty.";
140                throw new XMLParsingException( msg );
141            }
142    
143            if ( expiry.charAt( 0 ) == 'P' ) {
144                millis = TimeDuration.createTimeDuration( expiry ).getAsMilliSeconds();
145            } else {
146                try {
147                    millis = ( (long) Integer.parseInt( expiry ) ) * 60 * 1000;
148                    if ( millis < 1 ) {
149                        throw new NumberFormatException();
150                    }
151                } catch ( NumberFormatException e ) {
152                    String msg = Messages.getMessage( "WFS_EXPIRY_ATTRIBUTE_INVALID", expiry );
153                    throw new XMLParsingException( msg );
154                }
155            }
156            return millis;
157        }
158    
159        /**
160         * Parses the given <code>wfs:Lock</code> element.
161         *
162         * @param lockElement
163         * @return corresponding <code>Lock</code> object
164         * @throws XMLParsingException
165         */
166        private Lock parseLock( Element lockElement )
167                                throws XMLParsingException {
168    
169            String handle = XMLTools.getNodeAsString( lockElement, "@handle", nsContext, null );
170            QualifiedName typeName = XMLTools.getRequiredNodeAsQualifiedName( lockElement, "@typeName", nsContext );
171            Filter filter = null;
172            Element filterElement = (Element) XMLTools.getNode( lockElement, "ogc:Filter", nsContext );
173            if ( filterElement != null ) {
174                filter = AbstractFilter.buildFromDOM( filterElement, false );
175            }
176            return new Lock( handle, typeName, filter );
177        }
178    }