To use the Assembler interface approach in an assembler, you must implement the flex.data.assemblers.Assembler interface. The flex.data.assemblers.AbstractAssembler class extends this interface, so extending this class is the easiest way to write your own assembler.
Depending on the fill request that a client sends, the fill() method performs a specific query to create the Collection object that is returned to the client side. An assembler that implements the Assembler interface has an autoRefreshFill() method, which returns a Boolean value that determines whether to refresh fills when data items change. The autoRefreshFill() method of the AbstractAssembler class returns true.
When the assembler autoRefreshFill() method returns true, the assembler refreshFill() method is called for each item that is either created or updated. The refreshFill() method can return the following values:
|
Value |
Usage |
|---|---|
| DO_NOT_EXECUTE_FILL |
Leaves the contents of the fill unchanged. |
| EXECUTE_FILL |
Has the DataService object call its fill() method. |
| APPEND_TO_FILL |
Adds the changed item to the end of the list returned by the last fill invocation. |
| REMOVE_FROM_FILL |
Removes an item from the fill. |
In addition to fill() methods, the Assembler interface provides getItem()and getItems() methods for getting specific data items. It also provides the createItem() method for creating new items, the updateItem() method for updating an existing item, and the deleteItem() method for deleting items. You can override the createItem(), updateItem(), and deleteItem() methods to add your own synchronization logic.
The following example shows the source code for the ProductAssembler class that is part of the Data Management Service application in the LiveCycle Data Services ES Test Drive:
package flex.samples.product;
import java.util.List;
import java.util.Collection;
import java.util.Map;
import flex.data.DataSyncException;
import flex.data.assemblers.AbstractAssembler;
public class ProductAssembler extends AbstractAssembler {
public Collection fill(List fillArgs) {
ProductService service = new ProductService();
return service.getProducts();
}
public Object getItem(Map identity) {
ProductService service = new ProductService();
return service.getProduct(((Integer) identity.get("productId")).intValue());
}
public void createItem(Object item) {
ProductService service = new ProductService();
service.create((Product) item);
}
public void updateItem(Object newVersion, Object prevVersion, List changes) {
ProductService service = new ProductService();
boolean success = service.update((Product) newVersion);
if (!success) {
int productId = ((Product) newVersion).getProductId();
throw new DataSyncException(service.getProduct(productId), changes);
}
}
public void deleteItem(Object item) {
ProductService service = new ProductService();
boolean success = service.delete((Product) item);
if (!success) {
int productId = ((Product) item).getProductId();
throw new DataSyncException(service.getProduct(productId), null);
}
}
}
The Java source code for the ProductAssembler class and other classes it uses are in the WEB_INF/src/flex/samples/product directory of the lcds-samples web application. The ProductAssembler class uses the Assembler interface approach. It extends the flex.data.assemblers.AbstractAssembler class and does not override the autoRefreshFill() method, which returns true.
ProductAssembler delegates SQL database interaction to a data access object (DAO) called ProductService. The following example shows the source code for the ProductService class:
package flex.samples.product;
import java.util.ArrayList;
import java.util.List;
import java.sql.*;
import flex.samples.ConnectionHelper;
import flex.samples.DAOException;
public class ProductService {
public List getProducts() throws DAOException {
List list = new ArrayList();
Connection c = null;
try {
c = ConnectionHelper.getConnection();
Statement s = c.createStatement();
ResultSet rs = s.executeQuery("SELECT * FROM product ORDER BY name");
while (rs.next()) {
list.add(new Product(rs.getInt("product_id"),
rs.getString("name"),
rs.getString("description"),
rs.getString("image"),
rs.getString("category"),
rs.getDouble("price"),
rs.getInt("qty_in_stock")));
}
} catch (SQLException e) {
e.printStackTrace();
throw new DAOException(e);
} finally {
ConnectionHelper.close(c);
}
return list;
}
public List getProductsByName(String name) throws DAOException {
List list = new ArrayList();
Connection c = null;
try {
c = ConnectionHelper.getConnection();
PreparedStatement ps = c.prepareStatement("SELECT * FROM product WHERE UPPER(name) LIKE ? ORDER BY name");
ps.setString(1, "%" + name.toUpperCase() + "%");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
list.add(new Product(rs.getInt("product_id"),
rs.getString("name"),
rs.getString("description"),
rs.getString("image"),
rs.getString("category"),
rs.getDouble("price"),
rs.getInt("qty_in_stock")));
}
} catch (SQLException e) {
e.printStackTrace();
throw new DAOException(e);
} finally {
ConnectionHelper.close(c);
}
return list;
}
public Product getProduct(int productId) throws DAOException {
Product product = new Product();
Connection c = null;
try {
c = ConnectionHelper.getConnection();
PreparedStatement ps = c.prepareStatement("SELECT * FROM product WHERE product_id=?");
ps.setInt(1, productId);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
product = new Product();
product.setProductId(rs.getInt("product_id"));
product.setName(rs.getString("name"));
product.setDescription(rs.getString("description"));
product.setImage(rs.getString("image"));
product.setCategory(rs.getString("category"));
product.setPrice(rs.getDouble("price"));
product.setQtyInStock(rs.getInt("qty_in_stock"));
}
} catch (Exception e) {
e.printStackTrace();
throw new DAOException(e);
} finally {
ConnectionHelper.close(c);
}
return product;
}
public Product create(Product product) throws DAOException {
Connection c = null;
PreparedStatement ps = null;
try {
c = ConnectionHelper.getConnection();
ps = c.prepareStatement("INSERT INTO product (name, description, image, category, price, qty_in_stock) VALUES (?, ?, ?, ?, ?, ?)");
ps.setString(1, product.getName());
ps.setString(2, product.getDescription());
ps.setString(3, product.getImage());
ps.setString(4, product.getCategory());
ps.setDouble(5, product.getPrice());
ps.setInt(6, product.getQtyInStock());
ps.executeUpdate();
Statement s = c.createStatement();
// HSQLDB Syntax to get the identity (company_id) of inserted row
ResultSet rs = s.executeQuery("CALL IDENTITY()");
// MySQL Syntax to get the identity (product_id) of inserted row
// ResultSet rs = s.executeQuery("SELECT LAST_INSERT_ID()");
rs.next();
// Update the id in the returned object. This is important as this value must get returned to the client.
product.setProductId(rs.getInt(1));
} catch (Exception e) {
e.printStackTrace();
throw new DAOException(e);
} finally {
ConnectionHelper.close(c);
}
return product;
}
public boolean update(Product product) throws DAOException {
Connection c = null;
try {
c = ConnectionHelper.getConnection();
PreparedStatement ps = c.prepareStatement("UPDATE product SET name=?,
description=?, image=?, category=?, price=?, qty_in_stock=? WHERE
product_id=?");
ps.setString(1, product.getName());
ps.setString(2, product.getDescription());
ps.setString(3, product.getImage());
ps.setString(4, product.getCategory());
ps.setDouble(5, product.getPrice());
ps.setInt(6, product.getQtyInStock());
ps.setInt(7, product.getProductId());
return (ps.executeUpdate() == 1);
} catch (SQLException e) {
e.printStackTrace();
throw new DAOException(e);
} finally {
ConnectionHelper.close(c);
}
}
public boolean remove(Product product) throws DAOException {
Connection c = null;
try {
c = ConnectionHelper.getConnection();
PreparedStatement ps = c.prepareStatement("DELETE FROM product WHERE product_id=?");
ps.setInt(1, product.getProductId());
int count = ps.executeUpdate();
return (count == 1);
} catch (Exception e) {
e.printStackTrace();
throw new DAOException(e);
} finally {
ConnectionHelper.close(c);
}
}
public boolean delete(Product product) throws DAOException {
return remove(product);
}
}
ProductAssembler stores the state of individual products in a JavaBean named Product. The following example shows the source code for the Product class:
package flex.samples.product;
import java.io.Serializable;
public class Product implements Serializable {
static final long serialVersionUID = 103844514947365244L;
private int productId;
private String name;
private String description;
private String image;
private String category;
private double price;
private int qtyInStock;
public Product() {
}
public Product(int productId, String name, String description, String image, String category, double price, int qtyInStock) {
this.productId = productId;
this.name = name;
this.description = description;
this.image = image;
this.category = category;
this.price = price;
this.qtyInStock = qtyInStock;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
public int getQtyInStock() {
return qtyInStock;
}
public void setQtyInStock(int qtyInStock) {
this.qtyInStock = qtyInStock;
}
}
The following example shows a destination that uses ProductAssembler:
<destination id="inventory">
<properties>
<source>flex.samples.product.ProductAssembler</source>
<scope>application</scope>
<metadata>
<identity property="productId"/>
</metadata>
<network>
<paging enabled="false" pageSize="10" />
</network>
</properties>
</destination>
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/livecycle/8.2/programLC/programmer/lcds/dms_custom_assemblers_3.html