blob: 19920b1788e5a447dfffbae316e93c8326f0bbe7 [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 com.google.common.base.Objects;
import java.util.Iterator;
import org.eclipse.emf.common.util.URI;
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.naming.QualifiedName;
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();
Resource _eResource = type.eResource();
ResourceSet resourceSet = _eResource.getResourceSet();
String searchName = EntityUtils.getQualifiedDtoNameForQualifiedEntityName(qualifiedEntityName);
final Iterable<IEObjectDescription> objectDescs = ServiceListener.getEObjectDescriptions(OSBPDtoPackage.Literals.LDTO, searchName);
Iterator<IEObjectDescription> _iterator = objectDescs.iterator();
boolean _hasNext = _iterator.hasNext();
if (_hasNext) {
Iterator<IEObjectDescription> _iterator_1 = objectDescs.iterator();
final IEObjectDescription eobjectDesc = _iterator_1.next();
boolean _notEquals = (!Objects.equal(resourceSet, null));
if (_notEquals) {
QualifiedName _name = eobjectDesc.getName();
return _name.toString();
}
}
} 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 (((!Objects.equal(container, null)) && (!(container instanceof LTypedPackage)))) {
EObject _eContainer = container.eContainer();
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) {
String _fQNForLBean = EntityUtils.getFQNForLBean(bean);
return EntityUtils.getQualifiedDtoNameForQualifiedEntityName(_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>
*/
public static String getDtoFQNForLEntity(final LEntity entity) {
String _fQNForLEntity = EntityUtils.getFQNForLEntity(entity);
return EntityUtils.getQualifiedDtoNameForQualifiedEntityName(_fQNForLEntity);
}
/**
* 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 LEntity!
* <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);
}
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 qualifiedDtoName = type.getQualifiedName();
Resource _eResource = type.eResource();
ResourceSet resourceSet = _eResource.getResourceSet();
final Iterable<IEObjectDescription> objectDescs = ServiceListener.getEObjectDescriptions(OSBPDtoPackage.Literals.LDTO, qualifiedDtoName);
Iterator<IEObjectDescription> _iterator = objectDescs.iterator();
boolean _hasNext = _iterator.hasNext();
if (_hasNext) {
Iterator<IEObjectDescription> _iterator_1 = objectDescs.iterator();
final IEObjectDescription eobjectDesc = _iterator_1.next();
boolean _notEquals = (!Objects.equal(resourceSet, null));
if (_notEquals) {
URI _eObjectURI = eobjectDesc.getEObjectURI();
EObject _eObject = resourceSet.getEObject(_eObjectURI, true);
LDto dto = ((LDto) _eObject);
boolean _and = false;
boolean _notEquals_1 = (!Objects.equal(dto, null));
if (!_notEquals_1) {
_and = false;
} else {
LType _wrappedType = dto.getWrappedType();
boolean _notEquals_2 = (!Objects.equal(_wrappedType, null));
_and = _notEquals_2;
}
if (_and) {
LType _wrappedType_1 = dto.getWrappedType();
boolean _eIsProxy = _wrappedType_1.eIsProxy();
if (_eIsProxy) {
String _replace = qualifiedDtoName.replace(".dtos.", ".entities.");
String _replace_1 = _replace.replace("Dto", "");
final Iterable<IEObjectDescription> entityDescs = ServiceListener.getEObjectDescriptions(OSBPEntityPackage.Literals.LENTITY, _replace_1);
Iterator<IEObjectDescription> _iterator_2 = entityDescs.iterator();
boolean _hasNext_1 = _iterator_2.hasNext();
if (_hasNext_1) {
Iterator<IEObjectDescription> _iterator_3 = entityDescs.iterator();
final IEObjectDescription entityObjectDesc = _iterator_3.next();
boolean _notEquals_3 = (!Objects.equal(resourceSet, null));
if (_notEquals_3) {
URI _eObjectURI_1 = entityObjectDesc.getEObjectURI();
EObject _eObject_1 = resourceSet.getEObject(_eObjectURI_1, true);
entity = ((LEntity) _eObject_1);
}
}
} else {
LType _wrappedType_2 = dto.getWrappedType();
entity = ((LEntity) _wrappedType_2);
}
}
}
}
} 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;
boolean _and = false;
boolean _notEquals = (!Objects.equal(dto, null));
if (!_notEquals) {
_and = false;
} else {
LType _wrappedType = dto.getWrappedType();
boolean _notEquals_1 = (!Objects.equal(_wrappedType, null));
_and = _notEquals_1;
}
if (_and) {
LType _wrappedType_1 = dto.getWrappedType();
boolean _eIsProxy = _wrappedType_1.eIsProxy();
if (_eIsProxy) {
throw new IllegalStateException();
} else {
LType _wrappedType_2 = dto.getWrappedType();
entity = ((LEntity) _wrappedType_2);
}
}
return entity;
}
public static LDto getDto(final JvmType type) {
try {
String qualifiedDtoName = type.getQualifiedName();
Resource _eResource = type.eResource();
ResourceSet resourceSet = _eResource.getResourceSet();
final Iterable<IEObjectDescription> objectDescs = ServiceListener.getEObjectDescriptions(OSBPDtoPackage.Literals.LDTO, qualifiedDtoName);
Iterator<IEObjectDescription> _iterator = objectDescs.iterator();
boolean _hasNext = _iterator.hasNext();
if (_hasNext) {
Iterator<IEObjectDescription> _iterator_1 = objectDescs.iterator();
final IEObjectDescription eobjectDesc = _iterator_1.next();
boolean _notEquals = (!Objects.equal(resourceSet, null));
if (_notEquals) {
URI _eObjectURI = eobjectDesc.getEObjectURI();
EObject _eObject = resourceSet.getEObject(_eObjectURI, true);
return ((LDto) _eObject);
}
}
} catch (final Throwable _t) {
if (_t instanceof Exception) {
final Exception e = (Exception)_t;
} else {
throw Exceptions.sneakyThrow(_t);
}
}
return null;
}
}