-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathExpDataImpl.java
More file actions
978 lines (837 loc) · 32.9 KB
/
ExpDataImpl.java
File metadata and controls
978 lines (837 loc) · 32.9 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
/*
* Copyright (c) 2008-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.experiment.api;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.json.JSONObject;
import org.labkey.api.collections.CaseInsensitiveHashSet;
import org.labkey.api.collections.LongHashMap;
import org.labkey.api.data.Container;
import org.labkey.api.data.ContainerManager;
import org.labkey.api.data.SQLFragment;
import org.labkey.api.data.SimpleFilter;
import org.labkey.api.data.SqlSelector;
import org.labkey.api.data.Table;
import org.labkey.api.data.TableInfo;
import org.labkey.api.exp.ExperimentDataHandler;
import org.labkey.api.exp.ExperimentException;
import org.labkey.api.exp.Handler;
import org.labkey.api.exp.ObjectProperty;
import org.labkey.api.exp.XarFormatException;
import org.labkey.api.exp.XarSource;
import org.labkey.api.exp.api.DataType;
import org.labkey.api.exp.api.ExpData;
import org.labkey.api.exp.api.ExpDataClass;
import org.labkey.api.exp.api.ExpRun;
import org.labkey.api.exp.api.ExperimentService;
import org.labkey.api.exp.query.ExpDataClassDataTable;
import org.labkey.api.exp.query.ExpDataTable;
import org.labkey.api.exp.query.ExpSchema;
import org.labkey.api.files.FileContentService;
import org.labkey.api.pipeline.PipeRoot;
import org.labkey.api.pipeline.PipelineJob;
import org.labkey.api.pipeline.PipelineService;
import org.labkey.api.query.FieldKey;
import org.labkey.api.query.QueryRowReference;
import org.labkey.api.query.QueryService;
import org.labkey.api.query.ValidationException;
import org.labkey.api.search.SearchResultTemplate;
import org.labkey.api.search.SearchScope;
import org.labkey.api.search.SearchService;
import org.labkey.api.security.User;
import org.labkey.api.security.permissions.DataClassReadPermission;
import org.labkey.api.security.permissions.DeletePermission;
import org.labkey.api.security.permissions.MediaReadPermission;
import org.labkey.api.security.permissions.MoveEntitiesPermission;
import org.labkey.api.security.permissions.Permission;
import org.labkey.api.security.permissions.UpdatePermission;
import org.labkey.api.util.FileUtil;
import org.labkey.api.util.GUID;
import org.labkey.api.util.HtmlString;
import org.labkey.api.util.LinkBuilder;
import org.labkey.api.util.MimeMap;
import org.labkey.api.util.NetworkDrive;
import org.labkey.api.util.Pair;
import org.labkey.api.util.Path;
import org.labkey.api.util.StringUtilsLabKey;
import org.labkey.api.util.URLHelper;
import org.labkey.api.util.InputBuilder;
import org.labkey.api.view.ActionURL;
import org.labkey.api.view.HttpView;
import org.labkey.api.view.NavTree;
import org.labkey.api.view.ViewContext;
import org.labkey.api.webdav.SimpleDocumentResource;
import org.labkey.api.webdav.WebdavResource;
import org.labkey.experiment.controllers.exp.ExperimentController;
import org.labkey.vfs.FileLike;
import org.labkey.vfs.FileSystemLike;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import static org.labkey.api.exp.query.ExpSchema.SCHEMA_EXP_DATA;
public class ExpDataImpl extends AbstractRunItemImpl<Data> implements ExpData
{
public enum DataOperations
{
Edit("editing", UpdatePermission.class),
EditLineage("editing lineage", UpdatePermission.class),
Delete("deleting", DeletePermission.class),
Move("moving", MoveEntitiesPermission.class);
private final String _description; // used as a suffix in messaging users about what is not allowed
private final Class<? extends Permission> _permissionClass;
DataOperations(String description, Class<? extends Permission> permissionClass)
{
_description = description;
_permissionClass = permissionClass;
}
public String getDescription()
{
return _description;
}
public Class<? extends Permission> getPermissionClass()
{
return _permissionClass;
}
}
public static final SearchService.SearchCategory expDataCategory = new SearchService.SearchCategory("data", "ExpData", false) {
@Override
public Set<String> getPermittedContainerIds(User user, Map<String, Container> containers)
{
return getPermittedContainerIds(user, containers, DataClassReadPermission.class);
}
};
public static final SearchService.SearchCategory expMediaDataCategory = new SearchService.SearchCategory("mediaData", "ExpData for media objects", false) {
@Override
public Set<String> getPermittedContainerIds(User user, Map<String, Container> containers)
{
return getPermittedContainerIds(user, containers, MediaReadPermission.class);
}
};
/** Cache this because it can be expensive to recompute */
private Boolean _finalRunOutput;
/**
* Temporary mapping until experiment.xml contains the mime type
*/
private static final MimeMap MIME_MAP = new MimeMap();
static public List<ExpDataImpl> fromDatas(List<Data> datas)
{
List<ExpDataImpl> ret = new ArrayList<>(datas.size());
for (Data data : datas)
{
ret.add(new ExpDataImpl(data));
}
return ret;
}
// For serialization
protected ExpDataImpl() {}
public ExpDataImpl(Data data)
{
super(data);
}
@Override
public void setComment(User user, String comment) throws ValidationException
{
setComment(user, comment, true);
}
@Override
public void setComment(User user, String comment, boolean index) throws ValidationException
{
super.setComment(user, comment);
if (index)
index(SearchService.get().defaultTask().getQueue(getContainer(), SearchService.PRIORITY.modified), null);
}
@Override
@Nullable
public ActionURL detailsURL()
{
DataType dataType = getDataType();
if (dataType != null)
{
ActionURL url = dataType.getDetailsURL(this);
if (url != null)
return url;
}
return _object.detailsURL();
}
@Override
public @Nullable QueryRowReference getQueryRowReference()
{
return getQueryRowReference(null);
}
@Override
public @Nullable QueryRowReference getQueryRowReference(@Nullable User user)
{
ExpDataClassImpl dc = getDataClass(user);
if (dc != null)
return new QueryRowReference(getContainer(), SCHEMA_EXP_DATA, dc.getName(), FieldKey.fromParts(ExpDataTable.Column.RowId), getRowId());
// Issue 40123: see MedImmuneDataHandler MEDIMMUNE_DATA_TYPE, this claims the "Data" namespace
DataType type = getDataType();
if (type != null)
{
QueryRowReference queryRowReference = type.getQueryRowReference(this);
if (queryRowReference != null)
return queryRowReference;
}
return new QueryRowReference(getContainer(), ExpSchema.SCHEMA_EXP, ExpSchema.TableType.Data.name(), FieldKey.fromParts(ExpDataTable.Column.RowId), getRowId());
}
@Override
public List<ExpProtocolApplicationImpl> getTargetApplications()
{
return getTargetApplications(new SimpleFilter(FieldKey.fromParts("DataId"), getRowId()), ExperimentServiceImpl.get().getTinfoDataInput());
}
@Override
public List<ExpRunImpl> getTargetRuns()
{
return getTargetRuns(ExperimentServiceImpl.get().getTinfoDataInput(), "DataId");
}
@Override
public DataType getDataType()
{
return ExperimentService.get().getDataType(getLSIDNamespacePrefix());
}
@Override
public void setDataFileURI(URI uri)
{
ensureUnlocked();
_object.setDataFileUrl(ExpData.normalizeDataFileURI(uri));
}
@Override
public void save(User user)
{
// Replace the default "Data" cpastype if the Data belongs to a DataClass
ExpDataClassImpl dataClass = getDataClass();
if (dataClass != null && ExpData.DEFAULT_CPAS_TYPE.equals(getCpasType()))
setCpasType(dataClass.getLSID());
boolean isNew = getRowId() == 0;
save(user, ExperimentServiceImpl.get().getTinfoData(), true);
if (isNew)
{
if (dataClass != null)
{
Map<String, Object> map = new HashMap<>();
map.put("lsid", getLSID());
Table.insert(user, dataClass.getTinfo(), map);
}
}
index(SearchService.get().defaultTask().getQueue(getContainer(), SearchService.PRIORITY.modified), null);
}
@Override
protected void save(User user, TableInfo table, boolean ensureObject)
{
assert ensureObject;
super.save(user, table, true);
}
@Override
public URI getDataFileURI()
{
String url = _object.getDataFileUrl();
if (url == null)
return null;
try
{
return new URI(_object.getDataFileUrl());
}
catch (URISyntaxException use)
{
return null;
}
}
@Override
public ExperimentDataHandler findDataHandler()
{
return Handler.Priority.findBestHandler(ExperimentServiceImpl.get().getExperimentDataHandlers(), this);
}
@Override
public String getDataFileUrl()
{
return _object.getDataFileUrl();
}
@Override
public boolean hasFileScheme()
{
return !FileUtil.hasCloudScheme(getDataFileUrl());
}
@Override
@Nullable
public File getFile()
{
return _object.getFile();
}
@Override
public @Nullable FileLike getFileLike()
{
return _object.getFileLike();
}
@Override
@Nullable
public java.nio.file.Path getFilePath()
{
return _object.getFilePath();
}
@Override
public boolean isInlineImage()
{
return null != getFile() && MIME_MAP.isInlineImageFor(getFile());
}
@Override
public void delete(User user)
{
delete(user, true);
}
@Override
public void delete(User user, boolean deleteRunsUsingData)
{
ExperimentServiceImpl.get().deleteDataByRowIds(user, getContainer(), Collections.singleton(getRowId()), deleteRunsUsingData);
}
public String getMimeType()
{
if (null != getDataFileUrl())
return MIME_MAP.getContentTypeFor(getDataFileUrl());
else
return null;
}
@Override
public boolean isFileOnDisk()
{
java.nio.file.Path f = getFilePath();
if (f != null)
if (!FileUtil.hasCloudScheme(f))
return NetworkDrive.exists(f.toFile()) && !Files.isDirectory(f);
else
return Files.exists(f);
else
return false;
}
public boolean isPathAccessible()
{
java.nio.file.Path path = getFilePath();
return (null != path && Files.exists(path));
}
@Override
public String getCpasType()
{
String result = _object.getCpasType();
if (result != null)
return result;
ExpDataClass dataClass = getDataClass();
if (dataClass != null)
return dataClass.getLSID();
return ExpData.DEFAULT_CPAS_TYPE;
}
public void setGenerated(boolean generated)
{
ensureUnlocked();
_object.setGenerated(generated);
}
@Override
public boolean isGenerated()
{
return _object.isGenerated();
}
@Override
public boolean isFinalRunOutput()
{
if (_finalRunOutput == null)
{
ExpRun run = getRun();
_finalRunOutput = run != null && run.isFinalOutput(this);
}
return _finalRunOutput.booleanValue();
}
@Override
@Nullable
public ExpDataClassImpl getDataClass()
{
return getDataClass(null);
}
@Override
@Nullable
public ExpDataClassImpl getDataClass(@Nullable User user)
{
if (_object.getClassId() != null && getContainer() != null)
{
if (user == null)
return ExperimentServiceImpl.get().getDataClass(getContainer(), _object.getClassId());
else
return ExperimentServiceImpl.get().getDataClass(getContainer(), user, _object.getClassId());
}
return null;
}
@Override
public void importDataFile(PipelineJob job, XarSource xarSource) throws ExperimentException
{
String dataFileURL = getDataFileUrl();
if (dataFileURL == null)
return;
if (xarSource.shouldIgnoreDataFiles())
{
job.debug("Skipping load of data file " + dataFileURL + " based on the XAR source");
return;
}
job.debug("Trying to load data file " + dataFileURL + " into the system");
java.nio.file.Path path = FileUtil.stringToPath(getContainer(), dataFileURL);
if (!Files.exists(path))
{
job.debug("Unable to find the data file " + FileUtil.getAbsolutePath(getContainer(), path) + " on disk.");
return;
}
// Check that the file is under the pipeline root to prevent users from referencing a file that they
// don't have permission to import
PipeRoot pr = PipelineService.get().findPipelineRoot(job.getContainer());
if (!xarSource.allowImport(pr, job.getContainer(), path))
{
if (pr == null)
{
job.warn("No pipeline root was set, skipping load of file " + FileUtil.getAbsolutePath(getContainer(), path));
return;
}
job.debug("The data file " + FileUtil.getAbsolutePath(getContainer(), path) + " is not under the folder's pipeline root: " + pr + ". It will not be loaded directly, but may be loaded if referenced from other files that are under the pipeline root.");
return;
}
ExperimentDataHandler handler = findDataHandler();
try
{
handler.importFile(this, FileSystemLike.wrapFile(path), job.getInfo(), job.getLogger(), xarSource.getXarContext());
}
catch (ExperimentException e)
{
throw new XarFormatException(e);
}
job.debug("Finished trying to load data file " + dataFileURL + " into the system");
}
// Get all text and int strings from the data class for indexing
private void getIndexValues(
Map<String, Object> props,
@NotNull ExpDataClassDataTableImpl table,
Set<String> identifiersHi,
Set<String> identifiersMed,
Set<String> identifiersLo,
Set<String> keywordHi,
Set<String> keywordMed,
Set<String> keywordsLo,
JSONObject jsonData
)
{
CaseInsensitiveHashSet skipColumns = new CaseInsensitiveHashSet();
for (ExpDataClassDataTable.Column column : ExpDataClassDataTable.Column.values())
skipColumns.add(column.name());
skipColumns.add("Ancestors");
skipColumns.add("Container");
processIndexValues(props, table, skipColumns, identifiersHi, identifiersMed, identifiersLo, keywordHi, keywordMed, keywordsLo, jsonData);
}
@Override
@NotNull
public Collection<String> getAliases()
{
TableInfo mapTi = ExperimentService.get().getTinfoDataAliasMap();
TableInfo ti = ExperimentService.get().getTinfoAlias();
SQLFragment sql = new SQLFragment()
.append("SELECT a.name FROM ").append(mapTi, "m")
.append(" JOIN ").append(ti, "a")
.append(" ON m.alias = a.RowId WHERE m.lsid = ? ");
sql.add(getLSID());
ArrayList<String> aliases = new SqlSelector(mapTi.getSchema(), sql).getArrayList(String.class);
return Collections.unmodifiableList(aliases);
}
@Override
public String getDocumentId()
{
String dataClassName = "-";
ExpDataClass dc = getDataClass();
if (dc != null)
dataClassName = dc.getName();
// why not just data:rowId?
return "data:" + new Path(getContainer().getId(), dataClassName, Long.toString(getRowId())).encode();
}
@Override
public Map<String, ObjectProperty> getObjectProperties()
{
return getObjectProperties(getDataClass());
}
@Override
public Map<String, ObjectProperty> getObjectProperties(@Nullable User user)
{
return getObjectProperties(getDataClass(user));
}
private Map<String, ObjectProperty> getObjectProperties(ExpDataClassImpl dataClass)
{
HashMap<String,ObjectProperty> ret = new HashMap<>(super.getObjectProperties());
var ti = null == dataClass ? null : dataClass.getTinfo();
if (null != ti)
{
ret.putAll(getObjectProperties(ti));
}
return ret;
}
private static Pair<Long, ExpDataClass> getRowIdClassNameContainerFromDocumentId(String resourceIdentifier, Map<String, ExpDataClassImpl> dcCache)
{
if (resourceIdentifier.startsWith("data:"))
resourceIdentifier = resourceIdentifier.substring("data:".length());
Path path = Path.parse(resourceIdentifier);
if (path.size() != 3)
return null;
String containerId = path.get(0);
String dataClassName = path.get(1);
String rowIdString = path.get(2);
long rowId;
try
{
rowId = Long.parseLong(rowIdString);
if (rowId == 0)
return null;
}
catch (NumberFormatException ex)
{
return null;
}
Container c = ContainerManager.getForId(containerId);
if (c == null)
return null;
ExpDataClass dc = null;
if (!StringUtils.isEmpty(dataClassName) && !dataClassName.equals("-"))
{
String dcKey = containerId + '-' + dataClassName;
dc = dcCache.computeIfAbsent(dcKey, (x) -> ExperimentServiceImpl.get().getDataClass(c, dataClassName));
}
return new Pair<>(rowId, dc);
}
@Nullable
public static ExpDataImpl fromDocumentId(String resourceIdentifier)
{
Pair<Long, ExpDataClass> rowIdDataClass = getRowIdClassNameContainerFromDocumentId(resourceIdentifier, new HashMap<>());
if (rowIdDataClass == null)
return null;
Long rowId = rowIdDataClass.first;
ExpDataClass dc = rowIdDataClass.second;
if (dc != null)
return ExperimentServiceImpl.get().getExpData(dc, rowId);
else
return ExperimentServiceImpl.get().getExpData(rowId);
}
@Nullable
public static Map<String, ExpData> fromDocumentIds(Collection<String> resourceIdentifiers)
{
Map<Long, String> rowIdIdentifierMap = new LongHashMap<>();
Map<String, ExpDataClassImpl> dcCache = new HashMap<>();
Map<Long, ExpDataClass> dcMap = new LongHashMap<>();
Map<Long, List<Long>> dcRowIdMap = new LongHashMap<>(); // data rowIds with dataClass
List<Long> rowIds = new ArrayList<>(); // data rowIds without dataClass
for (String resourceIdentifier : resourceIdentifiers)
{
Pair<Long, ExpDataClass> rowIdDataClass = getRowIdClassNameContainerFromDocumentId(resourceIdentifier, dcCache);
if (rowIdDataClass == null)
continue;
Long rowId = rowIdDataClass.first;
ExpDataClass dc = rowIdDataClass.second;
rowIdIdentifierMap.put(rowId, resourceIdentifier);
if (dc != null)
{
dcMap.put(dc.getRowId(), dc);
dcRowIdMap
.computeIfAbsent(dc.getRowId(), (k) -> new ArrayList<>())
.add(rowId);
}
else
rowIds.add(rowId);
}
List<ExpData> expDatas = new ArrayList<>();
if (!rowIds.isEmpty())
expDatas.addAll(ExperimentServiceImpl.get().getExpDatas(rowIds));
if (!dcRowIdMap.isEmpty())
{
for (Long dataClassId : dcRowIdMap.keySet())
{
ExpDataClass dc = dcMap.get(dataClassId);
if (dc != null)
expDatas.addAll(ExperimentServiceImpl.get().getExpDatas(dc, dcRowIdMap.get(dataClassId)));
}
}
Map<String, ExpData> identifierDatas = new HashMap<>();
for (ExpData data : expDatas)
{
identifierDatas.put(rowIdIdentifierMap.get(data.getRowId()), data);
}
return identifierDatas;
}
@Override
public @Nullable URI getWebDavURL(@NotNull FileContentService.PathType type)
{
java.nio.file.Path path = getFilePath();
if (path == null)
{
return null;
}
Container c = getContainer();
if (c == null)
{
return null;
}
return FileContentService.get().getWebDavUrl(path, c, type);
}
@Override
public @Nullable WebdavResource createIndexDocument(@Nullable TableInfo tableInfo)
{
Container container = getContainer();
if (container == null)
return null;
Map<String, Object> props = new HashMap<>();
JSONObject jsonData = new JSONObject();
Set<String> keywordsHi = new HashSet<>();
Set<String> keywordsMed = new HashSet<>();
Set<String> keywordsLo = new HashSet<>();
Set<String> identifiersHi = new HashSet<>();
Set<String> identifiersMed = new HashSet<>();
Set<String> identifiersLo = new HashSet<>();
StringBuilder body = new StringBuilder();
// Name is an identifier with the highest weight
identifiersHi.add(getName());
keywordsMed.add(getName()); // also add to keywords since those are stemmed
// Description is added as a keywordsLo -- in Biologics it is common for the description to
// contain names of other DataClasses, e.g., "Mature desK of PS-10", which would be tokenized as
// [mature, desk, ps, 10] if added it as a keyword so we lower its priority to avoid useless results.
// CONSIDER: tokenize the description and extract identifiers
if (null != getDescription())
keywordsLo.add(getDescription());
String comment = getComment();
if (comment != null)
keywordsMed.add(comment);
// Add aliases in parentheses in the title
StringBuilder title = new StringBuilder(getName());
Collection<String> aliases = getAliases();
if (!aliases.isEmpty())
{
title.append(" (").append(StringUtils.join(aliases, ", ")).append(")");
identifiersHi.addAll(aliases);
}
ExpDataClassImpl dc = getDataClass(User.getSearchUser());
if (dc != null)
{
ActionURL show = new ActionURL(ExperimentController.ShowDataClassAction.class, container).addParameter("rowId", dc.getRowId());
NavTree t = new NavTree(dc.getName(), show);
String nav = NavTree.toJS(Collections.singleton(t), null, false, true).toString();
props.put(SearchService.PROPERTY.navtrail.toString(), nav);
props.put(DataSearchResultTemplate.PROPERTY, dc.getName());
body.append(dc.getName());
if (tableInfo == null)
tableInfo = QueryService.get().getUserSchema(User.getSearchUser(), container, SCHEMA_EXP_DATA).getTable(dc.getName());
if (!(tableInfo instanceof ExpDataClassDataTableImpl expDataClassDataTable))
throw new IllegalArgumentException(String.format("Unable to index data class item in %s. Table must be an instance of %s", dc.getName(), ExpDataClassDataTableImpl.class.getName()));
if (!expDataClassDataTable.getDataClass().equals(dc))
throw new IllegalArgumentException(String.format("Data class table mismatch for %s", dc.getName()));
// Collect other text columns and lookup display columns
getIndexValues(props, expDataClassDataTable, identifiersHi, identifiersMed, identifiersLo, keywordsHi, keywordsMed, keywordsLo, jsonData);
}
// === Stored, not indexed
if (dc != null && dc.isMedia())
props.put(SearchService.PROPERTY.categories.toString(), expMediaDataCategory.toString());
else
props.put(SearchService.PROPERTY.categories.toString(), expDataCategory.toString());
props.put(SearchService.PROPERTY.title.toString(), title.toString());
props.put(SearchService.PROPERTY.jsonData.toString(), jsonData);
ActionURL view = ExperimentController.ExperimentUrlsImpl.get().getDataDetailsURL(this);
view.setExtraPath(container.getId());
String docId = getDocumentId();
// Generate a summary explicitly instead of relying on a summary to be extracted
// from the document body. Placing lookup values and the description in the body
// would tokenize using the English analyzer and index "PS-12" as ["ps", "12"] which leads to poor results.
StringBuilder summary = new StringBuilder();
if (StringUtils.isNotEmpty(getDescription()))
summary.append(getDescription()).append("\n");
appendTokens(summary, keywordsMed);
appendTokens(summary, identifiersMed);
appendTokens(summary, identifiersLo);
props.put(SearchService.PROPERTY.summary.toString(), summary);
return new ExpDataResource(
getRowId(),
new Path(docId),
docId,
container.getEntityId(),
"text/plain",
body.toString(),
view,
props,
getCreatedBy(),
getCreated(),
getModifiedBy(),
getModified()
);
}
private static void appendTokens(StringBuilder sb, Collection<String> toks)
{
if (toks.isEmpty())
return;
sb.append(toks.stream().map(s -> s.length() > 30 ? StringUtilsLabKey.leftSurrogatePairFriendly(s, 30) + "\u2026" : s).collect(Collectors.joining(", "))).append("\n");
}
private static class ExpDataResource extends SimpleDocumentResource
{
final long _rowId;
public ExpDataResource(long rowId, Path path, String documentId, GUID containerId, String contentType, String body, URLHelper executeUrl, Map<String, Object> properties, User createdBy, Date created, User modifiedBy, Date modified)
{
super(path, documentId, containerId, contentType, body, executeUrl, createdBy, created, modifiedBy, modified, properties);
_rowId = rowId;
}
@Override
public void setLastIndexed(long ms, long modified)
{
ExperimentServiceImpl.get().setDataLastIndexed(_rowId, ms);
}
}
public static class DataSearchResultTemplate implements SearchResultTemplate
{
public static final String NAME = "data";
public static final String PROPERTY = "dataclass";
@Nullable
@Override
public String getName()
{
return NAME;
}
private ExpDataClass getDataClass()
{
if (HttpView.hasCurrentView())
{
ViewContext ctx = HttpView.currentContext();
String dataclass = ctx.getActionURL().getParameter(PROPERTY);
if (dataclass != null)
return ExperimentService.get().getDataClass(ctx.getContainer(), ctx.getUser(), dataclass);
}
return null;
}
@Nullable
@Override
public String getCategories()
{
ExpDataClass dataClass = getDataClass();
if (dataClass != null && dataClass.isMedia())
return expMediaDataCategory.getName();
return expDataCategory.getName();
}
@Nullable
@Override
public SearchScope getSearchScope()
{
return SearchScope.FolderAndSubfolders;
}
@NotNull
@Override
public String getResultNameSingular()
{
ExpDataClass dc = getDataClass();
if (dc != null)
return dc.getName();
return "data";
}
@NotNull
@Override
public String getResultNamePlural()
{
return getResultNameSingular();
}
@Override
public boolean includeNavigationLinks()
{
return true;
}
@Override
public boolean includeAdvanceUI()
{
return false;
}
@Nullable
@Override
public HtmlString getExtraHtml(ViewContext ctx)
{
String q = ctx.getActionURL().getParameter("q");
if (StringUtils.isNotBlank(q))
{
String dataclass = ctx.getActionURL().getParameter(PROPERTY);
ActionURL url = ctx.cloneActionURL().deleteParameter(PROPERTY);
url.replaceParameter(ActionURL.Param._dc, (int)Math.round(1000 * Math.random()));
StringBuilder html = new StringBuilder();
html.append("<div class=\"labkey-search-filter\">");
appendParam(html, null, dataclass, "All", false, url);
for (ExpDataClass dc : ExperimentService.get().getDataClasses(ctx.getContainer(), ctx.getUser(), true))
{
appendParam(html, dc.getName(), dataclass, dc.getName(), true, url);
}
html.append("</div>");
return HtmlString.unsafe(html.toString());
}
else
{
return null;
}
}
private void appendParam(StringBuilder sb, @Nullable String dataclass, @Nullable String current, @NotNull String label, boolean addParam, ActionURL url)
{
sb.append("<span>");
if (!Objects.equals(dataclass, current))
{
if (addParam)
url = url.clone().addParameter(PROPERTY, dataclass);
sb.append(LinkBuilder.simpleLink(label, url));
}
else
{
sb.append(label);
}
sb.append("</span> ");
}
@Override
public HtmlString getHiddenInputsHtml(ViewContext ctx)
{
String dataclass = ctx.getActionURL().getParameter(PROPERTY);
if (dataclass != null)
{
return InputBuilder.hidden().id("search-type").name(PROPERTY).value(dataclass).getHtmlString();
}
return null;
}
@Override
public String reviseQuery(ViewContext ctx, String q)
{
String dataclass = ctx.getActionURL().getParameter(PROPERTY);
if (null != dataclass)
return "+(" + q + ") +" + PROPERTY + ":" + dataclass;
else
return q;
}
@Override
public void addNavTrail(NavTree root, ViewContext ctx, @NotNull SearchScope scope, @Nullable String category)
{
SearchResultTemplate.super.addNavTrail(root, ctx, scope, category);
String dataclass = ctx.getActionURL().getParameter(PROPERTY);
if (dataclass != null)
{
String text = root.getText();
root.setText(text + " - " + dataclass);
}
}
}
}