KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > authoring > struts > formbeans > WeblogEntryFormEx


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 package org.apache.roller.ui.authoring.struts.formbeans;
19
20 import java.sql.Timestamp JavaDoc;
21 import java.text.DateFormat JavaDoc;
22 import java.text.ParseException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Calendar JavaDoc;
25 import java.util.TimeZone JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 import org.apache.commons.lang.StringUtils;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.apache.roller.RollerException;
39 import org.apache.roller.pojos.CommentData;
40 import org.apache.roller.pojos.EntryAttributeData;
41 import org.apache.roller.pojos.WeblogEntryData;
42 import org.apache.roller.pojos.WebsiteData;
43 import org.apache.roller.ui.core.RollerRequest;
44 import org.apache.roller.ui.core.RollerSession;
45 import org.apache.roller.ui.authoring.struts.forms.WeblogEntryForm;
46 import org.apache.roller.util.DateUtil;
47
48
49 /**
50  * Extends the WeblogEntryForm so that additional properties may be added.
51  * @struts.form name="weblogEntryFormEx"
52  * @author dmj
53  */

54 public class WeblogEntryFormEx extends WeblogEntryForm
55 {
56     private static Log mLogger =
57         LogFactory.getFactory().getInstance(WeblogEntryFormEx.class);
58     
59     private String JavaDoc mCategoryId = null;
60     private String JavaDoc mCreatorId = null;
61     private String JavaDoc mWebsiteId = null;
62     private Date JavaDoc mDate = new Date JavaDoc();
63     private String JavaDoc mDateString = null;
64     private Integer JavaDoc mHours = new Integer JavaDoc(0);
65     private Integer JavaDoc mMinutes = new Integer JavaDoc(0);
66     private Integer JavaDoc mSeconds = new Integer JavaDoc(0);
67     private String JavaDoc[] mReplacementWords = null;
68     private String JavaDoc[] pluginsArray = new String JavaDoc[0];
69     private String JavaDoc[] deleteComments = new String JavaDoc[0];
70     private String JavaDoc[] spamComments = new String JavaDoc[0];
71     private String JavaDoc trackbackUrl = null;
72     private Map JavaDoc attributes = new HashMap JavaDoc();
73     
74     public WeblogEntryFormEx()
75     {
76         super();
77         mLogger.debug("default construction");
78     }
79
80     public WeblogEntryFormEx(WeblogEntryData entryData, java.util.Locale JavaDoc locale)
81         throws RollerException
82     {
83         mLogger.debug("construction from existing entry");
84         copyFrom(entryData, locale);
85     }
86     
87     /**
88      * @param request
89      * @param response
90      */

91     public void initNew(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
92     {
93         mLogger.debug("init new called");
94         
95         RollerRequest rreq = RollerRequest.getRollerRequest(request);
96         RollerSession rses = RollerSession.getRollerSession(request);
97         if (rreq.getWebsite().getDefaultPlugins() != null)
98         {
99             setPluginsArray(StringUtils.split(
100                     rreq.getWebsite().getDefaultPlugins(), ",") );
101         }
102         status = WeblogEntryData.DRAFT;
103         allowComments = Boolean.TRUE;
104         locale = rreq.getWebsite().getLocale();
105         
106         // we want pubTime and updateTime to be empty for new entries -- AG
107
//updateTime = new Timestamp(new Date().getTime());
108
//pubTime = updateTime;
109
//initPubTimeDateStrings(rreq.getWebsite(), request.getLocale());
110
}
111     
112     /**
113      * Copy values from this Form to the WeblogEntryData.
114      */

115     public void copyTo(WeblogEntryData entry, Locale JavaDoc locale, Map JavaDoc paramMap)
116         throws RollerException
117     {
118         mLogger.debug("copy to called");
119         
120         super.copyTo(entry, locale);
121         
122         // calculate date for pubtime
123
if(getDateString() != null && !"0/0/0".equals(getDateString())) {
124             Date JavaDoc pubtime = null;
125             
126             TimeZone JavaDoc timezone = entry.getWebsite().getTimeZoneInstance();
127             try {
128                 DateFormat JavaDoc df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
129                 Date JavaDoc newDate = df.parse(getDateString());
130                 
131                 // Now handle the time from the hour, minute and second combos
132
if(newDate != null) {
133                     Calendar JavaDoc cal = Calendar.getInstance(locale);
134                     cal.setTime(newDate);
135                     cal.setTimeZone(timezone);
136                     cal.set(Calendar.HOUR_OF_DAY, getHours().intValue());
137                     cal.set(Calendar.MINUTE, getMinutes().intValue());
138                     cal.set(Calendar.SECOND, getSeconds().intValue());
139                     entry.setPubTime(new Timestamp JavaDoc(cal.getTimeInMillis()));
140                 }
141                 
142             } catch(Exception JavaDoc e) {
143                 mLogger.error(e);
144             }
145
146             mLogger.debug("set pubtime to "+entry.getPubTime());
147         }
148         
149         entry.setPlugins( StringUtils.join(this.pluginsArray,",") );
150         
151         if (getCategoryId() != null)
152         {
153             entry.setCategoryId(getCategoryId());
154         }
155         if (getAllowComments() == null)
156         {
157             entry.setAllowComments(Boolean.FALSE);
158         }
159         if (getRightToLeft() == null)
160         {
161             entry.setRightToLeft(Boolean.FALSE);
162         }
163         if (getPinnedToMain() == null)
164         {
165             entry.setPinnedToMain(Boolean.FALSE);
166         }
167         
168         Iterator JavaDoc params = paramMap.keySet().iterator();
169         while (params.hasNext())
170         {
171             String JavaDoc name = (String JavaDoc)params.next();
172             String JavaDoc[] value = (String JavaDoc[])paramMap.get(name);
173             if (name.startsWith("att_") && value.length > 0)
174             {
175                 try
176                 {
177                     entry.putEntryAttribute(name, value[0]);
178                 }
179                 catch (Exception JavaDoc e)
180                 {
181                     throw new RollerException("ERROR setting attributes",e);
182                 }
183             }
184         }
185     }
186     
187     /**
188      * Copy values from WeblogEntryData to this Form.
189      */

190     public void copyFrom(WeblogEntryData entry, Locale JavaDoc locale)
191         throws RollerException
192     {
193         mLogger.debug("copy from called");
194         
195         super.copyFrom(entry, locale);
196         mCategoryId = entry.getCategory().getId();
197         mCreatorId = entry.getCreator().getId();
198         mWebsiteId = entry.getWebsite().getId();
199         
200         initPubTimeDateStrings(entry.getWebsite(), locale);
201         
202         if (entry.getPlugins() != null)
203         {
204             pluginsArray = StringUtils.split(entry.getPlugins(), ",");
205         }
206         
207         attributes = new HashMap JavaDoc();
208         Iterator JavaDoc atts = entry.getEntryAttributes().iterator();
209         while (atts.hasNext())
210         {
211             EntryAttributeData att = (EntryAttributeData)atts.next();
212             attributes.put(att.getName(), att.getValue());
213         }
214     }
215     
216     public Map JavaDoc getAttributes()
217     {
218         return attributes;
219     }
220     
221     /**
222      * Localize the PubTime date string.
223      * @param locale
224      */

225     private void initPubTimeDateStrings(WebsiteData website, Locale JavaDoc locale)
226     {
227         mLogger.debug("init pub time date sting called");
228         
229         if(getPubTime() != null) {
230             mLogger.debug("figuring pubtime values");
231             
232             Calendar JavaDoc cal = Calendar.getInstance();
233             cal.setTime(getPubTime());
234             cal.setTimeZone(website.getTimeZoneInstance());
235             mHours = new Integer JavaDoc(cal.get(Calendar.HOUR_OF_DAY));
236             mMinutes = new Integer JavaDoc(cal.get(Calendar.MINUTE));
237             mSeconds = new Integer JavaDoc(cal.get(Calendar.SECOND));
238             
239             DateFormat JavaDoc df = DateFormat.getDateInstance(
240                     DateFormat.SHORT, locale);
241             df.setTimeZone(website.getTimeZoneInstance());
242             mDateString = df.format(getPubTime());
243             
244         } else {
245             mLogger.debug("pubtime is null, must be a draft");
246             
247             mDateString = "0/0/0";
248         }
249     }
250
251     /**
252      * Returns the category ID.
253      * @return String
254      */

255     public String JavaDoc getCategoryId()
256     {
257         return mCategoryId;
258     }
259     
260     public Date JavaDoc getDate()
261     {
262         return mDate;
263     }
264
265     /**
266      * Date string formatted using SHORT format from user's locale.
267      * @return Returns the dateString.
268      */

269     public String JavaDoc getDateString()
270     {
271         return mDateString;
272     }
273
274     /**
275      * @return Returns the hours.
276      */

277     public Integer JavaDoc getHours()
278     {
279         return mHours;
280     }
281
282     /**
283      * @return Returns the minutes.
284      */

285     public Integer JavaDoc getMinutes()
286     {
287         return mMinutes;
288     }
289     
290     /**
291      * Returns the array of replacement words.
292      * @return String[]
293      */

294     public String JavaDoc[] getReplacementWords()
295     {
296         return mReplacementWords;
297     }
298
299     /**
300      * @return Returns the seconds.
301      */

302     public Integer JavaDoc getSeconds()
303     {
304         return mSeconds;
305     }
306     
307     /**
308      * Sets the category id.
309      * @param categoryId the category id to set
310      */

311     public void setCategoryId(String JavaDoc categoryId)
312     {
313         this.mCategoryId = categoryId;
314     }
315
316     /**
317      * Date string formatted using SHORT format from user's locale.
318      * @param dateString The dateString to set.
319      */

320     public void setDateString(String JavaDoc dateString) throws ParseException JavaDoc
321     {
322         mLogger.debug("somebody setting date string");
323         mDateString = dateString;
324     }
325
326     /**
327      * @param hours The hours to set.
328      */

329     public void setHours(Integer JavaDoc hours)
330     {
331         mHours = hours;
332     }
333
334     /**
335      * @param minutes The minutes to set.
336      */

337     public void setMinutes(Integer JavaDoc minutes)
338     {
339         mMinutes = minutes;
340     }
341
342     public void setReplacementWords(String JavaDoc[] words)
343     {
344         this.mReplacementWords = words;
345     }
346
347     /**
348      * @param seconds The seconds to set.
349      */

350     public void setSeconds(Integer JavaDoc seconds)
351     {
352         mSeconds = seconds;
353     }
354
355     public String JavaDoc getDay()
356     {
357         java.util.Date JavaDoc theDay = getPubTime();
358         theDay = theDay!=null ? theDay : new java.util.Date JavaDoc();
359         return DateUtil.format8chars(theDay);
360     }
361
362     /**
363      * @return
364      */

365     public String JavaDoc[] getPluginsArray()
366     {
367         return pluginsArray;
368     }
369
370     /**
371      * @param strings
372      */

373     public void setPluginsArray(String JavaDoc[] strings)
374     {
375         pluginsArray = strings;
376     }
377     
378     public void doReset(
379             org.apache.struts.action.ActionMapping mapping,
380             javax.servlet.ServletRequest JavaDoc request)
381     {
382         super.doReset(mapping, request);
383         mLogger.debug("reset called");
384         
385         pluginsArray = new String JavaDoc[0];
386         
387         // reset time fields to current time
388
/* we want the date fields to be empty by default now -- Allen G
389         Calendar cal = Calendar.getInstance(request.getLocale());
390         Date now = new Date();
391         cal.setTime(now);
392         mHours = new Integer(cal.get(Calendar.HOUR_OF_DAY));
393         mMinutes = new Integer(cal.get(Calendar.MINUTE));
394         mSeconds = new Integer(cal.get(Calendar.SECOND));
395         DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, request.getLocale());
396         mDateString = df.format(now);
397         */

398         mDateString = "0/0/0";
399     }
400
401     /**
402      * @return Returns the selectedComments.
403      */

404     public String JavaDoc[] getDeleteComments() {
405         return deleteComments;
406     }
407     
408     /**
409      * @param selectedComments The selectedComments to set.
410      */

411     public void setDeleteComments(String JavaDoc[] selectedComments) {
412         this.deleteComments = selectedComments;
413     }
414     
415     /**
416      * @return Returns the commentsToMarkAsSpam.
417      */

418     public String JavaDoc[] getSpamComments() {
419         return spamComments;
420     }
421     
422     /**
423      * @param commentsToMarkAsSpam The commentsToMarkAsSpam to set.
424      */

425     public void setSpamComments(String JavaDoc[] commentsToMarkAsSpam) {
426         this.spamComments = commentsToMarkAsSpam;
427     }
428     
429     /**
430      * @return Returns the trackbackUrl.
431      */

432     public String JavaDoc getTrackbackUrl()
433     {
434         return trackbackUrl;
435     }
436     /**
437      * @param trackbackUrl The trackbackUrl to set.
438      */

439     public void setTrackbackUrl(String JavaDoc trackbackUrl)
440     {
441         this.trackbackUrl = trackbackUrl;
442     }
443
444     public String JavaDoc getCreatorId()
445     {
446         return mCreatorId;
447     }
448     public void setCreatorId(String JavaDoc creatorId)
449     {
450         mCreatorId = creatorId;
451     }
452
453     public String JavaDoc getWebsiteId()
454     {
455         return mWebsiteId;
456     }
457     public void setWebsiteId(String JavaDoc websiteId)
458     {
459         mWebsiteId = websiteId;
460     }
461
462     /** Convenience method for checking status */
463     public boolean isDraft()
464     {
465         return status.equals(WeblogEntryData.DRAFT);
466     }
467     
468     /** Convenience method for checking status */
469     public boolean isPending()
470     {
471         return status.equals(WeblogEntryData.PENDING);
472     }
473     
474     /** Convenience method for checking status */
475     public boolean isPublished()
476     {
477         return status.equals(WeblogEntryData.PUBLISHED);
478     }
479 }
480
481
Popular Tags