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.LARTypeException;
019    import com.liferay.portal.LayoutImportException;
020    import com.liferay.portal.NoSuchGroupException;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.servlet.SessionErrors;
024    import com.liferay.portal.kernel.upload.UploadPortletRequest;
025    import com.liferay.portal.kernel.util.ParamUtil;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.LayoutServiceUtil;
028    import com.liferay.portal.struts.PortletAction;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portlet.sites.action.ActionUtil;
031    
032    import java.io.File;
033    
034    import javax.portlet.ActionRequest;
035    import javax.portlet.ActionResponse;
036    import javax.portlet.PortletConfig;
037    import javax.portlet.RenderRequest;
038    import javax.portlet.RenderResponse;
039    
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionForward;
042    import org.apache.struts.action.ActionMapping;
043    
044    /**
045     * @author Alexander Chow
046     * @author Raymond Augé
047     */
048    public class ImportLayoutsAction extends PortletAction {
049    
050            @Override
051            public void processAction(
052                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
053                            ActionRequest actionRequest, ActionResponse actionResponse)
054                    throws Exception {
055    
056                    try {
057                            UploadPortletRequest uploadPortletRequest =
058                                    PortalUtil.getUploadPortletRequest(actionRequest);
059    
060                            long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
061                            boolean privateLayout = ParamUtil.getBoolean(
062                                    uploadPortletRequest, "privateLayout");
063                            File file = uploadPortletRequest.getFile("importFileName");
064    
065                            if (!file.exists()) {
066                                    throw new LARFileException("Import file does not exist");
067                            }
068    
069                            LayoutServiceUtil.importLayouts(
070                                    groupId, privateLayout, actionRequest.getParameterMap(), file);
071    
072                            addSuccessMessage(actionRequest, actionResponse);
073    
074                            String redirect = ParamUtil.getString(actionRequest, "redirect");
075    
076                            sendRedirect(actionRequest, actionResponse, redirect);
077                    }
078                    catch (Exception e) {
079                            if ((e instanceof LARFileException) ||
080                                    (e instanceof LARTypeException)) {
081    
082                                    SessionErrors.add(actionRequest, e.getClass().getName());
083                            }
084                            else {
085                                    _log.error(e, e);
086    
087                                    SessionErrors.add(
088                                            actionRequest, LayoutImportException.class.getName());
089                            }
090                    }
091            }
092    
093            @Override
094            public ActionForward render(
095                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
096                            RenderRequest renderRequest, RenderResponse renderResponse)
097                    throws Exception {
098    
099                    try {
100                            ActionUtil.getGroup(renderRequest);
101                    }
102                    catch (Exception e) {
103                            if (e instanceof NoSuchGroupException ||
104                                    e instanceof PrincipalException) {
105    
106                                    SessionErrors.add(renderRequest, e.getClass().getName());
107    
108                                    return mapping.findForward("portlet.layouts_admin.error");
109                            }
110                            else {
111                                    throw e;
112                            }
113                    }
114    
115                    return mapping.findForward(
116                            getForward(renderRequest, "portlet.layouts_admin.export_layouts"));
117            }
118    
119            private static Log _log = LogFactoryUtil.getLog(ImportLayoutsAction.class);
120    
121    }