5227 |
01 Feb 10 |
nicklas |
1 |
/** |
5227 |
01 Feb 10 |
nicklas |
$Id$ |
5227 |
01 Feb 10 |
nicklas |
3 |
|
5227 |
01 Feb 10 |
nicklas |
Copyright (C) 2010 Nicklas Nordborg |
5227 |
01 Feb 10 |
nicklas |
5 |
|
5227 |
01 Feb 10 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
5227 |
01 Feb 10 |
nicklas |
Available at http://base.thep.lu.se/ |
5227 |
01 Feb 10 |
nicklas |
8 |
|
5227 |
01 Feb 10 |
nicklas |
BASE is free software; you can redistribute it and/or |
5227 |
01 Feb 10 |
nicklas |
modify it under the terms of the GNU General Public License |
5227 |
01 Feb 10 |
nicklas |
as published by the Free Software Foundation; either version 3 |
5227 |
01 Feb 10 |
nicklas |
of the License, or (at your option) any later version. |
5227 |
01 Feb 10 |
nicklas |
13 |
|
5227 |
01 Feb 10 |
nicklas |
BASE is distributed in the hope that it will be useful, |
5227 |
01 Feb 10 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
5227 |
01 Feb 10 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5227 |
01 Feb 10 |
nicklas |
GNU General Public License for more details. |
5227 |
01 Feb 10 |
nicklas |
18 |
|
5227 |
01 Feb 10 |
nicklas |
You should have received a copy of the GNU General Public License |
5227 |
01 Feb 10 |
nicklas |
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 |
Event handler implementation intended to be used with a |
5227 |
01 Feb 10 |
nicklas |
{@link SynchronizedSpotDataParser} to extract the position |
5227 |
01 Feb 10 |
nicklas |
number from the parsed data. When cooperation is needed |
5227 |
01 Feb 10 |
nicklas |
between multiple event handlers (eg. using the {@link MultiEventHandler} |
5227 |
01 Feb 10 |
nicklas |
class, make sure that this event handler receives the event |
5227 |
01 Feb 10 |
nicklas |
before any other handler that needs the position value. |
5227 |
01 Feb 10 |
nicklas |
43 |
|
5227 |
01 Feb 10 |
nicklas |
@author Nicklas |
5227 |
01 Feb 10 |
nicklas |
@version 2.15 |
5227 |
01 Feb 10 |
nicklas |
@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 |
Create a new event handler. The position value is expected to |
5227 |
01 Feb 10 |
nicklas |
be found in the data column given by the two indexes. The |
5227 |
01 Feb 10 |
nicklas |
first index is the parser index. Usually this should be 0=the |
5227 |
01 Feb 10 |
nicklas |
master parser. The second index is the column holding the position |
5227 |
01 Feb 10 |
nicklas |
value, eg. the ID column which should also be 0. |
5227 |
01 Feb 10 |
nicklas |
67 |
|
5227 |
01 Feb 10 |
nicklas |
@param parserIndex The index of the parser, a value between 0 and |
5227 |
01 Feb 10 |
nicklas |
the number of synchronized data parsers |
5227 |
01 Feb 10 |
nicklas |
@param posIndex The index of the data column holding the position number, |
5227 |
01 Feb 10 |
nicklas |
a value between 0 and the number of columns in the parsed file |
5227 |
01 Feb 10 |
nicklas |
@param posBatcher An optional position batcher if a position/reporter |
5227 |
01 Feb 10 |
nicklas |
needs to be created |
5227 |
01 Feb 10 |
nicklas |
@param reporterIndex The index of the data column holding the internal |
5227 |
01 Feb 10 |
nicklas |
reporter id, ignored if no position batcher is used, -1 if the |
5227 |
01 Feb 10 |
nicklas |
external id index should be used instead of the internal id |
5227 |
01 Feb 10 |
nicklas |
@param externalIndex The index of the data column holding the external |
5227 |
01 Feb 10 |
nicklas |
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 |
Get the position number from the given parser/column. An exception is thrown |
5227 |
01 Feb 10 |
nicklas |
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 |
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 |
} |