Skip to main content
summaryrefslogtreecommitdiffstats
blob: 583580ed7099a1fa011578c295e31a8f2654057a (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
/**
 *                                                                            
 *  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.common

import org.eclipse.xtext.common.types.JvmType
import org.eclipse.osbp.dsl.semantic.dto.LDto
import org.eclipse.osbp.dsl.semantic.dto.OSBPDtoPackage
import org.eclipse.osbp.dsl.semantic.entity.LEntity
import org.eclipse.osbp.dsl.semantic.common.types.LEnum
import org.eclipse.osbp.dsl.semantic.common.types.LTypedPackage
import org.eclipse.osbp.dsl.semantic.dto.util.NamingConventionsUtil
import org.eclipse.osbp.dsl.semantic.entity.OSBPEntityPackage
import org.eclipse.emf.ecore.EObject
import org.eclipse.osbp.dsl.semantic.entity.LBean
import org.eclipse.emf.ecore.resource.ResourceSet

class EntityUtils {

	def static String getQualifiedDtoNameForEntity(JvmType type) {
		try {
			var qualifiedEntityName = type.qualifiedName
			var resourceSet = type.eResource.resourceSet
			var searchName = qualifiedEntityName.getQualifiedDtoNameForQualifiedEntityName;
			val objectDescs = ServiceListener.getEObjectDescriptions(OSBPDtoPackage.Literals.LDTO, searchName)
			if (objectDescs.iterator.hasNext) {
				val eobjectDesc = objectDescs.iterator.next
				if (resourceSet !== null) {

					//var dto = resourceSet.getEObject(eobjectDesc.EObjectURI, true) as LDto
					return eobjectDesc.name.toString
				}
			}
		} catch (Exception e) {
			e.printStackTrace
		}
		return null
	}

	def static LDto getDtoForEntity(JvmType type) {
		return getDtoForEntity(type.qualifiedName, type.eResource.resourceSet)
	}
	
	def static LDto getDtoForEntity(String qualifiedEntityName, ResourceSet resourceSet) {
		try {
			var searchName = qualifiedEntityName.getQualifiedDtoNameForQualifiedEntityName;
			val objectDescs = ServiceListener.getEObjectDescriptions(OSBPDtoPackage.Literals.LDTO, searchName)
			if (objectDescs.iterator.hasNext) {
				val eobjectDesc = objectDescs.iterator.next
				if (resourceSet !== null) {
					return resourceSet.getEObject(eobjectDesc.EObjectURI, true) as LDto
				}
			}
		} catch (Exception e) {
			e.printStackTrace
		}
		return null
	}

	/**
     * get the full qualified dto name for a LEnum
     * <br>
     * <b>The DTO for an LEnum <u>never</u> ends with the postfix <code>Dto</code>!</b> 
     */
	def static String getDtoFQNForLEnum(LEnum lenum) {
		var container = lenum as EObject
		while	((container !== null) && !(container instanceof LTypedPackage)) {
			container = container.eContainer
		}
		return applyDtoPackageNameFromEntityPackageNameForEnum(
			'''«(container as LTypedPackage).name».«lenum.name»''')
	}

	/**
     * get the full qualified dto name for a LBean
     * <br>
     * <b>The DTO for an LBean <u>always</u> ends with the postfix <code>Dto</code>!</b> 
     */
	def static String getDtoFQNForLBean(LBean bean) {
		return getQualifiedDtoNameForQualifiedEntityName(bean.FQNForLBean)
	}

	/**
     * get the full qualified dto name for a LEntity
     * <br>
     * <b>The DTO for an LEntity <u>always</u> ends with the postfix <code>Dto</code>!</b> 
     */
	def static String getDtoFQNForLEntity(LEntity entity) {
		return getQualifiedDtoNameForQualifiedEntityName(entity.FQNForLEntity)
	}

	/**
     * get the full qualified name for a LEntity
     */
	def static String getFQNForLEntity(LEntity entity) {
		return '''«(entity.eContainer as LTypedPackage).name».«entity.name»'''
	}

	/**
     * get the full qualified name for a LBean
     */
	def static String getFQNForLBean(LBean bean) {
		return '''«(bean.eContainer as LTypedPackage).name».«bean.name»'''
	}

	/**
     * get the full qualified dto name for a full qualified entity name!
     * <br>
     * <b>The DTO for an LEntity <u>always</u> ends with the postfix <code>Dto</code>!</b> 
     */
	public def static String getQualifiedDtoNameForQualifiedEntityName(String qualifiedEntityName) {
		return NamingConventionsUtil.toDtoQualifiedName(qualifiedEntityName);
	}

	/**
     * get the full qualified dto name for a full qualified entity name!
     * <br>
     * <b>The DTO for an LEntity <u>always</u> ends with the postfix <code>Dto</code>!</b> 
     */
	public def static String getQualifiedEntityNameForQualifiedDtoName(String qualifiedDtoName) {
		return NamingConventionsUtil.toFqnEntityName(qualifiedDtoName);
	}

	private final static String DTOS_PACKAGE_TOKEN = ".dtos."

	/**
	 * <b><u>Dirty Hack</u></b>: Apply the package name modification from LEnum to LDto
     * <br>
     * <b>The LEnum <u>may or may not</u> reside in a package <code>*.entities.*</code>!</b> 
     * <br>
     * <b>The LDto <u>always</u> resides in a package <code>*.dtos.*</code>!</b> 
	 */
	protected def static String applyDtoPackageNameFromEntityPackageNameForEnum(String fullQualifiedEnumName) {

		// simply replace .entities. with .dtos.
		var fullQualifiedDtoName = NamingConventionsUtil.toDtoQualifiedNameForEnum(fullQualifiedEnumName)

		// BUT if there is no .dtos.
		if (!fullQualifiedDtoName.contains(DTOS_PACKAGE_TOKEN)) {

			// just add .dtos. as last subpackage
			var lastDot = fullQualifiedDtoName.lastIndexOf(".")
			fullQualifiedDtoName = fullQualifiedDtoName.substring(0, lastDot) + DTOS_PACKAGE_TOKEN +
				fullQualifiedDtoName.substring(lastDot + 1)
		}
		return fullQualifiedDtoName
	}

	def static LEntity getEntityFromDto(JvmType type) {
		var LEntity entity = null
		try {
			var qualifiedDtoName = type?.qualifiedName
			var resourceSet = type?.eResource?.resourceSet
			val objectDescs = ServiceListener.getEObjectDescriptions(OSBPDtoPackage.Literals.LDTO, qualifiedDtoName)
			if (objectDescs?.iterator.hasNext) {
				val eobjectDesc = objectDescs.iterator.next
				if (resourceSet !== null) {
					var dto = resourceSet.getEObject(eobjectDesc.EObjectURI, true) as LDto
					if (dto !== null && dto.wrappedType !== null) {
						if (dto.wrappedType.eIsProxy) {
							val entityDescs = ServiceListener.getEObjectDescriptions(OSBPEntityPackage.Literals.LENTITY,
								qualifiedDtoName.replace(".dtos.", ".entities.").replace("Dto", ""))
							if (entityDescs.iterator.hasNext) {
								val entityObjectDesc = entityDescs.iterator.next
								if (resourceSet !== null) {
									entity = resourceSet.getEObject(entityObjectDesc.EObjectURI, true) as LEntity
								}
							}
						} else {
							entity = dto.wrappedType as LEntity
						}

					}
				}
			}
		} catch (Exception e) {
			// NOP
		}
		return entity
	}

	def static LEntity getEntityFromDto(LDto dto) {
		var LEntity entity = null
		if (dto !== null && dto.wrappedType !== null) {
			if (dto.wrappedType.eIsProxy) {
				throw new IllegalStateException
			} else {
				entity = dto.wrappedType as LEntity
			}

		}
		return entity
	}

	def static LDto getDto(JvmType type) {
		try {
			var qualifiedDtoName = type.qualifiedName
			var resourceSet = type.eResource.resourceSet
			val objectDescs = ServiceListener.getEObjectDescriptions(OSBPDtoPackage.Literals.LDTO, qualifiedDtoName)
			if (objectDescs.iterator.hasNext) {
				val eobjectDesc = objectDescs.iterator.next
				if (resourceSet !== null) {
					return resourceSet.getEObject(eobjectDesc.EObjectURI, true) as LDto
				}
			}
		} catch (Exception e) {
			// NOP
		}
		return null
	}

}

Back to the top