Skip to content

Commit

Permalink
Merge pull request #70 from VEuPathDB/enum-fixes
Browse files Browse the repository at this point in the history
Enum fixes
  • Loading branch information
ryanrdoherty authored Sep 7, 2023
2 parents bfd661c + 5f9c923 commit 5a8f3c4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,21 @@ public void setDisplayType(String displayType) throws WdkModelException {
}

/**
* Returns the minimun number of selections allowed for this param; the value
* in the model is intertwined with allowEmpty, which always takes precedence
* in a conflict with this value. Note that if this value is greater than
* Returns the minimum number of selections allowed for this param; the value
* in the model is intertwined with allowEmpty, which can sometimes conflict
* with the model value. Note that if this value is greater than
* that returned by getMaxSelectedCount(), there may be an empty range of the
* number of allowed values.
*
* min-sel -1 0 >0
* min-sel -1 0 >0
* --------------------
* allow 1 0 0 min
* empty 0 1 1 min
* allow| =1 0 0 min-sel
* empty| =0 1 1 min-sel
*
* @return The minimum number of allowed values for this param
*/
public int getMinSelectedCount() {
return _minSelectedCount > 1 ? _minSelectedCount : _allowEmpty ? 0 : 1;
return _minSelectedCount > 0 ? _minSelectedCount : _allowEmpty ? 0 : 1;
}

/**
Expand Down Expand Up @@ -508,6 +508,11 @@ protected boolean isEmptyValue(String value) {
return super.isEmptyValue(value) || new JSONArray(value).length() == 0;
}

@Override
public boolean isAllowEmpty() {
return getMinSelectedCount() == 0;
}

/**
* Builds the default value (and sanity default value) of the "current" enum values
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apache.log4j.Logger;
import org.gusdb.fgputil.FormatUtil;
import org.gusdb.fgputil.ListBuilder;
import org.gusdb.fgputil.iterator.IteratorUtil;
import org.gusdb.fgputil.json.JsonIterators;
import org.gusdb.wdk.model.WdkModelException;
import org.gusdb.wdk.model.query.spec.PartiallyValidatedStableValues;
Expand Down Expand Up @@ -512,6 +513,19 @@ String getValuesForSignature() {

abstract List<?> getSortedMembers();

protected <T> String createInCondition(String metadataTableName, String columnName, List<T> membersList) {
long conditionBatchSize = 1000;
StringBuilder multiCondition = new StringBuilder("( ");
for (int i = 0; i <= membersList.size() / conditionBatchSize; i++) {
Iterable<T> group = IteratorUtil.toIterable(membersList.stream()
.skip(i * conditionBatchSize)
.limit(conditionBatchSize)
.iterator());
if (i > 0) multiCondition.append(" OR ");
multiCondition.append(metadataTableName + "." + columnName + " IN (" + FormatUtil.join(group, ", ") + ") ");
}
return multiCondition.append(" )").toString();
}
}

private class NumberMembersFilter extends MembersFilter {
Expand Down Expand Up @@ -544,7 +558,7 @@ String getDisplayValue() {
@Override
protected String getValueSqlClause(String columnName, String metadataTableName) {
if (rawMembers.size() == 0) return "1 != 1";
return metadataTableName + "." + columnName + " IN (" + FormatUtil.join(rawMembers, ", ") + ") ";
return createInCondition(metadataTableName, columnName, rawMembers);
}

@Override
Expand Down Expand Up @@ -597,10 +611,15 @@ String getDisplayValue() {
protected String getValueSqlClause(String columnName, String metadataTableName) {
if (members.size() == 0) return "1 != 1";

List<String> membersEscaped = members.stream().map( member -> member.replaceAll("'", "''") ).collect( Collectors.toList() );

return metadataTableName + "." + columnName + " IN ('" + FormatUtil.join(membersEscaped, "', '") + "') ";
List<String> membersEscaped = members.stream()
// escape quotes
.map( member -> member.replaceAll("'", "''") )
// add quotes around members
.map( member -> "'" + member + "'")
// recollect
.collect( Collectors.toList() );

return createInCondition(metadataTableName, columnName, membersEscaped);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ public ParamValidity validate(PartiallyValidatedStableValues stableValues, Valid

// Determine if a default value must be generated
boolean defaultValueRequired =
stableValues.get(getName()) == null &&
isEmptyValue(value) &&
fillStrategy.shouldFillWhenMissing();
validationLog(() -> "defaultValueRequired = " + defaultValueRequired + " because value = " +
stableValues.get(getName()) + " and fillWhenMissing = " + fillStrategy.shouldFillWhenMissing());
Expand Down

0 comments on commit 5a8f3c4

Please sign in to comment.