import {Report} from "../items/Report"; import {JSONWriter} from "./JSONWriter"; import {SchemaConstants} from "./SchemaConstants"; import { SplitTypeEnum, PositionTypeEnum, StretchTypeEnum, ModeEnum, RotationEnum, VerticalTextAlignEnum, HorizontalTextAlignEnum, ScaleImageEnum, HorizontalImageAlignEnum, VerticalImageAlignEnum, LineStyleEnum, LineDirection, ReportDesignMode } from "../configs/enums"; import {JRPropertiesHolder} from "../jasper/JRPropertiesHolder"; import {JRBand} from "../jasper/JRBand"; import {JRElementGroup} from "../jasper/JRElementGroup"; import {JSONSchemaWriterVisitor} from "./JSONSchemaWriterVisitor"; import {JRElement} from "../jasper/JRElement"; import {JRTextField} from "../jasper/JRTextField"; import {JRTextElement} from "../jasper/JRTextElement"; import {JRFont} from "../jasper/JRFont"; import {JRImage} from "../jasper/JRImage"; import {JRExpression} from "../items/JRExpression"; import {JRSection} from "../jasper/JRSection"; import {JRBoxContainer} from "../jasper/JRBoxContainer"; import {Border, BorderItem} from "../items/properties/classes/Border"; import {JRDataTable} from "../jasper/JRDataTable"; import {PaperKind} from "../drawing/PaperKind"; import {LineShape} from "../items/LineShape"; import RectangleItem from "../items/RectangleItem"; import EllipseItem from "../items/EllipseItem"; import {TableItem} from "../items/data-table/DataTableItem"; import {ColumnHeaderBand, ColumnFooterBand} from "../items/band/ColumnBand"; import {DetailBand} from "../items/band/DetailBand"; import {Unit} from "../drawing/Unit"; export class JSONSchemaWriter { private _visitor: JSONSchemaWriterVisitor; private _report:Report; private _writer: JSONWriter; private _printable:boolean; constructor() { this._visitor = new JSONSchemaWriterVisitor(this); } /** * * @param report * @param printable: true 输出为打印格式(一些设计器相关信息会被转换或者移除,仅保留打印信息), false 输出为设计格式 * @returns {any} */ write(report:Report, printable = false): any { this.writeReport(report, printable); return this.writer.toJSON(); } get writer(): JSONWriter { return this._writer; } private writeReport(report: Report, printable = false) { this._report = report; this._printable = printable; this._writer = new JSONWriter(); const writer = this._writer; writer .addAttribute(SchemaConstants.ATTRIBUTE_name, report.getName()) .addAttribute(SchemaConstants.ATTRIBUTE_columnCount, report.getColumnCount(), 1) .addAttributeEnum(SchemaConstants.ATTRIBUTE_pageKind,PaperKind, report.paper.paperKind) .addAttributeUnit(SchemaConstants.ATTRIBUTE_pageWidth, report.paper.paperSize.width) .addAttributeUnit(SchemaConstants.ATTRIBUTE_pageHeight, report.paper.paperSize.height) .addAttribute(SchemaConstants.ATTRIBUTE_columnWidth, report.getColumnWidth()) .addAttribute(SchemaConstants.ATTRIBUTE_columnSpacing, report.getColumnSpacing()) .addAttribute(SchemaConstants.ATTRIBUTE_leftMargin, report.getLeftMargin()) .addAttribute(SchemaConstants.ATTRIBUTE_rightMargin, report.getRightMargin()) .addAttribute(SchemaConstants.ATTRIBUTE_topMargin, report.getTopMargin()) .addAttribute(SchemaConstants.ATTRIBUTE_bottomMargin, report.getBottomMargin()) .addAttributeUnit(SchemaConstants.ATTRIBUTE_bandWidth, report.bandWidth) .addAttribute(SchemaConstants.ATTRIBUTE_uuid, report.getUUID()); this.writeProperties(report); if (report.getTitle()) { writer.startElement(SchemaConstants.ELEMENT_title); this.writeBand(report.getTitle()); writer.closeElement(); } if (report.getPageHeader()) { writer.startElement(SchemaConstants.ELEMENT_pageHeader); this.writeBand(report.getPageHeader()); writer.closeElement(); } if (report.getColumnHeader()) { writer.startElement(SchemaConstants.ELEMENT_columnHeader); this.writeBand(report.getColumnHeader()); writer.closeElement(); } const detail = report.getDetailSection(); if (detail && detail.getBands().length > 0) { // 这里我们需要特殊的进行对 DataTable 的处理 // 为了用户爽我们也真是蛮拼的啊! // if (this._printable && report.designMode === ReportDesignMode.ChineseTable) { // this.writeDetailBandChineseTable(detail.getBands()[0]); // } // else { // writer.startElement(SchemaConstants.ELEMENT_detail); // this.writeSection(detail); // writer.closeElement(); // } writer.startElement(SchemaConstants.ELEMENT_detail); this.writeSection(detail); writer.closeElement(); } if (report.getColumnFooter()) { writer.startElement(SchemaConstants.ELEMENT_columnFooter); this.writeBand(report.getColumnFooter()); writer.closeElement(); } if (report.getPageFooter()) { writer.startElement(SchemaConstants.ELEMENT_pageFooter); this.writeBand(report.getPageFooter()); writer.closeElement(); } if (report.getLastPageFooter()) { writer.startElement(SchemaConstants.ELEMENT_lastPageFooter); this.writeBand(report.getLastPageFooter()); writer.closeElement(); } if (report.getSummary() != null) { writer.startElement(SchemaConstants.ELEMENT_summary); this.writeBand(report.getSummary()); writer.closeElement(); } if (report.getNoData() != null) { writer.startElement(SchemaConstants.ELEMENT_noData); this.writeBand(report.getNoData()); writer.closeElement(); } writer.end(); } writeDetailBandChineseTable(band:JRBand):void { if (! this._printable) { this.writeBand(band); return; } const dataTable:TableItem = (band.getElements().find(item => item.constructor.name === TableItem.name)) as TableItem; if (dataTable) { const tableItems = dataTable.getItems(); if (tableItems) { const columnHeaderBand = new ColumnHeaderBand(); const columnFooterBand = new ColumnFooterBand(); const detailBand = new DetailBand(); // 将表的 Header 区域设置为报表的 ColumnHeaderBand 中 columnHeaderBand.height = Unit.pixel(tableItems.header.height + dataTable.getY()); tableItems.header.items.forEach(item => columnHeaderBand.items.push(item)); // 将表的 Body 区域设置为报表的 DetailBand 中 columnFooterBand.height = Unit.pixel(tableItems.footer.height + band.getHeight() - (dataTable.getY() + dataTable.getHeight())); tableItems.footer.items.forEach(item => columnFooterBand.items.push(item)); // 将表的 Footer 区域设置为报表的 ColumnFooterBand 中 detailBand.height = Unit.pixel(tableItems.body.height); tableItems.body.items.forEach(item => detailBand.items.push(item)); this.writer.startElement(SchemaConstants.ELEMENT_columnHeader); this.writeBand(columnHeaderBand); this.writer.closeElement(); this.writer.startElement(SchemaConstants.ELEMENT_detail); this.writeBand(detailBand); this.writer.closeElement(); this.writer.startElement(SchemaConstants.ELEMENT_columnFooter); this.writeBand(columnFooterBand); this.writer.closeElement(); } } } writeElementGroup(elementGroup:JRElementGroup):void { this.writer.startElement(SchemaConstants.ELEMENT_elementGroup); this.writeChildElements(elementGroup); this.writer.closeElement(); } writeTextField(textField:JRTextField):void { this.writer.startElement(SchemaConstants.ELEMENT_textField) .addAttribute(SchemaConstants.ATTRIBUTE_isStretchWithOverflow, textField.isStretchWithOverflow(), false) .addAttribute(SchemaConstants.ATTRIBUTE_pattern, textField.getOwnPattern()) .addAttribute(SchemaConstants.ATTRIBUTE_isBlankWhenNull, textField.isOwnBlankWhenNull()) this.writeExpression(SchemaConstants.ATTRIBUTE_text, textField.getExpression()); this.writeReportElement(textField); this.writeBox(textField); this.writeTextElement(textField); this.writer.closeElement(); } writeImage(image:JRImage):void { this.writer.startElement(SchemaConstants.ELEMENT_image); this.writer.addAttributeEnum(SchemaConstants.ATTRIBUTE_scaleImage, ScaleImageEnum, image.getOwnScaleImageValue()); this.writer.addAttributeEnum(SchemaConstants.ATTRIBUTE_hAlign, HorizontalImageAlignEnum, image.getOwnHorizontalImageAlign()); this.writer.addAttributeEnum(SchemaConstants.ATTRIBUTE_vAlign, VerticalImageAlignEnum, image.getOwnVerticalImageAlign()); this.writeReportElement(image); this.writeExpression(SchemaConstants.ATTRIBUTE_value, image.getExpression()); this.writer.closeElement(); } writeLineShape(shape: LineShape) { this.writer.startElement(SchemaConstants.ELEMENT_line); this.writer.addAttributeEnum(SchemaConstants.ATTRIBUTE_direction, LineDirection, shape.direction); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_lineColor, shape.lineColor); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_lineWidth, shape.style.strokeWidth.value); this.writer.addAttributeEnum(SchemaConstants.ATTRIBUTE_lineStyle, LineStyleEnum, shape.lineStyle); this.writeReportElement(shape); this.writer.closeElement(); } writeRectShape(shape: RectangleItem) { this.writer.startElement(SchemaConstants.ELEMENT_rectangle); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_fill, shape.fill); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_lineColor, shape.lineColor); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_lineWidth, shape.style.strokeWidth.value); this.writer.addAttributeEnum(SchemaConstants.ATTRIBUTE_lineStyle, LineStyleEnum, shape.lineStyle); this.writeReportElement(shape); this.writer.closeElement(); } writeEllipseShape(shape: EllipseItem) { this.writer.startElement(SchemaConstants.ELEMENT_ellipse); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_fill, shape.fill); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_lineColor, shape.lineColor); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_lineWidth, shape.style.strokeWidth.value); this.writer.addAttributeEnum(SchemaConstants.ATTRIBUTE_lineStyle, LineStyleEnum, shape.lineStyle); this.writeReportElement(shape); this.writer.closeElement(); } writeStaticDataTable(dataTable: JRDataTable) { if (! this._printable) { this.writeTableItem(dataTable, SchemaConstants.ELEMENT_staticTable); return; } const tableItems = (dataTable as TableItem).getFlatItems(); if (tableItems) { tableItems.forEach(item => item.visit(this._visitor)); } } writeDataTable(dataTable: JRDataTable) { this.writeTableItem(dataTable, SchemaConstants.ELEMENT_dataTable); } private writeTableItem(dataTable: JRDataTable, elementName:string) { this.writer.startElement(elementName) .addAttribute(SchemaConstants.ATTRIBUTE_data, JSON.stringify({ sheet: dataTable.getSheet().toMinJS(), config: dataTable.getConfig() })); //console.log('schemaWritter config',dataTable.getConfig()); this.writeReportElement(dataTable); this.writer.closeElement(); } private writeSection(section:JRSection):void { if (section) { const bands = section.getBands(); if (bands && bands.length > 0) { for(let i = 0; i < bands.length; i++) { this.writeBand(bands[i]); } } } } private writeBand(band:JRBand):void { this.writer.startElement(SchemaConstants.ELEMENT_band); this.writer.addAttributePositive(SchemaConstants.ATTRIBUTE_height, band.getHeight()); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_splitType, SplitTypeEnum[band.getSplitTypeValue()]); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_uuid, band.getUUID()); this.writeProperties(band); this.writer.startElement(SchemaConstants.ELEMENT_elementGroup, true); this.writeChildElements(band); this.writer.closeElement(); this.writer.closeElement(); } private writeChildElements(container: JRElementGroup):void { const children = container.getChildren(); if (children && children.length > 0) { for (let child of children) { child.visit(this._visitor); } } } private writeExpression(name:string, expression:JRExpression):void { if (expression != null) { this.writer.addAttribute(name, expression.getText()); } } private writeBox(box:JRBoxContainer):void { if (box) { this.writer.startElement(SchemaConstants.ELEMENT_box) .addAttributeUnit(SchemaConstants.ATTRIBUTE_topPadding, box.getPadding().top) .addAttributeUnit(SchemaConstants.ATTRIBUTE_leftPadding, box.getPadding().left) .addAttributeUnit(SchemaConstants.ATTRIBUTE_bottomPadding, box.getPadding().bottom) .addAttributeUnit(SchemaConstants.ATTRIBUTE_rightPadding, box.getPadding().right); this.writeBorder(box.getBorder()); this.writer.closeElement(); } } private writeBorder(border:Border):void { if (border) { this.writeBorderItem(SchemaConstants.ELEMENT_topPen, border.top); this.writeBorderItem(SchemaConstants.ELEMENT_leftPen, border.left); this.writeBorderItem(SchemaConstants.ELEMENT_bottomPen, border.bottom); this.writeBorderItem(SchemaConstants.ELEMENT_rightPen, border.right); } } private writeBorderItem(element: string, item:BorderItem):void { if (item && item.borderVisible) { this.writer.startElement(element) .addAttribute(SchemaConstants.ATTRIBUTE_lineWidth, item.borderWidth.pixels) .addAttributeEnum(SchemaConstants.ATTRIBUTE_lineStyle, LineStyleEnum, item.borderStyle) .addAttribute(SchemaConstants.ATTRIBUTE_lineColor, item.borderColor) .closeElement(); } } private writeTextElement(textElement:JRTextElement):void { this.writer.startElement(SchemaConstants.ELEMENT_textElement); this.writer.addAttributeEnum(SchemaConstants.ATTRIBUTE_textAlignment, HorizontalTextAlignEnum, textElement.getOwnHorizontalTextAlign()); this.writer.addAttributeEnum(SchemaConstants.ATTRIBUTE_verticalAlignment, VerticalTextAlignEnum, textElement.getOwnVerticalTextAlign()); this.writer.addAttributeEnum(SchemaConstants.ATTRIBUTE_rotation, RotationEnum, textElement.getOwnRotationValue()); this.writeFont(textElement); this.writer.closeElement(); } private writeFont(font:JRFont):void { if (font) { this.writer.startElement(SchemaConstants.ELEMENT_font); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_fontName, font.getOwnFontName()); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_size, font.getOwnFontSize(), true); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_isBold, font.isOwnBold()); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_isItalic, font.isOwnItalic()); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_isUnderline, font.isOwnUnderline()); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_isStrikeThrough, font.isOwnStrikeThrough()); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_pdfFontName, font.getOwnPdfFontName()); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_pdfEncoding, font.getOwnPdfEncoding()); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_isPdfEmbedded, font.isOwnPdfEmbedded()); this.writer.closeElement(); } } private writeReportElement(element: JRElement) { this .writer.startElement(SchemaConstants.ELEMENT_reportElement) .addAttributeEnum(SchemaConstants.ATTRIBUTE_positionType, PositionTypeEnum, element.getPositionType(), PositionTypeEnum.FixRelativeToTop); this.writeStretchType(element.getStretchType()); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_isPrintRepeatedValues, element.isPrintRepeatedValues(), true); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_isRemoveLineWhenBlank, element.isRemoveLineWhenBlank(), false); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_isPrintInFirstWholeBand, element.isPrintInFirstWholeBand(), false); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_isPrintWhenDetailOverflows, element.isPrintWhenDetailOverflows(), false); this.writer.addAttributeEnum(SchemaConstants.ATTRIBUTE_mode, ModeEnum, element.getOwnModeValue()); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_x, element.getX()); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_y, element.getY()); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_width, element.getWidth()); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_height, element.getHeight()); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_forecolor, element.getOwnForecolor()); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_backcolor, element.getOwnBackcolor()); this.writer.addAttribute(SchemaConstants.ATTRIBUTE_uuid, element.getUUID()); this.writeProperties(element); this.writer.closeElement(); } private writeStretchType(stretchType: StretchTypeEnum) { this.writer.addAttributeEnum(SchemaConstants.ATTRIBUTE_stretchType, StretchTypeEnum, stretchType, StretchTypeEnum.NoStretch); } private writeProperties(propertiesHolder: JRPropertiesHolder):void { } }