18-Jul-06 (Created: 18-Jul-06) | More in 'Howto'

05.06 How to do updates in Aspire

How to do updates in Aspire

Updates are quite straight forward in Aspire. An update is initiated by a user submitting a form. A form can be submitted using either a get method or a post method. Or you can even use a javascript function to collect the relevent fields and fire of a URL to the server along with the parameters.

Structure of the update URL

Either way all of the suggested methods will conceptually resolve to sending a url to the server side with parameters. By knowing this structure you can understand the update process in Aspire better

http://your-host:yourport/your-webapp/servlet/UpdateServlet?request_name=UpdateRequest&arg1=10&arg2=20 etc.

The target of an update request in Aspire is the "UpdateServlet". This UpdateServlet requires one mandatory parameter called "request_name". This parameter or argument points to a symbolic name in the configuration file. In this example this symbolic name is "UpdateRequest". The rest of the arguments are optional.

Based on the symbolic name for the update request, Aspire will kick off things based on how this symbolic name is defined in a configuration file. Let us take a look at this definition.

UpdateRequest configuration definition


#*****************************************************
#Have a business class responsible for this request
#*****************************************************
request.updateRequest.classname=com.ai.db.DBRequestExecutor2
request.updateRequest.db=(my-database-name)
request.updateRequest.query_type=update
request.updateRequest.stmt=\
insert into table1 (a,b,c) values ('literal',{arg1},{arg2.quote})

#*****************************************************
#Based on sucess or failure redirect the user to a display page
#*****************************************************
request.updateRequest.redirectURL=\
/your-webapp/servlet/DisplayServlet?url=Page1&arg1={any-key}

request.updateRequest.failureRedirectURL=\
/your-webapp/servlet/DisplayServlet?url=Page1&arg1={any-key}&result=error

The above definition allows Aspire to do the following. Invoked by Aspire "com.ai.db.DBRequestExecutor2" will execute the insert statement on the specified database. The values from the incoming url are accordingly substituted into the insert statement before execution. These arguments can be literals, integers, or string representations. Once this statement is executed Aspire will commit or roleback based on an exception. If there is no exception, then the results are commited and the user redirected to the URL specified by the "redirectURL".If there is an exception then the results are rolled back and the user redirected to the failureRedirectURL. There is a sophisticated redirection mechanism availabe but not discussed here as I am focusssing on the simple case to aid understanding in the first pass. Values from the URL can be used in the redirect urls using Aspire substitution syntax. That is all there is to it

Role of business logic in updates

The case above is a specialized case where the business logic is a pre-fabricated part that knows how to execute SQL statements. These pre-fabricated parts saves you writing any Java code when appplicable. Some relevent pre-fabricated business parts are:

  • com.ai.db.DBRequestExecutor2 (executes SQL, or regular non-oracle styled stored procs)
  • com.ai.db.StoredProcExecutor2 (executes Oracle Stored Procs)
  • com.ai.db.PreTranslateArgsMultiRequestExecutor (Knows how to call other parts in a pipeline like fashion)
  • com.ai.mail.MailRequestExecutor (knows how to send emails)

When these parts does not satisfy the update need you can create your own part. Let us call this part mycom.mypkg.MyPart. Then you can change the updateRequest definition as follows:


#*****************************************************
#Have a business class responsible for this request
#*****************************************************
request.updateRequest.classname=mycom.mypkg.MyPart

#*****************************************************
#Based on sucess or failure redirect the user to a display page
#*****************************************************
request.updateRequest.redirectURL=\
/your-webapp/servlet/DisplayServlet?url=Page1&arg1={any-key}

request.updateRequest.failureRedirectURL=\
/your-webapp/servlet/DisplayServlet?url=Page1&arg1={any-key}&result=error

See how the control is transferred over to your business component. It is upto your component to do whatever is necessary. Essentially your part will receive a Map of input values and what you do with them is upto you. Hidden in the hashtable some reserved values such as request, response, session, and may be connection under some circumstances

How to kickoff an update pipeline for multiple updates

More than often you may need to update more than one table in response to a single incoming URL. And sometimes you may have to retrieve additional values from the database before an update. Such things are possible using a pre-fabricated part called PreTranslateArgsMultiRequestExecutor. Here is how you use this part to start an update pipeline.


#*****************************************************
#Have a business class responsible for this request
#*****************************************************
request.updateRequest.classname=com.ai.db.PreTranslateArgsMultiRequestExecutor
request.updateRequest.db=(my-database-name)
request.updateRequest.query_type=update
request.updateRequest.request.1=Update1
request.updateRequest.request.2=Update2

#*****************************************************
#Based on sucess or failure redirect the user to a display page
#*****************************************************
request.updateRequest.redirectURL=\
/your-webapp/servlet/DisplayServlet?url=Page1&arg1={any-key}
request.updateRequest.failureRedirectURL=\
/your-webapp/servlet/DisplayServlet?url=Page1&arg1={any-key}&result=error

#*****************************************************
#Define individual requests
#*****************************************************

request.Update1.classname=com.ai.db.DBRequestExecutor2
request.Update1.db=(my-database-name)
request.Update1.query_type=update
request.Update1.stmt=\
insert into table1 (a,b,c) values ('literal',{arg1},{arg2.quote})


request.Update2.classname=com.ai.db.DBRequestExecutor2
request.Update2.db=(my-database-name)
request.Update2.query_type=update
request.Update2.stmt=\
insert into table1 (a,b,c) values ('literal',{arg1},{arg2.quote})

You can also use selects in this pipeline. The output from the select statements can be used downstream to mine values for those update statements.

Relevent classes where this functionality is implemented

  • com.ai.servlets.RequestExecutorServlet
  • com.ai.servlets.DefaultExceptionAnalyzer

Further reading

  • How to paint or display as opposed to update web pages using Aspire.
  • How to redirect users based on exceptions during Aspire updates
  • How to writer parts in Aspire
  • How to use Aspire for data access
  • How to execute multiple SQL statements in Aspire
  • How to use pipelines in Aspire