45.2 Create a Report Manually with SQL and XML Queries

When you create a report, you can either use the Report Wizard to assist you or create the report yourself. To build this report, you will need to create two queries: a SQL query and an XML query.

45.2.1 Create a SQL query for your new report

To create a SQL query:

  1. Launch Reports Builder (or, if already open, choose File > New > Report).

  2. In the Welcome or New Report dialog box, select Build a new report manually, then click OK.

    Your new report displays in the Object Navigator as something like MODULE 2. You will also see the Data Model view of your new report.

  3. In the Data Model view, click the SQL Query tool in the tool palette, then click in an open area of the Data Model view to display the SQL Query Statement dialog box.

  4. In the SQL Query Statement field, enter the following SELECT statement:

    SELECT W.WAREHOUSE_ID,
           W.WAREHOUSE_NAME, 
           L.CITY, 
           L.STATE_PROVINCE, 
           C.COUNTRY_NAME
    FROM WAREHOUSES W, 
         HR.LOCATIONS L,
         HR.COUNTRIES C
    WHERE (W.LOCATION_ID = L.LOCATION_ID(+))
      AND (L.COUNTRY_ID = C.COUNTRY_ID(+))  
    ORDER BY C.COUNTRY_NAME, W.WAREHOUSE_NAME
    

    Note:

    You can enter this query in any of the following ways:
    • Copy and paste the code from the provided text file called xmlpds_sql.txt into the SQL Query Statement field.

    • Click Query Builder to build the query without entering any code manually.

    • Type the code in the SQL Query Statement field.

  5. Click OK.

    Note:

    If the Connect dialog box displays, enter the user ID, password, and name of the database that contains the sample schema.

    The data model displays in the Data Model view, and should look something like this:

    Figure 45-2 Data Model for the XML PDS example SQL query

    Description of Figure 45-2 follows
    Description of "Figure 45-2 Data Model for the XML PDS example SQL query"

  6. Save your report as inventoryreport_xml_your_initials.rdf.

You have created a SQL query to retrieve the data for your report.

45.2.2 Create an XML query to access your XML data source

In this section, you will create a query to access the XML data source. You can view the resulting report we have provided to make sure your query is correct. Please note that you must update the paths to the Data Definition files with the location of the example files we provided.

To create an XML query:

  1. In the Data Model view, choose Insert > Query to display the Data Wizard.

  2. If the Data Wizard Welcome page displays, click Next.

  3. On the Query page, click Next.

  4. On the Data Source page, click XML Query, then click Next.

  5. On the Data page, click Query Definition to display the Define XML Query dialog box.

  6. In the Define XML Query dialog box, under Data Definition, click Browse to locate the XSD file we have provided, warehouse_inventory.xsd and open it.

  7. Under Data Source, click Browse to locate the XML file we have provided that contains your data, warehouse_inventory.xml and open it.

    If you want to compare your data definition to the one we provided, make sure that you replace the data definition locations with the locations of your files.

  8. Click OK.

  9. In the Data Wizard, still on the Data page, click Next.

  10. On the Groups page, click Next.

  11. Click Finish to display your data model in the Data Model view. It should look something like this:

    Figure 45-3 Data model for the XML PDS example with XML and SQL queries

    Description of Figure 45-3 follows
    Description of "Figure 45-3 Data model for the XML PDS example with XML and SQL queries"

  12. Save your report.

You have created an XML query to access the XML data source we have provided.

45.2.3 Create a data link between two queries

You will now need to link the SQL query and the XML query so that you can access your corporate data as well as the data for each of the local warehouses.

To create a data link:

  1. In the Data Model view, click the Data Link tool in the tool palette.

  2. Click the WAREHOUSE_ID column in your first query (Q_1).

  3. Drag your cursor until it is over the WAREHOUSE_ID1 column in the second query (Q_2).

    Your data model should now look something like this:

    Figure 45-4 Data Model with a data link between a SQL query and an XML query

    Description of Figure 45-4 follows
    Description of "Figure 45-4 Data Model with a data link between a SQL query and an XML query"

    You will notice that the WAREHOUSE_ID column is now highlighted at the bottom of Q_1, with a line pointing to the WAREHOUSE_ID1 column.

  4. Save your report.

