blob: bf5deb2d131ab2f23e29313bdaf1a52fb01aff24 [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 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.common.types.LPackage
import org.eclipse.osbp.dsl.semantic.entity.LBean
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
}
/**
* 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 LEntity!
* <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);
}
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
}
}