blob: 84c0be5226aaf4813cf9d4cda3cf5bf9a8595dcd [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.utils.entityhelper
import com.google.inject.Inject
import java.util.Date
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.osbp.dsl.entity.xtext.extensions.ModelExtensions
import org.eclipse.osbp.dsl.semantic.common.types.LDataType
import org.eclipse.osbp.dsl.semantic.common.types.LEnum
import org.eclipse.osbp.dsl.semantic.common.types.LPackage
import org.eclipse.osbp.dsl.semantic.entity.LEntityAttribute
import org.eclipse.osbp.ecview.dsl.extensions.TypeHelper
import org.eclipse.osbp.ui.api.datamart.IDataMart.EType
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.dsl.semantic.common.types.LAttribute
import org.eclipse.xtext.common.types.JvmTypeReference
import org.eclipse.osbp.dsl.semantic.common.types.LDateType
import org.eclipse.osbp.runtime.common.layouting.IPropertyConstants
public class DataType {
@Inject
@Extension
TypeHelper typeHelper
@Inject ModelExtensions entityExtensions
@Inject
IJvmTypeProvider.Factory typeProviderFactory
def public LDataType getLDataType(LAttribute attributeRef) {
var type = attributeRef.type
var lDataType = null as LDataType
if (type instanceof LDataType) {
lDataType = type as LDataType
}
return lDataType
}
def public JvmType getJvmType(LAttribute 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;
}
}
} else if (type instanceof LEnum) {
var LPackage pkg = type.eContainer as LPackage
val typeProvider = getTypeProvider(attributeRef.eResource.resourceSet)
return typeProvider.findTypeByName(pkg.name + "." + type.name)
}
var JvmTypeReference typeJvm = null
if (attributeRef instanceof LEntityAttribute) {
typeJvm = entityExtensions.toTypeReference(attributeRef)
} else {
typeJvm = entityExtensions.toTypeReference(attributeRef)
}
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(LAttribute attribute) {
var lDataType = getLDataType(attribute)
if (lDataType !== null) {
if (lDataType.date) {
if (lDataType.dateType.equals(LDateType.TIMESTAMP)){
return EType.TIMESTAMP
} else if (lDataType.dateType.equals(LDateType.DATE)){
return EType.DATE
} else {
return EType.DATE
}
}
if (!lDataType.properties.isNullOrEmpty) {
// data type is defined as Blob
if (getJvmType(attribute).string && lDataType.properties.exists[it.key.equalsIgnoreCase(IPropertyConstants.PROPERTY_BLOB)]){
return EType.BLOPMAPPING
}
// data type is defined as rich text area
if ((getJvmType(attribute).string || lDataType.asBlob) && lDataType.properties.exists[it.value.equalsIgnoreCase(IPropertyConstants.PROPERTY_RICH_TEXT)]){
return EType.RICHTEXTAREA
}
// data type is defined as checkbox
if (getJvmType(attribute).boolean && lDataType.properties.exists[it.value.equalsIgnoreCase(IPropertyConstants.PROPERTY_CHECKBOX)]){
return EType.BOOLEAN_CHECKBOX
}
}
if (!attribute.properties.isNullOrEmpty) {
// Otherwise entity attribute is defined as Blob
if (getJvmType(attribute).string && attribute.properties.exists[it.key.equalsIgnoreCase(IPropertyConstants.PROPERTY_BLOB)]){
return EType.BLOPMAPPING
}
// Otherwise entity attribute is defined as rich text area
if ((getJvmType(attribute).string || lDataType.asBlob) && attribute.properties.exists[it.value.equalsIgnoreCase(IPropertyConstants.PROPERTY_RICH_TEXT)]){
return EType.RICHTEXTAREA
}
// data type is defined as rich text area
if (getJvmType(attribute).boolean && lDataType.properties.exists[it.value.equalsIgnoreCase(IPropertyConstants.PROPERTY_CHECKBOX)]){
return EType.BOOLEAN_CHECKBOX
}
}
if ("blobMapping".equalsIgnoreCase(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 TIMESTAMP: return (sourceType == EType.TIMESTAMP)
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
}
}