001    /*
002     * 
003     * $Revision: 14975 $ $Date: 2009-03-20 12:19:57 +0100 (Fri, 20 Mar 2009) $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.services.fieldquery;
025    
026    import org.jdom.Document;
027    import org.mycore.common.MCRCache;
028    import org.mycore.common.MCRConfiguration;
029    import org.mycore.common.MCRSessionMgr;
030    import org.mycore.parsers.bool.MCRCondition;
031    
032    /**
033     * Manages a cache that contains the data of the most recently used queries.
034     * There are {MCR.SearchServlet.CacheSize} most recently used queries 
035     * in a global cache. Additionally, the last query of search session is always 
036     * stored within the MCRSession.
037     * 
038     * @author Frank Lützenkirchen
039     */
040    public class MCRCachedQueryData
041    {
042      /** A cache that contains the data of the most recently used queries */
043      protected static MCRCache cache;
044      
045      static
046      { 
047        int cacheSize = MCRConfiguration.instance().getInt( "MCR.SearchServlet.CacheSize", 50 );
048        cache = new MCRCache( cacheSize, "SearchServlet query data cache" ); 
049      }
050      
051      private final static String LAST_QUERY_IN_SESSION = "LastQuery";
052      
053      public static MCRCachedQueryData getData(String id) {
054            // The n most recently used queries are in a global cache
055            MCRCachedQueryData data = (MCRCachedQueryData) (cache.get(id));
056            if (data == null) {
057                // Additionally, the last query of a single session is always there
058                data = (MCRCachedQueryData) (MCRSessionMgr.getCurrentSession().get(LAST_QUERY_IN_SESSION));
059                // May be the last query in session is the one we are looking for
060                if ((data != null) && (data.getResults().getID().equals(id)))
061                    return data;
062            }
063            return data; // May be null, if no cached query data found
064        }        
065      
066      /** The query as XML, for later reloading into search mask editor form */
067      private Document query; 
068      /** The result list */
069      private MCRResults results; 
070      /** The normalized query condition */
071      private MCRCondition condition;
072      /** The page last displayed */
073      private int page;
074      /** The number of hits per page */
075      private int numPerPage;
076      
077      public static void cache(MCRResults results, Document query, MCRCondition condition) {
078        MCRCachedQueryData data = new MCRCachedQueryData(results, query, condition);
079        cache.put( results.getID(), data );
080        MCRSessionMgr.getCurrentSession().put( LAST_QUERY_IN_SESSION, data );
081      }
082      
083      private MCRCachedQueryData( MCRResults results, Document query, MCRCondition condition )
084      {
085        this.results = results;
086        this.query = query;
087        this.condition = condition;
088      }
089    
090      public int getNumPerPage()
091      {
092        return numPerPage;
093      }
094    
095      public void setNumPerPage( int numPerPage )
096      {
097        this.numPerPage = numPerPage;
098      }
099    
100      public int getPage()
101      {
102        return page;
103      }
104    
105      public void setPage( int page )
106      {
107        this.page = page;
108      }
109    
110      public MCRCondition getCondition()
111      {
112        return condition;
113      }
114    
115      public Document getQuery()
116      {
117        return query;
118      }
119    
120      public MCRResults getResults()
121      {
122        return results;
123      }
124    }