src/core/net/sf/basedb/util/listable/PushToParentsTransformer.java

Code
Comments
Other
Rev Date Author Line
7772 17 Feb 20 nicklas 1 /**
7772 17 Feb 20 nicklas 2   $Id$
7772 17 Feb 20 nicklas 3
7772 17 Feb 20 nicklas 4   Copyright (C) 2020 Nicklas Nordborg
7772 17 Feb 20 nicklas 5
7772 17 Feb 20 nicklas 6   This file is part of BASE - BioArray Software Environment.
7772 17 Feb 20 nicklas 7   Available at http://base.thep.lu.se/
7772 17 Feb 20 nicklas 8
7772 17 Feb 20 nicklas 9   BASE is free software; you can redistribute it and/or
7772 17 Feb 20 nicklas 10   modify it under the terms of the GNU General Public License
7772 17 Feb 20 nicklas 11   as published by the Free Software Foundation; either version 3
7772 17 Feb 20 nicklas 12   of the License, or (at your option) any later version.
7772 17 Feb 20 nicklas 13
7772 17 Feb 20 nicklas 14   BASE is distributed in the hope that it will be useful,
7772 17 Feb 20 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
7772 17 Feb 20 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7772 17 Feb 20 nicklas 17   GNU General Public License for more details.
7772 17 Feb 20 nicklas 18
7772 17 Feb 20 nicklas 19   You should have received a copy of the GNU General Public License
7772 17 Feb 20 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
7772 17 Feb 20 nicklas 21 */
7772 17 Feb 20 nicklas 22 package net.sf.basedb.util.listable;
7772 17 Feb 20 nicklas 23
7772 17 Feb 20 nicklas 24 import java.util.Collections;
7772 17 Feb 20 nicklas 25 import java.util.HashSet;
7772 17 Feb 20 nicklas 26 import java.util.Set;
7772 17 Feb 20 nicklas 27
7772 17 Feb 20 nicklas 28 import net.sf.basedb.core.Item;
7772 17 Feb 20 nicklas 29
7772 17 Feb 20 nicklas 30 /**
7772 17 Feb 20 nicklas 31   Transformer implementation that can be used to to find parent items
7772 17 Feb 20 nicklas 32   to children that have a subtype with the "push annotations to parent"
7772 17 Feb 20 nicklas 33   flag set.
7772 17 Feb 20 nicklas 34   
7772 17 Feb 20 nicklas 35   Example: The source list is extracts of a subtype with this flag.
7772 17 Feb 20 nicklas 36   This transformer will find all parent items to source list. If
7772 17 Feb 20 nicklas 37   any of the parent items also has a subtype with the "push annotations" flag
7772 17 Feb 20 nicklas 38   to process is repeated.
7772 17 Feb 20 nicklas 39   
7772 17 Feb 20 nicklas 40   This implemntatation works with SAMPLE or EXTRACT types only. If starting with a
7772 17 Feb 20 nicklas 41   SAMPLE list it will also move up to BIOSOURCE and then back down one step.
7772 17 Feb 20 nicklas 42   If starting with a EXTRACT list it will move up to SAMPLE and BIOSOURCE (if
7772 17 Feb 20 nicklas 43   all intermediate steps have the "push annotations" flag).
7772 17 Feb 20 nicklas 44   
7772 17 Feb 20 nicklas 45   If used in a chain, this transformation should be used first in the chain.
7772 17 Feb 20 nicklas 46   
7772 17 Feb 20 nicklas 47   @since 3.16
7772 17 Feb 20 nicklas 48 */
7772 17 Feb 20 nicklas 49 public class PushToParentsTransformer 
7772 17 Feb 20 nicklas 50   implements SourceItemTransformer
7772 17 Feb 20 nicklas 51 {
7772 17 Feb 20 nicklas 52
7772 17 Feb 20 nicklas 53   private final Item itemType;
7772 17 Feb 20 nicklas 54   
7772 17 Feb 20 nicklas 55   public PushToParentsTransformer(Item itemType)
7772 17 Feb 20 nicklas 56   {
7772 17 Feb 20 nicklas 57     this.itemType = itemType;
7772 17 Feb 20 nicklas 58   }
7772 17 Feb 20 nicklas 59   
7772 17 Feb 20 nicklas 60   @Override
7772 17 Feb 20 nicklas 61   public Item getSourceItemType() 
7772 17 Feb 20 nicklas 62   {
7772 17 Feb 20 nicklas 63     return itemType;
7772 17 Feb 20 nicklas 64   }
7772 17 Feb 20 nicklas 65
7772 17 Feb 20 nicklas 66   @Override
7772 17 Feb 20 nicklas 67   public Item getTargetItemType() 
7772 17 Feb 20 nicklas 68   {
7772 17 Feb 20 nicklas 69     return itemType;
7772 17 Feb 20 nicklas 70   }
7772 17 Feb 20 nicklas 71
7772 17 Feb 20 nicklas 72   @Override
7772 17 Feb 20 nicklas 73   public Set<Integer> transform(TransformContext context, Set<Integer> source) 
7772 17 Feb 20 nicklas 74   {
7772 17 Feb 20 nicklas 75     if (source.size() == 0) return Collections.emptySet();
7772 17 Feb 20 nicklas 76     
7772 17 Feb 20 nicklas 77     Set<Integer> result = new HashSet<>();
7772 17 Feb 20 nicklas 78     if (itemType == Item.EXTRACT)
7772 17 Feb 20 nicklas 79     {
7772 17 Feb 20 nicklas 80       // Get parent extracts to sources with "push annotations"
7772 17 Feb 20 nicklas 81       Set<Integer> allExtracts = new ExtractToParentExtractTransformer(true, true).transform(context, source);
7772 17 Feb 20 nicklas 82       result.addAll(allExtracts);
7772 17 Feb 20 nicklas 83       
7772 17 Feb 20 nicklas 84       // Get parent samples to extracts with "push annotations"
7772 17 Feb 20 nicklas 85       Set<Integer> samples = new ExtractToSampleTransformer(true).transform(context, allExtracts);
7772 17 Feb 20 nicklas 86       if (samples.size() > 0)
7772 17 Feb 20 nicklas 87       {
7772 17 Feb 20 nicklas 88         samples = getAllSamples(context, samples);
7772 17 Feb 20 nicklas 89         // Move back down to extracts again since we need to finish with a set of extracts
7772 17 Feb 20 nicklas 90         samples = new SampleToChildSampleTransformer(true).transform(context, samples);
7772 17 Feb 20 nicklas 91         result.addAll(new SampleToExtractTransformer().transform(context, samples));
7772 17 Feb 20 nicklas 92       }        
7772 17 Feb 20 nicklas 93     }
7772 17 Feb 20 nicklas 94     else if (itemType == Item.SAMPLE)
7772 17 Feb 20 nicklas 95     {
7772 17 Feb 20 nicklas 96       // Get parent samples to sources with "push annotations"
7772 17 Feb 20 nicklas 97       result.addAll(getAllSamples(context, source));
7772 17 Feb 20 nicklas 98     }
7772 17 Feb 20 nicklas 99     
7772 17 Feb 20 nicklas 100     return result;
7772 17 Feb 20 nicklas 101   }
7772 17 Feb 20 nicklas 102
7772 17 Feb 20 nicklas 103   
7772 17 Feb 20 nicklas 104   private Set<Integer> getAllSamples(TransformContext context, Set<Integer> source)
7772 17 Feb 20 nicklas 105   {
7772 17 Feb 20 nicklas 106     Set<Integer> allSamples = new SampleToParentSampleTransformer(true, true).transform(context, source);
7772 17 Feb 20 nicklas 107     Set<Integer> bioSources = new SampleToBioSourceTransformer(true).transform(context, allSamples);
7772 17 Feb 20 nicklas 108     if (bioSources.size() > 0)
7772 17 Feb 20 nicklas 109     {
7772 17 Feb 20 nicklas 110       allSamples.addAll(new BioSourceToSampleTransformer().transform(context, bioSources));
7772 17 Feb 20 nicklas 111     }
7772 17 Feb 20 nicklas 112     return allSamples;
7772 17 Feb 20 nicklas 113   }
7772 17 Feb 20 nicklas 114   
7772 17 Feb 20 nicklas 115 }