1 package org.apache.jetspeed.portlets.admin;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.util.HashMap;
7 import java.util.Iterator;
8 import java.util.Map;
9
10 import javax.portlet.ActionRequest;
11 import javax.portlet.ActionResponse;
12 import javax.portlet.PortletConfig;
13 import javax.portlet.PortletException;
14 import javax.portlet.RenderRequest;
15 import javax.portlet.RenderResponse;
16
17 import org.apache.commons.fileupload.FileItem;
18 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
19 import org.apache.commons.fileupload.portlet.PortletFileUpload;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.jetspeed.CommonPortletServices;
23 import org.apache.jetspeed.headerresource.HeaderResource;
24 import org.apache.jetspeed.security.GroupManager;
25 import org.apache.jetspeed.security.RoleManager;
26 import org.apache.jetspeed.security.UserManager;
27 import org.apache.jetspeed.serializer.JetspeedSerializer;
28 import org.apache.jetspeed.serializer.JetspeedSerializerFactory;
29 import org.apache.portals.gems.dojo.AbstractDojoVelocityPortlet;
30
31 /***
32 * Manage the Portal Site
33 *
34 * @author <a href="mailto:firevelocity@gmail.com">Vivek Kumar</a>
35 *
36 * @version $Id$
37 */
38 public class JetspeedDataImporter extends AbstractDojoVelocityPortlet
39 {
40
41 protected final Log log = LogFactory.getLog(this.getClass());
42
43
44 protected UserManager userManager;
45
46 protected GroupManager groupManager;
47
48 protected RoleManager roleManager;
49
50 private HashMap roleMap = new HashMap();
51
52 private HashMap groupMap = new HashMap();
53
54 private HashMap userMap = new HashMap();
55
56 private HashMap mimeMap = new HashMap();
57
58 private HashMap mimeMapInt = new HashMap();
59
60 private HashMap mediaMap = new HashMap();
61
62 private HashMap capabilityMap = new HashMap();
63
64 private HashMap capabilityMapInt = new HashMap();
65
66 private HashMap clientMap = new HashMap();
67
68 private HashMap permissionMap = new HashMap();
69
70 private HashMap rulesMap = new HashMap();
71
72 int refCouter = 0;
73
74 private static String ENCODING_STRING = "JETSPEED 2.1 - 2006";
75
76 private static String JETSPEED = "JETSPEED";
77
78 protected JetspeedSerializerFactory serializerFactory;
79
80 protected void includeHeaderContent(HeaderResource headerResource)
81 {
82 headerResource.dojoAddCoreLibraryRequire("dojo.lang.*");
83
84 headerResource.dojoAddCoreLibraryRequire("dojo.dnd.HtmlDragManager");
85 headerResource.dojoAddCoreLibraryRequire("dojo.dnd.DragAndDrop");
86 headerResource.dojoAddCoreLibraryRequire("dojo.dnd.HtmlDragAndDrop");
87
88 headerResource.dojoAddCoreLibraryRequire("dojo.event.*");
89 headerResource.dojoAddCoreLibraryRequire("dojo.io");
90
91 headerResource.dojoAddCoreLibraryRequire("dojo.widget.ContentPane");
92 headerResource.dojoAddCoreLibraryRequire("dojo.widget.LayoutContainer");
93
94 headerResource.dojoAddCoreLibraryRequire("dojo.widget.Tree");
95 headerResource
96 .dojoAddCoreLibraryRequire("dojo.widget.TreeRPCController");
97 headerResource.dojoAddCoreLibraryRequire("dojo.widget.TreeSelector");
98 headerResource.dojoAddCoreLibraryRequire("dojo.widget.TreeNode");
99 headerResource.dojoAddCoreLibraryRequire("dojo.widget.TreeContextMenu");
100
101 headerResource
102 .dojoAddCoreLibraryRequire("dojo.widget.ValidationTextbox");
103 headerResource.dojoAddCoreLibraryRequire("dojo.widget.ComboBox");
104 headerResource.dojoAddCoreLibraryRequire("dojo.widget.Checkbox");
105 headerResource.dojoAddCoreLibraryRequire("dojo.widget.Dialog");
106 headerResource.dojoAddCoreLibraryRequire("dojo.widget.Button");
107
108 headerResource.dojoAddModuleLibraryRequire("jetspeed.desktop.core");
109 headerResource
110 .dojoAddModuleLibraryRequire("jetspeed.widget.EditorTable");
111 }
112
113 public void init(PortletConfig config) throws PortletException
114 {
115 super.init(config);
116 userManager = (UserManager) getPortletContext().getAttribute(
117 CommonPortletServices.CPS_USER_MANAGER_COMPONENT);
118 if (null == userManager)
119 {
120 PortletException pe = new PortletException(
121 "Failed to find the User Manager on SiteViewController initialization");
122 throw new RuntimeException(pe);
123 }
124 groupManager = (GroupManager) getPortletContext().getAttribute(
125 CommonPortletServices.CPS_GROUP_MANAGER_COMPONENT);
126 if (null == groupManager)
127 {
128 PortletException pe = new PortletException(
129 "Failed to find the Group Manager on SiteViewController initialization");
130 throw new RuntimeException(pe);
131 }
132 roleManager = (RoleManager) getPortletContext().getAttribute(
133 CommonPortletServices.CPS_ROLE_MANAGER_COMPONENT);
134 if (null == roleManager)
135 {
136 PortletException pe = new PortletException(
137 "Failed to find the Group Manager on SiteViewController initialization");
138 throw new RuntimeException(pe);
139 }
140 serializerFactory = (JetspeedSerializerFactory) getPortletContext().getAttribute(
141 CommonPortletServices.CPS_JETSPEED_SERIALIZER_FACTORY);
142 if (null == serializerFactory)
143 {
144 PortletException pe = new PortletException(
145 "Failed to find the SerializerFactory on SiteViewController initialization");
146 throw new RuntimeException(pe);
147 }
148
149 }
150
151 public void doView(RenderRequest request, RenderResponse response)
152 throws PortletException, IOException
153 {
154 super.doView(request, response);
155 request.getPortletSession().removeAttribute("status");
156 request.getPortletSession().removeAttribute("msg");
157 }
158
159 public void processAction(ActionRequest request,
160 ActionResponse actionResponse) throws PortletException,
161 java.io.IOException
162 {
163 String export = request.getParameter("export");
164 String fileName = "";
165 String destPath = "";
166 String fileType = "";
167 String path = "";
168 String usrFolder = "";
169 boolean success = false;
170 String filePath = "";
171 cleanUserFolder(request.getUserPrincipal().toString());
172 try
173 {
174 DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
175 PortletFileUpload portletFileUpload = new PortletFileUpload(
176 diskFileItemFactory);
177 if (PortletFileUpload.isMultipartContent(request))
178 {
179 Iterator fileIt = portletFileUpload.parseRequest(request)
180 .iterator();
181 while (fileIt.hasNext())
182 {
183 FileItem fileItem = (FileItem) fileIt.next();
184 if (fileItem.getFieldName().equals("importFile"))
185 {
186 synchronized (this)
187 {
188 fileName = fileItem.getName();
189 usrFolder = getTempFolder(request);
190 path = System.getProperty("file.separator");
191 filePath = usrFolder + path + fileItem.getName();
192 FileOutputStream out = new FileOutputStream(
193 filePath);
194 out.write(fileItem.get());
195 out.close();
196 }
197
198 }
199 }
200 success = importJetspeedData(filePath);
201 }
202 if (success)
203 {
204 request.getPortletSession().setAttribute("status", fileName);
205 request.getPortletSession().setAttribute("msg", "File imported succesfully");
206 }
207 else
208 {
209 request.getPortletSession().setAttribute("status", "false");
210 request.getPortletSession().setAttribute("msg", "Failed to import file. Please check XML file for correctness.");
211 }
212 } catch (Exception e)
213 {
214 request.getPortletSession().setAttribute("status", "false");
215 request.getPortletSession().setAttribute("msg", e.getMessage());
216
217 }
218 }
219
220 private boolean importJetspeedData(String filePath)
221 {
222 try
223 {
224 Map settings = new HashMap();
225 settings.put(JetspeedSerializer.KEY_PROCESS_USERS, Boolean.TRUE);
226 settings.put(JetspeedSerializer.KEY_PROCESS_CAPABILITIES,
227 Boolean.TRUE);
228 settings.put(JetspeedSerializer.KEY_PROCESS_PROFILER, Boolean.TRUE);
229 settings.put(JetspeedSerializer.KEY_PROCESS_USER_PREFERENCES,
230 Boolean.TRUE);
231 settings.put(JetspeedSerializer.KEY_OVERWRITE_EXISTING,
232 Boolean.TRUE);
233 settings.put(JetspeedSerializer.KEY_BACKUP_BEFORE_PROCESS,
234 Boolean.FALSE);
235 JetspeedSerializer serializer = serializerFactory.create(JetspeedSerializerFactory.PRIMARY);
236 try
237 {
238 serializer.importData(filePath, settings);
239 }
240 catch (Exception e)
241 {
242 serializer = serializerFactory.create(JetspeedSerializerFactory.SECONDARY);
243 serializer.importData(filePath, settings);
244 }
245 return true;
246 }
247 catch (Exception e)
248 {
249 return false;
250 }
251
252 }
253
254 private boolean cleanUserFolder(String userName)
255 {
256 boolean success = false;
257 synchronized (this)
258 {
259 String tmpdir = System.getProperty("java.io.tmpdir");
260 String path = System.getProperty("file.separator");
261 String folder = tmpdir + path + userName;
262 File dir = new File(folder);
263 if (dir.exists())
264 {
265 success = deleteDir(dir);
266 }
267 success = dir.mkdir();
268 }
269 return success;
270 }
271
272 private boolean deleteDir(File dir)
273 {
274 if (dir.exists())
275 {
276 File[] files = dir.listFiles();
277 for (int i = 0; i < files.length; i++)
278 {
279 if (files[i].isDirectory())
280 {
281 deleteDir(files[i]);
282 } else
283 {
284 files[i].delete();
285 }
286 }
287 }
288 return (dir.delete());
289 }
290
291 private String getTempFolder(ActionRequest request)
292 {
293 String dir = System.getProperty("java.io.tmpdir");
294 String path = System.getProperty("file.separator");
295 File file = new File(dir + path + request.getUserPrincipal());
296 file.mkdir();
297 return dir + path + request.getUserPrincipal();
298 }
299 }