package net.sf.jpluck.apps.conduit;
import net.sf.jpluck.ClientConfiguration;
import net.sf.jpluck.apps.ExitCodes;
import net.sf.jpluck.conversion.Conversion;
import net.sf.jpluck.conversion.ConversionListener;
import net.sf.jpluck.conversion.Destination;
import net.sf.jpluck.jxl.Document;
import net.sf.jpluck.jxl.JXL;
import net.sf.jpluck.swing.WindowUtil;
import net.sf.jpluck.ui.ConversionFrame;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Options;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.UIManager;
public class Conduit {
private static ConversionFrame conversionFrame;
public static void main(String[] args) {
try {
Options options = new Options();
options.addOption("destination", true, "PDB destination.");
options.addOption("autoupdate", false, "Run AutoUpdate.");
CommandLine cl = new GnuParser().parse(options, args);
if (!cl.hasOption("destination")) {
System.err.println("ERROR: destination not specified.");
System.exit(ExitCodes.UNSPECIFIED);
}
List documentList = new ArrayList();
List fileList = new ArrayList();
args = cl.getArgs();
for (int i = 0; i < args.length; i++) {
fileList.add(new File(args[i]));
}
ClientConfiguration conf = ClientConfiguration.getDefault();
if (cl.hasOption("autoupdate")) {
if (conf.isAutoUpdatePeriodEnabled()) {
if (conf.getAutoUpdatePeriod().withinPeriod()) {
fileList.addAll(Arrays.asList(conf.getAutoUpdateJXLs()));
}
} else {
fileList.addAll(Arrays.asList(conf.getAutoUpdateJXLs()));
}
}
for (int i = 0, n = fileList.size(); i < n; i++) {
try {
File file = (File) fileList.get(i);
JXL jxl = new JXL(file);
Document[] documents = jxl.getDocumentsToConvert();
Arrays.sort(documents);
documentList.addAll(Arrays.asList(documents));
} catch (Exception e) {
}
}
Document[] documents = (Document[]) documentList.toArray(new Document[documentList.size()]);
if (documents.length == 0) {
System.exit(ExitCodes.OK);
}
Destination destination = Destination.createForDirectory(new File(cl.getOptionValue("destination")));
Conversion conversion = new Conversion(documents, destination);
try {
UIManager.setLookAndFeel(conf.getLookAndFeel());
conf.applyPlasticTheme();
} catch (Exception e) {
}
conversionFrame = new ConversionFrame(conversion, "JPluck");
WindowUtil.center(conversionFrame);
conversionFrame.show();
ConduitListener conduitListener = new ConduitListener(conversion);
conversion.addConversionListener(conduitListener);
ConduitConversation conversation = new ConduitConversation();
conversation.startListening(conduitListener);
Thread thread = new Thread(conversion);
thread.start();
} catch (Exception e) {
e.printStackTrace();
System.exit(ExitCodes.UNSPECIFIED);
}
}
private static class ConduitListener implements ConversionListener, ConversationListener {
private Conversion conversion;
ConduitListener(Conversion conversion) {
this.conversion = conversion;
}
public void conversionCompleted(Document jxlDocument, int current, int total, boolean success) {
}
public void conversionFinished() {
System.out.println("upload-jxl: ");
System.exit(ExitCodes.OK);
}
public void conversionStarted(Document jxlDocument, int current, int total) {
}
public void generationCompleted(Document jxlDocument) {
System.out.println("sync:" + conversion.getDestination().getFilename(jxlDocument.getName()));
if (jxlDocument.getID() != null) {
System.out.println("id:" + jxlDocument.getID());
}
}
public void generationStarted(Document jxlDocument) {
}
public void setLastModified(String databaseName, long date) {
}
public void cancel() {
if (conversionFrame != null) {
conversionFrame.cancel();
}
}
public void timeout() {
}
public void statusMessage(String message) {
}
}
}