001    /**
002     * Copyright (c) 2000-2013 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 actionMapping, ActionForm actionForm,
050                            PortletConfig portletConfig, ActionRequest actionRequest,
051                            ActionResponse actionResponse)
052                    throws Exception {
053    
054                    InputStream inputStream = null;
055    
056                    try {
057                            UploadPortletRequest uploadPortletRequest =
058                                    PortalUtil.getUploadPortletRequest(actionRequest);
059    
060                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
061                                    CalEvent.class.getName(), actionRequest);
062    
063                            String fileName = uploadPortletRequest.getFileName("file");
064    
065                            validate(fileName);
066    
067                            inputStream = uploadPortletRequest.getFileAsStream("file");
068    
069                            CalEventServiceUtil.importICal4j(
070                                    serviceContext.getScopeGroupId(), inputStream);
071    
072                            sendRedirect(actionRequest, actionResponse);
073                    }
074                    catch (Exception e) {
075                            if (!(e instanceof ImportEventsException)) {
076                                    _log.error(e, e);
077                            }
078    
079                            SessionErrors.add(actionRequest, e.getClass());
080    
081                            setForward(actionRequest, "portlet.calendar.error");
082                    }
083                    finally {
084                            StreamUtil.cleanUp(inputStream);
085                    }
086            }
087    
088            protected void validate(String fileName) throws ImportEventsException {
089                    String fileNameExtension = FileUtil.getExtension(fileName);
090    
091                    if (!fileNameExtension.equals(CalendarUtil.ICAL_EXTENSION)) {
092                            throw new ImportEventsException();
093                    }
094            }
095    
096            private static Log _log = LogFactoryUtil.getLog(ImportEventsAction.class);
097    
098    }