6775 |
17 Mar 15 |
nicklas |
1 |
/** |
6775 |
17 Mar 15 |
nicklas |
$Id$ |
6775 |
17 Mar 15 |
nicklas |
3 |
|
6775 |
17 Mar 15 |
nicklas |
Copyright (C) 2015 Nicklas Nordborg |
6775 |
17 Mar 15 |
nicklas |
5 |
|
6775 |
17 Mar 15 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
6775 |
17 Mar 15 |
nicklas |
Available at http://base.thep.lu.se/ |
6775 |
17 Mar 15 |
nicklas |
8 |
|
6775 |
17 Mar 15 |
nicklas |
BASE is free software; you can redistribute it and/or |
6775 |
17 Mar 15 |
nicklas |
modify it under the terms of the GNU General Public License |
6775 |
17 Mar 15 |
nicklas |
as published by the Free Software Foundation; either version 3 |
6775 |
17 Mar 15 |
nicklas |
of the License, or (at your option) any later version. |
6775 |
17 Mar 15 |
nicklas |
13 |
|
6775 |
17 Mar 15 |
nicklas |
BASE is distributed in the hope that it will be useful, |
6775 |
17 Mar 15 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
6775 |
17 Mar 15 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
6775 |
17 Mar 15 |
nicklas |
GNU General Public License for more details. |
6775 |
17 Mar 15 |
nicklas |
18 |
|
6775 |
17 Mar 15 |
nicklas |
You should have received a copy of the GNU General Public License |
6775 |
17 Mar 15 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
6775 |
17 Mar 15 |
nicklas |
21 |
*/ |
6775 |
17 Mar 15 |
nicklas |
22 |
package net.sf.basedb.util.listable; |
6775 |
17 Mar 15 |
nicklas |
23 |
|
6775 |
17 Mar 15 |
nicklas |
24 |
import java.util.HashSet; |
6775 |
17 Mar 15 |
nicklas |
25 |
import java.util.Set; |
6775 |
17 Mar 15 |
nicklas |
26 |
|
6775 |
17 Mar 15 |
nicklas |
27 |
import net.sf.basedb.core.DbControl; |
6775 |
17 Mar 15 |
nicklas |
28 |
import net.sf.basedb.core.Item; |
6775 |
17 Mar 15 |
nicklas |
29 |
import net.sf.basedb.core.ItemQuery; |
6775 |
17 Mar 15 |
nicklas |
30 |
import net.sf.basedb.core.Sample; |
6775 |
17 Mar 15 |
nicklas |
31 |
import net.sf.basedb.core.query.Expressions; |
6775 |
17 Mar 15 |
nicklas |
32 |
import net.sf.basedb.core.query.Hql; |
6775 |
17 Mar 15 |
nicklas |
33 |
import net.sf.basedb.core.query.Restrictions; |
6775 |
17 Mar 15 |
nicklas |
34 |
|
6775 |
17 Mar 15 |
nicklas |
35 |
/** |
6775 |
17 Mar 15 |
nicklas |
Source item transformer implementation that transform sample items to their |
6775 |
17 Mar 15 |
nicklas |
parent sample items. From the given set of sample ID:s the transformer will |
6775 |
17 Mar 15 |
nicklas |
recursively load as many parent samples as possible. The source items |
6775 |
17 Mar 15 |
nicklas |
may optionally be included in the target set. |
6775 |
17 Mar 15 |
nicklas |
40 |
|
6775 |
17 Mar 15 |
nicklas |
@author Nicklas |
6775 |
17 Mar 15 |
nicklas |
@since 3.5 |
6775 |
17 Mar 15 |
nicklas |
43 |
*/ |
6775 |
17 Mar 15 |
nicklas |
44 |
public class SampleToParentSampleTransformer |
6775 |
17 Mar 15 |
nicklas |
45 |
extends AbstractSourceItemTransformer |
6775 |
17 Mar 15 |
nicklas |
46 |
{ |
6775 |
17 Mar 15 |
nicklas |
47 |
|
6775 |
17 Mar 15 |
nicklas |
48 |
private final boolean includeSourcesInTarget; |
7772 |
17 Feb 20 |
nicklas |
49 |
private boolean pushOnly; |
6775 |
17 Mar 15 |
nicklas |
50 |
|
6775 |
17 Mar 15 |
nicklas |
51 |
/** |
6775 |
17 Mar 15 |
nicklas |
Create a new sample to parent sample transformer. |
6775 |
17 Mar 15 |
nicklas |
@param includeSourcesInTarget TRUE to include the source items in the target |
6775 |
17 Mar 15 |
nicklas |
54 |
*/ |
6775 |
17 Mar 15 |
nicklas |
55 |
public SampleToParentSampleTransformer(boolean includeSourcesInTarget) |
6775 |
17 Mar 15 |
nicklas |
56 |
{ |
6775 |
17 Mar 15 |
nicklas |
57 |
super(Item.SAMPLE, Item.SAMPLE); |
6775 |
17 Mar 15 |
nicklas |
58 |
this.includeSourcesInTarget = includeSourcesInTarget; |
6775 |
17 Mar 15 |
nicklas |
59 |
} |
7772 |
17 Feb 20 |
nicklas |
60 |
|
7772 |
17 Feb 20 |
nicklas |
61 |
/** |
7772 |
17 Feb 20 |
nicklas |
Create a new sample to parent sample transformer that only load parents |
7772 |
17 Feb 20 |
nicklas |
to samples that has a subtype with "push annotations" set. This functionality |
7772 |
17 Feb 20 |
nicklas |
should only be used in the first transformation step. |
7772 |
17 Feb 20 |
nicklas |
@see PushToParentsTransformer |
7772 |
17 Feb 20 |
nicklas |
@since 3.16 |
7772 |
17 Feb 20 |
nicklas |
67 |
*/ |
7772 |
17 Feb 20 |
nicklas |
68 |
public SampleToParentSampleTransformer(boolean includeSourcesInTarget, boolean childrensThatPushOnly) |
7772 |
17 Feb 20 |
nicklas |
69 |
{ |
7772 |
17 Feb 20 |
nicklas |
70 |
this(includeSourcesInTarget); |
7772 |
17 Feb 20 |
nicklas |
71 |
this.pushOnly = childrensThatPushOnly; |
7772 |
17 Feb 20 |
nicklas |
72 |
} |
7772 |
17 Feb 20 |
nicklas |
73 |
|
6775 |
17 Mar 15 |
nicklas |
74 |
@Override |
6775 |
17 Mar 15 |
nicklas |
75 |
public Set<Integer> transform(TransformContext context, Set<Integer> source) |
6775 |
17 Mar 15 |
nicklas |
76 |
{ |
6775 |
17 Mar 15 |
nicklas |
77 |
DbControl dc = context.getDbControl(); |
6775 |
17 Mar 15 |
nicklas |
78 |
|
6775 |
17 Mar 15 |
nicklas |
79 |
ItemQuery<Sample> query = Sample.getQuery(); |
6848 |
13 Apr 15 |
nicklas |
80 |
query.setIncludes(context.getInclude()); |
6775 |
17 Mar 15 |
nicklas |
81 |
query.join(Hql.innerJoin("childCreationEvents", "ce")); |
6775 |
17 Mar 15 |
nicklas |
82 |
query.join(Hql.innerJoin("ce", "event", "evt")); |
6775 |
17 Mar 15 |
nicklas |
83 |
query.join(Hql.innerJoin("evt", "bioMaterial", "bm")); |
6775 |
17 Mar 15 |
nicklas |
84 |
query.restrict( |
6775 |
17 Mar 15 |
nicklas |
85 |
Restrictions.in( |
6775 |
17 Mar 15 |
nicklas |
86 |
Hql.alias("bm"), |
6775 |
17 Mar 15 |
nicklas |
87 |
Expressions.parameter("children") |
6775 |
17 Mar 15 |
nicklas |
88 |
) |
6775 |
17 Mar 15 |
nicklas |
89 |
); |
7772 |
17 Feb 20 |
nicklas |
90 |
if (pushOnly) |
7772 |
17 Feb 20 |
nicklas |
91 |
{ |
7772 |
17 Feb 20 |
nicklas |
92 |
query.join(Hql.innerJoin("bm", "itemSubtype", "st")); |
7772 |
17 Feb 20 |
nicklas |
93 |
query.restrict(Restrictions.eq(Hql.property("st", "pushAnnotations"), Expressions.bool(true))); |
7772 |
17 Feb 20 |
nicklas |
94 |
} |
6775 |
17 Mar 15 |
nicklas |
95 |
|
6775 |
17 Mar 15 |
nicklas |
// Keep track of all seen children and parents |
6775 |
17 Mar 15 |
nicklas |
97 |
Set<Integer> allParents = new HashSet<Integer>(); |
6775 |
17 Mar 15 |
nicklas |
98 |
Set<Integer> allChildren = new HashSet<Integer>(); |
6775 |
17 Mar 15 |
nicklas |
99 |
if (includeSourcesInTarget) allParents.addAll(source); |
6775 |
17 Mar 15 |
nicklas |
100 |
|
6775 |
17 Mar 15 |
nicklas |
// Children that we have not yet checked |
6775 |
17 Mar 15 |
nicklas |
102 |
Set<Integer> children = source; |
6775 |
17 Mar 15 |
nicklas |
103 |
while (children.size() > 0) |
6775 |
17 Mar 15 |
nicklas |
104 |
{ |
8094 |
04 Nov 22 |
nicklas |
105 |
context.collect(getSourceItemType(), children); |
8094 |
04 Nov 22 |
nicklas |
106 |
Set<Integer> parents = context.avoid(getTargetItemType(), safeIdList(dc, query, "children", children)); |
6775 |
17 Mar 15 |
nicklas |
107 |
|
6775 |
17 Mar 15 |
nicklas |
// Store new parents and "used" children |
6775 |
17 Mar 15 |
nicklas |
109 |
allParents.addAll(parents); |
6775 |
17 Mar 15 |
nicklas |
110 |
allChildren.addAll(children); |
6775 |
17 Mar 15 |
nicklas |
111 |
|
6775 |
17 Mar 15 |
nicklas |
// Remove children that we have already seen... |
6775 |
17 Mar 15 |
nicklas |
// ...the remaining parents become our new children |
6775 |
17 Mar 15 |
nicklas |
114 |
parents.removeAll(allChildren); |
6775 |
17 Mar 15 |
nicklas |
115 |
children = parents; |
6775 |
17 Mar 15 |
nicklas |
116 |
} |
6775 |
17 Mar 15 |
nicklas |
117 |
|
6775 |
17 Mar 15 |
nicklas |
118 |
return allParents; |
6775 |
17 Mar 15 |
nicklas |
119 |
} |
6775 |
17 Mar 15 |
nicklas |
120 |
|
6775 |
17 Mar 15 |
nicklas |
121 |
|
6775 |
17 Mar 15 |
nicklas |
122 |
} |