1
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
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 }