com.viaoa.hub.HubGroupByMerger Maven / Gradle / Ivy
Show all versions of oa-core Show documentation
package com.viaoa.hub;
import java.lang.reflect.Method;
import com.viaoa.object.OAObject;
import com.viaoa.util.OAPropertyPath;
import com.viaoa.util.OAString;
/**
* Used for GroupBy Link Properties (OABuilder).
* Uses a HubMerger to get objects, and then adds the found objects to a (calc) LinkProperty.
*
* Example:
* mergerPP: [Orders]:open().orderItems.item
* groupByPP: [Orders].customer
* groupByProperty (in customer): Hub- hubOpenOrderItems (groupBy calculated linkProperty)
*
* Example:
* mergerPP: [Orders]:open().orderItems
* groupByPP: [Orders].orderItems.item
* groupByProperty (in item): Hub hubOpenOrderItems (groupBy calculated linkProperty)
*
* @author vvia
* @param from object for the hub merger
* @param to object for the hub merger
*/
public class HubGroupByMerger {
private String mergerPropertyPath;
private String groupByPropertyPath;
private String groupByProperty;
private int cntAbove; // number of data.parent to go from mergePropertyPath to then use groupByPropertyPath
private HubMerger hubMerger;
public HubGroupByMerger(Hub hubRoot, String groupByPropertyPath, String groupByProperty) {
this(hubRoot, null, groupByPropertyPath, groupByProperty);
}
/**
* @param hubRoot
* @param mergerPropertyPath PP from Root to merger objects
* @param groupByPropertyPath PP from hubRoot objects to the object where there is a calc Hub for storing the found merger objects.
* *NOTE: this PP must start from same root as mergerPropertyPath.
* @param groupByProperty name of property Hub in groupByPP for storing the found merger objects.
*/
public HubGroupByMerger(Hub hubRoot, String mergerPropertyPath, String groupByPropertyPath, String groupByProperty) {
this.mergerPropertyPath = mergerPropertyPath;
this.groupByPropertyPath = groupByPropertyPath;
this.groupByProperty = groupByProperty;
final OAPropertyPath ppGroupByPropertyPath = new OAPropertyPath(hubRoot.getObjectClass(), groupByPropertyPath);
Method[] msGroupByPropertyPath = ppGroupByPropertyPath.getMethods();
if (OAString.isEmpty(mergerPropertyPath)) {
hubRoot.addHubListener(new HubListenerAdapter() {
@Override
public void afterAdd(HubEvent e) {
OAObject objFrom = (OAObject) e.getObject();
OAObject objTo = (OAObject) ppGroupByPropertyPath.getValue(objFrom);
if (objTo != null) {
Hub hub = (Hub) objTo.getProperty(groupByProperty);
hub.add(objFrom);
}
}
@Override
public void afterRemove(HubEvent e) {
OAObject objFrom = (OAObject) e.getObject();
OAObject objTo = (OAObject) ppGroupByPropertyPath.getValue(objFrom);
if (objTo != null) {
Hub hub = (Hub) objTo.getProperty(groupByProperty);
hub.remove(objFrom);
}
}
});
} else {
final OAPropertyPath ppMergerPropertyPath = new OAPropertyPath(hubRoot.getObjectClass(), mergerPropertyPath);
Method[] msMergerPropertyPath = ppMergerPropertyPath.getMethods();
int cnt = 0;
for (; cnt < msGroupByPropertyPath.length && cnt < msMergerPropertyPath.length; cnt++) {
if (!msGroupByPropertyPath[cnt].equals(msMergerPropertyPath[cnt])) {
break;
}
}
// find how much of the the groupBy PP is same as merger
this.cntAbove = msMergerPropertyPath.length - (cnt + 1);
final int groupByPropertyPathStartPos = cnt;
hubMerger = new HubMerger(hubRoot, null, mergerPropertyPath, false, null, true, false, false) {
@Override
protected void onAddToCombined(Data data, final OAObject obj) {
OAObject objFrom = obj;
if (cntAbove >= 0) {
for (int i = 0; data != null && i < cntAbove; i++) {
data = data.parent;
}
if (data == null) {
objFrom = null;
} else {
objFrom = data.parentObject;
}
}
if (objFrom != null) {
OAObject objTo = (OAObject) ppGroupByPropertyPath.getValue(objFrom, groupByPropertyPathStartPos);
if (objTo != null) {
Hub hub = (Hub) objTo.getProperty(groupByProperty);
hub.add(obj);
}
}
}
@Override
protected void onRemoveFromCombined(Data data, OAObject obj) {
for (int i = 0; data != null && i < cntAbove; i++) {
data = data.parent;
}
if (data != null) {
OAObject objx = (OAObject) ppGroupByPropertyPath.getValue(data.parentObject);
Hub hub = (Hub) objx.getProperty(groupByProperty);
hub.remove(obj);
}
}
};
hubMerger.setServerSideOnly(true);
}
}
}