forked from bimberlabinternal/LabDevKitModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContainerScopedTable.java
More file actions
347 lines (298 loc) · 13.6 KB
/
ContainerScopedTable.java
File metadata and controls
347 lines (298 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* Copyright (c) 2013-2019 LabKey Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.labkey.api.ldk.table;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.labkey.api.collections.CaseInsensitiveHashMap;
import org.labkey.api.data.ColumnInfo;
import org.labkey.api.data.CompareType;
import org.labkey.api.data.Container;
import org.labkey.api.data.ContainerFilter;
import org.labkey.api.data.ContainerManager;
import org.labkey.api.data.ContainerType;
import org.labkey.api.data.ConvertHelper;
import org.labkey.api.data.SimpleFilter;
import org.labkey.api.data.TableInfo;
import org.labkey.api.data.TableSelector;
import org.labkey.api.dataiterator.DataIterator;
import org.labkey.api.dataiterator.DataIteratorBuilder;
import org.labkey.api.dataiterator.DataIteratorContext;
import org.labkey.api.dataiterator.LoggingDataIterator;
import org.labkey.api.dataiterator.SimpleTranslator;
import org.labkey.api.query.BatchValidationException;
import org.labkey.api.query.DefaultQueryUpdateService;
import org.labkey.api.query.DuplicateKeyException;
import org.labkey.api.query.FieldKey;
import org.labkey.api.query.InvalidKeyException;
import org.labkey.api.query.QueryUpdateService;
import org.labkey.api.query.QueryUpdateServiceException;
import org.labkey.api.query.SimpleQueryUpdateService;
import org.labkey.api.query.SimpleUserSchema;
import org.labkey.api.query.UserSchema;
import org.labkey.api.query.ValidationException;
import org.labkey.api.security.User;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
/**
* This is designed to wrap a DB table which has a true PK (like an auto-incrementing rowid) and enforce a different
* column as the PK within a container.
*/
public class ContainerScopedTable<SchemaType extends UserSchema> extends CustomPermissionsTable<SchemaType>
{
private final String _pseudoPk;
private final Set<String> _realPKs = new HashSet<>();
private static final Logger _log = LogManager.getLogger(ContainerScopedTable.class);
public ContainerScopedTable(SchemaType schema, TableInfo st, ContainerFilter cf, String newPk)
{
super(schema, st, cf);
_pseudoPk = newPk;
}
@Override
public ContainerScopedTable<SchemaType> init()
{
super.init();
for (String col : getRealTable().getPkColumnNames())
{
var existing = getMutableColumn(col);
if (existing != null)
{
existing.setKeyField(false);
existing.setUserEditable(false);
existing.setHidden(true);
_realPKs.add(col);
}
}
var newKey = getMutableColumn(_pseudoPk);
assert newKey != null;
newKey.setKeyField(true);
return this;
}
@NotNull
@Override
public ContainerFilter getContainerFilter()
{
// TODO: does not look like anyone sets filter so don't think we need
return super.getContainerFilter(); // instanceof DelegatingContainerFilter ? super.getContainerFilter() : ContainerFilter.CURRENT;
}
@Override
public QueryUpdateService getUpdateService()
{
return new UpdateService(this);
}
@Override
public DataIteratorBuilder persistRows(DataIteratorBuilder data, DataIteratorContext context)
{
data = new ContainerScopedDataIteratorBuilder(data, context);
return super.persistRows(data, context);
}
protected class UpdateService extends SimpleQueryUpdateService
{
private final KeyManager _keyManager = new KeyManager();
public UpdateService(SimpleUserSchema.SimpleTable<?> ti)
{
super(ti, ti.getRealTable());
}
public UpdateService(SimpleUserSchema.SimpleTable<?> simpleTable, TableInfo table, DefaultQueryUpdateService.DomainUpdateHelper helper)
{
super(simpleTable, table, helper);
}
private ColumnInfo getPkCol()
{
assert _rootTable.getPkColumnNames().size() == 1;
return _rootTable.getPkColumns().get(0);
}
@Override
protected Map<String, Object> getRow(User user, Container container, Map<String, Object> keys) throws InvalidKeyException, QueryUpdateServiceException, SQLException
{
ColumnInfo pkCol = getPkCol();
keys = new CaseInsensitiveHashMap<>(keys); //create copy
if (!keys.containsKey(pkCol.getName()) || keys.get(pkCol.getName()) == null)
{
Object pseudoKey = keys.get(_pseudoPk);
if (pseudoKey != null)
{
SimpleFilter filter = new SimpleFilter(FieldKey.fromString(_pseudoPk), pseudoKey);
filter.addCondition(getContainerFieldKey(), container.getId());
TableSelector ts = new TableSelector(getQueryTable(), Collections.singleton(pkCol.getName()), filter, null);
Object[] results = ts.getArray(Object.class);
if (results.length == 0)
return null; // it should be up to the caller to decide if this is a problem/exception
else if (results.length > 1)
throw new InvalidKeyException("More than one existing row found key: " + pseudoKey);
keys.put(pkCol.getName(), results[0]);
}
}
return super.getRow(user, container, keys);
}
@Override
protected Map<String, Object> insertRow(User user, Container container, Map<String, Object> row) throws DuplicateKeyException, ValidationException, QueryUpdateServiceException, SQLException
{
Object value = row.get(_pseudoPk);
if (value != null && _keyManager.rowExists(container, value))
throw new ValidationException("There is already a record where " + _pseudoPk + " equals " + value);
return super.insertRow(user, container, row);
}
@Override
public List<Map<String,Object>> insertRows(User user, Container container, List<Map<String, Object>> rows, BatchValidationException errors, @Nullable Map<Enum, Object> configParameters, @Nullable Map<String, Object> extraScriptContext) throws DuplicateKeyException, QueryUpdateServiceException, SQLException
{
int idx = 1;
for (Map<String,Object> row : rows)
{
Object value = row.get(_pseudoPk);
if (value != null && _keyManager.rowExists(container, value))
{
ValidationException vex = new ValidationException("There is already a record where " + _pseudoPk + " equals " + value);
vex.setRowNumber(idx);
errors.addRowError(vex);
}
idx++;
}
return super.insertRows(user, container, rows, errors, configParameters, extraScriptContext);
}
@Override
protected Map<String, Object> updateRow(User user, Container container, Map<String, Object> row, @NotNull Map<String, Object> oldRow, @Nullable Map<Enum, Object> configParameters) throws InvalidKeyException, ValidationException, QueryUpdateServiceException, SQLException
{
Object oldValue = oldRow.get(_pseudoPk);
Object newValue = row.get(_pseudoPk);
if (newValue != null && !oldValue.equals(newValue) && _keyManager.rowExists(container, newValue))
{
if (!getSqlDialect().isCaseSensitive() & String.valueOf(newValue).equalsIgnoreCase(String.valueOf(oldValue)) & !String.valueOf(newValue).equals(String.valueOf(oldValue)))
{
// Changing case of the PK is allowable. If the DB is case-insensitive, then our DB test should tell us if this would create a violation
}
else
{
throw new ValidationException("There is already a record where " + _pseudoPk + " equals " + newValue);
}
}
return super.updateRow(user, container, row, oldRow, configParameters);
}
}
private class ContainerScopedDataIteratorBuilder implements DataIteratorBuilder
{
DataIteratorContext _context;
final DataIteratorBuilder _in;
ContainerScopedDataIteratorBuilder(@NotNull DataIteratorBuilder in, DataIteratorContext context)
{
_context = context;
_in = in;
}
@Override
public DataIterator getDataIterator(DataIteratorContext context)
{
_context = context;
DataIterator input = _in.getDataIterator(context);
if (null == input)
return null; // Can happen if context has errors
final String containerColName = getContainerFilterColumn();
final KeyManager keyManager = new KeyManager();
final SimpleTranslator it = new SimpleTranslator(input, context);
final Map<String, Integer> inputColMap = new HashMap<>();
for (int idx = 1; idx <= input.getColumnCount(); idx++)
{
ColumnInfo col = input.getColumnInfo(idx);
if (StringUtils.equalsIgnoreCase(_pseudoPk, col.getName()))
{
inputColMap.put(_pseudoPk, idx);
continue;
}
if (StringUtils.equalsIgnoreCase(getContainerFilterColumn(), col.getName()))
{
inputColMap.put(getContainerFilterColumn(), idx);
}
it.addColumn(idx);
}
//set the value of the RowId column
ColumnInfo pseudoPkCol = getColumn(_pseudoPk);
it.addColumn(pseudoPkCol, new Callable<>()
{
@Override
public Object call()
{
Container c = null;
if (inputColMap.containsKey(containerColName))
{
String containerId = (String) it.getInputColumnValue(inputColMap.get(containerColName));
if (containerId != null)
c = ContainerManager.getForId(containerId);
}
if (c == null)
{
c = getContainer();
}
assert c != null;
if (inputColMap.containsKey(_pseudoPk))
{
Object pesudoPkVal = it.getInputColumnValue(inputColMap.get(_pseudoPk));
if (pesudoPkVal != null)
{
// NOTE: this code is called for both inserts and updates:
if (_context.getInsertOption() == QueryUpdateService.InsertOption.INSERT && keyManager.rowExists(c, pesudoPkVal))
{
_context.getErrors().addRowError(new ValidationException("A record is already present with value: " + pesudoPkVal));
}
}
return pesudoPkVal;
}
_context.getErrors().addRowError(new ValidationException("Record is missing the keyfield"));
return null;
}
});
return LoggingDataIterator.wrap(it);
}
}
private class KeyManager
{
private final Set<Object> _encounteredKeys = new HashSet<>();
public KeyManager()
{
}
public boolean rowExists(Container c, Object key)
{
ColumnInfo pkCol = getColumn(FieldKey.fromString(_pseudoPk));
try
{
key = ConvertHelper.convert(key, pkCol.getJavaClass());
}
catch (ConversionException e)
{
_log.error("Unable to convert value for column: " + _pseudoPk + " to class: " + pkCol.getJavaClass() + " for table: " + getPublicSchemaName() + "." + getName(), e);
return false;
}
boolean exists = _encounteredKeys.contains(key);
_encounteredKeys.add(key);
if (exists)
{
return exists;
}
Container target = c.getContainerFor(ContainerType.DataType.sharedSchemaOwner);
SimpleFilter filter = new SimpleFilter(FieldKey.fromString(_pseudoPk), key, CompareType.EQUAL);
filter.addClause(ContainerFilter.current(target, getUserSchema().getUser()).createFilterClause(_rootTable.getSchema(), getContainerFieldKey()));
TableSelector ts = new TableSelector(_rootTable, Collections.singleton(_pseudoPk), filter, null);
return ts.getRowCount() > 0;
}
}
}