KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > net > admin > DatabaseExport


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002,2003 Fraunhofer Gesellschaft
5  * Fraunhofer Institut for Computer Architecture and Software Technology
6  * All Rights Reserved.
7  *
8  * Please visit http://snipsnap.org/ for updates and contact.
9  *
10  * --LICENSE NOTICE--
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  * --LICENSE NOTICE--
25  */

26 package org.snipsnap.net.admin;
27
28 import org.snipsnap.config.Configuration;
29 import org.snipsnap.container.Components;
30 import org.snipsnap.snip.SnipSpace;
31 import org.snipsnap.snip.XMLSnipExport;
32 import org.snipsnap.user.UserManager;
33 import org.snipsnap.app.Application;
34 import org.snipsnap.jdbc.IntHolder;
35 import org.radeox.util.logging.Logger;
36
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import javax.servlet.http.HttpServletResponse JavaDoc;
39 import javax.servlet.http.HttpSession JavaDoc;
40 import java.io.File JavaDoc;
41 import java.io.FileOutputStream JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.OutputStream JavaDoc;
44 import java.io.BufferedOutputStream JavaDoc;
45 import java.text.SimpleDateFormat JavaDoc;
46 import java.util.Arrays JavaDoc;
47 import java.util.Date JavaDoc;
48 import java.util.List JavaDoc;
49 import java.util.Map JavaDoc;
50 import java.util.HashMap JavaDoc;
51
52 public class DatabaseExport implements SetupHandler {
53   private HashMap JavaDoc workerThreads = new HashMap JavaDoc();
54
55   public String JavaDoc getName() {
56     return "export";
57   }
58
59   public Map JavaDoc setup(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Configuration config, Map JavaDoc errors) {
60     String JavaDoc appOid = (String JavaDoc) Application.get().getObject(Application.OID);
61     ExportThread workerThread = (ExportThread) workerThreads.get(appOid);
62     if (workerThread != null && workerThread.isAlive()) {
63       setRunning(workerThread, request.getSession());
64       return errors;
65     } else if(workerThread != null) {
66       workerThreads.remove(appOid);
67       request.getSession().removeAttribute("running");
68       errors.put("message", "export.okay");
69       return errors;
70     }
71
72     String JavaDoc output = request.getParameter("export.file");
73     request.setAttribute("exportFile", output);
74     String JavaDoc exportTypes[] = request.getParameterValues("export.types");
75     request.setAttribute("exportTypes", exportTypes);
76
77     UserManager um = (UserManager) Components.getComponent(UserManager.class);
78     SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class);
79
80     List JavaDoc users = null;
81     List JavaDoc snips = null;
82
83     String JavaDoc exportMatch = request.getParameter("export.match");
84     if (null == exportMatch) {
85       exportMatch = "";
86     }
87     request.setAttribute("exportMatch", exportMatch);
88
89     String JavaDoc exportIgnore = request.getParameter("export.ignore");
90     request.setAttribute("exportIgnore", exportIgnore == null ? "" : exportIgnore);
91     if ("".equals(exportIgnore)) {
92       exportIgnore = null;
93     }
94
95     for (int i = 0; i < exportTypes.length; i++) {
96       if ("users".equals(exportTypes[i])) {
97         users = um.getAll();
98         request.setAttribute("exportTypeUsers", "true");
99       }
100       if ("snips".equals(exportTypes[i])) {
101         if (null != exportMatch && !"".equals(exportMatch)) {
102           snips = Arrays.asList(space.match(exportMatch));
103         } else {
104           snips = space.getAll();
105         }
106         request.setAttribute("exportTypeSnips", "true");
107       }
108     }
109
110     if (users == null && snips == null) {
111       errors.put("export.types", "export.types");
112       return errors;
113     }
114
115     OutputStream JavaDoc out = null;
116     try {
117       if ("webinf".equals(output)) {
118         SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc("yyyyMMdd");
119         File JavaDoc outFile = new File JavaDoc(config.getWebInfDir(),
120                                 config.getName() + "-" + df.format(new Date JavaDoc()) + ".snip");
121         workerThread = new ExportThread(new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(outFile)), snips, users, exportIgnore, config.getFilePath());
122         workerThread.start();
123         workerThreads.put(appOid, workerThread);
124         setRunning(workerThread, request.getSession());
125         return errors;
126
127       } else if ("download".equals(output)) {
128         response.setContentType("text/xml");
129         out = response.getOutputStream();
130       } else {
131         errors.put("message", "export.failed");
132         return errors;
133       }
134
135       XMLSnipExport.store(new BufferedOutputStream JavaDoc(out), snips, users, exportIgnore, null, config.getFilePath());
136     } catch (IOException JavaDoc e) {
137       errors.put("message", "export.failed");
138     }
139
140     if(errors.size() == 0) {
141       return null;
142     }
143     return errors;
144   }
145
146   private void setRunning(ExportThread workerThread, HttpSession JavaDoc session) {
147     Map JavaDoc statusMap = (Map JavaDoc) session.getAttribute("running");
148     if (null == statusMap) {
149       statusMap = new HashMap JavaDoc();
150     }
151     statusMap.put("max", new Integer JavaDoc(workerThread.getMax()));
152     statusMap.put("current", new Integer JavaDoc(workerThread.getCurrent()));
153     statusMap.put("export", "true");
154     session.setAttribute("running", statusMap);
155
156   }
157
158   class ExportThread extends Thread JavaDoc {
159     private OutputStream JavaDoc out;
160     private List JavaDoc snips, users;
161     private String JavaDoc exportIgnore;
162     private File JavaDoc filePath;
163
164     private int maxValue = 0;
165     private IntHolder status;
166
167     public int getMax() {
168       return maxValue;
169     }
170
171     public int getCurrent() {
172       if(null == status) {
173         return 0;
174       }
175       return status.getValue();
176     }
177
178     public ExportThread(OutputStream JavaDoc out, List JavaDoc snips, List JavaDoc users, String JavaDoc ignore, File JavaDoc filePath) {
179       this.out = out;
180       this.snips = snips;
181       this.users = users;
182       this.exportIgnore = ignore;
183       this.filePath = filePath;
184       maxValue = (snips != null ? snips.size() : 0) + (users != null ? users.size() : 0);
185     }
186
187     public void run() {
188       status = XMLSnipExport.getStatus();
189       XMLSnipExport.store(out, snips, users, exportIgnore, null, filePath);
190       try {
191         out.close();
192       } catch (IOException JavaDoc e) {
193         Logger.warn("DatabaseExport: unable to close document", e);
194       }
195     }
196   }
197 }
Popular Tags