Chapter 10. A Mini Book Catalog Site

In this last chapter you will explore a real-life scenario of an online book catalog and how to apply APEX to create it in a matter of hours. Think of this catalog as the bare beginnings of a bookstore—you aren't even going to implement anything like shopping-cart functionality but you will use the various APEX techniques and features you learned throughout the earlier chapters in the book.

Before you create the online book catalog, let's define the scope of what you want to build. The application will be divided into two main areas—the Administration part of the web site (where the user can manage the list of available books) and the front end (which is the actual book catalog visible to the public).

In addition, you'll create a chart report to let the storekeeper view the current stockcount of all books in the store. Last of all, you'll pull all these separate pages together by providing a proper navigation scheme for your application using navigation lists and tabs.

Setting Up the Main Objects for Your Book Catalog

Problem

You need to set up the underlying database tables and sample records (books) required for the book catalog.

Solution

To set up the book catalog tables, follow these steps. (Note that you can find the code in the in the example download for this book—you don't need to type it in manually.)

  1. Create the main Books table as follows, together with the sample data:

    CREATE TABLE "BOOKS"
       (    "BOOKID" NVARCHAR2(255),
            "BOOKTITLE" NVARCHAR2(255),
            "BOOKISBN" NVARCHAR2(255),
            "BOOKPUBLISHER" NVARCHAR2(255),
            "BOOKEDITION" NVARCHAR2(255),
            "BOOKCATEGORY" NVARCHAR2(255),
            "BOOKDESCRIPTION" NVARCHAR2(255),
            "BOOKPRICE" FLOAT(9),
            "AUTHOR" NVARCHAR2(255),
            "BOOKIMAGE" BLOB,
    CONSTRAINT "BOOKS_PK" PRIMARY KEY ("BOOKID") ENABLE
       )
    /
    
    INSERT INTO BOOKS(BOOKID, BOOKTITLE, BOOKISBN, BOOKPUBLISHER, BOOKEDITION, BOOKCATEGORY,
    BOOKDESCRIPTION, BOOKPRICE, AUTHOR) VALUES('B1','PRO ODP.NET
    PROGRAMMING','9781430228202','APRESS PUBLISHING','2010','C3','This book is a comprehensive and
    easy-to-understand guide for using the Oracle Data Provider (ODP) version 11g on the .NET
    Framework',59.99,'ED ZEHOO')
    /
    
    INSERT INTO BOOKS(BOOKID, BOOKTITLE, BOOKISBN, BOOKPUBLISHER, BOOKEDITION, BOOKCATEGORY,
    BOOKDESCRIPTION, BOOKPRICE, AUTHOR) VALUES('B2','IPHONE PROGRAMMING','9781430228400','WROX
    PUBLISHING','2011','C3','This book describes the basics of iPhone and iPad development using
    Objective C',49.99,'GREG YAP')
    /
    
    INSERT INTO BOOKS(BOOKID, BOOKTITLE, BOOKISBN, BOOKPUBLISHER, BOOKEDITION, BOOKCATEGORY,
    BOOKDESCRIPTION, BOOKPRICE, AUTHOR) VALUES('B3','HOW MASTER CHIEF BECAME MASTER
    CHEF','1123433328400','TOR BOOKS','2010','C2','Master Chief goes on a vacation in China. Read
    about his exploits in this book!',39.99,'JAMES BURKE')
    /
    
    INSERT INTO BOOKS(BOOKID, BOOKTITLE, BOOKISBN, BOOKPUBLISHER, BOOKEDITION, BOOKCATEGORY,
    BOOKDESCRIPTION, BOOKPRICE, AUTHOR) VALUES('B4','THE CURSE OF AMMATTAR','9781430228400','TOR
    BOOKS','2011','C4','Classic horror story set in medieval Thailand',29.99,'SARAH HAWKINS')
    /
    
    INSERT INTO BOOKS(BOOKID, BOOKTITLE, BOOKISBN, BOOKPUBLISHER, BOOKEDITION, BOOKCATEGORY,
    BOOKDESCRIPTION, BOOKPRICE, AUTHOR) VALUES('B5','CHING CHONG: THE RISE AND FALL OF
    JACKSON','343322221400','NIECA BOOKS','2011','C1','A story about the misfortunes of Jackson
    Junior as he travels across Asia',12.99,'TARA WILLIAMS')
    /
    
    INSERT INTO BOOKS(BOOKID, BOOKTITLE, BOOKISBN, BOOKPUBLISHER, BOOKEDITION, BOOKCATEGORY,
    BOOKDESCRIPTION, BOOKPRICE, AUTHOR) VALUES('B6','UNFORTUNATE
    CIRCUMSTANCES','115062221400','PARAMOUNT BOOKS','2011','C1','Read about the unfortunate
    circumstances of Ali, someone you will absolutely not care about',19.99,'DANA T. ROLLS')
    /
    
    INSERT INTO BOOKS(BOOKID, BOOKTITLE, BOOKISBN, BOOKPUBLISHER, BOOKEDITION, BOOKCATEGORY,
    BOOKDESCRIPTION, BOOKPRICE, AUTHOR) VALUES('B7','SCARY LIONS','11553221400','PARAMOUNT
    BOOKS','2011','C1','A book about the greasy politicians in Mootawambaland and how Alex becomes
    one of them',15.99,'TERRY BARRACK')
    /
    
    INSERT INTO BOOKS(BOOKID, BOOKTITLE, BOOKISBN, BOOKPUBLISHER, BOOKEDITION, BOOKCATEGORY,
    BOOKDESCRIPTION, BOOKPRICE, AUTHOR) VALUES('B8','THE LIME TREE','22113221400','ZACK
    PUBLISHING','2011','C1','A book about how Sally became a top salesperson when she decides to
    sell lime as lemon',16.99,'JAMES LEE')
    /
  2. Create the Inventory table as follows:

    CREATE TABLE  "INVENTORY"
       (    "ID" NVARCHAR2(255),
            "BOOKID" NVARCHAR2(255),
            "COPIESINSTOCK" NUMBER(9,3),
             CONSTRAINT "INVENTORY_PK" PRIMARY KEY ("ID") ENABLE
       )
    /
    
    INSERT INTO INVENTORY(ID,BOOKID,COPIESINSTOCK) VALUES('1','B1',10)
    /
    
    INSERT INTO INVENTORY(ID,BOOKID,COPIESINSTOCK) VALUES('2','B2',15)
    /
    
    INSERT INTO INVENTORY(ID,BOOKID,COPIESINSTOCK) VALUES('3','B3',1)
    /
    
    INSERT INTO INVENTORY(ID,BOOKID,COPIESINSTOCK) VALUES('4','B4',6)
    /
    
    INSERT INTO INVENTORY(ID,BOOKID,COPIESINSTOCK) VALUES('5','B5',17)
    /
    
    INSERT INTO INVENTORY(ID,BOOKID,COPIESINSTOCK) VALUES('6','B6',9)
    /
    
    INSERT INTO INVENTORY(ID,BOOKID,COPIESINSTOCK) VALUES('7','B7',14)
    /
    
    INSERT INTO INVENTORY(ID,BOOKID,COPIESINSTOCK) VALUES('8','B8',9)
    /
  3. Create the Category table as follows:

    CREATE TABLE  "CATEGORY"
       (    "CATEGORYID" NVARCHAR2(255),
            "CATEGORYNAME" NVARCHAR2(255),
            "DESCRIPTION" NVARCHAR2(255),
             CONSTRAINT "CATEGORY_PK" PRIMARY KEY ("CATEGORYID") ENABLE
       )
    /
    
    INSERT INTO CATEGORY(CATEGORYID, CATEGORYNAME, DESCRIPTION) VALUES('C1','FICTION','Fiction
    selection')
    /
    
    INSERT INTO CATEGORY(CATEGORYID, CATEGORYNAME, DESCRIPTION)
    VALUES('C2','SCIENCEFICTION','Science Fiction Selection')
    /
    INSERT INTO CATEGORY(CATEGORYID, CATEGORYNAME, DESCRIPTION) VALUES('C3','COMPUTERS','Computer
    Books Selection')
    /
    
    INSERT INTO CATEGORY(CATEGORYID, CATEGORYNAME, DESCRIPTION) VALUES('C4','HORROR','Best Horror
    Selections')
    /

How It Works

The Books table is the main table that stores the list of books in the book catalog; the Inventory table stores the stockcount of each available book; and the Category table stores the full list of book categories (genres) available in the catalog. Table 10-1 describes the mapping between the various keys in each respective table.

Table 10.1. Foreign Key Mapping Between Different Tables in the Book Catalog Application

Foreign key mapping

Description

Books.BookID = Inventory.BookID

Each book has a corresponding entry in the Inventory table that stores the stockcount details for the book.

Books.BookCategory = Category.CategoryID

Each book belongs to a genre/category, the details of which are stored in the Category table. The ID stored in the Books.BookCategory column looks up the corresponding entry in the Category table.

Creating the Pages to Manage the List of Books

Problem

The storekeeper needs a way to manage the list of books in his book catalog.

Solution

You must first create the forms that allow the storekeeper to add, edit, and delete book titles in the book catalog.

To create the page that allows the storekeeper to add/edit/delete books, please follow these instructions:

  1. Create a new Database application named Book catalog.

  2. During the Pages step of the Create Application wizard, choose the Report and Form page type linked to the Books table, as shown in Figure 10-1.

    Adding a Report and Form

    Figure 10.1. Adding a Report and Form

  3. Click the Add Page button to add the form and report. After doing so, finish the wizard to create the application.

  4. You should now have the screen shown in Figure 10-2.

    The current list of pages in your application

    Figure 10.2. The current list of pages in your application

  5. If you launch the main Books form, you should see the screen shown in Figure 10-3.

    The book details entry form

    Figure 10.3. The book details entry form

  6. The book details entry form from Figure 10-3 lets the storekeeper add a new book to the book catalog, but the form is not complete yet. The BookCategory field should show a drop-down list of categories so that the storekeeper can pick from a list of categories instead of manually typing in the category code.

  7. Edit the form. In the Page Rendering section, right-click the P1_BOOKCATEGORY field, and choose to edit the field (as shown in Figure 10-4).

    Editing the Book Category field

    Figure 10.4. Editing the Book Category field

  8. In the Field Properties page, change the Display As field from Text Field to Select List, as shown in Figure 10-5.

    Changing the Display As field

    Figure 10.5. Changing the Display As field

  9. Scroll down to the List Of Values area, and specify the following SQL:

    SELECT Description, CategoryID FROM Category
  10. You should now have the screen shown in Figure 10-6.

    Specifying the SQL for the list of values area

    Figure 10.6. Specifying the SQL for the list of values area

  11. Save your changes and run the form. You should be able to select the book category from a list of values, as shown in Figure 10-7.

    The book category selection list in action

    Figure 10.7. The book category selection list in action

  12. Let's turn to the report that was generated earlier. When you run the report in the application, you should see the list of books shown in Figure 10-8.

    List of books

    Figure 10.8. List of books

  13. There's still one last thing to do at this point, which is to change the BookCategory column so that it displays the full category description instead of the category code. To do so, edit the report.

    Tip

    You might also want to change the heading of the report columns to display a more user friendly/readable title. You can do so by editing the report column and changing the Column Heading field.

  14. In the Page Rendering area of the report, right click on the Books node, and choose the Edit link (as shown in Figure 10-9).

    Editing the Books region

    Figure 10.9. Editing the Books region

  15. Scroll down to the source of the report, and change the existing SQL to the following:

    select
    "BOOKID",
    "BOOKTITLE",
    "CATEGORY"."DESCRIPTION" "BOOKCATEGORY",
    "BOOKISBN",
    "BOOKPUBLISHER",
    "BOOKEDITION",
    "BOOKDESCRIPTION",
    "BOOKPRICE",
    "AUTHOR",
    dbms_lob.getlength("BOOKIMAGE") "BOOKIMAGE"
     from   "BOOKS" LEFT JOIN "CATEGORY" ON "BOOKS"."BOOKCATEGORY" = "CATEGORY"."CATEGORYID"
  16. You should now have the screen shown in Figure 10-10.

    Changing the SQL for the region source

    Figure 10.10. Changing the SQL for the region source

  17. Apply the changes and run the report again. Note that the report displays the full category name instead of the category code, as shown in Figure 10-11.

    The full category name of the book displayed in the report

    Figure 10.11. The full category name of the book displayed in the report

  18. Try uploading book photos through your newly created form/report. In the report, edit a book by clicking the edit icon at the far left of each row.

  19. Browse for an image of the book and upload it through the form, as shown in Figure 10-12. Save all changes to the record.

    Uploading a book image

    Figure 10.12. Uploading a book image

  20. To confirm that the image was successfully uploaded, edit the record again; this time you see a Download link next to the control. Clicking the download link will bring you to the uploaded image file shown in Figure 10-13.

    Sample book image

    Figure 10.13. Sample book image

  21. Upload your own photos for all the other books in the Books table.

How It Works

As demonstrated in the earlier chapters of this book, APEX lets you easily create a form and report combination from a database table. This, in turn, allows you to easily and quickly setup CRUD (Create, Read, Update and Delete) functionality in your application.

Also covered in the previous chapters of this book was the use of List of Values (LOVs) dynamically generated from a specified SQL statement. LOVs can be used as the data source for Select Lists (drop-downs), as seen in this recipe.

Setting Up the Book Catalog Front End

Problem

You completed the bulk of the catalog back end in Recipe 10-2. Now you need the accompanying front-end portal that displays the list of books available in the catalog to the public, in a typical online book catalog format.

Solution

To create the catalog front end, follow these steps:

  1. In the same Book catalog application, create a new Report.

  2. In the wizard, choose to create a Classic Report.

  3. Name this report "My Mini Book catalog" and the region title as "Browse our books".

  4. In the SQL Query section of the report, write the following SQL:

    SELECT "BOOKID",
    "BOOKTITLE","BOOKISBN","BOOKPUBLISHER","BOOKEDITION","BOOKCATEGORY","BOOKDESCRIPTION","BOOKPRI
    CE","AUTHOR", dbms_lob.getlength("BOOKIMAGE") "BOOKIMAGE" FROM Books
  5. Complete the wizard to create the report. After you have done so, edit the report again. You should have the screen shown in Figure 10-14.

    Changing the SQL for the region source

    Figure 10.14. Changing the SQL for the region source

  6. Complete the rest of the wizard using the default settings.

  7. If you run your report, you should see the standard layout shown in Figure 10-15.

    The standard report layout

    Figure 10.15. The standard report layout

  8. This layout works, of course, but it's not a very user-friendly book catalog. It would be nicer to list each item in the format shown in Figure 10-16.

    The desired layout for the book listing in the front end catalog

    Figure 10.16. The desired layout for the book listing in the front end catalog

  9. First, you will need to change the report slightly so that the actual book images will appear in each row. To do this, edit the report. In the Page Rendering area of the report, right click on the BookImage column and choose to edit it.

  10. In the Number/Date Format field, type in the following text:

    IMAGE:BOOKS:BOOKIMAGE:BOOKID
  11. You should now have the screenshot shown in Figure 10-17.

    Setting the Number/Date format to display images in a report

    Figure 10.17. Setting the Number/Date format to display images in a report

  12. Save your changes and return to the main page definition area of the report.

  13. Click on the Report Attributes tab. Change the sorting of the columns so that the BookImage column appears first, as shown in Figure 10-18.

    Shifting the viewing order of the BOOKIMAGE column

    Figure 10.18. Shifting the viewing order of the BOOKIMAGE column

  14. Save your changes and run the report. You should now have something that looks like Figure 10-19.

    The book images displayed in the report

    Figure 10.19. The book images displayed in the report

  15. Edit the report again. In the Page Rendering area of the report, right-click on the "Browse our books" region and choose to edit it. Click on the Reports Attribute tab.

  16. In the list of columns, hide all columns (untick the checkbox in the Show column) except for the BOOKIMAGE and BOOKTITLE columns, as shown in Figure 10-20.

    Hiding all the other columns

    Figure 10.20. Hiding all the other columns

  17. Save your changes, and edit the report again.

  18. In the Page Rendering area, right click on the BOOKTITLE field and edit it.

  19. Scroll down to the Column Formatting area, and in the HTML Expression field, type the following HTML:

    <b>Title :</b> #BOOKTITLE#<br>
    <b>Author :</b> #AUTHOR#<br>
    <b>Publisher :</b> #BOOKPUBLISHER#<br>
    <b>Price :</b> USD #BOOKPRICE#<br>
    <b>Description :</b> #BOOKDESCRIPTION#<br>
  20. You should now have the screen shown in Figure 10-21.

    Defining a display template for the row via the HTML Expression field

    Figure 10.21. Defining a display template for the row via the HTML Expression field

  21. Save and apply your changes. Now try running the report again. You should now see the books laid out in a more user-friendly and readable fashion, as shown in Figure 10-22.

    The revised layout for the book catalog front end

    Figure 10.22. The revised layout for the book catalog front end

How It Works

The HTML Expression field allows you to define your own template to display columnar data in a report for each row. Using the #FIELDNAME# notation, you can display data from any report column inside your HTML block.

Tip

Again, you might want to tidy up your interface by providing column captions that are more intuitive.

Changing the Home Page of the Application

Problem

The current home page of the Book catalog application is the administration back end Books report. You would like to change it to be the catalog front end page.

Solution

To change the home page of an application, follow these instructions:

  1. In the same Book catalog application, click on the Shared Components icon.

  2. Under the Security area, click on the Security Attributes link, as shown in Figure 10-23.

    The Security Attributes link

    Figure 10.23. The Security Attributes link

  3. In the ensuing page, change the page number in the Home link property from 1 to the page number of the front end catalog page. For example, if the page number of your front end catalog page is 9, your Home link property should be set to the following:

    f?p=&APP_ID.:9:&SESSION.
  4. Save and apply your changes. Now run your application by clicking the Run Application icon. The application will now redirect you to the front end catalog page.

How It Works

As mentioned in Chapter 9, a typical link to a page in APEX has the following format:

f?p=(ApplicationID):(PageNumber):(SessionID):::::

This recipe shows you the &APP_ID. and &SESSION. substitution tags to dynamically place the application ID and session identifier respectively in the final generated link. In this recipe, the page number (id: 9) is hardcoded in the link.

Creating the Stockcount Report for the Storekeeper

Problem

The storekeeper's boss wants to see a visual report (pie chart) of the number of books (stockcount) by genre.

Solution

To create the stockcount pie chart, please follow these instructions:

  1. In the same Book catalog application, create a new page.

  2. Choose the Chart page type, and in the next step of the wizard, opt for the Flash Chart type.

  3. Choose the Pie & Doughnut chart type, as shown in Figure 10-24.

    The Pie & Doughnut chart type

    Figure 10.24. The Pie & Doughnut chart type

  4. Choose the 3D Pie chart type in the next step of the wizard, and for the page name and chart title, enter "Stockcount by Genre".

  5. When you are prompted to enter the SQL data source for the chart, enter the following SQL:

    SELECT NULL LINK,
            Category.CategoryName LABEL,
            SUM(Inventory.CopiesInStock) VALUE
    FROM Category,Books,Inventory WHERE Category.CategoryID= Books.BookCategory AND
    Books.BookID=Inventory.BookID GROUP BY Category.CategoryName
  6. You should now have the screen shown in Figure 10-25.

    Defining the SQL datasource for the chart

    Figure 10.25. Defining the SQL datasource for the chart

  7. Complete the wizard and run the page. You should see the total number of books instantly computed by genre, as shown in Figure 10-26.

    The stockcount chart in action

    Figure 10.26. The stockcount chart in action

How It Works

As you saw earlier in this book, you can easily generate a variety of visual charts from an SQL statement. In this recipe's example, you combine information from three different tables to retrieve the sum of the stockcount for each different book category.

Setting Up the Central Administration Page

Problem

You now have various pages, but they're all in pieces. You need to provide a central Administration page from which the storekeeper can navigate to the various administration pages.

Solution

First, you must set up a navigation list. To do so, please follow these steps:

  1. In the same Book catalog application, click the Shared Components icon.

  2. In the Navigation area, click the Lists link shown in Figure 10-27.

    The Lists link

    Figure 10.27. The Lists link

  3. Create a new navigation list by clicking the Create button in the top right corner.

  4. In the first step of the wizard, name the navigation list as "AdministrationOptions".

  5. In the next step of the wizard, create the following four entries:

    • Book Categories

    • Inventory & Stockcount

    • Books Listing

    • Stockcount by Genre report

  6. Tie each item to their respective report pages by clicking the little arrow icon next to each Target Page ID or Custom URL box, and choosing the correct page to link to from the pop-up window. This is illustrated in Figure 10-28.

    Setting up a navigation list

    Figure 10.28. Setting up a navigation list

  7. When you are done, complete the wizard to create the list.

Now that you've created the navigation list, you need to host it inside a page—the Central Administration page. To set up this page, please follow these steps:

  1. In the same Book catalog application, create a new blank page.

  2. Specify "Administration" as the page name.

  3. Complete the rest of the steps of the wizard using the default settings to create a blank page.

  4. Choose to Edit the page.

  5. In the Page Rendering area, right click on the Regions node, and choose to Create a new region.

  6. Choose the List region type, as shown in Figure 10-29.

    Creating a List region

    Figure 10.29. Creating a List region

  7. Set "Please pick an option below" as the title of the region.

  8. In the Source step of the wizard, choose the AdministrationOptions list you created earlier, as shown in Figure 10-30.

    Choosing the AdministrationOptions list for the region

    Figure 10.30. Choosing the AdministrationOptions list for the region

  9. Click the Create List Region button to complete the wizard.

  10. Now run the page. You should see the various links to the various administration pages, as shown in Figure 10-31. Try clicking each link to ensure you land at the correct page.

    The Central Administration page in action

    Figure 10.31. The Central Administration page in action

How It Works

Navigation Lists provide an easy way to setup a bunch of links that can be displayed as is (as shown in this recipe), or even hosted within a drop-down menu.

There is actually a side-benefit to setting up navigation lists. Once it is set up, it can be reused many times in an application. This can lead to easier maintenance of your application in the future. For instance, when a link is to be dropped, you don't have to go through every place in your application looking to delete that link. You could just remove it from the navigation list, and all areas that use the navigation list will be instantly updated.

Creating Tabs in Your Application

Problem

You have a Central Administration page—that's fine. But how do you navigate there from your main catalog page (which is the front end catalog page). You realize you need tabs.

Solution

To create tabs in your application, follow these steps:

  1. In the same Book catalog application, click the Shared Components icon.

  2. Under the Navigation area, click the Tabs link shown in Figure 10-32.

    The Tabs link in the Navigation area

    Figure 10.32. The Tabs link in the Navigation area

  3. Click on the Manage Tabs button in the top right corner of the ensuing page.

  4. Click the Edit Standard Tabs tab.

  5. Remove any and all existing tabs from this list (if an entry exists, click the Edit icon, and then click the Delete icon on the following page).

  6. You should now see the screen shown in Figure 10-33.

    Removing all standard tabs

    Figure 10.33. Removing all standard tabs

  7. Now click the Manage Tabs tab, and click the little Add button highlighted in Figure 10-34.

    Adding a new tab

    Figure 10.34. Adding a new tab

  8. Specify "Book catalogTabs" for the new tabset name, and skip the next few steps until you arrive at the "Tab Name" step of the wizard.

  9. Specify "Store" as the tab label, as shown in Figure 10-35.

    Creating the Store tab

    Figure 10.35. Creating the Store tab

  10. Click the Next button. Now you'll be able to specify the page that is associated with this tab. Choose the My Mini Book catalog page by clicking the arrow highlighted in Figure 10-36.

    Choosing the associated page for the tab

    Figure 10.36. Choosing the associated page for the tab

  11. Complete the rest of the wizard using the default settings provided.

  12. You should see your newly created tab displayed in the screen shown in Figure 10-37.

    The newly created Store tab

    Figure 10.37. The newly created Store tab

  13. Add another tab, and label it "Administration".

  14. When you are prompted to provide the page associated with this tab, choose the Central Administration ("Administration") page.

  15. Complete the rest of the wizard using the default settings.

  16. Now run your application again. You will see two tabs at the top of your application, as shown in Figure 10-38. One will bring you to the catalog front end page and the other will bring you to the Central Administration page.

    The final book catalog site with tabs

    Figure 10.38. The final book catalog site with tabs

How It Works

APEX takes away most of the work of visual representation of a tab from the developer. Once you associate a tab with a particular page, APEX will automatically highlight the tab for you (when you are on the page) and unhighlight the other tabs.

Tabs, like navigation lists, are also reusable throughout the application and provide the same benefit of easier maintenance: when a new tab needs to be added, just add it to an existing tabset, and voila! The new tab will instantly appear on every page in your application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset