001    /**
002     * Copyright (c) 2000-2011 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.portletconfiguration.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.NoSuchLayoutException;
021    import com.liferay.portal.PortletIdException;
022    import com.liferay.portal.kernel.exception.PortalException;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
026    import com.liferay.portal.kernel.servlet.SessionErrors;
027    import com.liferay.portal.kernel.staging.StagingUtil;
028    import com.liferay.portal.kernel.upload.UploadPortletRequest;
029    import com.liferay.portal.kernel.util.Constants;
030    import com.liferay.portal.kernel.util.ContentTypes;
031    import com.liferay.portal.kernel.util.FileUtil;
032    import com.liferay.portal.kernel.util.GetterUtil;
033    import com.liferay.portal.kernel.util.ParamUtil;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.model.Layout;
036    import com.liferay.portal.model.Portlet;
037    import com.liferay.portal.security.auth.PrincipalException;
038    import com.liferay.portal.service.LayoutLocalServiceUtil;
039    import com.liferay.portal.service.LayoutServiceUtil;
040    import com.liferay.portal.struts.ActionConstants;
041    import com.liferay.portal.theme.ThemeDisplay;
042    import com.liferay.portal.util.PortalUtil;
043    import com.liferay.portal.util.WebKeys;
044    import com.liferay.portlet.PortletPreferencesFactoryUtil;
045    
046    import java.io.File;
047    import java.io.FileInputStream;
048    
049    import java.util.Calendar;
050    import java.util.Date;
051    
052    import javax.portlet.ActionRequest;
053    import javax.portlet.ActionResponse;
054    import javax.portlet.PortletConfig;
055    import javax.portlet.PortletPreferences;
056    import javax.portlet.RenderRequest;
057    import javax.portlet.RenderResponse;
058    
059    import javax.servlet.http.HttpServletRequest;
060    import javax.servlet.http.HttpServletResponse;
061    
062    import org.apache.struts.action.ActionForm;
063    import org.apache.struts.action.ActionForward;
064    import org.apache.struts.action.ActionMapping;
065    
066    /**
067     * @author Jorge Ferrer
068     * @author Raymond Augé
069     */
070    public class ExportImportAction extends EditConfigurationAction {
071    
072            @Override
073            public void processAction(
074                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
075                            ActionRequest actionRequest, ActionResponse actionResponse)
076                    throws Exception {
077    
078                    Portlet portlet = null;
079    
080                    try {
081                            portlet = getPortlet(actionRequest);
082                    }
083                    catch (PrincipalException pe) {
084                            SessionErrors.add(
085                                    actionRequest, PrincipalException.class.getName());
086    
087                            setForward(actionRequest, "portlet.portlet_configuration.error");
088                    }
089    
090                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
091    
092                    try {
093                            if (cmd.equals("copy_from_live")) {
094                                    StagingUtil.copyFromLive(actionRequest, portlet);
095    
096                                    sendRedirect(actionRequest, actionResponse);
097                            }
098                            else if (cmd.equals(Constants.EXPORT)) {
099                                    exportData(actionRequest, actionResponse, portlet);
100                            }
101                            else if (cmd.equals(Constants.IMPORT)) {
102                                    importData(actionRequest, actionResponse, portlet);
103    
104                                    sendRedirect(actionRequest, actionResponse);
105                            }
106                            else if (cmd.equals("publish_to_live")) {
107                                    StagingUtil.publishToLive(actionRequest, portlet);
108    
109                                    sendRedirect(actionRequest, actionResponse);
110                            }
111                    }
112                    catch (Exception e) {
113                            if (e instanceof NoSuchLayoutException ||
114                                    e instanceof PrincipalException) {
115    
116                                    SessionErrors.add(actionRequest, e.getClass().getName());
117    
118                                    setForward(
119                                            actionRequest, "portlet.portlet_configuration.error");
120                            }
121                            else {
122                                    throw e;
123                            }
124                    }
125            }
126    
127            @Override
128            public ActionForward render(
129                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
130                            RenderRequest renderRequest, RenderResponse renderResponse)
131                    throws Exception {
132    
133                    Portlet portlet = null;
134    
135                    try {
136                            portlet = getPortlet(renderRequest);
137                    }
138                    catch (PrincipalException pe) {
139                            SessionErrors.add(
140                                    renderRequest, PrincipalException.class.getName());
141    
142                            return mapping.findForward("portlet.portlet_configuration.error");
143                    }
144    
145                    renderResponse.setTitle(getTitle(portlet, renderRequest));
146    
147                    return mapping.findForward(getForward(
148                            renderRequest, "portlet.portlet_configuration.export_import"));
149            }
150    
151            protected void exportData(
152                            ActionRequest actionRequest, ActionResponse actionResponse,
153                            Portlet portlet)
154                    throws Exception {
155    
156                    try {
157                            ThemeDisplay themeDisplay =
158                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
159    
160                            long plid = ParamUtil.getLong(actionRequest, "plid");
161                            long groupId = ParamUtil.getLong(actionRequest, "groupId");
162                            String fileName = ParamUtil.getString(
163                                    actionRequest, "exportFileName");
164                            String range = ParamUtil.getString(actionRequest, "range");
165    
166                            Date startDate = null;
167                            Date endDate = null;
168    
169                            if (range.equals("dateRange")) {
170                                    int startDateMonth = ParamUtil.getInteger(
171                                            actionRequest, "startDateMonth");
172                                    int startDateDay = ParamUtil.getInteger(
173                                            actionRequest, "startDateDay");
174                                    int startDateYear = ParamUtil.getInteger(
175                                            actionRequest, "startDateYear");
176                                    int startDateHour = ParamUtil.getInteger(
177                                            actionRequest, "startDateHour");
178                                    int startDateMinute = ParamUtil.getInteger(
179                                            actionRequest, "startDateMinute");
180                                    int startDateAmPm = ParamUtil.getInteger(
181                                            actionRequest, "startDateAmPm");
182    
183                                    if (startDateAmPm == Calendar.PM) {
184                                            startDateHour += 12;
185                                    }
186    
187                                    startDate = PortalUtil.getDate(
188                                            startDateMonth, startDateDay, startDateYear, startDateHour,
189                                            startDateMinute, themeDisplay.getTimeZone(),
190                                            new PortalException());
191    
192                                    int endDateMonth = ParamUtil.getInteger(
193                                            actionRequest, "endDateMonth");
194                                    int endDateDay = ParamUtil.getInteger(
195                                            actionRequest, "endDateDay");
196                                    int endDateYear = ParamUtil.getInteger(
197                                            actionRequest, "endDateYear");
198                                    int endDateHour = ParamUtil.getInteger(
199                                            actionRequest, "endDateHour");
200                                    int endDateMinute = ParamUtil.getInteger(
201                                            actionRequest, "endDateMinute");
202                                    int endDateAmPm = ParamUtil.getInteger(
203                                            actionRequest, "endDateAmPm");
204    
205                                    if (endDateAmPm == Calendar.PM) {
206                                            endDateHour += 12;
207                                    }
208    
209                                    endDate = PortalUtil.getDate(
210                                            endDateMonth, endDateDay, endDateYear, endDateHour,
211                                            endDateMinute, themeDisplay.getTimeZone(),
212                                            new PortalException());
213                            }
214                            else if (range.equals("fromLastPublishDate")) {
215                                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
216    
217                                    PortletPreferences preferences =
218                                            PortletPreferencesFactoryUtil.getPortletSetup(
219                                                    layout, portlet.getPortletId(), StringPool.BLANK);
220    
221                                    long lastPublishDate = GetterUtil.getLong(
222                                            preferences.getValue(
223                                                    "last-publish-date", StringPool.BLANK));
224    
225                                    if (lastPublishDate > 0) {
226                                            Calendar cal = Calendar.getInstance(
227                                                    themeDisplay.getTimeZone(), themeDisplay.getLocale());
228    
229                                            endDate = cal.getTime();
230    
231                                            cal.setTimeInMillis(lastPublishDate);
232    
233                                            startDate = cal.getTime();
234                                    }
235                            }
236    
237                            File file = LayoutServiceUtil.exportPortletInfoAsFile(
238                                    plid, groupId, portlet.getPortletId(),
239                                    actionRequest.getParameterMap(), startDate, endDate);
240    
241                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
242                                    actionRequest);
243                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
244                                    actionResponse);
245    
246                            ServletResponseUtil.sendFile(
247                                    request, response, fileName, new FileInputStream(file),
248                                    ContentTypes.APPLICATION_ZIP);
249    
250                            FileUtil.delete(file);
251    
252                            setForward(actionRequest, ActionConstants.COMMON_NULL);
253                    }
254                    catch (Exception e) {
255                            _log.error(e, e);
256                    }
257            }
258    
259            protected void importData(
260                            ActionRequest actionRequest, ActionResponse actionResponse,
261                            Portlet portlet)
262                    throws Exception {
263    
264                    try {
265                            UploadPortletRequest uploadPortletRequest =
266                                    PortalUtil.getUploadPortletRequest(actionRequest);
267    
268                            long plid = ParamUtil.getLong(uploadPortletRequest, "plid");
269                            long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
270                            File file = uploadPortletRequest.getFile("importFileName");
271    
272                            if (!file.exists()) {
273                                    throw new LARFileException("Import file does not exist");
274                            }
275    
276                            LayoutServiceUtil.importPortletInfo(
277                                    plid, groupId, portlet.getPortletId(),
278                                    actionRequest.getParameterMap(), file);
279    
280                            addSuccessMessage(actionRequest, actionResponse);
281                    }
282                    catch (Exception e) {
283                            if ((e instanceof LARFileException) ||
284                                    (e instanceof LARTypeException) ||
285                                    (e instanceof PortletIdException)) {
286    
287                                    SessionErrors.add(actionRequest, e.getClass().getName());
288                            }
289                            else {
290                                    _log.error(e, e);
291    
292                                    SessionErrors.add(
293                                            actionRequest, LayoutImportException.class.getName());
294                            }
295                    }
296            }
297    
298            private static Log _log = LogFactoryUtil.getLog(ExportImportAction.class);
299    
300    }