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