226 |
09 Jan 07 |
mbayer |
1 |
/* |
226 |
09 Jan 07 |
mbayer |
Copyright (C) Authors contributing to this file. |
226 |
09 Jan 07 |
mbayer |
3 |
|
226 |
09 Jan 07 |
mbayer |
This program is free software; you can redistribute it and/or modify |
226 |
09 Jan 07 |
mbayer |
it under the terms of the GNU General Public License as published by |
226 |
09 Jan 07 |
mbayer |
the Free Software Foundation; either version 2 of the License, or |
226 |
09 Jan 07 |
mbayer |
(at your option) any later version. |
226 |
09 Jan 07 |
mbayer |
8 |
|
226 |
09 Jan 07 |
mbayer |
This program is distributed in the hope that it will be useful, |
226 |
09 Jan 07 |
mbayer |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
226 |
09 Jan 07 |
mbayer |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
226 |
09 Jan 07 |
mbayer |
GNU General Public License for more details. |
226 |
09 Jan 07 |
mbayer |
13 |
|
226 |
09 Jan 07 |
mbayer |
You should have received a copy of the GNU General Public License along |
226 |
09 Jan 07 |
mbayer |
with this program; if not, write to the Free Software Foundation, Inc., |
226 |
09 Jan 07 |
mbayer |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
226 |
09 Jan 07 |
mbayer |
17 |
|
226 |
09 Jan 07 |
mbayer |
See also http://www.gnu.org/licenses/gpl.txt. |
226 |
09 Jan 07 |
mbayer |
19 |
*/ |
226 |
09 Jan 07 |
mbayer |
20 |
package sbrn.base; |
226 |
09 Jan 07 |
mbayer |
21 |
|
226 |
09 Jan 07 |
mbayer |
22 |
import net.sf.basedb.core.DbControl; |
226 |
09 Jan 07 |
mbayer |
23 |
import net.sf.basedb.core.File; |
226 |
09 Jan 07 |
mbayer |
24 |
import net.sf.basedb.core.Include; |
226 |
09 Jan 07 |
mbayer |
25 |
import net.sf.basedb.core.ItemQuery; |
226 |
09 Jan 07 |
mbayer |
26 |
import net.sf.basedb.core.PluginConfiguration; |
226 |
09 Jan 07 |
mbayer |
27 |
import net.sf.basedb.core.PluginDefinition; |
226 |
09 Jan 07 |
mbayer |
28 |
import net.sf.basedb.core.SessionControl; |
226 |
09 Jan 07 |
mbayer |
29 |
import net.sf.basedb.core.query.Hql; |
226 |
09 Jan 07 |
mbayer |
30 |
import net.sf.basedb.core.query.Orders; |
226 |
09 Jan 07 |
mbayer |
31 |
import net.sf.basedb.plugins.RawDataFlatFileImporter; |
226 |
09 Jan 07 |
mbayer |
32 |
|
226 |
09 Jan 07 |
mbayer |
33 |
import java.io.InputStream; |
226 |
09 Jan 07 |
mbayer |
34 |
import java.util.LinkedList; |
226 |
09 Jan 07 |
mbayer |
35 |
import java.util.List; |
226 |
09 Jan 07 |
mbayer |
36 |
/** |
226 |
09 Jan 07 |
mbayer |
Class for detecting BASE2 plugin configurations that are capable of importing data from a given file. |
226 |
09 Jan 07 |
mbayer |
38 |
|
226 |
09 Jan 07 |
mbayer |
@author Micha Bayer - Plant Bioinformatics Group, Scottish Crop Research Institute |
226 |
09 Jan 07 |
mbayer |
email: sbrn@scri.ac.uk, web http://www.scri.ac.uk |
226 |
09 Jan 07 |
mbayer |
41 |
*/ |
226 |
09 Jan 07 |
mbayer |
42 |
public class PluginConfigDetector |
226 |
09 Jan 07 |
mbayer |
43 |
{ |
226 |
09 Jan 07 |
mbayer |
44 |
|
226 |
09 Jan 07 |
mbayer |
//========================================vars===================================== |
226 |
09 Jan 07 |
mbayer |
46 |
|
226 |
09 Jan 07 |
mbayer |
47 |
private SessionControl sc = null; |
226 |
09 Jan 07 |
mbayer |
48 |
|
226 |
09 Jan 07 |
mbayer |
//========================================c'tor===================================== |
226 |
09 Jan 07 |
mbayer |
50 |
|
226 |
09 Jan 07 |
mbayer |
51 |
public PluginConfigDetector(SessionControl sc) |
226 |
09 Jan 07 |
mbayer |
52 |
{ |
226 |
09 Jan 07 |
mbayer |
53 |
this.sc = sc; |
226 |
09 Jan 07 |
mbayer |
54 |
} |
226 |
09 Jan 07 |
mbayer |
55 |
|
226 |
09 Jan 07 |
mbayer |
//========================================methods===================================== |
226 |
09 Jan 07 |
mbayer |
57 |
|
226 |
09 Jan 07 |
mbayer |
58 |
/** |
226 |
09 Jan 07 |
mbayer |
This method finds a suitable plugin configuration for importing raw data from a flat file, given an example file |
226 |
09 Jan 07 |
mbayer |
with the input data. |
226 |
09 Jan 07 |
mbayer |
@param exampleFile -- an example file that contains data in the format we want to import |
226 |
09 Jan 07 |
mbayer |
@return a PluginConfiguration that can import the data in the example file |
226 |
09 Jan 07 |
mbayer |
63 |
*/ |
226 |
09 Jan 07 |
mbayer |
64 |
public PluginConfiguration detectPluginConfig(File exampleFile) throws Exception |
226 |
09 Jan 07 |
mbayer |
65 |
{ |
226 |
09 Jan 07 |
mbayer |
66 |
System.out.println("auto-detecting PluginConfiguration"); |
226 |
09 Jan 07 |
mbayer |
67 |
|
226 |
09 Jan 07 |
mbayer |
68 |
DbControl dc = sc.newDbControl(); |
226 |
09 Jan 07 |
mbayer |
69 |
PluginConfiguration pc = null; |
226 |
09 Jan 07 |
mbayer |
70 |
try |
226 |
09 Jan 07 |
mbayer |
71 |
{ |
229 |
22 Jan 07 |
mbayer |
//get the plugin definition for net.sf.basedb.plugins.RawDataFlatFileImporter |
229 |
22 Jan 07 |
mbayer |
73 |
PluginDefinition plugin = PluginDefinition.getByClassName(dc, "net.sf.basedb.plugins.RawDataFlatFileImporter"); |
226 |
09 Jan 07 |
mbayer |
74 |
List<PluginConfiguration> configList = getConfigs(plugin,dc,exampleFile); |
226 |
09 Jan 07 |
mbayer |
75 |
|
226 |
09 Jan 07 |
mbayer |
//the configList object will contain the PluginConfigurations we are after |
226 |
09 Jan 07 |
mbayer |
//just use the first one available |
226 |
09 Jan 07 |
mbayer |
//if there are multiple hits we assume they can all do the same job |
226 |
09 Jan 07 |
mbayer |
79 |
pc = configList.get(0); |
226 |
09 Jan 07 |
mbayer |
80 |
} |
234 |
14 Feb 07 |
mbayer |
81 |
catch (IndexOutOfBoundsException x) |
226 |
09 Jan 07 |
mbayer |
82 |
{ |
226 |
09 Jan 07 |
mbayer |
83 |
x.printStackTrace(); |
234 |
14 Feb 07 |
mbayer |
84 |
throw new Exception("no suitable plugin configuration found for this data type"); |
226 |
09 Jan 07 |
mbayer |
85 |
} |
226 |
09 Jan 07 |
mbayer |
86 |
finally |
226 |
09 Jan 07 |
mbayer |
87 |
{ |
226 |
09 Jan 07 |
mbayer |
88 |
if (dc != null) |
226 |
09 Jan 07 |
mbayer |
89 |
dc.close(); |
226 |
09 Jan 07 |
mbayer |
90 |
} |
226 |
09 Jan 07 |
mbayer |
91 |
|
226 |
09 Jan 07 |
mbayer |
92 |
return pc; |
226 |
09 Jan 07 |
mbayer |
93 |
} |
226 |
09 Jan 07 |
mbayer |
94 |
|
226 |
09 Jan 07 |
mbayer |
95 |
//------------------------------------------------------------------------------------------------------------------------------------------------------ |
226 |
09 Jan 07 |
mbayer |
96 |
|
226 |
09 Jan 07 |
mbayer |
97 |
/** |
226 |
09 Jan 07 |
mbayer |
Retrieves all suitable plugin configs for a given plugin that are capable of importing the file provided as an example |
226 |
09 Jan 07 |
mbayer |
@param PluginDefinition plugin - the plugin definition for the task at hand |
226 |
09 Jan 07 |
mbayer |
@param DbControl dc - current database control object |
226 |
09 Jan 07 |
mbayer |
@param File file - the file with the data we want to import |
226 |
09 Jan 07 |
mbayer |
102 |
*/ |
226 |
09 Jan 07 |
mbayer |
103 |
private List<PluginConfiguration> getConfigs(PluginDefinition plugin,DbControl dc, File file) |
226 |
09 Jan 07 |
mbayer |
104 |
{ |
226 |
09 Jan 07 |
mbayer |
//this takes the retrieved configs |
226 |
09 Jan 07 |
mbayer |
106 |
List<PluginConfiguration> configs = new LinkedList<PluginConfiguration>(); |
226 |
09 Jan 07 |
mbayer |
107 |
|
226 |
09 Jan 07 |
mbayer |
108 |
try |
226 |
09 Jan 07 |
mbayer |
109 |
{ |
226 |
09 Jan 07 |
mbayer |
//query for all configs associated with the plugin |
226 |
09 Jan 07 |
mbayer |
111 |
ItemQuery<PluginConfiguration> configQuery = plugin.getPluginConfigurations(); |
226 |
09 Jan 07 |
mbayer |
112 |
configQuery.order(Orders.asc(Hql.property("name"))); |
226 |
09 Jan 07 |
mbayer |
113 |
configQuery.include(Include.MINE, Include.SHARED, Include.IN_PROJECT, Include.OTHERS); |
226 |
09 Jan 07 |
mbayer |
114 |
|
226 |
09 Jan 07 |
mbayer |
//check them all out individually to see whether they can potentially import the file |
226 |
09 Jan 07 |
mbayer |
116 |
for (PluginConfiguration config : configQuery.list(dc)) |
226 |
09 Jan 07 |
mbayer |
117 |
{ |
226 |
09 Jan 07 |
mbayer |
//this is the importer plugin |
226 |
09 Jan 07 |
mbayer |
119 |
RawDataFlatFileImporter importer = plugin.newInstance(RawDataFlatFileImporter.class,null, sc, config, null); |
226 |
09 Jan 07 |
mbayer |
120 |
|
226 |
09 Jan 07 |
mbayer |
//get a stream from the file |
226 |
09 Jan 07 |
mbayer |
122 |
InputStream in = file.getDownloadStream(0); |
226 |
09 Jan 07 |
mbayer |
123 |
|
226 |
09 Jan 07 |
mbayer |
//check for importability |
226 |
09 Jan 07 |
mbayer |
125 |
if (importer.isImportable(in)) |
226 |
09 Jan 07 |
mbayer |
126 |
{ |
226 |
09 Jan 07 |
mbayer |
127 |
configs.add(config); |
226 |
09 Jan 07 |
mbayer |
128 |
} |
226 |
09 Jan 07 |
mbayer |
129 |
|
226 |
09 Jan 07 |
mbayer |
//close the stream |
226 |
09 Jan 07 |
mbayer |
131 |
in.close(); |
226 |
09 Jan 07 |
mbayer |
132 |
} |
226 |
09 Jan 07 |
mbayer |
133 |
} |
226 |
09 Jan 07 |
mbayer |
134 |
catch (Exception x) |
226 |
09 Jan 07 |
mbayer |
135 |
{ |
226 |
09 Jan 07 |
mbayer |
136 |
x.printStackTrace(); |
226 |
09 Jan 07 |
mbayer |
137 |
} |
226 |
09 Jan 07 |
mbayer |
138 |
return configs; |
226 |
09 Jan 07 |
mbayer |
139 |
} |
226 |
09 Jan 07 |
mbayer |
140 |
|
226 |
09 Jan 07 |
mbayer |
141 |
//------------------------------------------------------------------------------------------------------------------------------------------------------ |
226 |
09 Jan 07 |
mbayer |
142 |
|
226 |
09 Jan 07 |
mbayer |
143 |
}//end class |