001    /**
002     * Copyright (c) 2000-2012 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 mapping, ActionForm form, PortletConfig portletConfig,
061                            ActionRequest actionRequest, ActionResponse actionResponse)
062                    throws Exception {
063    
064                    try {
065                            UploadPortletRequest uploadPortletRequest =
066                                    PortalUtil.getUploadPortletRequest(actionRequest);
067    
068                            checkExceededSizeLimit(uploadPortletRequest);
069    
070                            long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
071                            boolean privateLayout = ParamUtil.getBoolean(
072                                    uploadPortletRequest, "privateLayout");
073                            File file = uploadPortletRequest.getFile("importFileName");
074    
075                            if (!file.exists()) {
076                                    throw new LARFileException("Import file does not exist");
077                            }
078    
079                            LayoutServiceUtil.importLayouts(
080                                    groupId, privateLayout, actionRequest.getParameterMap(), file);
081    
082                            addSuccessMessage(actionRequest, actionResponse);
083    
084                            String redirect = ParamUtil.getString(actionRequest, "redirect");
085    
086                            sendRedirect(actionRequest, actionResponse, redirect);
087                    }
088                    catch (Exception e) {
089                            if ((e instanceof LARFileException) ||
090                                    (e instanceof LARFileSizeException) ||
091                                    (e instanceof LARTypeException)) {
092    
093                                    SessionErrors.add(actionRequest, e.getClass());
094                            }
095                            else if ((e instanceof LayoutPrototypeException) ||
096                                             (e instanceof LocaleException)) {
097    
098                                    SessionErrors.add(actionRequest, e.getClass(), e);
099                            }
100                            else {
101                                    _log.error(e, e);
102    
103                                    SessionErrors.add(
104                                            actionRequest, LayoutImportException.class.getName());
105                            }
106                    }
107            }
108    
109            @Override
110            public ActionForward render(
111                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
112                            RenderRequest renderRequest, RenderResponse renderResponse)
113                    throws Exception {
114    
115                    try {
116                            ActionUtil.getGroup(renderRequest);
117                    }
118                    catch (Exception e) {
119                            if (e instanceof NoSuchGroupException ||
120                                    e instanceof PrincipalException) {
121    
122                                    SessionErrors.add(renderRequest, e.getClass());
123    
124                                    return mapping.findForward("portlet.layouts_admin.error");
125                            }
126                            else {
127                                    throw e;
128                            }
129                    }
130    
131                    return mapping.findForward(
132                            getForward(renderRequest, "portlet.layouts_admin.export_layouts"));
133            }
134    
135            protected void checkExceededSizeLimit(HttpServletRequest request)
136                    throws PortalException {
137    
138                    UploadException uploadException = (UploadException)request.getAttribute(
139                            WebKeys.UPLOAD_EXCEPTION);
140    
141                    if (uploadException != null) {
142                            if (uploadException.isExceededSizeLimit()) {
143                                    throw new LARFileSizeException(uploadException.getCause());
144                            }
145    
146                            throw new PortalException(uploadException.getCause());
147                    }
148            }
149    
150            private static Log _log = LogFactoryUtil.getLog(ImportLayoutsAction.class);
151    
152    }