Java tutorial
/** * Este arquivo parte do Biblivre3. * * Biblivre3 um software livre; voc pode redistribu-lo e/ou * modific-lo dentro dos termos da Licena Pblica Geral GNU como * publicada pela Fundao do Software Livre (FSF); na verso 3 da * Licena, ou (caso queira) qualquer verso posterior. * * Este programa distribudo na esperana de que possa ser til, * mas SEM NENHUMA GARANTIA; nem mesmo a garantia implcita de * MERCANTIBILIDADE OU ADEQUAO PARA UM FIM PARTICULAR. Veja a * Licena Pblica Geral GNU para maiores detalhes. * * Voc deve ter recebido uma cpia da Licena Pblica Geral GNU junto * com este programa, Se no, veja em <http://www.gnu.org/licenses/>. * * @author Alberto Wagner <alberto@biblivre.org.br> * @author Danniel Willian <danniel@biblivre.org.br> * */ package biblivre3.administration.reports; import biblivre3.administration.ReportsDAO; import biblivre3.administration.ReportsDTO; import biblivre3.administration.reports.dto.AssetHoldingDto; import biblivre3.administration.reports.dto.BaseReportDto; import biblivre3.utils.NaturalOrderComparator; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.Image; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.pdf.Barcode39; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import java.util.Collections; import java.util.Comparator; import java.util.List; import org.apache.commons.lang.StringUtils; /** * @author Danniel Nascimento * */ public class AssetHoldingFullReport extends BaseBiblivreReport implements Comparator<String[]> { private Boolean topographic; public AssetHoldingFullReport(Boolean topographic) { this.topographic = topographic; } @Override protected BaseReportDto getReportData(ReportsDTO dto) { return new ReportsDAO().getAssetHoldingFullReportData(); } @Override protected void generateReportBody(Document document, BaseReportDto reportData) throws Exception { AssetHoldingDto dto = (AssetHoldingDto) reportData; String title = ""; if (this.topographic) { title = this.getText("REPORTS_TOPOGRAPHIC_TITLE"); } else { title = this.getText("REPORTS_ASSET_HOLDING_TITLE"); } Paragraph p1 = new Paragraph(title); p1.setAlignment(Paragraph.ALIGN_CENTER); document.add(p1); document.add(new Phrase("\n")); PdfPTable table = new PdfPTable(20); table.setWidthPercentage(100f); createHeader(table); PdfPCell cell; List<String[]> dataList = dto.getData(); Collections.sort(dataList, this); for (String[] data : dataList) { PdfContentByte cb = getWriter().getDirectContent(); String holdingSerial = StringUtils.leftPad(data[0], 10, "0"); Barcode39 code39 = new Barcode39(); code39.setExtended(true); code39.setCode(holdingSerial); code39.setStartStopText(false); Image image39 = code39.createImageWithBarcode(cb, null, null); image39.scalePercent(100f); cell = new PdfPCell(new Paragraph(new Phrase(new Chunk(image39, 0, 0)))); cell.setColspan(6); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); table.addCell(cell); cell = new PdfPCell(new Paragraph(this.getSmallFontChunk(data[1]))); cell.setColspan(3); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); table.addCell(cell); Paragraph para = new Paragraph(); para.add(new Phrase(this.getSmallFontChunk(data[2] + "\n"))); para.add(new Phrase(this.getSmallFontChunk(data[3] + "\n"))); if (StringUtils.isNotBlank(data[4])) { para.add(new Phrase(this.getBoldChunk(this.getText("REPORTS_LOCATION") + ": "))); para.add(new Phrase(this.getSmallFontChunk(data[4] + " "))); } if (StringUtils.isNotBlank(data[5])) { para.add(new Phrase(this.getBoldChunk(this.getText("REPORTS_EDITION") + ": "))); para.add(new Phrase(this.getSmallFontChunk(data[5] + " "))); } if (StringUtils.isNotBlank(data[6])) { para.add(new Phrase(this.getBoldChunk(this.getText("REPORTS_DATE") + ": "))); para.add(new Phrase(this.getSmallFontChunk(data[6]))); } cell = new PdfPCell(para); cell.setColspan(11); cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); cell.setVerticalAlignment(PdfPCell.ALIGN_TOP); cell.setPaddingTop(5f); cell.setPaddingLeft(7f); cell.setPaddingBottom(4f); table.addCell(cell); } document.add(table); } private void createHeader(PdfPTable table) { PdfPCell cell; cell = new PdfPCell(new Paragraph(this.getBoldChunk(this.getText("LABEL_SERIAL")))); cell.setBackgroundColor(headerBgColor); cell.setColspan(6); cell.setBorderWidth(headerBorderWidth); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); table.addCell(cell); cell = new PdfPCell(new Paragraph(this.getBoldChunk(this.getText("REPORTS_ASSET_HOLDING")))); cell.setBackgroundColor(headerBgColor); cell.setColspan(3); cell.setBorderWidth(headerBorderWidth); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); table.addCell(cell); cell = new PdfPCell(new Paragraph(this.getBoldChunk(this.getText("REPORTS_HOLDING")))); cell.setBackgroundColor(headerBgColor); cell.setColspan(11); cell.setBorderWidth(headerBorderWidth); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); table.addCell(cell); } @Override public int compare(String[] o1, String[] o2) { if (o1 == null && o2 == null) return 0; if (this.topographic) { return NaturalOrderComparator.NUMERICAL_ORDER.compare(o1[4], o2[4]); } else { return NaturalOrderComparator.NUMERICAL_ORDER.compare(o1[1], o2[1]); } } }