Ticket #1303: ScanImporter.java

File ScanImporter.java, 9.0 KB (added by Jari Häkkinen, 15 years ago)
Line 
1/**
2 $Id: ScanImporter.java 4551 2008-09-29 07:47:24Z nicklas $
3
4 Copyright (C) 2008 Nicklas Nordborg
5
6 This file is part of BASE - BioArray Software Environment.
7 Available at http://base.thep.lu.se/
8
9 BASE is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 3
12 of the License, or (at your option) any later version.
13
14 BASE is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with BASE. If not, see <http://www.gnu.org/licenses/>.
21*/
22package net.sf.basedb.plugins.batchimport;
23
24import java.util.Collections;
25import java.util.List;
26import java.util.Set;
27
28import net.sf.basedb.core.DbControl;
29import net.sf.basedb.core.File;
30import net.sf.basedb.core.Hardware;
31import net.sf.basedb.core.Hybridization;
32import net.sf.basedb.core.Image;
33import net.sf.basedb.core.Item;
34import net.sf.basedb.core.ItemQuery;
35import net.sf.basedb.core.Path;
36import net.sf.basedb.core.PathParameterType;
37import net.sf.basedb.core.PluginParameter;
38import net.sf.basedb.core.Protocol;
39import net.sf.basedb.core.ProtocolType;
40import net.sf.basedb.core.Scan;
41import net.sf.basedb.core.SystemItems;
42import net.sf.basedb.core.Version;
43import net.sf.basedb.core.plugin.About;
44import net.sf.basedb.core.plugin.AboutImpl;
45import net.sf.basedb.core.plugin.GuiContext;
46import net.sf.basedb.util.parser.FlatFileParser;
47import net.sf.basedb.util.parser.Mapper;
48
49/**
50 Plug-in for importing scan items in a batch. The plug-in can create new
51 items and updated existing items.
52
53 @author nicklas
54 @version 2.8
55 @base.modified $Date: 2008-09-29 09:47:24 +0200 (Mon, 29 Sep 2008) $
56*/
57public class ScanImporterNMC
58 extends AbstractItemImporter<Scan>
59{
60
61 private static final Set<GuiContext> guiContexts =
62 Collections.singleton(new GuiContext(Item.SCAN, GuiContext.Type.LIST));
63
64 private static final About about =
65 new AboutImpl
66 (
67 "Scan importer with images",
68 "Imports and updates scans in a batch (linking with images).",
69 Version.getMajor() + "." + Version.getMinor() + "." + Version.getMaintenance(),
70 "2008, Base 2 development team",
71 null,
72 null,
73 "http://base.thep.lu.se"
74 );
75
76
77 protected static final PluginParameter<String> hardwareColumnMapping = new PluginParameter<String>(
78 "hardwareColumnMapping",
79 "Scanner",
80 "Mapping that picks the name or ID of the array slide scanner from the data columns. " +
81 "The plug-in will first try to find a scanner with the given name. If none is found and " +
82 "the value is numeric it will try to load by internal ID. " +
83 "Example: \\Scanner\\",
84 optionalColumnMapping
85 );
86
87 protected static final PluginParameter<String> hybridizationColumnMapping = new PluginParameter<String>(
88 "hybridizationColumnMapping",
89 "Hybridization",
90 "Mapping that picks the name or ID of the hybridization from the data columns. " +
91 "The plug-in will first try to find a hybridization with the given name. If none is found and " +
92 "the value is numeric it will try to load by internal ID. " +
93 "Example: \\Hybridization\\",
94 optionalColumnMapping
95 );
96
97 // extra parameter needed for linking with image files
98 protected static final PluginParameter<String> fileColumnMapping = new PluginParameter<String>(
99 "fileColumnMapping",
100 "File",
101 "Mapping that picks the path of a image file from the columns. " +
102 "The path can be an absolute path (starting with /) or relative " +
103 "to the 'Data directory' parameter." +
104 "Example: \\File\\",
105 optionalColumnMapping
106 );
107
108 public ScanImporterNMC()
109 {}
110
111 /*
112 From the Plugin interface
113 ------------------------------------
114 */
115 @Override
116 public About getAbout()
117 {
118 return about;
119 }
120 // ------------------------------------
121
122 /*
123 From the InteractivePlugin interface
124 ------------------------------------
125 */
126 @Override
127 public Set<GuiContext> getGuiContexts()
128 {
129 return guiContexts;
130 }
131 // ------------------------------------
132
133 /*
134 From the AbstractItemImporter class
135 ------------------------------------
136 */
137
138 private Mapper nameMapper;
139 private Mapper descriptionMapper;
140 private Mapper protocolMapper;
141 private Mapper hardwareMapper;
142 private Mapper hybridizationMapper;
143 //extra mappers for linking with image file
144 private Mapper fileMapper;
145 private String imagesDirectory;
146
147 /**
148 Useable methods are:
149 <ul>
150 <li>{@link PropertyIdMethod#NAME}
151 <li>{@link InternalIdMethod#INTERNAL_ID}
152 <li>{@link FallbackIdMethod#NAME_OR_ID}
153 </ul>
154 */
155 @Override
156 protected IdMethod[] getIdMethods()
157 {
158 return new IdMethod[] { PropertyIdMethod.NAME,
159 InternalIdMethod.INTERNAL_ID, FallbackIdMethod.NAME_OR_ID};
160 }
161
162
163 /**
164 Add parameter for "Image directory" directory where image files may be located.
165 */
166 @Override
167 protected void addMoreJobParameters(List<PluginParameter<?>> parameters)
168 {
169 parameters.add(new PluginParameter<String>(
170 "imagesDirectory",
171 "Images directory",
172 "A base directory were image files are located. Relative file paths are " +
173 "resolved against this directory. If no directory is given, only absolute " +
174 "paths (=paths starting with /) can be used.",
175 new PathParameterType(Path.Type.DIRECTORY))
176 );
177 }
178
179
180 /**
181 Adds column mappings for name, externalId, description,
182 protocol, creation date, original quantity, pooled, parent
183 and used quantity from parent.
184 */
185 @Override
186 protected void addMoreColumnMappingParameters(List<PluginParameter<?>> parameters)
187 {
188 parameters.add(internalIdColumnMapping);
189 parameters.add(nameColumnMapping);
190 parameters.add(descriptionColumnMapping);
191 parameters.add(hybridizationColumnMapping);
192 parameters.add(protocolColumnMapping);
193 parameters.add(hardwareColumnMapping);
194 parameters.add(fileColumnMapping);
195 }
196
197 /**
198 Calls {@link Scan#getQuery()}.
199 */
200 @Override
201 protected ItemQuery<Scan> createItemQuery()
202 {
203 return Scan.getQuery();
204 }
205
206 @Override
207 protected void createColumnMappers(FlatFileParser ffp, boolean cropStrings)
208 {
209 nameMapper = getMapper(ffp, (String)job.getValue("nameColumnMapping"),
210 cropStrings ? Scan.MAX_NAME_LENGTH : null, null);
211 descriptionMapper = getMapper(ffp, (String)job.getValue("descriptionColumnMapping"),
212 cropStrings ? Scan.MAX_DESCRIPTION_LENGTH : null, null);
213 protocolMapper = getMapper(ffp, (String)job.getValue("protocolColumnMapping"), null, null);
214 hardwareMapper = getMapper(ffp, (String)job.getValue("hardwareColumnMapping"), null, null);
215 hybridizationMapper = getMapper(ffp, (String)job.getValue("hybridizationColumnMapping"), null, null);
216 fileMapper = getMapper(ffp, (String)job.getValue("fileColumnMapping"), null, null);
217 imagesDirectory = (String)job.getValue("imagesDirectory");
218 if (imagesDirectory != null && !imagesDirectory.endsWith("/"))
219 {
220 imagesDirectory += "/";
221 }
222 }
223
224 @Override
225 protected Scan createItem(DbControl dc, FlatFileParser.Data data)
226 {
227 Hybridization hyb = null;
228 if (hybridizationMapper != null)
229 {
230 hyb = findHybridization(dc, FallbackIdMethod.NAME_OR_ID, hybridizationMapper.getValue(data));
231 }
232
233 Scan scan = Scan.getNew(dc, hyb);
234 updateItem(dc, scan, data);
235 return scan;
236 }
237
238 @Override
239 protected void updateItem(DbControl dc, Scan scan, FlatFileParser.Data data)
240 {
241 if (nameMapper != null) scan.setName(nameMapper.getValue(data));
242 if (descriptionMapper != null) scan.setDescription(descriptionMapper.getValue(data));
243 if (hybridizationMapper != null && scan.isInDatabase())
244 {
245 String nameOrId = hybridizationMapper.getValue(data);
246 if (nameOrId != null)
247 {
248 Hybridization hyb = findHybridization(dc, FallbackIdMethod.NAME_OR_ID, hybridizationMapper.getValue(data));
249 scan.setHybridization(hyb);
250 }
251 }
252 if (protocolMapper != null)
253 {
254 String nameOrId = protocolMapper.getValue(data);
255 ProtocolType type = ProtocolType.getById(dc, SystemItems.getId(ProtocolType.SCANNING));
256 Protocol protocol = findProtocol(dc, FallbackIdMethod.NAME_OR_ID, nameOrId, type);
257 if (nameOrId == null || protocol != null) scan.setProtocol(protocol);
258 }
259 if (hardwareMapper != null)
260 {
261 String nameOrId = hardwareMapper.getValue(data);
262 Hardware scanner = findScanner(dc, FallbackIdMethod.NAME_OR_ID, nameOrId);
263 if (nameOrId == null || scanner != null) scan.setScanner(scanner);
264 }
265
266 updateMultiLineItem(dc, scan, data, 0);
267 }
268
269 protected void updateMultiLineItem(DbControl dc, Scan scan, FlatFileParser.Data data, int multiLineNum)
270 {
271
272 if (fileMapper != null) {
273 String filePath = fileMapper.getValue(data);
274 File file = null;
275 if (filePath != null)
276 {
277 if (!filePath.startsWith("/") && imagesDirectory != null)
278 {
279 filePath = imagesDirectory + filePath;
280 }
281 file = File.getByPath(dc, new Path(filePath, Path.Type.FILE), false);
282 }
283 if (file != null)
284 {
285 Image image = scan.newImage();
286 image.setFile(file);
287 image.setName(scan.getName()+"_"+file.getName());
288 dc.saveItemIf(scan, image, false);
289 }
290
291 }
292
293 }
294
295}