001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.communities.action;
016    
017    import com.liferay.portal.LARFileException;
018    import com.liferay.portal.LARTypeException;
019    import com.liferay.portal.LayoutImportException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.servlet.SessionErrors;
023    import com.liferay.portal.kernel.upload.UploadPortletRequest;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.service.LayoutServiceUtil;
026    import com.liferay.portal.util.PortalUtil;
027    
028    import java.io.File;
029    
030    import javax.portlet.ActionRequest;
031    import javax.portlet.ActionResponse;
032    import javax.portlet.PortletConfig;
033    
034    import org.apache.struts.action.ActionForm;
035    import org.apache.struts.action.ActionMapping;
036    
037    /**
038     * @author Alexander Chow
039     * @author Raymond Augé
040     */
041    public class ImportPagesAction extends EditPagesAction {
042    
043            public void processAction(
044                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
045                            ActionRequest actionRequest, ActionResponse actionResponse)
046                    throws Exception {
047    
048                    try {
049                            UploadPortletRequest uploadRequest =
050                                    PortalUtil.getUploadPortletRequest(actionRequest);
051    
052                            long groupId = ParamUtil.getLong(uploadRequest, "groupId");
053                            boolean privateLayout = ParamUtil.getBoolean(
054                                    uploadRequest, "privateLayout");
055                            File file = uploadRequest.getFile("importFileName");
056    
057                            if (!file.exists()) {
058                                    throw new LARFileException("Import file does not exist");
059                            }
060    
061                            LayoutServiceUtil.importLayouts(
062                                    groupId, privateLayout, actionRequest.getParameterMap(), file);
063    
064                            addSuccessMessage(actionRequest, actionResponse);
065                    }
066                    catch (Exception e) {
067                            if ((e instanceof LARFileException) ||
068                                    (e instanceof LARTypeException)) {
069    
070                                    SessionErrors.add(actionRequest, e.getClass().getName());
071                            }
072                            else {
073                                    _log.error(e, e);
074    
075                                    SessionErrors.add(
076                                            actionRequest, LayoutImportException.class.getName());
077                            }
078                    }
079            }
080    
081            private static Log _log = LogFactoryUtil.getLog(ImportPagesAction.class);
082    
083    }