return havingString;
}
public List<GenericValue> selectByMultiRelation(GenericValue value, ModelRelation modelRelationOne, ModelEntity modelEntityOne,
ModelRelation modelRelationTwo, ModelEntity modelEntityTwo, List<String> orderBy) throws GenericEntityException {
SQLProcessor sqlP = new SQLProcessor(helperInfo);
// get the tables names
String atable = modelEntityOne.getTableName(datasourceInfo);
String ttable = modelEntityTwo.getTableName(datasourceInfo);
// get the column name string to select
StringBuilder selsb = new StringBuilder();
List<String> collist = FastList.newInstance();
List<String> fldlist = FastList.newInstance();
for (Iterator<ModelField> iterator = modelEntityTwo.getFieldsIterator(); iterator.hasNext();) {
ModelField mf = iterator.next();
collist.add(mf.getColName());
fldlist.add(mf.getName());
selsb.append(ttable).append(".").append(mf.getColName());
if (iterator.hasNext()) {
selsb.append(", ");
} else {
selsb.append(" ");
}
}
// construct assoc->target relation string
int kmsize = modelRelationTwo.getKeyMapsSize();
StringBuilder wheresb = new StringBuilder();
for (int i = 0; i < kmsize; i++) {
ModelKeyMap mkm = modelRelationTwo.getKeyMap(i);
String lfname = mkm.getFieldName();
String rfname = mkm.getRelFieldName();
if (wheresb.length() > 0) {
wheresb.append(" AND ");
}
wheresb.append(atable).append(".").append(modelEntityOne.getField(lfname).getColName()).append(" = ").append(ttable).append(".").append(modelEntityTwo.getField(rfname).getColName());
}
// construct the source entity qualifier
// get the fields from relation description
kmsize = modelRelationOne.getKeyMapsSize();
Map<ModelField, Object> bindMap = FastMap.newInstance();
for (int i = 0; i < kmsize; i++) {
// get the equivalent column names in the relation
ModelKeyMap mkm = modelRelationOne.getKeyMap(i);
String sfldname = mkm.getFieldName();
String lfldname = mkm.getRelFieldName();
ModelField amf = modelEntityOne.getField(lfldname);
String lcolname = amf.getColName();
Object rvalue = value.get(sfldname);
bindMap.put(amf, rvalue);
// construct one condition
if (wheresb.length() > 0) {
wheresb.append(" AND ");
}
wheresb.append(atable).append(".").append(lcolname).append(" = ? ");
}
// construct a join sql query
StringBuilder sqlsb = new StringBuilder();
sqlsb.append("SELECT ");
sqlsb.append(selsb.toString());
sqlsb.append(" FROM ");
sqlsb.append(atable).append(", ").append(ttable);
sqlsb.append(" WHERE ");
sqlsb.append(wheresb.toString());
sqlsb.append(SqlJdbcUtil.makeOrderByClause(modelEntityTwo, orderBy, true, datasourceInfo));
// now execute the query
List<GenericValue> retlist = FastList.newInstance();
Delegator gd = value.getDelegator();
try {
sqlP.prepareStatement(sqlsb.toString());
for (Map.Entry<ModelField, Object> entry: bindMap.entrySet()) {
ModelField mf = entry.getKey();
Object curvalue = entry.getValue();
SqlJdbcUtil.setValue(sqlP, mf, modelEntityOne.getEntityName(), curvalue, modelFieldTypeReader);
}
sqlP.executeQuery();
//int collsize = collist.size();
while (sqlP.next()) {
Map<String, Object> emptyMap = Collections.emptyMap();
GenericValue gv = gd.makeValue(modelEntityTwo.getEntityName(), emptyMap);
// loop thru all columns for in one row
int idx = 1;
for (String fldname: fldlist) {
ModelField mf = modelEntityTwo.getField(fldname);
SqlJdbcUtil.getValue(sqlP.getResultSet(), idx, mf, gv, modelFieldTypeReader);
idx++;
}
retlist.add(gv);
}
} finally {
sqlP.close();
}
return retlist;
}