001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.event;
022:
023: import java.util.List;
024:
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: import org.apache.log4j.Logger;
029:
030: import org.apache.struts.action.Action;
031: import org.apache.struts.action.ActionForm;
032: import org.apache.struts.action.ActionForward;
033: import org.apache.struts.action.ActionMapping;
034: import org.apache.struts.action.DynaActionForm;
035: import com.methodhead.sitecontext.SiteContext;
036: import com.methodhead.property.Property;
037: import com.methodhead.util.OperationContext;
038: import com.methodhead.util.StrutsUtil;
039: import com.methodhead.auth.AuthUser;
040: import com.methodhead.auth.AuthAction;
041: import com.methodhead.auth.AuthUtil;
042: import com.methodhead.test.TestUtils;
043: import java.util.Date;
044: import java.text.DateFormat;
045: import java.text.ParseException;
046: import java.util.Calendar;
047: import java.util.GregorianCalendar;
048: import org.apache.commons.lang.StringUtils;
049:
050: /**
051: * <p>
052: * Use <tt>EventAction</tt> to build a web interface to view logged
053: * events. This action will respond to the mapping <tt>/listEvents</tt>,
054: * calling <tt>doListEvents()</tt>.
055: * </p>
056: * <p>
057: * The mapping's parameter can be used to set the page size of the
058: * returned list; the page size defaults to 50:
059: * </p>
060: * <xmp>
061: * <action
062: * path = "/listEvents"
063: * type = "com.methodhead.event.EventAction"
064: * scope = "request"
065: * name = "eventForm"
066: * validate = "false"
067: * parameter = "25">
068: * <forward name="list" path="/events.jsp"/>
069: * </action>
070: * </xmp>
071: * <p>
072: * The action is expecting a <tt>DynaActionForm</tt> with at least
073: * the following configuration:
074: * </p>
075: * <xmp> <form-bean
076: * name = "eventForm"
077: * dynamic = "true"
078: * type = "org.apache.struts.action.DynaActionForm">
079: *
080: * <form-property name="prev" type="java.lang.String"/>
081: * <form-property name="next" type="java.lang.String"/>
082: * <form-property name="page" type="java.lang.String"/>
083: * <form-property name="events" type="java.util.List" />
084: * </form-bean>
085: * </xmp>
086: * <p>
087: * The action will load the events for <tt>page</tt> (page 0 if
088: * <tt>page</tt> is not specified) and put them in <tt>events</tt>. If a
089: * previous or next page is available, the corresponding form property is
090: * set to its page number. The mapping's input is ignored; instead the
091: * <tt>list</tt> forward is returned.
092: * </p>
093: */
094: public class EventAction extends AuthAction {
095:
096: // constructors /////////////////////////////////////////////////////////////
097:
098: // constants ////////////////////////////////////////////////////////////////
099:
100: // classes //////////////////////////////////////////////////////////////////
101:
102: // methods //////////////////////////////////////////////////////////////////
103:
104: public ActionForward doListEvents(OperationContext op,
105: EventPolicy policy) throws Exception {
106:
107: //
108: // authorized?
109: //
110: String msg = policy.isListEventsAuthorized(op);
111: if (msg != null) {
112: StrutsUtil.addMessage(op.request, msg, null, null, null);
113: return op.mapping.findForward("accessDenied");
114: }
115:
116: Date start = TestUtils.getCurrentDate();
117: Date end = null;
118: DateFormat format = DateFormat
119: .getDateInstance(DateFormat.SHORT);
120:
121: //
122: // date provided?
123: //
124: if (!StringUtils.isBlank((String) op.form.get("date"))) {
125: try {
126: start = format.parse((String) op.form.get("date"));
127: } catch (ParseException e) {
128: Logger.getLogger("EventAction").error(
129: "Couldn't parse page from \""
130: + op.form.get("page") + "\"");
131: }
132: }
133:
134: //
135: // calculate start and end date
136: //
137: Calendar cal = new GregorianCalendar();
138: cal.setTime(start);
139:
140: cal.set(Calendar.HOUR_OF_DAY, 0);
141: cal.set(Calendar.MINUTE, 0);
142: cal.set(Calendar.SECOND, 0);
143: cal.set(Calendar.MILLISECOND, 0);
144: start = cal.getTime();
145:
146: cal.add(Calendar.DAY_OF_MONTH, 1);
147: end = cal.getTime();
148:
149: //
150: // get the events
151: //
152: Event event = new Event();
153: event.setSiteContext(SiteContext.getContext(op.request));
154: op.form.set("events", event.getEvents(start, end));
155:
156: //
157: // set date, next, and prev
158: //
159: op.form.set("date", format.format(start));
160: op.form.set("next", format.format(end));
161:
162: cal.add(Calendar.DAY_OF_MONTH, -2);
163: op.form.set("prev", format.format(cal.getTime()));
164:
165: //
166: // forward to list
167: //
168: return op.mapping.findForward("list");
169: }
170:
171: public ActionForward doExecute(ActionMapping mapping,
172: ActionForm form, HttpServletRequest request,
173: HttpServletResponse response) throws Exception {
174:
175: //
176: // get some things we'll need
177: //
178: DynaActionForm dynaForm = (DynaActionForm) form;
179: EventPolicy policy = (EventPolicy) StrutsUtil
180: .getPolicy(mapping);
181: AuthUser user = AuthUtil.getUser(request);
182: OperationContext op = new OperationContext(mapping, dynaForm,
183: request, response, user);
184:
185: if (mapping.getPath().equals("/listEvents"))
186: return doListEvents(op, policy);
187:
188: throw new Exception("Unexpected mapping path \""
189: + mapping.getPath() + "\"");
190: }
191:
192: // properties ///////////////////////////////////////////////////////////////
193:
194: // attributes ///////////////////////////////////////////////////////////////
195: }
|