KickJava   Java API By Example, From Geeks To Geeks.

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


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 /* Created on Mar 10, 2004 */
19 package org.apache.roller.ui.authoring.struts.actions;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29
30 import org.apache.commons.lang.builder.HashCodeBuilder;
31 import org.apache.struts.action.ActionMapping;
32 import org.apache.roller.RollerException;
33 import org.apache.roller.model.RollerFactory;
34 import org.apache.roller.pojos.UserData;
35 import org.apache.roller.pojos.WeblogEntryData;
36 import org.apache.roller.ui.core.BasePageModel;
37 import org.apache.roller.ui.core.RollerRequest;
38 import org.apache.roller.ui.core.RollerSession;
39 import org.apache.roller.ui.authoring.struts.actions.WeblogEntryPageModel.PageMode;
40 import org.apache.roller.ui.authoring.struts.formbeans.WeblogEntryFormEx;
41 import org.apache.commons.lang.StringUtils;
42
43 //import com.swabunga.spell.event.SpellCheckEvent;
44
import java.util.Map JavaDoc;
45 import org.apache.commons.logging.Log;
46 import org.apache.commons.logging.LogFactory;
47 import org.apache.roller.config.RollerRuntimeConfig;
48 import org.apache.roller.model.PluginManager;
49 import org.apache.roller.model.Roller;
50 import org.apache.roller.pojos.WebsiteData;
51
52 /**
53  * All data needed to render the edit-weblog page.
54  * @author David M Johnson
55  */

