Tuesday, March 20, 2012

Creating PDF document in JAVA using itext library

The java code to create a sample pdf document using itext.

Jar file required: itextpdf.jar

package itext;

import java.io.FileOutputStream;
import java.util.Date;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

public class SamplePDF {
private static String FILE = "c:/temp/SamplePdf.pdf";
private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
Font.BOLD);
private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
Font.NORMAL, BaseColor.RED);
private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
Font.BOLD);
private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
Font.BOLD);

public static void main(String[] args) {
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
addTitlePage(document);
addContent(document);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}



private static void addTitlePage(Document document)
throws DocumentException {
Paragraph preface = new Paragraph();
addEmptyLine(preface, 1);
preface.add(new Paragraph("Sample Document", catFont));
addEmptyLine(preface, 1);
// File properties
preface.add(new Paragraph(
"Report generated by: " + System.getProperty("user.name") + ", " + new Date(), smallBold));
addEmptyLine(preface, 3);
preface.add(new Paragraph(
"This is a sample document ceated by itext ",
smallBold));

addEmptyLine(preface, 8);


document.add(preface);
// Start a new page
document.newPage();
}

private static void addContent(Document document) throws DocumentException {
Anchor anchor = new Anchor("First Chapter", catFont);
anchor.setName("First Chapter");

// Second parameter is the number of the chapter
Chapter catPart = new Chapter(new Paragraph(anchor), 1);

Paragraph subPara = new Paragraph("Subcategory 1", subFont);
Section subCatPart = catPart.addSection(subPara);
subCatPart.add(new Paragraph("Hello"));

subPara = new Paragraph("Subcategory 2", subFont);
subCatPart = catPart.addSection(subPara);
subCatPart.add(new Paragraph("Paragraph 1"));
subCatPart.add(new Paragraph("Paragraph 2"));
subCatPart.add(new Paragraph("Paragraph 3"));

// Add a list
createList(subCatPart);
Paragraph paragraph = new Paragraph();
addEmptyLine(paragraph, 5);
subCatPart.add(paragraph);

// Add a table
createTable(subCatPart);

// Now add all this to the document
document.add(catPart);

// Next section
anchor = new Anchor("Second Chapter", catFont);
anchor.setName("Second Chapter");

// Second parameter is the number of the chapter
catPart = new Chapter(new Paragraph(anchor), 1);

subPara = new Paragraph("Subcategory", subFont);
subCatPart = catPart.addSection(subPara);
subCatPart.add(new Paragraph("This is a very important message"));

// Now add all this to the document
document.add(catPart);

}