You have created a data link between the WAREHOUSE_ID columns in the two queries.

45.2.4 Create a layout for your report using the Report Wizard

Before you can run any report, you must define a layout. The easiest way to do this is to use the Report Wizard.

To create a paper layout:

  1. In the Data Model view, right-click on the canvas, then choose Report Wizard.

  2. In the Report Wizard, on the Report Type page, select Create Paper Layout only, then click Next.

  3. On the Style page, select Group Above.

  4. On the Groups page, make sure the G_WAREHOUSE_ID and G_WAREHOUSE_ID1 groups are listed in the Group Fields list with a Down Print Direction.

  5. On the Fields page, click the double right arrows (>>) to move all of the fields to the Displayed Fields list.

  6. On the Labels page, adjust the labels as desired.

  7. On the Template page, select Predefined Template and click Beige, then click Finish to display your report output in the Paper Design view. It should look something like this:

    Figure 45-5 Paper Design view for the XML PDS report

    Description of Figure 45-5 follows
    Description of "Figure 45-5 Paper Design view for the XML PDS report"

  8. Save your report.

You have created the layout for your paper report.

Note:

You can also run the provided report inventory_report.rdf. Before you can run the report, double-click the XML query in the Data Model view, and point the XML data source to the appropriate XSD and XML files.

45.2.5 Apply alternating row colors to your report

Now that you have created the report, you can make it more user-friendly by using a summary column to apply alternating row colors.

To create a summary column to count the rows:

  1. In the Data Model view, click the Summary Column tool in the tool palette.

    If you are still in the Paper Design view, you can click the Data Model button in the toolbar to display the Data Model view.

  2. Click in the XML query group (G_WAREHOUSE_ID1) to create a summary column.

  3. Double-click the new summary column object (CS_1) to display the Property Inspector, and set the following properties:

    • Under General Information, set the Name property to LineNo.

    • Under Column, make sure the Column Type property is set to Summary, and that the Datatype property is set to Number.

    • Under Summary, set the Function property to Count, and set the Source property to PRODUCT_NAME.

To create a procedure that changes the row colors:

  1. In the Object Navigator, click the Program Units node for your report.

  2. Click the Create button in the toolbar to display the New Program Unit dialog box.

  3. In the New Program Unit dialog box, type linecolors as in the Name field.

  4. Select Procedure, and click OK to display the PL/SQL Editor for the new program unit.

  5. In the PL/SQL Editor, enter the following PL/SQL code to change the text color of the alternating rows to blue:

    PROCEDURE LineColors IS
    BEGIN
      if (:LineNo mod 2=0) then
            srw.set_text_color('blue');
      else 
        srw.set_text_color('black');
            end if; 
    END; 
    

    Note:

    You can copy and paste this code from the procedure provided in the xmlpds_code.txt file. Just copy the text under Line Colors Procedure.
  6. Click Compile to compile the procedure.

    If any errors display, make sure the code is correct, and that you created the summary column in the steps above.

  7. Click Close.

    Note:

    Optionally, you can also change the fill colors of the alternating rows by following the steps in the above section, and using the following PL/SQL code. In this example code, we have changed the fill color of alternating rows to red and blue:
    PROCEDURE LineColors IS
    BEGIN
      if (:LineNo mod 2=0)
       then
         srw.set_foreground_fill_color('blue');
         srw.set_fill_pattern('solid');
      else
       srw.set_foreground_fill_color('red');
       srw.set_fill_pattern('solid');
      end if;
    END; 
    

