Skip to content

Commit d43deaa

Browse files
Issue 52098, 49422: when looking up by alternate keys do that first so number names will resolve well (#6642)
1 parent 1b88987 commit d43deaa

32 files changed

Lines changed: 302 additions & 180 deletions

api/src/org/labkey/api/assay/AbstractAssayTsvDataHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ else if (mvIndicatorColumns.contains(column.name))
303303
{
304304
// Allow String values through if the column is a lookup and the settings allow lookups by alternate key.
305305
// The lookup table unique indices or display column value will be used to convert the column to the lookup value.
306-
if (!(settings.isAllowLookupByAlternateKey() && column.clazz == String.class && prop.getLookup() != null))
306+
if (!(settings.getLookupResolutionType().useAlternateKey() && column.clazz == String.class && prop.getLookup() != null))
307307
{
308308
// Otherwise, just use the expected PropertyDescriptor's column type
309309
column.clazz = prop.getPropertyDescriptor().getPropertyType().getJavaType();
@@ -771,7 +771,7 @@ else if (sampleLookup.isLookup())
771771
}
772772
}
773773

774-
if (dataTable != null && settings.isAllowLookupByAlternateKey())
774+
if (dataTable != null && settings.getLookupResolutionType().useAlternateKey())
775775
{
776776
ColumnInfo column = dataTable.getColumn(pd.getName());
777777
ForeignKey fk = column != null ? column.getFk() : null;
@@ -993,7 +993,7 @@ else if (o != remapped)
993993
// If allowLookupByAlternateKey is true or the sample lookup is by name, we call findExpMaterial which will attempt to resolve by name first and then rowId.
994994
// If allowLookupByAlternateKey is false, we will only try resolving by the rowId.
995995
ExpMaterial material = null;
996-
if (settings.isAllowLookupByAlternateKey() || isSampleLookupByName)
996+
if (settings.getLookupResolutionType().useAlternateKey() || isSampleLookupByName)
997997
{
998998
String materialName = o.toString();
999999
if (inputMaterials.containsKey(materialName))
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.labkey.api.data;
2+
3+
public enum LookupResolutionType
4+
{
5+
primaryKey(false), // known that the user will always supply the pk value
6+
alternateThenPrimaryKey(true); // If there is a situation where it's sometimes a primary and sometimes an alternate key, check for the alternate key first
7+
8+
final boolean _useAlternateKey;
9+
10+
LookupResolutionType(boolean useAlternateKey)
11+
{
12+
_useAlternateKey = useAlternateKey;
13+
}
14+
15+
public boolean useAlternateKey()
16+
{
17+
return _useAlternateKey;
18+
}
19+
20+
}

api/src/org/labkey/api/data/RemapCache.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*/
3434
public class RemapCache
3535
{
36-
Map<Key, SimpleTranslator.RemapPostConvert> remaps = new HashMap<>();
36+
Map<Key, SimpleTranslator.RemapConverter> remaps = new HashMap<>();
3737
private final boolean _allowBulkLoads;
3838

3939
public RemapCache()
@@ -140,17 +140,17 @@ private Key key(@NotNull TableInfo table)
140140
return new Key(table);
141141
}
142142

143-
private SimpleTranslator.RemapPostConvert remapper(Key key, Map<Key, SimpleTranslator.RemapPostConvert> remapCache, boolean includePkLookup)
143+
private SimpleTranslator.RemapConverter remapper(Key key, Map<Key, SimpleTranslator.RemapConverter> remapCache, boolean includePkLookup)
144144
{
145145
return remapCache.computeIfAbsent(key, (k) -> {
146146
TableInfo table = key.getTable();
147-
return new SimpleTranslator.RemapPostConvert(table, true, SimpleTranslator.RemapMissingBehavior.Null, _allowBulkLoads, includePkLookup, null);
147+
return new SimpleTranslator.RemapConverter(table, true, _allowBulkLoads, includePkLookup);
148148
});
149149
}
150150

151151
private <V> V remap(Key key, String value, boolean includePkLookup)
152152
{
153-
SimpleTranslator.RemapPostConvert remap = remapper(key, remaps, includePkLookup);
153+
SimpleTranslator.RemapConverter remap = remapper(key, remaps, includePkLookup);
154154
if (remap == null)
155155
throw new NotFoundException("Failed to create remap: " + key._schemaKey.toString() + "." + key._queryName);
156156
//noinspection unchecked

api/src/org/labkey/api/dataiterator/CoerceDataIterator.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import org.labkey.api.collections.CaseInsensitiveHashSet;
1919
import org.labkey.api.data.ColumnInfo;
20-
import org.labkey.api.data.JdbcType;
2120
import org.labkey.api.data.MultiValuedForeignKey;
2221
import org.labkey.api.data.TableInfo;
2322
import org.labkey.api.exp.PropertyType;
@@ -68,7 +67,7 @@ void init(TableInfo target, boolean useImportAliases, boolean outputAllColumns)
6867
else if (to.getFk() instanceof MultiValuedForeignKey)
6968
addColumn(to.getName(), i); // pass-through multi-value columns -- converting will stringify a collection
7069
else
71-
addConvertColumn(to.getName(), i, to.getJdbcType(), to.getFk(), RemapMissingBehavior.OriginalValue);
70+
addConvertColumn(to.getName(), i, to.getJdbcType(), to.getFk(), RemapMissingBehavior.Error, true);
7271
}
7372
else
7473
{
@@ -88,10 +87,4 @@ else if (to.getFk() instanceof MultiValuedForeignKey)
8887
}
8988
}
9089
}
91-
92-
@Override
93-
protected Object addConversionException(String fieldName, Object value, JdbcType target, Exception x)
94-
{
95-
return value;
96-
}
9790
}

