KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > weblog > formbeans > WeblogEntryFormEx


1 package org.roller.presentation.weblog.formbeans;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.roller.RollerException;
5 import org.roller.pojos.CommentData;
6 import org.roller.pojos.WeblogEntryData;
7 import org.roller.presentation.RollerRequest;
8 import org.roller.presentation.forms.WeblogEntryForm;
9 import org.roller.util.DateUtil;
10
11 import java.sql.Timestamp JavaDoc;
12 import java.text.DateFormat JavaDoc;
13 import java.text.ParseException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Calendar JavaDoc;
16 import java.util.Date JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Locale JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24 import org.roller.pojos.EntryAttributeData;
25 import org.roller.pojos.WebsiteData;
26
27 /**
28  * Extends the WeblogEntryForm so that additional properties may be added.
29  * @struts.form name="weblogEntryFormEx"
30  * @author dmj
31  */

32 public class WeblogEntryFormEx extends WeblogEntryForm
33 {
34     private String JavaDoc mCategoryId = null;
35     private Date JavaDoc mDate = new Date JavaDoc();
36     private String JavaDoc mDateString = null;
37     private Integer JavaDoc mHours = new Integer JavaDoc(0);
38     private Integer JavaDoc mMinutes = new Integer JavaDoc(0);
39     private Integer JavaDoc mSeconds = new Integer JavaDoc(0);
40     private String JavaDoc[] mReplacementWords = null;
41     private String JavaDoc[] pluginsArray = new String JavaDoc[0];
42     private String JavaDoc[] deleteComments = new String JavaDoc[0];
43     private String JavaDoc[] spamComments = new String JavaDoc[0];
44     private String JavaDoc trackbackUrl = null;
45     private Map JavaDoc attributes = new HashMap JavaDoc();
46     
47     public WeblogEntryFormEx()
48     {
49         super();
50     }
51
52     public WeblogEntryFormEx(WeblogEntryData entryData, java.util.Locale JavaDoc locale)
53         throws RollerException
54     {
55         copyFrom(entryData, locale);
56     }
57     
58     /**
59      * @param request
60      * @param response
61      */

62     public void initNew(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
63     {
64         RollerRequest rreq = RollerRequest.getRollerRequest(request);
65         if (rreq.getWebsite().getDefaultPlugins() != null)
66         {
67             setPluginsArray(StringUtils.split(
68                     rreq.getWebsite().getDefaultPlugins(), ",") );
69         }
70         allowComments = Boolean.TRUE;
71         updateTime = new Timestamp JavaDoc(new Date JavaDoc().getTime());
72         pubTime = updateTime;
73         initPubTimeDateStrings(rreq.getWebsite(), request.getLocale());
74     }
75     
76     /**
77      * Copy values from this Form to the WeblogEntryData.
78      */

79     public void copyTo(WeblogEntryData entry, Locale JavaDoc locale, Map JavaDoc paramMap)
80         throws RollerException
81     {
82         super.copyTo(entry, locale);
83         
84         // First parts the date string from the calendar
85
final DateFormat JavaDoc df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
86         final Date JavaDoc newDate;
87         try
88         {
89             newDate = df.parse(getDateString());
90         }
91         catch (ParseException JavaDoc e)
92         {
93             throw new RollerException("ERROR parsing date.");
94         }
95         
96         // Now handle the time from the hour, minute and second combos
97
final Calendar JavaDoc cal = Calendar.getInstance(locale);
98         cal.setTime(newDate);
99         cal.setTimeZone(entry.getWebsite().getTimeZoneInstance());
100         cal.set(Calendar.HOUR_OF_DAY, getHours().intValue());
101         cal.set(Calendar.MINUTE, getMinutes().intValue());
102         cal.set(Calendar.SECOND, getSeconds().intValue());
103         entry.setPubTime(new Timestamp JavaDoc(cal.getTimeInMillis()));
104         
105         entry.setPlugins( StringUtils.join(this.pluginsArray,",") );
106         
107         // checkboxes don't send a value for unchecked
108
if (entry.getPublishEntry() == null)
109         {
110             entry.setPublishEntry(Boolean.FALSE);
111         }
112         if (getCategoryId() != null)
113         {
114             entry.setCategoryId(getCategoryId());
115         }
116         
117         Iterator JavaDoc params = paramMap.keySet().iterator();
118         while (params.hasNext())
119         {
120             String JavaDoc name = (String JavaDoc)params.next();
121             String JavaDoc[] value = (String JavaDoc[])paramMap.get(name);
122             if (name.startsWith("att_") && value.length > 0)
123             {
124                 try
125                 {
126                     entry.putEntryAttribute(name, value[0]);
127                 }
128                 catch (Exception JavaDoc e)
129                 {
130                     throw new RollerException("ERROR setting attributes",e);
131                 }
132             }
133         }
134     }
135     
136     /**
137      * Copy values from WeblogEntryData to this Form.
138      */

139     public void copyFrom(WeblogEntryData entry, Locale JavaDoc locale)
140         throws RollerException
141     {
142         super.copyFrom(entry, locale);
143         mCategoryId = entry.getCategory().getId();
144         
145         initPubTimeDateStrings(entry.getWebsite(), locale);
146         
147         if (entry.getPlugins() != null)
148         {
149             pluginsArray = StringUtils.split(entry.getPlugins(), ",");
150         }
151         
152         attributes = new HashMap JavaDoc();
153         Iterator JavaDoc atts = entry.getEntryAttributes().iterator();
154         while (atts.hasNext())
155         {
156             EntryAttributeData att = (EntryAttributeData)atts.next();
157             attributes.put(att.getName(), att.getValue());
158         }
159       
160         populateSpamComments(entry);
161     }
162     
163     public Map JavaDoc getAttributes()
164     {
165         return attributes;
166     }
167     
168     /**
169      * Populate the spamComments array for this entry.
170      * @param entry
171      */

172     private void populateSpamComments(WeblogEntryData entry)
173     {
174         ArrayList JavaDoc spamList = new ArrayList JavaDoc();
175         Iterator JavaDoc it = entry.getComments(false).iterator();
176         while (it.hasNext()) {
177             CommentData comment = (CommentData)it.next();
178             if (comment.getSpam().booleanValue())
179             {
180                 spamList.add(comment.getId());
181             }
182         }
183         spamComments = (String JavaDoc[])spamList.toArray(new String JavaDoc[spamList.size()]);
184     }
185
186     /**
187      * Localize the PubTime date string.
188      * @param locale
189      */

190     private void initPubTimeDateStrings(WebsiteData website, Locale JavaDoc locale)
191     {
192         Calendar JavaDoc cal = Calendar.getInstance();
193         cal.setTime(getPubTime());
194         cal.setTimeZone(website.getTimeZoneInstance());
195         mHours = new Integer JavaDoc(cal.get(Calendar.HOUR_OF_DAY));
196         mMinutes = new Integer JavaDoc(cal.get(Calendar.MINUTE));
197         mSeconds = new Integer JavaDoc(cal.get(Calendar.SECOND));
198         
199         DateFormat JavaDoc df = DateFormat.getDateInstance(
200            DateFormat.SHORT, locale);
201         mDateString = df.format(getPubTime());
202     }
203
204     /**
205      * Returns the category ID.
206      * @return String
207      */

208     public String JavaDoc getCategoryId()
209     {
210         return mCategoryId;
211     }
212     
213     public Date JavaDoc getDate()
214     {
215         return mDate;
216     }
217
218     /**
219      * Date string formatted using SHORT format from user's locale.
220      * @return Returns the dateString.
221      */

222     public String JavaDoc getDateString()
223     {
224         return mDateString;
225     }
226
227     /**
228      * @return Returns the hours.
229      */

230     public Integer JavaDoc getHours()
231     {
232         return mHours;
233     }
234
235     /**
236      * @return Returns the minutes.
237      */

238     public Integer JavaDoc getMinutes()
239     {
240         return mMinutes;
241     }
242     
243     /**
244      * Returns the array of replacement words.
245      * @return String[]
246      */

247     public String JavaDoc[] getReplacementWords()
248     {
249         return mReplacementWords;
250     }
251
252     /**
253      * @return Returns the seconds.
254      */

255     public Integer JavaDoc getSeconds()
256     {
257         return mSeconds;
258     }
259     
260     /**
261      * Sets the category id.
262      * @param categoryId the category id to set
263      */

264     public void setCategoryId(String JavaDoc categoryId)
265     {
266         this.mCategoryId = categoryId;
267     }
268
269     /**
270      * Date string formatted using SHORT format from user's locale.
271      * @param dateString The dateString to set.
272      */

273     public void setDateString(String JavaDoc dateString) throws ParseException JavaDoc
274     {
275         mDateString = dateString;
276     }
277
278     /**
279      * @param hours The hours to set.
280      */

281     public void setHours(Integer JavaDoc hours)
282     {
283         mHours = hours;
284     }
285
286     /**
287      * @param minutes The minutes to set.
288      */

289     public void setMinutes(Integer JavaDoc minutes)
290     {
291         mMinutes = minutes;
292     }
293
294     public void setReplacementWords(String JavaDoc[] words)
295     {
296         this.mReplacementWords = words;
297     }
298
299     /**
300      * @param seconds The seconds to set.
301      */

302     public void setSeconds(Integer JavaDoc seconds)
303     {
304         mSeconds = seconds;
305     }
306
307     public String JavaDoc getDay()
308     {
309         java.util.Date JavaDoc theDay = getPubTime();
310         theDay = theDay!=null ? theDay : new java.util.Date JavaDoc();
311         return DateUtil.format8chars(theDay);
312     }
313
314     /**
315      * @return
316      */

317     public String JavaDoc[] getPluginsArray()
318     {
319         return pluginsArray;
320     }
321
322     /**
323      * @param strings
324      */

325     public void setPluginsArray(String JavaDoc[] strings)
326     {
327         pluginsArray = strings;
328     }
329     
330     public void doReset(
331             org.apache.struts.action.ActionMapping mapping,
332             javax.servlet.ServletRequest JavaDoc request)
333     {
334         super.doReset(mapping, request);
335         
336         pluginsArray = new String JavaDoc[0];
337         
338         // reset time fields to current time
339
Calendar JavaDoc cal = Calendar.getInstance(request.getLocale());
340         Date JavaDoc now = new Date JavaDoc();
341         cal.setTime(now);
342         mHours = new Integer JavaDoc(cal.get(Calendar.HOUR_OF_DAY));
343         mMinutes = new Integer JavaDoc(cal.get(Calendar.MINUTE));
344         mSeconds = new Integer JavaDoc(cal.get(Calendar.SECOND));
345         DateFormat JavaDoc df = DateFormat.getDateInstance(DateFormat.SHORT, request.getLocale());
346         mDateString = df.format(now);
347     }
348
349     /**
350      * @return Returns the selectedComments.
351      */

352     public String JavaDoc[] getDeleteComments() {
353         return deleteComments;
354     }
355     
356     /**
357      * @param selectedComments The selectedComments to set.
358      */

359     public void setDeleteComments(String JavaDoc[] selectedComments) {
360         this.deleteComments = selectedComments;
361     }
362     
363     /**
364      * @return Returns the commentsToMarkAsSpam.
365      */

366     public String JavaDoc[] getSpamComments() {
367         return spamComments;
368     }
369     
370     /**
371      * @param commentsToMarkAsSpam The commentsToMarkAsSpam to set.
372      */

373     public void setSpamComments(String JavaDoc[] commentsToMarkAsSpam) {
374         this.spamComments = commentsToMarkAsSpam;
375     }
376     
377     /**
378      * @return Returns the trackbackUrl.
379      */

380     public String JavaDoc getTrackbackUrl()
381     {
382         return trackbackUrl;
383     }
384     /**
385      * @param trackbackUrl The trackbackUrl to set.
386      */

387     public void setTrackbackUrl(String JavaDoc trackbackUrl)
388     {
389         this.trackbackUrl = trackbackUrl;
390     }
391 }
392
393
Popular Tags