blob: ba74620ccf20b0dd3737082797074b0a5b20c47e [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.scoping
import java.util.ArrayList
import javax.inject.Inject
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EReference
import org.eclipse.osbp.dsl.common.xtext.extensions.ModelExtensions
import org.eclipse.osbp.dsl.dto.xtext.jvmmodel.TypeHelper
import org.eclipse.osbp.dsl.semantic.common.types.LDataType
import org.eclipse.osbp.utils.entityhelper.DataType
import org.eclipse.osbp.xtext.datamartdsl.DatamartAttribute
import org.eclipse.osbp.xtext.datamartdsl.DatamartEntity
import org.eclipse.osbp.xtext.datamartdsl.DatamartMember
import org.eclipse.osbp.xtext.datamartdsl.DatamartOwner
import org.eclipse.osbp.xtext.datamartdsl.util.DatamartAttributeUtil
import org.eclipse.osbp.xtext.reportdsl.DatamartContainer
import org.eclipse.osbp.xtext.reportdsl.ReportDSLPackage
import org.eclipse.osbp.xtext.reportdsl.TableUnaryAggregation
import org.eclipse.xtext.common.types.JvmDeclaredType
import org.eclipse.xtext.common.types.JvmType
import org.eclipse.xtext.common.types.util.RawSuperTypes
import org.eclipse.xtext.resource.EObjectDescription
import org.eclipse.xtext.resource.IEObjectDescription
import org.eclipse.xtext.scoping.IScope
import org.eclipse.xtext.scoping.impl.MapBasedScope
/**
* This class contains custom scoping description.
*
* see : http://www.eclipse.org/Xtext/documentation.html#scoping
* on how and when to use it
*
*/
class ReportDSLScopeProvider extends AbstractReportDSLScopeProvider {
@Inject extension DataType
@Inject extension TypeHelper
@Inject extension ModelExtensions
override IScope getScope(EObject context, EReference reference) {
if (reference == ReportDSLPackage.Literals.DATAMART_TABLE_GROUP__GROUPING_REF) {
return context.getScope_DatamartProperty
} else if (reference == ReportDSLPackage.Literals.TABLE_ATTRIBUTE__VALUE_REF) {
return context.getScope_DatamartProperty
} else if (reference == ReportDSLPackage.Literals.VISIBILITY__VISIBILITY_ATTRIBUTE) {
return context.getScope_BooleanDatamartProperty
} else if (reference == ReportDSLPackage.Literals.TABLE_BASE_AGGREGATION__VALUE_REFS) {
if (context instanceof TableUnaryAggregation) {
return context.getScope_DatamartProperty.limitedScopeToSuperTypedNumber
}
return context.getScope_DatamartProperty
}
super.getScope(context, reference)
}
def IScope getLimitedScopeToSuperTypedNumber(IScope scope) {
var result = <IEObjectDescription>newArrayList
for (eObjDescription : scope.allElements) {
var eObj = eObjDescription.EObjectOrProxy
if (eObj instanceof DatamartAttribute) {
var attribute = eObj as DatamartAttribute
var type = attribute.attributeRef.type
if (type instanceof LDataType) {
if(isNumericOrWrapperType(type)) {
result.add(eObjDescription)
}
}
}
}
if (result.empty) {
return scope
}
return MapBasedScope.createScope(IScope.NULLSCOPE, result)
}
def ArrayList<IEObjectDescription> getDatamartPropertyResultList(EObject context) {
var result = <IEObjectDescription>newArrayList
var eObj = context
while ((eObj !== null) && !(eObj instanceof DatamartContainer)) {
eObj = eObj.eContainer
}
if (eObj !== null) {
var datamart = (eObj as DatamartContainer).datamartRef
if (datamart !== null && datamart.source !== null) {
if (datamart.source instanceof DatamartEntity) {
getDatamartAttributes(datamart.source as DatamartEntity, result)
}
} else {
result = eObj.eContainer.datamartPropertyResultList
}
}
return result
}
def IScope getScope_DatamartProperty(EObject context) {
return MapBasedScope.createScope(IScope.NULLSCOPE, context.datamartPropertyResultList)
}
def IScope getScope_BooleanDatamartProperty(EObject context) {
var resultBoolean = <IEObjectDescription>newArrayList
for (resultItem : context.datamartPropertyResultList) {
if (resultItem.EObjectOrProxy instanceof DatamartAttribute) {
var entityAttribute = (resultItem.EObjectOrProxy as DatamartAttribute).attributeRef
if (entityAttribute.jvmType.boolean) {
resultBoolean.add(resultItem)
}
}
}
return MapBasedScope.createScope(IScope.NULLSCOPE, resultBoolean)
}
def void getDatamartAttributes(DatamartEntity datamartEntity, ArrayList<IEObjectDescription> result) {
for (navigation : datamartEntity.navigations) {
if (navigation instanceof DatamartMember) {
(navigation as DatamartMember).datamartEntity.getDatamartAttributes(result)
for (attribute : (navigation as DatamartMember).datamartEntity.attributes) {
result.add(
EObjectDescription.create(DatamartAttributeUtil.getAliasedAttributeName(attribute), attribute))
}
}
if (navigation instanceof DatamartOwner) {
(navigation as DatamartOwner).datamartEntity.getDatamartAttributes(result)
for (attribute : (navigation as DatamartOwner).datamartEntity.attributes) {
result.add(
EObjectDescription.create(DatamartAttributeUtil.getAliasedAttributeName(attribute), attribute))
}
}
}
if (datamartEntity?.attributes.nullOrEmpty) {
DatamartAttributeUtil.fillEmptyAttributes(datamartEntity)
}
for (datamartAttribute : datamartEntity.attributes) {
result.add(
EObjectDescription.create(DatamartAttributeUtil.getAliasedAttributeName(datamartAttribute),
datamartAttribute))
}
}
def boolean containsSuperType(JvmType type, Class<?> clazz) {
if (!(type instanceof JvmDeclaredType)) {
return false
}
return new RawSuperTypes().collectNames(type).contains(clazz.canonicalName)
}
}