001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.LayoutPrototypeException;
021    import com.liferay.portal.LocaleException;
022    import com.liferay.portal.NoSuchGroupException;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.servlet.SessionErrors;
026    import com.liferay.portal.kernel.upload.UploadPortletRequest;
027    import com.liferay.portal.kernel.util.ParamUtil;
028    import com.liferay.portal.security.auth.PrincipalException;
029    import com.liferay.portal.service.LayoutServiceUtil;
030    import com.liferay.portal.struts.PortletAction;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portlet.sites.action.ActionUtil;
033    
034    import java.io.File;
035    
036    import javax.portlet.ActionRequest;
037    import javax.portlet.ActionResponse;
038    import javax.portlet.PortletConfig;
039    import javax.portlet.RenderRequest;
040    import javax.portlet.RenderResponse;
041    
042    import org.apache.struts.action.ActionForm;
043    import org.apache.struts.action.ActionForward;
044    import org.apache.struts.action.ActionMapping;
045    
046    /**
047     * @author Alexander Chow
048     * @author Raymond Augé
049     */
050    public class ImportLayoutsAction extends PortletAction {
051    
052            @Override
053            public void processAction(
054                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
055                            ActionRequest actionRequest, ActionResponse actionResponse)
056                    throws Exception {
057    
058                    try {
059                            UploadPortletRequest uploadPortletRequest =
060                                    PortalUtil.getUploadPortletRequest(actionRequest);
061    
062                            long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
063                            boolean privateLayout = ParamUtil.getBoolean(
064                                    uploadPortletRequest, "privateLayout");
065                            File file = uploadPortletRequest.getFile("importFileName");
066    
067                            if (!file.exists()) {
068                                    throw new LARFileException("Import file does not exist");
069                            }
070    
071                            LayoutServiceUtil.importLayouts(
072                                    groupId, privateLayout, actionRequest.getParameterMap(), file);
073    
074                            addSuccessMessage(actionRequest, actionResponse);
075    
076                            String redirect = ParamUtil.getString(actionRequest, "redirect");
077    
078                            sendRedirect(actionRequest, actionResponse, redirect);
079                    }
080                    catch (Exception e) {
081                            if ((e instanceof LARFileException) ||
082                                    (e instanceof LARTypeException)) {
083    
084                                    SessionErrors.add(actionRequest, e.getClass());
085                            }
086                            else if ((e instanceof LayoutPrototypeException) ||
087                                             (e instanceof LocaleException)) {
088    
089                                    SessionErrors.add(actionRequest, e.getClass(), e);
090                            }
091                            else {
092                                    _log.error(e, e);
093    
094                                    SessionErrors.add(
095                                            actionRequest, LayoutImportException.class.getName());
096                            }
097                    }
098            }
099    
100            @Override
101            public ActionForward render(
102                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
103                            RenderRequest renderRequest, RenderResponse renderResponse)
104                    throws Exception {
105    
106                    try {
107                            ActionUtil.getGroup(renderRequest);
108                    }
109                    catch (Exception e) {
110                            if (e instanceof NoSuchGroupException ||
111                                    e instanceof PrincipalException) {
112    
113                                    SessionErrors.add(renderRequest, e.getClass());
114    
115                                    return mapping.findForward("portlet.layouts_admin.error");
116                            }
117                            else {
118                                    throw e;
119                            }
120                    }
121    
122                    return mapping.findForward(
123                            getForward(renderRequest, "portlet.layouts_admin.export_layouts"));
124            }
125    
126            private static Log _log = LogFactoryUtil.getLog(ImportLayoutsAction.class);
127    
128    }