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