6021 |
23 Oct 20 |
nicklas |
1 |
package net.sf.basedb.reggie.query; |
6021 |
23 Oct 20 |
nicklas |
2 |
|
6021 |
23 Oct 20 |
nicklas |
3 |
import java.util.Collection; |
6021 |
23 Oct 20 |
nicklas |
4 |
import java.util.Collections; |
6021 |
23 Oct 20 |
nicklas |
5 |
|
6021 |
23 Oct 20 |
nicklas |
6 |
import net.sf.basedb.core.DbControl; |
6021 |
23 Oct 20 |
nicklas |
7 |
import net.sf.basedb.core.Type; |
6021 |
23 Oct 20 |
nicklas |
8 |
import net.sf.basedb.core.query.Query; |
6021 |
23 Oct 20 |
nicklas |
9 |
import net.sf.basedb.core.query.QueryElement; |
6021 |
23 Oct 20 |
nicklas |
10 |
import net.sf.basedb.core.query.Restriction; |
6021 |
23 Oct 20 |
nicklas |
11 |
|
6021 |
23 Oct 20 |
nicklas |
12 |
/** |
6021 |
23 Oct 20 |
nicklas |
Implements a restriction for job parameters. The parameter must first be joined |
6021 |
23 Oct 20 |
nicklas |
to the query with a {@link JobParameterJoin}. The restriction is created with |
6021 |
23 Oct 20 |
nicklas |
the help of a subquery that searches the correct table depending on the value |
6021 |
23 Oct 20 |
nicklas |
type of the parameter (eg. Type.STRING searches StringParameterValueData, etc.). |
6021 |
23 Oct 20 |
nicklas |
17 |
|
6021 |
23 Oct 20 |
nicklas |
A note about aliases. The alias used to join the parameter need to be specified |
6021 |
23 Oct 20 |
nicklas |
when creating this restriction. Another alias that is referencing the parameter |
6021 |
23 Oct 20 |
nicklas |
value is automatically created by appending "Val" to the given alias. This new |
6021 |
23 Oct 20 |
nicklas |
alias need to be used in the restriction for the value. Example: |
6021 |
23 Oct 20 |
nicklas |
22 |
|
6021 |
23 Oct 20 |
nicklas |
ItemQuery<Job> jobQuery = Job.getQuery(); |
6021 |
23 Oct 20 |
nicklas |
jobQuery.join(new JobParameterJoin("models", "m")); |
6021 |
23 Oct 20 |
nicklas |
jobQuery.restrict(new JobParameterRestriction("m", Type.STRING, Restrictions.eq(Hql.alias("mVal"), Expressions.string("Ki67")))); |
6021 |
23 Oct 20 |
nicklas |
26 |
|
6021 |
23 Oct 20 |
nicklas |
@since 4.28 |
6021 |
23 Oct 20 |
nicklas |
@see JobParameterJoin |
6021 |
23 Oct 20 |
nicklas |
@see JobParameter |
6021 |
23 Oct 20 |
nicklas |
30 |
*/ |
6021 |
23 Oct 20 |
nicklas |
31 |
public class JobParameterRestriction |
6021 |
23 Oct 20 |
nicklas |
32 |
implements Restriction |
6021 |
23 Oct 20 |
nicklas |
33 |
{ |
6021 |
23 Oct 20 |
nicklas |
34 |
|
6021 |
23 Oct 20 |
nicklas |
35 |
private final String alias; |
6021 |
23 Oct 20 |
nicklas |
36 |
private final String idAlias; |
6021 |
23 Oct 20 |
nicklas |
37 |
private final String valueAlias; |
6021 |
23 Oct 20 |
nicklas |
38 |
private final Restriction valueRestriction; |
6021 |
23 Oct 20 |
nicklas |
39 |
private final Type valueType; |
6021 |
23 Oct 20 |
nicklas |
40 |
private final String entityClass; |
6021 |
23 Oct 20 |
nicklas |
41 |
|
6021 |
23 Oct 20 |
nicklas |
42 |
public JobParameterRestriction(String alias, Type valueType, Restriction valueRestriction) |
6021 |
23 Oct 20 |
nicklas |
43 |
{ |
6021 |
23 Oct 20 |
nicklas |
44 |
this.alias = alias; |
6021 |
23 Oct 20 |
nicklas |
45 |
this.idAlias = alias+"Id"; |
6021 |
23 Oct 20 |
nicklas |
46 |
this.valueAlias = alias+"Val"; |
6021 |
23 Oct 20 |
nicklas |
47 |
this.valueType = valueType; |
6021 |
23 Oct 20 |
nicklas |
48 |
this.valueRestriction = valueRestriction; |
6021 |
23 Oct 20 |
nicklas |
49 |
this.entityClass = getEntityClass(valueType); |
6021 |
23 Oct 20 |
nicklas |
50 |
} |
6021 |
23 Oct 20 |
nicklas |
51 |
|
6021 |
23 Oct 20 |
nicklas |
52 |
@Override |
6021 |
23 Oct 20 |
nicklas |
53 |
public String toQl(Query query, DbControl dc) |
6021 |
23 Oct 20 |
nicklas |
54 |
{ |
6021 |
23 Oct 20 |
nicklas |
55 |
return "value("+alias+").id in " + |
6021 |
23 Oct 20 |
nicklas |
56 |
"(select " + idAlias + ".id from " + entityClass + " " + idAlias + |
6021 |
23 Oct 20 |
nicklas |
57 |
" join " + idAlias + ".values " + valueAlias + |
6021 |
23 Oct 20 |
nicklas |
58 |
" where " + valueRestriction.toQl(query, dc) + ")"; |
6021 |
23 Oct 20 |
nicklas |
59 |
} |
6021 |
23 Oct 20 |
nicklas |
60 |
|
6021 |
23 Oct 20 |
nicklas |
61 |
@Override |
6021 |
23 Oct 20 |
nicklas |
62 |
public Collection<? extends QueryElement> getChildren() |
6021 |
23 Oct 20 |
nicklas |
63 |
{ |
6021 |
23 Oct 20 |
nicklas |
64 |
return Collections.singleton(valueRestriction); |
6021 |
23 Oct 20 |
nicklas |
65 |
} |
6021 |
23 Oct 20 |
nicklas |
66 |
|
6021 |
23 Oct 20 |
nicklas |
67 |
@Override |
6021 |
23 Oct 20 |
nicklas |
68 |
public int hashCode() |
6021 |
23 Oct 20 |
nicklas |
69 |
{ |
6021 |
23 Oct 20 |
nicklas |
70 |
return alias.hashCode() + valueRestriction.hashCode(); |
6021 |
23 Oct 20 |
nicklas |
71 |
} |
6021 |
23 Oct 20 |
nicklas |
72 |
|
6021 |
23 Oct 20 |
nicklas |
73 |
@Override |
6021 |
23 Oct 20 |
nicklas |
74 |
public boolean equals(Object other) |
6021 |
23 Oct 20 |
nicklas |
75 |
{ |
6021 |
23 Oct 20 |
nicklas |
76 |
if (this == other) return true; |
6021 |
23 Oct 20 |
nicklas |
77 |
if ((other == null) || (super.getClass() != other.getClass())) return false; |
6021 |
23 Oct 20 |
nicklas |
78 |
JobParameterRestriction o = (JobParameterRestriction)other; |
6021 |
23 Oct 20 |
nicklas |
79 |
return alias.equals(o.alias) && valueRestriction.equals(o.valueRestriction); |
6021 |
23 Oct 20 |
nicklas |
80 |
} |
6021 |
23 Oct 20 |
nicklas |
81 |
|
6021 |
23 Oct 20 |
nicklas |
82 |
@Override |
6021 |
23 Oct 20 |
nicklas |
83 |
public String toString() |
6021 |
23 Oct 20 |
nicklas |
84 |
{ |
6021 |
23 Oct 20 |
nicklas |
85 |
return "value("+alias+").id in " + |
6021 |
23 Oct 20 |
nicklas |
86 |
"(select " + idAlias + ".id from " + entityClass + " " + idAlias + |
6021 |
23 Oct 20 |
nicklas |
87 |
" join " + idAlias + ".values " + valueAlias + |
6021 |
23 Oct 20 |
nicklas |
88 |
" where " + valueRestriction.toString() + ")"; |
6021 |
23 Oct 20 |
nicklas |
89 |
} |
6021 |
23 Oct 20 |
nicklas |
90 |
|
6021 |
23 Oct 20 |
nicklas |
91 |
private static String getEntityClass(Type valueType) |
6021 |
23 Oct 20 |
nicklas |
92 |
{ |
6021 |
23 Oct 20 |
nicklas |
93 |
switch (valueType) |
6021 |
23 Oct 20 |
nicklas |
94 |
{ |
6021 |
23 Oct 20 |
nicklas |
95 |
case BOOLEAN: |
6021 |
23 Oct 20 |
nicklas |
96 |
return "BooleanParameterValueData"; |
6021 |
23 Oct 20 |
nicklas |
97 |
case DATE: |
6021 |
23 Oct 20 |
nicklas |
98 |
return "DateParameterValueData"; |
6021 |
23 Oct 20 |
nicklas |
99 |
case DOUBLE: |
6021 |
23 Oct 20 |
nicklas |
100 |
return "DoubleParameterValueData"; |
6021 |
23 Oct 20 |
nicklas |
101 |
case FLOAT: |
6021 |
23 Oct 20 |
nicklas |
102 |
return "FloatParameterValueData"; |
6021 |
23 Oct 20 |
nicklas |
103 |
case INT: |
6021 |
23 Oct 20 |
nicklas |
104 |
return "IntegerParameterValueData"; |
6021 |
23 Oct 20 |
nicklas |
105 |
case LONG: |
6021 |
23 Oct 20 |
nicklas |
106 |
return "LongParameterValueData"; |
6021 |
23 Oct 20 |
nicklas |
107 |
case STRING: |
6021 |
23 Oct 20 |
nicklas |
108 |
return "StringParameterValueData"; |
6021 |
23 Oct 20 |
nicklas |
109 |
case TEXT: |
6021 |
23 Oct 20 |
nicklas |
110 |
return "TextParameterValueData"; |
6021 |
23 Oct 20 |
nicklas |
111 |
case TIMESTAMP: |
6021 |
23 Oct 20 |
nicklas |
112 |
return "TimestampParameterValueData"; |
6021 |
23 Oct 20 |
nicklas |
113 |
default: |
6021 |
23 Oct 20 |
nicklas |
114 |
throw new UnsupportedOperationException("getEntityClass:"+valueType); |
6021 |
23 Oct 20 |
nicklas |
115 |
} |
6021 |
23 Oct 20 |
nicklas |
116 |
} |
6021 |
23 Oct 20 |
nicklas |
117 |
|
6021 |
23 Oct 20 |
nicklas |
118 |
} |