KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > authoring > struts > actions > WeblogEntryManagementAction


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18
19 package org.apache.roller.ui.authoring.struts.actions;
20
21 import java.io.IOException JavaDoc;
22 import java.text.DateFormat JavaDoc;
23 import java.text.ParseException JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30
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.actions.DispatchAction;
35 import org.apache.roller.RollerException;
36 import org.apache.roller.model.RollerFactory;
37 import org.apache.roller.model.WeblogManager;
38 import org.apache.roller.pojos.WeblogCategoryData;
39 import org.apache.roller.pojos.WeblogEntryData;
40 import org.apache.roller.pojos.WebsiteData;
41 import org.apache.roller.ui.core.BasePageModel;
42 import org.apache.roller.ui.core.RollerRequest;
43 import org.apache.roller.ui.core.RollerSession;
44 import org.apache.roller.ui.authoring.struts.formbeans.WeblogEntryManagementForm;
45 import org.apache.roller.util.DateUtil;
46
47
48 /////////////////////////////////////////////////////////////////////////////
49
/**
50  * Query weblog entries and display the results in tabular form.
51  *
52  * @struts.action path="/roller-ui/authoring/weblogEntryManagement" name="weblogEntryManagementForm"
53  * scope="request" parameter="method"
54  *
55  * @struts.action-forward name="weblogEntryManagement.page" path=".WeblogEntryManagement"
56  *
57  * @author Dave Johnson
58  */

