1 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 ; 23 import java.net.MalformedURLException ; 24 import java.text.DateFormat ; 25 import java.text.ParseException ; 26 import java.text.SimpleDateFormat ; 27 import java.util.ArrayList ; 28 import java.util.Date ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Locale ; 33 import java.util.Map ; 34 35 import javax.servlet.ServletException ; 36 import javax.servlet.ServletRequest ; 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 import javax.servlet.jsp.JspFactory ; 40 import javax.servlet.jsp.PageContext ; 41 42 50 public class ExportEntriesAction extends DispatchAction 51 { 52 private SimpleDateFormat dayFormat = new SimpleDateFormat ("yyyyMMdd"); 53 private SimpleDateFormat monthFormat = new SimpleDateFormat ("yyyyMM"); 54 private SimpleDateFormat yearFormat = new SimpleDateFormat ("yyyy"); 55 56 public ActionForward unspecified( 57 ActionMapping mapping, 58 ActionForm actionForm, 59 HttpServletRequest request, 60 HttpServletResponse response) 61 throws IOException , ServletException 62 { 63 return edit(mapping, actionForm, request, response); 64 } 65 66 77 public ActionForward edit( 78 ActionMapping mapping, 79 ActionForm actionForm, 80 HttpServletRequest request, 81 HttpServletResponse response) 82 throws IOException , ServletException 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 e) 99 { 100 request.getSession().getServletContext().log("ERROR",e); 101 throw new ServletException (e); 102 } 103 return forward; 104 } 105 106 117 public ActionForward export( 118 ActionMapping mapping, 119 ActionForm actionForm, 120 HttpServletRequest request, 121 HttpServletResponse response) 122 throws IOException , ServletException 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 locale = Locale.getDefault(); final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); 136 Date startDate; 137 Date endDate; 138 try 139 { 140 startDate = DateUtil.getStartOfDay(df.parse(form.getStartDateString())); 141 endDate = DateUtil.getEndOfDay(df.parse(form.getEndDateString())); 142 } 143 catch (ParseException e) 144 { 145 throw new RollerException("ERROR parsing date:" + e.getMessage()); 146 } 147 148 if (startDate != null && endDate != null) 149 { 150 WeblogManager weblogMgr = 152 rreq.getRoller().getWeblogManager(); 153 154 158 List entries = weblogMgr.getWeblogEntries( 159 rreq.getWebsite(), startDate, endDate, null, WeblogManager.ALL, null); 166 ActionMessages messages = writeSuccessMessage(request, response, rreq, form); 167 168 Map entryMap = seperateByPeriod(entries, form.getFileBy()); 170 171 ExportRss exporter = new ExportRss(rreq.getWebsite()); 173 String exportTo = form.getExportFormat().toLowerCase(); 174 if ("atom".equals(exportTo)) 175 { 176 exporter.setExportAtom(true); 177 } 178 ArrayList fileNames = new ArrayList (); 179 Iterator it = entryMap.keySet().iterator(); 180 while(it.hasNext()) 181 { 182 String key = (String )it.next(); 183 ArrayList list = (ArrayList )entryMap.get(key); 184 exporter.exportEntries(list, key+"_"+exportTo+".xml"); 185 fileNames.add("Exported " + list.size() + " entry(s) to " + key+"_"+exportTo+".xml<br />"); 186 } 188 189 StringBuffer fileMessage = new StringBuffer (); 190 it = fileNames.iterator(); 191 while (it.hasNext()) 192 { 193 fileMessage.append((String )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 )request); 206 return edit(mapping, actionForm, request, response); 207 } 208 209 } 211 else 212 { 213 forward = mapping.findForward("access-denied"); 214 } 215 } 216 catch (Exception e) 217 { 218 request.getSession().getServletContext().log("ERROR",e); 219 throw new ServletException (e); 220 } 221 return forward; 222 } 223 224 232 private Map seperateByPeriod(List entries, String period) 233 { 234 HashMap map = new HashMap (); 235 WeblogEntryData entry = null; 236 String key = null; 237 ArrayList list = null; 238 SimpleDateFormat 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 it = entries.iterator(); 249 while (it.hasNext()) 250 { 251 entry = (WeblogEntryData)it.next(); 252 key = formatter.format(entry.getPubTime()); 253 list = (ArrayList )map.get(key); 254 if (list == null) 255 { 256 list = new ArrayList (); 257 map.put(key, list); 258 } 259 list.add(entry); 260 } 261 return map; 262 } 263 264 private ActionMessages writeSuccessMessage( 265 HttpServletRequest request, 266 HttpServletResponse response, 267 RollerRequest rreq, 268 WeblogQueryForm form) throws MalformedURLException 269 { 270 PageContext pageContext = 271 JspFactory.getDefaultFactory().getPageContext( 272 this.getServlet(), request, response, "", true, 8192, true); 273 Map params = new HashMap (); 274 params.put( RollerRequest.USERNAME_KEY, rreq.getUser().getUserName()); 275 params.put("rmik", "Files"); 276 String filesLink = RequestUtils.computeURL( 277 pageContext, (String )null, (String )null, (String )null, 278 "uploadFiles", params, (String )null, false); 279 280 String 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 |