I have a table where I have saved some parameters which act as logic for deciding which "receiver" to choose. I have illustrated the content / data of this table below:

For selecting the correct receiver I used the following SQL:
SELECT *
FROM tableOfReceivers
WHERE client = 'DU'
AND (country = countryOfDestination OR '*' = countryOfDestination)
AND (
(packages >= minNumberOfPackages OR minNumberOfPackages IS NULL) AND
(packages <= maxNumberOfPackages OR maxNumberOfPackages IS NULL)
)
AND (
(weight >= minNetWeight OR minNetWeight IS NULL) AND
(weight <= maxNetWeight OR maxNetWeight IS NULL)
)
ORDER BY priority;
The variables for above SQL is:
country = BE
packages = 5
weight = 50
The SQL works fine and I get the result I want. But I need to translate this into Java and when doing so I feel that it's over-complicated and I'm thinking that there has to be an easier what to do this.
Here is the Java code that I'm currently using:
ArrayList<tableOfReceivers > listOfReceivers = getPersistenceManager().getQueryManager().queryAll(
tableOfReceivers.class, "this.client = ?1", "this.client", new Object[] { client }, 0);
ArrayList<tableOfReceivers> result = new ArrayList<tableOfReceivers>();
for (tableOfReceivers rec : listOfReceivers) {
if ((StringUtils.equals(countryOfDestination, rec.getCountryOfDestination())
|| StringUtils.equals("*", rec.getCountryOfDestination()))) {
if (((numberOfPackages >= nvl(rec.getMinNumberOfPackages(), 0))
|| rec.getMinNumberOfPackages() == null)
&& (numberOfPackages <= nvl(rec.getMaxNumberOfPackages(),0))
|| rec.getMaxNumberOfPackages() == null) {
if (((totalNetWeight >= nvl(rec.getMinNetWeight(),0)) || rec.getMinNetWeight() == null)
&& (totalNetWeight <= nvl(rec.getMaxNetWeight(),0)) || rec.getMaxNetWeight() == null) {
result.add(rec);
}
}
}
}
Collections.sort(result, new Comparator<tableOfReceivers >() {
@Override
public int compare(tableOfReceivers o1, tableOfReceivers o2) {
return o1.getPriority().compareTo(o2.getPriority());
}
});
System.out.println("Receiver selection result is:" + "\n");
if(!result.isEmpty()){
System.out.println(result.get(0).getReceiver() + "\n");
}
}
public static Integer nvl(Integer value, Integer alternateValue) {
if (value == null)
return alternateValue;
return value;
}
So, is there an easier and better way to fetch what I need using only Java here?
Thanks!