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: /* Created on Mar 10, 2004 */
019: package org.apache.roller.ui.authoring.struts.actions;
020:
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.LinkedList;
024: import java.util.List;
025:
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028:
029: import org.apache.commons.lang.builder.HashCodeBuilder;
030: import org.apache.struts.action.ActionMapping;
031: import org.apache.roller.RollerException;
032: import org.apache.roller.business.RollerFactory;
033: import org.apache.roller.pojos.UserData;
034: import org.apache.roller.pojos.WeblogEntryData;
035: import org.apache.roller.ui.core.BasePageModel;
036: import org.apache.roller.ui.core.RollerRequest;
037: import org.apache.roller.ui.core.RollerSession;
038: import org.apache.roller.ui.authoring.struts.actions.WeblogEntryPageModel.PageMode;
039: import org.apache.roller.ui.authoring.struts.formbeans.WeblogEntryFormEx;
040: import org.apache.commons.lang.StringUtils;
041:
042: //import com.swabunga.spell.event.SpellCheckEvent;
043: import java.util.Map;
044: import org.apache.commons.logging.Log;
045: import org.apache.commons.logging.LogFactory;
046: import org.apache.roller.config.RollerRuntimeConfig;
047: import org.apache.roller.business.PluginManager;
048: import org.apache.roller.business.Roller;
049: import org.apache.roller.ui.core.plugins.WeblogEntryEditor;
050: import org.apache.roller.pojos.WebsiteData;
051: import org.apache.roller.ui.core.RollerContext;
052: import org.apache.roller.ui.core.plugins.UIPluginManager;
053: import org.apache.roller.util.URLUtilities;
054:
055: /**
056: * All data needed to render the edit-weblog page.
057: * @author David M Johnson
058: */
059: public class WeblogEntryPageModel extends BasePageModel {
060: private static Log logger = LogFactory.getFactory().getInstance(
061: WeblogEntryPageModel.class);
062:
063: private RollerRequest rollerRequest = null;
064: private PageMode mode = null;
065: private ArrayList words = null;
066: private WeblogEntryFormEx form;
067: private WeblogEntryData weblogEntry;
068:
069: public static class PageMode {
070: private String name;
071:
072: public PageMode(String name) {
073: this .name = name;
074: }
075:
076: public boolean equals(Object obj) {
077: return ((PageMode) obj).name.equals(name);
078: }
079:
080: public int hashCode() {
081: return HashCodeBuilder.reflectionHashCode(this );
082: }
083: }
084:
085: public static final PageMode EDIT_MODE = new PageMode("EDIT_MODE");
086: public static final PageMode SPELL_MODE = new PageMode("SPELL_MODE");
087: public static final PageMode PREVIEW_MODE = new PageMode(
088: "PREVIEW_MODE");
089:
090: public WeblogEntryPageModel(HttpServletRequest request,
091: HttpServletResponse response, ActionMapping mapping,
092: WeblogEntryFormEx form, PageMode mode, ArrayList words)
093: throws RollerException {
094: this (request, response, mapping, form, mode);
095: this .words = words;
096: }
097:
098: public WeblogEntryPageModel(HttpServletRequest request,
099: HttpServletResponse response, ActionMapping mapping,
100: WeblogEntryFormEx form, PageMode mode)
101: throws RollerException {
102: super ("dummy", request, response, mapping);
103: this .rollerRequest = RollerRequest.getRollerRequest(request);
104: this .form = form;
105: this .mode = mode;
106: getRequest().setAttribute("leftPage",
107: "/weblog/WeblogEditSidebar.jsp");
108: }
109:
110: public WebsiteData getWeblog() {
111: return this .rollerRequest.getWebsite();
112: }
113:
114: public String getTitle() {
115: if (StringUtils.isEmpty(form.getId())) {
116: return bundle.getString("weblogEdit.title.newEntry");
117: }
118: return bundle.getString("weblogEdit.title.editEntry");
119: }
120:
121: /**
122: * Get recent weblog entries using request parameters to determine
123: * username, date, and category name parameters.
124: * @return List of WeblogEntryData objects.
125: * @throws RollerException
126: */
127: public List getRecentPublishedEntries() throws RollerException {
128: RollerSession rollerSession = RollerSession
129: .getRollerSession(getRequest());
130: return RollerFactory.getRoller().getWeblogManager()
131: .getWeblogEntries(getWeblogEntry().getWebsite(), // userName
132: null, null, // startDate
133: null, // endDate
134: null, // catName
135: null, // tags
136: WeblogEntryData.PUBLISHED, // status
137: null, // sortby (null for pubTime)
138: null, 0, 20);
139: }
140:
141: /**
142: * Get recent weblog entries using request parameters to determine
143: * username, date, and category name parameters.
144: * @return List of WeblogEntryData objects.
145: * @throws RollerException
146: */
147: public List getRecentDraftEntries() throws RollerException {
148: RollerSession rollerSession = RollerSession
149: .getRollerSession(getRequest());
150: return RollerFactory.getRoller().getWeblogManager()
151: .getWeblogEntries(getWeblogEntry().getWebsite(), null,
152: null, // startDate
153: null, // endDate
154: null, // catName
155: null, // tags
156: WeblogEntryData.DRAFT, // status
157: "updateTime", // sortby
158: null, 0, 20); // maxEntries
159: }
160:
161: /**
162: * Get recent weblog entries using request parameters to determine
163: * username, date, and category name parameters.
164: * @return List of WeblogEntryData objects.
165: * @throws RollerException
166: */
167: public List getRecentPendingEntries() throws RollerException {
168: RollerSession rollerSession = RollerSession
169: .getRollerSession(getRequest());
170: return RollerFactory.getRoller().getWeblogManager()
171: .getWeblogEntries(getWeblogEntry().getWebsite(), null,
172: null, // startDate
173: null, // endDate
174: null, // catName
175: null, // tags
176: WeblogEntryData.PENDING, // status
177: "updateTime", // sortby
178: null, 0, 20);
179: }
180:
181: public List getHoursList() {
182: List ret = new LinkedList();
183: for (int i = 0; i < 24; i++) {
184: ret.add(new Integer(i));
185: }
186: return ret;
187: }
188:
189: public List getMinutesList() {
190: List ret = new LinkedList();
191: for (int i = 0; i < 60; i++) {
192: ret.add(new Integer(i));
193: }
194: return ret;
195: }
196:
197: public List getSecondsList() {
198: return getMinutesList();
199: }
200:
201: public boolean getHasPagePlugins() {
202: boolean ret = false;
203: try {
204: Roller roller = RollerFactory.getRoller();
205: PluginManager ppmgr = roller.getPagePluginManager();
206: ret = ppmgr.hasPagePlugins();
207: } catch (RollerException e) {
208: logger.error(e);
209: }
210: return ret;
211: }
212:
213: public List getPagePlugins() {
214: List list = new ArrayList();
215: try {
216: if (getHasPagePlugins()) {
217: Roller roller = RollerFactory.getRoller();
218: PluginManager ppmgr = roller.getPagePluginManager();
219: Map plugins = ppmgr.getWeblogEntryPlugins(getWebsite());
220: Iterator it = plugins.values().iterator();
221: while (it.hasNext())
222: list.add(it.next());
223: }
224: } catch (Exception e) {
225: logger.error(e);
226: }
227: return list;
228: }
229:
230: public String getEditorPage() {
231: // Select editor page selected by user (simple text editor,
232: // DHTML editor, Ekit Java applet, etc.
233: RollerSession rollerSession = RollerSession
234: .getRollerSession(getRequest());
235: String editorPage = weblogEntry.getWebsite().getEditorPage();
236: if (StringUtils.isEmpty(editorPage)) {
237: editorPage = "editor-text.jsp";
238: }
239: return editorPage;
240: }
241:
242: public WeblogEntryEditor getEditor() {
243:
244: UIPluginManager pmgr = RollerContext.getUIPluginManager();
245: return pmgr.getWeblogEntryEditor(this .website.getEditorPage());
246: }
247:
248: public UserData getUser() {
249: RollerSession rollerSession = RollerSession
250: .getRollerSession(getRequest());
251: return rollerSession.getAuthenticatedUser();
252: }
253:
254: public List getCategories() throws Exception {
255: RollerSession rollerSession = RollerSession
256: .getRollerSession(getRequest());
257: return RollerFactory.getRoller().getWeblogManager()
258: .getWeblogCategories(weblogEntry.getWebsite(), false);
259: }
260:
261: public WeblogEntryFormEx getWeblogEntryForm()
262: throws RollerException {
263: return this .form;
264: }
265:
266: /** returns a dummied-up weblog entry object */
267: public WeblogEntryData getWeblogEntry() throws RollerException {
268: if (weblogEntry == null) {
269: weblogEntry = new WeblogEntryData();
270: weblogEntry.setWebsite(getWebsite());
271: form.copyTo(weblogEntry, getRequest().getLocale(),
272: getRequest().getParameterMap());
273: weblogEntry.setWebsite(weblogEntry.getWebsite());
274: }
275: return weblogEntry;
276: }
277:
278: public String getPermaLink() throws RollerException {
279: String context = RollerRuntimeConfig.getAbsoluteContextURL();
280: return context + getWeblogEntry().getPermaLink();
281: }
282:
283: public static String makeSelect(String word, List words) {
284: StringBuffer buf = new StringBuffer("<select name=\"");
285: buf.append("replacementWords\" style=\"font-size: 10px;\">");
286: buf.append("<option selected=\"selected\" value=\"").append(
287: word);
288: buf.append("\">").append(word).append("</option>");
289: if (words == null || words.size() < 1) {
290: buf.append("<option value=\"").append(word);
291: buf.append("\">No Suggestions</option>");
292: } else {
293: for (Iterator it2 = words.iterator(); it2.hasNext();) {
294: word = it2.next().toString();
295: buf.append("<option value=\"").append(word);
296: buf.append("\">").append(word).append("</option>");
297: }
298: }
299: buf.append("</select>");
300: return buf.toString();
301: }
302:
303: /**
304: * @return Returns the mode.
305: */
306: public PageMode getMode() {
307: return mode;
308: }
309:
310: /**
311: * @param mode The mode to set.
312: */
313: public void setMode(PageMode mode) {
314: this .mode = mode;
315: }
316:
317: public boolean getEditMode() {
318: return mode.equals(EDIT_MODE);
319: }
320:
321: public boolean getSpellMode() {
322: return mode.equals(SPELL_MODE);
323: }
324:
325: public boolean getPreviewMode() {
326: return mode.equals(PREVIEW_MODE);
327: }
328:
329: /**
330: * @return Returns the words.
331: */
332: public ArrayList getWords() {
333: return words;
334: }
335:
336: /**
337: * @param words The words to set.
338: */
339: public void setWords(ArrayList words) {
340: this .words = words;
341: }
342:
343: public boolean getUserAuthorized() throws RollerException {
344: return getRollerSession().isUserAuthorized(
345: getWeblogEntry().getWebsite());
346: }
347:
348: public boolean getUserAuthorizedToAuthor() throws RollerException {
349: return getRollerSession().isUserAuthorizedToAuthor(
350: getWeblogEntry().getWebsite());
351: }
352:
353: public PageMode getEDIT_MODE() {
354: return EDIT_MODE;
355: }
356:
357: public int getCommentCount() {
358: // Don't check for comments on unsaved entry (fixed ROL-970)
359: if (weblogEntry.getId() == null)
360: return 0;
361: List comments = comments = weblogEntry
362: .getComments(false, false);
363: return comments != null ? comments.size() : 0;
364: }
365:
366: public String getFullPreviewURL() {
367: if (weblogEntry.getId() != null) {
368: return URLUtilities.getPreviewWeblogEntryURL(weblogEntry
369: .getAnchor(), website, null, false);
370: }
371: return null;
372: }
373: }
|