3987 |
17 Nov 10 |
gregory |
1 |
/* |
3987 |
17 Nov 10 |
gregory |
Copyright (C) 2006 Gregory Vincic |
3987 |
17 Nov 10 |
gregory |
3 |
|
3987 |
17 Nov 10 |
gregory |
This file is part of Proteios. |
3987 |
17 Nov 10 |
gregory |
Available at http://www.proteios.org/ |
3987 |
17 Nov 10 |
gregory |
6 |
|
3987 |
17 Nov 10 |
gregory |
Proteios is free software; you can redistribute it and/or modify it |
3987 |
17 Nov 10 |
gregory |
under the terms of the GNU General Public License as published by |
3987 |
17 Nov 10 |
gregory |
the Free Software Foundation; either version 2 of the License, or |
3987 |
17 Nov 10 |
gregory |
(at your option) any later version. |
3987 |
17 Nov 10 |
gregory |
11 |
|
3987 |
17 Nov 10 |
gregory |
Proteios is distributed in the hope that it will be useful, but |
3987 |
17 Nov 10 |
gregory |
WITHOUT ANY WARRANTY; without even the implied warranty of |
3987 |
17 Nov 10 |
gregory |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3987 |
17 Nov 10 |
gregory |
General Public License for more details. |
3987 |
17 Nov 10 |
gregory |
16 |
|
3987 |
17 Nov 10 |
gregory |
You should have received a copy of the GNU General Public License |
3987 |
17 Nov 10 |
gregory |
along with this program; if not, write to the Free Software |
3987 |
17 Nov 10 |
gregory |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
3987 |
17 Nov 10 |
gregory |
02111-1307, USA. |
3987 |
17 Nov 10 |
gregory |
21 |
*/ |
3987 |
17 Nov 10 |
gregory |
22 |
package greg; |
3987 |
17 Nov 10 |
gregory |
23 |
|
3987 |
17 Nov 10 |
gregory |
24 |
import org.proteios.core.BaseException; |
3987 |
17 Nov 10 |
gregory |
25 |
import org.proteios.core.DbControl; |
3987 |
17 Nov 10 |
gregory |
26 |
import org.proteios.core.File; |
3987 |
17 Nov 10 |
gregory |
27 |
import org.proteios.core.ProgressReporter; |
3987 |
17 Nov 10 |
gregory |
28 |
import org.proteios.core.SessionControl; |
3987 |
17 Nov 10 |
gregory |
29 |
import org.proteios.core.plugin.About; |
3987 |
17 Nov 10 |
gregory |
30 |
import org.proteios.core.plugin.AboutImpl; |
3987 |
17 Nov 10 |
gregory |
31 |
import org.proteios.core.plugin.ParameterValues; |
3987 |
17 Nov 10 |
gregory |
32 |
import org.proteios.core.plugin.Plugin; |
3987 |
17 Nov 10 |
gregory |
33 |
import org.proteios.core.plugin.Request; |
3987 |
17 Nov 10 |
gregory |
34 |
import org.proteios.core.plugin.Response; |
3987 |
17 Nov 10 |
gregory |
35 |
|
3987 |
17 Nov 10 |
gregory |
36 |
/** |
3987 |
17 Nov 10 |
gregory |
* This is a demo plugin that could be used as a template when writing your own |
3987 |
17 Nov 10 |
gregory |
* plugins. It's a part of the ImportXFile extension available at |
3987 |
17 Nov 10 |
gregory |
* www.proteios.org. |
3987 |
17 Nov 10 |
gregory |
40 |
* |
3987 |
17 Nov 10 |
gregory |
* @author gregory |
3987 |
17 Nov 10 |
gregory |
42 |
*/ |
3987 |
17 Nov 10 |
gregory |
43 |
public class ImportXFile |
3987 |
17 Nov 10 |
gregory |
44 |
implements Plugin |
3987 |
17 Nov 10 |
gregory |
45 |
{ |
3987 |
17 Nov 10 |
gregory |
46 |
/** |
3987 |
17 Nov 10 |
gregory |
* Key to identify the file parameter. The action that creates a job using |
3987 |
17 Nov 10 |
gregory |
* this plugin should name the incomming file parameter with this key. |
3987 |
17 Nov 10 |
gregory |
49 |
*/ |
3987 |
17 Nov 10 |
gregory |
50 |
public static final String FILEPARAM = "file"; |
3987 |
17 Nov 10 |
gregory |
51 |
/** |
3987 |
17 Nov 10 |
gregory |
* The file to "import". |
3987 |
17 Nov 10 |
gregory |
53 |
* |
3987 |
17 Nov 10 |
gregory |
* @see #init(SessionControl, ParameterValues, ParameterValues) |
3987 |
17 Nov 10 |
gregory |
55 |
*/ |
3987 |
17 Nov 10 |
gregory |
56 |
private File file = null; |
3987 |
17 Nov 10 |
gregory |
57 |
/** |
3987 |
17 Nov 10 |
gregory |
* Key to identify the prefix parameter. The action that creates a job using |
3987 |
17 Nov 10 |
gregory |
* this plugin should name the incomming prefix parameter with this key. |
3987 |
17 Nov 10 |
gregory |
60 |
*/ |
3987 |
17 Nov 10 |
gregory |
61 |
public static final String PREFIXPARAM = "prefix"; |
3987 |
17 Nov 10 |
gregory |
62 |
/** |
3987 |
17 Nov 10 |
gregory |
* The prefix to prepend to the imported file. |
3987 |
17 Nov 10 |
gregory |
64 |
* |
3987 |
17 Nov 10 |
gregory |
* @see #init(SessionControl, ParameterValues, ParameterValues) |
3987 |
17 Nov 10 |
gregory |
66 |
*/ |
3987 |
17 Nov 10 |
gregory |
67 |
private String prefix = null; |
3987 |
17 Nov 10 |
gregory |
68 |
|
3987 |
17 Nov 10 |
gregory |
69 |
|
3987 |
17 Nov 10 |
gregory |
70 |
/* |
3987 |
17 Nov 10 |
gregory |
* (non-Javadoc) |
3987 |
17 Nov 10 |
gregory |
72 |
* |
3987 |
17 Nov 10 |
gregory |
* @see org.proteios.core.plugin.Plugin#getAbout() |
3987 |
17 Nov 10 |
gregory |
74 |
*/ |
3987 |
17 Nov 10 |
gregory |
75 |
public About getAbout() |
3987 |
17 Nov 10 |
gregory |
76 |
{ |
3987 |
17 Nov 10 |
gregory |
77 |
return new AboutImpl("ImportXFile", |
3987 |
17 Nov 10 |
gregory |
78 |
"Example plugin used as a template for import plugins", "0.11", "", |
3987 |
17 Nov 10 |
gregory |
79 |
"Gregory Vincic", "gregory@thep.lu.se", "www.proteios.org"); |
3987 |
17 Nov 10 |
gregory |
80 |
} |
3987 |
17 Nov 10 |
gregory |
81 |
|
3987 |
17 Nov 10 |
gregory |
82 |
|
3987 |
17 Nov 10 |
gregory |
83 |
/* |
3987 |
17 Nov 10 |
gregory |
* (non-Javadoc) |
3987 |
17 Nov 10 |
gregory |
85 |
* |
3987 |
17 Nov 10 |
gregory |
* @see org.proteios.core.plugin.Plugin#getMainType() |
3987 |
17 Nov 10 |
gregory |
87 |
*/ |
3987 |
17 Nov 10 |
gregory |
88 |
public MainType getMainType() |
3987 |
17 Nov 10 |
gregory |
89 |
{ |
3987 |
17 Nov 10 |
gregory |
90 |
return MainType.IMPORT; |
3987 |
17 Nov 10 |
gregory |
91 |
} |
3987 |
17 Nov 10 |
gregory |
92 |
|
3987 |
17 Nov 10 |
gregory |
93 |
|
3987 |
17 Nov 10 |
gregory |
94 |
/* |
3987 |
17 Nov 10 |
gregory |
* (non-Javadoc) |
3987 |
17 Nov 10 |
gregory |
96 |
* |
3987 |
17 Nov 10 |
gregory |
* @see org.proteios.core.plugin.Plugin#run(org.proteios.core.plugin.Request, |
3987 |
17 Nov 10 |
gregory |
* org.proteios.core.plugin.Response, |
3987 |
17 Nov 10 |
gregory |
* org.proteios.core.ProgressReporter) |
3987 |
17 Nov 10 |
gregory |
100 |
*/ |
3987 |
17 Nov 10 |
gregory |
101 |
public void run(Request request, Response response, |
3987 |
17 Nov 10 |
gregory |
102 |
ProgressReporter progress) |
3987 |
17 Nov 10 |
gregory |
103 |
{ |
3987 |
17 Nov 10 |
gregory |
104 |
/* |
3987 |
17 Nov 10 |
gregory |
* Add the prefix to the file. This is where you'd put your fancy |
3987 |
17 Nov 10 |
gregory |
* algorithm that does something to the file in question. |
3987 |
17 Nov 10 |
gregory |
107 |
*/ |
3987 |
17 Nov 10 |
gregory |
108 |
file.setName(prefix + file.getName()); |
3987 |
17 Nov 10 |
gregory |
109 |
file.getDbControl().commit(); |
3987 |
17 Nov 10 |
gregory |
110 |
response.setDone("Done with this import"); |
3987 |
17 Nov 10 |
gregory |
111 |
} |
3987 |
17 Nov 10 |
gregory |
112 |
|
3987 |
17 Nov 10 |
gregory |
113 |
|
3987 |
17 Nov 10 |
gregory |
114 |
public void done() |
3987 |
17 Nov 10 |
gregory |
115 |
{ |
3987 |
17 Nov 10 |
gregory |
// Called when the execution has ended. Do some cleanup if you need here |
3987 |
17 Nov 10 |
gregory |
117 |
} |
3987 |
17 Nov 10 |
gregory |
118 |
|
3987 |
17 Nov 10 |
gregory |
119 |
|
3987 |
17 Nov 10 |
gregory |
120 |
public void init(SessionControl sc, ParameterValues configuration, |
3987 |
17 Nov 10 |
gregory |
121 |
ParameterValues job) |
3987 |
17 Nov 10 |
gregory |
122 |
throws BaseException |
3987 |
17 Nov 10 |
gregory |
123 |
{ |
3987 |
17 Nov 10 |
gregory |
// Before running this plugin it's initialized here. |
3987 |
17 Nov 10 |
gregory |
125 |
this.file = (File) job.getValue(FILEPARAM); |
3987 |
17 Nov 10 |
gregory |
126 |
this.prefix = (String) job.getValue(PREFIXPARAM); |
3987 |
17 Nov 10 |
gregory |
127 |
if (file == null || prefix == null) |
3987 |
17 Nov 10 |
gregory |
128 |
{ |
3987 |
17 Nov 10 |
gregory |
129 |
throw new BaseException( |
3987 |
17 Nov 10 |
gregory |
130 |
"Failed to initialize job. [file=" + file + "][prefix=" + prefix + "]"); |
3987 |
17 Nov 10 |
gregory |
131 |
} |
3987 |
17 Nov 10 |
gregory |
// Reattach the item using a fresh DbControl. Any changes to the file |
3987 |
17 Nov 10 |
gregory |
// are commited in the run(...) method. |
3987 |
17 Nov 10 |
gregory |
134 |
DbControl dc = sc.newDbControl(); |
3987 |
17 Nov 10 |
gregory |
135 |
dc.reattachItem(this.file); |
3987 |
17 Nov 10 |
gregory |
136 |
} |
3987 |
17 Nov 10 |
gregory |
137 |
} |