特里,你需要重新审视架构。如果更新是在提交时处理的,为什么还要费心将它们单独发送到服务器 - 正如 Tim 很好地指出的那样。我会做什么:
- 创建 2 个 Java 类:一个“Order”一个“LineItem”
- 让Order类实现Map接口Map
- 使用 Order 类进行重复控制(它将为您提供每个 LineItem 的键作为重复变量)
- 将repeat里面的字段绑定到Order[RepeatKey].fieldName
- 在对象数据源中使用 Order
- 在Order类中实现save方法,在对象数据源的save方法中调用
非常粗略的大纲,如果您需要我详细说明,请告诉我。Java 集合框架是您的朋友。
它比看起来容易:
public class LineItem {
private String unid;
private String partno;
private int quantity;
private long unitprice;
/**
* Constructor for new items
*/
public LineItem() {
this.unid = null;
}
/**
* Constructor for existing items
*/
public LineItem(Document doc) {
this.unid = doc.getUniversalId();
// more here
}
/**
* @return the unid
*/
public String getUnid() {
return this.unid;
}
/**
* @return the partno
*/
public String getPartno() {
return this.partno;
}
/**
* @param partno the partno to set
*/
public void setPartno(String partno) {
this.partno = partno;
}
/**
* @return the quantity
*/
public int getQuantity() {
return this.quantity;
}
/**
* @param quantity the quantity to set
*/
public void setQuantity(int quantity) {
this.quantity = quantity;
}
/**
* @return the unitprice
*/
public long getUnitprice() {
return this.unitprice;
}
/**
* @param unitprice the unitprice to set
*/
public void setUnitprice(long unitprice) {
this.unitprice = unitprice;
}
public void save(Database db) {
Document doc = null;
if (this.unid == null) {
doc = db.createDocument();
doc.replaceItem("Form", "LineItem");
}
doc.replaceItem("PartNo", this.partno);
// More here
doc.save();
}
}
对于订单 - 假设您从文档集合中加载。
public class Order implements Map<String, LineItem> {
// You might want to have a stack here to keep order
private final Map<String, LineItem> backingMap = new LinkedHashMap<String, LineItem>();
private final Set<String> deletedItemKeys = new HashSet<String>();
// The key we use for new items when unid is null
private int lastNewItemNumber = 0;
@Override
public int size() {
return this.backingMap.size();
}
@Override
public boolean isEmpty() {
return this.backingMap.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return this.backingMap.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return this.backingMap.containsValue(value);
}
@Override
public LineItem get(Object key) {
return this.backingMap.get(key);
}
@Override
public LineItem put(String key, LineItem value) {
// Here it gets a little special
// We need to prevent null keys
if (key == null) {
key = String.valueOf(this.lastNewItemNumber);
lastNewItemNumber++;
}
this.deletedItemKeys.remove(key);
return this.backingMap.put(key, value);
}
@Override
public LineItem remove(Object key) {
this.deletedItemKeys.add(key.toString());
return this.backingMap.remove(key);
}
@Override
public void putAll(Map<? extends String, ? extends LineItem> m) {
for (Map.Entry<? extends String, ? extends LineItem> me : m.entrySet()) {
this.put(me.getKey(), me.getValue());
}
}
@Override
public void clear() {
this.deletedItemKeys.addAll(this.backingMap.keySet());
this.backingMap.clear();
}
@Override
public Set<String> keySet() {
return this.backingMap.keySet();
}
@Override
public Collection<LineItem> values() {
return this.backingMap.values();
}
@Override
public Set<java.util.Map.Entry<String, LineItem>> entrySet() {
return this.backingMap.entrySet();
}
public void load(NotesDocumentCollection dc) throws NotesException {
Document doc = dc.getFirstDocument();
Document nextDoc;
while (doc != null) {
nextDoc = dc.getNextDocument(doc);
LineItem li = new LineItem(doc);
this.put(doc.getUniversalId(), li);
doc.recycle();
doc = nextDoc;
}
doc.recyle();
}
public void save(Database db) {
for (LineItem item : this.backingMap.values()) {
item.save(db);
}
// Now kill the left overs - needs error handling
for (String morituri : this.deletedItemKeys) {
Document delDoc = db.getDocumentByUnid(morituri);
if (delDoc != null) {
delDoc.remove(true);
}
}
}
}