private static void createTable(Section subCatPart)
throws BadElementException {
PdfPTable table = new PdfPTable(3);


PdfPCell c1 = new PdfPCell(new Phrase("Table Header 1"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);

c1 = new PdfPCell(new Phrase("Table Header 2"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);

c1 = new PdfPCell(new Phrase("Table Header 3"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);

table.addCell("1.0");
table.addCell("1.1");
table.addCell("1.2");
table.addCell("2.1");
table.addCell("2.2");
table.addCell("2.3");

subCatPart.add(table);

}

private static void createList(Section subCatPart) {
List list = new List(true, false, 10);
list.add(new ListItem("First point"));
list.add(new ListItem("Second point"));
list.add(new ListItem("Third point"));
subCatPart.add(list);
}

private static void addEmptyLine(Paragraph paragraph, int number) {
for (int i = 0; i < number; i++) {
paragraph.add(new Paragraph(" "));
}
}
}


Oracle SOA Suite 11g - Domain Value Map Qualifier Support

Oracle SOA Suite 11g - Domain Value Map Qualifier Support:

Qualifier support is the new feature in Oracle SOA Suite 11g.Qualifiers qualifies mappings. A mapping may not be valid unless qualified with additional information. For example, a domain value map containing a city code-to-city name mapping may have multiple mappings from KN to Kensington because Kensington is a city in both Canada and the USA. Therefore, this mapping requires a qualifier (USA or Canada) to qualify when the mapping becomes valid

Country (Qualifier)CityCodeCityName
USABOBoston

USA
BELG_NC
Belgrade
USA
BELG_MN_Streams
Belgrade
USA
NP
Northport
USA
KN
Kensington
Canada
KN
Kensington


You can also specify multiple qualifiers for a domain value map

Country (Qualifier)State (Qualifier)CityCodeCityName
USAMassachusettsBOBoston
USANorth CarolinaBELGBelgrade
USAMinnesotaBELGBelgrade
USAAlabamaNPNorthport
USAKNKansasKensington
CanadaPrince Edward IslandKNKensington

A qualifier order is used to find the best match during lookup at runtime. The order of a qualifier varies from highest to lowest depending on the role of the qualifier in defining a more exact match. In above Table, the state qualifier can have a higher order than the country qualifier, as a matching state indicates a more exact match.

Domain value maps support hierarchical lookup. If you specify a qualifier value during a lookup and no exact match is found, then the lookup mechanism tries to find a more generalized match by setting the higher order qualifiers to a "". It proceeds until a match is found, or until a match is not found with all qualifiers set to a ""

State=Arkansas, Country=Canada, CityCode=KN_USA
In this example, the State qualifier has a qualifier value of 1 and the Country qualifier has a qualifier value of 2.
the lookup mechanism sets the higher order qualifier State to the exact lookup value Arkansas and uses Canada"" for the lower order qualifier Country.

When no match is found, the lookup mechanism sets the higher order qualifier State to a value of "" and sets the next higher qualifier Country to an exact value of Canada.
When no match is found, the lookup mechanism sets the value of the previous higher order qualifier Country to a value of "". One matching row is found where CityCode is KN_USA and Kensington is returned as a value.
Domain Value Map Lookup Result

StateCountryShort ValueLookup Result
ArkansasCANADA" "KN_USANo Match
“”CANADAKN_USANo Match
“”“”KN_USAKensington


DVM Lookup function to use the Qualifier
dvm:lookupValue(dvmMetadataURI as string, SourceColumnName as string,SourceValue as string, TargetColumnName as string, DefaultValue as string,(QualifierSourceColumn as string, QualifierSourceValue as string)*) as string
e.g
dvm:lookupValue ('cityMap.dvm','CityCodes','BO','CityNames', 'CouldNotBeFound', 'State', 'Massachusetts')


Wednesday, March 14, 2012

how to add namespace Prefix to all the elements of JAX-WS webservice output

This post will explain, how to add namespace Prefix to all the elements of JAX-WS webservice output

Sometimes we may required to have the namespace prefix (ex.ns1) defined for all the elments of the JAX-WS webservice output.

The default annotation of the package-info.java class looks like below

@javax.xml.bind.annotation.XmlSchema(namespace = "PPDS:OPIRequest", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.reuters.eai.types;

This case only the parent element will be have the namespace prefix but not the child elements.

eg.

<ns1:Employee ns1="PPDS:OPIRequest">
<name></name>
<number></number>
</ns1:Employee>


To have the namespace prefix in all the output elements,modify the above annotaion in the package-info.java class as mentioned below.


@XmlSchema( namespace = "PPDS:OPIRequest", elementFormDefault = XmlNsForm.QUALIFIED, xmlns={@XmlNs(prefix="ns3", namespaceURI="PPDS:OPIRequest")})
package com.reuters.eai.types;
import javax.xml.bind.annotation.*;


e.g

<ns1:Employee ns1="PPDS:OPIRequest">
<ns1:name></ns1:name>
<ns1:number></ns1:number>
</ns1:Employee>


Friday, March 9, 2012

Oracle SOA Suite 11g – Utility to manage the composite revisions.

Oracle SOA Suite 11g – Utility to manage the composite revisions:

In our project we had a requirement to retire/undeploy the older versions of composites and keep the specified number of newer versions. We have developed a java utility to do this functionality.
Thought of sharing the same, it may help someone looking for the similar utility.
If we have more number of deployed composite, it will increase the server startup time and also the loading time of the em console, its better we can keep only the latest versions and retire or undeploy the older versions.
This java utility will keep the specified number of latest versions (based on the user input) and undeploy/retire all the other older versions based on the operation specified by the user. The default version will be skipped from undeploy/retire operation.
This utility is developed for UNIX server; the same can be modified to work on windows.
Steps to execute:-
Copy ManageComposites.java and ManageComposites.xml (Refer the attachment) files to the server to a particular location.
Open the putty session; change the window scroll size of the putty session to a higher value (to increase the display size)
Set the following environment variables before executing the utility
ORACLE_HOME
MWHOME
WLSHOME
Execute the script - ant –f ManageComposites.xml
Enter the input for the server details, operation(R – retire, U - Undeploy) and the number of versions to be kept for a composite.
O/P
Undeploy the older versions of all the composites expect the last two versions.





Thursday, March 8, 2012

Dispalying the XML node as formatted string

Dispalying the XML node as formatted string:

import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Node;

public class TransformUtil {

public static String nodeToString(Node node) throws Exception{
StringWriter sw = new StringWriter();
try {
Transformer t = new org.apache.xalan.processor.TransformerFactoryImpl().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.transform(new DOMSource(node), new StreamResult(sw));
} catch (TransformerException te) {
System.out.println("nodeToString Transformer Exception");
throw te;
}
return sw.toString();
}

}


Wednesday, March 7, 2012

Oracle SOA Suite11g - Generic plan file to deploy the composites to different environments


Oracle SOA Suite11g - Generic plan file to deploy the composites to different environments:

The deployment plan file can be used to change the run time configurations like WSDL endpoint, value of the partner link properties and the host details across the environments.
The below approach will help you to have one tokenized deployment plan file (template plan file) and build.properties file for each environments. The tokens will be replaced at deployment time based on the details available in the build.properties file corresponds to the particular environment. The generated deployment file needs to be attached to the composite.
  • Create a template plan file with tokenized properties that needs to be replaced with the build.properties file data.

EAI_cfgplan_template.xml


<?xml version="1.0" encoding="UTF-8"?>
<SOAConfigPlan xmlns:jca="http://platform.integration.oracle/blocks/adapter/fw/metadata" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:orawsp="http://schemas.oracle.com/ws/2006/01/policy" xmlns:edl="http://schemas.oracle.com/events/edl" xmlns="http://schemas.oracle.com/soa/configplan">
<composite name="Test_Invoker | Test1_invoker">
<reference name="Test">
<binding type="*">
<property name="oracle.webservices.auth.password">
<replace>@[email protected]</replace>
</property>
<property name="oracle.webservices.auth.username">
<replace>@[email protected]</replace>
</property>
</binding>
</reference>
</composite>
<wsdlAndSchema name="Test1.wsdl|Test2.wsdl ">
<searchReplace>
<search>http://testserver:8000</search>
</searchReplace>
</wsdlAndSchema>
</SOAConfigPlan>



  • Create build property files for each environment

dev_build.properties
server.host= server1.com
server.port=7003
server.protocol=http
server.username=testuser
server.password=password
QA_build.properties
server.host= server1.com
server.port=7003
server.protocol=http
server.username=QAuser
server.password=password

  • Ant script to generate the actual plan files by filling the tokens in the template plan files from the environment property file.

GeneratePlanFile.xml

<?xml version="1.0" encoding="iso-8859-1"?>
<project name="GeneratePlanfile" default="generatePlanfile" basedir=".">
<target name="generatePlanfile">
<delete>
<fileset dir="${basedir}" includes="EAI_cfgplan.xml"/>
</delete>
<copy file="${basedir}/EAI_cfgplan_template.xml" tofile="EAI_cfgplan.xml">
<filterchain>
<filterreader classname="org.apache.tools.ant.filters.ReplaceTokens">
<param type="propertiesfile" value="${basedir}/${envType}_build.properties"/>
<param type="tokenchar" name="begintoken" value="@"/>
<param type="tokenchar" name="endtoken" value="@"/>
</filterreader>
</filterchain>
</copy>
</target>
</project>

  • Execute the ant script, this will generate the actual Plan file required as shown below

ant -DenvType=dev -f GeneratePlanFile.xml

EAI_cfgplan.xml

<?xml version="1.0" encoding="UTF-8"?>
<SOAConfigPlan xmlns:jca="http://platform.integration.oracle/blocks/adapter/fw/metadata" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:orawsp="http://schemas.oracle.com/ws/2006/01/policy" xmlns:edl="http://schemas.oracle.com/events/edl" xmlns="http://schemas.oracle.com/soa/configplan">
<composite name="Test_Invoker | Test1_invoker">
<reference name="Test">
<binding type="*">
<property name="oracle.webservices.auth.password">
<replace>password</replace>
</property>
<property name="oracle.webservices.auth.username">
<replace>testuser</replace>
</property>
</binding>
</reference>
</composite>
<wsdlAndSchema name="Test1.wsdl|Test2.wsdl ">
<searchReplace>
<search>http://testserver:8000</search>
<replace>http://server1.com/7003</replace>
</searchReplace>
</wsdlAndSchema>
</SOAConfigPlan>