KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package org.roller.presentation.weblog.actions;
5
6 import java.io.File JavaDoc;
7 import java.io.IOException JavaDoc;
8 import java.util.ArrayList JavaDoc;
9
10 import javax.servlet.ServletContext JavaDoc;
11 import javax.servlet.ServletException JavaDoc;
12 import javax.servlet.http.HttpServletRequest JavaDoc;
13 import javax.servlet.http.HttpServletResponse JavaDoc;
14
15 import org.apache.struts.action.ActionError;
16 import org.apache.struts.action.ActionErrors;
17 import org.apache.struts.action.ActionForm;
18 import org.apache.struts.action.ActionForward;
19 import org.apache.struts.action.ActionMapping;
20 import org.apache.struts.action.ActionMessage;
21 import org.apache.struts.action.ActionMessages;
22 import org.apache.struts.actions.DispatchAction;
23 import org.roller.pojos.UserData;
24 import org.roller.presentation.MainPageAction;
25 import org.roller.presentation.RollerContext;
26 import org.roller.presentation.RollerRequest;
27 import org.roller.presentation.pagecache.PageCacheFilter;
28 import org.roller.presentation.weblog.formbeans.ImportEntriesForm;
29 import org.roller.util.StringUtils;
30
31 /**
32  * TODO: revisit this class once Atom 1.0 support comes to Rome
33  * @struts.action name="importEntries" path="/editor/importEntries"
34  * scope="request" parameter="method"
35  *
36  * @struts.action-forward name="importEntries.page" path="/weblog/import-entries.jsp"
37  *
38  * @author lance.lavandowska
39  */

40 public class ImportEntriesAction extends DispatchAction
41 {
42     public ActionForward importEntries(
43                               ActionMapping mapping,
44                               ActionForm actionForm,
45                               HttpServletRequest JavaDoc request,
46                               HttpServletResponse JavaDoc response)
47     throws IOException JavaDoc, ServletException JavaDoc
48     {
49         ActionForward forward = mapping.findForward("importEntries.page");
50         try
51         {
52             RollerRequest rreq = RollerRequest.getRollerRequest(request);
53             if ( !rreq.isUserAuthorizedToEdit() )
54             {
55                 forward = mapping.findForward("access-denied");
56             }
57             else
58             {
59                 getXmlFiles(actionForm, rreq);
60                 ImportEntriesForm form = (ImportEntriesForm)actionForm;
61                 if (StringUtils.isNotEmpty(form.getImportFileName()))
62                 {
63                     // "default" values
64
UserData user = rreq.getUser();
65
66                     // load selected file
67
ServletContext JavaDoc app = this.getServlet().getServletConfig().getServletContext();
68                     String JavaDoc dir = RollerContext.getUploadDir( app );
69                     File JavaDoc f = new File JavaDoc(dir + user.getUserName() +
70                                       "/" + form.getImportFileName());
71
72                     //ArchiveParser archiveParser =
73
//new ArchiveParser(rreq.getRoller(), rreq.getWebsite(), f);
74
String JavaDoc parseMessages = null; // archiveParser.parse();
75

76                     // buf will be non-zero if Entries were imported
77
if (parseMessages.length() > 0)
78                     {
79                         ActionMessages notices = new ActionMessages();
80                         notices.add(ActionMessages.GLOBAL_MESSAGE,
81                                      new ActionMessage("weblogEntryImport.importFiles",
82                                                    parseMessages));
83                         saveMessages(request, notices);
84
85                         // Flush the page cache
86
PageCacheFilter.removeFromCache( request, user );
87                         // refresh the front page cache
88
MainPageAction.flushMainPageCache();
89                     }
90                     else
91                     {
92                         ActionErrors errors = new ActionErrors();
93                         errors.add(ActionErrors.GLOBAL_ERROR,
94                                    new ActionError("error.importing.entries", ""));
95                         saveErrors(request,errors);
96                     }
97                 }
98             }
99         }
100         catch (Exception JavaDoc e)
101         {
102             request.getSession().getServletContext().log("ERROR",e);
103             throw new ServletException JavaDoc(e);
104         }
105         return forward;
106     }
107
108     /**
109      * Load list of XML files available for import.
110      * @param mapping
111      * @param actionForm
112      * @param request
113      * @param response
114      * @return
115      * @throws IOException
116      * @throws ServletException
117      */

118     public ActionForward edit(
119                               ActionMapping mapping,
120                               ActionForm actionForm,
121                               HttpServletRequest JavaDoc request,
122                               HttpServletResponse JavaDoc response)
123     throws IOException JavaDoc, ServletException JavaDoc
124     {
125         ActionForward forward = mapping.findForward("importEntries.page");
126         try
127         {
128             RollerRequest rreq = RollerRequest.getRollerRequest(request);
129             if ( !rreq.isUserAuthorizedToEdit() )
130             {
131                 forward = mapping.findForward("access-denied");
132             }
133             else
134             {
135                 getXmlFiles(actionForm, rreq);
136             }
137         }
138         catch (Exception JavaDoc e)
139         {
140             request.getSession().getServletContext().log("ERROR",e);
141             throw new ServletException JavaDoc(e);
142         }
143         return forward;
144     }
145
146     private void getXmlFiles(ActionForm actionForm, RollerRequest rreq)
147     {
148         ServletContext JavaDoc app = this.getServlet().getServletConfig().getServletContext();
149         String JavaDoc dir = RollerContext.getUploadDir( app );
150         File JavaDoc d = new File JavaDoc(dir + rreq.getUser().getUserName());
151         ArrayList JavaDoc xmlFiles = new ArrayList JavaDoc();
152         if (d.mkdirs() || d.exists())
153         {
154             File JavaDoc[] files = d.listFiles();
155             for (int i=0; i<files.length; i++)
156             {
157                 if (!files[i].isDirectory() &&
158                     files[i].getName().toLowerCase().endsWith(".xml"))
159                     // TODO : later change detection to use FileInfo
160
{
161                     xmlFiles.add(files[i].getName());
162                 }
163             }
164         }
165         ImportEntriesForm form = (ImportEntriesForm)actionForm;
166         form.setXmlFiles(xmlFiles);
167     }
168 }
169
Popular Tags