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