FR [ 2818547 ] Implement Query.setOnlySelection
https://sourceforge.net/tracker/?func=detail&aid=2818547&group_id=176962&atid=879335
This commit is contained in:
parent
e03872d7d4
commit
7eca8d37c1
|
@ -50,6 +50,8 @@ import org.compiere.util.Util;
|
|||
* <li>FR [ 2421313 ] Introduce Query.firstOnly convenient method
|
||||
* <li>FR [ 2546052 ] Introduce Query aggregate methods
|
||||
* <li>FR [ 2726447 ] Query aggregate methods for all return types
|
||||
* <li>FR [ 2818547 ] Implement Query.setOnlySelection
|
||||
* https://sourceforge.net/tracker/?func=detail&aid=2818547&group_id=176962&atid=879335
|
||||
* @author Redhuan D. Oon
|
||||
* <li>FR: [ 2214883 ] Remove SQL code and Replace for Query // introducing SQL String prompt in log.info
|
||||
* <li>FR: [ 2214883 ] - to introduce .setClient_ID
|
||||
|
@ -73,6 +75,7 @@ public class Query
|
|||
private boolean applyAccessFilter = false;
|
||||
private boolean onlyActiveRecords = false;
|
||||
private boolean onlyClient_ID = false;
|
||||
private int onlySelection_ID = -1;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -186,6 +189,16 @@ public class Query
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Only records that are in T_Selection with AD_PInstance_ID.
|
||||
* @param AD_PInstance_ID
|
||||
*/
|
||||
public Query setOnlySelection(int AD_PInstance_ID)
|
||||
{
|
||||
this.onlySelection_ID = AD_PInstance_ID;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of all po that match the query criteria.
|
||||
* @return List
|
||||
|
@ -573,6 +586,19 @@ public class Query
|
|||
whereBuffer.append(" AND ");
|
||||
whereBuffer.append("AD_Client_ID=?");
|
||||
}
|
||||
if (this.onlySelection_ID > 0)
|
||||
{
|
||||
String[] keys = table.getKeyColumns();
|
||||
if (keys.length != 1)
|
||||
{
|
||||
throw new DBException("Table "+table+" has 0 or more than 1 key columns");
|
||||
}
|
||||
//
|
||||
if (whereBuffer.length() > 0)
|
||||
whereBuffer.append(" AND ");
|
||||
whereBuffer.append(" EXISTS (SELECT 1 FROM T_Selection s WHERE s.AD_PInstance_ID=?"
|
||||
+" AND s.T_Selection_ID="+table.getTableName()+"."+keys[0]+")");
|
||||
}
|
||||
|
||||
StringBuffer sqlBuffer = new StringBuffer(selectClause);
|
||||
if (whereBuffer.length() > 0)
|
||||
|
@ -609,6 +635,11 @@ public class Query
|
|||
DB.setParameter(pstmt, i++, AD_Client_ID);
|
||||
log.finest("Parameter AD_Client_ID = "+AD_Client_ID);
|
||||
}
|
||||
if (this.onlySelection_ID > 0)
|
||||
{
|
||||
DB.setParameter(pstmt, i++, this.onlySelection_ID);
|
||||
log.finest("Parameter Selection AD_PInstance_ID = "+this.onlySelection_ID);
|
||||
}
|
||||
return pstmt.executeQuery();
|
||||
}
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
package test.functional;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -21,6 +22,7 @@ import org.adempiere.exceptions.DBException;
|
|||
import org.compiere.model.MTable;
|
||||
import org.compiere.model.POResultSet;
|
||||
import org.compiere.model.Query;
|
||||
import org.compiere.model.X_AD_Element;
|
||||
import org.compiere.util.DB;
|
||||
import org.compiere.util.Env;
|
||||
|
||||
|
@ -247,4 +249,31 @@ public class QueryTest extends AdempiereTestCase
|
|||
query.aggregate(null, Query.AGGREGATE_SUM);
|
||||
}});
|
||||
}
|
||||
|
||||
public void testOnlySelection() throws Exception
|
||||
{
|
||||
// Get one AD_PInstance_ID
|
||||
int AD_PInstance_ID = DB.getSQLValueEx(null, "SELECT MAX(AD_PInstance_ID) FROM AD_PInstance");
|
||||
assertTrue(AD_PInstance_ID > 0);
|
||||
|
||||
// Create selection list
|
||||
List<Integer> elements = new ArrayList<Integer>();
|
||||
elements.add(102); // AD_Element_ID=102 => AD_Client_ID
|
||||
elements.add(104); // AD_Element_ID=104 => AD_Column_ID
|
||||
DB.executeUpdateEx("DELETE FROM T_Selection WHERE AD_PInstance_ID="+AD_PInstance_ID, getTrxName());
|
||||
DB.createT_Selection(AD_PInstance_ID, elements, getTrxName());
|
||||
|
||||
String whereClause = "1=1"; // some dummy where clause
|
||||
int[] ids = new Query(getCtx(), X_AD_Element.Table_Name, whereClause, getTrxName())
|
||||
.setOnlySelection(AD_PInstance_ID)
|
||||
.setOrderBy(X_AD_Element.COLUMNNAME_AD_Element_ID)
|
||||
.getIDs();
|
||||
assertEquals("Resulting number of elements differ", elements.size(), ids.length);
|
||||
|
||||
for (int i = 0; i < elements.size(); i++)
|
||||
{
|
||||
int expected = elements.get(i);
|
||||
assertEquals("Element "+i+" not equals", expected, ids[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue