6200 |
08 Apr 21 |
nicklas |
1 |
package net.sf.basedb.reggie.plugins.cmd; |
6200 |
08 Apr 21 |
nicklas |
2 |
|
6200 |
08 Apr 21 |
nicklas |
3 |
import java.util.Date; |
6205 |
12 Apr 21 |
nicklas |
4 |
import java.util.List; |
6200 |
08 Apr 21 |
nicklas |
5 |
|
6205 |
12 Apr 21 |
nicklas |
6 |
import net.sf.basedb.core.DbControl; |
6205 |
12 Apr 21 |
nicklas |
7 |
import net.sf.basedb.core.Extract; |
6205 |
12 Apr 21 |
nicklas |
8 |
import net.sf.basedb.core.ItemQuery; |
6205 |
12 Apr 21 |
nicklas |
9 |
import net.sf.basedb.core.Tag; |
6205 |
12 Apr 21 |
nicklas |
10 |
import net.sf.basedb.core.query.Annotations; |
6205 |
12 Apr 21 |
nicklas |
11 |
import net.sf.basedb.core.query.Expressions; |
6205 |
12 Apr 21 |
nicklas |
12 |
import net.sf.basedb.core.query.Hql; |
6205 |
12 Apr 21 |
nicklas |
13 |
import net.sf.basedb.core.query.Restrictions; |
6205 |
12 Apr 21 |
nicklas |
14 |
import net.sf.basedb.reggie.Reggie; |
6205 |
12 Apr 21 |
nicklas |
15 |
import net.sf.basedb.reggie.dao.Annotationtype; |
6205 |
12 Apr 21 |
nicklas |
16 |
import net.sf.basedb.reggie.dao.Subtype; |
6887 |
24 Nov 22 |
nicklas |
17 |
import net.sf.basedb.reggie.plugins.cmd.LibraryInfo.BarcodeInfo; |
6205 |
12 Apr 21 |
nicklas |
18 |
import net.sf.basedb.util.Values; |
6205 |
12 Apr 21 |
nicklas |
19 |
import net.sf.basedb.util.formatter.NameableFormatter; |
6205 |
12 Apr 21 |
nicklas |
20 |
|
6200 |
08 Apr 21 |
nicklas |
21 |
/** |
6200 |
08 Apr 21 |
nicklas |
Holds all information about a pool. Validation will |
6200 |
08 Apr 21 |
nicklas |
be done at construction and errors are reported to |
6200 |
08 Apr 21 |
nicklas |
the JsonSection. Check the 'valid' flag before using |
6200 |
08 Apr 21 |
nicklas |
the information. |
6200 |
08 Apr 21 |
nicklas |
26 |
|
6200 |
08 Apr 21 |
nicklas |
@since 4.32 |
6200 |
08 Apr 21 |
nicklas |
28 |
*/ |
6200 |
08 Apr 21 |
nicklas |
29 |
public class PoolInfo |
6200 |
08 Apr 21 |
nicklas |
30 |
{ |
6200 |
08 Apr 21 |
nicklas |
31 |
|
6200 |
08 Apr 21 |
nicklas |
32 |
public String poolId; |
6205 |
12 Apr 21 |
nicklas |
33 |
public Extract pool; |
6200 |
08 Apr 21 |
nicklas |
34 |
public Date poolDate; |
6200 |
08 Apr 21 |
nicklas |
35 |
public String operator; |
6200 |
08 Apr 21 |
nicklas |
36 |
|
6200 |
08 Apr 21 |
nicklas |
37 |
public boolean valid; |
6200 |
08 Apr 21 |
nicklas |
38 |
|
6893 |
25 Nov 22 |
nicklas |
39 |
public PoolInfo(JsonSection section, LibraryInfo libInfo, MainInfo main, ImportContext ctx) |
6200 |
08 Apr 21 |
nicklas |
40 |
{ |
6205 |
12 Apr 21 |
nicklas |
41 |
if (section != null) |
6200 |
08 Apr 21 |
nicklas |
42 |
{ |
6205 |
12 Apr 21 |
nicklas |
43 |
poolId = section.getRequiredEntry("Pool ID", PatternValidator.CMD_ID); |
6205 |
12 Apr 21 |
nicklas |
44 |
if (poolId != null) pool = findExistingPool(poolId, section); |
6887 |
24 Nov 22 |
nicklas |
45 |
BarcodeInfo barcodeInfo = libInfo != null ? libInfo.barcodeInfo : null; |
6887 |
24 Nov 22 |
nicklas |
46 |
if (poolId != null && barcodeInfo != null && ctx != null) |
6346 |
16 Aug 21 |
nicklas |
47 |
{ |
6887 |
24 Nov 22 |
nicklas |
48 |
JsonSection duplicate = ctx.add("POOL:"+poolId+":"+barcodeInfo.name, section); |
6346 |
16 Aug 21 |
nicklas |
49 |
if (duplicate != null) |
6346 |
16 Aug 21 |
nicklas |
50 |
{ |
6887 |
24 Nov 22 |
nicklas |
51 |
String msg = "Pool barcode ["+barcodeInfo.name+"; ExternalRef="+poolId+"] duplicated in file: "; |
6346 |
16 Aug 21 |
nicklas |
52 |
section.addErrorMessage(msg+duplicate.getFile().getName()); |
6346 |
16 Aug 21 |
nicklas |
53 |
duplicate.addErrorMessage(msg+section.getFile().getName()); |
6346 |
16 Aug 21 |
nicklas |
54 |
} |
6346 |
16 Aug 21 |
nicklas |
55 |
|
6346 |
16 Aug 21 |
nicklas |
56 |
} |
6887 |
24 Nov 22 |
nicklas |
57 |
if (pool != null && barcodeInfo != null && barcodeInfo.tag != null) |
6205 |
12 Apr 21 |
nicklas |
58 |
{ |
6887 |
24 Nov 22 |
nicklas |
59 |
verifyUnusedBarcode(pool, barcodeInfo.tag, section); |
6205 |
12 Apr 21 |
nicklas |
60 |
} |
6893 |
25 Nov 22 |
nicklas |
61 |
poolDate = section.getRequiredEntry("Date", DateValidator.YYYY_MM_DD.warnIfFutureOrOlder(libInfo!=null?libInfo.libDate:null, main.refDate)); |
6212 |
14 Apr 21 |
nicklas |
62 |
operator = section.getOptionalEntry("Operator", null); |
6200 |
08 Apr 21 |
nicklas |
63 |
} |
6205 |
12 Apr 21 |
nicklas |
64 |
valid = section != null && !section.hasError(); |
6200 |
08 Apr 21 |
nicklas |
65 |
} |
6200 |
08 Apr 21 |
nicklas |
66 |
|
6205 |
12 Apr 21 |
nicklas |
67 |
/** |
6205 |
12 Apr 21 |
nicklas |
Find an existing pool with the given id. |
6205 |
12 Apr 21 |
nicklas |
69 |
*/ |
6205 |
12 Apr 21 |
nicklas |
70 |
private Extract findExistingPool(String poolId, JsonSection section) |
6205 |
12 Apr 21 |
nicklas |
71 |
{ |
6205 |
12 Apr 21 |
nicklas |
72 |
Extract pool = null; |
6205 |
12 Apr 21 |
nicklas |
73 |
DbControl dc = section.getFile().dc(); |
6205 |
12 Apr 21 |
nicklas |
74 |
ItemQuery<Extract> query = Extract.getQuery(); |
6205 |
12 Apr 21 |
nicklas |
75 |
query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT); |
6205 |
12 Apr 21 |
nicklas |
76 |
Subtype.POOLED_LIBRARY.addFilter(dc, query); |
6205 |
12 Apr 21 |
nicklas |
77 |
query.join(Annotations.innerJoin(null, Annotationtype.EXTERNAL_REF.load(dc), "eref")); |
6205 |
12 Apr 21 |
nicklas |
78 |
query.restrict(Restrictions.eq(Hql.alias("eref"), Expressions.string(poolId))); |
6205 |
12 Apr 21 |
nicklas |
79 |
List<Extract> list = query.list(dc); |
6205 |
12 Apr 21 |
nicklas |
80 |
if (list.size() > 1) |
6205 |
12 Apr 21 |
nicklas |
81 |
{ |
6205 |
12 Apr 21 |
nicklas |
82 |
section.addErrorMessage("Found "+list.size()+" pools with ExternalRef="+poolId); |
6205 |
12 Apr 21 |
nicklas |
83 |
} |
6205 |
12 Apr 21 |
nicklas |
84 |
else if (list.size() == 1) |
6205 |
12 Apr 21 |
nicklas |
85 |
{ |
6205 |
12 Apr 21 |
nicklas |
86 |
pool = list.get(0); |
6205 |
12 Apr 21 |
nicklas |
87 |
} |
6205 |
12 Apr 21 |
nicklas |
88 |
return pool; |
6205 |
12 Apr 21 |
nicklas |
89 |
} |
6205 |
12 Apr 21 |
nicklas |
90 |
|
6205 |
12 Apr 21 |
nicklas |
91 |
/** |
6205 |
12 Apr 21 |
nicklas |
Check if there are any other libs in the pool with the same barcode |
6205 |
12 Apr 21 |
nicklas |
93 |
*/ |
6205 |
12 Apr 21 |
nicklas |
94 |
private void verifyUnusedBarcode(Extract pool, Tag barcode, JsonSection section) |
6205 |
12 Apr 21 |
nicklas |
95 |
{ |
6205 |
12 Apr 21 |
nicklas |
96 |
DbControl dc = section.getFile().dc(); |
6205 |
12 Apr 21 |
nicklas |
97 |
ItemQuery<Extract> query = Extract.getQuery(); |
6584 |
11 Feb 22 |
nicklas |
98 |
query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT); |
6205 |
12 Apr 21 |
nicklas |
99 |
query.join(Hql.innerJoin("childCreationEvents", "cce")); |
6205 |
12 Apr 21 |
nicklas |
100 |
query.join(Hql.innerJoin("cce", "event", "pool")); |
6205 |
12 Apr 21 |
nicklas |
101 |
query.restrict(Restrictions.eq(Hql.alias("pool"), Hql.entity(pool.getCreationEvent()))); |
6205 |
12 Apr 21 |
nicklas |
102 |
query.restrict(Restrictions.eq(Hql.property("tag"), Hql.entity(barcode))); |
6205 |
12 Apr 21 |
nicklas |
103 |
List<Extract> libsInPool = query.list(dc); |
6205 |
12 Apr 21 |
nicklas |
104 |
if (libsInPool.size() > 0) |
6205 |
12 Apr 21 |
nicklas |
105 |
{ |
6205 |
12 Apr 21 |
nicklas |
106 |
section.addErrorMessage("Found other libraries in "+pool.getName() + |
6205 |
12 Apr 21 |
nicklas |
107 |
" with barcode "+barcode.getName()+": " + Values.getString(libsInPool, ", ", true, new NameableFormatter())); |
6205 |
12 Apr 21 |
nicklas |
108 |
} |
6205 |
12 Apr 21 |
nicklas |
109 |
|
6205 |
12 Apr 21 |
nicklas |
110 |
} |
6200 |
08 Apr 21 |
nicklas |
111 |
} |