001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/csw/manager/TransactionResultDocument.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2008 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/deegree/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015    
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     Lesser General Public License for more details.
020    
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025     Contact:
026    
027     Andreas Poth
028     lat/lon GmbH
029     Aennchenstr. 19
030     53115 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041    
042     ---------------------------------------------------------------------------*/
043    package org.deegree.ogcwebservices.csw.manager;
044    
045    import java.io.IOException;
046    import java.net.MalformedURLException;
047    import java.net.URI;
048    import java.util.ArrayList;
049    import java.util.List;
050    
051    import org.deegree.framework.log.ILogger;
052    import org.deegree.framework.log.LoggerFactory;
053    import org.deegree.framework.xml.NamespaceContext;
054    import org.deegree.framework.xml.XMLFragment;
055    import org.deegree.framework.xml.XMLParsingException;
056    import org.deegree.framework.xml.XMLTools;
057    import org.deegree.ogcbase.CommonNamespaces;
058    import org.w3c.dom.Document;
059    import org.w3c.dom.Element;
060    import org.w3c.dom.Node;
061    import org.xml.sax.SAXException;
062    
063    /**
064     * 
065     * 
066     * 
067     * @version $Revision: 9549 $
068     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
069     * @author last edited by: $Author: lbuesching $
070     * 
071     * @version 1.0. $Revision: 9549 $, $Date: 2008-01-16 11:17:02 +0100 (Mi, 16 Jan 2008) $
072     * 
073     * @since 2.0
074     */
075    public class TransactionResultDocument extends XMLFragment {
076    
077        private static final ILogger LOG = LoggerFactory.getLogger( TransactionDocument.class );
078    
079        private NamespaceContext nsc = CommonNamespaces.getNamespaceContext();
080    
081        /**
082         * initializes an empty TransactionDocument
083         * 
084         */
085        public TransactionResultDocument() {
086            try {
087                setSystemId( XMLFragment.DEFAULT_URL );
088            } catch ( MalformedURLException e ) {
089                LOG.logError( e.getMessage(), e );
090            }
091        }
092    
093        /**
094         * 
095         * @throws IOException
096         * @throws SAXException
097         */
098        public void createEmptyDocument( String version) {
099            Document doc = XMLTools.create();
100            URI namespaceURI = (version.equals( "2.0.2" )) ? CommonNamespaces.CSW202NS : CommonNamespaces.CSWNS; 
101            Element root = doc.createElementNS( namespaceURI.toASCIIString(),
102                                                "csw:TransactionResponse" );
103            setRootElement( root );
104    
105        }
106    
107        /**
108         * parses a CS-W TransactionResponse document and creates a jave class representation from it.
109         * 
110         * @return
111         * @throws XMLParsingException
112         */
113        public TransactionResult parseTransactionResponse( Transaction transaction )
114                                throws XMLParsingException {
115    
116            Element root = getRootElement();
117            int inserted = XMLTools.getNodeAsInt( root, "./csw:TransactionSummary/csw:totalInserted",
118                                                  nsc, 0 );
119            int deleted = XMLTools.getNodeAsInt( root, "./csw:TransactionSummary/csw:totalDeleted",
120                                                 nsc, 0 );
121            int updated = XMLTools.getNodeAsInt( root, "./csw:TransactionSummary/csw:totalUpdated",
122                                                 nsc, 0 );
123    
124            List list = XMLTools.getNodes( root, "./csw:InsertResult/child::*", nsc );
125            List<Node> records = new ArrayList<Node>( list.size() );
126            for ( int i = 0; i < list.size(); i++ ) {
127                records.add( (Node) list.get( i ) );
128            }
129            InsertResults ir = new InsertResults( records );
130    
131            return new TransactionResult( transaction, inserted, deleted, updated, ir );
132    
133        }
134    
135    }