blob: 35a670c4f6457a19669a01a7c1e7ea99c590b150 [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 java.util.Iterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.osbp.dsl.semantic.common.types.LEnum;
import org.eclipse.osbp.dsl.semantic.common.types.LType;
import org.eclipse.osbp.dsl.semantic.common.types.LTypedPackage;
import org.eclipse.osbp.dsl.semantic.dto.LDto;
import org.eclipse.osbp.dsl.semantic.dto.OSBPDtoPackage;
import org.eclipse.osbp.dsl.semantic.dto.util.NamingConventionsUtil;
import org.eclipse.osbp.dsl.semantic.entity.LBean;
import org.eclipse.osbp.dsl.semantic.entity.LEntity;
import org.eclipse.osbp.dsl.semantic.entity.OSBPEntityPackage;
import org.eclipse.osbp.utils.common.ServiceListener;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.common.types.JvmType;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.xbase.lib.Exceptions;
@SuppressWarnings("all")
public class EntityUtils {
public static String getQualifiedDtoNameForEntity(final JvmType type) {
try {
String qualifiedEntityName = type.getQualifiedName();
ResourceSet resourceSet = type.eResource().getResourceSet();
String searchName = EntityUtils.getQualifiedDtoNameForQualifiedEntityName(qualifiedEntityName);
final Iterable<IEObjectDescription> objectDescs = ServiceListener.getEObjectDescriptions(OSBPDtoPackage.Literals.LDTO, searchName);
boolean _hasNext = objectDescs.iterator().hasNext();
if (_hasNext) {
final IEObjectDescription eobjectDesc = objectDescs.iterator().next();
if ((resourceSet != null)) {
return eobjectDesc.getName().toString();
}
}
} catch (final Throwable _t) {
if (_t instanceof Exception) {
final Exception e = (Exception)_t;
e.printStackTrace();
} else {
throw Exceptions.sneakyThrow(_t);
}
}
return null;
}
public static LDto getDtoForEntity(final JvmType type) {
return EntityUtils.getDtoForEntity(type.getQualifiedName(), type.eResource().getResourceSet());
}
public static LDto getDtoForEntity(final String qualifiedEntityName, final ResourceSet resourceSet) {
try {
String searchName = EntityUtils.getQualifiedDtoNameForQualifiedEntityName(qualifiedEntityName);
final Iterable<IEObjectDescription> objectDescs = ServiceListener.getEObjectDescriptions(OSBPDtoPackage.Literals.LDTO, searchName);
boolean _hasNext = objectDescs.iterator().hasNext();
if (_hasNext) {
final IEObjectDescription eobjectDesc = objectDescs.iterator().next();
if ((resourceSet != null)) {
EObject _eObject = resourceSet.getEObject(eobjectDesc.getEObjectURI(), true);
return ((LDto) _eObject);
}
}
} catch (final Throwable _t) {
if (_t instanceof Exception) {
final Exception e = (Exception)_t;
e.printStackTrace();
} else {
throw Exceptions.sneakyThrow(_t);
}
}
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>
*/
public static String getDtoFQNForLEnum(final LEnum lenum) {
EObject container = ((EObject) lenum);
while (((container != null) && (!(container instanceof LTypedPackage)))) {
container = container.eContainer();
}
StringConcatenation _builder = new StringConcatenation();
String _name = ((LTypedPackage) container).getName();
_builder.append(_name);
_builder.append(".");
String _name_1 = lenum.getName();
_builder.append(_name_1);
return EntityUtils.applyDtoPackageNameFromEntityPackageNameForEnum(_builder.toString());
}
/**
* 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>
*/
public static String getDtoFQNForLBean(final LBean bean) {
return EntityUtils.getQualifiedDtoNameForQualifiedEntityName(EntityUtils.getFQNForLBean(bean));
}
/**
* 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 static String getDtoFQNForLEntity(final LEntity entity) {
return EntityUtils.getQualifiedDtoNameForQualifiedEntityName(EntityUtils.getFQNForLEntity(entity));
}
/**
* get the full qualified name for a LEntity
*/
public static String getFQNForLEntity(final LEntity entity) {
StringConcatenation _builder = new StringConcatenation();
EObject _eContainer = entity.eContainer();
String _name = ((LTypedPackage) _eContainer).getName();
_builder.append(_name);
_builder.append(".");
String _name_1 = entity.getName();
_builder.append(_name_1);
return _builder.toString();
}
/**
* get the full qualified name for a LBean
*/
public static String getFQNForLBean(final LBean bean) {
StringConcatenation _builder = new StringConcatenation();
EObject _eContainer = bean.eContainer();
String _name = ((LTypedPackage) _eContainer).getName();
_builder.append(_name);
_builder.append(".");
String _name_1 = bean.getName();
_builder.append(_name_1);
return _builder.toString();
}
/**
* 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 static String getQualifiedDtoNameForQualifiedEntityName(final 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 static String getQualifiedEntityNameForQualifiedDtoName(final 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 static String applyDtoPackageNameFromEntityPackageNameForEnum(final String fullQualifiedEnumName) {
String fullQualifiedDtoName = NamingConventionsUtil.toDtoQualifiedNameForEnum(fullQualifiedEnumName);
boolean _contains = fullQualifiedDtoName.contains(EntityUtils.DTOS_PACKAGE_TOKEN);
boolean _not = (!_contains);
if (_not) {
int lastDot = fullQualifiedDtoName.lastIndexOf(".");
String _substring = fullQualifiedDtoName.substring(0, lastDot);
String _plus = (_substring + EntityUtils.DTOS_PACKAGE_TOKEN);
String _substring_1 = fullQualifiedDtoName.substring((lastDot + 1));
String _plus_1 = (_plus + _substring_1);
fullQualifiedDtoName = _plus_1;
}
return fullQualifiedDtoName;
}
public static LEntity getEntityFromDto(final JvmType type) {
LEntity entity = null;
try {
String _qualifiedName = null;
if (type!=null) {
_qualifiedName=type.getQualifiedName();
}
String qualifiedDtoName = _qualifiedName;
Resource _eResource = null;
if (type!=null) {
_eResource=type.eResource();
}
ResourceSet _resourceSet = null;
if (_eResource!=null) {
_resourceSet=_eResource.getResourceSet();
}
ResourceSet resourceSet = _resourceSet;
final Iterable<IEObjectDescription> objectDescs = ServiceListener.getEObjectDescriptions(OSBPDtoPackage.Literals.LDTO, qualifiedDtoName);
Iterator<IEObjectDescription> _iterator = null;
if (objectDescs!=null) {
_iterator=objectDescs.iterator();
}
boolean _hasNext = _iterator.hasNext();
if (_hasNext) {
final IEObjectDescription eobjectDesc = objectDescs.iterator().next();
if ((resourceSet != null)) {
EObject _eObject = resourceSet.getEObject(eobjectDesc.getEObjectURI(), true);
LDto dto = ((LDto) _eObject);
if (((dto != null) && (dto.getWrappedType() != null))) {
boolean _eIsProxy = dto.getWrappedType().eIsProxy();
if (_eIsProxy) {
final Iterable<IEObjectDescription> entityDescs = ServiceListener.getEObjectDescriptions(OSBPEntityPackage.Literals.LENTITY,
qualifiedDtoName.replace(".dtos.", ".entities.").replace("Dto", ""));
boolean _hasNext_1 = entityDescs.iterator().hasNext();
if (_hasNext_1) {
final IEObjectDescription entityObjectDesc = entityDescs.iterator().next();
if ((resourceSet != null)) {
EObject _eObject_1 = resourceSet.getEObject(entityObjectDesc.getEObjectURI(), true);
entity = ((LEntity) _eObject_1);
}
}
} else {
LType _wrappedType = dto.getWrappedType();
entity = ((LEntity) _wrappedType);
}
}
}
}
} catch (final Throwable _t) {
if (_t instanceof Exception) {
final Exception e = (Exception)_t;
} else {
throw Exceptions.sneakyThrow(_t);
}
}
return entity;
}
public static LEntity getEntityFromDto(final LDto dto) {
LEntity entity = null;
if (((dto != null) && (dto.getWrappedType() != null))) {
boolean _eIsProxy = dto.getWrappedType().eIsProxy();
if (_eIsProxy) {
throw new IllegalStateException();
} else {
LType _wrappedType = dto.getWrappedType();
entity = ((LEntity) _wrappedType);
}
}
return entity;
}
public static LDto getDto(final JvmType type) {
try {
String qualifiedDtoName = type.getQualifiedName();
ResourceSet resourceSet = type.eResource().getResourceSet();
final Iterable<IEObjectDescription> objectDescs = ServiceListener.getEObjectDescriptions(OSBPDtoPackage.Literals.LDTO, qualifiedDtoName);
boolean _hasNext = objectDescs.iterator().hasNext();
if (_hasNext) {
final IEObjectDescription eobjectDesc = objectDescs.iterator().next();
if ((resourceSet != null)) {
EObject _eObject = resourceSet.getEObject(eobjectDesc.getEObjectURI(), true);
return ((LDto) _eObject);
}
}
} catch (final Throwable _t) {
if (_t instanceof Exception) {
final Exception e = (Exception)_t;
} else {
throw Exceptions.sneakyThrow(_t);
}
}
return null;
}
}