api/src/org/labkey/api/dataiterator/DataIteratorContext.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.jetbrains.annotations.NotNull;
2020
import org.jetbrains.annotations.Nullable;
2121
import org.labkey.api.collections.CaseInsensitiveHashSet;
22+
import org.labkey.api.data.LookupResolutionType;
2223
import org.labkey.api.query.BatchValidationException;
2324
import org.labkey.api.query.QueryImportPipelineJob;
2425
import org.labkey.api.query.QueryUpdateService;
@@ -50,7 +51,7 @@ public class DataIteratorContext
5051
boolean _failFast = true;
5152
boolean _verbose = false;
5253
boolean _supportAutoIncrementKey = false;
53-
boolean _allowImportLookupByAlternateKey = false;
54+
LookupResolutionType _lookupResolutionType = LookupResolutionType.primaryKey;
5455
QueryImportPipelineJob _backgroundJob = null;
5556
boolean _crossTypeImport = false;
5657
boolean _crossFolderImport = false;
@@ -60,6 +61,7 @@ public class DataIteratorContext
6061
private final Set<String> _dontUpdateColumnNames = new CaseInsensitiveHashSet();
6162
private final Set<String> _alternateKeys = new CaseInsensitiveHashSet();
6263
private String _dataSource;
64+
private boolean _withLookupRemapping = true;
6365

6466
private final Map<String, Object> _responseInfo = new HashMap<>(); // information from the import/loadRows context to be passed back to the API response object
6567
private Logger _logger;
@@ -162,15 +164,31 @@ public void setSupportAutoIncrementKey(boolean supportAutoIncrementKey)
162164
_supportAutoIncrementKey = supportAutoIncrementKey;
163165
}
164166

165-
public boolean isAllowImportLookupByAlternateKey()
167+
@NotNull
168+
public LookupResolutionType getLookupResolutionType()
169+
{
170+
return _lookupResolutionType == null ? LookupResolutionType.primaryKey : _lookupResolutionType;
171+
}
172+
173+
public void setLookupResolutionType(LookupResolutionType lookupResolutionType)
174+
{
175+
_lookupResolutionType = lookupResolutionType;
176+
}
177+
178+
public boolean isWithLookupRemapping()
179+
{
180+
return _withLookupRemapping;
181+
}
182+
183+
public void setWithLookupRemapping(boolean withLookupRemapping)
166184
{
167-
return _allowImportLookupByAlternateKey;
185+
_withLookupRemapping = withLookupRemapping;
168186
}
169187

170188
/** When true, allow importing lookup columns by the lookup table's alternate key instead of by primary key. */
171189
public void setAllowImportLookupByAlternateKey(boolean allowImportLookupByAlternateKey)
172190
{
173-
_allowImportLookupByAlternateKey = allowImportLookupByAlternateKey;
191+
_lookupResolutionType = allowImportLookupByAlternateKey ? LookupResolutionType.alternateThenPrimaryKey : LookupResolutionType.primaryKey;
174192
}
175193

176194
public boolean isCrossTypeImport()

api/src/org/labkey/api/dataiterator/EmbargoDataIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/**
1414
* StatementDataIterator is already complicated enough, so the cache-ahead functionality is implemented by a separate class called
1515
* EmbargoDataIterator. This class is similar to CachingDataIterator, however, where CachingDataIterator
16-
* caches rows that have have already been seen, EmbargoDataIterator caches rows _ahead_ and holds them back until
16+
* caches rows that have already been seen, EmbargoDataIterator caches rows _ahead_ and holds them back until
1717
* the StatementDataIterator indicates that they have been 'saved' and may be released.
1818
*/
1919

0 commit comments

Comments
 (0)