To create a format trigger for each field that calls the procedure:

  1. In the Object Navigator, under your report name, expand the Paper Layout node and navigate to Main Section > Body > M_G_WAREHOUSE_ID_GRPFR > R_G_WAREHOUSE_ID > M_G_WAREHOUSE_ID1_GRPFR > R_G_WAREHOUSE_ID1.

  2. Under R_G_WAREHOUSE_ID1, double-click F_PRODUCT_ID to display the Property Inspector.

    Note:

    If you cannot find a particular field, use the Find field at the top of the Object Navigator.
  3. In the Property Inspector, under Advanced Layout, double-click the Format Trigger property field to display the PL/SQL Editor.

  4. In the PL/SQL Editor, use the template to enter the following PL/SQL code:

    function F_PRODUCT_IDformatTrigger return Boolean is
    begin
      LineColors;
      return (TRUE);
    end;
    

    Note:

    Make sure you do not touch the boldface text. Simply type in the code below this text to create the format trigger. You can copy and paste this code from the procedure provided in the xmlpds_code.txt file. Just copy the text under Format Trigger Code.
  5. Add a format trigger for the following fields, using the same code as in the previous step. Be sure not to delete the first line of the code, where the format trigger name is defined:

    • F_PRODUCT_NAME

    • F_QUANTITY_ON_HAND

    • F_WAREHOUSE_ID1

  6. Save your report.

  7. Click the Run Paper Layout button in the toolbar to run your report to paper. Your report should look something like this:

    Figure 45-6 XML PDS Report with Alternating Row Colors

    Description of Figure 45-6 follows
    Description of "Figure 45-6 XML PDS Report with Alternating Row Colors"

You have now applied alternating row colors to your report.

45.2.6 Filter your XML data using a group filter

If you have a lot of data in your XML file, you might want to consider sorting and filtering it. You can do so by creating a group filter and a hierarchy. The steps in this section will show you how to create a filter that will only show the inventory items for a user-defined quantity amount. The filter will be based on a parameter that the user can enter at runtime. You will then create a hierarchy in your data model to group the data in your report.

To create a user parameter and a group filter:

  1. In the Object Navigator, under the User Parameters node, create a new user parameter called P_MAXQTY, with a Datatype of Number, Width of 20, and Initial Value of 50 (see Section 4.11.2, "Creating a user parameter").

  2. In the Data Model view, double-click the G_WAREHOUSE_ID1 group in the XML query to display its Property Inspector.

  3. In the Property Inspector, under Group:

    • set the Filter Type property to PL/SQL.

    • click the PL/SQL filter property field to display the PL/SQL Editor.

  4. In the PL/SQL Editor, use the template to enter the following PL/SQL code:

    function G_WAREHOUSE_ID1GroupFilter return boolean is
    begin
      if :quantity_on_hand < :P_maxqty then
         return (TRUE);
      else
             return (false);
      end if;
    end;
    

    Note:

    You can also copy and paste this code from the provided file called xmlpds_code.txt. Copy the code under the heading "Group Filter Code."
  5. Click Compile, and fix any errors.

    Note:

    If you are not familiar with compiling PL/SQL, refer to a PL/SQL reference manual.
  6. When the code compiles successfully, click Close.

  7. Save your report.

  8. Click the Run Paper Layout button in the toolbar to run your report to paper. Notice how a Parameter Form now displays where you can adjust the quantity of items displayed in your report.

    You can also run the provided report inventoryreport.rdf to view the results in Reports Builder.

  9. Save your report.

To create a hierarchy for the XML query:

  1. In the Data Model view, click the PRODUCT_ID column in the XML query, then drag it between the query name and the G_WAREHOUSE_ID1 group.

    Your data model should look like this:

    Figure 45-7 Data Model with Group Hierarchy

    Description of Figure 45-7 follows
    Description of "Figure 45-7 Data Model with Group Hierarchy"

    Note:

    Notice in the above image that a green circle displays above G_WAREHOUSE_ID1. This circle indicates that a group filter has been created for the group.
  2. Save your report. You have now created a group hierarchy that sorts the data in your report.