    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/standard/wfs/control/InitDigitizerModuleListener.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.wfs.control;
    038
    039    import java.io.BufferedReader;
    040    import java.io.FileReader;
    041    import java.io.IOException;
    042    import java.net.URI;
    043    import java.net.URISyntaxException;
    044
    045    import javax.servlet.http.HttpServletRequest;
    046    import javax.servlet.http.HttpSession;
    047
    048    import org.deegree.datatypes.QualifiedName;
    049    import org.deegree.enterprise.control.AbstractListener;
    050    import org.deegree.enterprise.control.FormEvent;
    051    import org.deegree.framework.log.ILogger;
    052    import org.deegree.framework.log.LoggerFactory;
    053    import org.deegree.framework.util.Parameter;
    054    import org.deegree.framework.util.ParameterList;
    055    import org.deegree.i18n.Messages;
    056    import org.deegree.portal.context.AbstractFrontend;
    057    import org.deegree.portal.context.GeneralExtension;
    058    import org.deegree.portal.context.Module;
    059    import org.deegree.portal.context.ViewContext;
    060    import org.deegree.portal.standard.wfs.WFSClientException;
    061    import org.deegree.portal.standard.wfs.configuration.DigitizerClientConfiguration;
    062
    063    /**
    064     * TODO describe function and usage of the class here.
    065     *
    066     * @author <a href="mailto:mays@lat-lon.de">Judit Mays</a>
    067     * @author last edited by: $Author: mschneider $
    068     *
    069     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
    070     */
    071    public class InitDigitizerModuleListener extends AbstractListener {
    072
    073        private static final ILogger LOG = LoggerFactory.getLogger( InitDigitizerModuleListener.class );
    074
    075        private final String moduleName = "DigitizerModule";
    076
    077        private static final String DIGITIZER_CLIENT_CONFIGURATION = "DIGITIZER_CLIENT_CONFIGURATION";
    078
    079        private static final String FEATURE_TYPES = "featureTypes";
    080
    081        private static final String WFS_ADDRESSES = "wfsAddresses";
    082
    083        private static final String FORM_TEMPLATES = "formTemplates";
    084
    085        private static final String WFS_INSERT_TEMPLATES = "wfsInsertTemplates";
    086
    087        private static final String WFS_UPDATE_TEMPLATES = "wfsUpdateTemplates";
    088
    089        private static final String WFS_DELETE_TEMPLATES = "wfsDeleteTemplates";
    090
    091        /*
    092         * (non-Javadoc)
    093         *
    094         * @see org.deegree.enterprise.control.AbstractListener#actionPerformed(org.deegree.enterprise.control.FormEvent)
    095         */
    096        @Override
    097        public void actionPerformed( FormEvent event ) {
    098
    099            HttpSession session = ( (HttpServletRequest) getRequest() ).getSession();
    100            ViewContext vc = (ViewContext) session.getAttribute( org.deegree.portal.Constants.CURRENTMAPCONTEXT );
    101            GeneralExtension ge = vc.getGeneral().getExtension();
    102
    103            Module module = null;
    104            DigitizerClientConfiguration config = new DigitizerClientConfiguration();
    105
    106            try {
    107                module = findDigitizerModule( ge );
    108                initConfig( config, module );
    109            } catch ( Exception e ) {
    110                LOG.logError( e.getLocalizedMessage(), e );
    111                gotoErrorPage( Messages.getMessage( "IGEO_STD_CLIENT_ERROR", e.getLocalizedMessage() ) );
    112                return;
    113            }
    114
    115            session.setAttribute( DIGITIZER_CLIENT_CONFIGURATION, config );
    116        }
    117
    118        /**
    119         * Gets the first digitizer module (search order: north-east-south-west-center).
    120         *
    121         * @param ge
    122         * @return the (first) digitizer module
    123         * @throws WFSClientException
    124         *             if no digitizer module can be found in the passed ge.
    125         */
    126        private Module findDigitizerModule( GeneralExtension ge )
    127                                throws WFSClientException {
    128
    129            AbstractFrontend fe = (AbstractFrontend) ge.getFrontend();
    130            Module[] modules = fe.getModulesByName( moduleName );
    131            if ( modules.length > 0 ) {
    132                return modules[0];
    133            } else {
    134                throw new WFSClientException( Messages.getMessage( "IGEO_STD_WFS_MISSING_MODULE", moduleName ) );
    135            }
    136        }
    137
    138        /**
    139         * Initialisation of module configuration. The fields featureTypeToAddressMap and featureTypeToTemplateMap in
    140         * <code>config</code> are initialized with the values from the passed module. If the module configuration is
    141         * invalid, an exception is thrown.
    142         *
    143         * @param config
    144         *            the client configuration object
    145         * @param module
    146         *            the module as configured in the current map context.
    147         * @throws WFSClientException
    148         *             if the module configuration is invalid.
    149         */
    150        private void initConfig( DigitizerClientConfiguration config, Module module )
    151                                throws WFSClientException {
    152
    153            ParameterList paramList = module.getParameter();
    154            String[] wfsAddrs = null;
    155            String[] forms = null;
    156            String[] insertTempls = null;
    157            String[] fTypes = null;
    158
    159            fTypes = extractMandatoryMultiValues( paramList, FEATURE_TYPES );
    160            wfsAddrs = extractMandatoryMultiValues( paramList, WFS_ADDRESSES );
    161            forms = extractMandatoryMultiValues( paramList, FORM_TEMPLATES );
    162            insertTempls = extractMandatoryMultiValues( paramList, WFS_INSERT_TEMPLATES );
    163
    164            validateConfig( fTypes, wfsAddrs, forms, insertTempls );
    165
    166            QualifiedName[] qualiNames = createQualifiedNamesFromFeatureTypes( fTypes );
    167
    168            for ( int i = 0; i < fTypes.length; i++ ) {
    169                config.addFeatureTypeAddress( qualiNames[i], wfsAddrs[i] );
    170                config.addFeatureTypeFormTemplate( qualiNames[i], forms[i] );
    171                config.addFeatureTypeInsertTemplate( qualiNames[i], insertTempls[i] );
    172            }
    173
    174            initOptionalConfig( config, module );
    175        }
    176
    177        /**
    178         * @param config
    179         * @param module
    180         * @throws WFSClientException
    181         */
    182        private void initOptionalConfig( DigitizerClientConfiguration config, Module module )
    183                                throws WFSClientException {
    184
    185            ParameterList paramList = module.getParameter();
    186            // String[] wfsAddrs = null;
    187            // String[] forms = null;
    188            String[] updateTemplates = null;
    189            String[] deleteTemplates = null;
    190            String[] fTypes = null;
    191
    192            fTypes = extractMandatoryMultiValues( paramList, FEATURE_TYPES );
    193            updateTemplates = extractOptionalMultiValues( paramList, WFS_UPDATE_TEMPLATES );
    194            deleteTemplates = extractOptionalMultiValues( paramList, WFS_DELETE_TEMPLATES );
    195
    196            if ( updateTemplates != null ) {
    197                // validate updateTemplate(s)
    198                for ( int i = 0; i < updateTemplates.length; i++ ) {
    199                    StringBuffer template = new StringBuffer( 10000 );
    200                    String path = getHomePath() + updateTemplates[i];
    201                    try {
    202                        BufferedReader br = new BufferedReader( new FileReader( path ) );
    203                        String line = null;
    204                        while ( ( line = br.readLine() ) != null ) {
    205                            template.append( line );
    206                        }
    207                        br.close();
    208                    } catch ( IOException e ) {
    209                        LOG.logError( e.getLocalizedMessage(), e );
    210                        throw new WFSClientException( Messages.getMessage( "IGEO_STD_WFS_WRONG_TEMPLATE",
    211                                                                           updateTemplates[i] ) );
    212                    }
    213                }
    214            }
    215            if ( deleteTemplates != null ) {
    216                // validate deleteTemplate(s)
    217                for ( int i = 0; i < deleteTemplates.length; i++ ) {
    218                    StringBuffer template = new StringBuffer( 10000 );
    219                    String path = getHomePath() + deleteTemplates[i];
    220                    try {
    221                        BufferedReader br = new BufferedReader( new FileReader( path ) );
    222                        String line = null;
    223                        while ( ( line = br.readLine() ) != null ) {
    224                            template.append( line );
    225                        }
    226                        br.close();
    227                    } catch ( IOException e ) {
    228                        LOG.logError( e.getLocalizedMessage(), e );
    229                        throw new WFSClientException( Messages.getMessage( "IGEO_STD_WFS_WRONG_TEMPLATE",
    230                                                                           deleteTemplates[i] ) );
    231                    }
    232                }
    233            }
    234            QualifiedName[] qualiNames = createQualifiedNamesFromFeatureTypes( fTypes );
    235
    236            for ( int i = 0; i < fTypes.length; i++ ) {
    237                if ( updateTemplates != null ) {
    238                    config.addFeatureTypeUpdateTemplate( qualiNames[i], updateTemplates[i] );
    239                }
    240                if ( deleteTemplates != null ) {
    241                    config.addFeatureTypeDeleteTemplate( qualiNames[i], deleteTemplates[i] );
    242                }
    243            }
    244        }
    245
    246        /**
    247         * @param paramList
    248         * @param parameter
    249         * @return Returns a String[] containing all values (separated by ";") for the passed parameter.
    250         * @throws WFSClientException
    251         *             if the mandatory parameter is not part of the parameter list.
    252         */
    253        private String[] extractMandatoryMultiValues( ParameterList paramList, String parameter )
    254                                throws WFSClientException {
    255            String multiValues = null;
    256            try {
    257                multiValues = (String) paramList.getParameter( parameter ).getValue();
    258            } catch ( Exception e ) {
    259                LOG.logError( e.getLocalizedMessage(), e );
    260                throw new WFSClientException( Messages.getMessage( "IGEO_STD_MISSING_MAND_PARAM", parameter ) );
    261            }
    262            if ( multiValues.startsWith( "'" ) && multiValues.endsWith( "'" ) ) {
    263                // strip ' from front and end of string
    264                multiValues = multiValues.substring( 1, multiValues.length() - 1 );
    265            }
    266
    267            return multiValues.split( ";" );
    268        }
    269
    270        /**
    271         * @param paramList
    272         * @param parameter
    273         * @return Returns a String[] containing all values (separated by ";") for the passed parameter, or null
    274         */
    275        private String[] extractOptionalMultiValues( ParameterList paramList, String parameter ) {
    276
    277            Parameter param = paramList.getParameter( parameter );
    278            if ( param == null ) {
    279                return null;
    280            }
    281            String optValues = (String) param.getValue();
    282            if ( optValues.startsWith( "'" ) && optValues.endsWith( "'" ) ) {
    283                // strip ' from front and end of string
    284                optValues = optValues.substring( 1, optValues.length() - 1 );
    285            }
    286            return optValues.split( ";" );
    287        }
    288
    289        /**
    290         * @param featureTypes
    291         * @param wfsAddrs
    292         * @param formTemplates
    293         * @param insertTemplates
    294         * @throws WFSClientException
    295         */
    296        private void validateConfig( String[] featureTypes, String[] wfsAddrs, String[] formTemplates,
    297                                     String[] insertTemplates )
    298                                throws WFSClientException {
    299
    300            // validate number of param values
    301            if ( featureTypes.length != wfsAddrs.length ) {
    302                throw new WFSClientException( Messages.getMessage( "IGEO_STD_WFS_WRONG_PARAMS_NUM", FEATURE_TYPES,
    303                                                                   WFS_ADDRESSES ) );
    304            } else if ( featureTypes.length != formTemplates.length ) {
    305                throw new WFSClientException( Messages.getMessage( "IGEO_STD_WFS_WRONG_PARAMS_NUM", FEATURE_TYPES,
    306                                                                   FORM_TEMPLATES ) );
    307            } else if ( featureTypes.length != insertTemplates.length ) {
    308                throw new WFSClientException( Messages.getMessage( "IGEO_STD_WFS_WRONG_PARAMS_NUM", FEATURE_TYPES,
    309                                                                   WFS_INSERT_TEMPLATES ) );
    310            }
    311
    312            // validate featureTypes as fully qualified names
    313            createQualifiedNamesFromFeatureTypes( featureTypes );
    314
    315            // TODO validate module configuration
    316            // TODO validate wfsAddress(es).
    317            // TODO validate formTemplate(s): do they exist in the file system?
    318
    319            // validate insertTemplate(s)
    320            for ( int i = 0; i < insertTemplates.length; i++ ) {
    321                StringBuffer template = new StringBuffer( 10000 );
    322                String path = getHomePath() + insertTemplates[i];
    323                try {
    324                    BufferedReader br = new BufferedReader( new FileReader( path ) );
    325                    String line = null;
    326                    while ( ( line = br.readLine() ) != null ) {
    327                        template.append( line );
    328                    }
    329                    br.close();
    330                } catch ( IOException e ) {
    331                    LOG.logError( e.getLocalizedMessage(), e );
    332                    throw new WFSClientException( Messages.getMessage( "IGEO_STD_WFS_WRONG_TEMPLATE", insertTemplates[i] ) );
    333                }
    334            }
    335
    336        }
    337
    338        /**
    339         * @param featureTypes
    340         * @return an array of QualifiedNames for the passed featureTypes
    341         * @throws WFSClientException
    342         *             if featureTypes can not be transformed to <code>QualifiedName</code>s.
    343         */
    344        private QualifiedName[] createQualifiedNamesFromFeatureTypes( String[] featureTypes )
    345                                throws WFSClientException {
    346            QualifiedName[] qualifiedNames = null;
    347            qualifiedNames = new QualifiedName[featureTypes.length];
    348
    349            for ( int i = 0; i < featureTypes.length; i++ ) {
    350                // featureTypes[i]={http://hau.mich.blau.de/gruen}:Farbe
    351                String ns = featureTypes[i].substring( ( 1 + featureTypes[i].indexOf( "{" ) ),
    352                                                       featureTypes[i].indexOf( "}:" ) );
    353                String ftName = featureTypes[i].substring( 2 + featureTypes[i].indexOf( "}:" ) );
    354
    355                try {
    356                    qualifiedNames[i] = new QualifiedName( null, ftName, new URI( ns ) );
    357                } catch ( URISyntaxException e ) {
    358                    LOG.logError( e.getLocalizedMessage(), e );
    359                    throw new WFSClientException( Messages.getMessage( "IGEO_STD_WFS_INVALID_NS", featureTypes[i], ns ) );
    360                }
    361            }
    362            return qualifiedNames;
    363        }
    364
    365    }