-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: JasperReport, Hibernate et les collections
PostPosted: Thu May 12, 2005 5:17 am 
Newbie

Joined: Wed May 11, 2005 8:15 am
Posts: 6
Location: Bourgogne / France
Bonjour,
Pour ceux qui veulent utiliser hibernate avec jasperreport, voici un petit bout de code de mon cru bien utile.
Il s'agit simplment d'un JRDataSource adapaté à hibernate.

Il permet de retouver les propriètés imbriquées (components) (en utilisant "__" à la place du ".").
Il est également utile pour les sous-rapports (subreports) en mappant les collection vers un JRDataSource.


Code:
import java.lang.reflect.*;
import java.util.*;

import org.apache.commons.beanutils.*;
import org.apache.commons.logging.*;

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.*;
import net.sf.jasperreports.engine.export.*;
import net.sf.jasperreports.engine.util.*;
import net.sf.jasperreports.view.*;


public class JRHibernateDataSource implements JRDataSource {

    protected HashMap fieldsToIdxMap=new HashMap();

    protected Iterator iterator;

    protected Object currentValue;


    public JRHibernateDataSource(Collection list) {
        this.iterator = list.iterator();
    }

    public JRHibernateDataSource(Map list) {
      this.iterator = list.values().iterator();
  }

    private Object nestedFieldValue(Object object, String field) {
        Object value = null;
        if (field.indexOf("__")>-1) {
            try {
                Method nestedGetter = PropertyUtils.getReadMethod(PropertyUtils.getPropertyDescriptor(object, field.substring(0,field.indexOf("__"))));
                Object nestedObject = nestedGetter.invoke(object, null);
                value=nestedFieldValue(nestedObject,field.substring(field.indexOf("__")+2,field.length()));
            } catch (Exception ex) {
                ex.printStackTrace();
            }           
        } else {
            try {
                Method getter = PropertyUtils.getReadMethod(PropertyUtils.getPropertyDescriptor(object, field));
                value = getter.invoke(object, null);
                if(Collection.class.isAssignableFrom(getter.getReturnType())) {
                    return new JRHibernateDataSource((Collection)value);
                }
                if(Map.class.isAssignableFrom(getter.getReturnType())) {
                    return new JRHibernateDataSource((Map)value);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }           
        }
        return value;
    }
   
    public Object getCurrentValue() throws JRException {
        return currentValue;
    }

    public Object getFieldValue(JRField field) throws JRException {
        return nestedFieldValue(currentValue,field.getName());
    }
   
    public boolean next() throws JRException {
        currentValue = iterator.hasNext() ? iterator.next() : null;
        return currentValue != null;
    }



Sébastien


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 4:34 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Poste ça sur le wiki s'il te plait. C'est plus perenne

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.