1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.calendar.action;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.servlet.SessionErrors;
20  import com.liferay.portal.kernel.upload.UploadPortletRequest;
21  import com.liferay.portal.kernel.util.CalendarUtil;
22  import com.liferay.portal.kernel.util.FileUtil;
23  import com.liferay.portal.service.ServiceContext;
24  import com.liferay.portal.service.ServiceContextFactory;
25  import com.liferay.portal.struts.PortletAction;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.portlet.calendar.ImportEventsException;
28  import com.liferay.portlet.calendar.model.CalEvent;
29  import com.liferay.portlet.calendar.service.CalEventServiceUtil;
30  
31  import java.io.File;
32  
33  import javax.portlet.ActionRequest;
34  import javax.portlet.ActionResponse;
35  import javax.portlet.PortletConfig;
36  
37  import org.apache.struts.action.ActionForm;
38  import org.apache.struts.action.ActionMapping;
39  
40  /**
41   * <a href="ImportEventsAction.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Bruno Farache
44   * @author Juan Fernández
45   */
46  public class ImportEventsAction extends PortletAction {
47  
48      public void processAction(
49              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
50              ActionRequest actionRequest, ActionResponse actionResponse)
51          throws Exception {
52  
53          try {
54              UploadPortletRequest uploadRequest =
55                  PortalUtil.getUploadPortletRequest(actionRequest);
56  
57              ServiceContext serviceContext = ServiceContextFactory.getInstance(
58                  CalEvent.class.getName(), actionRequest);
59  
60              File file = uploadRequest.getFile("file");
61  
62              validate(file);
63  
64              CalEventServiceUtil.importICal4j(
65                  serviceContext.getScopeGroupId(), file);
66  
67              sendRedirect(actionRequest, actionResponse);
68          }
69          catch (Exception e) {
70              if (!(e instanceof ImportEventsException)) {
71                  _log.error(e, e);
72              }
73  
74              SessionErrors.add(actionRequest, e.getClass().getName());
75  
76              setForward(actionRequest, "portlet.calendar.error");
77          }
78      }
79  
80      private void validate(File file) throws ImportEventsException {
81          String fileNameExtension = FileUtil.getExtension(file.getName());
82  
83          if (!fileNameExtension.equals(CalendarUtil.ICAL_EXTENSION)) {
84              throw new ImportEventsException();
85          }
86      }
87  
88      private static Log _log = LogFactoryUtil.getLog(ExportEventsAction.class);
89  
90  }