    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/ogcwebservices/csw/manager/TransactionResultDocument.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.csw.manager;
    037
    038    import java.net.MalformedURLException;
    039    import java.net.URI;
    040    import java.util.ArrayList;
    041    import java.util.List;
    042
    043    import org.deegree.framework.log.ILogger;
    044    import org.deegree.framework.log.LoggerFactory;
    045    import org.deegree.framework.xml.NamespaceContext;
    046    import org.deegree.framework.xml.XMLFragment;
    047    import org.deegree.framework.xml.XMLParsingException;
    048    import org.deegree.framework.xml.XMLTools;
    049    import org.deegree.ogcbase.CommonNamespaces;
    050    import org.w3c.dom.Document;
    051    import org.w3c.dom.Element;
    052    import org.w3c.dom.Node;
    053
    054    /**
    055     *
    056     *
    057     *
    058     * @version $Revision: 18195 $
    059     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
    060     * @author last edited by: $Author: mschneider $
    061     *
    062     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
    063     *
    064     * @since 2.0
    065     */
    066    public class TransactionResultDocument extends XMLFragment {
    067
    068        private static final long serialVersionUID = 2354194484320566707L;
    069
    070        private static final ILogger LOG = LoggerFactory.getLogger( TransactionDocument.class );
    071
    072        private NamespaceContext nsc = CommonNamespaces.getNamespaceContext();
    073
    074        /**
    075         * initializes an empty TransactionDocument
    076         *
    077         */
    078        public TransactionResultDocument() {
    079            try {
    080                setSystemId( XMLFragment.DEFAULT_URL );
    081            } catch ( MalformedURLException e ) {
    082                LOG.logError( e.getMessage(), e );
    083            }
    084        }
    085
    086        /**
    087         * @param version
    088         *
    089         */
    090        public void createEmptyDocument( String version) {
    091            Document doc = XMLTools.create();
    092            URI namespaceURI = (version.equals( "2.0.2" )) ? CommonNamespaces.CSW202NS : CommonNamespaces.CSWNS;
    093            Element root = doc.createElementNS( namespaceURI.toASCIIString(),
    094                                                "csw:TransactionResponse" );
    095            setRootElement( root );
    096
    097        }
    098
    099        /**
    100         * parses a CS-W TransactionResponse document and creates a jave class representation from it.
    101         * @param transaction
    102         *
    103         * @return the result
    104         * @throws XMLParsingException
    105         */
    106        public TransactionResult parseTransactionResponse( Transaction transaction )
    107                                throws XMLParsingException {
    108
    109            Element root = getRootElement();
    110            int inserted = XMLTools.getNodeAsInt( root, "./csw:TransactionSummary/csw:totalInserted",
    111                                                  nsc, 0 );
    112            int deleted = XMLTools.getNodeAsInt( root, "./csw:TransactionSummary/csw:totalDeleted",
    113                                                 nsc, 0 );
    114            int updated = XMLTools.getNodeAsInt( root, "./csw:TransactionSummary/csw:totalUpdated",
    115                                                 nsc, 0 );
    116
    117            List<Node> list = XMLTools.getNodes( root, "./csw:InsertResult/child::*", nsc );
    118            List<Node> records = new ArrayList<Node>( list.size() );
    119            for ( int i = 0; i < list.size(); i++ ) {
    120                records.add( list.get( i ) );
    121            }
    122            InsertResults ir = new InsertResults( records );
    123
    124            return new TransactionResult( transaction, inserted, deleted, updated, ir );
    125
    126        }
    127
    128    }