Sunday, September 20, 2009

How to turn on Assertion in jdeveloper

While writing code, we might make certain assumptions.

Say, our govt runs a loan program, where everyone who request for a loan, would at least get something. There would never be a case, when people return empty handed..

public Long fulFillWish() {
/*depending on business rules, give loans..however since this is govt..everyone would get something.*/
if(retunAmt <> Run/Debug/Profile
Edit the default Run Configuration


Tuesday, September 1, 2009

Creating a Web Service From Java Class

The example below makes use of the latest Oracle JDeveloper 11g available for download from here.

Find below the step by step process to create a web service from the java class and how to test it using the integrated weblogic server that comes bundled with the JDeveloper 11g. This approach is also known as Bottom Up approach.

# Open your JDeveloper


# Create a new application and lets name it as MyDateWebService


# Name the project as DateService and shuttle across the Project technologies that we intend to use. Here, I select Web Services. The dependent Java technology gets selected of its own.


# Once Finish button is pressed, the view of the Jdeveloper looks like as depicted below


# Within this newly created project, we create a new java class by doing right click on the project and selecting 'New' - 'New Gallery' window pops open


# Name the java file as 'CurrentDate'


# Modify the newly created java file and add method which needs to be exposed as webservice. Here, I have created getDate() method that returns a string value of the current datetime. Import the needed Date class as shown.


# Now, our class is ready to be exposed as web service. Right click on the java class and in the window that pops open, select 'Create Web Service'


# The Create Web Service process starts. In the step 2, choose Java EE 1.5, with support for JAX-WS Annotations


# In Step3, modify the name of the web service to 'CurrentDateWS'



# Step 5, confirm the methods that need to be exposed as web Service. Press Finish here as we are fine with the default settings in other steps.





# Observe the annotation change in the java class


# Now, our web service is all ready to be tested. Right Click on the java class and choose 'test Web Service'




# This opens the HTTP Analyser window as shown below. Press Send request button.


# As we see here in the window below, the HTTP Analyser presents below the response from the deployed web service.

# Double click on the WSDL URL present in the HTTP Analyser to view the WSDL associated with our web Service as shown below
This ends our simple exercise to create a web service from Java class.

ADF: Transaction Level Validation

In ADF-BC4J, we can declaratively define validations. Validations can be at
1. Attribute Level
2. Entity Level
3. Transaction Level

In this post, we are going to see How to execute transaction level validation. As compared to entity level validation,which fires once for each entity, a transaction level validation is fired once for ALL entities.

Lets take a use case.
Say my rule is, There should be only one clerk per Dept.
Or
A customer can have multiple Address associated to it. but only one address can be primary address.

Such validation, should be at Transaction Level.

Lets say you are creating employees.
You add a employee "MARK" in dept 90, as "ST_CLERK"
As soon as row looses focus, or you create a new row, Entity level valdiations would fire.
Now add another employee "JOE" in dept 90, as "ST_CLERK", tab out. Again entity level validations would fire.

Now if you commit, Transaction level validation would fire. This gives us a chance to throw an error to user, that You cannot add more then One Clerk in a Dept.

If we had created this validation at Entity level instead, unnecessarily the validation would have fired twice.


Now lets look at the syntax of Transaction Level validation.

Following code is in EOImpl

public boolean validateSingleClerkinDept(ArrayList ctxList) {
System.out.println("in method validator");
int noOfClerks = 0;
boolean isValid = true;
for (int i = 0; i < ctxList.size(); i ) {
JboValidatorContext ctx = (JboValidatorContext)ctxList.get(i);
EmployeesEOImpl eorow = (EmployeesEOImpl)ctx.getSourceRow();
if(eorow.getJobId().equals("ST_CLERK")){
System.out.println("validating for " eorow.getFirstName());
RowSet rowset = eorow.getEmpVVO1();
ViewObject vo = rowset.getViewObject();
vo.setWhereClause("JOB_Id = :pJobId AND DEPARTMENT_ID = :pDeptId");
vo.defineNamedWhereClauseParam("pJobId", null, null);
vo.defineNamedWhereClauseParam("pDeptId", null, null);
vo.setNamedWhereClauseParam("pJobId", eorow.getJobId());
vo.setNamedWhereClauseParam("pDeptId", eorow.getDepartmentId());
rowset.getViewObject().setQueryMode(ViewObject.QUERY_MODE_SCAN_VIEW_ROWS |
ViewObject.QUERY_MODE_SCAN_DATABASE_TABLES |
ViewObject.QUERY_MODE_SCAN_ENTITY_ROWS);
rowset.executeQuery();
if (rowset.hasNext()) {
noOfClerks ;

}
if(noOfClerks > 1) {
isValid = false;
break;

}
}
}
return isValid;
}


We are all set with as per as back end is concerned.

Test from AM. Create "Mark " and "JOE" as above, and only when you commit you'll see the error message.

Notice that method validator is only called once, for Both new rows that were created.

Creating a WSDL File From XSD File

In this post, lets learn how we can create services interface - WSDL from schema file using Oracle JDeveloper 11g. Here, we attempt to create a service which when provided with an Employee Id returns the Employee Name. Incase no matching records are found for an Id, it simply returns the string value - "Unknown".


# We start with opening the JDeveloper 11g


# Create an application - "MyWSDLApplication" using the Generic Application template.


# Name the project as "MyWSDLProject" and select "Web Services" as the project technologies.


# The Application Navigator view should look like this


# Lets create the schema file first. For this, right click on the project and from the context menu, select New. In the Gallery that opens, select General -> XML -> XML Schema


# Name the schema as Employee.xsd

# The schema editor window opens as shown in the figure below.

# We will be adding two elements - EmpId and EmpName here. Right click on the root schema element and from the context menu, select Insert inside schema -> element

# Once the two elements are specified, we provide them types by right click on the element and from context menu - select Set Type as shown below

# As depicted in the figure below, EmpId type is taken as long while EmpName type is string.

# With the schema defined, we move onto creating the WSDL document. Right click on the project and from context Menu, select New. In the gallery, choose Business Tier -> Web Services -> WSDL Document. Press OK button.
# Name WSDL document as EmployeeWSDL


# Once OK button is presses, the JDeveloper editor view would look like this

# Arrange the schema and WSDL editor windows in a vertical split arrangement like as shown below. This can be achieved by dragging the Employee.xsd tab below.


# Now, we begin creating the WSDL document. Drag and Drop the EmpId element (input for our case) onto the Port Type window. This opens the 'Create Port Type' window. Lets name it as EmployeeDetail.

# After our previous action, the WSDL editor should look like this

# We have a problem here as can be seen from the image. The output is shown as EmpId while we expect it to be EmpName.

# To correct this, we select the output node in the Port Types window. Drag and Drop EmpName element from the schema onto this node. With this, the WSDL document appears like

# Next we change the name of the messages to EmployeeRequestMessage and EmployeeResponseMessage using the property inspector window.
# With the changes in message name, we need to remap these to the operation messages as else these appear with errorneous icon like we have output icon below. To correct this, drag and drop the relevant messages onto the correct nodes. Example : EmployeeRequestMessage is dropped onto the input node of EmpIdOperation and EmployeeResponseMessage is dropped onto the output of EmpIdOperation


# Here, we make the operation name more meaningful using property Inspector - getEmployeeNameOperation.

# The complete WSDL view should appear like this

# Now, we drag and drop the EmployeeDetail node onto the Bindings/Partner Link Types window. In the Create Binding window, we check the SOAP12.


# This should result in the view as shown below :

# Next, drag and drop the root node from the Bindings window onto the Services window. This results in the view as shown below

# At this point, the structure window should look like this. This completes our WSDL document creation process.


# We can validate our WSDL document by doing right click on the EmployeeWSDL (in the Applications Navigator Window) and from context menu, choose Validate WSDL. Confirm the message in the Messages- Log window.


This completes our exercise to create WSDL document using the schema file. Next, we will create Web Service using this WSDL document.

What is a Web Service ?


Before we explore about web services, lets pay attention to the lay man definition for the term - 'Service'. As I look for its definition in my Soft copy of Wordweb, it displays the Noun form of 'Service' as 'Work done by one person or group that benefits another'. Now, in programming terms - I can safely fit this definition as an application or subprogram that executes a unit a work as part of a business process implementation.

To standardise this definition furtherwe say that the service has got some characteristics as

- Loosely coupled. The service provider and consumer are loosely bound with each other. So, in a way - the consumer can easily shift to any other provider without issues.
- Separation of concerns so as to have an encapsulation of implementations. What would this mean > Ah... Well here, it means that as long as the interface (discussed below) remains constant, consumer or provider of service can change their implementation patterns without forcing the other party to make corresponding changes.
- Now, the common question that may strike us at this time is how would a consumer know about how to call this service made available by the provider. For exactly this reason, there is a service contract defined by the interface that the associated service implementation has to honour. WSDL (Web Services Definition Language) provides the standard way to expose the interface definition and the contract (operations performed).

I guess, we are ready to define Web Services now :
A web service is a discrete, reusable software component that is accessed programmatically over the Internet to return a response. Interaction with a Web service is based on a set of standard protocols and technologies called Web services. The term Web Services describes a standardized way of integrating Web-based applications using the open standards.

For Web Services, data and functionality is exchanged in XML format, SOAP [Simple Object Applications Protocol] is used to transfer the data, WSDL is used for describing the services available and UDDI is used for listing what services are available (like a telephone directory listing the names).

More on SOAP, WSDL and UDDI in next post. ;)