Play with JCN (Java
compute node )
As the name spell it is the mix behaiour of java and compute
node
One of the most important question is why and when to use
JCN in IIB ToolKit.
Ans.This question can be better understood in the way you
have the requirement as the same work can be done with Compute node also than
why JCN ....
----it provies the flexibility.
----you can use the java code
free from applying mind in esql.
----Becouse java is inbuilt in
toolkit you can use the java inbuilt method.
----Higher use when u use
Collection (java.util) package i will
explain with example so that you can know
the horizon of java is unlimited but with compute node you have the
limited access.
------------------------------ let us having an interesting example using Collection
(TreeSet)------------------------
Set<String>
setSheetNames = new
TreeSet<String>();
String
SheetName = null;
MbElement
root = message.getRootElement();
java.util.List
record = (java.util.List)root.evaluateXPath(new
MbXPath("/db/sheets")); // DFDL tree
int
count = record.size();
MbElement
rec = root.getLastChild().getFirstChild().getFirstChild();
for(int
i=1; i<= count; i++)//storing the unique sheet names into Set object
{
SheetName=SheetName.trim();
SheetName
= rec.getFirstElementByPath("Spends").getValueAsString();
setSheetNames.add(SheetName);
rec =
rec.getNextSibling();
}
int countForSheets = setSheetNames.size();
Iterator itr = setSheetNames.iterator();
String newsheetname=null;
try{
while(itr.hasNext()) //Iterating unique sheet names and
creating unique sheets by using Set object
{
newsheetname
= (String) itr.next();
sheetdb
= (SXSSFSheet)workbook.createSheet(newsheetname);
Row
rowheadSheet = sheetdb.createRow((int) 0);// rowhead1
}
Are You really Feeling
interesting than we should go some little inside ..........
Now it is the question
Answer phase Not theory but only code
and practical :-
All
the ans will depends upon the tree u have :-
1.How to access DFDL element in JCN.
Ans:-
MbElement
root = message.getRootElement();
java.util.List record =
(java.util.List)root.evaluateXPath(new MbXPath("/db/sheets")); // DFDL
MbElement rec =
root.getLastChild().getFirstChild().getFirstChild();//According to above Figure
for(int
i=1; i<= count; i++) //For loop to access the element
from the tree
{
String SheetName
= rec.getFirstElementByPath("Spends").getValueAsString();
//Do
your required work ....................................
rec =
rec.getNextSibling();
}
2.How to access Local Environment
variables in JCN.
Ans:- String fname=(String)inAssembly.getLocalEnvironment().evaluateXPath("/File/Name");
3.How to access Environment
variables in JCN.
Ans:-
String rate
=
inAssembly.getGlobalEnvironment().getRootElement().getFirstElementByPath("RatePerJPMile").getValue().toString();
4.How to Throw and catch the
Exception.
Ans:-
1)
try{
//your work that may thorw some
Exception
}catch
(Exception e)
{
throw new
MbUserException(this,"evaluate()","","",e.toString(),null);
}
2) public void
evaluate(MbMessageAssembly inAssembly) throws MbException....
3)You can use try and catch block.
Remember after
passing from the JCN your Exception tree will be Affected.
5.How to generate the EXCEL Sheet
using JCN.
6.How to write and read from EXCEL
Sheet in JCN.
Ans:- This is
the Combine code for 5
and 6 for Generating EXCEL sheet in JCN with complete code
package abc.xyz
import
java.io.ByteArrayOutputStream;
import
java.text.DateFormatSymbols;
import
java.text.SimpleDateFormat;
import
java.util.Date;
import
java.util.Iterator;
import
java.util.Set;
import
java.util.TreeSet;
import
org.apache.poi.ss.usermodel.Cell;
import
org.apache.poi.ss.usermodel.Row;
import
org.apache.poi.ss.usermodel.Sheet;
import
org.apache.poi.xssf.streaming.SXSSFSheet;
import
org.apache.poi.xssf.streaming.SXSSFWorkbook;
import
com.ibm.broker.javacompute.MbJavaComputeNode;
import
com.ibm.broker.plugin.MbBLOB;
import
com.ibm.broker.plugin.MbElement;
import
com.ibm.broker.plugin.MbException;
import
com.ibm.broker.plugin.MbMessage;
import
com.ibm.broker.plugin.MbMessageAssembly;
import
com.ibm.broker.plugin.MbOutputTerminal;
import
com.ibm.broker.plugin.MbUserException;
import
com.ibm.broker.plugin.MbXPath;
public
class ABC_JavaCompute
extends MbJavaComputeNode {
public void
evaluate(MbMessageAssembly inAssembly) throws MbException {
MbOutputTerminal out = getOutputTerminal("out");
//String rate =
inAssembly.getGlobalEnvironment().getRootElement().getFirstElementByPath("Rate").getValue().toString();
MbMessage inMessage =
inAssembly.getMessage();
System.out.println(".................................................");
byte[] outdata =
createFile(inMessage);
System.out.println(".........................................");
MbMessageAssembly
outAssembly = null;
try {
MbMessage outMessage = new MbMessage();
copyMessageHeaders(inMessage,
outMessage);
outAssembly = new MbMessageAssembly(inAssembly, outMessage);
//String
fname=(String)inAssembly.getLocalEnvironment().evaluateXPath("/File/Name");
// Sending
the Output excel file data as a BLOB message
MbElement outRoot =
outMessage.getRootElement();
MbElement outParser =
outRoot.createElementAsLastChild(MbBLOB.PARSER_NAME);
outParser.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,
"BLOB", outdata);
//getting
the file name from Environment
//String
OutputFileName =
inAssembly.getGlobalEnvironment().getRootElement().getFirstElementByPath("OutputFileName").getValue().toString();
//OutputFileName
= OutputFileName.substring(0, OutputFileName.lastIndexOf('.'));
//
FilenameUtils.removeExtension();
//UPDATING
LOCAL ENVIRONMENT with Directory values and the File Name
MbElement outLocal=
outAssembly.getGlobalEnvironment().getRootElement();
MbElement destination =
outLocal.createElementAsLastChild(MbElement.TYPE_NAME,"OutFileName",
null);
SimpleDateFormat
datef = new SimpleDateFormat("MM");
String
monthName = datef.format(new Date());
int month =
Integer.parseInt(monthName);
String outputFileName = getMonth(month);
String
monthNameChar = outputFileName.substring(0, 3).toUpperCase();
SimpleDateFormat
year = new SimpleDateFormat("yy");
String
yearName = year.format(new Date());
destination.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"OutputFileName","FileName"+monthNameChar+yearName+".xlsx");
}
catch
(MbException e) {
// Re-throw
to allow Broker handling of MbException
throw e;
} catch (RuntimeException e) {
// Re-throw to allow Broker
handling of RuntimeException
throw e;
} catch
(Exception e) {
throw new MbUserException(this,
"evaluate()", "", "", e.toString(),null);
}
out.propagate(outAssembly);
System.out.println("exit from.................JCN");
}
public void
copyMessageHeaders(MbMessage inMessage, MbMessage outMessage)
throws MbException {
MbElement outRoot = outMessage.getRootElement();
MbElement header =
inMessage.getRootElement().getFirstChild();
while (header != null
&& header.getNextSibling() != null) // stop before
{
outRoot.addAsLastChild(header.copy());
header =
header.getNextSibling();
}
}
private String getMonth(int month)
{
return new
DateFormatSymbols().getMonths()[month-1];
}
private byte[] createFile(MbMessage message)
{
byte[] output = null;
try{
SXSSFWorkbook workbook = new
SXSSFWorkbook(10000000);
SXSSFSheet sheetdb = null;
Set<String>
setSheetNames = new
TreeSet<String>();
String SheetName = null;
MbElement root1 = message.getRootElement();
java.util.List record1 =
(java.util.List)root1.evaluateXPath(new MbXPath("/db/sheets"));
int count1
= record1.size();
MbElement rec1 =
root1.getLastChild().getFirstChild().getFirstChild();
for(int i=1; i<= count1; i++)//storing the unique sheet names into
Set object
{
SheetName =
rec1.getFirstElementByPath("Spends").getValueAsString();
SheetName=SheetName.trim();
setSheetNames.add(SheetName);
rec1 = rec1.getNextSibling();
}
int
countForSheets = setSheetNames.size();
Iterator
itr = setSheetNames.iterator();
String
newsheetname=null;
try{
while(itr.hasNext())//Iterating
unique sheet names and creating unique sheets by using Set object
{
newsheetname
= (String) itr.next();
sheetdb =
(SXSSFSheet)workbook.createSheet(newsheetname);
Row
rowheadSheet = sheetdb.createRow((int) 0);// rowhead1
createRowHead(sheetdb,
rowheadSheet);
}
}catch(Exception
e){}
root1=null;
rec1=null;
record1=null;
MbElement root = message.getRootElement();
java.util.List record = (java.util.List)root.evaluateXPath(new
MbXPath("/_Invoice/body"));
int count
= record.size();
MbElement rec = root.getLastChild().getFirstChild().getNextSibling().getFirstChild();
//This is
For sheet 1
SXSSFSheet
sheetdblast=null;
for (int i = 0; i < count; i++) {
String sheetNameUnique =
rec.getFirstElementByPath("sheetNameUnique").getValueAsString();
sheetdblast = (SXSSFSheet)
workbook.getSheet(sheetNameUnique);
int rowNumber = sheetdblast.getLastRowNum();
Row row =
sheetdblast.createRow((int)1+rowNumber);
Row rowForHeader = sheetdblast.getRow(0);
String
JPPercentage=null;
String
JetPercentage=null;
String
PartnerPercentage=null;
try{
JPPercentage
=rec.getFirstElementByPath("JPPercentage").getValueAsString();
}catch(Exception
e){}try{
JetPercentage=rec.getFirstElementByPath("JetPercentage").getValueAsString();
}catch(Exception
e){}try{
PartnerPercentage
=rec.getFirstElementByPath("PartnerPercentage").getValueAsString();
}catch(Exception
e){}try{
rowForHeader.getCell(7).setCellValue("BilledMiles
- JP "+"("+JPPercentage+"%)");
}catch(Exception
e){}try{
rowForHeader.getCell(8).setCellValue("BilledMiles
- 9W"+"("+JPPercentage+"%)");
}catch(Exception
e){}try{
rowForHeader.getCell(9).setCellValue("BilledMiles
- EBL"+"("+PartnerPercentage+"%)");
}catch(Exception
e){}
double
RateFinal=0.0;
double
TotalMiles=0.0;
double
JPMiles=0.0;
double
PartnerMiles=0.0;
double
JetMiles=0.0;
try{
String
MemberNumber=rec.getFirstElementByPath("JpNumber").getValueAsString();
double
MemberNumber123=Double.parseDouble(MemberNumber);
row.createCell(0).setCellValue(MemberNumber123);
}catch(Exception
e){}try{
row.createCell(1).setCellValue(rec.getFirstElementByPath("Name").getValueAsString());
}catch(Exception
e){}try{
row.createCell(2).setCellValue(rec.getFirstElementByPath("ActivityCode").getValueAsString());
}catch(Exception
e){}try{
String
Year=rec.getFirstElementByPath("Year").getValueAsString();
double Year123=Double.parseDouble(Year);
row.createCell(3).setCellValue(Year123);
}catch(Exception e){}try{
String
Month=rec.getFirstElementByPath("Month").getValueAsString();
double Month123=Double.parseDouble(Month);
row.createCell(4).setCellValue(Month123);
}catch(Exception e){}try{
String
Date=rec.getFirstElementByPath("Date").getValueAsString();
double Date123=Double.parseDouble(Date);
row.createCell(5).setCellValue(Date123);
}catch(Exception e){}try{
String TotalMiles123=rec.getFirstElementByPath("TotalMiles").getValueAsString();
TotalMiles=Double.parseDouble(TotalMiles123);
row.createCell(6).setCellValue(TotalMiles);
}catch(Exception
e){}
try{
String JPPercentage123=rec.getFirstElementByPath("JPPercentage").getValueAsString();
double
JPPercentageFinal=Double.parseDouble(JPPercentage123);
JPMiles=TotalMiles*JPPercentageFinal/100;
row.createCell(7).setCellValue(JPMiles);
}catch(Exception e){}
try{
String
JetPercentage123=rec.getFirstElementByPath("JetPercentage").getValueAsString();
double
JetPercentageFinal=Double.parseDouble(JetPercentage123);
JetMiles=TotalMiles*JetPercentageFinal/100;
row.createCell(8).setCellValue(JetMiles);
}catch(Exception
e){}
try{
String
PartnerPercentage123=rec.getFirstElementByPath("PartnerPercentage").getValueAsString();
double
PartnerPercentageFinal=Double.parseDouble(PartnerPercentage123);
PartnerMiles=TotalMiles*PartnerPercentageFinal/100;
row.createCell(9).setCellValue(PartnerMiles);
}catch(Exception
e){}
try{
String
Rate=rec.getFirstElementByPath("Rate").getValueAsString();
RateFinal=Double.parseDouble(Rate);
row.createCell(10).setCellValue(RateFinal);
}catch(Exception
e){}try{
row.createCell(11).setCellValue(rec.getFirstElementByPath("TransactionDate").getValueAsString());
}catch(Exception
e){}
try{
double
AmountJP=JPMiles*RateFinal;
row.createCell(12).setCellValue(AmountJP);
}catch(Exception
e){}
try{
double
AmountJET=JetMiles*RateFinal;
row.createCell(13).setCellValue(AmountJET);
}catch(Exception
e){}
try{
double
AmountEBL=PartnerMiles*RateFinal;
row.createCell(14).setCellValue(AmountEBL);
}catch(Exception
e){}
try{
row.createCell(15).setCellValue(rec.getFirstElementByPath("Description").getValueAsString());
}catch(Exception
e){}
rec = rec.getNextSibling();
}
root =null;
record = null;
rec = null;
MbElement
rootLast = message.getRootElement();
java.util.List
recordLast = (java.util.List)rootLast.evaluateXPath(new
MbXPath("/db/sheets"));
int
countsheet = recordLast.size();
MbElement
recLast = rootLast.getLastChild().getFirstChild().getFirstChild();
int
countForSheets123 = setSheetNames.size();
Iterator
itr123 = setSheetNames.iterator();
while(itr123.hasNext())//Iterating
unique sheet names and creating unique sheets by using Set object
{
newsheetname
= (String) itr123.next();
//for(int
recordTotal=0;recordTotal<countsheet ;recordTotal++){
try{
Sheet
worksheet =
workbook.getSheet(newsheetname);
int
rowsCount = worksheet.getPhysicalNumberOfRows();
Cell cell1=null;
// Row getRow =
worksheet.createRow((int)rowsCount);
Row getRow =null;
double Totalmiles2=0;
double Totalmiles1=0;
//worksheet.getLastRowNum()
for (int x = 0; x<rowsCount; x++)
{
try
{
getRow = worksheet.getRow(x);
String Totalmiles = getRow.getCell((int)
6).toString();
try
{
Totalmiles2=Double.valueOf(Totalmiles);
Totalmiles1+=Totalmiles2;
}catch(Exception e){
System.out.println("String Contain Empty
value or null");
}
}catch(Exception
e){}
}
//Calculating
the Total of jetpriviledge miles
double
JPMiles1=0;
double
JPMiles2=0;
for (int x
= 0; x<rowsCount; x++)
{
try{
getRow =
worksheet.getRow(x);
String
JPMiles= getRow.getCell((int) 7).toString();
try{ JPMiles2=Double.valueOf(JPMiles);
JPMiles1+=JPMiles2;
}catch(Exception
e){
System.out.println("String Contain
Empty value or null");
}
}catch(Exception
e){}
cell1 =
getRow.createCell(12);
cell1.setCellValue(AmountTotalJP1);
cell1 =
getRow.createCell(13);
cell1.setCellValue(AmountTotalJET1);
//int
x=worksheet.getLastRowNum();
//getRow =
worksheet.getRow(x);
cell1 =
getRow.createCell(14);
cell1.setCellValue(AmountTotalPartner1);
}catch(Exception
e){}
}
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
workbook.write(bOut);
output=
bOut.toByteArray();
} catch ( Exception ex ) {
System.out.println(ex);
}
return output;
}
//This below code is for creating header in Excel Sheet
rowheadSheet.createCell(12).setCellValue("Amount-asd ");
rowheadSheet.createCell(13).setCellValue("Amount-dfg ");
rowheadSheet.createCell(14).setCellValue("Amount
- gfh ");
rowheadSheet.createCell(15).setCellValue("Description ");
for(int colNum = 0;
colNum<rowheadSheet.getLastCellNum();colNum++)
{
firstSheet.autoSizeColumn(colNum );
|
7.How to access the header in JCN.
Ans: - Please
refer to the above code with ---------Green------------color
8.How to porcess the result to out
terminal in JCN.
Ans:- - Please refer to the above code with
---------Red------------color
9.Benefit of using JCN.
Ans:- IF u
like java than U can understand that
whatever u can do with java u can implement in JCN .
10.Drawback of using JCN.
Ans:- Understanding and accessing the element in JCN is difficult and if some where NULLPointer Exception Will
come than that will be like a chunk of bone in your neck.........
This will
continue....................................................
No comments:
Post a Comment