/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) 1999-2006 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/
package org.olat.commons.calendar.ui;
import org.olat.core.commons.modules.bc.FolderConfig;
import org.olat.core.gui.components.Component;
import org.olat.core.gui.components.form.Form;
import org.olat.core.gui.components.link.Link;
import org.olat.core.gui.components.link.LinkFactory;
import org.olat.core.gui.components.panel.Panel;
import org.olat.core.gui.components.velocity.VelocityContainer;
import org.olat.core.gui.control.controller.BasicController;
import org.olat.core.gui.control.Event;
import org.olat.core.gui.control.WindowControl;
import org.olat.core.gui.translator.PackageTranslator;
import org.olat.core.gui.translator.Translator;
import org.olat.core.gui.UserRequest;
import org.olat.core.logging.Tracing;
import org.olat.core.logging.OLATRuntimeException;
import org.olat.core.util.Util;
import org.olat.commons.calendar.CalendarManager;
import org.olat.commons.calendar.CalendarManagerFactory;
import org.olat.commons.calendar.ImportCalendarManager;
import com.oreilly.servlet.multipart.FilePart;
import com.oreilly.servlet.multipart.MultipartParser;
import com.oreilly.servlet.multipart.ParamPart;
import com.oreilly.servlet.multipart.Part;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.lang.StringBuffer;
import java.net.URL;
import java.util.Locale;
/**
* Description:<BR>
* <P>
* Initial Date: August 22, 2008
*
* @author Udit Sajjanhar
*/
public class CalendarImportByUrlController extends BasicController {
private static final String PACKAGE = Util.getPackageName(CalendarManager.class);
private static final String VELOCITY_ROOT = Util.getPackageVelocityRoot(CalendarManager.class);
private static final String COMMAND_PROCESS_UPLOAD = "purl";
private VelocityContainer importVC;
private Translator translator;
private static final String COMMAND_PROCESS_URL = "purl";
private CalendarImportUrlForm importUrlForm;
private Link cancelButton;
private Panel panel;
CalendarImportByUrlController(UserRequest ureq, Locale locale, WindowControl wControl) {
super(ureq, wControl);
translator = new PackageTranslator(PACKAGE, locale);
importVC = new VelocityContainer("calmanage", VELOCITY_ROOT + "/calImportByUrl.html", translator, this);
importUrlForm = new CalendarImportUrlForm("calImportName", translator);
importUrlForm.addListener(this);
panel = new Panel("panel");
panel.setContent(importUrlForm);
importVC.put("urlinput", panel);
cancelButton = LinkFactory.createButton("cancel", importVC, this);
putInitialPanel(importVC);
}
/**
* @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest, org.olat.core.gui.components.Component, org.olat.core.gui.control.Event)
*/
public void event(UserRequest ureq, Component source, Event event) {
if (source == cancelButton) {
fireEvent(ureq, Event.CANCELLED_EVENT);
} else if (source == importUrlForm) {
if (event == Form.EVNT_VALIDATION_OK) {
try {
String calendarContent = ImportCalendarManager.getContentFromUrl(importUrlForm.getCalendarUrl());
processCalendarUrl(ureq, calendarContent);
} catch (IOException e) {
getWindowControl().setError(translator.translate("cal.import.url.invalid"));
return;
}
} else if (event == Form.EVNT_FORM_CANCELLED) {
fireEvent(ureq, Event.CANCELLED_EVENT);
}
}
}
private void processCalendarUrl(UserRequest ureq, String content) {
try {
// store the content of the url in a file by a temporary name
CalendarManager calManager = CalendarManagerFactory.getInstance().getCalendarManager();
String calID = ImportCalendarManager.getTempCalendarIDForUpload(ureq);
File tmpFile = calManager.getCalendarFile(CalendarManager.TYPE_USER, calID);
BufferedWriter output = new BufferedWriter(new FileWriter(tmpFile));
output.write(content);
output.close();
// try to parse the tmp file
Object calendar = calManager.readCalendar(CalendarManager.TYPE_USER, calID);
if (calendar != null) {
fireEvent(ureq, Event.DONE_EVENT);
} else {
getWindowControl().setError(translator.translate("cal.import.url.content.invalid"));
}
} catch (IOException e){
getWindowControl().setError(translator.translate("cal.import.url.file.write.error"));
} catch (OLATRuntimeException e) {
getWindowControl().setError(translator.translate("cal.import.url.content.invalid"));
}
}
/**
*
* @see org.olat.core.gui.control.DefaultController#doDispose(boolean)
*/
protected void doDispose() {
// do nothing here yet
}
public String getImportUrl() {
return importUrlForm.getCalendarUrl();
}
}