001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/ogcwebservices/sos/describeplatform/PlatformDescriptionDocument.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.sos.describeplatform;
037    
038    import java.io.IOException;
039    import java.net.URL;
040    import java.util.ArrayList;
041    import java.util.Hashtable;
042    import java.util.List;
043    
044    import javax.xml.transform.TransformerException;
045    
046    import org.deegree.datatypes.QualifiedName;
047    import org.deegree.framework.util.StringTools;
048    import org.deegree.framework.xml.XMLFragment;
049    import org.deegree.framework.xml.XMLParsingException;
050    import org.deegree.framework.xml.XMLTools;
051    import org.deegree.framework.xml.XSLTDocument;
052    import org.deegree.ogcwebservices.OGCWebServiceException;
053    import org.deegree.ogcwebservices.sos.ComponentDescriptionDocument;
054    import org.deegree.ogcwebservices.sos.WFSRequestGenerator;
055    import org.deegree.ogcwebservices.sos.WFSRequester;
056    import org.deegree.ogcwebservices.sos.configuration.SOSDeegreeParams;
057    import org.deegree.ogcwebservices.sos.configuration.SourceServerConfiguration;
058    import org.deegree.ogcwebservices.sos.sensorml.Classifier;
059    import org.deegree.ogcwebservices.sos.sensorml.ComponentDescription;
060    import org.deegree.ogcwebservices.sos.sensorml.EngineeringCRS;
061    import org.deegree.ogcwebservices.sos.sensorml.Identifier;
062    import org.deegree.ogcwebservices.sos.sensorml.LocationModel;
063    import org.w3c.dom.Document;
064    import org.w3c.dom.Node;
065    import org.xml.sax.SAXException;
066    
067    /**
068     * gets the platform metadata from a xsl transformed wfs result
069     *
070     * @author <a href="mailto:mkulbe@lat-lon.de">Matthias Kulbe </a>
071     *
072     */
073    
074    public class PlatformDescriptionDocument extends ComponentDescriptionDocument {
075    
076        /**
077         *
078         */
079        private static final long serialVersionUID = 645409071795675313L;
080    
081        private static final String XML_TEMPLATE = "DescribePlatformTemplate.xml";
082    
083        /**
084         * creates an document from a template file
085         */
086        @Override
087        public void createEmptyDocument()
088                                throws IOException, SAXException {
089            URL url = PlatformDescriptionDocument.class.getResource( XML_TEMPLATE );
090            if ( url == null ) {
091                throw new IOException( "The resource '" + XML_TEMPLATE + " could not be found." );
092            }
093            load( url );
094        }
095    
096        /**
097         * gets the platform descriptions from a wfs and transform it with a xslt script
098         *
099         * @param deegreeParams
100         * @param typNames
101         * @return the metadata
102         * @throws OGCWebServiceException
103         *
104         */
105        public PlatformMetadata[] getPlatform( SOSDeegreeParams deegreeParams, String[] typNames )
106                                throws OGCWebServiceException {
107    
108            try {
109    
110                // gets the documents from wfs server
111                Document[] docs = getPlatformDescriptionDocuments( deegreeParams, typNames );
112    
113                ArrayList<PlatformMetadata> platformMetadata = new ArrayList<PlatformMetadata>( 1000 );
114    
115                for ( int d = 0; d < docs.length; d++ ) {
116    
117                    if ( docs[d] != null ) {
118    
119                        List<Node> nl = XMLTools.getNodes( docs[d], "sml:Platforms/sml:Platform", nsContext );
120    
121                        // process all platforms in document
122                        for ( int y = 0; y < nl.size(); y++ ) {
123    
124                            Node platformNode = nl.get( y );
125    
126                            // get identifiedAs
127                            List<Node> identifierList = XMLTools.getNodes( platformNode, "sml:identifiedAs", nsContext );
128                            if ( ( identifierList == null ) || ( identifierList.size() <= 0 ) ) {
129                                throw new XMLParsingException( "at least one identifiedAs required" );
130    
131                            }
132                            ArrayList<Identifier> identifiers = new ArrayList<Identifier>( identifierList.size() );
133                            for ( int i = 0; i < identifierList.size(); i++ ) {
134                                identifiers.add( getIdentifiedAs( identifierList.get( i ) ) );
135                            }
136    
137                            // get ClassifiedAs
138                            List<Node> classifierList = XMLTools.getNodes( platformNode, "sml:classifiedAs", nsContext );
139                            ArrayList<Classifier> classifiers = new ArrayList<Classifier>( classifierList.size() );
140                            for ( int i = 0; i < classifierList.size(); i++ ) {
141                                classifiers.add( getClassifiedAs( classifierList.get( i ) ) );
142                            }
143    
144                            // get attachedTo
145                            String attachedTo = getAttachedTo( XMLTools.getNode( platformNode, "sml:attachedTo", nsContext ) );
146    
147                            // get hasCRS
148                            EngineeringCRS hasCRS = getHasCRS( XMLTools.getNode( platformNode, "sml:hasCRS", nsContext ) );
149    
150                            // get locatedUsing
151                            List<Node> locationModelList = XMLTools.getNodes( platformNode, "sml:locatedUsing", nsContext );
152                            ArrayList<LocationModel> locationModels = new ArrayList<LocationModel>(
153                                                                                                    locationModelList.size() );
154                            for ( int i = 0; i < locationModelList.size(); i++ ) {
155                                locationModels.add( getLocatedUsing( locationModelList.get( i ) ) );
156                            }
157    
158                            // get describedBy
159                            ComponentDescription describedBy = getDescribedBy( XMLTools.getNode( platformNode,
160                                                                                                 "sml:describedBy",
161                                                                                                 nsContext ) );
162    
163                            // get carries
164                            List<Node> carriesList = XMLTools.getNodes( platformNode, "sml:carries", nsContext );
165                            ArrayList<String> carries = new ArrayList<String>( carriesList.size() );
166                            for ( int i = 0; i < carriesList.size(); i++ ) {
167                                String s = XMLTools.getRequiredNodeAsString( carriesList.get( i ), "sml:Asset/text()",
168                                                                             nsContext );
169                                carries.add( s );
170                            }
171    
172                            Identifier[] ids = identifiers.toArray( new Identifier[identifiers.size()] );
173                            Classifier[] cls = classifiers.toArray( new Classifier[classifiers.size()] );
174                            LocationModel[] lm = locationModels.toArray( new LocationModel[locationModels.size()] );
175                            String[] crrs = carries.toArray( new String[carries.size()] );
176                            PlatformMetadata pmd = new PlatformMetadata( ids, cls, hasCRS, lm, describedBy, attachedTo,
177                                                                         crrs );
178                            // add act Metadata to ArrayList
179                            platformMetadata.add( pmd );
180                        }
181    
182                    }
183                }
184    
185                // return the Array with Sensormetadata
186                PlatformMetadata[] pfmd = new PlatformMetadata[platformMetadata.size()];
187                return platformMetadata.toArray( pfmd );
188    
189            } catch ( Exception e ) {
190                e.printStackTrace();
191                throw new OGCWebServiceException( "sos webservice failure" );
192            }
193        }
194    
195        /**
196         * requests all servers which serves one of the requested platforms and returns the transformed
197         * result docs
198         *
199         */
200        private Document[] getPlatformDescriptionDocuments( SOSDeegreeParams dp, String[] typNames )
201                                throws IOException, SAXException, XMLParsingException, TransformerException,
202                                OGCWebServiceException {
203    
204            ArrayList<Document> resultDocuments = new ArrayList<Document>();
205    
206            Hashtable<String, ArrayList<String>> servers = new Hashtable<String, ArrayList<String>>();
207    
208            for ( int t = 0; t < typNames.length; t++ ) {
209                String sourceServerId = dp.getPlatformConfiguration( typNames[t] ).getSourceServerId();
210    
211                if ( servers.containsKey( sourceServerId ) ) {
212                    servers.get( sourceServerId ).add( typNames[t] );
213                } else {
214                    ArrayList<String> temp = new ArrayList<String>();
215                    temp.add( typNames[t] );
216                    servers.put( sourceServerId, temp );
217                }
218    
219            }
220    
221            String[] keySet = servers.keySet().toArray( new String[servers.keySet().size()] );
222    
223            // request all servers from servers hashtable
224            for ( int i = 0; i < keySet.length; i++ ) {
225    
226                List<String> ids = servers.get( keySet[i] );
227    
228                String[] idProps = new String[ids.size()];
229                for ( int a = 0; a < ids.size(); a++ ) {
230                    idProps[a] = dp.getPlatformConfiguration( ids.get( a ) ).getIdPropertyValue();
231                }
232    
233                QualifiedName pdft = dp.getSourceServerConfiguration( keySet[i] ).getPlatformDescriptionFeatureType();
234                QualifiedName pdid = dp.getSourceServerConfiguration( keySet[i] ).getPlatformDescriptionIdPropertyName();
235                Document request = WFSRequestGenerator.createIsLikeOperationWFSRequest( idProps, pdft, pdid );
236    
237                SourceServerConfiguration ssc = dp.getSourceServerConfiguration( keySet[i] );
238    
239                Document resultDoc = null;
240                try {
241                    resultDoc = WFSRequester.sendWFSrequest( request, ssc.getDataService() );
242                } catch ( Exception e ) {
243                    throw new OGCWebServiceException( this.getClass().getName(), "could not get platform data from WFS "
244                                                                                 + StringTools.stackTraceToString( e ) );
245                }
246                if ( resultDoc != null ) {
247                    URL pdxs = dp.getSourceServerConfiguration( keySet[i] ).getPlatformDescriptionXSLTScriptSource();
248                    XSLTDocument sheet = new XSLTDocument();
249                    sheet.load( pdxs );
250                    XMLFragment input = new XMLFragment();
251                    input.setRootElement( resultDoc.getDocumentElement() );
252                    XMLFragment result = sheet.transform( input );
253                    resultDocuments.add( result.getRootElement().getOwnerDocument() );
254                }
255    
256            }
257    
258            return resultDocuments.toArray( new Document[resultDocuments.size()] );
259        }
260    
261    }