/*
* Copyright 2009 Peter Karich, peat_hal ‘at’ users ‘dot’ sourceforge ‘dot’ net.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* under the License.
*/
package de.timefinder.core.ui.command;
import de.timefinder.data.Event;
import de.timefinder.data.ICalendarSettings;
import de.timefinder.data.JavaOneInterval;
import de.timefinder.data.Person;
import de.timefinder.data.Role;
import de.timefinder.data.IntervalInt;
import de.timefinder.data.access.Dao;
import de.timefinder.core.io.ImportJavaOne;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;
import javolution.util.FastMap;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.springframework.core.io.UrlResource;
import org.springframework.richclient.application.Application;
import org.springframework.richclient.application.statusbar.StatusBar;
import org.springframework.richclient.progress.ProgressMonitor;
/**
* This UI class provides the Import class to the user
*
* @author Peter Karich, peat_hal ‘at’ users ‘dot’ sourceforge ‘dot’ net
*/
public class ImportJavaOneFileCommand extends AbstractImportCommand {
public static final String ID = "importJavaOneFile";
private ICalendarSettings settings;
private InputStream istream;
public ImportJavaOneFileCommand() {
super(ID);
}
public void setSettings(ICalendarSettings settings) {
this.settings = settings;
}
/**
* Starts a new frame and close the active window.
*
* @see Application#close()
*/
@Override
protected void doOnce() {
checkIfDataIsAlreadyAvailable();
closeAllViews();
//days==5; 14 timeslots per day
JOptionPane.showMessageDialog(null, tr.get(ID + ".question", 5, 9));
StatusBar bar = getApplicationWindow().getStatusBar();
try {
int resultDownload = JOptionPane.showConfirmDialog(null, tr.get(ID + ".downloadQuestion"),
tr.get(ID + ".downloadQuestion"), JOptionPane.YES_NO_OPTION);
if (resultDownload == JOptionPane.YES_OPTION) {
} else {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle(tr.get(ID + ".fileDialog"));
int res = fc.showOpenDialog(getApplicationWindow().getControl());
if (res != JFileChooser.APPROVE_OPTION) {
bar.setMessage(tr.get(ID + ".noFileSelected"));
return;
}
file = fc.getSelectedFile();
if (!file.getName().endsWith(".j1")) {
int resultOP = JOptionPane.showConfirmDialog(null, tr.get(ID + ".confirmation.message"),
tr.get(ID + ".confirmation.title"), JOptionPane.OK_CANCEL_OPTION);
if (resultOP != JOptionPane.OK_OPTION) {
return;
}
}
istream = new FileInputStream(file);
}
} catch (Exception ex) {
String errorMsg = "Cannot read file! " + ex.getLocalizedMessage();
logger.fatal(errorMsg, ex);
bar.setErrorMessage(errorMsg);
return;
}
MySwingWorker sw = new MySwingWorker(ID) {
@Override
protected void myconstruct() throws Exception {
StatusBar bar = getApplicationWindow().getStatusBar();
ProgressMonitor pm = bar.getProgressMonitor();
int resultDownload = JOptionPane.showConfirmDialog(null, tr.get(ID + ".downloadQuestion"),
tr.get(ID + ".downloadQuestion"), JOptionPane.YES_NO_OPTION);
if (resultDownload == JOptionPane.YES_OPTION) {
String str = "http://peat_hal.users.sourceforge.net/sessions_catalog.j1";
bar.setMessage(tr.get("downloadFrom", str));
istream = new UrlResource(str).getInputStream();
pm.worked(5);
bar.setMessage("");
}
pm.taskStarted(tr.get(ID + ".startTask"), -1);
pm.worked(10);
ImportJavaOne importer = new ImportJavaOne();
ImportJavaOne.overwriteSettings(settings);
FastMap<String, Person> persons = new FastMap<String, Person>();
// TODO update progres:
Collection<JavaOneInterval> coll = importer.parse(istream);
double tmp = 80.0 / (coll.size() + 1);
int counter = 0;
Dao<Event> eDao = dataPool.getDao(Event.class);
for (JavaOneInterval ev : coll) {
pm.worked(10 + (int) (counter * tmp));
counter++;
Event event = eDao.create();
if (ev.getStart() >= 0 && ev.getDuration() > 0) {
IntervalInt si = settings.toInterval(ev.getStartDateTime(), ev.getEndDateTime());
if (si != null)
event.setInterval(si.getStart(), si.getDuration());
}
event.setName(ev.getId());
event.setDescription(ev.getDescription());
if (ev.getSpeakers() != null) {
for (String str : ev.getSpeakers().split(";")) {
str = str.trim();
Person p = persons.get(str);
if (p == null) {
p = new Person();
p.setName(str);
persons.put(str, p);
}
p.addEvent(event, Role.TEACHER, true);
}
}
eDao.attach(event);
}
pm.worked(90);
dataPool.getDao(Person.class).attachAll(persons.values());
pm.worked(100);
}
@Override
protected void done() {
openAllViews();
super.done();
}
};
sw.execute();
}
}