blob: f5d89bba80d8cebbb55daf35005cc8f0bbf3c134 [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.autowirehelper.utils;
import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import javax.inject.Inject;
import org.apache.commons.lang.StringEscapeUtils;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.osbp.dsl.semantic.common.types.LAttribute;
import org.eclipse.osbp.dsl.semantic.common.types.LDataType;
import org.eclipse.osbp.dsl.semantic.common.types.LKeyAndValue;
import org.eclipse.osbp.dsl.semantic.common.types.LReference;
import org.eclipse.osbp.dsl.semantic.common.types.LScalarType;
import org.eclipse.osbp.dsl.semantic.dto.LDto;
import org.eclipse.osbp.dsl.semantic.dto.LDtoAbstractAttribute;
import org.eclipse.osbp.dsl.semantic.dto.LDtoFeature;
import org.eclipse.osbp.dsl.semantic.dto.LDtoInheritedAttribute;
import org.eclipse.osbp.dsl.semantic.dto.LDtoInheritedReference;
import org.eclipse.osbp.dsl.semantic.entity.LEntity;
import org.eclipse.osbp.ecview.dsl.extensions.BindableTypeResolver;
import org.eclipse.osbp.ecview.dsl.extensions.OperationExtensions;
import org.eclipse.osbp.ecview.dsl.extensions.TypeHelper;
import org.eclipse.osbp.ecview.semantic.uimodel.UiBeanSlot;
import org.eclipse.osbp.ecview.semantic.uimodel.UiBindingExpression;
import org.eclipse.osbp.ecview.semantic.uimodel.UiLayout;
import org.eclipse.osbp.ecview.semantic.uimodel.UiModel;
import org.eclipse.osbp.ecview.semantic.uimodel.UiView;
import org.eclipse.osbp.ecview.semantic.uimodel.impl.UiViewImpl;
import org.eclipse.osbp.runtime.common.annotations.DomainDescription;
import org.eclipse.osbp.runtime.common.annotations.DomainKey;
import org.eclipse.osbp.runtime.common.annotations.Properties;
import org.eclipse.osbp.utils.common.EntityUtils;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.common.types.JvmAnnotationAnnotationValue;
import org.eclipse.xtext.common.types.JvmAnnotationReference;
import org.eclipse.xtext.common.types.JvmAnnotationType;
import org.eclipse.xtext.common.types.JvmAnnotationValue;
import org.eclipse.xtext.common.types.JvmDeclaredType;
import org.eclipse.xtext.common.types.JvmFeature;
import org.eclipse.xtext.common.types.JvmField;
import org.eclipse.xtext.common.types.JvmMember;
import org.eclipse.xtext.common.types.JvmStringAnnotationValue;
import org.eclipse.xtext.common.types.JvmType;
import org.eclipse.xtext.common.types.JvmTypeReference;
import org.eclipse.xtext.common.types.util.TypeReferences;
import org.eclipse.xtext.naming.IQualifiedNameProvider;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.InputOutput;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ListExtensions;
@SuppressWarnings("all")
public class AutowireUtil {
public static class Pair {
public String key;
public String value;
public Pair(final String key, final String value) {
this.key = key;
this.value = value;
}
}
@Inject
@Extension
private IQualifiedNameProvider _iQualifiedNameProvider;
@Inject
@Extension
private TypeHelper _typeHelper;
@Inject
private TypeReferences typeReferences;
public String generateId(final UiLayout layout) {
final BindableTypeResolver resolver = new BindableTypeResolver();
UiBindingExpression _autoWireSource = layout.getAutoWireSource();
JvmType resolvedType = resolver.resolveType(_autoWireSource);
String viewName = "";
EObject parent = layout.eContainer();
while ((!(parent instanceof UiModel))) {
{
if ((parent instanceof UiViewImpl)) {
String _name = ((UiViewImpl) parent).getName();
viewName = _name;
}
EObject _eContainer = parent.eContainer();
parent = _eContainer;
}
}
UiModel uimodel = ((UiModel) parent);
StringConcatenation _builder = new StringConcatenation();
String _packageName = uimodel.getPackageName();
_builder.append(_packageName, "");
{
boolean _notEquals = (!Objects.equal(viewName, null));
if (_notEquals) {
_builder.append(".");
_builder.append(viewName, "");
}
}
{
boolean _notEquals_1 = (!Objects.equal(resolvedType, null));
if (_notEquals_1) {
_builder.append(".");
String _identifier = resolvedType.getIdentifier();
_builder.append(_identifier, "");
}
}
return _builder.toString();
}
public UiView getUiView(final UiLayout layout) {
EObject parent = layout.eContainer();
while ((!(parent instanceof UiViewImpl))) {
EObject _eContainer = parent.eContainer();
parent = _eContainer;
}
if ((parent instanceof UiViewImpl)) {
return ((UiViewImpl) parent);
}
return null;
}
public boolean isAttribute(final JvmType type, final String attribute, final String filterName) {
LDto dto = EntityUtils.getDto(type);
boolean _notEquals = (!Objects.equal(dto, null));
if (_notEquals) {
List<LDtoAbstractAttribute> _attributes = dto.getAttributes();
final Function1<LDtoAbstractAttribute, Boolean> _function = new Function1<LDtoAbstractAttribute, Boolean>() {
public Boolean apply(final LDtoAbstractAttribute it) {
String _name = AutowireUtil.this.toName(it);
return Boolean.valueOf(Objects.equal(_name, attribute));
}
};
LDtoAbstractAttribute attr = IterableExtensions.<LDtoAbstractAttribute>findFirst(_attributes, _function);
boolean _notEquals_1 = (!Objects.equal(attr, null));
if (_notEquals_1) {
LScalarType _datatype = this.toDatatype(attr);
LDataType datatype = ((LDataType) _datatype);
String _name = datatype.getName();
return Objects.equal(_name, filterName);
}
}
return false;
}
/**
* Detects all the existing properties of the jvmtype corresponding attribute, the datatype of the attribute and references.
*/
public List<AutowireUtil.Pair> toProperties(final JvmDeclaredType type, final String attributeName) {
Map<String, List<AutowireUtil.Pair>> _fieldProperties = this.toFieldProperties(type);
final List<AutowireUtil.Pair> result = _fieldProperties.get(attributeName);
boolean _notEquals = (!Objects.equal(result, null));
if (_notEquals) {
for (final AutowireUtil.Pair prop : result) {
String _unescapeHtml = StringEscapeUtils.unescapeHtml(prop.value);
prop.value = _unescapeHtml;
}
}
List<AutowireUtil.Pair> _xifexpression = null;
boolean _notEquals_1 = (!Objects.equal(result, null));
if (_notEquals_1) {
_xifexpression = result;
} else {
_xifexpression = CollectionLiterals.<AutowireUtil.Pair>newArrayList();
}
return _xifexpression;
}
/**
* Detects all the existing properties of the jvmtype corresponding reference and the referenced dto.
*/
public Map<String, List<AutowireUtil.Pair>> toFieldProperties(final JvmDeclaredType jvmType) {
final Map<String, List<AutowireUtil.Pair>> propMap = CollectionLiterals.<String, List<AutowireUtil.Pair>>newHashMap();
Iterable<JvmFeature> _allFeatures = jvmType.getAllFeatures();
Iterable<JvmField> _filter = Iterables.<JvmField>filter(_allFeatures, JvmField.class);
final Consumer<JvmField> _function = new Consumer<JvmField>() {
public void accept(final JvmField it) {
StringConcatenation _builder = new StringConcatenation();
String _qualifiedName = jvmType.getQualifiedName();
_builder.append(_qualifiedName, "");
_builder.append(".");
String _simpleName = it.getSimpleName();
_builder.append(_simpleName, "");
final String attrPropKey = _builder.toString();
final List<AutowireUtil.Pair> values = CollectionLiterals.<AutowireUtil.Pair>newArrayList();
propMap.put(attrPropKey, values);
EList<JvmAnnotationReference> _annotations = it.getAnnotations();
final Function1<JvmAnnotationReference, Boolean> _function = new Function1<JvmAnnotationReference, Boolean>() {
public Boolean apply(final JvmAnnotationReference e) {
JvmAnnotationType _annotation = e.getAnnotation();
String _identifier = _annotation.getIdentifier();
String _name = Properties.class.getName();
final boolean result = _identifier.equals(_name);
return Boolean.valueOf(result);
}
};
Iterable<JvmAnnotationReference> _filter = IterableExtensions.<JvmAnnotationReference>filter(_annotations, _function);
final Consumer<JvmAnnotationReference> _function_1 = new Consumer<JvmAnnotationReference>() {
public void accept(final JvmAnnotationReference it) {
final JvmAnnotationReference ref = it;
EList<JvmAnnotationValue> _values = ref.getValues();
final Function1<JvmAnnotationValue, JvmAnnotationAnnotationValue> _function = new Function1<JvmAnnotationValue, JvmAnnotationAnnotationValue>() {
public JvmAnnotationAnnotationValue apply(final JvmAnnotationValue it) {
return ((JvmAnnotationAnnotationValue) it);
}
};
List<JvmAnnotationAnnotationValue> _map = ListExtensions.<JvmAnnotationValue, JvmAnnotationAnnotationValue>map(_values, _function);
final Consumer<JvmAnnotationAnnotationValue> _function_1 = new Consumer<JvmAnnotationAnnotationValue>() {
public void accept(final JvmAnnotationAnnotationValue av) {
EList<JvmAnnotationReference> _values = av.getValues();
final Consumer<JvmAnnotationReference> _function = new Consumer<JvmAnnotationReference>() {
public void accept(final JvmAnnotationReference it) {
EList<JvmAnnotationValue> _values = it.getValues();
JvmAnnotationValue _get = _values.get(0);
final JvmStringAnnotationValue keyAn = ((JvmStringAnnotationValue) _get);
EList<JvmAnnotationValue> _values_1 = it.getValues();
JvmAnnotationValue _get_1 = _values_1.get(1);
final JvmStringAnnotationValue valueAn = ((JvmStringAnnotationValue) _get_1);
EList<String> _values_2 = keyAn.getValues();
String _get_2 = _values_2.get(0);
EList<String> _values_3 = valueAn.getValues();
String _get_3 = _values_3.get(0);
AutowireUtil.Pair _pair = new AutowireUtil.Pair(_get_2, _get_3);
values.add(_pair);
}
};
_values.forEach(_function);
}
};
_map.forEach(_function_1);
}
};
_filter.forEach(_function_1);
}
};
_filter.forEach(_function);
return propMap;
}
/**
* Detects all the existing properties of the jvmtype corresponding datatype of the attribute.
*/
public LDataType toDataType(final JvmType type, final String attributeName) {
LDto dto = EntityUtils.getDto(type);
boolean _notEquals = (!Objects.equal(dto, null));
if (_notEquals) {
List<LDtoAbstractAttribute> _attributes = dto.getAttributes();
final Function1<LDtoAbstractAttribute, Boolean> _function = new Function1<LDtoAbstractAttribute, Boolean>() {
public Boolean apply(final LDtoAbstractAttribute it) {
String _name = AutowireUtil.this.toName(it);
return Boolean.valueOf(Objects.equal(_name, attributeName));
}
};
LDtoAbstractAttribute attr = IterableExtensions.<LDtoAbstractAttribute>findFirst(_attributes, _function);
boolean _notEquals_1 = (!Objects.equal(attr, null));
if (_notEquals_1) {
LScalarType dataType = this.toDatatype(attr);
if ((dataType instanceof LDataType)) {
LScalarType _datatype = this.toDatatype(attr);
return ((LDataType) _datatype);
}
}
}
return null;
}
/**
* Provides the datatype of the corresponding attribute.
*/
public List<LKeyAndValue> toDataTypeProperties(final JvmType type, final String attributeName) {
LDataType dataType = this.toDataType(type, attributeName);
boolean _notEquals = (!Objects.equal(dataType, null));
if (_notEquals) {
return dataType.getProperties();
}
return CollectionLiterals.<LKeyAndValue>newArrayList();
}
public String toName(final LDtoFeature feature) {
if ((feature instanceof LDtoInheritedAttribute)) {
LAttribute _inheritedFeature = ((LDtoInheritedAttribute)feature).getInheritedFeature();
return _inheritedFeature.getName();
} else {
if ((feature instanceof LDtoInheritedReference)) {
LReference _inheritedFeature_1 = ((LDtoInheritedReference)feature).getInheritedFeature();
return _inheritedFeature_1.getName();
} else {
return feature.getName();
}
}
}
public LScalarType toDatatype(final LDtoAbstractAttribute att) {
if ((att instanceof LDtoInheritedAttribute)) {
LAttribute _inheritedFeature = ((LDtoInheritedAttribute)att).getInheritedFeature();
return _inheritedFeature.getType();
} else {
return att.getType();
}
}
public boolean isBlob(final JvmType type, final String attribute) {
LDto dto = EntityUtils.getDto(type);
List<LDtoAbstractAttribute> _attributes = dto.getAttributes();
final Function1<LDtoAbstractAttribute, Boolean> _function = new Function1<LDtoAbstractAttribute, Boolean>() {
public Boolean apply(final LDtoAbstractAttribute it) {
String _name = it.getName();
return Boolean.valueOf(Objects.equal(_name, attribute));
}
};
Iterable<LDtoAbstractAttribute> attrs = IterableExtensions.<LDtoAbstractAttribute>filter(_attributes, _function);
for (final LDtoAbstractAttribute attr : attrs) {
{
LScalarType _type = attr.getType();
LDataType datatype = ((LDataType) _type);
InputOutput.<LDataType>println(datatype);
return datatype.isAsBlob();
}
}
return false;
}
public String getEntityName(final UiBeanSlot uiBeanSlot) {
JvmTypeReference _jvmType = uiBeanSlot.getJvmType();
JvmType _type = _jvmType.getType();
LEntity _entityFromDto = EntityUtils.getEntityFromDto(_type);
final LEntity entity = ((LEntity) _entityFromDto);
String eName = null;
boolean _equals = Objects.equal(entity, null);
if (_equals) {
JvmTypeReference _jvmType_1 = uiBeanSlot.getJvmType();
JvmType _type_1 = _jvmType_1.getType();
boolean _equals_1 = Objects.equal(_type_1, null);
if (_equals_1) {
eName = "";
} else {
JvmTypeReference _jvmType_2 = uiBeanSlot.getJvmType();
JvmType _type_2 = _jvmType_2.getType();
boolean _notEquals = (!Objects.equal(_type_2, null));
if (_notEquals) {
eName = "";
}
}
} else {
String _name = entity.getName();
eName = _name;
}
return eName;
}
public boolean isClass(final JvmType type) {
boolean _is = this.typeReferences.is(type, Class.class);
if (_is) {
return true;
}
return false;
}
public boolean isByteArray(final JvmType type) {
boolean _is = this.typeReferences.is(type, byte[].class);
if (_is) {
return true;
}
return false;
}
public boolean isAllowed(final JvmType type, final JvmType beanType, final OperationExtensions.OperationInfo info) {
boolean _isNumberWithDigits = this._typeHelper.isNumberWithDigits(type);
if (_isNumberWithDigits) {
return true;
} else {
boolean _isNumberWithoutDigits = this._typeHelper.isNumberWithoutDigits(type);
if (_isNumberWithoutDigits) {
return true;
} else {
boolean _isString = this._typeHelper.isString(type);
if (_isString) {
return true;
} else {
boolean _isDate = this._typeHelper.isDate(type);
if (_isDate) {
return true;
} else {
boolean _isByteArray = this.isByteArray(type);
if (_isByteArray) {
return true;
} else {
boolean _isBoolean = this._typeHelper.isBoolean(type);
if (_isBoolean) {
return true;
} else {
boolean _isEnum = this._typeHelper.isEnum(type);
if (_isEnum) {
return true;
} else {
boolean _isDomainReference = this._typeHelper.isDomainReference(info);
if (_isDomainReference) {
boolean _matched = false;
if (!_matched) {
if (type instanceof JvmDeclaredType) {
_matched=true;
boolean _or = false;
boolean _isAnnotationOf = this.isAnnotationOf(((JvmDeclaredType) type), DomainKey.class);
if (_isAnnotationOf) {
_or = true;
} else {
boolean _isAnnotationOf_1 = this.isAnnotationOf(((JvmDeclaredType) type), DomainDescription.class);
_or = _isAnnotationOf_1;
}
if (_or) {
return true;
}
}
}
return false;
} else {
boolean _and = false;
JvmField _field = info.getField();
boolean _notEquals = (!Objects.equal(_field, null));
if (!_notEquals) {
_and = false;
} else {
JvmField _field_1 = info.getField();
boolean _isCollection = this._typeHelper.isCollection(_field_1);
_and = _isCollection;
}
if (_and) {
return true;
}
}
}
}
}
}
}
}
}
return false;
}
private boolean isAnnotationOf(final JvmDeclaredType type, final Class<?> clazz) {
EList<JvmMember> _members = type.getMembers();
for (final JvmMember member : _members) {
EList<JvmAnnotationReference> _annotations = member.getAnnotations();
for (final JvmAnnotationReference annotation : _annotations) {
JvmAnnotationType _annotation = annotation.getAnnotation();
String _identifier = _annotation.getIdentifier();
String _canonicalName = clazz.getCanonicalName();
boolean _equals = _identifier.equals(_canonicalName);
if (_equals) {
return true;
}
}
}
EList<JvmTypeReference> _superTypes = type.getSuperTypes();
for (final JvmTypeReference superType : _superTypes) {
JvmType _type = superType.getType();
if ((_type instanceof JvmDeclaredType)) {
JvmType _type_1 = superType.getType();
return this.isAnnotationOf(((JvmDeclaredType) _type_1), clazz);
}
}
return false;
}
}