Application Developer's Guide
This section describes how to set an XML data stream to input to a complex parameter type. It contains the following sections:
Complex parameter types provide a facility to use streaming XML data as an input to Liquid Data. You can define an XML data stream of an arbitrary type, and you can use that XML data as input to a query.
Figure 9-1 shows the overall architecture of sending XML data as an input to a query.
Figure 9-1 Setting XML Data as an input to a stored query
To evaluate a Complex Parameter Type query from the EJB API, you use the setXMLData
method on the QueryParameters
object.
The following Java code sets the XML data for the input to a stored query, then executes the stored query.
query = (Query) home.create();
QueryParameters qp = new QueryParameters();
qp.setXMLData("CPTSAMPLE", queryParam);
qr= query.executeStored(queryName, qp);
where CPTSAMPLE
is a stored query and queryParam
is some XML String
value.
This section provides sample Java code, using the QueryParameters.setXMLData
method, to input XML data into a complex parameter type. For information on defining complex parameter types to Liquid data, see Using Complex Parameter Types in the Administration Guide. For information on using complex parameter types in the Data View Builder, see Using Complex Parameter Types In Queries in Building Queries and Data Views.
The Data View Builder project for the example shown here is installed in the following directory:
BEA_HOME/weblogic81/samples/liquiddata/buildQuery/db-cpt
The code shown in this sample is installed as the following file:
BEA_HOME/weblogic81/samples/liquiddata/ejbAPI/src/ejbSample/QueryWithCptParamClient.java
The section is divided into the following parts:
Assume the following sample query is saved in the Liquid Data repository as the stored query named crm
_cptSample.xq
. This query uses a complex parameter type (CPTSAMPLE
) which contains promotion plan names, and then combines those promotion plan names with the details from the CRM database (PB-CR
).
{-- Generated by Data View Builder 8.1 --}
namespace crm1 = "urn:schemas-bea-com:ld-crmp"
namespace crm = "urn:schemas-bea-com:ld-cptSample"
<crm1:db>
{
for $CPTSAMPLE.PROMOTION_2 in ($#CPTSAMPLE of type element
crm:db)/crm:PROMOTION
let $PROMOTION_PLAN_3 :=
for $PB_CR.PROMOTION_PLAN_4 in
document("PB-CR")/db/PROMOTION_PLAN
where ($CPTSAMPLE.PROMOTION_2/crm:PROMOTION_NAME eq
$PB_CR.PROMOTION_PLAN_4/PROMOTION_NAME)
return
<PROMOTION_PLAN>
<PROMOTION_NAME>{ xf:data($PB_CR.PROMOTION_PLAN_4/PROMOTION_NAME) }</PROMOTION_NAME>
<PLAN_NAME>{ xf:data($PB_CR.PROMOTION_PLAN_4/PLAN_NAME)}</PLAN_NAME>
<FROM_DATE>{ cast as
xs:string(xf:data($PB_CR.PROMOTION_PLAN_4/FROM_DATE)) }</FROM_DATE>
<TO_DATE>{ cast as xs:string(xf:data($PB_CR.PROMOTION_PLAN_4/TO_DATE))
}</TO_DATE>
<PRICE>{ cast as xs:string(xf:data($PB_CR.PROMOTION_PLAN_4/PRICE)) }
</PRICE>
</PROMOTION_PLAN>
where xf:not(xf:empty($PROMOTION_PLAN_3))
return
<PROMOTION>
<STATE>{ xf:data($CPTSAMPLE.PROMOTION_2/crm:STATE) }</STATE>
<PROMOTION_NAME>{ xf:data($CPTSAMPLE.PROMOTION_2/crm:PROMOTION_NAME) }</PROMOTION_NAME>
{ $PROMOTION_PLAN_3 }
</PROMOTION>
}
</crm1:db>
The following code sample shows the setXMLData
method used to input data into a query that uses a complex parameter type source. To simplify the sample code, this sample creates a String
variable named queryParam
to represent the XML data stream; you typically will use the setXMLData
method to reference an object which contains XML data.
package ejbSample;
import java.rmi.RemoteException;
import java.util.Properties;
import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.bea.ldi.server.*;
import com.bea.ldi.server.common.*;
import java.io.*;
import java.rmi.*;
public class QueryWithCptParamClient {
private String url=null;
private String JNDI_NAME="bea.ldi.server.QueryHome";
private String queryName=null;
private static QueryHome home=null;
private Query query =null;
// public static boolean stop=false;
private String queryParam =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?> " +
" <db xmlns=\"urn:schemas-bea-com:ld-cptSample\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xsi:schemaLocation=\"urn:schemas-bea-com:ld-cptSample crm-p-cptSample.xsd\">" +
"<PROMOTION>" +
"<STATE>CA</STATE>" +
"<PROMOTION_NAME>BROADBAND UPSELL</PROMOTION_NAME>" +
" </PROMOTION>" +
"<PROMOTION>" +
"<STATE>TX</STATE>" +
"<PROMOTION_NAME>WIRELESS UPSELL</PROMOTION_NAME>" +
"</PROMOTION>" +
"<PROMOTION>" +
"<STATE>WA</STATE>" +
"<PROMOTION_NAME>NEW PRODUCTS</PROMOTION_NAME>" +
"</PROMOTION>" +
"<PROMOTION>" +
"<STATE>AZ</STATE>" +
"<PROMOTION_NAME>HOLIDAY PROMOTION</PROMOTION_NAME>" +
"</PROMOTION>" +
"<PROMOTION>" +
"<STATE>NV</STATE>" +
" <PROMOTION_NAME>SALES PROMOTION</PROMOTION_NAME>" +
" </PROMOTION>" +
" </db>";
/* normally you would pass the argument for the parameter. But
* in this example we are hardcoding the XML data stream
*/
public QueryWithCptParamClient(String url, String queryName){
this.url= url;
this.queryName=queryName;
}
public static void main(String[] args) throws Exception
{
QueryWithCptParamClient qpc = new QueryWithCptParamClient(args[0], args[1]);
qpc.runQuery();
}
public void runQuery() throws Exception{
QueryResult qr=null;
try{
if(home==null)
home = lookupHome();
// log("Creating a query client");
query = (Query) home.create();
QueryParameters qp = new QueryParameters();
qp.setXMLData("CPTSAMPLE", queryParam);
qr= query.executeStored(queryName, qp);
System.out.println("Query Result: >>>>>>>\n");
if(!qr.isEmpty())
qr.printWithFormat(new OutputStreamWriter(System.out), true);
}catch(Exception e){
throw e;
}finally{
qr.close();
}
}
/**
* Lookup the EJBs home in the JNDI tree
*/
private QueryHome lookupHome()
throws NamingException
{
// Lookup the beans home using JNDI
Context ctx = getInitialContext();
try {
return (QueryHome)ctx.lookup(JNDI_NAME);
} catch (NamingException ne) {
ne.printStackTrace();
log("The client was unable to lookup the EJBHome. Please make sure ");
log("that you have deployed the ejb with the JNDI name "+JNDI_NAME+" on the WebLogic server at "+url);
throw ne;
}
}
private Context getInitialContext() throws NamingException {
try {
// Get an InitialContext
Properties h = new Properties();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, url);
return new InitialContext(h);
} catch (NamingException ne) {
ne.printStackTrace();
log("We were unable to get a connection to the WebLogic server at "+url);
log("Please make sure that the server is running.");
throw ne;
}
}
private static void log(String s) {
System.out.println(s);
}
}
Perform the following steps to build and run the crm-cpt
complex parameter type example.
/
weblogic81/samples/domains/liquiddata directory as in the following example:cd /bea/weblogic81/samples/domains/liquiddata
setLDExamplesEnv.cmd
script (setLDExamplesEnv.sh
on UNIX systems) to set up the environment for the samples, as follows:setLDExamplesEnv
/
weblogic81/samples/liquiddata/ejbAPI/build
directory as in the following example:cd /bea/weblogic81/samples/liquiddata
/ejbAPI/build
ant
/weblogic81/samples/liquiddata/ejbAPI/obj
directory as in the following example:cd /bea/
weblogic81/samples/liquiddata
/ejbAPI/obj
set classpath=%CLASSPATH%;./
java ejbSample.QueryWithCptParamClient t3://localhost:7001 crm_cptSample
Note: Make sure your Liquid Data Samples domain is running or this command will fail.
When you run this sample successfully, results similar to the following appear in your command window:
D:\bea\weblogic81\samples\liquiddata\ejbAPI\obj>java ejbSample.QueryWithCptParamClient t3://localhost:7001 crm_cptSample
Result: >>>>>>>
<crm1:db xmlns:crm1="urn:schemas-bea-com:ld-crmp">
<PROMOTION>
<STATE>CA</STATE>
<PROMOTION_NAME>BROADBAND UPSELL</PROMOTION_NAME>
<PROMOTION_PLAN>
<PROMOTION_NAME>BROADBAND UPSELL</PROMOTION_NAME>
<PLAN_NAME>High Speed Holidays</PLAN_NAME>
<FROM_DATE>2001-11-22</FROM_DATE>
<TO_DATE>2002-12-31</TO_DATE>
<PRICE>100</PRICE>
</PROMOTION_PLAN>
</PROMOTION>
<PROMOTION>
<STATE>TX</STATE>
<PROMOTION_NAME>WIRELESS UPSELL</PROMOTION_NAME>
<PROMOTION_PLAN>
<PROMOTION_NAME>WIRELESS UPSELL</PROMOTION_NAME>
<PLAN_NAME>Family Holiday Connect</PLAN_NAME>
<FROM_DATE>2001-11-22</FROM_DATE>
<TO_DATE>2002-12-31</TO_DATE>
<PRICE>49.99</PRICE>
</PROMOTION_PLAN>
</PROMOTION>
<PROMOTION>
<STATE>WA</STATE>
<PROMOTION_NAME>NEW PRODUCTS</PROMOTION_NAME>
<PROMOTION_PLAN>
<PROMOTION_NAME>NEW PRODUCTS</PROMOTION_NAME>
<PLAN_NAME>New Phone for the Holidays</PLAN_NAME>
<FROM_DATE>2001-11-22</FROM_DATE>
<TO_DATE>2002-12-31</TO_DATE>
<PRICE>149.99</PRICE>
</PROMOTION_PLAN>
</PROMOTION>
<PROMOTION>
<STATE>AZ</STATE>
<PROMOTION_NAME>HOLIDAY PROMOTION</PROMOTION_NAME>
<PROMOTION_PLAN>
<PROMOTION_NAME>HOLIDAY PROMOTION</PROMOTION_NAME>
<PLAN_NAME>New Year New Connections</PLAN_NAME>
<FROM_DATE>2001-11-22</FROM_DATE>
<TO_DATE>2002-12-31</TO_DATE>
<PRICE>39.99</PRICE>
</PROMOTION_PLAN>
</PROMOTION>
<PROMOTION>
<STATE>NV</STATE>
<PROMOTION_NAME>SALES PROMOTION</PROMOTION_NAME>
<PROMOTION_PLAN>
<PROMOTION_NAME>SALES PROMOTION</PROMOTION_NAME>
<PLAN_NAME>Family Plan</PLAN_NAME>
<FROM_DATE>2001-11-22</FROM_DATE>
<TO_DATE>2002-12-31</TO_DATE>
<PRICE>39.99</PRICE>
</PROMOTION_PLAN>
</PROMOTION>
</crm1:db>