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.io.StringReader; 041 import java.net.URL; 042 import java.util.Enumeration; 043 import java.util.HashMap; 044 import java.util.List; 045 import java.util.Map; 046 047 import javax.servlet.http.HttpServletRequest; 048 import javax.xml.bind.JAXBContext; 049 import javax.xml.bind.JAXBException; 050 import javax.xml.bind.Marshaller; 051 import javax.xml.bind.Unmarshaller; 052 053 import org.apache.commons.httpclient.HttpException; 054 import org.apache.commons.httpclient.HttpMethod; 055 import org.deegree.enterprise.control.ajax.ResponseHandler; 056 import org.deegree.enterprise.control.ajax.WebEvent; 057 import org.deegree.framework.log.ILogger; 058 import org.deegree.framework.log.LoggerFactory; 059 import org.deegree.framework.util.FileUtils; 060 import org.deegree.framework.util.HttpUtils; 061 import org.deegree.framework.util.StringTools; 062 import org.deegree.framework.util.TimeTools; 063 import org.deegree.framework.xml.XMLFragment; 064 import org.deegree.portal.cataloguemanager.model.CICitation; 065 import org.deegree.portal.cataloguemanager.model.CIOnlineResource; 066 import org.deegree.portal.cataloguemanager.model.Date; 067 import org.deegree.portal.cataloguemanager.model.DistributionFormat; 068 import org.deegree.portal.cataloguemanager.model.ExceptionBean; 069 import org.deegree.portal.cataloguemanager.model.Linkage; 070 import org.deegree.portal.cataloguemanager.model.MDDataIdentification; 071 import org.deegree.portal.cataloguemanager.model.MDDigitalTransferOptions; 072 import org.deegree.portal.cataloguemanager.model.MDDistribution; 073 import org.deegree.portal.cataloguemanager.model.MDFormat; 074 import org.deegree.portal.cataloguemanager.model.MDLegalConstraints; 075 import org.deegree.portal.cataloguemanager.model.MDMetadata; 076 import org.deegree.portal.cataloguemanager.model.MDReferenceSystem; 077 import org.deegree.portal.cataloguemanager.model.MDRepresentativeFraction; 078 import org.deegree.portal.cataloguemanager.model.MetadataBean; 079 import org.deegree.portal.cataloguemanager.model.Name; 080 import org.deegree.portal.cataloguemanager.model.OnLine; 081 import org.deegree.portal.cataloguemanager.model.ParentIdentifier; 082 import org.deegree.portal.cataloguemanager.model.ResourceConstraints; 083 import org.deegree.portal.cataloguemanager.model.SpatialResolution; 084 import org.deegree.portal.cataloguemanager.model.TransferOptions; 085 import org.deegree.portal.cataloguemanager.model.Version; 086 import org.xml.sax.SAXException; 087 088 /** 089 * 090 * 091 * 092 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 093 * @author last edited by: $Author: apoth $ 094 * 095 * @version $Revision: 30690 $, $Date: 2011-05-06 10:23:46 +0200 (Fr, 06 Mai 2011) $ 096 */ 097 public class UpdateMetadataListener extends InsertMetadataListener { 098 099 private static final ILogger LOG = LoggerFactory.getLogger( UpdateMetadataListener.class ); 100 101 @Override 102 public void actionPerformed( WebEvent event, ResponseHandler responseHandler ) 103 throws IOException { 104 105 try { 106 conf = getCatalogueManagerConfiguration( event ); 107 MetadataBean bean = (MetadataBean) event.getAsBean(); 108 109 JAXBContext jc = JAXBContext.newInstance( "org.deegree.portal.cataloguemanager.model" ); 110 Unmarshaller u = jc.createUnmarshaller(); 111 112 String tmp = conf.getTemplateURL( bean.getHlevel() ); 113 URL url = conf.resolve( tmp ); 114 MDMetadata mdMetadata = (MDMetadata) u.unmarshal( url ); 115 tmp = bean.getHlevel(); 116 if ( "series".equalsIgnoreCase( tmp ) ) { 117 tmp = "datasetcollection"; 118 } 119 if ( "dataset".equalsIgnoreCase( tmp ) && bean.getParentId() != null ) { 120 ParentIdentifier pi = new ParentIdentifier(); 121 pi.setCharacterString( bean.getParentId() ); 122 mdMetadata.setParentIdentifier( pi ); 123 } 124 mdMetadata.getHierarchyLevelName().setCharacterString( tmp ); 125 126 // metadata settings 127 mdMetadata.getFileIdentifier().setCharacterString( bean.getIdentifier() ); 128 if ( "dataset".equalsIgnoreCase( bean.getHlevel() ) ) { 129 mdMetadata.getParentIdentifier().setCharacterString( bean.getParentId() ); 130 } 131 setContact( mdMetadata, bean ); 132 mdMetadata.getDateStamp().setDateTime( TimeTools.getISOFormattedTime() ); 133 if ( bean.getCrs() != null ) { 134 MDReferenceSystem mdRef = mdMetadata.getReferenceSystemInfo().getMDReferenceSystem(); 135 mdRef.getReferenceSystemIdentifier().getRSIdentifier().getCode().setCharacterString( bean.getCrs() ); 136 } 137 138 // data identification settings 139 MDDataIdentification mdDataId = mdMetadata.getIdentificationInfo().getMDDataIdentification(); 140 mdDataId.getAbstract().setCharacterString( bean.getAbstract_() ); 141 setKeywords( mdDataId, bean ); 142 setPointOfContact( mdDataId, bean ); 143 mdDataId.getTopicCategory().setMDTopicCategoryCode( bean.getTopCat() ); 144 setExtent( mdDataId, bean ); 145 SpatialResolution spr = mdDataId.getSpatialResolution(); 146 if ( bean.getScale() == null || bean.getScale().trim().length() == 0 ) { 147 mdDataId.setSpatialResolution( null ); 148 } else { 149 MDRepresentativeFraction frac = spr.getMDResolution().getEquivalentScale().getMDRepresentativeFraction(); 150 frac.getDenominator().setInteger( Integer.parseInt( bean.getScale().trim() ) ); 151 } 152 153 CICitation citation = mdDataId.getCitation().getCICitation(); 154 citation.getTitle().setCharacterString( bean.getDatasetTitle() ); 155 citation.getIdentifier().getMDIdentifier().getCode().setCharacterString( bean.getIdentifier() ); 156 157 // citation dates 158 List<Date> dates = citation.getDate(); 159 dates.clear(); 160 if ( bean.getCreation() != null ) { 161 addDate( dates, "creation", bean.getCreation() ); 162 } 163 if ( bean.getPublication() != null ) { 164 addDate( dates, "publication", bean.getPublication() ); 165 } 166 if ( bean.getRevision() != null ) { 167 addDate( dates, "revision", bean.getRevision() ); 168 } 169 170 // data access 171 if ( bean.getTransferOnline() != null && bean.getTransferOnline().trim().length() > 0 ) { 172 setDistributionInfo( mdMetadata, bean ); 173 } else { 174 mdMetadata.setDistributionInfo( null ); 175 } 176 177 // lineage 178 if ( bean.getLineage() != null && bean.getLineage().trim().length() > 0 ) { 179 mdMetadata.getDataQualityInfo().getDQDataQuality().getLineage().getLILineage().getStatement().setCharacterString( 180 bean.getLineage() ); 181 } else { 182 mdMetadata.setDataQualityInfo( null ); 183 } 184 185 // resource constraints 186 List<ResourceConstraints> list = mdMetadata.getIdentificationInfo().getMDDataIdentification().getResourceConstraints(); 187 if ( bean.getAccessConstraints() == null ) { 188 list.clear(); 189 } else { 190 MDLegalConstraints legal = null; 191 for ( ResourceConstraints resourceConstraints : list ) { 192 if ( resourceConstraints.getMDLegalConstraints() != null ) { 193 legal = resourceConstraints.getMDLegalConstraints(); 194 legal.getAccessConstraints().getMDRestrictionCode().setCodeListValue( 195 bean.getAccessConstraints() ); 196 legal.getAccessConstraints().getMDRestrictionCode().setValue( bean.getAccessConstraints() ); 197 } 198 } 199 } 200 201 updateCSW( responseHandler, mdMetadata ); 202 203 } catch ( Exception e ) { 204 LOG.logError( e.getMessage(), e ); 205 ExceptionBean eb = new ExceptionBean( getClass().getName(), e.getMessage() ); 206 responseHandler.writeAndClose( true, eb ); 207 return; 208 } 209 210 } 211 212 private void setDistributionInfo( MDMetadata mdMetadata, MetadataBean bean ) { 213 MDDistribution mddist = mdMetadata.getDistributionInfo().getMDDistribution(); 214 if ( mddist == null ) { 215 mddist = new MDDistribution(); 216 TransferOptions to = new TransferOptions(); 217 MDDigitalTransferOptions dto = new MDDigitalTransferOptions(); 218 OnLine ol = new OnLine(); 219 CIOnlineResource ciolr = new CIOnlineResource(); 220 ciolr.setLinkage( new Linkage() ); 221 ol.setCIOnlineResource( ciolr ); 222 dto.setOnLine( ol ); 223 to.setMDDigitalTransferOptions( dto ); 224 mddist.setTransferOptions( to ); 225 } 226 MDDigitalTransferOptions to = mddist.getTransferOptions().getMDDigitalTransferOptions(); 227 to.getOnLine().getCIOnlineResource().getLinkage().setURL( bean.getTransferOnline() ); 228 if ( bean.getTransferFormatName() != null && bean.getTransferFormatName().length() > 0 ) { 229 DistributionFormat format = mdMetadata.getDistributionInfo().getMDDistribution().getDistributionFormat(); 230 if ( format == null ) { 231 format = new DistributionFormat(); 232 MDFormat mdf = new MDFormat(); 233 mdf.setName( new Name() ); 234 mdf.setVersion( new Version() ); 235 format.setMDFormat( mdf ); 236 } 237 format.getMDFormat().getName().setCharacterString( bean.getTransferFormatName() ); 238 if ( bean.getTransferFormatVersion() != null && bean.getTransferFormatVersion().length() > 0 ) { 239 format.getMDFormat().getVersion().setCharacterString( bean.getTransferFormatVersion() ); 240 } else { 241 format.getMDFormat().getVersion().setCharacterString( "unknown" ); 242 } 243 } else { 244 mdMetadata.getDistributionInfo().getMDDistribution().setDistributionFormat( null ); 245 } 246 } 247 248 @SuppressWarnings("unchecked") 249 private String updateCSW( ResponseHandler responseHandler, MDMetadata mdMetadata ) 250 throws JAXBException, IOException, HttpException, SAXException { 251 252 JAXBContext jc = JAXBContext.newInstance( "org.deegree.portal.cataloguemanager.model" ); 253 Marshaller m = jc.createMarshaller(); 254 255 ByteArrayOutputStream bos = new ByteArrayOutputStream( 10000 ); 256 m.marshal( mdMetadata, bos ); 257 258 URL url = getClass().getResource( "update.xml" ); 259 String s = FileUtils.readTextFile( url ).toString(); 260 String t = new String( bos.toByteArray() ); 261 int c = t.indexOf( "?>" ); 262 t = t.substring( c + 2 ); 263 s = StringTools.replace( s, "$id$", mdMetadata.getFileIdentifier().getCharacterString(), false ); 264 s = StringTools.replace( s, "$data$", t, false ); 265 String csw = conf.getCatalogueURL(); 266 267 XMLFragment xml = new XMLFragment( new StringReader( s ), XMLFragment.DEFAULT_URL ); 268 Enumeration<String> en = ( (HttpServletRequest) getRequest() ).getHeaderNames(); 269 Map<String, String> map = new HashMap<String, String>(); 270 while ( en.hasMoreElements() ) { 271 String name = (String) en.nextElement(); 272 if ( !name.equalsIgnoreCase( "accept-encoding" ) && !name.equalsIgnoreCase( "content-length" ) 273 && !name.equalsIgnoreCase( "user-agent" ) ) { 274 map.put( name, ( (HttpServletRequest) getRequest() ).getHeader( name ) ); 275 } 276 } 277 HttpMethod post = HttpUtils.performHttpPost( csw, xml, 60000, null, null, map ); 278 s = post.getResponseBodyAsString(); 279 if ( s.toLowerCase().indexOf( "exception" ) > -1 ) { 280 ExceptionBean eb = new ExceptionBean( getClass().getName(), "update failed" ); 281 responseHandler.writeAndClose( true, eb ); 282 } else { 283 responseHandler.writeAndClose( "insert performed" ); 284 } 285 286 return s; 287 } 288 289 }