tools.dynamia.zk.reports.ViewDescriptorDataSource Maven / Gradle / Ivy
/*
* Copyright (C) 2009 - 2019 Dynamia Soluciones IT S.A.S - NIT 900302344-1
* Colombia - South America
* All Rights Reserved.
*
* DynamiaTools is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License (LGPL v3) as
* published by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DynamiaTools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with DynamiaTools. If not, see .
*/
package tools.dynamia.zk.reports;
import net.sf.jasperreports.engine.JRField;
import net.sf.jasperreports.engine.data.JRAbstractBeanDataSource;
import tools.dynamia.commons.BeanUtils;
import tools.dynamia.domain.EntityReference;
import tools.dynamia.domain.EntityReferenceRepository;
import tools.dynamia.domain.util.DomainUtils;
import tools.dynamia.viewers.Field;
import tools.dynamia.viewers.ViewDescriptor;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class ViewDescriptorDataSource extends JRAbstractBeanDataSource {
private ViewDescriptor viewDescriptor;
private Object currentBean;
private Iterator iterator;
private Collection data;
private Map cache = new HashMap<>();
public ViewDescriptorDataSource(ViewDescriptor viewDescriptor, Collection beanCollection) {
super(false);
this.data = beanCollection;
this.viewDescriptor = viewDescriptor;
if (this.data != null) {
this.iterator = this.data.iterator();
}
}
@Override
public Object getFieldValue(JRField jrfield) {
Field field = viewDescriptor.getField(jrfield.getName());
Object value = null;
if (field.getFieldClass() != null && field.getFieldClass().equals(boolean.class)) {
value = BeanUtils.invokeBooleanGetMethod(currentBean, field.getName());
} else {
value = BeanUtils.invokeGetMethod(currentBean, field.getName());
value = checkAndLoadEntityReferenceValue(field, value);
}
if (value != null && jrfield.getValueClass().equals(String.class)) {
value = value.toString();
}
return value;
}
private Object checkAndLoadEntityReferenceValue(Field col, Object value) {
try {
String entityAlias = (String) col.getParams().get("entityAlias");
if (entityAlias != null && value instanceof Serializable) {
String key = entityAlias + ":" + value;
Object cacheValue = cache.get(key);
if (cacheValue == null) {
EntityReferenceRepository repo = DomainUtils.getEntityReferenceRepositoryByAlias(entityAlias);
if (repo != null) {
EntityReference ref = repo.load((Serializable) value);
if (ref != null) {
value = ref.toString();
cache.put(key, value);
}
}
} else {
value = cacheValue;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
@Override
public void moveFirst() {
if (this.data != null) {
this.iterator = this.data.iterator();
}
}
@Override
public boolean next() {
boolean hasNext = false;
if (this.iterator != null) {
hasNext = this.iterator.hasNext();
if (hasNext) {
this.currentBean = this.iterator.next();
}
}
return hasNext;
}
}