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