KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > event > EventAction


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.event;
22
23 import java.util.List JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28 import org.apache.log4j.Logger;
29
30 import org.apache.struts.action.Action;
31 import org.apache.struts.action.ActionForm;
32 import org.apache.struts.action.ActionForward;
33 import org.apache.struts.action.ActionMapping;
34 import org.apache.struts.action.DynaActionForm;
35 import com.methodhead.sitecontext.SiteContext;
36 import com.methodhead.property.Property;
37 import com.methodhead.util.OperationContext;
38 import com.methodhead.util.StrutsUtil;
39 import com.methodhead.auth.AuthUser;
40 import com.methodhead.auth.AuthAction;
41 import com.methodhead.auth.AuthUtil;
42 import com.methodhead.test.TestUtils;
43 import java.util.Date JavaDoc;
44 import java.text.DateFormat JavaDoc;
45 import java.text.ParseException JavaDoc;
46 import java.util.Calendar JavaDoc;
47 import java.util.GregorianCalendar JavaDoc;
48 import org.apache.commons.lang.StringUtils;
49
50
51 /**
52  * <p>
53  * Use <tt>EventAction</tt> to build a web interface to view logged
54  * events. This action will respond to the mapping <tt>/listEvents</tt>,
55  * calling <tt>doListEvents()</tt>.
56  * </p>
57  * <p>
58  * The mapping's parameter can be used to set the page size of the
59  * returned list; the page size defaults to 50:
60  * </p>
61  * <xmp>
62  * <action
63  * path = "/listEvents"
64  * type = "com.methodhead.event.EventAction"
65  * scope = "request"
66  * name = "eventForm"
67  * validate = "false"
68  * parameter = "25">
69  * <forward name="list" path="/events.jsp"/>
70  * </action>
71  * </xmp>
72  * <p>
73  * The action is expecting a <tt>DynaActionForm</tt> with at least
74  * the following configuration:
75  * </p>
76  * <xmp> <form-bean
77  * name = "eventForm"
78  * dynamic = "true"
79  * type = "org.apache.struts.action.DynaActionForm">
80  *
81  * <form-property name="prev" type="java.lang.String"/>
82  * <form-property name="next" type="java.lang.String"/>
83  * <form-property name="page" type="java.lang.String"/>
84  * <form-property name="events" type="java.util.List" />
85  * </form-bean>
86  * </xmp>
87  * <p>
88  * The action will load the events for <tt>page</tt> (page 0 if
89  * <tt>page</tt> is not specified) and put them in <tt>events</tt>. If a
90  * previous or next page is available, the corresponding form property is
91  * set to its page number. The mapping's input is ignored; instead the
92  * <tt>list</tt> forward is returned.
93  * </p>
94  */

95 public class EventAction
96 extends
97   AuthAction {
98
99   // constructors /////////////////////////////////////////////////////////////
100

101   // constants ////////////////////////////////////////////////////////////////
102

103   // classes //////////////////////////////////////////////////////////////////
104

105   // methods //////////////////////////////////////////////////////////////////
106

107   public ActionForward doListEvents(
108     OperationContext op,
109     EventPolicy policy )
110   throws
111     Exception JavaDoc {
112
113     //
114
// authorized?
115
//
116
String JavaDoc msg = policy.isListEventsAuthorized( op );
117     if ( msg != null ) {
118       StrutsUtil.addMessage( op.request, msg, null, null, null );
119       return op.mapping.findForward( "accessDenied" );
120     }
121
122     Date JavaDoc start = TestUtils.getCurrentDate();
123     Date JavaDoc end = null;
124     DateFormat JavaDoc format = DateFormat.getDateInstance( DateFormat.SHORT );
125
126     //
127
// date provided?
128
//
129
if ( !StringUtils.isBlank( ( String JavaDoc )op.form.get( "date" ) ) ) {
130       try {
131         start = format.parse( ( String JavaDoc )op.form.get( "date" ) );
132       }
133       catch ( ParseException JavaDoc e ) {
134         Logger.getLogger( "EventAction" ).error(
135           "Couldn't parse page from \"" + op.form.get( "page" ) + "\"" );
136       }
137     }
138
139     //
140
// calculate start and end date
141
//
142
Calendar JavaDoc cal = new GregorianCalendar JavaDoc();
143     cal.setTime( start );
144
145     cal.set( Calendar.HOUR_OF_DAY, 0 );
146     cal.set( Calendar.MINUTE, 0 );
147     cal.set( Calendar.SECOND, 0 );
148     cal.set( Calendar.MILLISECOND, 0 );
149     start = cal.getTime();
150
151     cal.add( Calendar.DAY_OF_MONTH, 1 );
152     end = cal.getTime();
153
154     //
155
// get the events
156
//
157
Event event = new Event();
158     event.setSiteContext( SiteContext.getContext( op.request ) );
159     op.form.set( "events", event.getEvents( start, end ) );
160
161     //
162
// set date, next, and prev
163
//
164
op.form.set( "date", format.format( start ) );
165     op.form.set( "next", format.format( end ) );
166
167     cal.add( Calendar.DAY_OF_MONTH, -2 );
168     op.form.set( "prev", format.format( cal.getTime() ) );
169
170     //
171
// forward to list
172
//
173
return op.mapping.findForward( "list" );
174   }
175
176   public ActionForward doExecute(
177     ActionMapping mapping,
178     ActionForm form,
179     HttpServletRequest JavaDoc request,
180     HttpServletResponse JavaDoc response )
181   throws
182     Exception JavaDoc {
183
184     //
185
// get some things we'll need
186
//
187
DynaActionForm dynaForm = ( DynaActionForm )form;
188     EventPolicy policy = ( EventPolicy )StrutsUtil.getPolicy( mapping );
189     AuthUser user = AuthUtil.getUser( request );
190     OperationContext op = new OperationContext( mapping, dynaForm, request, response, user );
191
192     if ( mapping.getPath().equals( "/listEvents" ) )
193       return doListEvents( op, policy );
194
195     throw
196       new Exception JavaDoc( "Unexpected mapping path \"" + mapping.getPath() + "\"" );
197   }
198
199   // properties ///////////////////////////////////////////////////////////////
200

201   // attributes ///////////////////////////////////////////////////////////////
202
}
203
Popular Tags