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.ByteArrayOutputStream;
039    import java.io.IOException;
040    import java.net.URL;
041    import java.nio.charset.Charset;
042    import java.util.List;
043    
044    import javax.xml.bind.JAXBContext;
045    import javax.xml.bind.JAXBException;
046    import javax.xml.bind.Marshaller;
047    import javax.xml.bind.Unmarshaller;
048    
049    import org.apache.commons.httpclient.HttpClient;
050    import org.apache.commons.httpclient.HttpException;
051    import org.apache.commons.httpclient.methods.PostMethod;
052    import org.apache.commons.httpclient.methods.StringRequestEntity;
053    import org.deegree.enterprise.control.ajax.ResponseHandler;
054    import org.deegree.enterprise.control.ajax.WebEvent;
055    import org.deegree.framework.log.ILogger;
056    import org.deegree.framework.log.LoggerFactory;
057    import org.deegree.framework.util.FileUtils;
058    import org.deegree.framework.util.StringTools;
059    import org.deegree.framework.util.TimeTools;
060    import org.deegree.portal.cataloguemanager.model.CICitation;
061    import org.deegree.portal.cataloguemanager.model.CIOnlineResource;
062    import org.deegree.portal.cataloguemanager.model.Date;
063    import org.deegree.portal.cataloguemanager.model.DistributionFormat;
064    import org.deegree.portal.cataloguemanager.model.ExceptionBean;
065    import org.deegree.portal.cataloguemanager.model.Linkage;
066    import org.deegree.portal.cataloguemanager.model.MDDataIdentification;
067    import org.deegree.portal.cataloguemanager.model.MDDigitalTransferOptions;
068    import org.deegree.portal.cataloguemanager.model.MDDistribution;
069    import org.deegree.portal.cataloguemanager.model.MDFormat;
070    import org.deegree.portal.cataloguemanager.model.MDMetadata;
071    import org.deegree.portal.cataloguemanager.model.MDReferenceSystem;
072    import org.deegree.portal.cataloguemanager.model.MDRepresentativeFraction;
073    import org.deegree.portal.cataloguemanager.model.MetadataBean;
074    import org.deegree.portal.cataloguemanager.model.Name;
075    import org.deegree.portal.cataloguemanager.model.OnLine;
076    import org.deegree.portal.cataloguemanager.model.SpatialResolution;
077    import org.deegree.portal.cataloguemanager.model.TransferOptions;
078    import org.deegree.portal.cataloguemanager.model.Version;
079    
080    /**
081     * 
082     * 
083     * 
084     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
085     * @author last edited by: $Author: apoth $
086     * 
087     * @version $Revision: 27070 $, $Date: 2010-09-29 10:23:47 +0200 (Mi, 29. Sep 2010) $
088     */
089    public class UpdateMetadataListener extends InsertMetadataListener {
090    
091        private static final ILogger LOG = LoggerFactory.getLogger( UpdateMetadataListener.class );
092    
093        @Override
094        public void actionPerformed( WebEvent event, ResponseHandler responseHandler )
095                                throws IOException {
096    
097            try {
098                conf = getCatalogueManagerConfiguration( event );
099                MetadataBean bean = (MetadataBean) event.getAsBean();
100    
101                JAXBContext jc = JAXBContext.newInstance( "org.deegree.portal.cataloguemanager.model" );
102                Unmarshaller u = jc.createUnmarshaller();
103    
104                String tmp = conf.getTemplateURL( bean.getHlevel() );
105                URL url = conf.resolve( tmp );
106                MDMetadata mdMetadata = (MDMetadata) u.unmarshal( url );
107                tmp = bean.getHlevel();
108                if ( "series".equalsIgnoreCase( tmp ) ) {
109                    tmp = "datasetcollection";
110                }
111                mdMetadata.getHierarchyLevelName().setCharacterString( tmp );
112    
113                // metadata settings
114                mdMetadata.getFileIdentifier().setCharacterString( bean.getIdentifier() );
115                setContact( mdMetadata, bean );
116                mdMetadata.getDateStamp().setDateTime( TimeTools.getISOFormattedTime() );
117                if ( bean.getCrs() != null ) {
118                    MDReferenceSystem mdRef = mdMetadata.getReferenceSystemInfo().getMDReferenceSystem();
119                    mdRef.getReferenceSystemIdentifier().getRSIdentifier().getCode().setCharacterString( bean.getCrs() );
120                }
121    
122                // data identification settings
123                MDDataIdentification mdDataId = mdMetadata.getIdentificationInfo().getMDDataIdentification();
124                mdDataId.getAbstract().setCharacterString( bean.getAbstract_() );
125                setKeywords( mdDataId, bean );
126                setPointOfContact( mdDataId, bean );
127                mdDataId.getTopicCategory().setMDTopicCategoryCode( bean.getTopCat() );
128                setExtent( mdDataId, bean );
129                SpatialResolution spr = mdDataId.getSpatialResolution();
130                if ( bean.getScale() == null || bean.getScale().trim().length() == 0 ) {
131                    mdDataId.setSpatialResolution( null );
132                } else {
133                    MDRepresentativeFraction frac = spr.getMDResolution().getEquivalentScale().getMDRepresentativeFraction();
134                    frac.getDenominator().setInteger( Integer.parseInt( bean.getScale().trim() ) );
135                }
136    
137                CICitation citation = mdDataId.getCitation().getCICitation();
138                citation.getTitle().setCharacterString( bean.getDatasetTitle() );
139                citation.getIdentifier().getMDIdentifier().getCode().setCharacterString( bean.getIdentifier() );
140    
141                List<Date> dates = citation.getDate();
142                dates.clear();
143                if ( bean.getCreation() != null ) {
144                    addDate( dates, "creation", bean.getCreation() );
145                }
146                if ( bean.getPublication() != null ) {
147                    addDate( dates, "publication", bean.getPublication() );
148                }
149                if ( bean.getRevision() != null ) {
150                    addDate( dates, "revision", bean.getRevision() );
151                }
152    
153                if ( bean.getTransferOnline() != null && bean.getTransferOnline().trim().length() > 0 ) {
154                    setDistributionInfo( mdMetadata, bean );
155                } else {
156                    mdMetadata.setDistributionInfo( null );
157                }
158    
159                if ( bean.getLineage() != null && bean.getLineage().trim().length() > 0 ) {
160                    mdMetadata.getDataQualityInfo().getDQDataQuality().getLineage().getLILineage().getStatement().setCharacterString(
161                                                                                                                                      bean.getLineage() );
162                } else {
163                    mdMetadata.setDataQualityInfo( null );
164                }
165    
166                updateCSW( responseHandler, mdMetadata );
167    
168            } catch ( Exception e ) {
169                LOG.logError( e.getMessage(), e );
170                ExceptionBean eb = new ExceptionBean( getClass().getName(), e.getMessage() );
171                responseHandler.writeAndClose( true, eb );
172                return;
173            }
174    
175        }
176        
177        private void setDistributionInfo( MDMetadata mdMetadata, MetadataBean bean ) {
178            MDDistribution mddist = mdMetadata.getDistributionInfo().getMDDistribution();
179            if ( mddist == null ) {
180                mddist = new MDDistribution();
181                TransferOptions to = new TransferOptions();
182                MDDigitalTransferOptions dto = new MDDigitalTransferOptions();
183                OnLine ol = new OnLine();
184                CIOnlineResource ciolr = new CIOnlineResource();
185                ciolr.setLinkage( new Linkage() );
186                ol.setCIOnlineResource( ciolr );
187                dto.setOnLine( ol );
188                to.setMDDigitalTransferOptions( dto );
189                mddist.setTransferOptions( to );
190            }
191            MDDigitalTransferOptions to = mddist.getTransferOptions().getMDDigitalTransferOptions();
192            to.getOnLine().getCIOnlineResource().getLinkage().setURL( bean.getTransferOnline() );
193            if ( bean.getTransferFormatName() != null && bean.getTransferFormatName().length() > 0 ) {
194                DistributionFormat format = mdMetadata.getDistributionInfo().getMDDistribution().getDistributionFormat();
195                if ( format == null ) {
196                    format = new DistributionFormat();
197                    MDFormat mdf = new MDFormat();
198                    mdf.setName( new Name() );
199                    mdf.setVersion( new Version() );
200                    format.setMDFormat( mdf );
201                }
202                format.getMDFormat().getName().setCharacterString( bean.getTransferFormatName() );
203                if ( bean.getTransferFormatVersion() != null && bean.getTransferFormatVersion().length() > 0 ) {
204                    format.getMDFormat().getVersion().setCharacterString( bean.getTransferFormatVersion() );
205                } else {
206                    format.getMDFormat().getVersion().setCharacterString( "unknown" );
207                }
208            } else {
209                mdMetadata.getDistributionInfo().getMDDistribution().setDistributionFormat( null );
210            }
211        }
212    
213        private String updateCSW( ResponseHandler responseHandler, MDMetadata mdMetadata )
214                                throws JAXBException, IOException, HttpException {
215    
216            JAXBContext jc = JAXBContext.newInstance( "org.deegree.portal.cataloguemanager.model" );
217            Marshaller m = jc.createMarshaller();
218    
219            ByteArrayOutputStream bos = new ByteArrayOutputStream( 10000 );
220            m.marshal( mdMetadata, bos );
221    
222            URL url = getClass().getResource( "update.xml" );
223            String s = FileUtils.readTextFile( url ).toString();
224            String t = new String( bos.toByteArray() );
225            int c = t.indexOf( "?>" );
226            t = t.substring( c + 2 );
227            s = StringTools.replace( s, "$id$", mdMetadata.getFileIdentifier().getCharacterString(), false );
228            s = StringTools.replace( s, "$data$", t, false );
229            String csw = conf.getCatalogueURL();
230            PostMethod post = new PostMethod( csw );
231            post.getParams().setContentCharset( Charset.defaultCharset().displayName() );
232            StringRequestEntity re = new StringRequestEntity( s );
233            post.setRequestEntity( re );
234            HttpClient client = new HttpClient();
235            client.executeMethod( post );
236            s = post.getResponseBodyAsString();
237            if ( s.toLowerCase().indexOf( "exception" ) > -1 ) {
238                ExceptionBean eb = new ExceptionBean( getClass().getName(), "update failed" );
239                responseHandler.writeAndClose( true, eb );
240            } else {
241                responseHandler.writeAndClose( "insert performed" );
242            }
243    
244            return s;
245        }
246    
247    }