    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/ogcwebservices/csw/manager/XMLFactory.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.URI;
    039    import java.util.List;
    040
    041    import org.deegree.framework.log.ILogger;
    042    import org.deegree.framework.log.LoggerFactory;
    043    import org.deegree.framework.xml.XMLException;
    044    import org.deegree.framework.xml.XMLParsingException;
    045    import org.deegree.framework.xml.XMLTools;
    046    import org.deegree.model.filterencoding.Filter;
    047    import org.deegree.ogcbase.CommonNamespaces;
    048    import org.deegree.ogcwebservices.OGCWebServiceException;
    049    import org.deegree.ogcwebservices.wfs.operation.transaction.TransactionResponse;
    050    import org.w3c.dom.Document;
    051    import org.w3c.dom.Element;
    052    import org.w3c.dom.Node;
    053    import org.w3c.dom.NodeList;
    054
    055    /**
    056     *
    057     * @version $Revision: 25619 $
    058     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
    059     * @author last edited by: $Author: apoth $
    060     *
    061     * @version $Revision: 25619 $, $Date: 2010-08-02 08:17:24 +0200 (Mo, 02. Aug 2010) $
    062     *
    063     */
    064    public class XMLFactory {
    065
    066        private static ILogger LOG = LoggerFactory.getLogger( XMLFactory.class );
    067
    068        private static final URI CSWNS = CommonNamespaces.CSWNS;
    069
    070        /**
    071         * @return a XML representation of a {@link Transaction} object
    072         *
    073         * @param transaction
    074         * @throws XMLParsingException
    075         * @throws XMLException
    076         * @throws OGCWebServiceException
    077         */
    078        public static final TransactionDocument export( Transaction transaction )
    079                                throws XMLParsingException, OGCWebServiceException {
    080
    081            TransactionDocument transDoc = new TransactionDocument();
    082            try {
    083                transDoc.createEmptyDocument();
    084            } catch ( Exception e ) {
    085                throw new XMLParsingException( e.getMessage() );
    086            }
    087
    088            transDoc.getRootElement().setAttribute( "service", "CSW" );
    089            String version = transaction.getVersion();
    090            if ( version == null || "".equals( version.trim() ) ) {
    091                version = "2.0.0";
    092            }
    093            transDoc.getRootElement().setAttribute( "version", version );
    094            transDoc.getRootElement().setAttribute( "verboseResponse", "" + transaction.verboseResponse() );
    095
    096            List<Operation> ops = transaction.getOperations();
    097            for ( int i = 0; i < ops.size(); i++ ) {
    098                Operation operation = ops.get( i );
    099                appendOperation( transDoc.getRootElement(), operation );
    100            }
    101
    102            return transDoc;
    103
    104        }
    105
    106        /**
    107         * @return an XML representation of a {@link TransactionResponse} object
    108         *
    109         * @param response
    110         * @throws XMLParsingException
    111         */
    112        public static final HarvetResultDocument export( HarvestResult response )
    113                                throws XMLParsingException {
    114
    115            String version = response.getRequest().getVersion();
    116            if ( version == null || "".equals( version.trim() ) ) {
    117                version = "2.0.1";
    118            }
    119
    120            HarvetResultDocument harvestRespDoc = new HarvetResultDocument();
    121            try {
    122                harvestRespDoc.createEmptyDocument( version );
    123            } catch ( Exception e ) {
    124                throw new XMLParsingException( e.getMessage() );
    125            }
    126
    127            Element root = harvestRespDoc.getRootElement();
    128            root.setAttribute( "version", version );
    129
    130            URI namespaceURI = ( version.equals( "2.0.2" ) ) ? CommonNamespaces.CSW202NS : CommonNamespaces.CSWNS;
    131
    132            Element elem = XMLTools.appendElement( root, namespaceURI, "csw:TransactionSummary" );
    133            root.appendChild( elem );
    134            XMLTools.appendElement( elem, namespaceURI, "csw:totalInserted", Integer.toString( response.getTotalInserted() ) );
    135            XMLTools.appendElement( elem, namespaceURI, "csw:totalUpdated", Integer.toString( response.getTotalUpdated() ) );
    136            XMLTools.appendElement( elem, namespaceURI, "csw:totalDeleted", Integer.toString( response.getTotalDeleted() ) );
    137
    138            List<Node> records = response.getResults().getRecords();
    139            if ( records.size() > 0 ) {
    140                elem = XMLTools.appendElement( root, namespaceURI, "csw:InsertResult" );
    141                Element briefRecord = XMLTools.appendElement( elem, namespaceURI, "csw:BriefRecord" );
    142                Document owner = root.getOwnerDocument();
    143                for ( int i = 0; i < records.size(); ++i ) {
    144                    LOG.logDebug( "(" + i + " of " + records.size() + ") trying to insert xmlnode: "
    145                                  + records.get( i ).getNodeName() );
    146                    NodeList childs = records.get( i ).getChildNodes();
    147                    for ( int j = 0; j < childs.getLength(); j++ ) {
    148                        Node a = owner.importNode( childs.item( j ), true );
    149                        briefRecord.appendChild( a );
    150                    }
    151                }
    152            }
    153
    154            return harvestRespDoc;
    155        }
    156
    157        /**
    158         * @return an XML representation of a {@link TransactionResponse} object
    159         *
    160         * @param response
    161         * @throws XMLParsingException
    162         */
    163        public static final TransactionResultDocument export( TransactionResult response )
    164                                throws XMLParsingException {
    165
    166            String version = response.getRequest().getVersion();
    167            if ( version == null || "".equals( version.trim() ) ) {
    168                version = "2.0.1";
    169            }
    170
    171            TransactionResultDocument transRespDoc = new TransactionResultDocument();
    172            try {
    173                transRespDoc.createEmptyDocument( version );
    174            } catch ( Exception e ) {
    175                throw new XMLParsingException( e.getMessage() );
    176            }
    177
    178            Element root = transRespDoc.getRootElement();
    179            root.setAttribute( "version", version );
    180
    181            URI namespaceURI = ( version.equals( "2.0.2" ) ) ? CommonNamespaces.CSW202NS : CommonNamespaces.CSWNS;
    182
    183            Element elem = XMLTools.appendElement( root, namespaceURI, "csw:TransactionSummary" );
    184            root.appendChild( elem );
    185            XMLTools.appendElement( elem, namespaceURI, "csw:totalInserted", Integer.toString( response.getTotalInserted() ) );
    186            XMLTools.appendElement( elem, namespaceURI, "csw:totalUpdated", Integer.toString( response.getTotalUpdated() ) );
    187            XMLTools.appendElement( elem, namespaceURI, "csw:totalDeleted", Integer.toString( response.getTotalDeleted() ) );
    188
    189            List<Node> records = response.getResults().getRecords();
    190            if ( records.size() > 0 ) {
    191                elem = XMLTools.appendElement( root, namespaceURI, "csw:InsertResult" );
    192                Element briefRecord = XMLTools.appendElement( elem, namespaceURI, "csw:BriefRecord" );
    193                Document owner = root.getOwnerDocument();
    194                for ( int i = 0; i < records.size(); ++i ) {
    195                    LOG.logDebug( "(" + i + " of " + records.size() + ") trying to insert xmlnode: "
    196                                  + records.get( i ).getNodeName() );
    197                    NodeList childs = records.get( i ).getChildNodes();
    198                    for ( int j = 0; j < childs.getLength(); j++ ) {
    199                        Node a = owner.importNode( childs.item( j ), true );
    200                        briefRecord.appendChild( a );
    201                    }
    202                }
    203                LOG.logDebug( "Successfully inserted " + records.size() + " brief records into the result documents" );
    204                // root.appendChild( elem );
    205            }
    206
    207            return transRespDoc;
    208
    209        }
    210
    211        /**
    212         *
    213         * @param root
    214         * @param operation
    215         * @throws OGCWebServiceException
    216         */
    217        public static void appendOperation( Element root, Operation operation )
    218                                throws OGCWebServiceException {
    219
    220            if ( "Insert".equals( operation.getName() ) ) {
    221                appendInsert( root, (Insert) operation );
    222            } else if ( "Update".equals( operation.getName() ) ) {
    223                appendUpdate( root, (Update) operation );
    224            } else if ( "Delete".equals( operation.getName() ) ) {
    225                appendDelete( root, (Delete) operation );
    226            } else {
    227                throw new OGCWebServiceException( "unknown CS-W transaction operation: " + operation.getName() );
    228            }
    229        }
    230
    231        /**
    232         * appends an Delete operation to the passed root element
    233         *
    234         * @param root
    235         * @param delete
    236         */
    237        public static void appendDelete( Element root, Delete delete ) {
    238            Document doc = root.getOwnerDocument();
    239            Element op = doc.createElementNS( CSWNS.toASCIIString(), "csw:" + delete.getName() );
    240            if ( delete.getHandle() != null ) {
    241                op.setAttribute( "handle", delete.getHandle() );
    242            }
    243            if ( delete.getTypeName() != null ) {
    244
    245                op.setAttribute( "typeName", delete.getTypeName().toASCIIString() );
    246            }
    247
    248            Filter filter = delete.getConstraint();
    249            Element constraint = doc.createElementNS( CSWNS.toASCIIString(), "csw:Constraint" );
    250            constraint.setAttribute( "version", "1.1.0" );
    251            op.appendChild( constraint );
    252            org.deegree.model.filterencoding.XMLFactory.appendFilter( constraint, filter );
    253            root.appendChild( op );
    254
    255        }
    256
    257        /**
    258         * appends an Update operation to the passed root element
    259         *
    260         * @param root
    261         * @param update
    262         */
    263        public static void appendUpdate( Element root, Update update ) {
    264            Document doc = root.getOwnerDocument();
    265            Element op = doc.createElementNS( CSWNS.toASCIIString(), "csw:" + update.getName() );
    266            if ( update.getHandle() != null ) {
    267                op.setAttribute( "handle", update.getHandle() );
    268            }
    269            if ( update.getTypeName() != null ) {
    270                op.setAttribute( "typeName", update.getTypeName().toASCIIString() );
    271            }
    272            XMLTools.insertNodeInto( update.getRecord(), op );
    273            Filter filter = update.getConstraint();
    274            Element constraint = doc.createElementNS( CSWNS.toASCIIString(), "csw:Constraint" );
    275            constraint.setAttribute( "version", "1.1.0" );
    276            op.appendChild( constraint );
    277            org.deegree.model.filterencoding.XMLFactory.appendFilter( constraint, filter );
    278            root.appendChild( op );
    279        }
    280
    281        /**
    282         * appends an Insert operation to the passed root element
    283         *
    284         * @param root
    285         * @param insert
    286         */
    287        public static void appendInsert( Element root, Insert insert ) {
    288            Document doc = root.getOwnerDocument();
    289            Element tmp = doc.createElementNS( CSWNS.toASCIIString(), "csw:" + insert.getName() );
    290            Element op = (Element) root.appendChild( tmp );
    291            if ( insert.getHandle() != null ) {
    292                op.setAttribute( "handle", insert.getHandle() );
    293            }
    294            List<Element> list = insert.getRecords();
    295            for ( Element e : list ) {
    296                Node copy = doc.importNode( e, true );
    297                op.appendChild( copy );
    298            }
    299            // for ( int i = 0; i < list.size(); i++ ) {
    300            // XMLTools.insertNodeInto( list.get( i ), op );
    301            // }
    302
    303        }
    304    }