private static abstract class BaseSaveEventListener extends BaseEventListener {
public BaseSaveEventListener() {
super();
}
protected void handleEvent(Event event) {
final Component target = event.getTarget();
final String triggerEventName = event.getName();
final List tmplist = new ArrayList(_dataTargets.size());
//fire onSave for each binding
for(final Iterator it = _dataTargets.iterator();it.hasNext();) {
final BindingInfo bi = (BindingInfo) it.next();
final Component dt = bi.getComponent();
final Binding binding = bi.getBinding();
final DataBinder binder = binding.getBinder();
final Component dataTarget = DataBinder.isTemplate(dt) ?
DataBinder.lookupClone(target, dt) : dt;
//bug# 1904389: NullPointerException if save-when in collection binding
//event.getTarget() might not inside the collection item (i.e. row, listitem, etc.)
//then binder.lookupClone() will return null dataTarget.
if (dataTarget != null) {
final Object[] vals = binding.getAttributeValues(dataTarget);
if (vals != null) {
tmplist.add(new BindingInfo(binding, dataTarget, vals));
Events.sendEvent(new BindingSaveEvent("onBindingSave", dataTarget, target, binding, vals[0]));
}
} else {
//bi.getComponent a template and a null dataTarget, meaning all collection items has to
//be handled. Search the owner to iterate all cloned items.
final List clones = scanClones(binder, dt);
for (final Iterator itc = clones.iterator(); itc.hasNext();) {
final Component dataTarget1 = (Component)itc.next();
final Object[] vals = binding.getAttributeValues(dataTarget1);
if (vals != null) {
tmplist.add(new BindingInfo(binding, dataTarget1, vals));
Events.sendEvent(new BindingSaveEvent("onBindingSave", dataTarget1, target, binding, vals[0]));
}
}
}
}
//fire onValidate for target component
Events.sendEvent(new Event("onBindingValidate", target));
//saveAttribute for each binding
Component loadOnSaveProxy = null;
Component dataTarget = null;
DataBinder binder = null;
final List loadOnSaveInfos = new ArrayList(tmplist.size());
for(final Iterator it = tmplist.iterator();it.hasNext();) {
final BindingInfo bi = (BindingInfo) it.next();
dataTarget = bi.getComponent();
final Binding binding = bi.getBinding();
if (binder == null) {
binder = binding.getBinder();
}
final Object[] vals = bi.getAttributeValues();
binding.saveAttributeValue(dataTarget, vals, loadOnSaveInfos, triggerEventName);
if (loadOnSaveProxy == null && dataTarget.isListenerAvailable("onLoadOnSave", true)) {
loadOnSaveProxy = dataTarget;
}
}
//bug #1895856 : data binding LoadOnSave works only on the last save-when component