KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package org.roller.presentation.weblog.actions;
5
6 import org.apache.struts.action.ActionForm;
7 import org.apache.struts.action.ActionForward;
8 import org.apache.struts.action.ActionMapping;
9 import org.apache.struts.action.ActionMessage;
10 import org.apache.struts.action.ActionMessages;
11 import org.apache.struts.actions.DispatchAction;
12 import org.apache.struts.util.RequestUtils;
13 import org.roller.RollerException;
14 import org.roller.model.WeblogManager;
15 import org.roller.pojos.WeblogEntryData;
16 import org.roller.presentation.BasePageModel;
17 import org.roller.presentation.RollerRequest;
18 import org.roller.presentation.velocity.ExportRss;
19 import org.roller.presentation.weblog.formbeans.WeblogQueryForm;
20 import org.roller.util.DateUtil;
21
22 import java.io.IOException JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.text.DateFormat JavaDoc;
25 import java.text.ParseException JavaDoc;
26 import java.text.SimpleDateFormat JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Locale JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import javax.servlet.ServletException JavaDoc;
36 import javax.servlet.ServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import javax.servlet.http.HttpServletResponse JavaDoc;
39 import javax.servlet.jsp.JspFactory JavaDoc;
40 import javax.servlet.jsp.PageContext JavaDoc;
41
42 /**
43  * @struts.action path="/editor/exportEntries" name="weblogQueryForm"
44  * scope="request" parameter="method"
45  *
46  * @struts.action-forward name="exportEntries.page" path="/weblog/export-entries.jsp"
47  *
48  * @author lance.lavandowska
49  */

