| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| */ |
| package org.eclipse.osbp.xtext.reportdsl.common.item; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| import org.eclipse.birt.report.model.api.OdaDataSetHandle; |
| import org.eclipse.birt.report.model.api.ReportItemHandle; |
| import org.eclipse.birt.report.model.api.TextItemHandle; |
| import org.eclipse.birt.report.model.api.activity.SemanticException; |
| import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants; |
| import org.eclipse.birt.report.model.api.elements.structures.ComputedColumn; |
| import org.eclipse.birt.report.model.elements.interfaces.IReportItemModel; |
| import org.eclipse.birt.report.model.elements.interfaces.IStyleModel; |
| import org.eclipse.osbp.ui.api.datamart.IDataMart; |
| import org.eclipse.osbp.ui.api.datamart.IDataMart.EType; |
| import org.eclipse.osbp.utils.entityhelper.DataType; |
| import org.eclipse.osbp.xtext.datamartdsl.DatamartAttribute; |
| import org.eclipse.osbp.xtext.datamartdsl.DatamartDefinition; |
| import org.eclipse.osbp.xtext.reportdsl.AutoTextEnum; |
| import org.eclipse.osbp.xtext.reportdsl.NonTranslatableText; |
| import org.eclipse.osbp.xtext.reportdsl.RenderingEnum; |
| import org.eclipse.osbp.xtext.reportdsl.StringBinder; |
| import org.eclipse.osbp.xtext.reportdsl.StringBinderValue; |
| import org.eclipse.osbp.xtext.reportdsl.TableAttribute; |
| import org.eclipse.osbp.xtext.reportdsl.TranslatableText; |
| import org.eclipse.osbp.xtext.reportdsl.common.DatatypeUtilities; |
| import org.eclipse.osbp.xtext.reportdsl.jvmmodel.ReportGeneratorWorkSet; |
| import org.eclipse.osbp.xtext.reportdsl.jvmmodel.Utilities; |
| import org.eclipse.xtext.util.Pair; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public class TextItem extends ABaseItem { |
| |
| private static final Logger LOGGER = LoggerFactory.getLogger(TextItem.class); |
| private final TextItemHandle fItem; |
| |
| public TextItem(ReportGeneratorWorkSet workset, String i18nKey, String content, String contentType) { |
| super(workset); |
| fItem = workset.getFactory().newTextItem(null); |
| try { |
| fItem.setContentKey(i18nKey); |
| fItem.setContentType(contentType); |
| fItem.setContent(content); |
| } catch (SemanticException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| |
| public TextItem(ReportGeneratorWorkSet workset, String content, String contentType) { |
| super(workset); |
| fItem = workset.getFactory().newTextItem(null); |
| try { |
| fItem.setContentType(contentType); |
| fItem.setContent(content); |
| } catch (SemanticException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| |
| public TextItem(ReportGeneratorWorkSet workset, String contentType, DatamartDefinition datamart, |
| List<Pair<DatamartAttribute, String>> datamartAttributes) { |
| super(workset); |
| fItem = workset.getFactory().newTextItem(null); |
| try { |
| fItem.setContentType(contentType); |
| String dataSetName = Utilities.getDataSetName(null, datamart); |
| OdaDataSetHandle dataSetHandle = workset.getDataSets().get(dataSetName); |
| if (dataSetHandle != null) { |
| fItem.setDataSet(dataSetHandle); |
| setBoundColumns(datamartAttributes); |
| } |
| } catch (SemanticException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| |
| public TextItem(ReportGeneratorWorkSet workset, String contentType, DatamartAttribute datamartAttribute, String dataTypeStr) { |
| super(workset); |
| fItem = workset.getFactory().newTextItem(null); |
| try { |
| fItem.setContentType(contentType); |
| fItem.setContent(getContent(createComputedColumn(datamartAttribute, dataTypeStr))); |
| } catch (SemanticException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| |
| public TextItem(ReportGeneratorWorkSet workset, String contentType, IDataMart.EType eType, String attributeName) { |
| super(workset); |
| fItem = workset.getFactory().newTextItem(null); |
| try { |
| fItem.setContentType(contentType); |
| // The font family is only required for PDF rendering. |
| if (RenderingEnum.PDF.getLiteral().equals(workset.getReport().getRendering().getLiteral())) { |
| setFontFamily(eType); |
| } |
| fItem.setContent(getContent(workset.getReport().getRendering(), eType, attributeName)); |
| } catch (SemanticException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| |
| private void setFontFamily(EType eType) throws SemanticException { |
| if (IDataMart.EType.BOOLEAN_CHECKBOX.equals(eType)){ |
| fItem.setProperty(IStyleModel.FONT_FAMILY_PROP, "Wingdings"); |
| } |
| |
| } |
| |
| public TextItem(ReportGeneratorWorkSet workset, String contentType, ComputedColumn column) { |
| super(workset); |
| fItem = workset.getFactory().newTextItem(null); |
| try { |
| fItem.setContentType(contentType); |
| fItem.setContent(getContent(column)); |
| } catch (SemanticException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| |
| public TextItem(ReportGeneratorWorkSet workset, String contentType, DatamartDefinition datamart, |
| StringBinder stringBinder, DataType dtType) { |
| super(workset); |
| fItem = workset.getFactory().newTextItem(null); |
| try { |
| fItem.setContentType(contentType); |
| String dataSetName = Utilities.getDataSetName(null, datamart); |
| OdaDataSetHandle dataSetHandle = workset.getDataSets().get(dataSetName); |
| if (dataSetHandle != null) { |
| fItem.setDataSet(dataSetHandle); |
| setContent(stringBinder, dtType); |
| } |
| } catch (SemanticException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| |
| public TextItem(ReportGeneratorWorkSet workset, String contentType, AutoTextEnum autoTextEnum) { |
| super(workset); |
| fItem = workset.getFactory().newTextItem(null); |
| try { |
| fItem.setContentType(contentType); |
| setContent(autoTextEnum); |
| } catch (SemanticException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| |
| |
| // private String getContent(ArrayList<ComputedColumn> boundColumns){ |
| // StringBuilder result = new StringBuilder(); |
| // for (int columncount = 0; columncount < boundColumns.size(); |
| // columncount++) { |
| // result.append(getRowContent(boundColumns.get(columncount).getName())); |
| // if ((boundColumns.size() > 1) && ((columncount + 1) != |
| // boundColumns.size())) { |
| // result.append(" + \" \" + "); |
| // } |
| // } |
| // return getValueOfContent(result.toString()); |
| // } |
| // |
| |
| private String getContent(ArrayList<Object> elements) { |
| StringBuilder result = new StringBuilder(); |
| for (int elementsCount = 0; elementsCount < elements.size(); elementsCount++) { |
| Object element = elements.get(elementsCount); |
| String elementValue = getElementValue(element); |
| result.append(elementValue); |
| if ((elements.size() > 1) && ((elementsCount + 1) != elements.size())) { |
| result.append(" + \" \" + "); |
| } |
| } |
| return getValueOfContent(result.toString()); |
| } |
| |
| private String getContent(Object element) { |
| return getValueOfContent(getElementValue(element)); |
| } |
| |
| private String getContent(RenderingEnum renderingEnum, IDataMart.EType eType, String attributeName) { |
| String content = ""; |
| if (EType.BOOLEAN_CHECKBOX.equals(eType)){ |
| // In case of PDF rendering the Windings char code is used. |
| if (RenderingEnum.PDF.getLiteral().equals(renderingEnum.getLiteral())) { |
| content = "if(row[\"" + attributeName + "\"]){String.fromCharCode(254);}else{String.fromCharCode(168);}"; |
| // In case of HTML rendering the unicode char code is used. |
| } else if (RenderingEnum.HTML.getLiteral().equals(renderingEnum.getLiteral())) { |
| content = "if(row[\"" + attributeName + "\"]){String.fromCharCode(9745);}else{String.fromCharCode(9723);}"; |
| } |
| } |
| return getValueOfContent(content); |
| } |
| |
| private String getElementValue(Object element) { |
| String elementValue = ""; |
| if (element instanceof ComputedColumn) { |
| String dataType = ((ComputedColumn) element).getDataType(); |
| if (DesignChoiceConstants.COLUMN_DATA_TYPE_DATE.equals(dataType)) { |
| elementValue = getDateRowContent(((ComputedColumn) element).getName()); |
| } else if (DesignChoiceConstants.COLUMN_DATA_TYPE_TIME.equals(dataType)) { |
| elementValue = getTimeRowContent(((ComputedColumn) element).getName()); |
| } else if (DesignChoiceConstants.COLUMN_DATA_TYPE_DATETIME.equals(dataType)) { |
| elementValue = getDateTimeRowContent(((ComputedColumn) element).getName()); |
| } else { |
| elementValue = getRowContent(((ComputedColumn) element).getName()); |
| } |
| } else if (element instanceof TranslatableText) { |
| elementValue = "reportContext.getMessage(\"" + (((TranslatableText) element).getValue()) |
| + "\",reportContext.getLocale())"; |
| } else if (element instanceof NonTranslatableText) { |
| elementValue = "\"" + (((NonTranslatableText) element).getValue()) + "\""; |
| } |
| |
| return elementValue; |
| } |
| |
| private String getDateRowContent(String rowContent) { |
| return "java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT,reportContext.getLocale()).format(row[\"" |
| + rowContent + "\"] || new Date(\"01-01-1970\"))"; |
| } |
| |
| private String getDateTimeRowContent(String rowContent) { |
| return "java.text.DateFormat.getDateTimeInstance(java.text.DateFormat.SHORT,java.text.DateFormat.SHORT,reportContext.getLocale()).format(row[\"" |
| + rowContent + "\"] || new Date(\"01-01-1970\"))"; |
| } |
| |
| private String getTimeRowContent(String rowContent) { |
| return "java.text.DateFormat.getTimeInstance(java.text.DateFormat.SHORT,reportContext.getLocale()).format(row[\"" |
| + rowContent + "\"] || new Date(\"01-01-1970\"))"; |
| } |
| |
| private String getValueOfContent(String content) { |
| return "<VALUE-OF>" + content + "</VALUE-OF>"; |
| } |
| |
| private void setBoundColumns(List<Pair<DatamartAttribute, String>> datamartAttributes) { // NOSONAR |
| ArrayList<Object> boundColumns = new ArrayList<>(); |
| try { |
| for (Pair<DatamartAttribute, String> pair : datamartAttributes) { |
| DatamartAttribute datamartAttribute = pair.getFirst(); |
| String dataTypeStr = pair.getSecond(); |
| boundColumns.add(createComputedColumn(datamartAttribute, dataTypeStr)); |
| } |
| // --- set all bound elements --- |
| fItem.getElement().setProperty(IReportItemModel.BOUND_DATA_COLUMNS_PROP, boundColumns); |
| fItem.setContent(getContent(boundColumns)); |
| } catch (SemanticException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| |
| private void setContent(StringBinder stringBinder, DataType dtType) throws SemanticException { // NOSONAR |
| ArrayList<Object> contentElements = new ArrayList<>(); |
| for (StringBinderValue stringBinderValue : stringBinder.getElements()) { |
| if (stringBinderValue instanceof TableAttribute) { |
| DatamartAttribute datamartAttribute = ((TableAttribute) stringBinderValue).getValueRef(); |
| String dataTypeStr = DatatypeUtilities.getDatatypeName(datamartAttribute, dtType); |
| ComputedColumn computedColumn = createComputedColumn(datamartAttribute, dataTypeStr); |
| contentElements.add(computedColumn); |
| } else if (stringBinderValue instanceof StringBinderValue) { |
| contentElements.add(stringBinderValue); |
| } |
| } |
| fItem.setContent(getContent(contentElements)); |
| } |
| |
| private void setContent(AutoTextEnum autoTextEnum) throws SemanticException { // NOSONAR |
| String content; |
| switch (autoTextEnum) { |
| case ACTUAL_DATE: |
| content = "java.text.DateFormat.getDateInstance(3).format(new Date())"; |
| fItem.setContent(getValueOfContent(content)); |
| break; |
| case ACTUAL_TIME: |
| content = "java.text.DateFormat.getTimeInstance(3).format(new Date())"; |
| fItem.setContent(getValueOfContent(content)); |
| break; |
| default: |
| break; |
| } |
| } |
| |
| @Override |
| public ReportItemHandle getHandle() { |
| return fItem; |
| } |
| |
| } |