56 public class WeblogEntryPageModel extends BasePageModel
57 {
58     private static Log logger =
59        LogFactory.getFactory().getInstance(WeblogEntryPageModel.class);
60         
61     private RollerRequest rollerRequest = null;
62     private PageMode mode = null;
63     private ArrayList JavaDoc words = null;
64     private WeblogEntryFormEx form;
65     private WeblogEntryData weblogEntry;
66         
67     public static class PageMode {
68         private String JavaDoc name;
69         public PageMode(String JavaDoc name) {
70             this.name = name;
71         }
72         public boolean equals(Object JavaDoc obj) {
73             return ((PageMode)obj).name.equals(name);
74         }
75         public int hashCode() {
76             return HashCodeBuilder.reflectionHashCode(this);
77         }
78     }
79     
80     public static final PageMode EDIT_MODE = new PageMode("EDIT_MODE");
81     public static final PageMode SPELL_MODE = new PageMode("SPELL_MODE");
82     public static final PageMode PREVIEW_MODE = new PageMode("PREVIEW_MODE");
83     
84
85     public WeblogEntryPageModel(
86             HttpServletRequest JavaDoc request,
87             HttpServletResponse JavaDoc response,
88             ActionMapping mapping,
89             WeblogEntryFormEx form,
90             PageMode mode,
91             ArrayList JavaDoc words) throws RollerException
92     {
93         this(request, response, mapping, form, mode);
94         this.words = words;
95     }
96
97     public WeblogEntryPageModel(
98             HttpServletRequest JavaDoc request,
99             HttpServletResponse JavaDoc response,
100             ActionMapping mapping,
101             WeblogEntryFormEx form,
102             PageMode mode) throws RollerException
103     {
104         super("dummy", request, response, mapping);
105         this.rollerRequest = RollerRequest.getRollerRequest(request);
106         this.form = form;
107         this.mode = mode;
108         getRequest().setAttribute("leftPage","/weblog/WeblogEditSidebar.jsp");
109     }
110     
111     public WebsiteData getWeblog() {
112         return this.rollerRequest.getWebsite();
113     }
114             
115     public String JavaDoc getTitle()
116     {
117         if (StringUtils.isEmpty(form.getId()))
118         {
119             return bundle.getString("weblogEdit.title.newEntry");
120         }
121         return bundle.getString("weblogEdit.title.editEntry");
122     }
123
124     /**
125      * Get recent weblog entries using request parameters to determine
126      * username, date, and category name parameters.
127      * @return List of WeblogEntryData objects.
128      * @throws RollerException
129      */

130     public List JavaDoc getRecentPublishedEntries() throws RollerException
131     {
132         RollerSession rollerSession = RollerSession.getRollerSession(getRequest());
133         return RollerFactory.getRoller().getWeblogManager()
134             .getWeblogEntries(
135                 getWeblogEntry().getWebsite(), // userName
136
null,
137                 null, // startDate
138
null, // endDate
139
null, // catName
140
WeblogEntryData.PUBLISHED, // status
141
null, // sortby (null for pubTime)
142
null, 0, 20);
143     }
144
145     /**
146      * Get recent weblog entries using request parameters to determine
147      * username, date, and category name parameters.
148      * @return List of WeblogEntryData objects.
149      * @throws RollerException
150      */

151     public List JavaDoc getRecentDraftEntries() throws RollerException
152     {
153         RollerSession rollerSession = RollerSession.getRollerSession(getRequest());
154         return RollerFactory.getRoller().getWeblogManager()
155             .getWeblogEntries(
156                 getWeblogEntry().getWebsite(),
157                 null,
158                 null, // startDate
159
null, // endDate
160
null, // catName
161
WeblogEntryData.DRAFT, // status
162
"updateTime", // sortby
163
null, 0, 20); // maxEntries
164
}
165     
166     /**
167      * Get recent weblog entries using request parameters to determine
168      * username, date, and category name parameters.
169      * @return List of WeblogEntryData objects.
170      * @throws RollerException
171      */

172     public List JavaDoc getRecentPendingEntries() throws RollerException
173     {
174         RollerSession rollerSession = RollerSession.getRollerSession(getRequest());
175         return RollerFactory.getRoller().getWeblogManager()
176             .getWeblogEntries(
177                 getWeblogEntry().getWebsite(),
178                 null,
179                 null, // startDate
180
null, // endDate
181
null, // catName
182
WeblogEntryData.PENDING, // status
183
"updateTime", // sortby
184
null, 0, 20);
185     }
186  
187     public List JavaDoc getHoursList()
188     {
189         List JavaDoc ret = new LinkedList JavaDoc();
190         for (int i=0; i<24; i++)
191         {
192             ret.add(new Integer JavaDoc(i));
193         }
194         return ret;
195     }
196
197     public List JavaDoc getMinutesList()
198     {
199         List JavaDoc ret = new LinkedList JavaDoc();
200         for (int i=0; i<60; i++)
201         {
202             ret.add(new Integer JavaDoc(i));
203         }
204         return ret;
205     }
206
207     public List JavaDoc getSecondsList()
208     {
209         return getMinutesList();
210     }
211
212     public boolean getHasPagePlugins()
213     {
214         boolean ret = false;
215         try {
216             Roller roller = RollerFactory.getRoller();
217             PluginManager ppmgr = roller.getPagePluginManager();
218             ret = ppmgr.hasPagePlugins();
219         } catch (RollerException e) {
220             logger.error(e);
221         }
222         return ret;
223     }
224     
225     public List JavaDoc getPagePlugins()
226     {
227         List JavaDoc list = new ArrayList JavaDoc();
228         try {
229             if (getHasPagePlugins())
230             {
231                 Roller roller = RollerFactory.getRoller();
232                 PluginManager ppmgr = roller.getPagePluginManager();
233                 Map JavaDoc plugins = ppmgr.getWeblogEntryPlugins(
234                     getWebsite());
235                 Iterator JavaDoc it = plugins.values().iterator();
236                 while (it.hasNext()) list.add(it.next());
237             }
238         } catch (Exception JavaDoc e) {
239             logger.error(e);
240         }
241         return list;
242     }
243
244     public String JavaDoc getEditorPage()
245     {
246         // Select editor page selected by user (simple text editor,
247
// DHTML editor, Ekit Java applet, etc.
248
RollerSession rollerSession = RollerSession.getRollerSession(getRequest());
249         String JavaDoc editorPage = weblogEntry.getWebsite().getEditorPage();
250         if (StringUtils.isEmpty( editorPage ))
251         {
252             editorPage = "editor-text.jsp";
253         }
254         return editorPage;
255     }
256
257     public UserData getUser()
258     {
259         RollerSession rollerSession = RollerSession.getRollerSession(getRequest());
260         return rollerSession.getAuthenticatedUser();
261     }
262
263     public List JavaDoc getCategories() throws Exception JavaDoc
264     {
265         RollerSession rollerSession = RollerSession.getRollerSession(getRequest());
266         return RollerFactory.getRoller().getWeblogManager()
267             .getWeblogCategories(weblogEntry.getWebsite(), false);
268     }
269     
270     public WeblogEntryFormEx getWeblogEntryForm() throws RollerException
271     {
272         return this.form;
273     }
274
275     /** returns a dummied-up weblog entry object */
276     public WeblogEntryData getWeblogEntry() throws RollerException
277     {
278         if (weblogEntry == null)
279         {
280             weblogEntry = new WeblogEntryData();
281             weblogEntry.setWebsite(getWebsite());
282             form.copyTo(weblogEntry,
283                     getRequest().getLocale(), getRequest().getParameterMap());
284             weblogEntry.setWebsite(weblogEntry.getWebsite());
285         }
286         return weblogEntry;
287     }
288     
289     public String JavaDoc getPermaLink() throws RollerException
290     {
291         String JavaDoc context = RollerRuntimeConfig.getAbsoluteContextURL();
292         return context + getWeblogEntry().getPermaLink();
293     }
294     
295     public static String JavaDoc makeSelect(String JavaDoc word, List JavaDoc words)
296     {
297         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("<select name=\"");
298         buf.append("replacementWords\" style=\"font-size: 10px;\">");
299         buf.append("<option selected=\"selected\" value=\"").append(word);
300         buf.append("\">").append(word).append("</option>");
301         if (words == null || words.size() < 1)
302         {
303             buf.append("<option value=\"").append(word);
304             buf.append("\">No Suggestions</option>");
305         }
306         else
307         {
308             for (Iterator JavaDoc it2=words.iterator(); it2.hasNext();)
309             {
310                 word = it2.next().toString();
311                 buf.append("<option value=\"").append(word);
312                 buf.append("\">").append(word).append("</option>");
313             }
314         }
315         buf.append("</select>");
316         return buf.toString();
317     }
318
319     /**
320      * @return Returns the mode.
321      */

322     public PageMode getMode() {
323         return mode;
324     }
325     
326     /**
327      * @param mode The mode to set.
328      */

329     public void setMode(PageMode mode) {
330         this.mode = mode;
331     }
332     
333     public boolean getEditMode()
334     {
335         return mode.equals(EDIT_MODE);
336     }
337     
338     public boolean getSpellMode()
339     {
340         return mode.equals(SPELL_MODE);
341     }
342     
343     public boolean getPreviewMode()
344     {
345         return mode.equals(PREVIEW_MODE);
346     }
347     
348     /**
349      * @return Returns the words.
350      */

351     public ArrayList JavaDoc getWords() {
352         return words;
353     }
354     /**
355      * @param words The words to set.
356      */

357     public void setWords(ArrayList JavaDoc words) {
358         this.words = words;
359     }
360
361     public boolean getUserAuthorized() throws RollerException
362     {
363         return getRollerSession().isUserAuthorized(getWeblogEntry().getWebsite());
364     }
365     
366     public boolean getUserAuthorizedToAuthor() throws RollerException
367     {
368         return getRollerSession().isUserAuthorizedToAuthor(getWeblogEntry().getWebsite());
369     }
370
371     public PageMode getEDIT_MODE() {
372         return EDIT_MODE;
373     }
374     
375     public int getCommentCount() {
376         // Don't check for comments on unsaved entry (fixed ROL-970)
377
if (weblogEntry.getId() == null) return 0;
378         List JavaDoc comments = comments = weblogEntry.getComments(false, false);
379         return comments != null ? comments.size() : 0;
380     }
381 }
382
383
Popular Tags