001    //$HeadURL: 
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2010 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.portal.cataloguemanager.control;
037    
038    import java.io.IOException;
039    import java.net.URL;
040    import java.net.URLEncoder;
041    import java.nio.charset.Charset;
042    import java.util.Calendar;
043    import java.util.Enumeration;
044    import java.util.HashMap;
045    import java.util.List;
046    import java.util.Map;
047    
048    import javax.servlet.http.HttpServletRequest;
049    
050    import org.apache.commons.httpclient.HttpMethod;
051    import org.deegree.enterprise.control.ajax.ResponseHandler;
052    import org.deegree.enterprise.control.ajax.WebEvent;
053    import org.deegree.framework.log.ILogger;
054    import org.deegree.framework.log.LoggerFactory;
055    import org.deegree.framework.util.HttpUtils;
056    import org.deegree.framework.util.TimeTools;
057    import org.deegree.framework.xml.NamespaceContext;
058    import org.deegree.framework.xml.XMLFragment;
059    import org.deegree.framework.xml.XMLParsingException;
060    import org.deegree.framework.xml.XMLTools;
061    import org.deegree.ogcbase.CommonNamespaces;
062    import org.deegree.portal.cataloguemanager.model.ExceptionBean;
063    import org.deegree.portal.cataloguemanager.model.MetadataBean;
064    
065    /**
066     * 
067     * 
068     * 
069     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
070     * @author last edited by: $Author: apoth $
071     * 
072     * @version $Revision: 27592 $, $Date: 2010-10-28 15:41:38 +0200 (Do, 28. Okt 2010) $
073     */
074    public class LoadMetadataSetListener extends AbstractMetadataListener {
075    
076        private static ILogger LOG = LoggerFactory.getLogger( LoadMetadataSetListener.class );
077    
078        private static String GRBID = "request=GetRecordById&OUTPUTSCHEMA=http://www.isotc211.org/2005/gmd&service=CSW&version=2.0.2&ID=";
079    
080        private static String PARAM_ID = "ID";
081    
082        private XMLFragment xml;
083    
084        private static NamespaceContext nsContext = CommonNamespaces.getNamespaceContext();
085    
086        @SuppressWarnings("unchecked")
087        @Override
088        public void actionPerformed( WebEvent event, ResponseHandler responseHandler )
089                                throws IOException {
090    
091            CatalogueManagerConfiguration config = getCatalogueManagerConfiguration( event );
092            Map<String, String> param = event.getParameter();
093            LOG.logDebug( "load metadataset request param: ", param );
094            String charset = event.getCharacterEncoding();
095            if ( charset == null ) {
096                charset = Charset.defaultCharset().displayName();
097            }
098    
099            String s = URLEncoder.encode( param.get( PARAM_ID ), charset );
100            URL url = new URL( config.getCatalogueURL() + '?' + GRBID + s );
101            LOG.logDebug( "load metadataset: ", url );
102            try {
103                Enumeration<String> en = ( (HttpServletRequest) getRequest() ).getHeaderNames();
104                Map<String, String> map = new HashMap<String, String>();
105                while ( en.hasMoreElements() ) {
106                    String name = (String) en.nextElement();
107                    map.put( name, ( (HttpServletRequest) getRequest() ).getHeader( name ) );
108                }
109                HttpMethod get = HttpUtils.performHttpGet( url.toExternalForm(), null, 60000, null, null, map );           
110                xml = new XMLFragment();
111                xml.load( get.getResponseBodyAsStream(), url.toExternalForm() );       
112                xml = new XMLFragment( XMLTools.getFirstChildElement( xml.getRootElement() ) );
113            } catch ( Exception e ) {
114                LOG.logError( e.getMessage(), e );
115                ExceptionBean eb = new ExceptionBean( getClass().getName(), e.getMessage() );
116                responseHandler.writeAndClose( true, eb );
117                return;
118            }
119            if ( LOG.getLevel() == ILogger.LOG_DEBUG ) {
120                LOG.logDebug( xml.getAsPrettyString() );
121            }
122    
123            event.getSession().setAttribute( "MD_TEMPLATE", xml );
124    
125            MetadataBean metadata = new MetadataBean();
126    
127            if ( param.get( "ISTEMPLATE" ) == null || "false".equalsIgnoreCase( param.get( "ISTEMPLATE" ) ) ) {
128                String tmp = getValue( config.getXPath( "datasetTitle" ) );
129                if ( "".equals( tmp ) ) {
130                    tmp = getValue( config.getXPath( "seviceTitle" ) );
131                }
132                metadata.setDatasetTitle( tmp );
133                metadata.setIdentifier( getValue( config.getXPath( "identifier" ) ) );
134                metadata.setAbstract_( getValue( config.getXPath( "abstract_" ) ) );
135            }
136            String tmp = getValue( config.getXPath( "begin" ) );
137            if ( tmp != null && tmp.trim().length() > 0 ) {
138                Calendar cal = TimeTools.createCalendar( tmp );
139                metadata.setBegin( toISO( cal ) );
140            }
141            metadata.setContactCity( getValue( config.getXPath( "contactCity" ) ) );
142            metadata.setContactCountry( getValue( config.getXPath( "contactCountry" ) ) );
143            metadata.setContactDeliveryPoint( getValue( config.getXPath( "contactDeliveryPoint" ) ) );
144            metadata.setContactEmailAddress( getValue( config.getXPath( "contactEmailAddress" ) ) );
145            metadata.setContactFacsimile( getValue( config.getXPath( "contactFacsimile" ) ) );
146            metadata.setContactIndividualName( getValue( config.getXPath( "contactIndividualName" ) ) );
147            metadata.setContactOrganisationName( getValue( config.getXPath( "contactOrganisationName" ) ) );
148            metadata.setContactPostalCode( getValue( config.getXPath( "contactPostalCode" ) ) );
149            metadata.setContactRole( getValue( config.getXPath( "contactRole" ) ) );
150            metadata.setContactVoice( getValue( config.getXPath( "contactVoice" ) ) );
151            tmp = getValue( config.getXPath( "creation" ) );
152            if ( tmp != null && tmp.trim().length() > 0 ) {
153                Calendar cal = TimeTools.createCalendar( tmp );
154                metadata.setCreation( toISO( cal ) );
155            }
156            metadata.setCrs( getValue( config.getXPath( "crs" ) ) );
157            tmp = getValue( config.getXPath( "end" ) );
158            if ( tmp != null && tmp.trim().length() > 0 ) {
159                Calendar cal = TimeTools.createCalendar( tmp );
160                metadata.setEnd( toISO( cal ) );
161            }
162            metadata.setGeogrDescription( getValue( config.getXPath( "geogrDescription" ) ) );
163            metadata.setHlevel( getValue( config.getXPath( "hlevel" ) ) );
164    
165            metadata.setKeywords( getValues( config.getXPath( "keywords" ) ) );
166            metadata.setParentId( getValue( config.getXPath( "parentId" ) ) );
167            metadata.setPocCity( getValue( config.getXPath( "pocCity" ) ) );
168            metadata.setPocCountry( getValue( config.getXPath( "pocCountry" ) ) );
169            metadata.setPocDeliveryPoint( getValue( config.getXPath( "pocDeliveryPoint" ) ) );
170            metadata.setPocEmailAddress( getValue( config.getXPath( "pocEmailAddress" ) ) );
171            metadata.setPocFacsimile( getValue( config.getXPath( "pocFacsimile" ) ) );
172            metadata.setPocIndividualName( getValue( config.getXPath( "pocIndividualName" ) ) );
173            metadata.setPocOrganisationName( getValue( config.getXPath( "pocOrganisationName" ) ) );
174            metadata.setPocPostalCode( getValue( config.getXPath( "pocPostalCode" ) ) );
175            metadata.setPocRole( getValue( config.getXPath( "pocRole" ) ) );
176            metadata.setPocVoice( getValue( config.getXPath( "pocVoice" ) ) );
177            tmp = getValue( config.getXPath( "publication" ) );
178            if ( tmp != null && tmp.trim().length() > 0 ) {
179                Calendar cal = TimeTools.createCalendar( tmp );
180                metadata.setPublication( toISO( cal ) );
181            }
182            tmp = getValue( config.getXPath( "revision" ) );
183            if ( tmp != null && tmp.trim().length() > 0 ) {
184                Calendar cal = TimeTools.createCalendar( tmp );
185                metadata.setRevision( toISO( cal ) );
186            }
187            metadata.setScale( getValue( config.getXPath( "scale" ) ) );
188            metadata.setTopCat( getValue( config.getXPath( "topCat" ) ) );
189    
190            tmp = getValue( config.getXPath( "begin" ) );
191            if ( tmp.length() == 0 ) {
192                tmp = getValue( config.getXPath( "begin2" ) );
193            }
194            Calendar cal = TimeTools.createCalendar( tmp );
195            metadata.setBegin( toISO( cal ) );
196    
197            tmp = getValue( config.getXPath( "end" ) );
198            if ( tmp.length() == 0 ) {
199                tmp = getValue( config.getXPath( "end2" ) );
200            }
201            cal = TimeTools.createCalendar( tmp );
202            metadata.setEnd( toISO( cal ) );
203            metadata.setLineage( getValue( config.getXPath( "lineage" ) ) );
204    
205            metadata.setTransferOnline( getValue( config.getXPath( "transferOptOnline" ) ) );
206    
207            // result page uses UTF-8 encoding
208            String charEnc = Charset.defaultCharset().displayName();
209            responseHandler.setContentType( "application/json; charset=" + charEnc );
210            responseHandler.writeAndClose( true, metadata );
211        }
212    
213        private String toISO( Calendar cal ) {
214            StringBuffer sb = new StringBuffer();
215            sb.append( Integer.toString( cal.get( Calendar.YEAR ) ) ).append( '-' );
216            sb.append( Integer.toString( cal.get( Calendar.MONTH ) + 1 ) ).append( '-' );
217            sb.append( Integer.toString( cal.get( Calendar.DAY_OF_MONTH ) ) );
218            return sb.toString();
219        }
220    
221        private String getValue( String xPath )
222                                throws IOException {
223            try {
224                return XMLTools.getNodeAsString( xml.getRootElement(), xPath, nsContext, "" );
225            } catch ( XMLParsingException e ) {
226                throw new IOException( e.getMessage() );
227            }
228        }
229    
230        private List<String> getValues( String xPath )
231                                throws IOException {
232            try {
233                return XMLTools.getNodesAsStringList( xml.getRootElement(), xPath, nsContext );
234            } catch ( XMLParsingException e ) {
235                throw new IOException( e.getMessage() );
236            }
237        }
238    }