| /* |
| * |
| * 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.ui.contentassist |
| |
| import com.google.inject.Inject |
| import org.eclipse.emf.ecore.EObject |
| import org.eclipse.jface.viewers.StyledString |
| import org.eclipse.osbp.dsl.semantic.common.types.LDataType |
| import org.eclipse.osbp.xtext.basic.ui.contentassist.BasicDSLProposalProviderHelper |
| import org.eclipse.osbp.xtext.datamartdsl.ConditionalExpression |
| import org.eclipse.osbp.xtext.datamartdsl.DatamartAttribute |
| import org.eclipse.osbp.xtext.datamartdsl.DatamartCondition |
| import org.eclipse.osbp.xtext.datamartdsl.DatamartCube |
| import org.eclipse.osbp.xtext.datamartdsl.DatamartCubeAxis |
| import org.eclipse.osbp.xtext.datamartdsl.DatamartCubeElement |
| import org.eclipse.osbp.xtext.datamartdsl.DatamartDefineDerivedMeasure |
| import org.eclipse.osbp.xtext.datamartdsl.DatamartDerivedMeasure |
| import org.eclipse.osbp.xtext.datamartdsl.DatamartEntity |
| import org.eclipse.osbp.xtext.datamartdsl.DatamartHierarchy |
| import org.eclipse.osbp.xtext.datamartdsl.DatamartMeasure |
| import org.eclipse.osbp.xtext.datamartdsl.DatamartOrder |
| import org.eclipse.osbp.xtext.datamartdsl.DatamartSlicer |
| import org.eclipse.osbp.xtext.datamartdsl.DatamartTask |
| import org.eclipse.osbp.xtext.datamartdsl.OrderEnum |
| import org.eclipse.osbp.xtext.datamartdsl.ui.DatamartDSLDocumentationTranslator |
| import org.eclipse.osbp.xtext.datamartdsl.util.AxisEnumUtil |
| import org.eclipse.osbp.xtext.datamartdsl.util.SetAggregationEnumUtil |
| import org.eclipse.xtext.Keyword |
| import org.eclipse.xtext.RuleCall |
| import org.eclipse.xtext.common.ui.contentassist.TerminalsProposalProvider |
| import org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext |
| import org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor |
| import org.eclipse.osbp.xtext.datamartdsl.DatamartAxis |
| import org.eclipse.osbp.xtext.datamartdsl.Expression |
| import org.eclipse.osbp.xtext.datamartdsl.DatamartAttributeBase |
| |
| /** |
| * see http://www.eclipse.org/Xtext/documentation.html#contentAssist on how to customize content assistant |
| */ |
| class DatamartDSLProposalProvider extends AbstractDatamartDSLProposalProvider { |
| |
| @Inject TerminalsProposalProvider provider |
| @Inject BasicDSLProposalProviderHelper providerHelper |
| |
| override protected StyledString getKeywordDisplayString(Keyword keyword) { |
| return BasicDSLProposalProviderHelper.getKeywordDisplayString(keyword, |
| DatamartDSLDocumentationTranslator.instance()) |
| } |
| |
| /** |
| * This override will enable 1 length non letter characters as keyword. |
| */ |
| override protected boolean isKeywordWorthyToPropose(Keyword keyword) { |
| return true; |
| } |
| |
| override protected boolean isValidProposal(String proposal, String prefix, ContentAssistContext context) { |
| var result = super.isValidProposal(proposal, prefix, context); |
| if (context.currentModel instanceof DatamartAxis) { |
| return isAxisValidProposal(context.currentModel as DatamartAxis, proposal, result); |
| } else if (context.currentModel instanceof DatamartHierarchy) { |
| return isHierarchyValidProposal(context.currentModel as DatamartHierarchy, proposal, result); |
| } else if (context.currentModel instanceof DatamartAttribute) { |
| return isAttributeValidProposal(context.currentModel as DatamartAttribute, proposal, result); |
| } else if (context.currentModel instanceof DatamartCondition) { |
| return isConditionValidProposal(context.currentModel as DatamartCondition, proposal, result); |
| } else if (context.currentModel instanceof ConditionalExpression) { |
| return isConditionalExpressionValidProposal(context.currentModel as ConditionalExpression, proposal, |
| result); |
| } else if (context.currentModel instanceof DatamartOrder) { |
| return isOrderValidProposal(context.currentModel as DatamartOrder, proposal, result); |
| } else if (context.currentModel instanceof Expression) { |
| return isExpressionValidProposal(context.currentModel as Expression, proposal, result); |
| } |
| return result; |
| } |
| |
| def isExpressionValidProposal(Expression expression, String proposal, boolean result) { |
| if(expression.eContainer instanceof ConditionalExpression) { |
| if((expression.eContainer as ConditionalExpression).left instanceof DatamartAttributeBase) { |
| var currentAttribute = (expression.eContainer as ConditionalExpression).left as DatamartAttributeBase |
| var type = currentAttribute.attributeRef.type as LDataType |
| // do not allow optional for non-string attributes due to union expression with * |
| if("optional".equals(proposal) && !"String".equals(type.name)) { |
| return false |
| } |
| } |
| } |
| return true |
| } |
| |
| def boolean isOrderValidProposal(DatamartOrder currentOrder, String proposal, boolean result) { |
| if (proposal.equals("columnWeight")) { |
| return true; |
| } |
| for (e : OrderEnum.values.toList) { |
| if (e.literal.equals(proposal)) { |
| return true |
| } |
| } |
| var eObj = currentOrder.eContainer() |
| if (eObj instanceof DatamartEntity) { |
| for (attribute : (eObj as DatamartEntity).entityRef.allAttributes) { |
| if (attribute.name.equals(proposal) && !attribute.isId && !attribute.isUuid) { |
| return true |
| } |
| } |
| } |
| return false |
| } |
| |
| def boolean isConditionalExpressionValidProposal(ConditionalExpression currentConditionalExpression, String proposal, |
| boolean result) { |
| if (proposal.equals("column") || proposal.equals("filter") || proposal.equals("attribute")) { |
| return false |
| } |
| if (!currentConditionalExpression.getOperator().getLiteral().equals("=") && |
| (proposal.equals("filtered") || proposal.equals("selected") || proposal.equals("ranged"))) { |
| return false |
| } |
| return true |
| } |
| |
| def boolean isConditionValidProposal(DatamartCondition currentCondition, String proposal, boolean result) { |
| var eObj = currentCondition.eContainer() |
| switch (eObj) { |
| DatamartEntity: |
| if (proposal.equals("column") || proposal.equals("filter")) { |
| return false; |
| } |
| DatamartTask: |
| if (proposal.equals("attribute")) { |
| return false; |
| } |
| DatamartCube: |
| if (proposal.equals("column") || proposal.equals("filter") || proposal.equals("attribute")) { |
| return false; |
| } |
| } |
| if (proposal.equals("filtered") || proposal.equals("optional") || proposal.equals("selected")) { |
| return false |
| } |
| return true |
| } |
| |
| def boolean isAttributeValidProposal(DatamartAttribute currentAttribute, String proposal, boolean result) { |
| for (idx : AxisEnumUtil.VALUES) { |
| |
| // for now, only 2 axis are possible for sql entities |
| if (proposal.equals(idx.getLiteral()) && (idx.value > 1 || idx.value < 0)) { |
| return false |
| } |
| } |
| if (proposal.equals("aggregate")) { |
| var typeTemp = currentAttribute.getAttributeRef().getType() |
| if (typeTemp != null && (typeTemp instanceof LDataType)) { |
| var type = typeTemp as LDataType |
| if (type.jvmTypeReference == null) { |
| return false |
| } |
| if (!type.jvmTypeReference.getSimpleName().equalsIgnoreCase("float") && |
| !type.jvmTypeReference.getSimpleName().equalsIgnoreCase("double") && |
| !type.jvmTypeReference.getSimpleName().equalsIgnoreCase("integer")) { |
| return false |
| } |
| } else { |
| return false |
| } |
| } |
| return true |
| } |
| |
| def boolean isHierarchyValidProposal(DatamartHierarchy currentHierarchy, String proposal, boolean result) { |
| var DatamartCubeAxis axis = null |
| var DatamartSlicer slicer = null |
| var eObj = currentHierarchy.eContainer() |
| while (!(eObj instanceof DatamartCubeElement)) { |
| eObj = eObj.eContainer() |
| } |
| if (eObj != null) { |
| if (eObj instanceof DatamartCubeAxis) { |
| axis = eObj as DatamartCubeAxis |
| if (axis != null && axis.getElements() != null) { |
| for (element : axis.getElements()) { |
| if (element instanceof DatamartHierarchy) { |
| if ((element as DatamartHierarchy).getHierarchyRef() != null && |
| (element as DatamartHierarchy).getHierarchyRef() != currentHierarchy) { |
| if (proposal.equals((element as DatamartHierarchy).getHierarchyRef().getName())) { |
| return true |
| } |
| } |
| if (proposal.equals("detailed")) { |
| return true |
| } |
| if (proposal.equals("condensed") || proposal.equals("default") || |
| proposal.equals("exploded")) { |
| if ((element as DatamartHierarchy).getLevelRef() != null) |
| return false |
| } |
| if (proposal.equals("selected")) { // multiselect useless for hierarchies, useful only for slicers |
| return false |
| } |
| return true |
| } |
| if (element instanceof DatamartMeasure) { |
| if ((element as DatamartMeasure).getMeasureRef() != null) { |
| if (proposal.equals((element as DatamartMeasure).getMeasureRef().getName())) { |
| return false |
| } |
| } |
| } |
| return true |
| } |
| } |
| } |
| if (eObj instanceof DatamartSlicer) { |
| slicer = eObj as DatamartSlicer |
| if (slicer != null && slicer.getElement() != null) { |
| var element = slicer.getElement() |
| if (element instanceof DatamartHierarchy) { |
| if ((element as DatamartHierarchy).getHierarchyRef() != null && |
| (element as DatamartHierarchy).getHierarchyRef() != currentHierarchy) { |
| if (proposal.equals((element as DatamartHierarchy).getHierarchyRef().getName())) { |
| return true |
| } |
| } |
| return true |
| } |
| if (element instanceof DatamartMeasure) { |
| if ((element as DatamartMeasure).getMeasureRef() != null) { |
| if (proposal.equals((element as DatamartMeasure).getMeasureRef().getName())) { |
| return false |
| } |
| } |
| } |
| return true |
| } |
| } |
| if (eObj instanceof DatamartDefineDerivedMeasure) { |
| return true |
| } |
| } |
| return false |
| } |
| |
| def boolean isAxisValidProposal(DatamartAxis currentAxis, String proposal, boolean result) { |
| if ("{".equals(proposal)) |
| return true |
| if ("}".equals(proposal)) |
| return true |
| var eObj = currentAxis.eContainer.eContainer |
| var DatamartCubeAxis axisWithMeasure = null |
| if (eObj != null) { |
| if(eObj instanceof DatamartCube) { |
| // find out the axis carrying a measure |
| var idx = 0 |
| for (DatamartCubeElement axisslicer : (eObj as DatamartCube).getAxisslicer()) { |
| if (axisslicer instanceof DatamartCubeAxis) { |
| for (element : (axisslicer as DatamartCubeAxis).getElements()) { |
| if (element instanceof DatamartMeasure || element instanceof DatamartDerivedMeasure) { |
| axisWithMeasure = axisslicer as DatamartCubeAxis |
| } |
| } |
| idx = idx + 1 |
| } |
| } |
| |
| // find out the axis number we are in |
| idx = 0 |
| var axisOrdinal = 0 |
| for (axisslicer : (eObj as DatamartCube).getAxisslicer()) { |
| if (axisslicer instanceof DatamartCubeAxis) { |
| if (axisslicer == currentAxis.eContainer) { |
| axisOrdinal = idx |
| } |
| idx = idx + 1 |
| } |
| } |
| var orderedProposal = AxisEnumUtil.get(axisOrdinal).getLiteral() |
| if (proposal.equals(orderedProposal)) { |
| return true |
| } |
| } |
| if(eObj instanceof DatamartEntity) { |
| return true |
| } |
| } |
| |
| for (setAggregation : SetAggregationEnumUtil.VALUES) { |
| if (setAggregation.getLiteral().equals(proposal)) { |
| return true |
| } |
| } |
| if (proposal.equals("hierarchy")) { |
| return true |
| } |
| |
| if ((proposal.equals("measure") || proposal.equals("derived")) && |
| (axisWithMeasure == null || axisWithMeasure == currentAxis)) { |
| return true |
| } |
| return false |
| } |
| |
| override public void complete_QualifiedName(EObject model, RuleCall ruleCall, ContentAssistContext context, |
| ICompletionProposalAcceptor acceptor) { |
| providerHelper.complete_PackageName(model, ruleCall, context, acceptor, this) |
| } |
| |
| // ------------------------ delegates to TerminalsProposalProvider ----------------- |
| override public void complete_TRANSLATABLESTRING(EObject model, RuleCall ruleCall, ContentAssistContext context, |
| ICompletionProposalAcceptor acceptor) { |
| provider.complete_STRING(model, ruleCall, context, acceptor) |
| } |
| |
| override public void complete_TRANSLATABLEID(EObject model, RuleCall ruleCall, ContentAssistContext context, |
| ICompletionProposalAcceptor acceptor) { |
| provider.complete_ID(model, ruleCall, context, acceptor) |
| } |
| |
| override public void complete_ID(EObject model, RuleCall ruleCall, ContentAssistContext context, |
| ICompletionProposalAcceptor acceptor) { |
| provider.complete_ID(model, ruleCall, context, acceptor) |
| } |
| |
| override public void complete_INT(EObject model, RuleCall ruleCall, ContentAssistContext context, |
| ICompletionProposalAcceptor acceptor) { |
| provider.complete_INT(model, ruleCall, context, acceptor) |
| } |
| |
| } |