KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > blog > www > DynamicBlogNavigator


1 /*
2  * Copyright (c) 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenChronicle
5  *
6  * $Id: DynamicBlogNavigator.java,v 1.1 2007/02/20 02:18:11 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.blog.www;
23
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28
29 import org.opensubsystems.blog.data.Blog;
30 import org.opensubsystems.blog.data.Entry;
31 import org.opensubsystems.core.data.DataConstant;
32 import org.opensubsystems.core.util.Log;
33 import org.opensubsystems.core.www.WebCommonConstants;
34 import org.opensubsystems.core.www.WebUtils;
35 import org.opensubsystems.patterns.listdata.data.DataCondition;
36 import org.opensubsystems.patterns.listdata.www.ListBrowserServlet;
37
38 /**
39  * Class responsible for parsing and creating URLs for blogs and their entries.
40  * The URLs are dynamic that is they use parameter names and values in the URL
41  * in the format required by the ListBrowserServlet.
42  *
43  * @version $Id: DynamicBlogNavigator.java,v 1.1 2007/02/20 02:18:11 bastafidli Exp $
44  * @author Miro Halas
45  * @code.reviewer Miro Halas
46  * @code.reviewed 1.3 2006/07/15 09:09:26 bastafidli
47  */

48 public class DynamicBlogNavigator extends BlogNavigator
49 {
50    // Constants ////////////////////////////////////////////////////////////////
51

52    /**
53     * Page where user can login to the system when accessing the application
54     * using dynamic URLs.
55     */

56    public static final String JavaDoc DYNAMIC_LOGIN_WEB_PAGE = "logindynamic.html";
57    
58    /**
59     * Page where user can logout to the system when accessing the application
60     * using dynamic URLs.
61     */

62    public static final String JavaDoc DYNAMIC_LOGOUT_WEB_PAGE = "logoutdynamic.html";
63    
64    /**
65     * Static URL that will be used to handle posted data from the dynamic
66     * pages. This URL will invoke the BlogEditServlet that can handle the
67     * data posted from the dynamically generated URL and then redirect back
68     * to the dynamically generated URL.
69     */

70    public static final String JavaDoc DYNAMIC_POST_WEB_PAGE = "dynamic.html";
71    
72    // Cached values ////////////////////////////////////////////////////////////
73

74    /**
75     * Logger for this class
76     */

77    private static Logger JavaDoc s_logger = Log.getInstance(DynamicBlogNavigator.class);
78
79    // Constructors /////////////////////////////////////////////////////////////
80

81    /**
82     * Creates a new instance of DynamicBlogNavigator
83     *
84     * @param hsrqRequest - request that will be used by this navigator
85     */

86    public DynamicBlogNavigator(
87       HttpServletRequest JavaDoc hsrqRequest
88    )
89    {
90       super(hsrqRequest);
91    }
92   
93    // URL Accessors ////////////////////////////////////////////////////////////
94

95    /**
96     * {@inheritDoc}
97     */

98    public boolean getIsDynamic()
99    {
100       return true;
101    }
102
103    /**
104     * {@inheritDoc}
105     */

106    public String JavaDoc getPostURL(
107    )
108    {
109       // For dynamic navigation the URL will be posted to the servlet which
110
// handles the static URLs using special page name to detect that
111
// once the processing is done, it should go back to the dynamic URL
112
return DYNAMIC_POST_WEB_PAGE;
113    }
114
115    /**
116     * {@inheritDoc}
117     */

118    public String JavaDoc getRootURL(
119    )
120    {
121       StringBuffer JavaDoc sbURL;
122
123       sbURL = new StringBuffer JavaDoc(m_strBlogDirectoryURL);
124       sbURL.append("blogs");
125
126       return sbURL.toString();
127    }
128
129    /**
130     * {@inheritDoc}
131     */

132    public String JavaDoc getURL(
133       Blog blog
134    )
135    {
136       return getBlogURL(blog.getIdAsObject());
137    }
138    
139    /**
140     * {@inheritDoc}
141     */

142    public String JavaDoc getBlogURL(
143       Object JavaDoc objBlogIdentification
144    )
145    {
146       String JavaDoc strURL;
147       
148       if (objBlogIdentification instanceof Integer JavaDoc)
149       {
150          // The object identification is blog id so we can construct the dynamic
151
// URL
152
StringBuffer JavaDoc sbURL;
153          
154          /*
155          <a HREF="<%=contextpath
156             %>/entries?<%=ListBrowserServlet.LIST_PARAM_LIST_PARENT_DATA_TYPE
157             %>=<%=DataConstant.BLOG_DATA_TYPE
158             %>&<%=ListBrowserServlet.LIST_PARAM_LIST_PARENT_IDENTIFIER
159             %>=<bean:write name="blog" property="id" format="#"/>">
160          */

161          
162          sbURL = new StringBuffer JavaDoc(m_strBlogDirectoryURL);
163          sbURL.append("entries");
164          sbURL.append(WebCommonConstants.URL_PARAMETER_SEPARATOR_CHAR); // ?
165
sbURL.append(ListBrowserServlet.LIST_PARAM_LIST_PARENT_DATA_TYPE);
166          sbURL.append(WebCommonConstants.URL_PARAMETER_VALUE_SEPARATOR_CHAR); // =
167
sbURL.append(DataConstant.BLOG_DATA_TYPE);
168          sbURL.append(WebCommonConstants.URL_PARAMETER_PARAMETER_SEPARATOR_CHAR); // &
169
sbURL.append(ListBrowserServlet.LIST_PARAM_LIST_PARENT_IDENTIFIER);
170          sbURL.append(WebCommonConstants.URL_PARAMETER_VALUE_SEPARATOR_CHAR); // =
171
sbURL.append(((Integer JavaDoc)objBlogIdentification).intValue());
172
173          strURL = sbURL.toString();
174       }
175       else
176       {
177          strURL = super.getBlogURL(objBlogIdentification);
178       }
179       
180       return strURL;
181    }
182
183    /**
184     * {@inheritDoc}
185     */

186    public String JavaDoc getURL(
187       Blog blog,
188       Entry entry
189    )
190    {
191       return getURL(entry);
192    }
193
194    /**
195     * {@inheritDoc}
196     */

197    public String JavaDoc getURL(
198       Entry entry
199    )
200    {
201       BlogEntryIdentification entryIdentification = new BlogEntryIdentification();
202       
203       entryIdentification.m_objBlogIdentification = new Integer JavaDoc(entry.getParentId());
204       entryIdentification.m_iBlogEntryIdentification = entry.getId();
205       
206       return getURL(entryIdentification);
207    }
208    
209    /**
210     * {@inheritDoc}
211     */

212    public String JavaDoc getURL(
213       BlogEntryIdentification entryIdentification
214    )
215    {
216       StringBuffer JavaDoc sbURL;
217
218       /*
219       <a HREF="<%=contextpath
220          %>/entries?<%=ListBrowserServlet.LIST_PARAM_LIST_PARENT_DATA_TYPE
221          %>=<%=DataConstant.BLOG_DATA_TYPE
222          %>&<%=ListBrowserServlet.LIST_PARAM_LIST_PARENT_IDENTIFIER
223          %>=<bean:write name="blogentry"
224                         property="parentId"
225                         format="#"/>&EXTRA_CONDITION_ATTRIBUTE=<%=Entry.COL_BLOGENTRY_ID
226          %>&<%=ListBrowserServlet.LIST_PARAM_EXTRA_CONDITION_ATTRIBUTE
227          %>=<%=Entry.COL_BLOGENTRY_ID
228          %>&<%=ListBrowserServlet.LIST_PARAM_EXTRA_CONDITION_OPERAND
229          %>=<%=DataCondition.OPERAND_EQUALS
230          %>&<%=ListBrowserServlet.LIST_PARAM_EXTRA_CONDITION_VALUE_TYPE
231          %>=<%=DataCondition.VALUE_TYPE_ID
232          %>&<%=ListBrowserServlet.LIST_PARAM_EXTRA_CONDITION_VALUE
233          %>=<bean:write name="blogentry"
234                         property="id"
235                         format="#"/>">
236       */

237       
238       sbURL = new StringBuffer JavaDoc(m_strBlogDirectoryURL);
239       sbURL.append("entries");
240       sbURL.append(WebCommonConstants.URL_PARAMETER_SEPARATOR_CHAR); // ?
241
sbURL.append(ListBrowserServlet.LIST_PARAM_LIST_PARENT_DATA_TYPE);
242       sbURL.append(WebCommonConstants.URL_PARAMETER_VALUE_SEPARATOR_CHAR); // =
243
sbURL.append(DataConstant.BLOG_DATA_TYPE);
244       sbURL.append(WebCommonConstants.URL_PARAMETER_PARAMETER_SEPARATOR_CHAR); // &
245
sbURL.append(ListBrowserServlet.LIST_PARAM_LIST_PARENT_IDENTIFIER);
246       sbURL.append(WebCommonConstants.URL_PARAMETER_VALUE_SEPARATOR_CHAR); // =
247
sbURL.append(((Integer JavaDoc)entryIdentification.m_objBlogIdentification).intValue());
248       sbURL.append(WebCommonConstants.URL_PARAMETER_PARAMETER_SEPARATOR_CHAR); // &
249
sbURL.append(ListBrowserServlet.LIST_PARAM_EXTRA_CONDITION_ATTRIBUTE);
250       sbURL.append(WebCommonConstants.URL_PARAMETER_VALUE_SEPARATOR_CHAR); // =
251
sbURL.append(Entry.COL_BLOGENTRY_ID);
252       sbURL.append(WebCommonConstants.URL_PARAMETER_PARAMETER_SEPARATOR_CHAR); // &
253
sbURL.append(ListBrowserServlet.LIST_PARAM_EXTRA_CONDITION_OPERAND);
254       sbURL.append(WebCommonConstants.URL_PARAMETER_VALUE_SEPARATOR_CHAR); // =
255
sbURL.append(DataCondition.OPERAND_EQUALS);
256       sbURL.append(WebCommonConstants.URL_PARAMETER_PARAMETER_SEPARATOR_CHAR); // &
257
sbURL.append(ListBrowserServlet.LIST_PARAM_EXTRA_CONDITION_VALUE_TYPE);
258       sbURL.append(WebCommonConstants.URL_PARAMETER_VALUE_SEPARATOR_CHAR); // =
259
sbURL.append(DataCondition.VALUE_TYPE_ID);
260       sbURL.append(WebCommonConstants.URL_PARAMETER_PARAMETER_SEPARATOR_CHAR); // &
261
sbURL.append(ListBrowserServlet.LIST_PARAM_EXTRA_CONDITION_VALUE);
262       sbURL.append(WebCommonConstants.URL_PARAMETER_VALUE_SEPARATOR_CHAR); // =
263
sbURL.append(entryIdentification.m_iBlogEntryIdentification);
264       
265       return sbURL.toString();
266    }
267    
268    /**
269     * {@inheritDoc}
270     */

271    public Object JavaDoc getBlogIdentification(
272       HttpServletRequest JavaDoc hsrqRequest
273    )
274    {
275       Object JavaDoc objReturn;
276       Integer JavaDoc iBlogId;
277       
278       iBlogId = getBlogId(hsrqRequest);
279       if (iBlogId == null)
280       {
281          objReturn = super.getBlogIdentification(hsrqRequest);
282       }
283       else
284       {
285          objReturn = iBlogId;
286       }
287       
288       return objReturn;
289    }
290
291    /**
292     * {@inheritDoc}
293     */

294    public BlogEntryIdentification getBlogEntryIdentification(
295       HttpServletRequest JavaDoc hsrqRequest
296    )
297    {
298       BlogEntryIdentification entryIdentification = null;
299       Object JavaDoc objBlogId;
300       Integer JavaDoc iEntryId;
301       
302       objBlogId = getBlogId(hsrqRequest);
303       iEntryId = getBlogEntryId(hsrqRequest);
304
305       if ((objBlogId != null) && (iEntryId != null))
306       {
307          entryIdentification = new BlogEntryIdentification();
308          entryIdentification.m_objBlogIdentification = objBlogId;
309          entryIdentification.m_iBlogEntryIdentification = iEntryId.intValue();
310       }
311       else
312       {
313          s_logger.log(Level.FINEST,
314                   "The request doesn't identify valid Blog entry.");
315          // Just return null and let the caller handle it
316
}
317       
318       return entryIdentification;
319    }
320    
321    /**
322     * {@inheritDoc}
323     */

324    public int isIndexPage(
325    )
326    {
327       // When we are using dynamic navigation using ListBrowserServlet this
328
// method should never be called
329
throw new UnsupportedOperationException JavaDoc(
330          "Application should never call this method for dynamic navigation");
331    }
332    
333    /**
334     * {@inheritDoc}
335     */

336    public boolean isBlogIndexPage(
337    )
338    {
339       // When we are using dynamic navigation using ListBrowserServlet this
340
// method should never be called
341
throw new UnsupportedOperationException JavaDoc(
342          "Application should never call this method for dynamic navigation");
343    }
344
345
346    /**
347     * Test if the requested path is path to the page where user can login to
348     * the system.
349     *
350     * @return boolean - true if the page is login page
351     */

352    public boolean isLoginPage(
353    )
354    {
355       String JavaDoc strPath;
356       
357       strPath = WebUtils.getFullRequestPath(m_hsrqRequest);
358
359       return strPath.endsWith(DYNAMIC_LOGIN_WEB_PAGE);
360    }
361
362    /**
363     * Test if the requested path is path to the page where user can login to
364     * the system.
365     *
366     * @return boolean - true if the page is login page
367     */

368    public boolean isLogoutPage(
369    )
370    {
371       String JavaDoc strPath;
372       
373       strPath = WebUtils.getFullRequestPath(m_hsrqRequest);
374
375       return strPath.endsWith(DYNAMIC_LOGOUT_WEB_PAGE);
376    }
377    /**
378     * {@inheritDoc}
379     */

380    public String JavaDoc getFirstPageURL(
381    )
382    {
383       // When we are using dynamic navigation using ListBrowserServlet we use
384
// the listdata custom JSP tags and therefore this method is not necessary
385
throw new UnsupportedOperationException JavaDoc(
386          "Application should never call this method for dynamic navigation");
387    }
388
389    /**
390     * {@inheritDoc}
391     */

392    public String JavaDoc getLastPageURL(
393    )
394    {
395       // When we are using dynamic navigation using ListBrowserServlet we use
396
// the listdata custom JSP tags and therefore this method is not necessary
397
throw new UnsupportedOperationException JavaDoc(
398          "Application should never call this method for dynamic navigation");
399    }
400    
401    // Helper methods ///////////////////////////////////////////////////////////
402

403    /**
404     * Get blog id from the request by looking at all possible request parameters
405     * that can be used in the page or in the URL
406     *
407     * @param hsrqRequest - the servlet request.
408     * @return Integer - id of the blog or null if no is present
409     */

410    protected Integer JavaDoc getBlogId(
411       HttpServletRequest JavaDoc hsrqRequest
412    )
413    {
414       Integer JavaDoc iBlogId = null;
415       String JavaDoc strTemp;
416       
417       // Parse all potential request parameters that can contain blog id
418
strTemp = hsrqRequest.getParameter("BLOG_ID");
419       if (strTemp != null)
420       {
421          try
422          {
423             iBlogId = new Integer JavaDoc(strTemp);
424          }
425          catch (NumberFormatException JavaDoc nfeExc)
426          {
427             // Ignore this
428
}
429       }
430       
431       if (iBlogId == null)
432       {
433          strTemp = hsrqRequest.getParameter("BLOGENTRY_BLOG_ID");
434          if (strTemp != null)
435          {
436             try
437             {
438                iBlogId = new Integer JavaDoc(strTemp);
439             }
440             catch (NumberFormatException JavaDoc nfeExc)
441             {
442                // Ignore this
443
}
444          }
445       }
446       
447       if (iBlogId == null)
448       {
449          int iParentDataType = DataConstant.NO_DATA_TYPE;
450          
451          strTemp = hsrqRequest.getParameter(
452                       ListBrowserServlet.LIST_PARAM_LIST_PARENT_DATA_TYPE);
453          if (strTemp != null)
454          {
455             try
456             {
457                iParentDataType = Integer.parseInt(strTemp);
458                if (iParentDataType == DataConstant.BLOG_DATA_TYPE)
459                {
460                   strTemp = hsrqRequest.getParameter(
461                                ListBrowserServlet.LIST_PARAM_LIST_PARENT_IDENTIFIER);
462                   if (strTemp != null)
463                   {
464                      iBlogId = new Integer JavaDoc(strTemp);
465                   }
466                }
467             }
468             catch (NumberFormatException JavaDoc nfeExc)
469             {
470                // Ignore this
471
}
472          }
473       }
474       
475       return iBlogId;
476    }
477
478    /**
479     * Get blog entry id from the request by looking at all possible request
480     * parameters that can be used in the page or in the URL
481     *
482     * @param hsrqRequest - the servlet request.
483     * @return Integer - id of the blog entry or null if no is present
484     */

485    protected Integer JavaDoc getBlogEntryId(
486       HttpServletRequest JavaDoc hsrqRequest
487    )
488    {
489       Integer JavaDoc iBlogEntryId = null;
490       String JavaDoc strTemp;
491       
492       // Parse all potential request parameters that can contain blog id
493
strTemp = hsrqRequest.getParameter("BLOGENTRY_ID");
494       if (strTemp != null)
495       {
496          try
497          {
498             iBlogEntryId = new Integer JavaDoc(strTemp);
499          }
500          catch (NumberFormatException JavaDoc nfeExc)
501          {
502             // Ignore this
503
}
504       }
505       
506       return iBlogEntryId;
507    }
508 }
509
Popular Tags