Skip to main content
summaryrefslogtreecommitdiffstats
blob: 80b54445a0a34342e56a3dc96463ff1dedc9a287 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
 *                                                                            
 * 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.toLowerCase.equals(IPropertyConstants.PROPERTY_BLOB)]){
					return EType.BLOPMAPPING
				}
				// data type is defined as rich text area
				if (getJvmType(attribute).string && lDataType.properties.exists[it.value.toLowerCase.equals(IPropertyConstants.PROPERTY_RICH_TEXT)]){
					return EType.RICHTEXTAREA
				}
				// data type is defined as checkbox
				if (getJvmType(attribute).boolean && lDataType.properties.exists[it.value.toLowerCase.equals(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.toLowerCase.equals(IPropertyConstants.PROPERTY_BLOB)]){
					return EType.BLOPMAPPING
				}
				// Otherwise entity attribute is defined as rich text area
				if (getJvmType(attribute).string && attribute.properties.exists[it.value.toLowerCase.equals(IPropertyConstants.PROPERTY_RICH_TEXT)]){
					return EType.RICHTEXTAREA
				}
				// data type is defined as rich text area
				if (getJvmType(attribute).boolean && lDataType.properties.exists[it.value.toLowerCase.equals(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
	}
}

Back to the top