59 public final class WeblogEntryManagementAction extends DispatchAction
60 {
61     //-----------------------------------------------------------------------
62
/**
63      * Respond to request to add a new or edit an existing weblog entry.
64      * Loads the appropriate model objects and forwards the request to
65      * the edit weblog page.
66      */

67     public ActionForward query(
68         ActionMapping mapping,
69         ActionForm actionForm,
70         HttpServletRequest JavaDoc request,
71         HttpServletResponse JavaDoc response)
72         throws IOException JavaDoc, ServletException JavaDoc, RollerException
73     {
74         WeblogEntryManagementForm form = (WeblogEntryManagementForm)actionForm;
75         RollerRequest rreq = RollerRequest.getRollerRequest(request);
76         WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager();
77         RollerSession rses = RollerSession.getRollerSession(request);
78         
79         // ensure that weblog is specfied and user has permission to work there
80
if (rreq.getWebsite() != null && rses.isUserAuthorized(rreq.getWebsite())) {
81             String JavaDoc status= form.getStatus().equals("ALL") ? null : form.getStatus();
82             request.setAttribute("model", new PageModel(
83                request,
84                response,
85                mapping,
86                rreq.getWebsite(),
87                form));
88         }
89         else
90         {
91             return mapping.findForward("access-denied");
92         }
93         return mapping.findForward("weblogEntryManagement.page");
94     }
95     
96     public class PageModel extends BasePageModel {
97         private List JavaDoc entries = null;
98         private RollerRequest rollerRequest = null;
99         private HttpServletRequest JavaDoc request = null;
100         private WebsiteData website = null;
101         private String JavaDoc category = null;
102         private Date JavaDoc startDate = null;
103         private Date JavaDoc endDate = null;
104         private String JavaDoc status = WeblogEntryData.PUBLISHED;
105         private Integer JavaDoc maxEntries = null;
106         private boolean more = false;
107         private WeblogEntryManagementForm queryForm = null;
108
109         public PageModel(
110                 HttpServletRequest JavaDoc request,
111                 HttpServletResponse JavaDoc response,
112                 ActionMapping mapping,
113                 WebsiteData website,
114                 WeblogEntryManagementForm queryForm) throws RollerException {
115             super("weblogEntryQuery.title", request, response, mapping);
116             rollerRequest = RollerRequest.getRollerRequest(request);
117             this.request = request;
118             this.queryForm = queryForm;
119             this.website = website;
120
121             if (null != queryForm.getCategoryId() && !queryForm.getCategoryId().equals("")) {
122                 WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager();
123                 WeblogCategoryData cd = wmgr.getWeblogCategory(queryForm.getCategoryId());
124                 category = cd.getPath();
125             }
126
127             final DateFormat JavaDoc df =
128                     DateFormat.getDateInstance(DateFormat.SHORT, request.getLocale());
129             String JavaDoc start = queryForm.getStartDateString();
130             if (null != start && start.trim().length() > 0) {
131                 try {
132                     startDate = DateUtil.getStartOfDay(df.parse(start));
133                 } catch (ParseException JavaDoc e) {
134                     throw new RollerException("ERROR parsing start date.");
135                 }
136             }
137
138             String JavaDoc end = queryForm.getEndDateString();
139             if (null != end && end.trim().length() > 0) {
140                 try {
141                     endDate = DateUtil.getEndOfDay(df.parse(end));
142                 } catch (ParseException JavaDoc e) {
143                     throw new RollerException("ERROR parsing end date.");
144                 }
145             }
146
147             this.status = "ALL".equals(queryForm.getStatus()) ? null: queryForm.getStatus();
148             this.maxEntries = maxEntries;
149
150             entries = RollerFactory.getRoller().getWeblogManager().getWeblogEntries(
151                     website,
152                     null,
153                     startDate,
154                     endDate,
155                     category,
156                     status,
157                     queryForm.getSortby(),
158 null, queryForm.getOffset(),
159                     queryForm.getCount() + 1);
160            if (entries.size() > queryForm.getCount()) {
161                more = true;
162                entries.remove(entries.size()-1);
163            }
164         }
165
166         public String JavaDoc getBaseURL() {
167             return getRequest().getContextPath();
168         }
169
170         /**
171          * Get recent weblog entries using request parameters to determine
172          * username, date, and category name parameters.
173          * @return List of WeblogEntryData objects.
174          * @throws RollerException
175          */

176         public List JavaDoc getRecentWeblogEntries() throws RollerException {
177             return entries;
178         }
179
180         public int getWeblogEntryCount() {
181             return entries.size();
182         }
183         
184         public List JavaDoc getCategories() throws Exception JavaDoc {
185             RollerRequest rreq = RollerRequest.getRollerRequest(request);
186             List JavaDoc categories = RollerFactory.getRoller().getWeblogManager()
187             .getWeblogCategories(rreq.getWebsite());
188             return categories;
189         }
190
191         public Date JavaDoc getEarliestDate() {
192             Date JavaDoc date = null;
193             if (entries.size() > 0) {
194                 WeblogEntryData earliest = (WeblogEntryData)entries.get(entries.size()-1);
195                 date = earliest.getPubTime();
196             }
197             return date;
198         }
199
200         public Date JavaDoc getLatestDate() {
201             Date JavaDoc date = null;
202             if (entries.size() > 0) {
203                 WeblogEntryData latest = (WeblogEntryData)entries.get(0);
204                 date = latest.getPubTime();
205             }
206             return date;
207         }
208
209         public String JavaDoc getNextLink() {
210             if (more) {
211                 int offset = queryForm.getOffset() + queryForm.getCount();
212                 offset = (offset < 0) ? 0 : offset;
213                 return getQueryLink() + "&offset=" + offset;
214             } else {
215                 return null;
216             }
217         }
218
219         public String JavaDoc getPrevLink() {
220             if (queryForm.getOffset() > 0) {
221                 int offset = queryForm.getOffset() - queryForm.getCount();
222                 offset = (offset < 0) ? 0 : offset;
223                 return getQueryLink() + "&offset=" + offset;
224             } else {
225                 return null;
226             }
227         }
228
229         private String JavaDoc getQueryLink() {
230             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
231             sb.append(request.getContextPath());
232             sb.append("/roller-ui/authoring/weblogEntryManagement.do"); // TODO: get path from Struts
233
sb.append("?method=query");
234             sb.append("&weblog=");
235             sb.append(getWebsite().getHandle());
236             sb.append("&count=");
237             sb.append(queryForm.getCount());
238             return sb.toString();
239         }
240     }
241 }
242
Popular Tags