
io.vertigo.dynamo.plugins.export.pdfrtf.AbstractExporterIText Maven / Gradle / Ivy
/**
* vertigo - simple java starter
*
* Copyright (C) 2013, KleeGroup, [email protected] (http://www.kleegroup.com)
* KleeGroup, Centre d'affaire la Boursidiere - BP 159 - 92357 Le Plessis Robinson Cedex - France
*
* 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.
*/
package io.vertigo.dynamo.plugins.export.pdfrtf;
import io.vertigo.dynamo.domain.metamodel.DtField;
import io.vertigo.dynamo.domain.model.DtObject;
import io.vertigo.dynamo.export.model.Export;
import io.vertigo.dynamo.export.model.ExportField;
import io.vertigo.dynamo.export.model.ExportSheet;
import io.vertigo.dynamo.impl.export.util.ExportUtil;
import io.vertigo.dynamo.store.StoreManager;
import io.vertigo.lang.Assertion;
import java.awt.Color;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
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;
/**
* @author pchretien, npiedeloup
*/
public abstract class AbstractExporterIText {
private static final String CREATOR = "System";
private final Map> referenceCache = new HashMap<>();
private final Map> denormCache = new HashMap<>();
private final StoreManager storeManager;
/**
* Constructor.
* @param storeManager Store manager
*/
protected AbstractExporterIText(final StoreManager storeManager) {
Assertion.checkNotNull(storeManager);
//-----
this.storeManager = storeManager;
}
/**
* We create a writer that listens to the document and directs a PDF-stream to out
*
* @param document Document
* @param out OutputStream
* @throws DocumentException Itext exception
*/
protected abstract void createWriter(final Document document, final OutputStream out) throws DocumentException;
/**
* Méthode principale qui gère l'export d'un tableau vers un fichier ODS.
*
* @param export paramètres du document à exporter
* @param out flux de sortie
* @throws DocumentException Exception
*/
public final void exportData(final Export export, final OutputStream out) throws DocumentException {
// step 1: creation of a document-object
final boolean landscape = export.getOrientation() == Export.Orientation.Landscape;
final Rectangle pageSize = landscape ? PageSize.A4.rotate() : PageSize.A4;
final Document document = new Document(pageSize, 20, 20, 50, 50); // left, right, top, bottom
// step 2: we create a writer that listens to the document and directs a PDF-stream to out
createWriter(document, out);
// we add some meta information to the document, and we open it
final String title = export.getTitle();
if (title != null) {
final HeaderFooter header = new HeaderFooter(new Phrase(title), false);
header.setAlignment(Element.ALIGN_LEFT);
header.setBorder(Rectangle.NO_BORDER);
document.setHeader(header);
document.addTitle(title);
}
final String author = export.getAuthor();
document.addAuthor(author);
document.addCreator(CREATOR);
document.open();
try {
// pour ajouter l'ouverture automatique de la boîte de dialogue imprimer (print(false) pour imprimer directement)
// ((PdfWriter) writer).addJavaScript("this.print(true);", false);
for (final ExportSheet exportSheet : export.getSheets()) {
final Table datatable;
if (exportSheet.hasDtObject()) {
// table
datatable = new Table(2);
datatable.setCellsFitPage(true);
datatable.setPadding(4);
datatable.setSpacing(0);
// data rows
renderObject(exportSheet, datatable);
} else {
// table
datatable = new Table(exportSheet.getExportFields().size());
datatable.setCellsFitPage(true);
datatable.setPadding(4);
datatable.setSpacing(0);
// headers
renderHeaders(exportSheet, datatable);
// data rows
renderList(exportSheet, datatable);
}
document.add(datatable);
}
} finally {
// we close the document
document.close();
}
}
private void renderObject(final ExportSheet exportSheet, final Table datatable) throws BadElementException {
final Font labelFont = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD);
final Font valueFont = FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL);
for (final ExportField exportColumn : exportSheet.getExportFields()) {
datatable.getDefaultCell().setBorderWidth(2);
datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
datatable.addCell(new Phrase(exportColumn.getLabel().getDisplay(), labelFont));
datatable.getDefaultCell().setBorderWidth(1);
datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
final DtField dtField = exportColumn.getDtField();
final Object value = dtField.getDataAccessor().getValue(exportSheet.getDtObject());
final int horizontalAlignement;
if (value instanceof Number || value instanceof Date) {
horizontalAlignement = Element.ALIGN_RIGHT;
} else if (value instanceof Boolean) {
horizontalAlignement = Element.ALIGN_CENTER;
} else {
horizontalAlignement = Element.ALIGN_LEFT;
}
datatable.getDefaultCell().setHorizontalAlignment(horizontalAlignement);
String text = ExportUtil.getText(storeManager, referenceCache, denormCache, exportSheet.getDtObject(), exportColumn);
if (text == null) {
text = "";
}
datatable.addCell(new Phrase(8, text, valueFont));
}
}
/**
* Effectue le rendu des headers.
*
* @param parameters Paramètres
* @param datatable Table
*/
private static void renderHeaders(final ExportSheet parameters, final Table datatable) throws BadElementException {
// table header
final Font font = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD);
datatable.getDefaultCell().setBorderWidth(2);
datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
for (final ExportField exportColumn : parameters.getExportFields()) {
datatable.addCell(new Phrase(exportColumn.getLabel().getDisplay(), font));
}
// end of the table header
datatable.endHeaders();
}
/**
* Effectue le rendu de la liste.
*
* @param parameters Paramètres
* @param datatable Table
*/
private void renderList(final ExportSheet parameters, final Table datatable) throws BadElementException {
// data rows
final Font font = FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL);
final Font whiteFont = FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL);
whiteFont.setColor(Color.WHITE);
datatable.getDefaultCell().setBorderWidth(1);
datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
// Parcours des DTO de la DTC
for (final DtObject dto : parameters.getDtList()) {
for (final ExportField exportColumn : parameters.getExportFields()) {
final DtField dtField = exportColumn.getDtField();
final Object value = dtField.getDataAccessor().getValue(dto);
final int horizontalAlignement;
if (value instanceof Number || value instanceof Date) {
horizontalAlignement = Element.ALIGN_RIGHT;
} else if (value instanceof Boolean) {
horizontalAlignement = Element.ALIGN_CENTER;
} else {
horizontalAlignement = Element.ALIGN_LEFT;
}
datatable.getDefaultCell().setHorizontalAlignment(horizontalAlignement);
String text = ExportUtil.getText(storeManager, referenceCache, denormCache, dto, exportColumn);
if (text == null) {
text = "";
}
datatable.addCell(new Phrase(8, text, font));
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy