blob: 7921674cb25562c0ad87b27132d4372f385ed300 [file] [log] [blame]
/**
* Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), 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:
* Florian Pirchner - Initial implementation
*/
package org.eclipse.osbp.runtime.web.ecview.services.vaadin.impl
import org.eclipse.osbp.dsl.semantic.common.types.LAnnotationDef
import org.eclipse.osbp.dsl.semantic.common.types.LOperation
import org.eclipse.osbp.dsl.semantic.entity.LBean
import org.eclipse.osbp.dsl.semantic.entity.LEntity
import org.eclipse.osbp.runtime.common.annotations.DomainDescription
import org.eclipse.osbp.runtime.common.annotations.DomainKey
class BeanHelper {
/**
* @param bean the bean
* @return the caption property if it could be found. Null otherwise.
*/
def static String findCaptionProperty(Object bean) {
if (bean == null) {
return null
}
if (bean instanceof Class<?>) {
return findCaptionProperty(bean as Class<?>)
} else {
return findCaptionProperty(bean.class)
}
}
/**
* @param beanClass the bean class
* @return the caption property if it could be found. Null otherwise.
*/
def static String findCaptionProperty(Class<?> beanClass) {
if (beanClass == null) {
return null
}
// try to find annotation in class
for (field : beanClass.declaredFields) {
if (field.isAnnotationPresent(typeof(DomainKey))) {
return field.name
}
}
// include super classes too
for (field : beanClass.fields) {
if (field.isAnnotationPresent(typeof(DomainKey))) {
return field.name
}
}
return null
}
/**
* @param lEntity the entity
* @return the caption property if it could be found. Null otherwise.
*/
def static String findCaptionProperty(LEntity lEntity) {
var String bestMatch = null
if (lEntity == null) {
return null
}
for (field : lEntity.allAttributes) {
if (field.domainKey
|| field.name.equalsIgnoreCase("Name")
|| field.name.equalsIgnoreCase("Number")
|| field.name.equalsIgnoreCase("Description")
|| field.uuid
) {
bestMatch = field.name
}
}
return bestMatch
// // include super classes too
// for (field : beanClass.fields) {
// if (field.isAnnotationPresent(typeof(DomainKey))) {
// return field.name
// }
// }
}
/**
* @param lBean the bean
* @return the caption property if it could be found. Null otherwise.
*/
def static String findCaptionProperty(LBean lBean) {
var String bestMatch = null
if (lBean == null) {
return null
}
for (field : lBean.allAttributes) {
if (field.domainKey
|| field.name.equalsIgnoreCase("Name")
|| field.name.equalsIgnoreCase("Number")
|| field.name.equalsIgnoreCase("Description")
|| field.uuid
) {
bestMatch = field.name
}
}
return bestMatch
// // include super classes too
// for (field : beanClass.fields) {
// if (field.isAnnotationPresent(typeof(DomainKey))) {
// return field.name
// }
// }
}
/**
* @param bean the bean
* @return the description property if it could be found. Null otherwise.
*/
def static String findDescriptionProperty(Object bean) {
if (bean == null) {
return null
}
if (bean instanceof Class<?>) {
return findDescriptionProperty(bean as Class<?>)
} else {
return findDescriptionProperty(bean.class)
}
}
/**
* @param beanClass the beanClass
* @return the description property if it could be found. Null otherwise.
*/
def static String findDescriptionProperty(Class<?> beanClass) {
if (beanClass == null) {
return null
}
// try to find annotation in fields of class
for (field : beanClass.declaredFields) {
if (field.isAnnotationPresent(typeof(DomainDescription))) {
return field.name
}
}
// try to find annotation in methods of class
for (method : beanClass.declaredMethods) {
if (method.isAnnotationPresent(typeof(DomainDescription))) {
return OperationExtensions.toPropertyName(method.name)
}
}
// include super classes too
for (field : beanClass.fields) {
if (field.isAnnotationPresent(typeof(DomainDescription))) {
return field.name
}
}
// include super class too
for (method : beanClass.methods) {
if (method.isAnnotationPresent(typeof(DomainDescription))) {
return OperationExtensions.toPropertyName(method.name)
}
}
return null
}
/**
* @param entity the entity
* @return the description property if it could be found. Null otherwise.
*/
def static String findDescriptionProperty(LEntity entity) {
if (entity == null) {
return null
}
// try to find annotation in fields of class
for (field : entity.attributes) {
if (field.domainDescription) {
return field.name
}
}
// try to find annotation in methods of class
for (method : entity.operations) {
val LAnnotationDef def = method.annotations.findFirst[
it.annotation.annotationType.qualifiedName.equals((typeof(DomainDescription)).canonicalName)
];
if(def != null){
return OperationExtensions.toPropertyName(method.name)
}
}
// include super classes too
for (field : entity.allAttributes) {
if (field.domainDescription) {
return field.name
}
}
// include super class too
for (method : entity.allFeatures) {
if (method instanceof LOperation){
val LAnnotationDef def = method.annotations.findFirst[
it.annotation.annotationType.qualifiedName.equals((typeof(DomainDescription)).canonicalName)
];
if(def != null){
return OperationExtensions.toPropertyName(method.name)
}
}
}
return null
}
/**
* @param bean the bean
* @return the description property if it could be found. Null otherwise.
*/
def static String findDescriptionProperty(LBean bean) {
if (bean == null) {
return null
}
// try to find annotation in fields of class
for (field : bean.attributes) {
if (field.domainDescription) {
return field.name
}
}
// try to find annotation in methods of class
for (method : bean.operations) {
val LAnnotationDef def = method.annotations.findFirst[
it.annotation.annotationType.qualifiedName.equals((typeof(DomainDescription)).canonicalName)
];
if(def != null){
return OperationExtensions.toPropertyName(method.name)
}
}
// include super classes too
for (field : bean.allAttributes) {
if (field.domainDescription) {
return field.name
}
}
// include super class too
for (method : bean.allFeatures) {
if (method instanceof LOperation){
val LAnnotationDef def = method.annotations.findFirst[
it.annotation.annotationType.qualifiedName.equals((typeof(DomainDescription)).canonicalName)
];
if(def != null){
return OperationExtensions.toPropertyName(method.name)
}
}
}
return null
}
}