package org.jbpm.ui.sync;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import javax.security.auth.login.Configuration;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Display;
import org.jbpm.ui.DesignerLogger;
import org.jbpm.ui.DesignerPlugin;
import org.jbpm.ui.resource.Messages;
public abstract class DataImporter {
static {
Configuration.setConfiguration(new KerberosLoginConfiguration());
}
private final SyncResources resources;
protected DataImporter(SyncResources resources) {
this.resources = resources;
loadProperties();
}
public SyncResources getResources() {
return resources;
}
private void loadProperties() {
try {
resources.load();
} catch (Exception e) {
throw new RuntimeException("Unable to load connection settings", e);
}
}
public void saveProperties() {
try {
resources.save();
} catch (IOException e) {
DesignerLogger.logError("Unable save connection settings to storage", e);
}
}
public boolean isConfigured() {
return resources != null && resources.isValid();
}
public abstract void connect() throws Exception;
protected File getCacheFile() {
String fileName = getClass().getSimpleName() + ".xml";
return new File(DesignerPlugin.getPreferencesFolder(), fileName);
}
protected abstract void clearInMemoryCache();
protected abstract void loadRemoteData() throws Exception;
protected abstract void saveCachedData() throws Exception;
public abstract Object loadCachedData() throws Exception;
public final void synchronize() {
final ProgressMonitorDialog monitorDialog = new ProgressMonitorDialog(Display.getCurrent().getActiveShell());
monitorDialog.setCancelable(true);
final IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
monitor.beginTask(Messages.getString("task.SynchronizeData"), 3);
monitor.subTask(Messages.getString("task.Connect"));
connect();
monitor.worked(1);
monitor.subTask(Messages.getString("task.LoadData"));
clearInMemoryCache();
loadRemoteData();
monitor.worked(1);
monitor.subTask(Messages.getString("task.SaveData"));
saveCachedData();
monitor.worked(1);
} catch (Exception e) {
throw new InvocationTargetException(e);
} finally {
monitor.done();
}
}
};
try {
monitorDialog.run(true, false, runnable);
} catch (InvocationTargetException ex) {
throw new RuntimeException(ex.getTargetException());
} catch (InterruptedException ex) {
//
}
}
public abstract SyncSettingsWizardPage createConnectionSettingsWizardPage();
}