6791 |
23 Mar 15 |
nicklas |
1 |
/** |
6791 |
23 Mar 15 |
nicklas |
$Id$ |
6791 |
23 Mar 15 |
nicklas |
3 |
|
6791 |
23 Mar 15 |
nicklas |
Copyright (C) 2015 Nicklas Nordborg |
6791 |
23 Mar 15 |
nicklas |
5 |
|
6791 |
23 Mar 15 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
6791 |
23 Mar 15 |
nicklas |
Available at http://base.thep.lu.se/ |
6791 |
23 Mar 15 |
nicklas |
8 |
|
6791 |
23 Mar 15 |
nicklas |
BASE is free software; you can redistribute it and/or |
6791 |
23 Mar 15 |
nicklas |
modify it under the terms of the GNU General Public License |
6791 |
23 Mar 15 |
nicklas |
as published by the Free Software Foundation; either version 3 |
6791 |
23 Mar 15 |
nicklas |
of the License, or (at your option) any later version. |
6791 |
23 Mar 15 |
nicklas |
13 |
|
6791 |
23 Mar 15 |
nicklas |
BASE is distributed in the hope that it will be useful, |
6791 |
23 Mar 15 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
6791 |
23 Mar 15 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
6791 |
23 Mar 15 |
nicklas |
GNU General Public License for more details. |
6791 |
23 Mar 15 |
nicklas |
18 |
|
6791 |
23 Mar 15 |
nicklas |
You should have received a copy of the GNU General Public License |
6791 |
23 Mar 15 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
6791 |
23 Mar 15 |
nicklas |
21 |
*/ |
6791 |
23 Mar 15 |
nicklas |
22 |
package net.sf.basedb.util.listable; |
6791 |
23 Mar 15 |
nicklas |
23 |
|
6791 |
23 Mar 15 |
nicklas |
24 |
import java.util.HashSet; |
6791 |
23 Mar 15 |
nicklas |
25 |
import java.util.Set; |
6791 |
23 Mar 15 |
nicklas |
26 |
|
6791 |
23 Mar 15 |
nicklas |
27 |
import net.sf.basedb.core.DbControl; |
6791 |
23 Mar 15 |
nicklas |
28 |
import net.sf.basedb.core.DerivedBioAssay; |
6791 |
23 Mar 15 |
nicklas |
29 |
import net.sf.basedb.core.Item; |
6791 |
23 Mar 15 |
nicklas |
30 |
import net.sf.basedb.core.ItemQuery; |
6791 |
23 Mar 15 |
nicklas |
31 |
import net.sf.basedb.core.query.Expressions; |
6791 |
23 Mar 15 |
nicklas |
32 |
import net.sf.basedb.core.query.Hql; |
6791 |
23 Mar 15 |
nicklas |
33 |
import net.sf.basedb.core.query.Restrictions; |
6791 |
23 Mar 15 |
nicklas |
34 |
|
6791 |
23 Mar 15 |
nicklas |
35 |
/** |
6791 |
23 Mar 15 |
nicklas |
Source item transformer implementation that transform derived bioassay items |
6791 |
23 Mar 15 |
nicklas |
to parent derived bioassays. |
6791 |
23 Mar 15 |
nicklas |
38 |
|
6791 |
23 Mar 15 |
nicklas |
@author Nicklas |
6791 |
23 Mar 15 |
nicklas |
@since 3.5 |
6791 |
23 Mar 15 |
nicklas |
41 |
*/ |
6791 |
23 Mar 15 |
nicklas |
42 |
public class DerivedBioAssayToParentDerivedBioAssayTransformer |
6791 |
23 Mar 15 |
nicklas |
43 |
extends AbstractSourceItemTransformer |
6791 |
23 Mar 15 |
nicklas |
44 |
{ |
6791 |
23 Mar 15 |
nicklas |
45 |
|
6791 |
23 Mar 15 |
nicklas |
46 |
private final boolean includeSourcesInTarget; |
6791 |
23 Mar 15 |
nicklas |
47 |
|
6791 |
23 Mar 15 |
nicklas |
48 |
/** |
6791 |
23 Mar 15 |
nicklas |
Create a new derived bioassay to parent derived bioassay transformer. |
6791 |
23 Mar 15 |
nicklas |
50 |
*/ |
6791 |
23 Mar 15 |
nicklas |
51 |
public DerivedBioAssayToParentDerivedBioAssayTransformer(boolean includeSourcesInTarget) |
6791 |
23 Mar 15 |
nicklas |
52 |
{ |
6792 |
23 Mar 15 |
nicklas |
53 |
super(Item.DERIVEDBIOASSAY, Item.DERIVEDBIOASSAY); |
6791 |
23 Mar 15 |
nicklas |
54 |
this.includeSourcesInTarget = includeSourcesInTarget; |
6791 |
23 Mar 15 |
nicklas |
55 |
} |
6791 |
23 Mar 15 |
nicklas |
56 |
|
6791 |
23 Mar 15 |
nicklas |
57 |
@Override |
6791 |
23 Mar 15 |
nicklas |
58 |
public Set<Integer> transform(TransformContext context, Set<Integer> source) |
6791 |
23 Mar 15 |
nicklas |
59 |
{ |
6791 |
23 Mar 15 |
nicklas |
60 |
DbControl dc = context.getDbControl(); |
6791 |
23 Mar 15 |
nicklas |
61 |
|
6791 |
23 Mar 15 |
nicklas |
62 |
ItemQuery<DerivedBioAssay> query = DerivedBioAssay.getQuery(); |
6848 |
13 Apr 15 |
nicklas |
63 |
query.setIncludes(context.getInclude()); |
6791 |
23 Mar 15 |
nicklas |
64 |
query.joinPermanent(Hql.innerJoin("children", "c")); |
6796 |
24 Mar 15 |
nicklas |
65 |
query.restrict( |
6791 |
23 Mar 15 |
nicklas |
66 |
Restrictions.in( |
6791 |
23 Mar 15 |
nicklas |
67 |
Hql.alias("c"), |
6791 |
23 Mar 15 |
nicklas |
68 |
Expressions.parameter("children") |
6791 |
23 Mar 15 |
nicklas |
69 |
) |
6791 |
23 Mar 15 |
nicklas |
70 |
); |
6791 |
23 Mar 15 |
nicklas |
71 |
|
6791 |
23 Mar 15 |
nicklas |
// Keep track of all seen children and parents |
6791 |
23 Mar 15 |
nicklas |
73 |
Set<Integer> allParents = new HashSet<Integer>(); |
6791 |
23 Mar 15 |
nicklas |
74 |
Set<Integer> allChildren = new HashSet<Integer>(); |
6791 |
23 Mar 15 |
nicklas |
75 |
if (includeSourcesInTarget) allParents.addAll(source); |
6791 |
23 Mar 15 |
nicklas |
76 |
|
6791 |
23 Mar 15 |
nicklas |
// Children that we have not yet checked |
6791 |
23 Mar 15 |
nicklas |
78 |
Set<Integer> children = source; |
6791 |
23 Mar 15 |
nicklas |
79 |
while (children.size() > 0) |
6791 |
23 Mar 15 |
nicklas |
80 |
{ |
8094 |
04 Nov 22 |
nicklas |
81 |
context.collect(getSourceItemType(), children); |
8094 |
04 Nov 22 |
nicklas |
82 |
Set<Integer> parents = context.avoid(getTargetItemType(), safeIdList(dc, query, "children", children)); |
6791 |
23 Mar 15 |
nicklas |
83 |
|
6791 |
23 Mar 15 |
nicklas |
// Store new parents and "used" children |
6791 |
23 Mar 15 |
nicklas |
85 |
allParents.addAll(parents); |
6791 |
23 Mar 15 |
nicklas |
86 |
allChildren.addAll(children); |
6791 |
23 Mar 15 |
nicklas |
87 |
|
6791 |
23 Mar 15 |
nicklas |
// Remove children that we have already seen... |
6791 |
23 Mar 15 |
nicklas |
// ...the remaining parents become our new children |
6791 |
23 Mar 15 |
nicklas |
90 |
parents.removeAll(allChildren); |
6791 |
23 Mar 15 |
nicklas |
91 |
children = parents; |
6791 |
23 Mar 15 |
nicklas |
92 |
} |
6791 |
23 Mar 15 |
nicklas |
93 |
|
6791 |
23 Mar 15 |
nicklas |
94 |
return allParents; |
6791 |
23 Mar 15 |
nicklas |
95 |
} |
6791 |
23 Mar 15 |
nicklas |
96 |
|
6791 |
23 Mar 15 |
nicklas |
97 |
} |