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