package m.szajowski.schedule;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.vaadin.Application;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.StreamResource;
import com.vaadin.terminal.StreamResource.StreamSource;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
public class SchedulePdf implements StreamSource {
private final ByteArrayOutputStream os = new ByteArrayOutputStream();
private final Collection<Day> days;
private final String person;
private final String date;
public SchedulePdf(final String date, final String person, final Collection<Day> days) {
this.date = date;
this.person = person;
this.days = days;
}
public void schowScheduleWindow(final Application application) {
prepareContent();
Window window = new Window("Zestawienie czasu pracy pracownika za " + date + " - " + person);
((VerticalLayout) window.getContent()).setSizeFull();
window.setModal(true);
window.setResizable(true);
window.setWidth(800, Sizeable.UNITS_PIXELS);
window.setHeight(600, Sizeable.UNITS_PIXELS);
window.center();
Embedded e = new Embedded();
e.setCodetype("UTF-8");
e.setSizeFull();
e.setType(Embedded.TYPE_BROWSER);
// Here we create a new StreamResource which downloads our StreamSource,
// which is our pdf.
StreamResource resource = new StreamResource(this, "schedule.pdf?"
+ System.currentTimeMillis(), application);
// Set the right mime type
resource.setMIMEType("application/pdf");
e.setSource(resource);
window.addComponent(e);
application.getMainWindow().addWindow(window);
}
private void prepareContent() {
Document document = null;
try {
BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250,
BaseFont.EMBEDDED);
Font f = new Font(bf, 12, Font.NORMAL);
document = new Document(PageSize.A4, 10, 10, 50, 50);
PdfWriter instance = PdfWriter.getInstance(document, os);
document.open();
Paragraph paragraph = new Paragraph("Zestawienie czasu pracy pracownika za " + date
+ " - " + person, f);
paragraph.setAlignment(Element.ALIGN_CENTER);
document.add(paragraph);
addEmptyLine(document, 2);
PdfPTable table = new PdfPTable(5);
Phrase phrase = new Phrase("Dzień miesiąca", f);
PdfPCell c1 = new PdfPCell(phrase);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Czas pracy\u0144", f));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Program"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Czas nieobecności", f));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Przyczyna nieobecność", f));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);
for (Day day : days) {
PdfPCell cell = new PdfPCell(new Phrase(getValue(day.getDay()), f));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Phrase(getValue(day.getWorkTime()), f));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Phrase(getValue(day.getProgram()), f));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Phrase(getValue(day.getAbsenceTime()), f));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Phrase(getValue(day.getAbsenceReason()), f));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
}
document.add(table);
addEmptyLine(document, 3);
Paragraph paragraph2 = new Paragraph("Podpis pracownika");
paragraph2.setAlignment(Element.ALIGN_CENTER);
document.add(paragraph2);
Paragraph paragraph3 = new Paragraph("...........................................");
paragraph3.setAlignment(Element.ALIGN_CENTER);
document.add(paragraph3);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (document != null) {
document.close();
}
}
}
public String getValue(final Object value) {
try {
return value == null ? "" : new String(value.toString().getBytes(), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
private static void addEmptyLine(final Document document, final int number) throws Exception {
for (int i = 0; i < number; i++) {
document.add(new Paragraph(" "));
}
}
public InputStream getStream() {
return new ByteArrayInputStream(os.toByteArray());
}
}