KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > weblog > actions > WeblogEntryPageModel


1 /*
2  * Created on Mar 10, 2004
3  */

4 package org.roller.presentation.weblog.actions;
5
6 import com.swabunga.spell.event.SpellCheckEvent;
7
8 import org.apache.commons.lang.builder.HashCodeBuilder;
9 import org.apache.struts.action.ActionForward;
10 import org.apache.struts.action.ActionMapping;
11 import org.roller.RollerException;
12 import org.roller.model.WeblogManager;
13 import org.roller.pojos.UserData;
14 import org.roller.pojos.WeblogEntryData;
15 import org.roller.presentation.BasePageModel;
16 import org.roller.presentation.RollerContext;
17 import org.roller.presentation.RollerRequest;
18 import org.roller.presentation.tags.calendar.CalendarModel;
19 import org.roller.presentation.velocity.ContextLoader;
20 import org.roller.presentation.weblog.formbeans.WeblogEntryFormEx;
21 import org.roller.presentation.weblog.tags.EditWeblogCalendarModel;
22 import org.roller.util.StringUtils;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.ListIterator JavaDoc;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 /**
35  * All data needed to render the edit-weblog page.
36  * @author David M Johnson
37  */

38 public class WeblogEntryPageModel extends BasePageModel
39 {
40     private RollerRequest rollerRequest = null;
41     private PageMode mode = null;
42     private ArrayList JavaDoc words = null;
43     private WeblogEntryFormEx form;
44     private List JavaDoc comments = null;
45     private WeblogEntryData weblogEntry;
46         
47     public static class PageMode {
48         private String JavaDoc name;
49         public PageMode(String JavaDoc name) {
50             this.name = name;
51         }
52         public boolean equals(Object JavaDoc obj) {
53             return ((PageMode)obj).name.equals(name);
54         }
55         public int hashCode() {
56             return HashCodeBuilder.reflectionHashCode(this);
57         }
58     }
59     
60     public static final PageMode EDIT_MODE = new PageMode("EDIT_MODE");
61     public static final PageMode SPELL_MODE = new PageMode("SPELL_MODE");
62     public static final PageMode PREVIEW_MODE = new PageMode("PREVIEW_MODE");
63     
64
65     public WeblogEntryPageModel(
66             HttpServletRequest JavaDoc request,
67             HttpServletResponse JavaDoc response,
68             ActionMapping mapping,
69             WeblogEntryFormEx form,
70             PageMode mode,
71             ArrayList JavaDoc words) throws RollerException
72     {
73         this(request, response, mapping, form, mode);
74         this.words = words;
75     }
76
77     public WeblogEntryPageModel(
78             HttpServletRequest JavaDoc request,
79             HttpServletResponse JavaDoc response,
80             ActionMapping mapping,
81             WeblogEntryFormEx form,
82             PageMode mode) throws RollerException
83     {
84         super(request, response, mapping);
85         this.rollerRequest = RollerRequest.getRollerRequest(request);
86         this.form = form;
87         this.mode = mode;
88         
89         getRequest().setAttribute("leftPage","/weblog/WeblogEditSidebar.jsp");
90         
91         if (null != form.getId())
92         {
93             WeblogManager wmgr = rollerRequest.getRoller().getWeblogManager();
94             comments = wmgr.getComments(form.getId(), false);
95         }
96     }
97
98     public String JavaDoc getBaseURL()
99     {
100         return getRequest().getContextPath();
101     }
102
103     /**
104      * Get recent weblog entries using request parameters to determine
105      * username, date, and category name parameters.
106      * @return List of WeblogEntryData objects.
107      * @throws RollerException
108      */

109     public List JavaDoc getRecentPublishedEntries() throws RollerException
110     {
111         return rollerRequest.getRoller().getWeblogManager()
112             .getWeblogEntries(
113                 rollerRequest.getWebsite(), // userName
114
null, // startDate
115
null, // endDate
116
null, // catName
117
WeblogManager.PUB_ONLY, // status
118
new Integer JavaDoc(20)); // maxEntries
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 JavaDoc getRecentDraftEntries() throws RollerException
128     {
129         return rollerRequest.getRoller().getWeblogManager()
130             .getWeblogEntries(
131                 rollerRequest.getWebsite(),
132                 null, // startDate
133
null, // endDate
134
null, // catName
135
WeblogManager.DRAFT_ONLY, // status
136
new Integer JavaDoc(20)); // maxEntries
137
}
138
139     public List JavaDoc getHoursList()
140     {
141         List JavaDoc ret = new LinkedList JavaDoc();
142         for (int i=0; i<24; i++)
143         {
144             ret.add(new Integer JavaDoc(i));
145         }
146         return ret;
147     }
148
149     public List JavaDoc getMinutesList()
150     {
151         List JavaDoc ret = new LinkedList JavaDoc();
152         for (int i=0; i<60; i++)
153         {
154             ret.add(new Integer JavaDoc(i));
155         }
156         return ret;
157     }
158
159     public List JavaDoc getSecondsList()
160     {
161         return getMinutesList();
162     }
163
164     public boolean getHasPagePlugins()
165     {
166         return ContextLoader.hasPlugins();
167     }
168
169     public String JavaDoc getEditorPage()
170     {
171         // Select editor page selected by user (simple text editor,
172
// DHTML editor, Ekit Java applet, etc.
173
String JavaDoc editorPage = rollerRequest.getWebsite().getEditorPage();
174         if (StringUtils.isEmpty( editorPage ))
175         {
176             editorPage = "editor-text.jsp";
177         }
178         return editorPage;
179     }
180
181     public CalendarModel getCalendarModel() throws Exception JavaDoc
182     {
183         // Determine URL to self
184
ActionForward selfForward = getMapping().findForward("editWeblog");
185         String JavaDoc selfUrl= getRequest().getContextPath()+selfForward.getPath();
186
187         // Setup weblog calendar model
188
CalendarModel model = new EditWeblogCalendarModel(
189                 rollerRequest, getResponse(), selfUrl );
190         return model;
191     }
192
193     public UserData getUser()
194     {
195         return rollerRequest.getUser();
196     }
197
198     public List JavaDoc getCategories() throws Exception JavaDoc
199     {
200         return rollerRequest.getRoller().getWeblogManager()
201             .getWeblogCategories(rollerRequest.getWebsite(), false);
202     }
203
204     public List JavaDoc getComments() throws Exception JavaDoc
205     {
206         return comments;
207     }
208     
209     public WeblogEntryFormEx getWeblogEntryForm() throws RollerException
210     {
211         return this.form;
212     }
213
214     /** returns a dummied-up weblog entry object */
215     public WeblogEntryData getWeblogEntry() throws RollerException
216     {
217         if (weblogEntry == null)
218         {
219             weblogEntry = new WeblogEntryData();
220             weblogEntry.setWebsite(rollerRequest.getWebsite());
221             form.copyTo(weblogEntry,
222                     getRequest().getLocale(), getRequest().getParameterMap());
223             weblogEntry.setWebsite(rollerRequest.getWebsite());
224         }
225         return weblogEntry;
226     }
227     
228     public String JavaDoc getPermaLink() throws RollerException
229     {
230         String JavaDoc context = RollerContext
231             .getRollerContext(rollerRequest.getRequest())
232             .getAbsoluteContextUrl(rollerRequest.getRequest());
233         return context + getWeblogEntry().getPermaLink();
234     }
235     
236     public String JavaDoc getSpellCheckHtml() throws RollerException
237     {
238         String JavaDoc text = getWeblogEntry().getText();
239         String JavaDoc escapeText = StringUtils.replace( text, "<", "{" );
240         escapeText = StringUtils.replace( escapeText, ">", "}" );
241         StringBuffer JavaDoc newText = new StringBuffer JavaDoc(escapeText);
242         ArrayList JavaDoc events = (ArrayList JavaDoc)
243             getRequest().getSession().getAttribute("spellCheckEvents");
244         SpellCheckEvent event = null;
245         String JavaDoc word = null;
246         int start = -1;
247         int end = -1;
248         String JavaDoc select = null;
249         for(ListIterator JavaDoc it=events.listIterator(events.size()); it.hasPrevious();)
250         {
251             event = (SpellCheckEvent)it.previous();
252             word = event.getInvalidWord();
253             start = event.getWordContextPosition();
254             end = start + word.length();
255             select = makeSelect(word, event.getSuggestions());
256     
257             newText.replace( start, end, select );
258         }
259         escapeText = StringUtils.replace( newText.toString(), "}", "&gt;" );
260         escapeText = StringUtils.replace( escapeText, "{", "&lt;" );
261         return escapeText;
262     }
263     
264     public static String JavaDoc makeSelect(String JavaDoc word, List JavaDoc words)
265     {
266         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("<select name=\"");
267         buf.append("replacementWords\" style=\"font-size: 10px;\">");
268         buf.append("<option selected=\"selected\" value=\"").append(word);
269         buf.append("\">").append(word).append("</option>");
270         if (words == null || words.size() < 1)
271         {
272             buf.append("<option value=\"").append(word);
273             buf.append("\">No Suggestions</option>");
274         }
275         else
276         {
277             for (Iterator JavaDoc it2=words.iterator(); it2.hasNext();)
278             {
279                 word = it2.next().toString();
280                 buf.append("<option value=\"").append(word);
281                 buf.append("\">").append(word).append("</option>");
282             }
283         }
284         buf.append("</select>");
285         return buf.toString();
286     }
287
288     /**
289      * @return Returns the mode.
290      */

291     public PageMode getMode() {
292         return mode;
293     }
294     
295     /**
296      * @param mode The mode to set.
297      */

298     public void setMode(PageMode mode) {
299         this.mode = mode;
300     }
301     
302     public boolean getEditMode()
303     {
304         return mode.equals(EDIT_MODE);
305     }
306     
307     public boolean getSpellMode()
308     {
309         return mode.equals(SPELL_MODE);
310     }
311     
312     public boolean getPreviewMode()
313     {
314         return mode.equals(PREVIEW_MODE);
315     }
316     
317     /**
318      * @return Returns the words.
319      */

320     public ArrayList JavaDoc getWords() {
321         return words;
322     }
323     /**
324      * @param words The words to set.
325      */

326     public void setWords(ArrayList JavaDoc words) {
327         this.words = words;
328     }
329 }
330
Popular Tags