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.ByteArrayOutputStream;
039 import java.io.IOException;
040 import java.io.OutputStream;
041 import java.net.URI;
042 import java.net.URL;
043 import java.nio.charset.Charset;
044 import java.util.ArrayList;
045 import java.util.HashMap;
046 import java.util.List;
047 import java.util.Locale;
048 import java.util.Map;
049 import java.util.Properties;
050 import java.util.UUID;
051
052 import javax.servlet.http.HttpServletResponse;
053 import javax.xml.transform.dom.DOMSource;
054 import javax.xml.transform.stream.StreamResult;
055
056 import net.sf.jasperreports.engine.JRDataSource;
057 import net.sf.jasperreports.engine.JREmptyDataSource;
058 import net.sf.jasperreports.engine.JasperRunManager;
059
060 import org.apache.commons.httpclient.HttpMethod;
061 import org.deegree.datatypes.QualifiedName;
062 import org.deegree.enterprise.control.ajax.ResponseHandler;
063 import org.deegree.enterprise.control.ajax.WebEvent;
064 import org.deegree.framework.log.ILogger;
065 import org.deegree.framework.log.LoggerFactory;
066 import org.deegree.framework.util.HttpUtils;
067 import org.deegree.framework.xml.XMLFragment;
068 import org.deegree.framework.xml.XMLTools;
069 import org.deegree.framework.xml.XSLTDocument;
070 import org.deegree.i18n.Messages;
071 import org.deegree.model.filterencoding.ComplexFilter;
072 import org.deegree.model.filterencoding.Filter;
073 import org.deegree.model.filterencoding.Literal;
074 import org.deegree.model.filterencoding.LogicalOperation;
075 import org.deegree.model.filterencoding.Operation;
076 import org.deegree.model.filterencoding.OperationDefines;
077 import org.deegree.model.filterencoding.PropertyIsLikeOperation;
078 import org.deegree.model.filterencoding.PropertyName;
079 import org.deegree.ogcbase.CommonNamespaces;
080 import org.deegree.ogcbase.PropertyPath;
081 import org.deegree.ogcwebservices.csw.discovery.GetRecords;
082 import org.deegree.ogcwebservices.csw.discovery.Query;
083 import org.deegree.ogcwebservices.csw.discovery.XMLFactory;
084 import org.deegree.ogcwebservices.csw.discovery.GetRecords.RESULT_TYPE;
085 import org.deegree.portal.cataloguemanager.model.ExceptionBean;
086 import org.w3c.dom.Element;
087 import org.w3c.dom.Node;
088
089 /**
090 * TODO add class documentation here
091 *
092 * @author <a href="mailto:name@deegree.org">Andreas Poth</a>
093 * @author last edited by: $Author: admin $
094 *
095 * @version $Revision: $, $Date: $
096 */
097 public class FullMetadataSetListener extends AbstractMetadataListener {
098
099 private static final ILogger LOG = LoggerFactory.getLogger( FullMetadataSetListener.class );
100
101 private CatalogueManagerConfiguration config;
102
103 private Locale loc;
104
105 /*
106 * (non-Javadoc)
107 *
108 * @see
109 * org.deegree.enterprise.control.ajax.AbstractListener#actionPerformed(org.deegree.enterprise.control.ajax.WebEvent
110 * , org.deegree.enterprise.control.ajax.ResponseHandler)
111 */
112 @SuppressWarnings("unchecked")
113 public void actionPerformed( WebEvent event, ResponseHandler responseHandler )
114 throws IOException {
115
116 loc = getRequest().getLocale();
117
118 config = getCatalogueManagerConfiguration( event );
119 Map<String, String> param = event.getParameter();
120 String id = param.get( "ID" );
121 String format = param.get( "FORMAT" );
122 String cswAddress = param.get( "CSW" );
123 GetRecords getRecords = createGetRecords( id );
124 XMLFragment resultXML = null;
125 try {
126 resultXML = performQuery( getRecords, cswAddress );
127 } catch ( Exception e ) {
128 LOG.logError( e );
129 ExceptionBean bean = new ExceptionBean( this.getClass().getName(), e.getMessage() );
130 responseHandler.writeAndClose( true, bean );
131 return;
132 }
133
134 if ( "XML".equalsIgnoreCase( format ) ) {
135 writeXML( responseHandler, resultXML );
136 } else if ( "PDF".equalsIgnoreCase( format ) ) {
137 writePDF( event, responseHandler, resultXML );
138 } else {
139 writeHTML( responseHandler, cswAddress, resultXML );
140 }
141
142 }
143
144 private void writePDF( WebEvent event, ResponseHandler responseHandler, XMLFragment resultXML )
145 throws IOException {
146 Properties p = Messages.getProperties( loc );
147 XSLTDocument xsl = null;
148 try {
149 xsl = new XSLTDocument( config.getPdfXSL() );
150 XMLFragment xml = xsl.transform( resultXML );
151 List<Node> nodes = XMLTools.getNodes( xml.getRootElement(), "./*", CommonNamespaces.getNamespaceContext() );
152 for ( Node node : nodes ) {
153 if ( node instanceof Element ) {
154 String key = node.getLocalName();
155 String value = XMLTools.getStringValue( node );
156 p.put( key, value );
157 }
158 }
159
160 JRDataSource ds = new JREmptyDataSource();
161 String path = event.getAbsolutePath( "./WEB-INF/conf/cataloguemanager/reports/report1.jasper" );
162 byte[] result = JasperRunManager.runReportToPdf( path, p, ds );
163 HttpServletResponse resp = responseHandler.getHttpServletResponse();
164 resp.setContentType( "application/pdf" );
165 resp.setContentLength( result.length );
166 OutputStream os = resp.getOutputStream();
167 os.write( result );
168 os.flush();
169 os.close();
170 } catch ( Exception e ) {
171 LOG.logError( e );
172 ExceptionBean bean = new ExceptionBean( this.getClass().getName(), e.getMessage() );
173 responseHandler.writeAndClose( true, bean );
174 return;
175 }
176
177 }
178
179 private void writeHTML( ResponseHandler responseHandler, String cswAddress, XMLFragment resultXML )
180 throws IOException {
181 String html;
182 try {
183 html = formatResult( resultXML, cswAddress );
184 } catch ( Exception e ) {
185 LOG.logError( e );
186 ExceptionBean bean = new ExceptionBean( this.getClass().getName(), e.getMessage() );
187 responseHandler.writeAndClose( true, bean );
188 return;
189 }
190 String charEnc = Charset.defaultCharset().displayName();
191 responseHandler.setContentType( "application/json; charset=" + charEnc );
192 responseHandler.writeAndClose( html );
193 }
194
195 private void writeXML( ResponseHandler responseHandler, XMLFragment resultXML )
196 throws IOException {
197 String charEnc = Charset.defaultCharset().displayName();
198 responseHandler.setContentType( "text/xml; charset=" + charEnc );
199 String s = "<table width='900' cellpadding='2' border='0'><tr><td width='80'></td>";
200 s += ( "<td><textarea cols='97' rows='35'>" + resultXML.getAsPrettyString() + "</textarea></td>" );
201 s += "</tr><tr><td></td><td><a class='button' href='javascript:closeFullMD()'>";
202 s += ( Messages.get( loc, "CATMANAGE_RESULT_CLOSE" ) + "</a></td>" );
203 s += "</tr></table>";
204 responseHandler.writeAndClose( s );
205 }
206
207 /**
208 * @param resultXML
209 * @return
210 */
211 private String formatResult( XMLFragment resultXML, String cswAddress )
212 throws Exception {
213 URL xslURL = config.getFullHTMLXSL();
214 XSLTDocument xsl = new XSLTDocument( xslURL );
215 ByteArrayOutputStream bos = new ByteArrayOutputStream( 10000 );
216 DOMSource xmlSource = new DOMSource( resultXML.getRootElement() );
217 DOMSource xslSource = new DOMSource( xsl.getRootElement().getOwnerDocument(),
218 xsl.getSystemId() == null ? null : xsl.getSystemId().toString() );
219 StreamResult sr = new StreamResult( bos );
220 Map<String, String> param = new HashMap<String, String>();
221 param.put( "TITLE", Messages.get( loc, "CATMANAGE_RESULT_TITLE" ) );
222 param.put( "ABSTRACT", Messages.get( loc, "CATMANAGE_RESULT_ABSTRACT" ) );
223 param.put( "TOPICCATEGORY", Messages.get( loc, "CATMANAGE_RESULT_TOPICCATEGORY" ) );
224 param.put( "HIERARCHYLEVEL", Messages.get( loc, "CATMANAGE_RESULT_HIERARCHYLEVEL" ) );
225 param.put( "GEOGRDESC", Messages.get( loc, "CATMANAGE_RESULT_GEOGRDESC" ) );
226 param.put( "CREATIONDATE", Messages.get( loc, "CATMANAGE_RESULT_CREATIONDATE" ) );
227 param.put( "PUBLICATIONDATE", Messages.get( loc, "CATMANAGE_RESULT_PUBLICATIONDATE" ) );
228 param.put( "REVISIONDATE", Messages.get( loc, "CATMANAGE_RESULT_REVISIONDATE" ) );
229 param.put( "CONTACT", Messages.get( loc, "CATMANAGE_RESULT_CONTACT" ) );
230 param.put( "NAME", Messages.get( loc, "CATMANAGE_RESULT_NAME" ) );
231 param.put( "ORGANISATION", Messages.get( loc, "CATMANAGE_RESULT_ORGANISATION" ) );
232 param.put( "ADDRESS", Messages.get( loc, "CATMANAGE_RESULT_ADDRESS" ) );
233 param.put( "VOICE", Messages.get( loc, "CATMANAGE_RESULT_VOICE" ) );
234 param.put( "FAX", Messages.get( loc, "CATMANAGE_RESULT_FAX" ) );
235 param.put( "EMAIL", Messages.get( loc, "CATMANAGE_RESULT_EMAIL" ) );
236 param.put( "CLOSE", Messages.get( loc, "CATMANAGE_RESULT_CLOSE" ) );
237 param.put( "XML", Messages.get( loc, "CATMANAGE_RESULT_XML" ) );
238 param.put( "PDF", Messages.get( loc, "CATMANAGE_RESULT_PDF" ) );
239 param.put( "CSW", cswAddress );
240
241 XSLTDocument.transform( xmlSource, xslSource, sr, null, param );
242 return new String( bos.toByteArray() );
243 }
244
245 /**
246 * @param cswAddress
247 * @return
248 */
249 private XMLFragment performQuery( GetRecords getRecords, String cswAddress )
250 throws Exception {
251 XMLFragment gr = XMLFactory.exportWithVersion( getRecords );
252 LOG.logDebug( "GetRecords: ", gr.getAsPrettyString() );
253 HttpMethod method = HttpUtils.performHttpPost( cswAddress, gr, 60000, null, null, null );
254 XMLFragment xml = new XMLFragment();
255 xml.load( method.getResponseBodyAsStream(), cswAddress );
256 if ( LOG.getLevel() == ILogger.LOG_DEBUG ) {
257 LOG.logDebug( "GetRecords result: ", xml.getAsPrettyString() );
258 }
259 return xml;
260 }
261
262 /**
263 * @return
264 */
265 private GetRecords createGetRecords( String id ) {
266 QualifiedName qn = new QualifiedName( "ResourceIdentifier",
267 URI.create( "http://www.opengis.net/cat/csw/apiso/1.0" ) );
268 PropertyName propertyName = new PropertyName( qn );
269 PropertyIsLikeOperation operation1 = new PropertyIsLikeOperation( propertyName, new Literal( '*' + id + '*' ),
270 '*', '?', '/' );
271 qn = new QualifiedName( "Identifier", URI.create( "http://www.opengis.net/cat/csw/apiso/1.0" ) );
272 propertyName = new PropertyName( qn );
273 PropertyIsLikeOperation operation2 = new PropertyIsLikeOperation( propertyName, new Literal( '*' + id + '*' ),
274 '*', '?', '/' );
275 List<Operation> list = new ArrayList<Operation>();
276 list.add( operation1 );
277 list.add( operation2 );
278 Filter filter = new ComplexFilter( new LogicalOperation( OperationDefines.OR, list ) );
279 List<QualifiedName> typeNames = new ArrayList<QualifiedName>();
280 typeNames.add( new QualifiedName( "{http://www.isotc211.org/2005/gmd}:MD_Metadata" ) );
281 Query query = new Query( "full", new ArrayList<QualifiedName>(), new HashMap<String, QualifiedName>(),
282 new ArrayList<PropertyPath>(), filter, null, typeNames,
283 new HashMap<String, QualifiedName>() );
284 return new GetRecords( UUID.randomUUID().toString(), "2.0.2", null, null, RESULT_TYPE.RESULTS,
285 "application/xml", "http://www.isotc211.org/2005/gmd", 1, config.getStepSize() * 2, -1,
286 null, query );
287 }
288
289 }