October 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Building Web Applications with Oracle HTML DB, Part 2

  • August 25, 2004
  • By Michael Klaene
  • Send Email »
  • More Articles »

Adding JavaScript to the mix

Many Web application developers like to augment the functionality of their systems with client-side scripting. Oracle HTML DB supports JavaScript, which can be very useful when trying to compensate for a Web browser's stateless behavior. It is possible to respond to user actions such as mouse clicks and inter-field navigation. Probably most important, JavaScript can help avoid uneccessary requests made against the Web server, thereby improving application responsiveness.

There really are no limits to using JavaScript within HTML DB. The following steps will demonstrate just one way developers use JavaScript to improve their applications. HTML DB adds several standard buttons to wizard-generated forms. On the employee update form, one of these buttons will delete the current record. An unintentional delete from a production database can have serious consequences, so we might want to verify the user's decision before actually doing the deed. HTML DB provides several JavaScript functions. We will use one here to add a confirmation before sending a 'delete' request to the server.

  • In your application, navigate to the form that updates employee information.
  • Click Show Edit Links from the bottom of the page. Click the edit option on the delete button.
  • Scroll down. In the Target is a field, select the 'URL' option.
  • In the URL Target field, enter the following JavaScript code:
  • javascript:confirmDelete('Do you really want to delete this
                              Employee record?');
    
  • Click Apply Changes; then, run the page.

When deleting a record from the employee table, a user will first need to confirm their choice. It is also possible to perform client-side edits with JavaScript; however, it should be noted that Oracle HTML DB allows you to add item validations in a simple, wizard-like fashion. Using the declaritive approach to validation with HTML DB is quick and easy. When violations occur during data entry, HTML DB can use JavaScript behind the scenes to present messages to the user.

Adding complex validations

Sometimes, even the simplest of applications requires validation processing that is more complex in nature. For example, if you are entering data into the database, SQL lookups may be required in some validation code to ensure that certain business rules are enforced. Oracle HTML DB allows you to write custom PL/SQL code so developers can customize as necessary. Our first example uses PL/SQL to check the value of multiple fields in a form.

  • Click Show Edit Page from the bottom of the employee update page.
  • In the Page Processing section, under Validations, click Create.
  • For Name, type 'CHECK_JOB_AGAINST_SALARY'. Leave Validation Type as 'PL/SQL Expression'. Click Next.
  • In the Type field, select 'Function Returning Error Text'.
  • In Validation, enter the following PL/SQL code:
  • BEGIN
       --Only Presidents can make the big bucks:
       IF :P4_JOB <> 'PRESIDENT'  AND :P4_SAL > 75000 THEN
           RETURN 'Only the President can make this much money!!!!';
       ELSE
           RETURN NULL;
       END IF;
    EXCEPTION
         WHEN OTHERS THEN 
             RETURN SQLERRM;
    END;
    
  • In Error Message, enter 'Only the President can make this much money!!!!'
  • Select 'P4_SAL' as the Associated Item.
  • Save these changes and run the page.

Attempt to update an employee who is not the 'President' by giving them a salary greater than $75,000. When you do so, this validation code will execute and the error will be displayed next to the salary field.

As mentioned previously, you may be required to access the database during form validation. Add another validation by the name of 'CHECK_FOR_MULTIPLE_MANAGERS' that enforces a business rule that states for any given department, there can be at most one manager. Here is the PL/SQL validation code we will use:

DECLARE
   mgr_cnt PLS_INTEGER := 0;
BEGIN
   --Only 1 Manager per department:
   IF :P4_JOB = 'MANAGER' THEN

   SELECT COUNT(1)
      INTO mgr_cnt
      FROM emp
      WHERE deptno = :P4_DEPTNO
      AND empno <> :P4_EMPNO
      AND job = 'MANAGER';

      IF mgr_cnt > 0 THEN
          RETURN 'Department '||:P4_DNAME||' already has a
                  Manager!!!!';
      END IF;
   END IF; 

   RETURN NULL;

EXCEPTION
   WHEN OTHERS THEN
      RETURN SQLERRM;
 END;

In our application, there is a department called 'Research'. In this department, employee Ford has the job title of 'Manager' and employee Jones is an 'Analyst'. According to business rules, Jones can't be promoted to Manager while Ford already holds the title of Manager. Figure 2 shows the results of attempting to do so: a repopulated form with a descriptive error message.



Click here for a larger image.

Figure 2: Update Failure on Employee Form

Conclusion

Hopefully, these articles have demonstrated how to construct an HTML DB application with functionality that most business applications require. It is not possible for me to step through all of the functionality that Oracle HTML DB offers. Experimentation is the best way to learn how this tool can help you build robust web-enabled applications, in a minimal amount of time. Visit Oracle's HTML DB site for more information. There, you will find, among other things, a fairly thick User's Guide that will serve as a nice reference while you work.

About the Author

Michael Klaene is a Senior Consultant with Sogeti LLC. He has spent over 9 years in Information Technology and is an experienced Systems Analyst, delivering solutions that involve numerous technologies, such as J2EE and .NET.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel