blob: 1f48a3a154e305cb3a75c44b6e0150690d43db2e [file] [log] [blame]
/*
*
* Copyright (c) 2013, 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
*
*/
package org.eclipse.osbp.xtext.datamartdsl.scoping;
import java.util.ArrayList
import javax.inject.Inject
import org.eclipse.emf.common.util.BasicEList
import org.eclipse.emf.common.util.EList
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EReference
import org.eclipse.emf.ecore.InternalEObject
import org.eclipse.osbp.dsl.common.xtext.extensions.ModelExtensions
import org.eclipse.osbp.dsl.semantic.entity.LEntity
import org.eclipse.osbp.dsl.semantic.entity.LEntityAttribute
import org.eclipse.osbp.dsl.semantic.entity.LEntityFeature
import org.eclipse.osbp.xtext.cubedsl.CubeDimension
import org.eclipse.osbp.xtext.cubedsl.CubeDimensionEntityEntity
import org.eclipse.osbp.xtext.cubedsl.CubeDimensionUsage
import org.eclipse.osbp.xtext.cubedsl.CubeHierarchy
import org.eclipse.osbp.xtext.cubedsl.CubeLevel
import org.eclipse.osbp.xtext.cubedsl.CubeMeasure
import org.eclipse.osbp.xtext.cubedsl.CubeType
import org.eclipse.osbp.xtext.datamartdsl.DatamartCube
import org.eclipse.osbp.xtext.datamartdsl.DatamartDSLPackage
import org.eclipse.osbp.xtext.datamartdsl.DatamartDefineDerivedMeasure
import org.eclipse.osbp.xtext.datamartdsl.DatamartDefinition
import org.eclipse.osbp.xtext.datamartdsl.DatamartEntity
import org.eclipse.osbp.xtext.datamartdsl.DatamartHierarchy
import org.eclipse.osbp.xtext.datamartdsl.DatamartMember
import org.eclipse.osbp.xtext.datamartdsl.DatamartNavigation
import org.eclipse.osbp.xtext.datamartdsl.DatamartOwner
import org.eclipse.osbp.xtext.datamartdsl.DatamartReference
import org.eclipse.osbp.xtext.datamartdsl.util.DatamartScopes
import org.eclipse.xtext.resource.CompilerPhases
import org.eclipse.xtext.scoping.IScope
import org.eclipse.xtext.scoping.Scopes
import org.eclipse.xtext.scoping.impl.FilteringScope
import org.slf4j.LoggerFactory
/**
* This class contains custom scoping description.
*
* see : http://www.eclipse.org/Xtext/documentation/latest/xtext.html#scoping
* on how and when to use it
*
*/
@SuppressWarnings("restriction")
public class DatamartDSLScopeProvider extends AbstractDatamartDSLScopeProvider {
@Inject extension ModelExtensions
@Override
override IScope getScope(EObject context, EReference reference) {
if (reference == DatamartDSLPackage.Literals.DATAMART_HIERARCHY__HIERARCHY_REF) {
return getDatamartHierarchy_hierarchyRef(context)
} else if (reference == DatamartDSLPackage.Literals.DATAMART_MEASURE__MEASURE_REF) {
return getDatamartMeasure_measureRef(context)
} else if (reference == DatamartDSLPackage.Literals.DATAMART_DERIVED_MEASURE__DERIVED_REF) {
return getDatamartDerivedMeasure_derivedRef(context)
} else if (reference == DatamartDSLPackage.Literals.DATAMART_HIERARCHY__LEVEL_REF) {
return getDatamartHierarchy_levelRef(context)
} else if (reference == DatamartDSLPackage.Literals.DATAMART_HIERARCHY__EXCEPT_REF) {
return getDatamartHierarchy_levelRef(context)
} else if (reference == DatamartDSLPackage.Literals.DATAMART_HIERARCHY__ORDER_REF) {
return getDatamartMeasure_measureRef(context)
} else if (reference == DatamartDSLPackage.Literals.DATAMART_ATTRIBUTE_BASE__ATTRIBUTE_REF) {
return getDatamartAttribute_attributeRef(context)
} else if (reference == DatamartDSLPackage.Literals.DATAMART_ORDER__ORDER_BY) {
return getDatamartAttribute_attributeRef(context)
} else if (reference == DatamartDSLPackage.Literals.ATTRIBUTE_FILLER_DATA__ATTRIBUTE_REF) {
return getFillerData_attributeRef(context)
} else if (reference == DatamartDSLPackage.Literals.DATAMART_REFERENCE__REF) {
return getDatamartEntity_datamartReferenceRef(context, reference)
} else if (reference == DatamartDSLPackage.Literals.DATAMART_ENTITY__ENTITY_REF) {
return getDatamartEntity_datamartEntityRef(context, reference)
} else {
val scope = super.getScope(context, reference);
return scope
}
}
def IScope getDatamartEntity_filterEntityRef(EObject context, EReference reference) {
val scope = super.getScope(context, reference);
return new FilteringScope(scope, [
val entityProxy = it.EObjectOrProxy
var LEntity entity = null;
if (entityProxy.eIsProxy) {
// This is the resolving solution as XTend itself uses in
// org.eclipse.xtend.core.macro.declaration.XtendAnnotationReferenceImpl.getAnnotationType()
// This provokes no problems of loops or cycling linking.
val uri = (entityProxy as InternalEObject).eProxyURI
entity = context.eResource.resourceSet.getEObject(uri, true) as LEntity
} else {
entity = entityProxy as LEntity;
}
var inheritedClass = false
if (entity !== null) {
if (entity.superType !== null &&
(!entity.superType.isMappedSuperclass()/* || entity.superType.superType !== null */)) {
inheritedClass = true;
}
if (!inheritedClass && !entity.isMappedSuperclass) {
return true
}
}
return false
])
// return Scopes::scopeFor(result)
}
def IScope getFillerData_attributeRef(EObject context) {
var result = <EObject>newArrayList
var eObj = context.eContainer()
while (eObj !== null && !(eObj instanceof DatamartDefinition)) {
eObj = eObj.eContainer()
}
if (eObj !== null) {
if ((eObj as DatamartDefinition).source instanceof DatamartEntity) {
var entity = (eObj as DatamartDefinition).source as DatamartEntity
entity.iterateEntity(result)
}
}
return Scopes::scopeFor(result)
}
def void iterateEntity(DatamartEntity entity, ArrayList<EObject> result) {
for (prop : entity.attributes) {
result.add(prop.attributeRef)
}
for (navigation : entity.navigations) {
switch (navigation) {
DatamartMember: (navigation as DatamartMember).datamartEntity.iterateEntity(result)
DatamartOwner: (navigation as DatamartOwner).datamartEntity.iterateEntity(result)
}
}
}
/**
*
* @param context
* @return LEntity
*/
def LEntity getDatamartEntity(EObject context) {
var eObj = context.eContainer()
while (eObj !== null && !(eObj instanceof DatamartEntity)) {
eObj = eObj.eContainer()
}
if (eObj !== null) {
return (eObj as DatamartEntity).entityRef
} else {
return null
}
}
/**
*
* @param context
* @return
*/
def IScope getDatamartAttribute_attributeRef(EObject context) {
var result = <EObject>newArrayList
var entity = context.datamartEntity
if (entity !== null) {
for (LEntityAttribute attr : entity.allAttributes) {
if (attr.name !== null && !(attr.isId || attr.isUuid)) {
result.add(attr)
}
}
}
return Scopes::scopeFor(result)
}
def getDatamartEntity_datamartReferenceRef(EObject context, EReference reference) {
var result = <EObject>newArrayList
var eObj = context.eContainer
while (!(eObj instanceof DatamartEntity)) {
eObj = eObj.eContainer
if (eObj === null) {
return Scopes::scopeFor(result)
}
}
if (eObj instanceof DatamartEntity) {
for (ref : (eObj as DatamartEntity).entityRef.allReferences) {
if ((context as DatamartReference).eContainer instanceof DatamartOwner) {
if (!(ref as LEntityFeature).toMany) {
result.add(ref)
}
} else {
if ((ref as LEntityFeature).toMany) {
result.add(ref)
}
}
}
}
return Scopes::scopeFor(result)
}
def getDatamartEntity_datamartEntityRef(EObject context, EReference reference) {
var result = <EObject>newArrayList
var eObj = context.eContainer
while (!(eObj instanceof DatamartEntity)) {
eObj = eObj.eContainer
if (eObj === null) {
return super.getScope(context, reference);
}
}
if (eObj instanceof DatamartEntity) {
for (ref : (eObj as DatamartEntity).entityRef.allReferences) {
if ((context as DatamartEntity).eContainer instanceof DatamartOwner) {
if (!(ref as LEntityFeature).toMany) {
result.add(ref.type)
}
} else {
if ((ref as LEntityFeature).toMany) {
result.add(ref.type)
}
}
}
}
return Scopes::scopeFor(result)
}
def IScope getDatamartHierarchy_hierarchyRef(EObject context) {
var result = <EObject>newArrayList
var eObj = context.eContainer
while (!(eObj instanceof DatamartDefinition)) {
eObj = eObj.eContainer
}
if (eObj !== null) {
if (eObj instanceof DatamartDefinition) {
if ((eObj as DatamartDefinition).source !== null) {
var datamartSource = eObj.source
if (datamartSource instanceof DatamartCube) {
if ((datamartSource).cubeRef !== null) {
var cube = datamartSource.cubeRef
if (cube !== null) {
if (cube.getCubeTypeEntity() !== null) {
var hierList = new BasicEList<CubeHierarchy>();
for (CubeDimensionUsage ccCubeDimensionUsage : cube.getCubeTypeEntity().getDimensionUsages()) {
hierList.addAll(ccCubeDimensionUsage.sourceValue.hierarchies)
}
result = getHierarchyList(hierList, result);
}
}
}
}
}
}
}
return Scopes.scopeFor(result)
}
def IScope getDatamartHierarchy_levelRef(EObject eObj) {
var result = <EObject>newArrayList
var eObjLocal = eObj
while (!(eObjLocal instanceof DatamartHierarchy)) {
eObjLocal = eObjLocal.eContainer()
}
if (eObjLocal !== null) {
if (eObjLocal instanceof DatamartHierarchy) {
var hierarchy = eObjLocal.hierarchyRef
if (hierarchy !== null) {
if (hierarchy instanceof CubeHierarchy) {
var entity = hierarchy.cubeDimEntity
if (entity !== null) {
for (CubeLevel ccCubeLevel : entity.hierarchLevels) {
if (ccCubeLevel.name !== null) {
result.add(ccCubeLevel)
}
}
if (entity.dimEntity !== null) {
entity.dimEntity.recurseLevels(result)
}
}
}
}
}
}
return Scopes.scopeFor(result)
}
def void recurseLevels(CubeDimensionEntityEntity dimEntity, ArrayList<EObject> result) {
if (dimEntity !== null) {
for (CubeLevel ccCubeLevel : dimEntity.hierarchLevels) {
if (ccCubeLevel.name !== null) {
result.add(ccCubeLevel);
}
}
dimEntity.dimEntity.recurseLevels(result)
}
}
def IScope getDatamartMeasure_measureRef(EObject eObj) {
var result = <EObject>newArrayList
var eObjLocal = eObj.eContainer();
while (!(eObjLocal instanceof DatamartDefinition)) {
eObjLocal = eObjLocal.eContainer();
}
if (eObjLocal !== null) {
if (eObjLocal instanceof DatamartDefinition) {
if (eObjLocal.source !== null) {
var datamartSource = eObjLocal.source
if (datamartSource instanceof DatamartCube) {
if (datamartSource.cubeRef !== null) {
var cube = datamartSource.cubeRef
if (cube !== null) {
if (cube instanceof CubeType) {
if (cube.cubeTypeEntity !== null) {
for (CubeMeasure ccCubeMeasure : cube.cubeTypeEntity.measures) {
if (ccCubeMeasure.name !== null) {
result.add(ccCubeMeasure);
}
}
}
}
}
}
}
}
}
}
return Scopes.scopeFor(result)
}
def IScope getDatamartDerivedMeasure_derivedRef(EObject eObj) {
val result = <EObject>newArrayList
var eObjLocal = eObj.eContainer();
while (!(eObjLocal instanceof DatamartCube)) {
eObjLocal = eObjLocal?.eContainer();
}
if (eObjLocal !== null) {
val cube = eObjLocal as DatamartCube
cube.axisslicer.filter(typeof(DatamartDefineDerivedMeasure)).forEach [
result += it
]
}
return Scopes.scopeFor(result)
}
def ArrayList<EObject> getHierarchyList(EList<CubeHierarchy> hierList, ArrayList<EObject> result) {
for (CubeHierarchy ccCubeHierarchy : hierList) {
var name = ccCubeHierarchy.name
if ((name === null) || (name.length() == 0)) {
var eObj = ccCubeHierarchy.eContainer
while (!(eObj instanceof CubeDimension)) {
eObj = eObj.eContainer
}
name = (eObj as CubeDimension).name
ccCubeHierarchy.name = name
}
result.add(ccCubeHierarchy)
}
return result;
}
}