001: package org.apache.jetspeed.portlets.admin;
002:
003: import java.io.File;
004: import java.io.FileOutputStream;
005: import java.io.IOException;
006: import java.util.HashMap;
007: import java.util.Iterator;
008: import java.util.Map;
009:
010: import javax.portlet.ActionRequest;
011: import javax.portlet.ActionResponse;
012: import javax.portlet.PortletConfig;
013: import javax.portlet.PortletException;
014: import javax.portlet.RenderRequest;
015: import javax.portlet.RenderResponse;
016:
017: import org.apache.commons.fileupload.FileItem;
018: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
019: import org.apache.commons.fileupload.portlet.PortletFileUpload;
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.apache.jetspeed.CommonPortletServices;
023: import org.apache.jetspeed.headerresource.HeaderResource;
024: import org.apache.jetspeed.security.GroupManager;
025: import org.apache.jetspeed.security.RoleManager;
026: import org.apache.jetspeed.security.UserManager;
027: import org.apache.jetspeed.serializer.JetspeedSerializer;
028: import org.apache.jetspeed.serializer.JetspeedSerializerFactory;
029: import org.apache.portals.gems.dojo.AbstractDojoVelocityPortlet;
030:
031: /**
032: * Manage the Portal Site
033: *
034: * @author <a href="mailto:firevelocity@gmail.com">Vivek Kumar</a>
035: *
036: * @version $Id$
037: */
038: public class JetspeedDataImporter extends AbstractDojoVelocityPortlet {
039:
040: protected final Log log = LogFactory.getLog(this .getClass());
041:
042: // components
043: protected UserManager userManager;
044:
045: protected GroupManager groupManager;
046:
047: protected RoleManager roleManager;
048:
049: private HashMap roleMap = new HashMap();
050:
051: private HashMap groupMap = new HashMap();
052:
053: private HashMap userMap = new HashMap();
054:
055: private HashMap mimeMap = new HashMap();
056:
057: private HashMap mimeMapInt = new HashMap();
058:
059: private HashMap mediaMap = new HashMap();
060:
061: private HashMap capabilityMap = new HashMap();
062:
063: private HashMap capabilityMapInt = new HashMap();
064:
065: private HashMap clientMap = new HashMap();
066:
067: private HashMap permissionMap = new HashMap();
068:
069: private HashMap rulesMap = new HashMap();
070:
071: int refCouter = 0;
072:
073: private static String ENCODING_STRING = "JETSPEED 2.1 - 2006";
074:
075: private static String JETSPEED = "JETSPEED";
076:
077: protected JetspeedSerializerFactory serializerFactory;
078:
079: protected void includeHeaderContent(HeaderResource headerResource) {
080: headerResource.dojoAddCoreLibraryRequire("dojo.lang.*");
081: // headerResource.dojoAddCoreLibraryRequire("dojo.dnd.*");
082: headerResource
083: .dojoAddCoreLibraryRequire("dojo.dnd.HtmlDragManager");
084: headerResource
085: .dojoAddCoreLibraryRequire("dojo.dnd.DragAndDrop");
086: headerResource
087: .dojoAddCoreLibraryRequire("dojo.dnd.HtmlDragAndDrop");
088:
089: headerResource.dojoAddCoreLibraryRequire("dojo.event.*");
090: headerResource.dojoAddCoreLibraryRequire("dojo.io");
091:
092: headerResource
093: .dojoAddCoreLibraryRequire("dojo.widget.ContentPane");
094: headerResource
095: .dojoAddCoreLibraryRequire("dojo.widget.LayoutContainer");
096:
097: headerResource.dojoAddCoreLibraryRequire("dojo.widget.Tree");
098: headerResource
099: .dojoAddCoreLibraryRequire("dojo.widget.TreeRPCController");
100: headerResource
101: .dojoAddCoreLibraryRequire("dojo.widget.TreeSelector");
102: headerResource
103: .dojoAddCoreLibraryRequire("dojo.widget.TreeNode");
104: headerResource
105: .dojoAddCoreLibraryRequire("dojo.widget.TreeContextMenu");
106:
107: headerResource
108: .dojoAddCoreLibraryRequire("dojo.widget.ValidationTextbox");
109: headerResource
110: .dojoAddCoreLibraryRequire("dojo.widget.ComboBox");
111: headerResource
112: .dojoAddCoreLibraryRequire("dojo.widget.Checkbox");
113: headerResource.dojoAddCoreLibraryRequire("dojo.widget.Dialog");
114: headerResource.dojoAddCoreLibraryRequire("dojo.widget.Button");
115:
116: headerResource
117: .dojoAddModuleLibraryRequire("jetspeed.desktop.core");
118: headerResource
119: .dojoAddModuleLibraryRequire("jetspeed.widget.EditorTable");
120: }
121:
122: public void init(PortletConfig config) throws PortletException {
123: super .init(config);
124: userManager = (UserManager) getPortletContext().getAttribute(
125: CommonPortletServices.CPS_USER_MANAGER_COMPONENT);
126: if (null == userManager) {
127: PortletException pe = new PortletException(
128: "Failed to find the User Manager on SiteViewController initialization");
129: throw new RuntimeException(pe);
130: }
131: groupManager = (GroupManager) getPortletContext().getAttribute(
132: CommonPortletServices.CPS_GROUP_MANAGER_COMPONENT);
133: if (null == groupManager) {
134: PortletException pe = new PortletException(
135: "Failed to find the Group Manager on SiteViewController initialization");
136: throw new RuntimeException(pe);
137: }
138: roleManager = (RoleManager) getPortletContext().getAttribute(
139: CommonPortletServices.CPS_ROLE_MANAGER_COMPONENT);
140: if (null == roleManager) {
141: PortletException pe = new PortletException(
142: "Failed to find the Group Manager on SiteViewController initialization");
143: throw new RuntimeException(pe);
144: }
145: serializerFactory = (JetspeedSerializerFactory) getPortletContext()
146: .getAttribute(
147: CommonPortletServices.CPS_JETSPEED_SERIALIZER_FACTORY);
148: if (null == serializerFactory) {
149: PortletException pe = new PortletException(
150: "Failed to find the SerializerFactory on SiteViewController initialization");
151: throw new RuntimeException(pe);
152: }
153:
154: }
155:
156: public void doView(RenderRequest request, RenderResponse response)
157: throws PortletException, IOException {
158: super .doView(request, response);
159: request.getPortletSession().removeAttribute("status");
160: request.getPortletSession().removeAttribute("msg");
161: }
162:
163: public void processAction(ActionRequest request,
164: ActionResponse actionResponse) throws PortletException,
165: java.io.IOException {
166: String export = request.getParameter("export");
167: String fileName = "";
168: String destPath = "";
169: String fileType = "";
170: String path = "";
171: String usrFolder = "";
172: boolean success = false;
173: String filePath = "";
174: cleanUserFolder(request.getUserPrincipal().toString());
175: try {
176: DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
177: PortletFileUpload portletFileUpload = new PortletFileUpload(
178: diskFileItemFactory);
179: if (PortletFileUpload.isMultipartContent(request)) {
180: Iterator fileIt = portletFileUpload.parseRequest(
181: request).iterator();
182: while (fileIt.hasNext()) {
183: FileItem fileItem = (FileItem) fileIt.next();
184: if (fileItem.getFieldName().equals("importFile")) {
185: synchronized (this ) {
186: fileName = fileItem.getName();
187: usrFolder = getTempFolder(request);
188: path = System.getProperty("file.separator");
189: filePath = usrFolder + path
190: + fileItem.getName();
191: FileOutputStream out = new FileOutputStream(
192: filePath);
193: out.write(fileItem.get());
194: out.close();
195: }
196:
197: }
198: }
199: success = importJetspeedData(filePath);
200: }
201: if (success) {
202: request.getPortletSession().setAttribute("status",
203: fileName);
204: request.getPortletSession().setAttribute("msg",
205: "File imported succesfully");
206: } else {
207: request.getPortletSession().setAttribute("status",
208: "false");
209: request
210: .getPortletSession()
211: .setAttribute("msg",
212: "Failed to import file. Please check XML file for correctness.");
213: }
214: } catch (Exception e) {
215: request.getPortletSession().setAttribute("status", "false");
216: request.getPortletSession().setAttribute("msg",
217: e.getMessage());
218: // throw new PortletException("Error occured in file uplodad");
219: }
220: }
221:
222: private boolean importJetspeedData(String filePath) {
223: try {
224: Map settings = new HashMap();
225: settings.put(JetspeedSerializer.KEY_PROCESS_USERS,
226: Boolean.TRUE);
227: settings.put(JetspeedSerializer.KEY_PROCESS_CAPABILITIES,
228: Boolean.TRUE);
229: settings.put(JetspeedSerializer.KEY_PROCESS_PROFILER,
230: Boolean.TRUE);
231: settings.put(
232: JetspeedSerializer.KEY_PROCESS_USER_PREFERENCES,
233: Boolean.TRUE);
234: settings.put(JetspeedSerializer.KEY_OVERWRITE_EXISTING,
235: Boolean.TRUE);
236: settings.put(JetspeedSerializer.KEY_BACKUP_BEFORE_PROCESS,
237: Boolean.FALSE);
238: JetspeedSerializer serializer = serializerFactory
239: .create(JetspeedSerializerFactory.PRIMARY);
240: try {
241: serializer.importData(filePath, settings);
242: } catch (Exception e) {
243: serializer = serializerFactory
244: .create(JetspeedSerializerFactory.SECONDARY);
245: serializer.importData(filePath, settings);
246: }
247: return true;
248: } catch (Exception e) {
249: return false;
250: }
251:
252: }
253:
254: private boolean cleanUserFolder(String userName) {
255: boolean success = false;
256: synchronized (this ) {
257: String tmpdir = System.getProperty("java.io.tmpdir");
258: String path = System.getProperty("file.separator");
259: String folder = tmpdir + path + userName;
260: File dir = new File(folder);
261: if (dir.exists()) {
262: success = deleteDir(dir);
263: }
264: success = dir.mkdir();
265: }
266: return success;
267: }
268:
269: private boolean deleteDir(File dir) {
270: if (dir.exists()) {
271: File[] files = dir.listFiles();
272: for (int i = 0; i < files.length; i++) {
273: if (files[i].isDirectory()) {
274: deleteDir(files[i]);
275: } else {
276: files[i].delete();
277: }
278: }
279: }
280: return (dir.delete());
281: }
282:
283: private String getTempFolder(ActionRequest request) {
284: String dir = System.getProperty("java.io.tmpdir");
285: String path = System.getProperty("file.separator");
286: File file = new File(dir + path + request.getUserPrincipal());
287: file.mkdir();
288: return dir + path + request.getUserPrincipal();
289: }
290: }
|