| /** |
| * |
| * 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.utils.entityhelper |
| |
| import com.google.inject.Inject |
| import java.util.Date |
| import org.eclipse.emf.ecore.resource.ResourceSet |
| import org.eclipse.osbp.dsl.semantic.common.types.LDataType |
| import org.eclipse.osbp.dsl.semantic.entity.LEntityAttribute |
| import org.eclipse.osbp.ecview.dsl.extensions.TypeHelper |
| import org.eclipse.xtext.common.types.JvmEnumerationType |
| import org.eclipse.xtext.common.types.JvmType |
| import org.eclipse.xtext.common.types.access.IJvmTypeProvider |
| import org.eclipse.osbp.ui.api.datamart.IDataMart.EType |
| |
| public class DataType { |
| |
| @Inject |
| @Extension |
| private TypeHelper typeHelper; |
| |
| @Inject |
| IJvmTypeProvider.Factory typeProviderFactory |
| |
| def public LDataType getLDataType(LEntityAttribute attributeRef) { |
| var type = attributeRef.type |
| var lDataType = null as LDataType |
| if (type instanceof LDataType) { |
| lDataType = type as LDataType |
| } |
| return lDataType |
| } |
| |
| def public JvmType getJvmType(LEntityAttribute attributeRef) { |
| var type = attributeRef.type |
| var lDataType = null as LDataType |
| var ltype = null as JvmType |
| if (type instanceof LDataType) { |
| lDataType = type as LDataType |
| if (lDataType.isDate) { |
| val typeProvider = getTypeProvider(attributeRef.eResource.resourceSet) |
| return typeProvider.findTypeByName(Date.name) |
| } else if (lDataType.isAsBlob) { |
| val typeProvider = getTypeProvider(attributeRef.eResource.resourceSet) |
| return typeProvider.findTypeByName("byte[]") |
| } else { |
| var jvmTypeReference = lDataType.jvmTypeReference |
| ltype = jvmTypeReference.type |
| if (ltype != null) { |
| return ltype; |
| } |
| } |
| } |
| var typeJvm = attributeRef.typeJvm |
| var typeJvmType = typeJvm.type |
| return typeJvmType |
| } |
| |
| def getTypeProvider(ResourceSet rs) { |
| typeProviderFactory.findOrCreateTypeProvider(rs) |
| } |
| |
| def public EType getBasicType(String typeIdentifier) { |
| if (typeIdentifier != null){ |
| var identifier = typeIdentifier |
| if (typeIdentifier.startsWith("java.lang.")) { |
| identifier = typeIdentifier.substring("java.lang.".length).toLowerCase |
| } |
| switch (identifier) { |
| /** |
| * Date, String, int, boolean, Time |
| */ |
| case "boolean": return EType.BOOLEAN |
| case "byte": return EType.BYTE |
| case "short": return EType.SHORT |
| case "int": return EType.INTEGER |
| case "integer": return EType.INTEGER |
| case "long": return EType.LONG |
| case "double": return EType.DOUBLE |
| case "float": return EType.FLOAT |
| case "BigDecimal": return EType.DOUBLE |
| case "java.math.BigDecimal": return EType.DOUBLE |
| case "string": return EType.STRING |
| case "java.util.Date": return EType.DATE |
| } |
| } |
| return EType.none |
| } |
| |
| |
| def public EType getBasicType(JvmType type) { |
| if (type.boolean) { |
| return EType.BOOLEAN |
| } |
| else if (type.numberWithDigits) { |
| val retcode = getBasicType(type.identifier) |
| if (retcode != EType.none) { |
| return retcode |
| } |
| return EType.DOUBLE |
| } |
| else if (type.numberWithoutDigits) { |
| val retcode = getBasicType(type.identifier) |
| if (retcode != EType.none) { |
| return retcode |
| } |
| return EType.INTEGER |
| } |
| else if (type.string) { |
| return EType.STRING |
| } |
| else if (type.date) { |
| return EType.DATE |
| } |
| else if (type instanceof JvmEnumerationType) { |
| return EType.LENUM; |
| } |
| return getBasicType(type.identifier) |
| } |
| |
| def public EType getBasicType(LEntityAttribute attribute) { |
| var lDataType = getLDataType(attribute) |
| if (lDataType != null) { |
| if (lDataType.date) { |
| return EType.DATE |
| } |
| if ("blobMapping".equals(lDataType.name)) { |
| return EType.BLOPMAPPING |
| } |
| } |
| return getBasicType(getJvmType(attribute)) |
| } |
| |
| def public EType getBasicType(Class<?> typeClass) { |
| return getBasicType(typeClass.canonicalName) |
| } |
| |
| def public boolean canBeCastFrom(LEntityAttribute targetAttribute, Class<?> sourceClass) { |
| return canBeCastFrom(targetAttribute, getBasicType(sourceClass)) |
| } |
| |
| def public boolean canBeCastFrom(LEntityAttribute targetAttribute, EType sourceType) { |
| var targetType = getBasicType(targetAttribute) |
| |
| switch (targetType) { |
| case BOOLEAN: return (sourceType == EType.BOOLEAN) |
| case BYTE: return (sourceType == EType.BYTE) |
| case DATE: return (sourceType == EType.DATE) |
| case FLOAT: return (sourceType == EType.FLOAT || sourceType == EType.LONG || sourceType == EType.INTEGER || sourceType == EType.SHORT || sourceType == EType.BOOLEAN) |
| case DOUBLE: return (sourceType == EType.FLOAT || sourceType == EType.DOUBLE || sourceType == EType.LONG || sourceType == EType.INTEGER || sourceType == EType.SHORT || sourceType == EType.BOOLEAN) |
| case SHORT: return (sourceType == EType.SHORT || sourceType == EType.BOOLEAN) |
| case INTEGER: return (sourceType == EType.INTEGER || sourceType == EType.SHORT || sourceType == EType.BOOLEAN) |
| case LONG: return (sourceType == EType.LONG || sourceType == EType.INTEGER || sourceType == EType.SHORT || sourceType == EType.BOOLEAN) |
| case STRING: return true |
| case TIME: return (sourceType == EType.TIME) |
| case LENUM: return (sourceType == EType.INTEGER || sourceType == EType.BOOLEAN) |
| case none: return false |
| } |
| return false |
| } |
| } |