50 public class ExportEntriesAction extends DispatchAction
51 {
52     private SimpleDateFormat JavaDoc dayFormat = new SimpleDateFormat JavaDoc("yyyyMMdd");
53     private SimpleDateFormat JavaDoc monthFormat = new SimpleDateFormat JavaDoc("yyyyMM");
54     private SimpleDateFormat JavaDoc yearFormat = new SimpleDateFormat JavaDoc("yyyy");
55
56     public ActionForward unspecified(
57                               ActionMapping mapping,
58                               ActionForm actionForm,
59                               HttpServletRequest JavaDoc request,
60                               HttpServletResponse JavaDoc response)
61     throws IOException JavaDoc, ServletException JavaDoc
62     {
63         return edit(mapping, actionForm, request, response);
64     }
65     
66     /**
67      * Prepare the form for selecting date-range to export.
68      *
69      * @param mapping
70      * @param actionForm
71      * @param request
72      * @param response
73      * @return
74      * @throws IOException
75      * @throws ServletException
76      */

77     public ActionForward edit(
78                               ActionMapping mapping,
79                               ActionForm actionForm,
80                               HttpServletRequest JavaDoc request,
81                               HttpServletResponse JavaDoc response)
82     throws IOException JavaDoc, ServletException JavaDoc
83     {
84         ActionForward forward = mapping.findForward("exportEntries.page");
85         try
86         {
87             RollerRequest rreq = RollerRequest.getRollerRequest(request);
88             if ( !rreq.isUserAuthorizedToEdit() )
89             {
90                 forward = mapping.findForward("access-denied");
91             }
92             else
93             {
94                 request.setAttribute("model",
95                     new BasePageModel(request, response, mapping));
96             }
97         }
98         catch (Exception JavaDoc e)
99         {
100             request.getSession().getServletContext().log("ERROR",e);
101             throw new ServletException JavaDoc(e);
102         }
103         return forward;
104     }
105
106     /**
107      * Export entries from the requested date-range to XML.
108      *
109      * @param mapping
110      * @param actionForm
111      * @param request
112      * @param response
113      * @return
114      * @throws IOException
115      * @throws ServletException
116      */

117     public ActionForward export(
118                               ActionMapping mapping,
119                               ActionForm actionForm,
120                               HttpServletRequest JavaDoc request,
121                               HttpServletResponse JavaDoc response)
122     throws IOException JavaDoc, ServletException JavaDoc
123     {
124         ActionForward forward = mapping.findForward("exportEntries.page");
125         try
126         {
127             RollerRequest rreq = RollerRequest.getRollerRequest(request);
128             WeblogQueryForm form = (WeblogQueryForm)actionForm;
129             if ( rreq.isUserAuthorizedToEdit() )
130             {
131                 request.setAttribute("model",
132                                      new BasePageModel(request, response, mapping));
133                 
134                 Locale JavaDoc locale = Locale.getDefault();//rreq.getWebsite().getLocaleInstance();
135
final DateFormat JavaDoc df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
136                 Date JavaDoc startDate;
137                 Date JavaDoc endDate;
138                 try
139                 {
140                     startDate = DateUtil.getStartOfDay(df.parse(form.getStartDateString()));
141                     endDate = DateUtil.getEndOfDay(df.parse(form.getEndDateString()));
142                 }
143                 catch (ParseException JavaDoc e)
144                 {
145                     throw new RollerException("ERROR parsing date:" + e.getMessage());
146                 }
147                 
148                 if (startDate != null && endDate != null)
149                 {
150                     // this work should go into a Thread!
151
WeblogManager weblogMgr =
152                         rreq.getRoller().getWeblogManager();
153                     
154                     //List entries = weblogMgr.getWeblogEntriesInDateRange(
155
//rreq.getUser().getUserName(), null, startDate, endDate, false);
156
//System.out.println("Export: got " + entries.size() + " entries.");
157

158                     List JavaDoc entries = weblogMgr.getWeblogEntries(
159                                     rreq.getWebsite(), // userName
160
startDate, // startDate
161
endDate, // endDate
162
null, // catName
163
WeblogManager.ALL, // status
164
null); // maxEntries
165

166                     ActionMessages messages = writeSuccessMessage(request, response, rreq, form);
167
168                     // seperate the entries as specified: day, month, year
169
Map JavaDoc entryMap = seperateByPeriod(entries, form.getFileBy());
170
171                     // now export each List in the entryMap
172
ExportRss exporter = new ExportRss(rreq.getWebsite());
173                     String JavaDoc exportTo = form.getExportFormat().toLowerCase();
174                     if ("atom".equals(exportTo))
175                     {
176                         exporter.setExportAtom(true);
177                     }
178                     ArrayList JavaDoc fileNames = new ArrayList JavaDoc();
179                     Iterator JavaDoc it = entryMap.keySet().iterator();
180                     while(it.hasNext())
181                     {
182                         String JavaDoc key = (String JavaDoc)it.next();
183                         ArrayList JavaDoc list = (ArrayList JavaDoc)entryMap.get(key);
184                         exporter.exportEntries(list, key+"_"+exportTo+".xml");
185                         fileNames.add("Exported " + list.size() + " entry(s) to " + key+"_"+exportTo+".xml<br />");
186                         //System.out.println("Exported: " + list.size() + " entries for " + key);
187
}
188                     
189                     StringBuffer JavaDoc fileMessage = new StringBuffer JavaDoc();
190                     it = fileNames.iterator();
191                     while (it.hasNext())
192                     {
193                         fileMessage.append((String JavaDoc)it.next());
194                     }
195                     if (fileMessage.length() > 0)
196                     {
197                         messages.add(ActionMessages.GLOBAL_MESSAGE,
198                                      new ActionMessage("weblogEntryExport.exportFiles",
199                                                        fileMessage.toString()));
200                     }
201                     saveMessages(request, messages);
202                 }
203                 else
204                 {
205                     form.reset(mapping, (ServletRequest JavaDoc)request);
206                     return edit(mapping, actionForm, request, response);
207                 }
208
209                 //forward = mapping.findForward("exportEntries.done");
210
}
211             else
212             {
213                 forward = mapping.findForward("access-denied");
214             }
215         }
216         catch (Exception JavaDoc e)
217         {
218             request.getSession().getServletContext().log("ERROR",e);
219             throw new ServletException JavaDoc(e);
220         }
221         return forward;
222     }
223
224     /**
225      * Place entries into Lists, placed into date-related
226      * buckets. The individual buckets may represent a
227      * day, month, or year, depending on which the user specified.
228      *
229      * @param entries
230      * @return
231      */

232     private Map JavaDoc seperateByPeriod(List JavaDoc entries, String JavaDoc period)
233     {
234         HashMap JavaDoc map = new HashMap JavaDoc();
235         WeblogEntryData entry = null;
236         String JavaDoc key = null;
237         ArrayList JavaDoc list = null;
238         SimpleDateFormat JavaDoc formatter = monthFormat;
239         if ("year".equals(period))
240         {
241             formatter = yearFormat;
242         }
243         else if ("day".equals(period))
244         {
245             formatter = dayFormat;
246         }
247         
248         Iterator JavaDoc it = entries.iterator();
249         while (it.hasNext())
250         {
251             entry = (WeblogEntryData)it.next();
252             key = formatter.format(entry.getPubTime());
253             list = (ArrayList JavaDoc)map.get(key);
254             if (list == null)
255             {
256                 list = new ArrayList JavaDoc();
257                 map.put(key, list);
258             }
259             list.add(entry);
260         }
261         return map;
262     }
263
264     private ActionMessages writeSuccessMessage(
265                     HttpServletRequest JavaDoc request,
266                     HttpServletResponse JavaDoc response,
267                     RollerRequest rreq,
268                     WeblogQueryForm form) throws MalformedURLException JavaDoc
269     {
270         PageContext JavaDoc pageContext =
271             JspFactory.getDefaultFactory().getPageContext(
272                 this.getServlet(), request, response, "", true, 8192, true);
273         Map JavaDoc params = new HashMap JavaDoc();
274         params.put( RollerRequest.USERNAME_KEY, rreq.getUser().getUserName());
275         params.put("rmik", "Files");
276         String JavaDoc filesLink = RequestUtils.computeURL(
277              pageContext, (String JavaDoc)null, (String JavaDoc)null, (String JavaDoc)null,
278              "uploadFiles", params, (String JavaDoc)null, false);
279         
280         String JavaDoc message =
281             "Exporting Entries from " +
282             form.getStartDateString() + " to " +
283             form.getEndDateString() + ".<br />" +
284             "Check your <a HREF=\"" + filesLink + "\">Files</a>.";
285
286         ActionMessages messages = new ActionMessages();
287         messages.add(ActionMessages.GLOBAL_MESSAGE,
288                      new ActionMessage("weblogEntryExport.exportSuccess", message));
289                                                                       
290         return messages;
291     }
292 }
293
Popular Tags