001    /**
002     * Copyright (c) 2000-2013 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.layoutsadmin.action;
016    
017    import com.liferay.portal.LARFileException;
018    import com.liferay.portal.LARFileSizeException;
019    import com.liferay.portal.LARTypeException;
020    import com.liferay.portal.LayoutImportException;
021    import com.liferay.portal.LayoutPrototypeException;
022    import com.liferay.portal.LocaleException;
023    import com.liferay.portal.NoSuchGroupException;
024    import com.liferay.portal.kernel.exception.PortalException;
025    import com.liferay.portal.kernel.log.Log;
026    import com.liferay.portal.kernel.log.LogFactoryUtil;
027    import com.liferay.portal.kernel.servlet.SessionErrors;
028    import com.liferay.portal.kernel.upload.UploadException;
029    import com.liferay.portal.kernel.upload.UploadPortletRequest;
030    import com.liferay.portal.kernel.util.ParamUtil;
031    import com.liferay.portal.security.auth.PrincipalException;
032    import com.liferay.portal.service.LayoutServiceUtil;
033    import com.liferay.portal.struts.PortletAction;
034    import com.liferay.portal.util.PortalUtil;
035    import com.liferay.portal.util.WebKeys;
036    import com.liferay.portlet.sites.action.ActionUtil;
037    
038    import java.io.File;
039    
040    import javax.portlet.ActionRequest;
041    import javax.portlet.ActionResponse;
042    import javax.portlet.PortletConfig;
043    import javax.portlet.RenderRequest;
044    import javax.portlet.RenderResponse;
045    
046    import javax.servlet.http.HttpServletRequest;
047    
048    import org.apache.struts.action.ActionForm;
049    import org.apache.struts.action.ActionForward;
050    import org.apache.struts.action.ActionMapping;
051    
052    /**
053     * @author Alexander Chow
054     * @author Raymond Aug??
055     */
056    public class ImportLayoutsAction extends PortletAction {
057    
058            @Override
059            public void processAction(
060                            ActionMapping actionMapping, ActionForm actionForm,
061                            PortletConfig portletConfig, ActionRequest actionRequest,
062                            ActionResponse actionResponse)
063                    throws Exception {
064    
065                    try {
066                            UploadPortletRequest uploadPortletRequest =
067                                    PortalUtil.getUploadPortletRequest(actionRequest);
068    
069                            checkExceededSizeLimit(uploadPortletRequest);
070    
071                            long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
072                            boolean privateLayout = ParamUtil.getBoolean(
073                                    uploadPortletRequest, "privateLayout");
074                            File file = uploadPortletRequest.getFile("importFileName");
075    
076                            if (!file.exists()) {
077                                    throw new LARFileException("Import file does not exist");
078                            }
079    
080                            LayoutServiceUtil.importLayouts(
081                                    groupId, privateLayout, actionRequest.getParameterMap(), file);
082    
083                            addSuccessMessage(actionRequest, actionResponse);
084    
085                            String redirect = ParamUtil.getString(actionRequest, "redirect");
086    
087                            sendRedirect(actionRequest, actionResponse, redirect);
088                    }
089                    catch (Exception e) {
090                            if ((e instanceof LARFileException) ||
091                                    (e instanceof LARFileSizeException) ||
092                                    (e instanceof LARTypeException)) {
093    
094                                    SessionErrors.add(actionRequest, e.getClass());
095                            }
096                            else if ((e instanceof LayoutPrototypeException) ||
097                                             (e instanceof LocaleException)) {
098    
099                                    SessionErrors.add(actionRequest, e.getClass(), e);
100                            }
101                            else {
102                                    _log.error(e, e);
103    
104                                    SessionErrors.add(
105                                            actionRequest, LayoutImportException.class.getName());
106                            }
107                    }
108            }
109    
110            @Override
111            public ActionForward render(
112                            ActionMapping actionMapping, ActionForm actionForm,
113                            PortletConfig portletConfig, RenderRequest renderRequest,
114                            RenderResponse renderResponse)
115                    throws Exception {
116    
117                    try {
118                            ActionUtil.getGroup(renderRequest);
119                    }
120                    catch (Exception e) {
121                            if (e instanceof NoSuchGroupException ||
122                                    e instanceof PrincipalException) {
123    
124                                    SessionErrors.add(renderRequest, e.getClass());
125    
126                                    return actionMapping.findForward("portlet.layouts_admin.error");
127                            }
128                            else {
129                                    throw e;
130                            }
131                    }
132    
133                    return actionMapping.findForward(
134                            getForward(renderRequest, "portlet.layouts_admin.export_layouts"));
135            }
136    
137            protected void checkExceededSizeLimit(HttpServletRequest request)
138                    throws PortalException {
139    
140                    UploadException uploadException = (UploadException)request.getAttribute(
141                            WebKeys.UPLOAD_EXCEPTION);
142    
143                    if (uploadException != null) {
144                            if (uploadException.isExceededSizeLimit()) {
145                                    throw new LARFileSizeException(uploadException.getCause());
146                            }
147    
148                            throw new PortalException(uploadException.getCause());
149                    }
150            }
151    
152            private static Log _log = LogFactoryUtil.getLog(ImportLayoutsAction.class);
153    
154    }