| /** |
| * |
| * 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 v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| * |
| * |
| * This copyright notice shows up in the generated Java code |
| * |
| */ |
| |
| package org.eclipse.osbp.xtext.reportdsl.jvmmodel |
| |
| import com.vaadin.data.Container.Filter |
| import com.vaadin.ui.VerticalLayout |
| import java.util.ArrayList |
| import java.util.HashSet |
| import java.util.List |
| import java.util.Set |
| import javax.inject.Inject |
| import org.eclipse.e4.core.contexts.IEclipseContext |
| import org.eclipse.e4.core.services.events.IEventBroker |
| import org.eclipse.e4.ui.model.application.MApplication |
| import org.eclipse.e4.ui.workbench.IPresentationEngine |
| import org.eclipse.osbp.dsl.semantic.entity.LEntity |
| import org.eclipse.osbp.ui.api.datamart.IDataMart |
| import org.eclipse.osbp.ui.api.report.IReportProvider |
| import org.eclipse.osbp.ui.api.themes.IThemeResourceService |
| import org.eclipse.osbp.ui.api.user.IUser |
| import org.eclipse.osbp.xtext.action.ActionPackage |
| import org.eclipse.osbp.xtext.datamartdsl.jvmmodel.DatamartDSLJvmModelInferrer |
| import org.eclipse.osbp.xtext.reportdsl.DatamartTableAttribute |
| import org.eclipse.osbp.xtext.reportdsl.Grid |
| import org.eclipse.osbp.xtext.reportdsl.GridCell |
| import org.eclipse.osbp.xtext.reportdsl.GridRow |
| import org.eclipse.osbp.xtext.reportdsl.RenderingEnum |
| import org.eclipse.osbp.xtext.reportdsl.Report |
| import org.eclipse.osbp.xtext.reportdsl.ReportDesignFile |
| import org.eclipse.osbp.xtext.reportdsl.ReportDesignTemplate |
| import org.eclipse.osbp.xtext.reportdsl.ReportPackage |
| import org.eclipse.osbp.xtext.reportdsl.TableAttribute |
| import org.eclipse.osbp.xtext.reportdsl.common.AbstractReportBean |
| import org.eclipse.osbp.xtext.reportdsl.common.BaseReport |
| import org.eclipse.xtext.common.types.JvmDeclaredType |
| import org.eclipse.xtext.common.types.JvmVisibility |
| import org.eclipse.xtext.naming.IQualifiedNameProvider |
| import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer |
| import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor |
| import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder |
| import org.eclipse.osbp.xtext.reportdsl.Container |
| import org.eclipse.osbp.ui.api.e4.IE4Focusable |
| import java.util.Map |
| import org.eclipse.osbp.xtext.reportdsl.DatamartTable |
| |
| /** |
| * <p>Infers a JVM model from the source model.</p> |
| * |
| * <p>The JVM model should contain all elements that would appear in the Java code |
| * which is generated from the source model. Other models link against the JVM model rather than the source model.</p> |
| */ |
| class ReportDSLJvmModelInferrer extends AbstractModelInferrer { |
| |
| /** |
| * convenience API to build and initialize JVM types and their members. |
| */ |
| @Inject extension JvmTypesBuilder |
| @Inject extension IQualifiedNameProvider |
| @Inject extension DatamartDSLJvmModelInferrer |
| |
| def dispatch void infer(ReportPackage pckg, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) { |
| pckg.reports.forEach [ report | |
| var cls = report.toClass(report.fullyQualifiedName + "Report") |
| acceptor.accept(cls, [ |
| superTypes += _typeReferenceBuilder.typeRef(BaseReport) |
| superTypes += _typeReferenceBuilder.typeRef(IE4Focusable) |
| it.fileHeader = pckg.documentation |
| it.toConstructor(report) |
| it.toOperations(pckg, report) |
| ]) |
| cls.addReportBean(pckg, report, acceptor) |
| ] |
| } |
| |
| def void addReportBean(JvmDeclaredType type, ReportPackage pckg, Report report, IJvmDeclaredTypeAcceptor acceptor) { |
| var beanCls = report.toClass("Bean") |
| type.members += beanCls |
| acceptor.accept(beanCls, [ |
| getSuperTypes().add(_typeReferenceBuilder.typeRef(AbstractReportBean)) |
| static = true |
| it.members += report.toMethod("createReport", _typeReferenceBuilder.typeRef(BaseReport), [ |
| visibility = JvmVisibility.PROTECTED |
| annotations += _annotationTypesBuilder.annotationRef(Override) |
| body = [ |
| append('''return new «report.fullyQualifiedName»Report(null,null,null);''') |
| ] |
| ]) |
| ]) |
| } |
| |
| def void toConstructor(JvmDeclaredType type, Report report) { |
| type.members += report.toConstructor([ |
| documentation = report.documentation |
| annotations += _annotationTypesBuilder.annotationRef(Inject) |
| parameters += report.toParameter("parent", _typeReferenceBuilder.typeRef(VerticalLayout)) |
| parameters += report.toParameter("context", _typeReferenceBuilder.typeRef(IEclipseContext)) |
| parameters += report.toParameter("app", _typeReferenceBuilder.typeRef(MApplication)) |
| body = [append('''super(parent,context,app);''')] |
| ]) |
| } |
| |
| def void toOperations(JvmDeclaredType type, ReportPackage pckg, Report report) { |
| type.members += report.toField("eventBroker", _typeReferenceBuilder.typeRef(IEventBroker)) [ |
| annotations += _annotationTypesBuilder.annotationRef(Inject) |
| ] |
| type.members += report.toField("renderingEngine", _typeReferenceBuilder.typeRef(IPresentationEngine)) [ |
| annotations += _annotationTypesBuilder.annotationRef(Inject) |
| ] |
| type.members += report.toField("themeResourceService", _typeReferenceBuilder.typeRef(IThemeResourceService)) [ |
| annotations += _annotationTypesBuilder.annotationRef(Inject) |
| ] |
| type.members += |
| report.toField("staticData", |
| _typeReferenceBuilder.typeRef(ArrayList, _typeReferenceBuilder.typeRef(String))) [ |
| initializer = [ |
| append( |
| ''' |
| new ArrayList() {{ |
| «report.addStaticData»}}''') |
| ] |
| ] |
| type.members += |
| report.toMethod("getStaticData", |
| _typeReferenceBuilder.typeRef(List, _typeReferenceBuilder.typeRef(String)), [ |
| annotations += _annotationTypesBuilder.annotationRef(Override) |
| body = [ |
| append(''' |
| return staticData;''') |
| ] |
| ]) |
| if (report.toolbar !== null) { |
| var packageName = (report.toolbar.eContainer as ActionPackage).fullyQualifiedName.toString |
| type.members += |
| report.toField("toolbarClass", |
| _typeReferenceBuilder. |
| typeRef('''«packageName».«report.toolbar.name.toString.toFirstUpper»Toolbar''')) |
| } |
| |
| // create view |
| type.members += report.toMethod("createView", _typeReferenceBuilder.typeRef(Void::TYPE), [ |
| parameters += report.toParameter("parent", _typeReferenceBuilder.typeRef(VerticalLayout)) |
| body = [append('''«report.createView»''')] |
| ]) |
| type.members += |
| report.toMethod("getRenderingAs", _typeReferenceBuilder.typeRef(IReportProvider.Rendering), [ |
| visibility = JvmVisibility.PROTECTED |
| annotations += _annotationTypesBuilder.annotationRef(Override) |
| body = [ |
| switch (report.rendering) { |
| case RenderingEnum.HTML: |
| append('''return Rendering.HTML_STREAM;''') |
| case RenderingEnum.PDF: |
| append('''return Rendering.PDF_STREAM;''') |
| } |
| ] |
| ]) |
| type.members += report.toMethod("getReportName", _typeReferenceBuilder.typeRef(String), [ |
| visibility = JvmVisibility.PROTECTED |
| annotations += _annotationTypesBuilder.annotationRef(Override) |
| body = [append('''return "«report.name»";''')] |
| ]) |
| if (pckg.externalCssURIBundle !== null || report.externalCssURIBundle !== null) { |
| type.members += report.toMethod("getExternalCssURIBundle", _typeReferenceBuilder.typeRef(String), [ |
| visibility = JvmVisibility.PROTECTED |
| annotations += _annotationTypesBuilder.annotationRef(Override) |
| body = [ |
| if (report.externalCssURIBundle !== null) { |
| append('''return "«report.externalCssURIBundle»";''') |
| } else { |
| append('''return "«pckg.externalCssURIBundle»";''') |
| } |
| ] |
| ]) |
| } |
| if (pckg.externalCssURI !== null || report.externalCssURI !== null) { |
| type.members += report.toMethod("getExternalCssURI", _typeReferenceBuilder.typeRef(String), [ |
| visibility = JvmVisibility.PROTECTED |
| annotations += _annotationTypesBuilder.annotationRef(Override) |
| body = [ |
| if (report.externalCssURI !== null) { |
| append('''return "«report.externalCssURI»";''') |
| } else { |
| append('''return "«pckg.externalCssURI»";''') |
| } |
| ] |
| ]) |
| } |
| type.members += |
| report.toMethod("getDatamartInstanceMap", |
| _typeReferenceBuilder.typeRef(Map, _typeReferenceBuilder.typeRef(String), |
| _typeReferenceBuilder.typeRef(IDataMart)), [ |
| documentation = report.datamartRef.documentation |
| visibility = JvmVisibility.PROTECTED |
| annotations += _annotationTypesBuilder.annotationRef(Override) |
| parameters += report.toParameter("user", _typeReferenceBuilder.typeRef(IUser)) |
| body = [ |
| append('''«report.createDatamartInstanceMap»''') |
| ] |
| ]) |
| type.members += report.toMethod("getSelectById", _typeReferenceBuilder.typeRef(boolean), [ |
| documentation = report.datamartRef.documentation |
| visibility = JvmVisibility.PROTECTED |
| annotations += _annotationTypesBuilder.annotationRef(Override) |
| body = [ |
| append(''' |
| return «report.selectById.booleanValue»;''') |
| ] |
| ]) |
| if (report.design instanceof ReportDesignFile) { |
| type.members += report.toMethod("getReportDesignFilename", _typeReferenceBuilder.typeRef(String), [ |
| documentation = report.design.documentation |
| visibility = JvmVisibility.PROTECTED |
| annotations += _annotationTypesBuilder.annotationRef(Override) |
| body = [ |
| append('''return "«(report.design as ReportDesignFile).name»";''') |
| ] |
| ]) |
| } else if (report.design instanceof ReportDesignTemplate) { |
| type.members += report.toMethod("getReportDesignFilename", _typeReferenceBuilder.typeRef(String), [ |
| visibility = JvmVisibility.PROTECTED |
| annotations += _annotationTypesBuilder.annotationRef(Override) |
| body = [ |
| append('''return null;''') |
| ] |
| ]) |
| } |
| type.members += report.toMethod("applyBeanFilter", _typeReferenceBuilder.typeRef(Void::TYPE), [ |
| parameters += report.toParameter("filter", _typeReferenceBuilder.typeRef(Filter)) |
| body = [ |
| // if (dtoQualifiedName == null) { |
| append(''' |
| // !!! no datamart, entity and DTO found for the report --> nothing can be done in here !!! |
| ''') |
| // } |
| // else { |
| // append(''' |
| // if (filter != null) { |
| // fFilterGenerator = ReportBeanFilterGenerator.filter("«entity.fullyQualifiedName.toString»", filter); |
| // } |
| // ''') |
| // } |
| ] |
| ]) |
| type.members += report.toMethod("applyBeanDto", _typeReferenceBuilder.typeRef(Void::TYPE), [ |
| parameters += report.toParameter("dto", _typeReferenceBuilder.typeRef(Object)) |
| body = [ |
| // if (dtoQualifiedName == null) { |
| append(''' |
| // !!! no datamart, entity and DTO found for the report --> nothing can be done in here !!! |
| ''') |
| // } |
| // else { |
| // append(''' |
| // if (dto != null) { |
| // fFilterGenerator = ReportBeanFilterGenerator.dto(dto, ((«dtoQualifiedName»)dto).getId()); |
| // } |
| // ''') |
| // } |
| ] |
| ]) |
| } |
| |
| def addStaticData(Report report) { |
| if (report.design instanceof ReportDesignTemplate) { |
| if ((report.design as ReportDesignTemplate).header !== null) { |
| return decodeContainer((report.design as ReportDesignTemplate).header) |
| } |
| } |
| } |
| |
| def String decodeContainer(Container container) { |
| var body = "" |
| for (element : container.elements) { |
| switch (element) { |
| Grid: { |
| for (row : element.rows) { |
| switch (row) { |
| GridRow: |
| for (cell : (row as GridRow).cells) { |
| body = body + decodeContainer(cell) |
| } |
| GridCell: |
| body = body + decodeContainer(row as GridCell) |
| } |
| } |
| } |
| DatamartTableAttribute: { |
| if ((element as DatamartTableAttribute).column instanceof TableAttribute) { |
| body = '''«body»add("«((element as DatamartTableAttribute).column as TableAttribute).valueRef.attributeRef.name»"); |
| ''' |
| } |
| } |
| } |
| } |
| return body |
| } |
| |
| /** |
| * get a set of all entities used in the given report |
| */ |
| def Set<LEntity> findRequestedEntities(Report report) { |
| var entities = new HashSet<LEntity>() |
| entities.addAll(report.datamartRef.findAllEntities()) |
| return entities |
| } |
| |
| def createView(Report report) ''' |
| super.createView(parent); |
| getContext().set(IE4Focusable.class, this); |
| workset = new ReportGeneratorWorkSet(); |
| «IF report.toolbar !== null» |
| toolbarClass = new «report.toolbar.name.toString.toFirstUpper»Toolbar(UUID.randomUUID().toString()); |
| toolbarClass.createToolbar(getContext(), renderingEngine, themeResourceService); |
| addPDFDownload(toolbarClass); |
| «ENDIF» |
| createFilter(); |
| ''' |
| |
| def createDatamartInstanceMap(Report report) { |
| var dataSetName = Utilities.getDataSetName(null, report.datamartRef).toString |
| var text = ''' |
| Map<String, IDataMart> datamartInstanceMap = new HashMap(); |
| «report.datamartRef.fullyQualifiedName»Datamart «report.datamartRef.name.toFirstLower»Datamart = new «report.datamartRef.fullyQualifiedName»Datamart(); |
| «report.datamartRef.name.toFirstLower»Datamart.setUser(user); |
| datamartInstanceMap.put("«dataSetName»", «report.datamartRef.name.toFirstLower»Datamart);''' |
| if (report.design instanceof ReportDesignTemplate){ |
| for (detailElement : (report.design as ReportDesignTemplate).detail.elements){ |
| if (detailElement instanceof DatamartTable){ |
| var datamartDefinition = (detailElement as DatamartTable).datamartRef |
| if (datamartDefinition !== null) { |
| var alias = (detailElement as DatamartTable).name |
| dataSetName = Utilities.getDataSetName(alias, datamartDefinition).toString |
| text = ''' |
| «text» |
| «datamartDefinition.fullyQualifiedName»Datamart «datamartDefinition.name.toFirstLower»Datamart = new «datamartDefinition.fullyQualifiedName»Datamart(); |
| «datamartDefinition.name.toFirstLower»Datamart.setUser(user); |
| datamartInstanceMap.put("«dataSetName»", «datamartDefinition.name.toFirstLower»Datamart);''' |
| } |
| } |
| } |
| text = ''' |
| «text» |
| return datamartInstanceMap;''' |
| } |
| return text |
| } |
| } |
| |