blob: 21b0be47a303c759e0f495f35ad2307f626562df [file] [log] [blame]
/**
*
* 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
*
*
* This copyright notice shows up in the generated Java code
*
*/
package org.eclipse.osbp.xtext.reportdsl.validation
import java.util.Collection
import java.util.HashSet
import java.util.LinkedHashSet
import java.util.Set
import org.eclipse.emf.ecore.EObject
import org.eclipse.osbp.dsl.semantic.entity.LEntityAttribute
import org.eclipse.osbp.xtext.datamartdsl.DatamartAttribute
import org.eclipse.osbp.xtext.reportdsl.AutoText
import org.eclipse.osbp.xtext.reportdsl.DatamartTable
import org.eclipse.osbp.xtext.reportdsl.DatamartTableAttribute
import org.eclipse.osbp.xtext.reportdsl.DatamartTableDetail
import org.eclipse.osbp.xtext.reportdsl.DatamartTableGroup
import org.eclipse.osbp.xtext.reportdsl.DatamartTableHeader
import org.eclipse.osbp.xtext.reportdsl.Grid
import org.eclipse.osbp.xtext.reportdsl.GridCell
import org.eclipse.osbp.xtext.reportdsl.Image
import org.eclipse.osbp.xtext.reportdsl.ImageSourceEnum
import org.eclipse.osbp.xtext.reportdsl.PageFooter
import org.eclipse.osbp.xtext.reportdsl.PageHeader
import org.eclipse.osbp.xtext.reportdsl.PageTemplate
import org.eclipse.osbp.xtext.reportdsl.ReportDSLPackage
import org.eclipse.osbp.xtext.reportdsl.SubSubTitle
import org.eclipse.osbp.xtext.reportdsl.SubTitle
import org.eclipse.osbp.xtext.reportdsl.TableAggregation
import org.eclipse.osbp.xtext.reportdsl.TableAttribute
import org.eclipse.osbp.xtext.reportdsl.TableBinaryAggregation
import org.eclipse.osbp.xtext.reportdsl.TableMultipleAggregation
import org.eclipse.osbp.xtext.reportdsl.TableUnaryAggregation
import org.eclipse.osbp.xtext.reportdsl.Text
import org.eclipse.osbp.xtext.reportdsl.Title
import org.eclipse.xtext.validation.Check
import org.eclipse.osbp.xtext.reportdsl.GridRow
/**
* Custom validation rules.
*
* see http://www.eclipse.org/Xtext/documentation.html#validation
*/
class ReportDSLValidator extends AbstractReportDSLValidator {
@Check
def checkTableAttribute(TableAttribute tableAttribute) {
var eObj = tableAttribute.eContainer
val checkPageTemplateBelonging = tableAttribute.checkPageTemplateBelonging
val checkTableHeaderBelonging = tableAttribute.checkTableHeaderBelonging
if (checkPageTemplateBelonging || checkTableHeaderBelonging) {
while ((eObj !== null) && !(eObj instanceof DatamartTableAttribute)){
eObj = eObj.eContainer
}
if (eObj instanceof DatamartTableAttribute){
var datamartTableAttribute = eObj as DatamartTableAttribute
error('''A table attribute is not allowed in a «IF checkPageTemplateBelonging»page template«ENDIF»«IF checkTableHeaderBelonging»table header«ENDIF» definition''', datamartTableAttribute, ReportDSLPackage.Literals.DATAMART_TABLE_ATTRIBUTE__COLUMN)
}
} else {
while ((eObj !== null) && !(eObj instanceof DatamartTableDetail)){
eObj = eObj.eContainer
}
if (eObj instanceof DatamartTableDetail){
var datamartTableDetail = eObj as DatamartTableDetail
var duplicates = datamartTableDetail.columnFormatters.findDuplicates
if (duplicates.contains(tableAttribute.valueRef)) {
error('Duplicated table attributes are not allowed in a table template definition', tableAttribute, ReportDSLPackage.Literals.TABLE_ATTRIBUTE__VALUE_REF)
}
}
}
}
@Check
def checkDatamartTableAttribute(DatamartTableAttribute attribute) {
var eObj = attribute.eContainer
while ((eObj !== null) && !(eObj instanceof DatamartTableDetail)){
eObj = eObj.eContainer
}
if (eObj instanceof DatamartTableDetail){
var datamartTableDetail = eObj as DatamartTableDetail
if (attribute?.style?.name.equals(datamartTableDetail?.style?.name)) {
error('Avoid the use of the same style in table attribute as in table detail.', datamartTableDetail, ReportDSLPackage.Literals.WITH_STYLE__STYLE)
error('Avoid the use of the same style in table attribute as in table detail.', attribute, ReportDSLPackage.Literals.WITH_STYLE__STYLE)
}
}
}
@Check
def checkDatamartTableGroup(DatamartTableGroup datamartTableGroup) {
var eObj = datamartTableGroup.eContainer
if (datamartTableGroup.checkPageTemplateBelonging) {
while ((eObj !== null) && !(eObj instanceof DatamartTable)){
eObj = eObj.eContainer
}
if (eObj instanceof DatamartTable){
var datamartTable = eObj as DatamartTable
error('A group definition is not allowed within a page template.', datamartTable, ReportDSLPackage.Literals.DATAMART_TABLE__GROUPS)
}
}
}
@Check
def checkTableAggregationOnGroup(TableAggregation aggregation) {
if (aggregation.onGroup !== null) {
var eObj = aggregation.eContainer
// A group definition is allowed within a table, but not within a grid.
// So if the first container found is a Grid instead of a DatamartTable the group in defined within a grid.
while ((eObj !== null) && (!(eObj instanceof DatamartTable) && !(eObj instanceof Grid))){
eObj = eObj.eContainer
}
if (eObj instanceof Grid){
error('A group definition is not allowed within a grid.', aggregation, ReportDSLPackage.Literals.TABLE_AGGREGATION__ON_GROUP)
}
}
}
@Check
def checkTable(DatamartTable table) {
var place = ""
var pageTemplateBelonging = table.checkPageTemplateBelonging
var eObj = table.eContainer
while ((eObj !== null) && (!((eObj instanceof PageHeader) || (eObj instanceof PageFooter) || (eObj instanceof GridCell)))){
eObj = eObj.eContainer
}
if (eObj instanceof PageHeader){
var pageHeader = eObj as PageHeader
if (pageTemplateBelonging) {
place = "page"
} else {
place = "header"
}
error(place.errorTxt, pageHeader, ReportDSLPackage.Literals.CONTAINER__ELEMENTS)
} else if (eObj instanceof PageFooter){
var pageFooter = eObj as PageFooter
if (pageTemplateBelonging) {
place = "page"
} else {
place = "footer"
}
error(place.errorTxt, pageFooter, ReportDSLPackage.Literals.CONTAINER__ELEMENTS)
} else if (eObj instanceof GridCell){
val gridCell = eObj as GridCell
if (pageTemplateBelonging) {
place = "page"
} else {
var eObj2 = table.eContainer
while ((eObj2 !== null) && (!((eObj2 instanceof PageHeader) || (eObj2 instanceof PageFooter)))){
eObj2 = eObj2.eContainer
}
if (eObj2 instanceof PageHeader){
place = "header"
} else if (eObj2 instanceof PageFooter){
place = "footer"
}
}
if (!place.isNullOrEmpty){
error(place.errorTxt, gridCell, ReportDSLPackage.Literals.CONTAINER__ELEMENTS)
}
}
}
@Check
def checkText(Text text) {
if (text.checkTableHeaderBelonging) {
error('''A text is not allowed in a table header definition''', text.eContainer, ReportDSLPackage.Literals.CONTAINER__ELEMENTS)
}
}
@Check
def checkAutoText(AutoText text) {
if (text.checkTableHeaderBelonging) {
error('''A auto text is not allowed in a table header definition''', text.eContainer, ReportDSLPackage.Literals.CONTAINER__ELEMENTS)
}
}
@Check
def checkTitle(Title title) {
if (title.checkTableHeaderBelonging) {
error('''A title is not allowed in a table header definition''', title.eContainer, ReportDSLPackage.Literals.CONTAINER__ELEMENTS)
}
}
@Check
def checkSubTitle(SubTitle title) {
if (title.checkTableHeaderBelonging) {
error('''A sub title is not allowed in a table header definition''', title.eContainer, ReportDSLPackage.Literals.CONTAINER__ELEMENTS)
}
}
@Check
def checkSubSubTitle(SubSubTitle title) {
if (title.checkTableHeaderBelonging) {
error('''A sub sub title is not allowed in a table header definition''', title.eContainer, ReportDSLPackage.Literals.CONTAINER__ELEMENTS)
}
}
@Check
def checkImage(Image image) {
if (image.checkTableHeaderBelonging) {
error('''A image is not allowed in a table header definition''', image.eContainer, ReportDSLPackage.Literals.CONTAINER__ELEMENTS)
}
}
@Check
def checkTableAggregation(TableAggregation aggregation) {
val checkPageTemplateBelonging = aggregation.checkPageTemplateBelonging
val checkTableHeaderBelonging = aggregation.checkTableHeaderBelonging
if (checkPageTemplateBelonging || checkTableHeaderBelonging) {
error('''A table aggregation is not allowed in a «IF checkPageTemplateBelonging»page template«ENDIF»«IF checkTableHeaderBelonging»table header«ENDIF» definition''', aggregation, ReportDSLPackage.Literals.TABLE_AGGREGATION__ON_GROUP)
}
}
@Check
def checkTableUnaryAggregation(TableUnaryAggregation aggregation) {
if (aggregation.checkPageTemplateBelonging) {
error('A table unary aggregation is not allowed in a page template definition', aggregation, ReportDSLPackage.Literals.TABLE_UNARY_AGGREGATION__AGGREGATION)
} else if (aggregation.checkPageHeaderBelonging) {
error('A table unary aggregation is not allowed in a page header definition', aggregation, ReportDSLPackage.Literals.TABLE_UNARY_AGGREGATION__AGGREGATION)
} else if (aggregation.checkPageFooterBelonging) {
error('A table unary aggregation is not allowed in a page footer definition', aggregation, ReportDSLPackage.Literals.TABLE_UNARY_AGGREGATION__AGGREGATION)
}
}
@Check
def checkTableMultipleAggregation(TableMultipleAggregation aggregation) {
if (aggregation.checkPageTemplateBelonging) {
error('A table multiple aggregation is not allowed in a page template definition', aggregation, ReportDSLPackage.Literals.TABLE_MULTIPLE_AGGREGATION__AGGREGATION)
} else if (aggregation.checkPageHeaderBelonging) {
error('A table multiple aggregation is not allowed in a page header definition', aggregation, ReportDSLPackage.Literals.TABLE_MULTIPLE_AGGREGATION__AGGREGATION)
} else if (aggregation.checkPageFooterBelonging) {
error('A table multiple aggregation is not allowed in a page footer definition', aggregation, ReportDSLPackage.Literals.TABLE_MULTIPLE_AGGREGATION__AGGREGATION)
}
}
@Check
def checkTableBinaryAggregation(TableBinaryAggregation aggregation) {
if (aggregation.checkPageTemplateBelonging) {
error('A table binary aggregation is not allowed in a page template definition', aggregation, ReportDSLPackage.Literals.TABLE_BINARY_AGGREGATION__AGGREGATION)
} else if (aggregation.checkPageHeaderBelonging) {
error('A table binary aggregation is not allowed in a page header definition', aggregation, ReportDSLPackage.Literals.TABLE_BINARY_AGGREGATION__AGGREGATION)
} else if (aggregation.checkPageFooterBelonging) {
error('A table binary aggregation is not allowed in a page footer definition', aggregation, ReportDSLPackage.Literals.TABLE_BINARY_AGGREGATION__AGGREGATION)
}
}
@Check
def checkImageUrl(Image image) {
if (image.filename.empty){
error('An empty file name is not allowed!', image, ReportDSLPackage.Literals.IMAGE__FILENAME)
} else if (ImageSourceEnum.URL.equals(image.sourceType)) {
if (!(image.filename.startsWith("http://") || image.filename.startsWith("https://"))){
error('A non http URL is not allowed!', image, ReportDSLPackage.Literals.IMAGE__FILENAME)
}
}
}
@Check
def checkGridCellColumnSpan(Grid grid) {
var sumColSpan = 0
var firstColSpan = 0
for (GridRow row: grid.getRows()) {
var rowCellSpan = 0
var cellSize = row.getCells().size()
for (GridCell cell : row.getCells()){
rowCellSpan = rowCellSpan + (cell.getColumnspan - 1)
}
if (rowCellSpan < 0){
sumColSpan = cellSize
}else{
sumColSpan = rowCellSpan + cellSize
}
if (firstColSpan == 0){
firstColSpan = sumColSpan
}else if (sumColSpan != firstColSpan){
for (GridCell cell : row.getCells()){
error('''Sum of all cells's ColumnSpan of this row should be «firstColSpan».''', cell , ReportDSLPackage.Literals.GRID_CELL__COLUMNSPAN)
}
}
}
}
def private boolean checkPageTemplateBelonging(EObject attribute) {
var eObj = attribute.eContainer
while ((eObj!==null) && !(eObj instanceof PageTemplate)){
eObj = eObj.eContainer
}
if ((eObj instanceof PageTemplate)){
return true
}
return false
}
def private boolean checkGridBelonging(EObject attribute) {
var eObj = attribute.eContainer
while ((eObj!==null) && !(eObj instanceof Grid)){
eObj = eObj.eContainer
}
if ((eObj instanceof Grid)){
return true
}
return false
}
def private boolean checkTableHeaderBelonging(EObject attribute) {
var eObj = attribute.eContainer
while ((eObj!==null) && !(eObj instanceof DatamartTableHeader)){
eObj = eObj.eContainer
}
if ((eObj instanceof DatamartTableHeader)){
return (eObj.eContainer instanceof DatamartTable)
}
return false
}
def private boolean checkPageHeaderBelonging(EObject attribute) {
var eObj = attribute.eContainer
while ((eObj!==null) && !(eObj instanceof PageHeader)){
eObj = eObj.eContainer
}
if ((eObj instanceof PageHeader)){
return true
}
return false
}
def private boolean checkPageFooterBelonging(EObject attribute) {
var eObj = attribute.eContainer
while ((eObj!==null) && !(eObj instanceof PageFooter)){
eObj = eObj.eContainer
}
if ((eObj instanceof PageFooter)){
return true
}
return false
}
def private Set<DatamartAttribute> findDuplicates(Collection<DatamartTableAttribute> list) {
var duplicates = new LinkedHashSet<DatamartAttribute>();
var uniques = new HashSet<LEntityAttribute>();
for(DatamartTableAttribute attribute : list) {
if (attribute.column instanceof TableAttribute){
var tableAttribute = attribute.column as TableAttribute
if(!uniques.add(tableAttribute.valueRef.attributeRef)) {
duplicates.add(tableAttribute.valueRef);
}
}
}
return duplicates;
}
def private String errorTxt(String place) '''A table is not allowed in a place template definition'''
}