Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: ru/gammalabs/ice/frontend/web/RequestProcessor.java


1   /*
2    * $Id: RequestProcessor.java,v 1.2 2003/01/31 23:39:27 dimitry Exp $
3    *
4    * ==================================================================
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18   * ==================================================================
19   *
20   */
21  
22  package ru.gammalabs.ice.frontend.web;
23  
24  import ru.gammalabs.ice.publishing.framework.*;
25  import ru.gammalabs.ice.frontend.SearchCriteria;
26  import ru.gammalabs.ice.structure.PartitionManager;
27  import ru.gammalabs.ice.structure.PartitionException;
28  import ru.gammalabs.ice.structure.PartitionModel;
29  import ru.gammalabs.ice.utils.http.DefaultRequestProcessor;
30  
31  import javax.servlet.http.HttpServletRequest;
32  
33  import org.apache.log4j.Category;
34  
35  import java.util.*;
36  
37  /**
38   * This <code>RequestProcessor</code> does default request processing,
39   * then tries to retrieve from the request currently displayed <code>PartitionModel</code>,
40   * page <code>Template</code>, used for displaying the result page, and
41   * all of the <code>SearchCriteria</code> objects.
42   * @see ru.gammalabs.ice.utils.http.RequestProcessor
43   * @see ru.gammalabs.ice.utils.http.DefaultRequestProcessor
44   * @see ru.gammalabs.ice.structure.PartitionModel
45   * @see ru.gammalabs.ice.publishing.framework.Template
46   * @see ru.gammalabs.ice.frontend.SearchCriteria
47   * @author Vasily Antashov <vv@v6.ru>
48   * @version $Revision: 1.2 $
49   */
50  public class RequestProcessor extends DefaultRequestProcessor
51  {
52  
53    private static final Category log = Category.getInstance(RequestProcessor.class.getName());
54  
55    /**
56     * Currently displayed <code>PartitionModel</code>.
57     * @see ru.gammalabs.ice.structure.PartitionModel
58     */
59    private PartitionModel currentPartition = null;
60  
61    /**
62     * Page <code>Template</code>, used for displaying the result page.
63     * @see ru.gammalabs.ice.publishing.framework.Template
64     */
65    private Template currentPageTemplate = null;
66  
67    /**
68     * Map containing <code>SearchCriteria</code> objects, supplied in the request.
69     * @see ru.gammalabs.ice.frontend.SearchCriteria
70     */
71    private Map searchCriteriaObjects = null;
72  
73    private Locale locale;
74  
75    /**
76     * The constructor tries to retrieve from the <code>request</code>
77     * currently displayed <code>PartitionModel</code>,
78     * page <code>Template</code>, used for displaying the result page,
79     * and all of the <code>SearchCriteria</code> objects.
80     * The following request keys are used to retrieve the objects mentioned:
81     * WebKeys.PARTITION_ID_PARAMETER_REQUEST_KEY for currently displayed <code>PartitionModel</code>;
82     * WebKeys.PAGE_TEMPLATE_NAME_PARAMETER_REQUEST_KEY for page <code>Template</code>;
83     * WebKeys.SEARCH_CRITERIA_PARAMETER_REQUEST_KEY for <code>SearchCriteria</code> objects.
84     * In case page <code>Template</code> can't be retrieved (either not specified in the request
85     * or an error happened during <code>Template</code> retrieval) and <code>PartitionModel<code> is
86     * retrieved successfully, <code>PartitionModel</code>'s <code>Template</code> will be used as page
87     * <code>Template</code> for displaying the result page.
88     * @see ru.gammalabs.ice.structure.PartitionModel
89     * @see ru.gammalabs.ice.publishing.framework.Template
90     * @see ru.gammalabs.ice.frontend.SearchCriteria
91     * @see ru.gammalabs.ice.frontend.web.WebKeys
92     * @param request <code>HttpServletRequest</code> to be processed
93     */
94    public RequestProcessor(HttpServletRequest request)
95    {
96      super();
97      this.locale = Util.getSessionData(request).getContextLocale();
98      this.setRequestString(request.getQueryString());
99      currentPartition = retrieveCurrentPartition(request);
100     currentPageTemplate = retrieveCurrentPageTemplate(request);
101     if ((currentPageTemplate == null) && (currentPartition != null))
102     {
103       this.currentPageTemplate = currentPartition.getPageTemplate();
104     }
105     searchCriteriaObjects = retrieveSearchCriteriaObjects();
106   }
107 
108   /**
109    * Returns current <code>PartitionModel</code>.
110    * @see ru.gammalabs.ice.structure.PartitionModel
111    * @return <code>PartitionModel</code>, or <code>null</code> in case current <code>PartitionModel</code>
112    * can't be retrieved from the request
113    */
114   public PartitionModel getCurrentPartition()
115   {
116     return this.currentPartition;
117   }
118 
119   /**
120    * Returns page <code>Template</code>, used for displaying the result page.
121    * @see ru.gammalabs.ice.publishing.framework.Template
122    * @return <code>Template</code>, or <code>null</code> in case page <code>Template</code>
123    * can't be retrieved from the request
124    */
125   public Template getCurrentPageTemplate()
126   {
127     return this.currentPageTemplate;
128   }
129 
130   /**
131    * Returns <code>SearchCriteria</code> with the specified <code>searchCriteriaName</code>.
132    * @see ru.gammalabs.ice.frontend.SearchCriteria
133    * @param searchCriteriaName name of the <code>SearchCriteria</code> to be retrieved
134    * @return <code>SearchCriteria</code>, or <code>null</code> in case <code>SearchCriteria</code> with
135    * such <code>searchCriteriaName</code> can't be retrieved from the request
136    */
137   public SearchCriteria getSearchCriteria(String searchCriteriaName)
138   {
139     if (searchCriteriaName == null) return null;
140     if (searchCriteriaObjects == null) return null;
141 
142     return (SearchCriteria)searchCriteriaObjects.get(searchCriteriaName);
143   }
144 
145   private PartitionModel retrieveCurrentPartition(HttpServletRequest request)
146   {
147 
148     // try to retrieve current partition id from the request
149     Long currentPartitionId = (Long)this.get(WebKeys.PARTITION_ID_PARAMETER_REQUEST_KEY, new Long(0));
150 
151     // in case current partition id is not specified in the request
152     if (currentPartitionId == null)
153     {
154         currentPartitionId = (Long) request.getSession().getAttribute(ru.gammalabs.ice.presentation.web.WebKeys.ACTIVE_PARTITION);
155       log.info("\"" + WebKeys.PARTITION_ID_PARAMETER_REQUEST_KEY + "\" parameter is not specified... getting from session");
156       if (currentPartitionId == null)
157       {
158         log.info("\"" + WebKeys.PARTITION_ID_PARAMETER_REQUEST_KEY + "\" parameter is not specified in session");
159         return null;
160       }
161     }
162 
163     request.getSession().setAttribute(ru.gammalabs.ice.presentation.web.WebKeys.ACTIVE_PARTITION, currentPartitionId);
164 
165     // retrieve partition manager
166     PartitionManager partitionManager = Util.getSessionData(request).getPartitionManager();
167 
168     // try to retrieve current partition
169     PartitionModel currentPartition = null;
170     try
171     {
172       currentPartition = partitionManager.getPartition(currentPartitionId.longValue());
173     }
174     catch (PartitionException e)
175     {
176       // something has gone wrong
177       log.info("unknown error while retrieving current partition", e);
178     }
179 
180     // in case PartitionManager returns null (which should not be the case)
181     if (currentPartition == null)
182     {
183       log.info("can't retrieve current partition (PartitionManager returns null); partition id: " + currentPartitionId.toString());
184     }
185 
186     return currentPartition;
187 
188   }
189 
190   private Template retrieveCurrentPageTemplate(HttpServletRequest request)
191   {
192 
193     // declare current page template name
194     String currentPageTemplateName = "";
195 
196     // try to retrieve current page template from the request
197     currentPageTemplateName = (String)this.get(WebKeys.PAGE_TEMPLATE_NAME_PARAMETER_REQUEST_KEY, currentPageTemplateName);
198 
199     // in case current page template name is not specified in the request
200     if (currentPageTemplateName == null)
201     {
202       log.info("\"" + WebKeys.PAGE_TEMPLATE_NAME_PARAMETER_REQUEST_KEY + "\" parameter is not specified");
203       return null;
204     }
205 
206     // retrieve page template manager
207     TemplateManager pageTemplateManager = Util.getSessionData(request).getPageTemplateManager();
208 
209     // try to retrieve current page template by name
210     Template pageTemplate = pageTemplateManager.getByName(currentPageTemplateName);
211 
212     // in case TemplateManager returns null (which might be the case)
213     if (pageTemplate == null)
214     {
215       log.info("can't retrieve current page template (TemplateManager returns null); page template name: " + currentPageTemplateName);
216     }
217 
218     return pageTemplate;
219 
220   }
221 
222   private Map retrieveSearchCriteriaObjects()
223   {
224 
225     SearchCriteria[] searchCriteriaObjectsArray = {};
226 
227     searchCriteriaObjectsArray = (SearchCriteria[])this.get(WebKeys.SEARCH_CRITERIA_PARAMETER_REQUEST_KEY, searchCriteriaObjectsArray);
228 
229     if (searchCriteriaObjectsArray == null)
230     {
231       return null;
232     }
233 
234     Map searchCriteriaObjectsMap = new HashMap();
235 
236     for (int i=0; i<searchCriteriaObjectsArray.length; i++)
237     {
238       SearchCriteria searchCriteria = searchCriteriaObjectsArray[i];
239       if (searchCriteria == null) continue;
240 
241       // sets current partition identifier
242       if (this.currentPartition != null)
243       {
244         searchCriteria.setCurrentPartitionId(new Long(this.currentPartition.getId()));
245         searchCriteria.assignThis();
246       }
247 
248       // in case search locale is not specified deliberately in the search criteria,
249       // search for CUs only in the current locale
250       if (searchCriteria.getLocale() == null)
251       {
252         searchCriteria.setLocale(locale);
253       }
254 
255       // in case there are no partitions specified deliberately in the search criteria,
256       // include in search results those CUs that are not linked to any partition
257       if (searchCriteria.getPartitions() == null)
258       {
259         searchCriteria.setIncludeNotLinkedElements(AbstractSearchCriteria.INCLUDE_NOT_LINKED_ELEMENTS);
260       }
261 
262       searchCriteriaObjectsMap.put(searchCriteria.getName(), searchCriteria);
263     }
264 
265     return searchCriteriaObjectsMap;
266 
267   }
268 
269 }