src/core/net/sf/basedb/util/importer/spotdata/PositionEventHandler.java

Code
Comments
Other
Rev Date Author Line
5227 01 Feb 10 nicklas 1 /**
5227 01 Feb 10 nicklas 2   $Id$
5227 01 Feb 10 nicklas 3
5227 01 Feb 10 nicklas 4   Copyright (C) 2010 Nicklas Nordborg
5227 01 Feb 10 nicklas 5
5227 01 Feb 10 nicklas 6   This file is part of BASE - BioArray Software Environment.
5227 01 Feb 10 nicklas 7   Available at http://base.thep.lu.se/
5227 01 Feb 10 nicklas 8
5227 01 Feb 10 nicklas 9   BASE is free software; you can redistribute it and/or
5227 01 Feb 10 nicklas 10   modify it under the terms of the GNU General Public License
5227 01 Feb 10 nicklas 11   as published by the Free Software Foundation; either version 3
5227 01 Feb 10 nicklas 12   of the License, or (at your option) any later version.
5227 01 Feb 10 nicklas 13
5227 01 Feb 10 nicklas 14   BASE is distributed in the hope that it will be useful,
5227 01 Feb 10 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5227 01 Feb 10 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5227 01 Feb 10 nicklas 17   GNU General Public License for more details.
5227 01 Feb 10 nicklas 18
5227 01 Feb 10 nicklas 19   You should have received a copy of the GNU General Public License
5227 01 Feb 10 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
5227 01 Feb 10 nicklas 21 */
5227 01 Feb 10 nicklas 22 package net.sf.basedb.util.importer.spotdata;
5227 01 Feb 10 nicklas 23
5227 01 Feb 10 nicklas 24 import net.sf.basedb.core.BaseException;
5227 01 Feb 10 nicklas 25 import net.sf.basedb.core.PositionBatcher;
5227 01 Feb 10 nicklas 26 import net.sf.basedb.core.Reporter;
5227 01 Feb 10 nicklas 27 import net.sf.basedb.core.ReporterBatcher;
5227 01 Feb 10 nicklas 28 import net.sf.basedb.core.data.ReporterData;
5227 01 Feb 10 nicklas 29 import net.sf.basedb.util.Values;
5227 01 Feb 10 nicklas 30 import net.sf.basedb.util.bfs.BfsParser;
5227 01 Feb 10 nicklas 31 import net.sf.basedb.util.bfs.EventHandler;
5227 01 Feb 10 nicklas 32 import net.sf.basedb.util.bfs.EventType;
5227 01 Feb 10 nicklas 33 import net.sf.basedb.util.bfs.MultiEventHandler;
5227 01 Feb 10 nicklas 34 import net.sf.basedb.util.importer.spotdata.SynchronizedSpotDataParser.SynchronizedData;
5227 01 Feb 10 nicklas 35
5227 01 Feb 10 nicklas 36 /**
5227 01 Feb 10 nicklas 37   Event handler implementation intended to be used with a 
5227 01 Feb 10 nicklas 38   {@link SynchronizedSpotDataParser} to extract the position
5227 01 Feb 10 nicklas 39   number from the parsed data. When cooperation is needed
5227 01 Feb 10 nicklas 40   between multiple event handlers (eg. using the {@link MultiEventHandler}
5227 01 Feb 10 nicklas 41   class, make sure that this event handler receives the event
5227 01 Feb 10 nicklas 42   before any other handler that needs the position value.
5227 01 Feb 10 nicklas 43   
5227 01 Feb 10 nicklas 44   @author Nicklas
5227 01 Feb 10 nicklas 45   @version 2.15
5227 01 Feb 10 nicklas 46   @base.modified $Date$
5227 01 Feb 10 nicklas 47 */
5227 01 Feb 10 nicklas 48 public class PositionEventHandler
5227 01 Feb 10 nicklas 49   implements EventHandler
5227 01 Feb 10 nicklas 50
5227 01 Feb 10 nicklas 51 {
5227 01 Feb 10 nicklas 52   
5227 01 Feb 10 nicklas 53   private final int parserIndex;
5227 01 Feb 10 nicklas 54   private final int posIndex;
5227 01 Feb 10 nicklas 55   private final PositionBatcher posBatcher;
5227 01 Feb 10 nicklas 56   private final int reporterIndex;
5227 01 Feb 10 nicklas 57   private final int externalIndex;
5227 01 Feb 10 nicklas 58   private ReporterBatcher reporterBatcher;
5227 01 Feb 10 nicklas 59   private int currentPosition;
5227 01 Feb 10 nicklas 60   
5227 01 Feb 10 nicklas 61   /**
5227 01 Feb 10 nicklas 62     Create a new event handler. The position value is expected to
5227 01 Feb 10 nicklas 63     be found in the data column given by the two indexes. The
5227 01 Feb 10 nicklas 64     first index is the parser index. Usually this should be 0=the 
5227 01 Feb 10 nicklas 65     master parser. The second index is the column holding the position
5227 01 Feb 10 nicklas 66     value, eg. the ID column which should also be 0.
5227 01 Feb 10 nicklas 67     
5227 01 Feb 10 nicklas 68     @param parserIndex The index of the parser, a value between 0 and
5227 01 Feb 10 nicklas 69       the number of synchronized data parsers
5227 01 Feb 10 nicklas 70     @param posIndex The index of the data column holding the position number, 
5227 01 Feb 10 nicklas 71       a value between 0 and the number of columns in the parsed file
5227 01 Feb 10 nicklas 72     @param posBatcher An optional position batcher if a position/reporter
5227 01 Feb 10 nicklas 73       needs to be created
5227 01 Feb 10 nicklas 74     @param reporterIndex The index of the data column holding the internal
5227 01 Feb 10 nicklas 75       reporter id, ignored if no position batcher is used, -1 if the
5227 01 Feb 10 nicklas 76       external id index should be used instead of the internal id
5227 01 Feb 10 nicklas 77     @param externalIndex The index of the data column holding the external
5227 01 Feb 10 nicklas 78       reporter id, only used if reporterIndex=-1 is specified
5227 01 Feb 10 nicklas 79   */
5227 01 Feb 10 nicklas 80   public PositionEventHandler(int parserIndex, int posIndex, 
5227 01 Feb 10 nicklas 81     PositionBatcher posBatcher, int reporterIndex, int externalIndex)
5227 01 Feb 10 nicklas 82   {
5227 01 Feb 10 nicklas 83     this.parserIndex = parserIndex;
5227 01 Feb 10 nicklas 84     this.posIndex = posIndex;
5227 01 Feb 10 nicklas 85     this.posBatcher = posBatcher;
5227 01 Feb 10 nicklas 86     this.reporterIndex = reporterIndex;
5227 01 Feb 10 nicklas 87     this.externalIndex = externalIndex;
5227 01 Feb 10 nicklas 88     if (reporterIndex == -1 && posBatcher != null)
5227 01 Feb 10 nicklas 89     {
5227 01 Feb 10 nicklas 90       reporterBatcher = ReporterBatcher.getNew(posBatcher.getDbControl());
5227 01 Feb 10 nicklas 91     }
5227 01 Feb 10 nicklas 92   }
5227 01 Feb 10 nicklas 93   
5227 01 Feb 10 nicklas 94   /**
5227 01 Feb 10 nicklas 95     Get the position number from the given parser/column. An exception is thrown
5227 01 Feb 10 nicklas 96     if a valid integer value is not found.
5227 01 Feb 10 nicklas 97   */
6875 20 Apr 15 nicklas 98   @SuppressWarnings("rawtypes")
5227 01 Feb 10 nicklas 99   @Override
5227 01 Feb 10 nicklas 100   public void handleEvent(EventType eventType, Object eventData, BfsParser parser)
5227 01 Feb 10 nicklas 101   {
5227 01 Feb 10 nicklas 102     if (eventType == SynchronizedSpotDataParser.DATA_EVENT)
5227 01 Feb 10 nicklas 103     {
5227 01 Feb 10 nicklas 104       SynchronizedData[] sdata = (SynchronizedData[])eventData;
5227 01 Feb 10 nicklas 105       SynchronizedData posData = sdata[parserIndex];
5227 01 Feb 10 nicklas 106       String[] rdata = posData.getData();
5227 01 Feb 10 nicklas 107       String value = rdata[posIndex];
5227 01 Feb 10 nicklas 108       Integer position = Values.getInteger(value, null);
5227 01 Feb 10 nicklas 109       if (position == null)
5227 01 Feb 10 nicklas 110       {
5227 01 Feb 10 nicklas 111         BfsParser posParser = posData.getParser();
5227 01 Feb 10 nicklas 112         throw new BaseException("Missing or invalid value for 'position'" +
5227 01 Feb 10 nicklas 113             " at line " + posParser.getCurrentLine() + 
5227 01 Feb 10 nicklas 114             ", column " + posIndex +
5227 01 Feb 10 nicklas 115             ", in file '" + posParser.getFilename() + "': " + value);
5227 01 Feb 10 nicklas 116       }
5227 01 Feb 10 nicklas 117       if (posBatcher != null)
5227 01 Feb 10 nicklas 118       {
5227 01 Feb 10 nicklas 119         ReporterData reporter = null;
5227 01 Feb 10 nicklas 120         if (reporterIndex != -1)
5227 01 Feb 10 nicklas 121         {
5227 01 Feb 10 nicklas 122           value = rdata[reporterIndex];
5227 01 Feb 10 nicklas 123           Integer reporterId = Values.getInteger(value, null);
5227 01 Feb 10 nicklas 124           if (reporterId != null) reporter = Reporter.getProxy(reporterIndex);
5227 01 Feb 10 nicklas 125         }
5227 01 Feb 10 nicklas 126         else
5227 01 Feb 10 nicklas 127         {
5227 01 Feb 10 nicklas 128           value = Values.getStringOrNull(rdata[externalIndex]);
5227 01 Feb 10 nicklas 129           if (value != null) reporter = reporterBatcher.getByExternalId(value);
5227 01 Feb 10 nicklas 130         }
5227 01 Feb 10 nicklas 131         posBatcher.insert(position, reporter);
5227 01 Feb 10 nicklas 132       }
5227 01 Feb 10 nicklas 133       currentPosition = position;
5227 01 Feb 10 nicklas 134     }
5227 01 Feb 10 nicklas 135   }
5227 01 Feb 10 nicklas 136
5227 01 Feb 10 nicklas 137   /**
5227 01 Feb 10 nicklas 138     Get the current position.
5227 01 Feb 10 nicklas 139   */
5227 01 Feb 10 nicklas 140   public int getCurrentPosition()
5227 01 Feb 10 nicklas 141   {
5227 01 Feb 10 nicklas 142     return currentPosition;
5227 01 Feb 10 nicklas 143   }
5227 01 Feb 10 nicklas 144 }