001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.authoring.struts.actions;
020:
021: import java.io.IOException;
022: import java.text.DateFormat;
023: import java.text.ParseException;
024: import java.util.Arrays;
025: import java.util.Date;
026: import java.util.List;
027:
028: import javax.servlet.ServletException;
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031:
032: import org.apache.struts.action.ActionForm;
033: import org.apache.struts.action.ActionForward;
034: import org.apache.struts.action.ActionMapping;
035: import org.apache.struts.actions.DispatchAction;
036: import org.apache.commons.lang.StringUtils;
037: import org.apache.roller.RollerException;
038: import org.apache.roller.business.RollerFactory;
039: import org.apache.roller.business.WeblogManager;
040: import org.apache.roller.pojos.WeblogCategoryData;
041: import org.apache.roller.pojos.WeblogEntryData;
042: import org.apache.roller.pojos.WebsiteData;
043: import org.apache.roller.ui.core.BasePageModel;
044: import org.apache.roller.ui.core.RollerRequest;
045: import org.apache.roller.ui.core.RollerSession;
046: import org.apache.roller.ui.authoring.struts.formbeans.WeblogEntryManagementForm;
047: import org.apache.roller.util.DateUtil;
048:
049: /////////////////////////////////////////////////////////////////////////////
050: /**
051: * Query weblog entries and display the results in tabular form.
052: *
053: * @struts.action path="/roller-ui/authoring/weblogEntryManagement" name="weblogEntryManagementForm"
054: * scope="request" parameter="method"
055: *
056: * @struts.action-forward name="weblogEntryManagement.page" path=".WeblogEntryManagement"
057: *
058: * @author Dave Johnson
059: */
060: public final class WeblogEntryManagementAction extends DispatchAction {
061: //-----------------------------------------------------------------------
062: /**
063: * Respond to request to display weblog entry management page.
064: * Loads the appropriate model objects and forwards the request to
065: * the edit weblog page.
066: */
067: public ActionForward query(ActionMapping mapping,
068: ActionForm actionForm, HttpServletRequest request,
069: HttpServletResponse response) throws IOException,
070: ServletException, RollerException {
071: WeblogEntryManagementForm form = (WeblogEntryManagementForm) actionForm;
072: RollerRequest rreq = RollerRequest.getRollerRequest(request);
073: WeblogManager wmgr = RollerFactory.getRoller()
074: .getWeblogManager();
075: RollerSession rses = RollerSession.getRollerSession(request);
076:
077: // ensure that weblog is specfied and user has permission to work there
078: if (rreq.getWebsite() != null
079: && rses.isUserAuthorized(rreq.getWebsite())) {
080: String status = form.getStatus().equals("ALL") ? null
081: : form.getStatus();
082: request.setAttribute("model", new PageModel(request,
083: response, mapping, rreq.getWebsite(), form));
084: } else {
085: return mapping.findForward("access-denied");
086: }
087: return mapping.findForward("weblogEntryManagement.page");
088: }
089:
090: public class PageModel extends BasePageModel {
091: private List entries = null;
092: private RollerRequest rollerRequest = null;
093: private HttpServletRequest request = null;
094: private WebsiteData website = null;
095: private String category = null;
096: private Date startDate = null;
097: private Date endDate = null;
098: private String status = WeblogEntryData.PUBLISHED;
099: private Integer maxEntries = null;
100: private boolean more = false;
101: private WeblogEntryManagementForm queryForm = null;
102:
103: public PageModel(HttpServletRequest request,
104: HttpServletResponse response, ActionMapping mapping,
105: WebsiteData website, WeblogEntryManagementForm queryForm)
106: throws RollerException {
107: super ("weblogEntryQuery.title", request, response, mapping);
108: rollerRequest = RollerRequest.getRollerRequest(request);
109: this .request = request;
110: this .queryForm = queryForm;
111: this .website = website;
112:
113: if (null != queryForm.getCategoryId()
114: && !queryForm.getCategoryId().equals("")) {
115: WeblogManager wmgr = RollerFactory.getRoller()
116: .getWeblogManager();
117: WeblogCategoryData cd = wmgr
118: .getWeblogCategory(queryForm.getCategoryId());
119: category = cd.getPath();
120: }
121:
122: final DateFormat df = DateFormat.getDateInstance(
123: DateFormat.SHORT, request.getLocale());
124: String start = queryForm.getStartDateString();
125: if (null != start && start.trim().length() > 0) {
126: try {
127: startDate = DateUtil.getStartOfDay(df.parse(start));
128: } catch (ParseException e) {
129: throw new RollerException(
130: "ERROR parsing start date.");
131: }
132: }
133:
134: String end = queryForm.getEndDateString();
135: if (null != end && end.trim().length() > 0) {
136: try {
137: endDate = DateUtil.getEndOfDay(df.parse(end));
138: } catch (ParseException e) {
139: throw new RollerException("ERROR parsing end date.");
140: }
141: }
142:
143: this .status = "ALL".equals(queryForm.getStatus()) ? null
144: : queryForm.getStatus();
145: this .maxEntries = maxEntries;
146:
147: String[] tagsarr = new String[0];
148:
149: if (queryForm.getTags() != null)
150: tagsarr = StringUtils.split(queryForm.getTags()
151: .toLowerCase(), ' ');
152:
153: int offset = queryForm.getOffset();
154: if ("POST".equals(request.getMethod())) {
155: offset = 0;
156: }
157:
158: entries = RollerFactory.getRoller().getWeblogManager()
159: .getWeblogEntries(website, null, startDate,
160: endDate, category, Arrays.asList(tagsarr),
161: status, queryForm.getSortby(), null,
162: offset, queryForm.getCount() + 1);
163: if (entries.size() > queryForm.getCount()) {
164: more = true;
165: entries.remove(entries.size() - 1);
166: }
167: }
168:
169: public String getBaseURL() {
170: return getRequest().getContextPath();
171: }
172:
173: /**
174: * Get recent weblog entries using request parameters to determine
175: * username, date, and category name parameters.
176: * @return List of WeblogEntryData objects.
177: * @throws RollerException
178: */
179: public List getRecentWeblogEntries() throws RollerException {
180: return entries;
181: }
182:
183: public int getWeblogEntryCount() {
184: return entries.size();
185: }
186:
187: public List getCategories() throws Exception {
188: RollerRequest rreq = RollerRequest
189: .getRollerRequest(request);
190: List categories = RollerFactory.getRoller()
191: .getWeblogManager().getWeblogCategories(
192: rreq.getWebsite());
193: return categories;
194: }
195:
196: public Date getEarliestDate() {
197: Date date = null;
198: if (entries.size() > 0) {
199: WeblogEntryData earliest = (WeblogEntryData) entries
200: .get(entries.size() - 1);
201: date = earliest.getPubTime();
202: }
203: return date;
204: }
205:
206: public Date getLatestDate() {
207: Date date = null;
208: if (entries.size() > 0) {
209: WeblogEntryData latest = (WeblogEntryData) entries
210: .get(0);
211: date = latest.getPubTime();
212: }
213: return date;
214: }
215:
216: public String getNextLink() {
217: if (more) {
218: int offset = queryForm.getOffset()
219: + queryForm.getCount();
220: offset = (offset < 0) ? 0 : offset;
221: return getQueryLink() + "&offset=" + offset;
222: } else {
223: return null;
224: }
225: }
226:
227: public String getPrevLink() {
228: if (queryForm.getOffset() > 0) {
229: int offset = queryForm.getOffset()
230: - queryForm.getCount();
231: offset = (offset < 0) ? 0 : offset;
232: return getQueryLink() + "&offset=" + offset;
233: } else {
234: return null;
235: }
236: }
237:
238: private String getQueryLink() {
239: StringBuffer sb = new StringBuffer();
240: sb.append(request.getContextPath());
241: sb.append("/roller-ui/authoring/weblogEntryManagement.do"); // TODO: get path from Struts
242: sb.append("?method=query");
243: sb.append("&weblog=");
244: sb.append(getWebsite().getHandle());
245:
246: sb.append("&count=");
247: sb.append(queryForm.getCount());
248:
249: if (StringUtils.isNotEmpty(queryForm.getStartDateString())) {
250: sb.append("&startDateString=");
251: sb.append(queryForm.getStartDateString());
252: }
253:
254: if (StringUtils.isNotEmpty(queryForm.getEndDateString())) {
255: sb.append("&endDateString=");
256: sb.append(queryForm.getEndDateString());
257: }
258:
259: if (StringUtils.isNotEmpty(queryForm.getCategoryId())) {
260: sb.append("&categoryId=");
261: sb.append(queryForm.getCategoryId());
262: }
263:
264: if (StringUtils.isNotEmpty(queryForm.getTags())) {
265: sb.append("&tags=");
266: sb.append(queryForm.getTags());
267: }
268:
269: if (StringUtils.isNotEmpty(queryForm.getSortby())) {
270: sb.append("&sortBy=");
271: sb.append(queryForm.getSortby());
272: }
273:
274: if (StringUtils.isNotEmpty(queryForm.getStatus())) {
275: sb.append("&status=");
276: sb.append(queryForm.getStatus());
277: }
278:
279: return sb.toString();
280: }
281: }
282: }
|