View Javadoc

1   package ar.com.fdvs.dj.test;
2   
3   import java.util.Iterator;
4   import java.util.List;
5   
6   import net.sf.jasperreports.engine.JRDataSource;
7   import net.sf.jasperreports.engine.query.JRHibernateQueryExecuterFactory;
8   import net.sf.jasperreports.view.JasperViewer;
9   
10  import org.hibernate.Session;
11  
12  import ar.com.fdvs.dj.core.DJConstants;
13  import ar.com.fdvs.dj.domain.DynamicReport;
14  import ar.com.fdvs.dj.domain.Style;
15  import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
16  import ar.com.fdvs.dj.domain.builders.StyleBuilder;
17  import ar.com.fdvs.dj.domain.constants.Font;
18  import ar.com.fdvs.dj.domain.constants.GroupLayout;
19  import ar.com.fdvs.dj.test.domain.db.Customer;
20  import ar.com.fdvs.dj.test.hibernate.HibernateUtil;
21  import ar.com.fdvs.dj.test.hibernate.TestSchema;
22  
23  public class HQLReportTest extends BaseDjReportTest {
24  
25  	private Session hsession;
26  
27  	public DynamicReport buildReport() throws Exception {
28  		Style groupStyle = new StyleBuilder(false).setFont(new Font(18, Font._FONT_VERDANA, true)).build();
29  		/**
30  		 * Creates the DynamicReportBuilder and sets the basic options for
31  		 * the report
32  		 */
33  		FastReportBuilder drb = new FastReportBuilder();
34  		drb
35  			.addColumn("City", "city", String.class.getName(),50,groupStyle)
36  			.addColumn("Last Name", "lastName", String.class.getName(),50)
37  			.addColumn("First Name", "firstName", String.class.getName(),30)
38  			.addColumn("Id", "id", Long.class.getName(),30)
39  			.addColumn("Street", "street", String.class.getName(),50)
40  			.addGroups(1, GroupLayout.VALUE_IN_HEADER)
41  			.setTitle("Customers")
42  			.setSubtitle("Order by city and last name")
43  			.setQuery("from Customer order by city, lastName", DJConstants.QUERY_LANGUAGE_HQL)
44  			.setTemplateFile("templates/TemplateReportTest.jrxml")
45  			.setUseFullPageWidth(true);
46  
47  		DynamicReport dr = drb.build();
48  
49  		this.hsession = HibernateUtil.getSession();
50  		params.put(JRHibernateQueryExecuterFactory.PARAMETER_HIBERNATE_SESSION, hsession);
51  
52  
53  		return dr;
54  	}
55  	
56  	protected JRDataSource getDataSource() {
57  		return null; //we use Hibernate Session
58  	}	
59  	
60  	public static void main(String[] args) throws Exception {
61  		TestSchema.buildConfiguration();
62  		HQLReportTest test = new HQLReportTest();
63  		test.testReport();
64  		JasperViewer.viewReport(test.jp);
65  		//JasperDesignViewer.viewReportDesign(DynamicJasperHelper.generateJasperReport(test.dr, new ClassicLayoutManager(),test.params));
66  	}
67  	
68  	public void testHibernate() {
69  		TestSchema.buildConfiguration();
70  		Session s = HibernateUtil.getSession();
71  		List l = s.createQuery("from Customer order by lastName").list();
72  		for (Iterator iterator = l.iterator(); iterator.hasNext();) {
73  			Customer cust = (Customer) iterator.next();
74  			log.debug(cust.getFirstName() + ", " + cust.getLastName());
75  		}
76  	}
77  	
78  
79  }