001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/portal/standard/csw/control/DetailedSearchListener.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.List;
041    
042    import javax.servlet.http.HttpServletRequest;
043    import javax.servlet.http.HttpSession;
044    
045    import org.deegree.enterprise.control.FormEvent;
046    import org.deegree.enterprise.control.RPCMember;
047    import org.deegree.enterprise.control.RPCParameter;
048    import org.deegree.enterprise.control.RPCStruct;
049    import org.deegree.enterprise.control.RPCWebEvent;
050    import org.deegree.i18n.Messages;
051    import org.deegree.model.crs.CRSFactory;
052    import org.deegree.model.crs.CoordinateSystem;
053    import org.deegree.model.crs.UnknownCRSException;
054    import org.deegree.model.spatialschema.Envelope;
055    import org.deegree.model.spatialschema.GeometryFactory;
056    import org.deegree.portal.standard.csw.CatalogClientException;
057    import org.deegree.portal.standard.csw.model.DataSessionRecord;
058    
059    /**
060     * A <code>${type_name}</code> class.<br/>
061     *
062     * TODO class description
063     *
064     * @author <a href="mailto:mays@lat-lon.de">Judit Mays</a>
065     * @author last edited by: $Author: mschneider $
066     *
067     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
068     */
069    public class DetailedSearchListener extends SimpleSearchListener {
070    
071        /**
072         * param used in jsp pages
073         */
074        public static final String SESSION_DETAILEDSEARCHPARAM = "DETAILEDSEARCHPARAM";
075    
076        protected Envelope bbox = null;
077    
078        /*
079         * (non-Javadoc)
080         *
081         * @see org.deegree.enterprise.control.WebListener#actionPerformed(org.deegree.enterprise.control.FormEvent)
082         */
083        @Override
084        public void actionPerformed( FormEvent event ) {
085    
086            super.actionPerformed( event );
087    
088            if ( bbox != null ) {
089                HttpSession session = ( (HttpServletRequest) this.getRequest() ).getSession( true );
090    
091                List dsrList = (ArrayList) session.getAttribute( SESSION_DATARECORDS );
092                if ( dsrList != null && dsrList.size() > 0 ) {
093                    dsrList = addBoundingBox( dsrList, bbox );
094                }
095                session.setAttribute( SESSION_DATARECORDS, dsrList );
096            }
097        }
098    
099        /*
100         * (non-Javadoc)
101         *
102         * @see org.deegree.portal.standard.csw.control.SimpleSearchListener#validateRequest(org.deegree.enterprise.control.RPCWebEvent)
103         *
104         * validates the request to be performed. @param rpcEvent event object containing the request to be performed
105         */
106        @Override
107        protected void validateRequest( RPCWebEvent rpcEvent )
108                                throws CatalogClientException {
109    
110            // validity check for common search variables
111            super.validateRequest( rpcEvent );
112    
113            // validity check for specific detailed search variables
114            RPCStruct struct = extractRPCStruct( rpcEvent, 1 );
115    
116            // validity check for date values
117            RPCMember dateFromMem = struct.getMember( Constants.RPC_DATEFROM );
118            RPCMember dateToMem = struct.getMember( Constants.RPC_DATETO );
119            validateDates( dateFromMem, dateToMem );
120    
121            // create envelope if a bounding box was part of the request
122            if ( struct.getMember( Constants.RPC_BBOX ) != null ) {
123    
124                RPCStruct bboxStruct = (RPCStruct) extractRPCMember( struct, Constants.RPC_BBOX );
125    
126                Double minx = (Double) extractRPCMember( bboxStruct, Constants.RPC_BBOXMINX );
127                Double miny = (Double) extractRPCMember( bboxStruct, Constants.RPC_BBOXMINY );
128                Double maxx = (Double) extractRPCMember( bboxStruct, Constants.RPC_BBOXMAXX );
129                Double maxy = (Double) extractRPCMember( bboxStruct, Constants.RPC_BBOXMAXY );
130    
131                // FIXME check if srs is correct
132                CoordinateSystem srs;
133                try {
134                    srs = CRSFactory.create( config.getSrs() );
135                } catch ( UnknownCRSException e ) {
136                    throw new CatalogClientException( e.getMessage(), e );
137                }
138    
139                bbox = GeometryFactory.createEnvelope( minx.doubleValue(), miny.doubleValue(), maxx.doubleValue(),
140                                                       maxy.doubleValue(), srs );
141            }
142    
143            // write request parameter into session to reconstruct the search form
144            RPCParameter[] params = extractRPCParameters( rpcEvent );
145            HttpSession session = ( (HttpServletRequest) this.getRequest() ).getSession( true );
146            session.setAttribute( SESSION_DETAILEDSEARCHPARAM, params );
147    
148        }
149    
150        /**
151         * Checks, whether day values are smaler than 32, whether month values are smaller than 13 and whether the
152         * year(from) is smaller than year(to). If not, a CatalogClientException is thrown.
153         *
154         * @param dateFromMem
155         * @param dateToMem
156         * @throws CatalogClientException
157         */
158        private void validateDates( RPCMember dateFromMem, RPCMember dateToMem )
159                                throws CatalogClientException {
160    
161            Integer fromYear = null;
162            if ( dateFromMem != null ) {
163                RPCStruct st = (RPCStruct) dateFromMem.getValue();
164                if ( st.getMember( Constants.RPC_YEAR ) != null ) {
165                    try {
166                        fromYear = Integer.valueOf( st.getMember( Constants.RPC_YEAR ).getValue().toString() );
167                    } catch ( Exception e ) {
168                        throw new CatalogClientException( Messages.getMessage( "IGEO_STD_CSW_INVALID_PARAM",
169                                                                               Constants.RPC_YEAR ) );
170                    }
171                }
172                if ( st.getMember( Constants.RPC_MONTH ) != null ) {
173                    try {
174                        Integer fromMonth = Integer.valueOf( st.getMember( Constants.RPC_MONTH ).getValue().toString() );
175                        if ( fromMonth.intValue() > 12 || fromMonth.intValue() < 1 ) {
176                            throw new Exception();
177                        }
178                    } catch ( Exception e ) {
179                        throw new CatalogClientException( Messages.getMessage( "IGEO_STD_CSW_INVALID_PARAM",
180                                                                               Constants.RPC_MONTH ) );
181                    }
182                }
183                if ( st.getMember( Constants.RPC_DAY ) != null ) {
184                    try {
185                        Integer fromDay = Integer.valueOf( st.getMember( Constants.RPC_DAY ).getValue().toString() );
186                        if ( fromDay.intValue() > 31 || fromDay.intValue() < 1 ) {
187                            throw new Exception();
188                        }
189                    } catch ( Exception e ) {
190                        throw new CatalogClientException( Messages.getMessage( "IGEO_STD_CSW_INVALID_PARAM",
191                                                                               Constants.RPC_DAY ) );
192                    }
193                }
194            }
195    
196            Integer toYear = null;
197            if ( dateToMem != null ) {
198                RPCStruct st = (RPCStruct) dateToMem.getValue();
199                if ( st.getMember( Constants.RPC_YEAR ) != null ) {
200                    try {
201                        toYear = Integer.valueOf( st.getMember( Constants.RPC_YEAR ).getValue().toString() );
202                    } catch ( Exception e ) {
203                        throw new CatalogClientException( Messages.getMessage( "IGEO_STD_CSW_INVALID_PARAM",
204                                                                               Constants.RPC_YEAR ) );
205                    }
206                }
207                if ( st.getMember( Constants.RPC_MONTH ) != null ) {
208                    try {
209                        Integer toMonth = Integer.valueOf( st.getMember( Constants.RPC_MONTH ).getValue().toString() );
210                        if ( toMonth.intValue() > 12 || toMonth.intValue() < 1 ) {
211                            throw new Exception();
212                        }
213                    } catch ( Exception e ) {
214                        throw new CatalogClientException( Messages.getMessage( "IGEO_STD_CSW_INVALID_PARAM",
215                                                                               Constants.RPC_MONTH ) );
216                    }
217                }
218                if ( st.getMember( Constants.RPC_DAY ) != null ) {
219                    try {
220                        Integer toDay = Integer.valueOf( st.getMember( Constants.RPC_DAY ).getValue().toString() );
221                        if ( toDay.intValue() > 31 || toDay.intValue() < 1 ) {
222                            throw new Exception();
223                        }
224                    } catch ( Exception e ) {
225                        throw new CatalogClientException( Messages.getMessage( "IGEO_STD_CSW_INVALID_PARAM",
226                                                                               Constants.RPC_DAY ) );
227                    }
228                }
229            }
230    
231            if ( fromYear != null && toYear != null && fromYear.intValue() > toYear.intValue() ) {
232                throw new CatalogClientException( Messages.getMessage( "IGEO_STD_CSW_INVALID_PERIOD", fromYear, toYear ) );
233            }
234        }
235    
236        /**
237         * Adds the passed bounding box to each element of the passed List, that does not already have a bounding box.
238         *
239         * @param dataSessionRecList
240         * @param boundingBox
241         * @return Returns the passed List, with the passed bounding box having been added to all those elements, that did
242         *         not have a bounding box defined.
243         */
244        private List addBoundingBox( List dataSessionRecList, Envelope boundingBox ) {
245    
246            for ( int i = 0; i < dataSessionRecList.size(); i++ ) {
247    
248                if ( ( (DataSessionRecord) dataSessionRecList.get( i ) ).getBoundingBox() == null ) {
249                    ( (DataSessionRecord) dataSessionRecList.get( i ) ).setBoundingBox( boundingBox );
250                }
251            }
252    
253            return dataSessionRecList;
254        }
255    
256    }