package org.connla;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfPage;
import com.lowagie.text.pdf.PdfWriter;
//TODO left, center, right alignment
public class Connla {
private Connlon[][] itsData;
private List itsDataList;
private boolean itsHasList;
private Connlon[] itsHeaders;
private String itsTitle;
private static final String itsAuthor = "MJOPR";
private int[] itsWidths;
private int itsRows;
private int itsCols;
public Connla(String theTitle, int[] theWidths, Connlon[] theHeaders,
Connlon[][] theData) {
itsTitle = theTitle;
itsWidths = theWidths;
itsCols = itsWidths.length;
itsData = theData;
itsDataList = null;
itsHasList = false;
itsHeaders = theHeaders;
itsRows = itsData.length;
}
public Connla(String theTitle, int[] theWidths, Connlon[] theHeaders,
List theDataList) {
itsTitle = theTitle;
itsWidths = theWidths;
itsCols = itsWidths.length;
itsData = null;
itsDataList = theDataList;
itsHasList = true;
itsHeaders = theHeaders;
itsRows = itsDataList.size();
}
public Connla(String theTitle, Connlon[] theHeaders, List theData) {
this(theTitle, null, theHeaders, theData);
}
public Connla(String theTitle, int[] theWidths, List theData) {
this(theTitle, theWidths, null, theData);
}
public Connla(String theTitle, Connlon[] theHeaders, Connlon[][] theData) {
this(theTitle, null, theHeaders, theData);
}
public Connla(String theTitle, int[] theWidths, Connlon[][] theData) {
this(theTitle, theWidths, null, theData);
}
public void toHTML(String theFileName) throws IOException {
if (theFileName != null) {
File aFile = new File(theFileName);
aFile = new File(aFile.getParent());
if (!aFile.canWrite()) {
if (!aFile.mkdirs()) {
//TODO throw
}
}
BufferedWriter aWriter = new BufferedWriter(new FileWriter(theFileName));
aWriter.write(toHTML());
aWriter.close();
} else {
//TODO throw
}
}
public String toHTML() {
StringBuffer aSB = new StringBuffer("<html>\n");
aSB.append("\t<head>\n");
aSB.append("\t\t<title>");
aSB.append(itsTitle);
aSB.append("</Title>\n"); //TODO stylesheet
aSB.append("\t</head>\n");
aSB.append("\t<style>\n");//todo make more general
aSB.append("\tcaption {font-family: sans-serif;}\n");
aSB.append("\ttd {font-family: sans-serif;}\n");
aSB.append("\ttr {font-family: sans-serif;}\n");
aSB.append("\th1 {font-family: sans-serif;}\n");
aSB.append("\th2 {font-family: sans-serif;}\n");
aSB.append("\th3 {font-family: sans-serif;}\n");
aSB.append("\th4 {font-family: sans-serif;}\n");//TODO extend to all oronly ones used
aSB.append("\t</style>\n");
aSB.append("\t<body>\n");
aSB.append(toHTMLTable());
aSB.append("\t</body>\n");
aSB.append("</html>\n");
return aSB.toString();
}
public String toHTMLTable() {
StringBuffer aSB = new StringBuffer("<h1>");
aSB.append(itsTitle);
aSB.append("</h1>\n");
if (itsData != null || itsDataList != null) {
aSB.append("<table border='0' width='100%'>\n");
}
/*TODO aSB.append("<caption>");
aSB.append(itsTitle);
aSB.append("</caption>\n");*/
if (itsHeaders != null) {
aSB.append("<tr>");
for (int i = 0; i < itsCols; i++) {
aSB.append(itsHeaders[i].toHTML());
}
aSB.append("</tr>\n");
}
Iterator aIterator = null;
if (itsHasList) {
aIterator = itsDataList.iterator();
}
for (int i = 0; i < itsRows; i++) {
aSB.append("<tr>");
Connlon[] aConnlons = null;
if (itsHasList) {
aConnlons = (Connlon[]) aIterator.next();
} else {
aConnlons = itsData[i];
}
for (int j = 0; j < itsCols; j++) {
aSB.append(aConnlons[j].toHTML());
}
aSB.append("</tr>\n");
}
aSB.append("</table>\n");
return aSB.toString();
}
//TODO move to general class
private void appendIdenticalStrings(StringBuffer theSB, String theString,
int theNumber) {
for (int i = 0; i < itsTitle.length(); i++) {
theSB.append(theString);
}
}
public String toTXT() {
StringBuffer aSB = new StringBuffer(itsTitle);
aSB.append("\n");
appendIdenticalStrings(aSB, "=", itsTitle.length());
aSB.append("\n\n");
// calculate lengths
if (itsData != null || itsDataList != null) {
int[] aLengths = new int[itsCols];
if (itsHeaders != null) {
for (int i = 0; i < itsCols; i++) {
aLengths[i] = itsHeaders[i].length();
}
} else {
for (int i = 0; i < itsCols; i++) {
aLengths[i] = 0;
}
}
Iterator aIterator = null;
if (itsHasList) {
aIterator = itsDataList.iterator();
}
for (int i = 0; i < itsRows; i++) {
Connlon[] aConnlons = null;
if (itsHasList) {
aConnlons = (Connlon[]) aIterator.next();
} else {
aConnlons = itsData[i];
}
for (int j = 0; j < itsCols; j++) {
int aLength = aConnlons[j].length();
if (aLength > aLengths[j]) {
aLengths[j] = aLength;
}
}
}
// export data
if (itsHeaders != null) {
for (int i = 0; i < itsCols; i++) {
aSB.append(itsHeaders[i].toTXT(aLengths[i]));
if (i != itsCols - 1) {
aSB.append(" ");
}
}
aSB.append("\n");
for (int i = 0; i < itsCols; i++) {
for (int j = 0; j < aLengths[i]; j++) {
aSB.append("-");
}
if (i != itsCols - 1) {
aSB.append(" ");
}
}
aSB.append("\n");
}
aIterator = null;
if (itsHasList) {
aIterator = itsDataList.iterator();
}
for (int i = 0; i < itsRows; i++) {
Connlon[] aConnlons = null;
if (itsHasList) {
aConnlons = (Connlon[]) aIterator.next();
} else {
aConnlons = itsData[i];
}
for (int j = 0; j < itsCols; j++) {
aSB.append(aConnlons[j].toTXT(aLengths[j]));
if (j != itsCols - 1) {
aSB.append(" ");
}
}
if (i != itsRows - 1) {
aSB.append("\n");
}
}
}
return aSB.toString();
}
public void toTXT(String theFileName) throws IOException {
if (theFileName != null) {
File aFile = new File(theFileName);
aFile = new File(aFile.getParent());
if (!aFile.canWrite()) {
if (!aFile.mkdirs()) {
//TODO throw
}
}
BufferedWriter aWriter = new BufferedWriter(new FileWriter(theFileName));
aWriter.write(toTXT());
aWriter.close();
} else {
//TODO throw
}
}
public String toCSV() {
StringBuffer aSB = new StringBuffer(itsTitle);
aSB.append("\n\n\n");
if (itsData != null || itsDataList != null) {
if (itsHeaders != null) {
for (int i = 0; i < itsCols; i++) {
aSB.append(itsHeaders[i].toCSV());
if (i != itsCols - 1) {
aSB.append(",");
}
}
aSB.append("\n\n");
}
Iterator aIterator = null;
if (itsHasList) {
aIterator = itsDataList.iterator();
}
for (int i = 0; i < itsRows; i++) {
Connlon[] aConnlons = null;
if (itsHasList) {
aConnlons = (Connlon[]) aIterator.next();
} else {
aConnlons = itsData[i];
}
for (int j = 0; j < itsCols; j++) {
aSB.append(aConnlons[j].toCSV());
if (j != itsCols - 1) {
aSB.append(",");
}
}
if (i != itsRows - 1) {
aSB.append("\n");
}
}
}
return aSB.toString();
}
public void toCSV(String theFileName) throws IOException {
if (theFileName != null) {
File aFile = new File(theFileName);
aFile = new File(aFile.getParent());
if (!aFile.canWrite()) {
if (!aFile.mkdirs()) {
//TODO throw
}
}
BufferedWriter aWriter = new BufferedWriter(new FileWriter(theFileName));
aWriter.write(toCSV());
aWriter.close();
} else {
//TODO throw
}
}
private int[] redistribute(int[] theWidths, int theSkipLeading) {
if (theSkipLeading < 1) {
return theWidths;
}
int[] aWidths = new int[theWidths.length - theSkipLeading];
float aExtra = 0.0f;
float aTotal = 0.0f;
for (int i = 0; i < theWidths.length; i++) {
if (i < theSkipLeading) {
aExtra += theWidths[i];
}
aTotal += theWidths[i];
}
float aNewTotal = aTotal - aExtra;
float aSubTotal = 0.0f;
for (int i = 0; i < aWidths.length - 1; i++) {
float aValue = (float) theWidths[i + theSkipLeading]
* (1.0f + aExtra / aNewTotal);
aWidths[i] = (int) aValue;
aSubTotal += aValue;
}
// correct for rounding errors
aWidths[aWidths.length - 1] = (int) (aTotal - aSubTotal);
return aWidths;
}
public void toPDF(Document theDocument, int theSkipLeadingCols)
throws Exception {
//TODO aDocument.newPage();
int[] aWidths = redistribute(itsWidths, theSkipLeadingCols);
theDocument.setMargins(50, 50, 50, 50);
if (PdfPage.PORTRAIT.equals(PdfPage.LANDSCAPE)) {
theDocument.setPageSize(PageSize.A4);
//TODO check if this is // correct setting
} else if (PdfPage.LANDSCAPE.equals(PdfPage.LANDSCAPE)) {
theDocument.setPageSize(PageSize.A4.rotate());
} else {
theDocument.setPageSize(PageSize.A4);
//TODO print error
}
// PdfContentByte aPdfContentByte = // TODO
// aPdfContentByte.setFontAndSize(new BaseFont(), 8);
theDocument.addAuthor(itsAuthor);
//TODOaddCreator(theAuthor);
theDocument.addProducer();
theDocument.addTitle(itsTitle);
//TODOaddKeywords("asdf, wwer");
theDocument.addCreationDate();
StringBuffer aSubject = new StringBuffer(itsTitle);
aSubject.append(" - ");
//TODO aSubject.append(itsVersion);
// aSubject.append(" - ");
aSubject.append(getDateTime());
theDocument.addSubject(aSubject.toString());
//TODO in footer?
HeaderFooter aHeader = new HeaderFooter(
new Phrase(aSubject.toString()), false);
// aHeader.setBorder(Rectangle.NO_BORDER);
// aDocument.setHeader(aHeader);
HeaderFooter aFooter = new HeaderFooter(new Phrase(), true);
aFooter.setAlignment(HeaderFooter.ALIGN_RIGHT);
aFooter.setBorder(Rectangle.NO_BORDER);
theDocument.setFooter(aFooter);
theDocument.open();
theDocument.add(new Phrase(itsTitle, FontFactory.getFont(
FontFactory.HELVETICA, 24, Font.BOLD)));
Table aTable = new Table(aWidths.length);
aTable.setPadding(4);
aTable.setSpacing(0);
aTable.setWidths(aWidths);
aTable.setWidth(100);
if (itsHeaders != null) {
aTable.setDefaultRowspan(aWidths.length);
for (int i = 0 + theSkipLeadingCols; i < itsCols; i++) {
aTable.addCell(itsHeaders[i].toPDF());
}
aTable.endHeaders();
}
if (itsData != null || itsDataList != null) {
aTable.setDefaultCellBorderWidth(0);
aTable.setDefaultRowspan(1);
aTable.setDefaultHorizontalAlignment(Element.ALIGN_LEFT);
Iterator aIterator = null;
if (itsHasList) {
aIterator = itsDataList.iterator();
}
for (int i = 0; i < itsRows; i++) {
Connlon[] aConnlons = null;
if (itsHasList) {
aConnlons = (Connlon[]) aIterator.next();
} else {
aConnlons = itsData[i];
}
for (int j = 0 + theSkipLeadingCols; j < itsCols; j++) {
aTable.addCell(aConnlons[j].toPDF());
}
}
}
theDocument.add(aTable);
theDocument.close();
}
public void toPDF(String theFileName) throws Exception {
toPDF(theFileName, 0);
}
public void toPDF(String theFileName, int theSkipLeadingCols)
throws Exception {
if (theFileName != null) {
File aFile = new File(theFileName);
aFile = new File(aFile.getParent());
if (!aFile.canWrite()) {
if (!aFile.mkdirs()) {
//TODO throw
}
}
Document aDocument = new Document();
PdfWriter.getInstance(aDocument, new FileOutputStream(theFileName));
toPDF(aDocument, theSkipLeadingCols);
} else {
//TODO throw
}
}
public ByteArrayOutputStream toPDF() throws Exception {
return toPDF(0);
}
public ByteArrayOutputStream toPDF(int theSkipLeadingCols) throws Exception {
Document aDocument = new Document();
ByteArrayOutputStream aBAOS = new ByteArrayOutputStream();
PdfWriter.getInstance(aDocument, aBAOS);
toPDF(aDocument, theSkipLeadingCols);
return aBAOS;
}
public void toXLS(String theFileName) throws Exception {
//TODO color, backgroundcolor
HSSFWorkbook aWorkbook = new HSSFWorkbook();
HSSFSheet aSheet = aWorkbook.createSheet(itsTitle);
HSSFRow aRow = null;
HSSFCell aCell = null;
int aOffSet = 0;
if (itsHeaders != null) { // bold
aRow = aSheet.createRow((short) 0);
for (int i = 0; i < itsCols; i++) {
aCell = aRow.createCell((short) i);
aCell = itsHeaders[i].toXLS(aCell, aWorkbook);
}
aOffSet++;
}
if (itsData != null || itsDataList != null) {
Iterator aIterator = null;
if (itsHasList) {
aIterator = itsDataList.iterator();
}
for (int i = 0; i < itsRows; i++) {
Connlon[] aConnlons = null;
if (itsHasList) {
aConnlons = (Connlon[]) aIterator.next();
} else {
aConnlons = itsData[i];
}
aRow = aSheet.createRow((short) i + aOffSet);
for (int j = 0; j < itsCols; j++) {
aCell = aRow.createCell((short) j);
aCell = aConnlons[j].toXLS(aCell, aWorkbook);
}
}
}
if (theFileName != null) {
File aFile = new File(theFileName);
aFile = new File(aFile.getParent());
if (!aFile.canWrite()) {
if (!aFile.mkdirs()) {
//TODO throw
}
}
FileOutputStream aFOS = new FileOutputStream(theFileName);
aWorkbook.write(aFOS);
aFOS.close() ;
} else {
//TODO throw
}
}
//todo move to more general place
public static String getDateTime() {
DateFormat aDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return aDateFormat.format(Calendar.getInstance().getTime());
}
}