|
TECHNOLOGY: Application Express
Serving Application Express Reports
By Sergio Leunissen
Use Oracle Application Express (formerly Oracle HTML DB) to deliver reports that are user-customizable and secure.
Every organization needs to deliver reports on the Web. Often, slight variations of the same report need to be created for different end users. If you are a Web developer or a DBA who frequently needs to deliver reports and you want to make those reports more flexible
or your life easier by consolidating multiple reports into a single one, use Oracle Application Express's built-in session
state management, reporting engine, and authorization features to make building highly flexible yet secure reports a breeze.
Background/Overview
Oracle Application Express has a powerful reporting engine that supports the use of
templates to control look and feel, authorization schemes for column-level access control, sorting by column,
and flexible pagination options. This article covers how you can use some of these features to deliver a report with
- A variable number of columns based on user input
- Column-level access control
Step 1: Get Access to an Oracle
HTML DB Workspace
To get started, you need access to an HTML DB workspace. You can either download and install a copy in your own database (Oracle9i Database 9.2.0.3 or later) or register for a workspace online at htmldb.oracle.com.
To register for a workspace at htmldb.oracle.com:
- In the Tasks region, click on Request Workspace.
- Click on Next.
- In the First Name field, enter your first name.
- In the Last Name field, enter your last name.
- In the Email field, enter a valid e-mail
address, to which your login credentials
will be sent after the workspace request is approved.
- Click on Next.
- In the Workspace field, enter a
unique workspace name. If the name
you enter is already taken, you will be asked to enter another.
- Click on Next.
- Choose Request a new schema.
- Enter a unique schema name.
- For the initial space allocation, choose Small: 2 Megabytes.
- In the Description field, enter why you are requesting the workspacefor example, Testing the Oracle Magazine Step-by-Step Article.
- Click on Next.
- Review your selections, and click
on Accept.
You can log into your new workspace when you receive your login
credentials via e-mail.
From your installation or the
online Oracle Application Express, log into
your workspace.
Step 2: Create an Application
Once logged into your workspace, you need to create a new application. An Oracle Application Express application is a collection of pages that shares properties such as design templates, lists of values,
and authentication settings. For this
example, you use an existing application as a template for the new application. This new application looks just like the one being copied, but it lacks application data and logic, which you add.
To create an Oracle Application Express application based on an existing
application, begin on the Welcome
to HTML DB page:
- Click on Application Builder.
- Click on Create Application.
- Choose Based on an Existing Application.
- From the Which Application list, choose Sample Application.
- In the New Application Name field, enter Oracle Magazine Step by Step.
- Check the NavBar Icons box.
- Click on the Create Application button.
- Click on Run (the traffic light icon). The new application starts.
- In the Username field, enter your first name.
- Click on the Login button.
At this point, you have created an application with three empty pages, each associated with a tab, as shown in Figure 1. Oracle Application Express supports a two-level tab navigation model, but tabs are not required. The application uses templates that were copied from the application named Sample Application. Note the developer toolbar at the bottom of your page, containing the buttons Edit Application, Edit Page 1, New, and so on. Use this toolbar to add functionality to the application in an iterative fashion.
Figure 1: An empty application
Step 3: Create the Report
In Oracle Application Express, a report is the formatted result of a SQL query. For this example, you use the tables that are installed with the sample application. In particular, the tables DEMO_PRODUCT_INFO and DEMO_ORDER_ITEMS are joined to show all products with detailed information.
Creating a report is as easy as stepping through a wizard. There are two types of report wizards in Oracle Application Express: the Easy Report and the SQL Report. The Easy Report wizard does not require you to provide any SQL to create a report; the wizard guides you through the steps for choosing tables and columns. For this example, however, you use the SQL Report wizard, which requires you to enter
a SQL query.
To create a report by using the SQL Report wizard:
- In the developer toolbar at the bottom of the page, click on New.
- Choose Component, and click on Next.
- Choose Report, and click on Next.
- Choose SQL Report, and click on Next.
- Optionally change the page name, and click on Next.
- Where prompted, enter the following query:
select p.category,
p.product_name,
p.product_description,
p.product_avail,
i.unit_price
from demo_product_info p,
demo_order_items i
where p.product_id = i.product_id
- Click on Next.
- In the Region name field, enter Products.
- Click on Next.
- Click on Finish.
- Click on Run Page.
The report has been created as shown in Figure 2, and you'll notice that it contains columns for the product's category, name, description, availability, and price. Next, add some check boxes to the page that will allow you to remove columns from the report at runtime.
Figure 2: The report after completion of the SQL Report wizard
Step 4: Add a Region to Hold an
Input Form
In Oracle Application Express, content is placed on a page by use of regions. In step 3, you placed a Report region on the page, and now you add a Form region that will hold any form elements needed to control the report's appearance.
Oracle Application Express renders regions on a page from top to bottom in a sequence-number order that you specify when you create a region. To ensure that the Form region appears on the page before the Report region, you adjust its sequence accordingly.
Make sure you are running page 1, the page with the newly created report (created in step 3 above and shown in Figure 2). To create the Form region and adjust the sequence:
- In the developer toolbar at the bottom of the page, click on New.
- Choose Page Control, and click on Next.
- Choose Region, and click on Next.
- Choose HTML, and click on Next.
- In the Title field, enter Report Options.
- Change the value in the Sequence field to 5.
- Click on Next.
- Click on Create Region.
- Click on Run.
Step 5: Add Check BoxES to the
Form Region
In Oracle Application Express, forms consist of items, which are form elements for which the session state is automatically managed. Items can take many shapes. For example, a form item can be a text field, a text area, a hidden field, a select list, and so on. Here, you add an item of type check box.
To add the check box item:
- In the developer toolbar, click on New.
- Choose Page Control, and click on Next.
- Choose Item, and click on Next.
- In the Item Name field, enter P1_SHOW_COLUMNS.
- From the Region list, select the Report Options region.
- From the Display As list, select Checkbox.
- Click on Next.
- In the List of Values Query box, enter
STATIC2:Category;CAT,Description;DESCR,
Availability;AVAIL
and click on Next.
- In the Label field, enter Show Columns.
- Click on Next.
- In the Default field, enter
CAT:DESCR:AVAIL.
- Click on Create Item.
- Click on Run.
You have added Category, Description, and Availability check boxes to your form. Note that when you created the group of check boxes, you could have based its range of values on a database query. Because
the domain of values is unlikely to change for this particular group of check boxes, a static list of values was appropriate. When the default value
of the check box in No. 11 is set, all three check boxes will be checked when the user first runs the page.
Figure 3: Products report after adding check boxes and the Run Report button
Step 6: Add Button to Run Report
Next, you add a button to the Form region. Clicking on this button
causes Oracle Application Express to render page 1 again.
To add a button to the page:
- In the developer toolbar, click on New.
- Choose Page Control, and click on Next.
- Choose Button, and click on Next.
- From the Region list, select Report Options.
- From the Task radio group, choose Create a button in a region position, and click on Next.
- In the Button Name field, enter RUN_REPORT.
- In the Label field, enter Run Report, and click on Next.
- In the Display Properties step, leave all attributes unchanged, and click on Next.
- In the Branch to Page field, enter 1.
- Click on Create Button.
- Click on Run.
You have added a Run Report button to your form, so you can now rerun the report at will. Note that during the last step of the Create Button wizard, you created a branch, an instruction to the Oracle Application Express engine to go to a page and run it. In this case, the branch executes when you click on the Run Report button. Rather than going to a different page, the branch takes you back to page 1, allowing you to run the report over and over.
Step 7: Modify Report to Incorporate User Input
Now that you have created check boxes and a Run Report button, you need to adjust the report attributes so that end users can control which columns appear in the report. When an end user submits a page with checked check boxes, all checked values are submitted as a colon-separated list and automatically stored in session state. So if the Category and Description check boxes are checked, the item called P_SHOW_COLUMNS will have the following value:
CAT:DESCR
This value can be referenced with bind variable syntaxin this case:
:P1_SHOW_COLUMNS
To enable end users to control which columns appear in the report, you reference this bind variable in a display condition applied to several columns.
To set up the display condition with the bind variable:
- In the developer toolbar, click on Edit Page 1.
- In Page Rendering, under Regions, click on Products.
- Switch to the Report Attributes view, by clicking on the Report Attributes tab.
- Next to the PRODUCT_DESCRIPTION column, click on the edit icon.
- Scroll down to Conditional Display, or click on the Conditions quick link at the top of the page.
- From the Condition Type list, choose PL/SQL Expression.
- In the Expression 1 field, enter
instr (:P1_SHOW_COLUMNS, 'DESCR' ) >0
- Click on Apply Changes.
- Repeat the actions in Nos. 4
through 8 above for the CATEGORY and
AVAILABILITY columns, making sure to reference the appropriate check box value (CAT and AVAIL, respectively).
- In the Tasks region on the right side, click on Run this page.
You now see the report page again.
Step 8: Clean Up Report
Next, make a few cosmetic changes to the report to add the final touches. To clean up the report:
- In the developer toolbar, click on Edit Page 1.
- In Page Rendering, under Regions, click on Products.
- Switch to the Report Attributes view, by clicking on the Report Attributes tab.
- Under Report Column Attributes, click on the Custom radio button.
- Adjust the column headings as needed. For example, change Product Avail to Availability.
- Adjust the alignment of the UNIT_PRICE column to the right, by selecting right from the Column Align list.
- Click on Apply Changes.
- In the green vertical bar on the left side, click on Run Page.
You now see the report again with some minor adjustments.
Step 9: Creating an Access
Control Rule
You're now ready to start applying access control, allowing only certain users to view certain columns in the report. For this example, you implement access control by using an authorization scheme.
Authorization schemes are centrally defined access control rules that can be applied to different elements within an application. For example, you can use an authorization scheme to control access to individual pages, tabs, fields, or buttons. You can implement authorization schemes by using simple declarative instructions such as "the value of item X has the value Y" or by using more-complex techniques that involve SQL queries selecting from multiple database tables. Often, authorization schemes perform some sort of check based on the username of the currently logged-in user. In this exercise, you implement access control on the UNIT_PRICE column in the report, by showing this column to all users except GUEST users.
The username of the currently logged-in user is determined by the authentication scheme, a method that verifies a user's identity and then informs the Oracle Application Express engine that it has succeeded and passes along the relevant username. That username may come from a login page inside an Oracle Application Express application or from an external login page, as is the case when you use Oracle Application Server single sign-on, for example.
Oracle Application Express applications allow the authentication scheme to be changed at any time. This gives developers the flexibility to change to authentication based on Lightweight Directory Access Protocol (LDAP) or Oracle Application Server single sign-on, for example, without changing any application logic. For this exercise, you use an authentication method that doesn't challenge
users for a password but simply sets the currently logged-in user to whatever username that person entered in the username field on the login page. Although this is not a particularly secure authentication scheme, it is convenient for testing various user scenarios.
To create an authorization scheme:
In the developer toolbar, click on Edit Application.
In the subtabs under the Application tab, click on Security.
Click on Authorization.
Click on Create.
Choose From Scratch, and click on Next.
In the Name field, enter Guest user not allowed.
From the Scheme Type list, select Value of Item in Expression 1 Does NOT Equal Expression 2.
In the Expression 1 field, enter APP_USER.
In the Expression 2 field, enter GUEST.
In the "Identify error message
displayed when scheme violated" field, enter Guest user not allowed.
Click on Create.
Note that this implementation of an authorization scheme is purposely simplified. Rather than simply comparing the logged-in user with a single static value GUEST, authorization schemes usually query database tables to enforce access control.
Step 10: Applying Authorization
Having created the authorization scheme, you are now ready to apply it
to the appropriate element in the application. Remember that only privileged users are allowed to view price data about products and that GUEST users are not allowed to see this information.
To apply the authorization scheme:
- In the navigation bar at the top right of the page, click on Build.
- In the page list, click on Page 1.
- In Page Rendering, under Regions, click on Products.
- Click on the Report Attributes tab.
- Next to the UNIT_PRICE column, click on the edit icon.
- Scroll down, and under Authorization, from the Authorization Scheme list select the newly created scheme Guest user not allowed.
- Click on Apply Changes.
- In the Tasks region on the right side of the page, click on Run this page.
Step 11: Testing Access Control
To test access control, do the following:
- At the top right of the report page, click on Logout.
- In the Username field, enter guest.
- Click on Login.
Note that the price column does not appear in the report, because you are now logged in as a GUEST user.
Conclusion
In this article, you've seen how using Oracle Application Express can help you deliver highly flexible reports to the Web without programming or scripting. For more step-by-step instructions on building and formatting Oracle Application Express reports, see the Oracle Application Express How To pages at otn.oracle.com/products/database/application_express.
Sergio Leunissen (sergio.leunissen@oracle.com) is a senior product manager at Oracle Corporation.
|