Reporting – Sort Orders, Selections, and Related Data TEC02 Brian Ciccolo
Agenda Navigating the report Java source Determining a report’s “root table” Adding sort order options Adding selection options Adding related data lookup maps
Navigating the Report Java Source Before you can customize the Java, you must first understand the Java. Before you can understand the Java, you must first understand its components. ~ Ancient X2 Proverb
Copyright Comment /* * ==================================================================== * * X2 Development Corporation * * Copyright (c) X2 Development Corporation. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, is not permitted without a written agreement * from X2 Development Corporation. * * ==================================================================== */
Package and Imports package com.x2dev.sis.reports.system; import net.sf.jasperreports.engine.JRDataSource; import org.apache.ojb.broker.query.*; import com.x2dev.sis.model.beans.*; import com.x2dev.sis.model.business.*; import com.x2dev.sis.tools.reports.*; import com.x2dev.sis.web.UserDataContainer;
Class Comment /** * Prepares the data for the Student Profile report. This report simple selects * students from the current school (with an optional criteria for YOG or homeroom) * and order the results by YOG, homeroom, or last name. * X2 Development Corporation */
Class Definition public class StudentProfileData extends ReportJavaSourceNet { // Bunch of stuff... }
Constants /** * Name for the "active only" report parameter. The value is an Boolean. */ public static final String ACTIVE_ONLY_PARAM = "activeOnly"; /** * Name for the enumerated "selection" report parameter. The value is an Integer. */ public static final String QUERY_BY_PARAM = "queryBy"; /** * Name for the "selection value" report parameter. The value is a String. */ public static final String QUERY_STRING_PARAM = "queryString"; /** * Name for the "secondary students" report parameter. The value is a Boolean. */ public static final String SECONDARY_STUDENT_PARAM = "secondaryStudent";
Member Variables private Student m_currentStudent;
gatherData() /** com.x2dev.sis.tools.reports.ReportJavaSourceDori#gatherData() protected JRDataSource gatherData() { // Another bunch of stuff... return new QueryIteratorDataSource(getBroker().getIteratorByQuery(query)); }
Other Methods /** com.x2dev.sis.tools.ToolJavaSource# * saveState(com.x2dev.sis.web.UserDataContainer) protected void saveState(UserDataContainer userData) { /* * If we're in the context of a single student, print the * report for just that student. */ m_currentStudent = (Student) userData.getCurrentRecord(Student.class); }
The gatherData() Formula Declare an instance of the Criteria type Add various filters based on user input Declare an instance of QueryByCriteria Add various sorts based on user input Execute the query and return the results
A Report’s “Root Table” Embedded in the “thing” returned at the end of gatherData() Defines what fields are available in the report format Might not be based on an actual table in the database
Finding the “Root Table” Guess Become a Java programmer and work at X2 Use heuristics
Using Heuristics In the format: Deduce from the fields that have already been defined In the Java source: Examine the return statement in gatherData() o If a QueryIteratorDataSource, find where the query is declared – that’s the root table o If a DataGrid, find where grid.set() is called – those are the fields in the root table
Exercise 1: Custom Sort Customize the Student List report by adding an option to sort the results by state ID.
Exercise 1, Step 1 <input name="sort" data-type="integer" display-type="select“ display-name="report.shared.sort"> <option value="0" display-name="report.shared.sort.currentSort“ context-dependent="true"/>
Exercise 1, Step 2 switch (sort) { case 0: // Current sort applyCurrentSort(query); break; case 1: // Name view query.addOrderByAscending(Student.COL_NAME_VIEW); break; case 2: // YOG query.addOrderByAscending(Student.COL_YOG); query.addOrderByAscending(Student.COL_NAME_VIEW); break; case 3: // Homeroom query.addOrderByAscending(Student.COL_HOMEROOM); query.addOrderByAscending(Student.COL_NAME_VIEW); break; case 4: // State ID query.addOrderByAscending(Student.COL_STATE_ID); query.addOrderByAscending(Student.COL_NAME_VIEW); break; default: // No sort specified break; }
Exercise 1, Step 3 Mark custom Compile Java source Save Test
Exercise 2: Custom Filter Customize the Student Contacts report by adding an option to filter the results by students’ last name (i.e., only return those students whose last name begins with a user-specified letter).
Exercise 2, Step 1 <input name="queryBy" data-type="integer" display-type="select“ display-name="report.shared.query"> <option value="0" display-name="report.shared.query.currentSelection“ context-dependent="true"/>
Exercise 2, Step 2 switch (queryBy) { case 0: // Current selection SubQuery subQuery = new SubQuery(Student.class, X2BaseBean.COL_OID, getCurrentCriteria()); criteria.addIn(StudentContact.COL_STUDENT_OID, subQuery); break; case 2: // YOG criteria.addEqualTo(StudentContact.REL_STUDENT + "." + Student.COL_YOG, getParameter(QUERY_STRING_PARAM)); break; case 3: // Homeroom criteria.addEqualTo(Student.COL_HOMEROOM, getParameter(QUERY_STRING_PARAM)); break; case 4: // Last name begins with criteria.addBeginsWithIgnoreCase(Student.COL_NAME_VIEW, getParameter(QUERY_STRING_PARAM)); break; default: // No additional criteria (this is the case for "All") break; }
Exercise 2, Step 3 Mark custom Compile Java source Save Test
Exercise 3: Related Data Customize the Student List report (again) by displaying the homeroom teacher’s name next to the student’s homeroom number on the format.
Adding an Option for “Current Selection” Adding an option to run a report based on the current selection of student records is detailed in knowledge base article K located in the session folder.
Thank you.