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.calendar.action;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.upload.UploadPortletRequest;
021    import com.liferay.portal.kernel.util.CalendarUtil;
022    import com.liferay.portal.kernel.util.FileUtil;
023    import com.liferay.portal.kernel.util.StreamUtil;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.service.ServiceContextFactory;
026    import com.liferay.portal.struts.PortletAction;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portlet.calendar.ImportEventsException;
029    import com.liferay.portlet.calendar.model.CalEvent;
030    import com.liferay.portlet.calendar.service.CalEventServiceUtil;
031    
032    import java.io.InputStream;
033    
034    import javax.portlet.ActionRequest;
035    import javax.portlet.ActionResponse;
036    import javax.portlet.PortletConfig;
037    
038    import org.apache.struts.action.ActionForm;
039    import org.apache.struts.action.ActionMapping;
040    
041    /**
042     * @author Bruno Farache
043     * @author Juan Fernández
044     */
045    public class ImportEventsAction extends PortletAction {
046    
047            @Override
048            public void processAction(
049                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
050                            ActionRequest actionRequest, ActionResponse actionResponse)
051                    throws Exception {
052    
053                    InputStream inputStream = null;
054    
055                    try {
056                            UploadPortletRequest uploadPortletRequest =
057                                    PortalUtil.getUploadPortletRequest(actionRequest);
058    
059                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
060                                    CalEvent.class.getName(), actionRequest);
061    
062                            String fileName = uploadPortletRequest.getFileName("file");
063    
064                            validate(fileName);
065    
066                            inputStream = uploadPortletRequest.getFileAsStream("file");
067    
068                            CalEventServiceUtil.importICal4j(
069                                    serviceContext.getScopeGroupId(), inputStream);
070    
071                            sendRedirect(actionRequest, actionResponse);
072                    }
073                    catch (Exception e) {
074                            if (!(e instanceof ImportEventsException)) {
075                                    _log.error(e, e);
076                            }
077    
078                            SessionErrors.add(actionRequest, e.getClass().getName());
079    
080                            setForward(actionRequest, "portlet.calendar.error");
081                    }
082                    finally {
083                            StreamUtil.cleanUp(inputStream);
084                    }
085            }
086    
087            protected void validate(String fileName) throws ImportEventsException {
088                    String fileNameExtension = FileUtil.getExtension(fileName);
089    
090                    if (!fileNameExtension.equals(CalendarUtil.ICAL_EXTENSION)) {
091                            throw new ImportEventsException();
092                    }
093            }
094    
095            private static Log _log = LogFactoryUtil.getLog(ExportEventsAction.class);
096    
097    }