001    //$HeadURL: svn+ssh://developername@svn.wald.intevation.org/deegree/base/trunk/resources/eclipse/files_template.xml $
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.portal.cataloguemanager.control;
037    
038    import java.io.File;
039    import java.io.IOException;
040    import java.io.InputStream;
041    import java.io.OutputStream;
042    import java.util.Calendar;
043    import java.util.List;
044    
045    import javax.activation.DataSource;
046    import javax.mail.internet.MimeBodyPart;
047    import javax.mail.internet.MimeMultipart;
048    import javax.servlet.ServletException;
049    import javax.servlet.http.HttpServletRequest;
050    import javax.servlet.http.HttpServletResponse;
051    
052    import org.deegree.enterprise.control.ajax.ResponseHandler;
053    import org.deegree.enterprise.control.ajax.WebEvent;
054    import org.deegree.framework.log.ILogger;
055    import org.deegree.framework.log.LoggerFactory;
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.framework.xml.XSLTDocument;
062    import org.deegree.ogcbase.CommonNamespaces;
063    import org.deegree.portal.cataloguemanager.model.ExceptionBean;
064    import org.deegree.portal.cataloguemanager.model.MetadataBean;
065    import org.stringtree.json.JSONWriter;
066    
067    /**
068     * TODO add class documentation here
069     * 
070     * @author <a href="mailto:name@deegree.org">Andreas Poth</a>
071     * @author last edited by: $Author: admin $
072     * 
073     * @version $Revision: $, $Date: $
074     */
075    public class UploadMetadatasetListener extends AbstractMetadataListener {
076    
077        private static final ILogger LOG = LoggerFactory.getLogger( UploadMetadatasetListener.class );
078    
079        private static NamespaceContext nsContext = CommonNamespaces.getNamespaceContext();
080    
081        private XMLFragment xml;
082    
083        /*
084         * (non-Javadoc)
085         * 
086         * @see
087         * org.deegree.enterprise.control.ajax.AbstractListener#actionPerformed(org.deegree.enterprise.control.ajax.WebEvent
088         * , org.deegree.enterprise.control.ajax.ResponseHandler)
089         */
090        public void actionPerformed( WebEvent event, ResponseHandler responseHandler )
091                                throws IOException {
092            HttpServletRequest request = (HttpServletRequest) getRequest();
093    
094            try {
095                StreamDataSource sds = new StreamDataSource( request );
096                MimeMultipart multi = new MimeMultipart( sds );
097    
098                MimeBodyPart content = (MimeBodyPart) multi.getBodyPart( 0 );
099                InputStream is = content.getInputStream();
100                xml = new XMLFragment();
101                xml.load( is, XMLFragment.DEFAULT_URL );
102    
103            } catch ( Exception e ) {
104                LOG.logError( e );
105                ExceptionBean bean = new ExceptionBean( this.getClass().getName(), e.getMessage() );
106                responseHandler.writeAndClose( true, bean );
107                return;
108            }
109    
110            String rootName = xml.getRootElement().getLocalName();
111            String uri = xml.getRootElement().getNamespaceURI();
112            if ( uri != null && uri.trim().length() > 0 ) {
113                rootName = '{' + uri + "}:" + rootName;
114            }
115    
116            String s = getInitParameter( rootName );
117            if ( s != null ) {
118                File f = new File( s );
119                if ( !f.isAbsolute() ) {
120                    f = new File( event.getAbsolutePath( getInitParameter( s ) ) );
121                }
122                try {
123                    XSLTDocument xslt = new XSLTDocument( f.toURL() );
124                    xml = xslt.transform( xml );
125                } catch ( Exception e ) {
126                    LOG.logError( e );
127                    ExceptionBean bean = new ExceptionBean( this.getClass().getName(), e.getMessage() );
128                    responseHandler.writeAndClose( true, bean );
129                    return;
130                }
131            }
132    
133            event.getSession().setAttribute( "MD_TEMPLATE", xml );
134    
135            CatalogueManagerConfiguration config = getCatalogueManagerConfiguration( event );
136    
137            MetadataBean metadata = new MetadataBean();
138    
139            String tmp = getValue( config.getXPath( "datasetTitle" ) );
140            if ( "".equals( tmp ) ) {
141                tmp = getValue( config.getXPath( "seviceTitle" ) );
142            }
143            metadata.setDatasetTitle( tmp );
144            metadata.setIdentifier( getValue( config.getXPath( "identifier" ) ) );
145            metadata.setAbstract_( getValue( config.getXPath( "abstract_" ) ) );
146            tmp = getValue( config.getXPath( "begin" ) );
147            if ( tmp != null && tmp.trim().length() > 0 ) {
148                Calendar cal = TimeTools.createCalendar( tmp );
149                metadata.setBegin( toISO( cal ) );
150            }
151            metadata.setContactCity( getValue( config.getXPath( "contactCity" ) ) );
152            metadata.setContactCountry( getValue( config.getXPath( "contactCountry" ) ) );
153            metadata.setContactDeliveryPoint( getValue( config.getXPath( "contactDeliveryPoint" ) ) );
154            metadata.setContactEmailAddress( getValue( config.getXPath( "contactEmailAddress" ) ) );
155            metadata.setContactFacsimile( getValue( config.getXPath( "contactFacsimile" ) ) );
156            metadata.setContactIndividualName( getValue( config.getXPath( "contactIndividualName" ) ) );
157            metadata.setContactOrganisationName( getValue( config.getXPath( "contactOrganisationName" ) ) );
158            metadata.setContactPostalCode( getValue( config.getXPath( "contactPostalCode" ) ) );
159            metadata.setContactRole( getValue( config.getXPath( "contactRole" ) ) );
160            metadata.setContactVoice( getValue( config.getXPath( "contactVoice" ) ) );
161            tmp = getValue( config.getXPath( "creation" ) );
162            if ( tmp != null && tmp.trim().length() > 0 ) {
163                Calendar cal = TimeTools.createCalendar( tmp );
164                metadata.setCreation( toISO( cal ) );
165            }
166            metadata.setCrs( getValue( config.getXPath( "crs" ) ) );
167            tmp = getValue( config.getXPath( "end" ) );
168            if ( tmp != null && tmp.trim().length() > 0 ) {
169                Calendar cal = TimeTools.createCalendar( tmp );
170                metadata.setEnd( toISO( cal ) );
171            }
172            metadata.setGeogrDescription( getValue( config.getXPath( "geogrDescription" ) ) );
173            metadata.setHlevel( getValue( config.getXPath( "hlevel" ) ) );
174    
175            metadata.setKeywords( getValues( config.getXPath( "keywords" ) ) );
176            metadata.setParentId( getValue( config.getXPath( "parentId" ) ) );
177            metadata.setPocCity( getValue( config.getXPath( "pocCity" ) ) );
178            metadata.setPocCountry( getValue( config.getXPath( "pocCountry" ) ) );
179            metadata.setPocDeliveryPoint( getValue( config.getXPath( "pocDeliveryPoint" ) ) );
180            metadata.setPocEmailAddress( getValue( config.getXPath( "pocEmailAddress" ) ) );
181            metadata.setPocFacsimile( getValue( config.getXPath( "pocFacsimile" ) ) );
182            metadata.setPocIndividualName( getValue( config.getXPath( "pocIndividualName" ) ) );
183            metadata.setPocOrganisationName( getValue( config.getXPath( "pocOrganisationName" ) ) );
184            metadata.setPocPostalCode( getValue( config.getXPath( "pocPostalCode" ) ) );
185            metadata.setPocRole( getValue( config.getXPath( "pocRole" ) ) );
186            metadata.setPocVoice( getValue( config.getXPath( "pocVoice" ) ) );
187            tmp = getValue( config.getXPath( "publication" ) );
188            if ( tmp != null && tmp.trim().length() > 0 ) {
189                Calendar cal = TimeTools.createCalendar( tmp );
190                metadata.setPublication( toISO( cal ) );
191            }
192            tmp = getValue( config.getXPath( "revision" ) );
193            if ( tmp != null && tmp.trim().length() > 0 ) {
194                Calendar cal = TimeTools.createCalendar( tmp );
195                metadata.setRevision( toISO( cal ) );
196            }
197            metadata.setScale( getValue( config.getXPath( "scale" ) ) );
198            metadata.setTopCat( getValue( config.getXPath( "topCat" ) ) );
199    
200            tmp = getValue( config.getXPath( "begin" ) );
201            if ( tmp.length() == 0 ) {
202                tmp = getValue( config.getXPath( "begin2" ) );
203            }
204            Calendar cal = TimeTools.createCalendar( tmp );
205            metadata.setBegin( toISO( cal ) );
206    
207            tmp = getValue( config.getXPath( "end" ) );
208            if ( tmp.length() == 0 ) {
209                tmp = getValue( config.getXPath( "end2" ) );
210            }
211            cal = TimeTools.createCalendar( tmp );
212            metadata.setEnd( toISO( cal ) );
213            metadata.setLineage( getValue( config.getXPath( "lineage" ) ) );
214    
215            metadata.setTransferOnline( getValue( config.getXPath( "transferOptOnline" ) ) );
216    
217            s = getValue( config.getXPath( "accessConstraints" ) );
218            if ( s == null ) {
219                s = getValue( config.getXPath( "srvAccessConstraints" ) );
220            }
221            metadata.setAccessConstraints( s );
222    
223            JSONWriter writer = new JSONWriter( false );
224            metadata.getClass().getModifiers();
225            request.setAttribute( "TEMPLATES", writer.write( metadata ) );
226    
227            try {
228                HttpServletResponse resp = responseHandler.getHttpServletResponse();
229                // result page uses UTF-8 encoding
230                resp.setCharacterEncoding( "UTF-8" );
231                request.getRequestDispatcher( '/' + getNextPage() ).forward( request, resp );
232            } catch ( ServletException e ) {
233                LOG.logError( e );
234                ExceptionBean bean = new ExceptionBean( this.getClass().getName(), e.getMessage() );
235                responseHandler.writeAndClose( true, bean );
236                return;
237            }
238        }
239    
240        private String toISO( Calendar cal ) {
241            StringBuffer sb = new StringBuffer();
242            sb.append( Integer.toString( cal.get( Calendar.YEAR ) ) ).append( '-' );
243            sb.append( Integer.toString( cal.get( Calendar.MONTH ) + 1 ) ).append( '-' );
244            sb.append( Integer.toString( cal.get( Calendar.DAY_OF_MONTH ) ) );
245            return sb.toString();
246        }
247    
248        private String getValue( String xPath )
249                                throws IOException {
250            try {
251                return XMLTools.getNodeAsString( xml.getRootElement(), xPath, nsContext, "" );
252            } catch ( XMLParsingException e ) {
253                throw new IOException( e.getMessage() );
254            }
255        }
256    
257        private List<String> getValues( String xPath )
258                                throws IOException {
259            try {
260                return XMLTools.getNodesAsStringList( xml.getRootElement(), xPath, nsContext );
261            } catch ( XMLParsingException e ) {
262                throw new IOException( e.getMessage() );
263            }
264        }
265    
266        /**
267         * This class maps the request stream to the content parser that is able to pick files from it.
268         */
269        private class StreamDataSource implements DataSource {
270            private HttpServletRequest m_req;
271    
272            public StreamDataSource( HttpServletRequest req ) {
273                m_req = req;
274            }
275    
276            /**
277             * Returns the content type for the request stream.
278             */
279            public String getContentType() {
280                return m_req.getContentType();
281            }
282    
283            /**
284             * Returns a stream from the request.
285             */
286            public InputStream getInputStream()
287                                    throws IOException {
288                return m_req.getInputStream();
289            }
290    
291            /**
292             * This method is useless and it always returns a null.
293             */
294            public String getName() {
295                return null;
296            }
297    
298            /**
299             * Maps output to System.out. Do something more sensible here...
300             */
301            public OutputStream getOutputStream() {
302                return System.out;
303            }
304    
305        }
306    
307    }