001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/portal/standard/csw/control/InitCSWModuleListener.java $
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    
037    package org.deegree.portal.standard.csw.control;
038    
039    import java.util.ArrayList;
040    import java.util.HashMap;
041    import java.util.List;
042    
043    import javax.servlet.http.HttpServletRequest;
044    import javax.servlet.http.HttpSession;
045    
046    import org.deegree.enterprise.control.AbstractListener;
047    import org.deegree.enterprise.control.FormEvent;
048    import org.deegree.framework.log.ILogger;
049    import org.deegree.framework.log.LoggerFactory;
050    import org.deegree.framework.util.ParameterList;
051    import org.deegree.i18n.Messages;
052    import org.deegree.portal.context.AbstractFrontend;
053    import org.deegree.portal.context.GeneralExtension;
054    import org.deegree.portal.context.Module;
055    import org.deegree.portal.context.ViewContext;
056    import org.deegree.portal.standard.csw.CatalogClientException;
057    import org.deegree.portal.standard.csw.configuration.CSWClientConfiguration;
058    
059    /**
060     * This classes initializes the configurations of the the CSW-module from the WMC to be used by the CSW client
061     * 
062     * @author <a href="mailto:mays@lat-lon.de">Judit Mays</a>
063     * @author last edited by: $Author: jmays $
064     * 
065     * @version $Revision: 19071 $, $Date: 2009-08-12 16:42:35 +0200 (Mi, 12. Aug 2009) $
066     */
067    public class InitCSWModuleListener extends AbstractListener {
068    
069        private static final ILogger LOG = LoggerFactory.getLogger( InitCSWModuleListener.class );
070    
071        @Override
072        public void actionPerformed( FormEvent event ) {
073    
074            HttpSession session = ( (HttpServletRequest) getRequest() ).getSession();
075            ViewContext vc = (ViewContext) session.getAttribute( org.deegree.portal.Constants.CURRENTMAPCONTEXT );
076            GeneralExtension gen = vc.getGeneral().getExtension();
077    
078            Module module = null;
079    
080            try {
081                module = findCswClientModule( gen );
082            } catch ( Exception e ) {
083                LOG.logError( "Error in findCswClientModule: " + e.getMessage() );
084                gotoErrorPage( Messages.getMessage( "IGEO_STD_CSW_CLIENT_ERROR", e.getMessage() ) );
085                return;
086            }
087    
088            CSWClientConfiguration config = new CSWClientConfiguration();
089    
090            ParameterList parList = module.getParameter();
091    
092            try {
093                initConfig( config, parList );
094            } catch ( CatalogClientException e ) {
095                LOG.logError( "Error when initializing: " + e.getMessage() );
096                gotoErrorPage( Messages.getMessage( "IGEO_STD_CSW_CLIENT_ERROR", e.getMessage() ) );
097                return;
098            }
099    
100            // srs is available from context
101            String srs = "EPSG:4236";
102            srs = vc.getGeneral().getBoundingBox()[0].getCoordinateSystem().getIdentifier();
103            config.setSrs( srs );
104    
105            session.setAttribute( Constants.CSW_CLIENT_CONFIGURATION, config );
106    
107            return;
108        }
109    
110        /**
111         * Extracts all the needed configurations from the WMC csw-module and fills the config nstance with them
112         * 
113         * @param config
114         * @param parList
115         * @throws CatalogClientException
116         */
117        protected void initConfig( CSWClientConfiguration config, ParameterList parList )
118                                throws CatalogClientException {
119    
120            String value = extractOptionalSingleValue( parList, "maxRecords" );
121            if ( value != null ) {
122                config.setMaxRecords( Integer.valueOf( value ).intValue() );
123            }
124    
125            String[][] kvp = null;
126    
127            String[] profileNames = extractMandatoryProfileNames( parList );
128            for ( int i = 0; i < profileNames.length; i++ ) {
129                HashMap<String, String> keyToXSL = new HashMap<String, String>();
130                String[] profileValues = extractOptionalMultiValues( parList, profileNames[i] );
131                kvp = extractKvpFromParamsList( profileValues );
132                for ( int j = 0; j < kvp[0].length; j++ ) {
133                    keyToXSL.put( kvp[0][j], kvp[1][j] ); // elementSetName=kvp[0][j],xslFile=kvp[1][j]
134                }
135                config.addProfileXSL( profileNames[i], keyToXSL );
136            }
137    
138            String[] catalogueValues = extractMandatoryMultiValues( parList, "Catalogues" );
139            kvp = extractKvpFromParamsList( catalogueValues );
140            for ( int i = 0; i < kvp[0].length; i++ ) {
141                config.addCatalogueURL( kvp[0][i], kvp[1][i] );
142            }
143    
144            String[] protocolValues = extractMandatoryMultiValues( parList, "Protocols" );
145            kvp = extractKvpFromParamsList( protocolValues );
146            for ( int i = 0; i < kvp[0].length; i++ ) {
147                String[] protocols = kvp[1][i].split( "," );
148                List<String> list = new ArrayList<String>( protocols.length );
149                for ( int j = 0; j < protocols.length; j++ ) {
150                    list.add( protocols[j] );
151                }
152                config.addCatalogueProtocol( kvp[0][i], list );
153            }
154    
155            String[] formatValues = extractMandatoryMultiValues( parList, "Formats" );
156            kvp = extractKvpFromParamsList( formatValues );
157            for ( int i = 0; i < kvp[0].length; i++ ) {
158                String[] formats = kvp[1][i].split( "," );
159                List<String> list = new ArrayList<String>( formats.length );
160                for ( int j = 0; j < formats.length; j++ ) {
161                    list.add( formats[j] );
162                }
163                config.addCatalogueFormat( kvp[0][i], list );
164            }
165    
166            // path to mapContextTemplate
167            // is needed for shopping cart, but shopping cart is currently disabled.
168            // TODO comment in again, if shopping cart is enabled again.
169            // config.setMapContextTemplatePath( extractMandatorySingleValue( parList, "mapContextTemplate" ) );
170    
171            // all namspace bindings
172            // config.setNamespaceBindings( extractMandatoryMultiValues( parList, "namespaceBindings" ) );
173    
174            // xPath in data catalog
175            config.setXPathToDataIdentifier( extractMandatorySingleValue( parList, "XPathToDataId" ) );
176            config.setXPathToDataTitle( extractMandatorySingleValue( parList, "XPathToDataTitle" ) );
177    
178            // xPath in service catalog
179            config.setXPathToServiceIdentifier( extractMandatorySingleValue( parList, "XPathToServiceId" ) );
180            config.setXPathToServiceTitle( extractMandatorySingleValue( parList, "XPathToServiceTitle" ) );
181            config.setXPathToServiceOperatesOnTitle( extractMandatorySingleValue( parList, "XPathToServiceOperatesOnTitle" ) );
182            config.setXPathToServiceAddress( extractMandatorySingleValue( parList, "XPathToServiceAddress" ) );
183            config.setXPathToServiceType( extractMandatorySingleValue( parList, "XPathToServiceType" ) );
184            config.setXPathToServiceTypeVersion( extractMandatorySingleValue( parList, "XPathToServiceTypeVersion" ) );
185    
186            /*
187             * TODO implement or delete initialBbox String initialBbox = (String)parList.getParameter("InitialBbox"
188             * ).getValue(); initialBbox = initialBbox.substring(1, initialBbox.length() - 1 ); Envelope env =
189             * createBboxFromString( initialBbox );
190             */
191        }
192    
193        /**
194         * Extracts the profile name parameter
195         * 
196         * @param paramList
197         * @return Returns a String[] containing all profile names from the passed parameter list.
198         * @throws CatalogClientException
199         *             if the mandatory parameter is not part of the parameter list.
200         */
201        private String[] extractMandatoryProfileNames( ParameterList paramList )
202                                throws CatalogClientException {
203    
204            String[] paramNames = paramList.getParameterNames();
205            List<String> profileNames = new ArrayList<String>( paramNames.length );
206    
207            for ( int i = 0; i < paramNames.length; i++ ) {
208                if ( paramNames[i].startsWith( "Profiles." ) ) {
209                    profileNames.add( paramNames[i] );
210                }
211            }
212            if ( profileNames.size() < 1 ) {
213                throw new CatalogClientException( Messages.getMessage( "IGEO_STD_CSW_MISSING_PROFILE" ) );
214            }
215    
216            return profileNames.toArray( new String[profileNames.size()] );
217        }
218    
219        /**
220         * Extracts optional single parameter from the WMC csw-client
221         * 
222         * @param paramList
223         * @param parameter
224         * @return Returns the value for the passed parameter. May return null, if the parameter is not part of the passed
225         *         parameter list.
226         */
227        private String extractOptionalSingleValue( ParameterList paramList, String parameter ) {
228    
229            String value = null;
230            if ( paramList.getParameter( parameter ) != null ) {
231                value = (String) paramList.getParameter( parameter ).getValue();
232    
233                if ( value.startsWith( "'" ) && value.endsWith( "'" ) ) {
234                    // strip ' from front and end of string
235                    value = value.substring( 1, value.length() - 1 );
236                }
237            }
238            return value;
239        }
240    
241        /**
242         * Extracts optional comma separated multivalue parameter from the WMC csw-client
243         * 
244         * @param paramList
245         * @param parameter
246         * @return Returns a String[] containing all values (separated at ";") for the passed parameter. May return null, if
247         *         the parameter is not part of the passed parameter list.
248         */
249        private String[] extractOptionalMultiValues( ParameterList paramList, String parameter ) {
250    
251            String multiValues = null;
252            if ( paramList.getParameter( parameter ) != null ) {
253                multiValues = (String) paramList.getParameter( parameter ).getValue();
254    
255                if ( multiValues.startsWith( "'" ) && multiValues.endsWith( "'" ) ) {
256                    // strip ' from front and end of string
257                    multiValues = multiValues.substring( 1, multiValues.length() - 1 );
258                }
259            }
260            return multiValues == null ? null : multiValues.split( ";" );
261        }
262    
263        /**
264         * Extracts a mandatory parameter as a String from the WMC csw-module
265         * 
266         * @param paramList
267         * @param parameter
268         * @return Returns the single value for the passed parameter.
269         * @throws CatalogClientException
270         *             if the mandatory parameter is not part of the parameter list.
271         */
272        private String extractMandatorySingleValue( ParameterList paramList, String parameter )
273                                throws CatalogClientException {
274            String value = null;
275            try {
276                value = (String) paramList.getParameter( parameter ).getValue();
277            } catch ( Exception e ) {
278                throw new CatalogClientException( Messages.getMessage( "IGEO_STD_CSW_MISSING_MAND_PARAM", parameter ) );
279            }
280            if ( value.startsWith( "'" ) && value.endsWith( "'" ) ) {
281                value = value.substring( 1, value.length() - 1 ); // strip ' from front and end of string
282            }
283    
284            return value;
285        }
286    
287        /**
288         * Extracts comma separated values from the a given parameter in the WMC csw-client
289         * 
290         * @param paramList
291         * @param parameter
292         * @return Returns a String[] containing all values (separated by ";") for the passed parameter.
293         * @throws CatalogClientException
294         *             if the mandatory parameter is not part of the parameter list.
295         */
296        private String[] extractMandatoryMultiValues( ParameterList paramList, String parameter )
297                                throws CatalogClientException {
298            String multiValues = null;
299            try {
300                multiValues = (String) paramList.getParameter( parameter ).getValue();
301            } catch ( Exception e ) {
302                throw new CatalogClientException( Messages.getMessage( "IGEO_STD_CSW_MISSING_MAND_PARAM", parameter ) );
303            }
304            if ( multiValues.startsWith( "'" ) && multiValues.endsWith( "'" ) ) {
305                // strip ' from front and end of string
306                multiValues = multiValues.substring( 1, multiValues.length() - 1 );
307            }
308    
309            return multiValues.split( ";" );
310        }
311    
312        /**
313         * Extract the key/value pair parameters and returns a two dimensional array with their value
314         * 
315         * @param values
316         * @return Returns the key value pairs for the passed values from the parameter list.
317         */
318        private String[][] extractKvpFromParamsList( String[] values ) {
319    
320            String[][] kvp = new String[2][];
321            // kvp[0][i] = key[i]
322            // kvp[1][i] = value[i]
323    
324            kvp[0] = new String[values.length];
325            kvp[1] = new String[values.length];
326    
327            // FIXME unsafe! assuming AOK, but should catch exceptions for the split at "|"
328            for ( int i = 0; i < values.length; i++ ) {
329                String[] tmpKVP = values[i].split( "\\|" );
330                kvp[0][i] = tmpKVP[0];
331                kvp[1][i] = tmpKVP[1];
332            }
333    
334            return kvp;
335        }
336    
337        /**
338         * Extracts the csw module from the GeneralExtension in the WebMapContext
339         * 
340         * @param gen
341         *            the general extension of the WMC in which to search for a given module
342         * @return Returns the (first) csw client module found in one of the GUI areas. Search order is north, east, south,
343         *         west, center.
344         * @throws CatalogClientException
345         *             if the csw client module cannot be found.
346         */
347        protected Module findCswClientModule( GeneralExtension gen )
348                                throws CatalogClientException {
349            final String moduleName = "CswModule";
350    
351            AbstractFrontend fe = (AbstractFrontend) gen.getFrontend();
352            Module[] mods = fe.getModulesByName( moduleName );
353            if ( mods.length > 0 ) {
354                return mods[0];
355            } else {
356                LOG.logError( Messages.getMessage( "IGEO_STD_CSW_MISSING_MODULE" ) );
357                throw new CatalogClientException( Messages.getMessage( "IGEO_STD_CSW_MISSING_MODULE" ) );
358            }
359        }
360    }