`

SDO 编程

阅读更多

Creating data objects
     BOFactory boFactory = (BOFactory)ServiceManager.INSTANCE.locateService("com/ibm/websphere/bo/BOFactory");
The following code snippet shows how you can determine the type of a process variable and   create a new data object of that type:

 

Type type = getVariableType("Output");

 

 Output = boFactory.createByType(type);

To create a nested data object, you can use the createDataObject method of the DataObject:

 

DataObject nested = Output.createDataObject("processBusinessObject");

 

You can also create a data object using the create method of the BOFactory service. You have to specify name and namespace:

 

MyBO = factory.create(" http://JavaSnippets/bpc/samples","BO");

Copying data objects
   BOCopy copyService = (BOCopy)ServiceManager.INSTANCE.locateService("com/ibm/websphere/bo/BOCopy");
The BOCopy service offers to methods to create a deep copy of data objects: copy and copyInto. The copy method returns a copy of the original DataObject and the copyInto method allows to copy the original DataObject into the structure of an existing DataObject.
Copy:
 DataObject dataObject = copyService.copy(ProcessBusinessObject);
CopyInto:
 copyService.copyInto(ProcessBO, Output, "processBO");

 

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@page import="com.ibm.websphere.sca.ServiceManager"%>
<%@page import="com.ibm.websphere.bo.BOFactory"%>
<%@page import="commonj.sdo.DataObject"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>     
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<%
ServiceManager serviceManager = ServiceManager.INSTANCE;

//Create the Business Object factory to create business object at runtime
BOFactory boFactory = (BOFactory)serviceManager.locateService("com/ibm/websphere/bo/BOFactory");
%> 
Initialization for BO Factory is done.
<br>
<%
//Create Customer business object
//Use BOFactory.create() method to create the data object, because the business object is created by the Business Object editor
//First parameter is the namespace of the business object. You can find it from the Namespace field of the properties view in the Business Object Editor.
//Second parameter is the name of the business object. It can be found in the Name field of the properties view in the Business Object Editor.
DataObject customer = boFactory.create("http://BankModule/com/bank/eMerged", "Customer");

//set values for the simple-typed attributes
customer.setString("customerName", "John Smith");
customer.setString("customerId", "11111111");

//The customer business object has an attribute of type Address, which is another business object.
//To set value on this attribute, create a business object using DataObject.createDataObject().
//However, because this method will overwrite any existing value, add isSet() to make sure the attribute is not already set.
if (!customer.isSet("address")){
DataObject address = customer.createDataObject("address");
address.setString("wholeAddress", "8200 Warden Avenue");
//The only attribute left is the "accounts" attribute. A customer business object contains a list of accounts.
//First, create the data object for the Account business object. And set the values for the attributes
DataObject ac1 = boFactory.create("http://BankModule/com/bank/eMerged", "Account");
ac1.setString("accountNum", "ABC12345");
ac1.setFloat("balance", 5678.9f);

DataObject ac2 = boFactory.create("http://BankModule/com/bank/eMerged", "Account");
ac2.setString("accountNum", "DEF34567");
ac2.setFloat("balance", 100.3f);

//Second, prepare the list for adding the account data object
ArrayList acList = new ArrayList();
acList.add(ac1);
acList.add(ac2);

//Third, set the "accounts" attribute to be the list.
customer.setList("accounts", acList);
}
%>
Customer business object has been created.
<br>
<br>
Printing customer information...
<br>

<!-- Retrieve customer name --> 
Customer name: <%= customer.getString("customerName") %>

<br>

<%
//Retrieve customer address object
DataObject address2 = customer.getDataObject("address");
%>

Customer address: <%= address2.getString("wholeAddress") %>
<br>


Customer's account list which is an array: 
<% 
//Retrieve the list of customer accounts
List list = customer.getList("accounts"); 
DataObject a1 = (DataObject)list.get(0);
%>
<br>
Account 1 : <%= a1.getString("accountNum")%>
</body>
</html>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics