ExtensionManual: CreateXFileImportJob.java

File CreateXFileImportJob.java, 2.1 kB (added by gregory, 1 year ago)

Action that creates a job

Line 
1 /**
2  *
3  */
4 package greg;
5
6 import java.util.List;
7 import org.proteios.action.ProteiosAction;
8 import org.proteios.action.job.ListJobs;
9 import org.proteios.core.DbControl;
10 import org.proteios.core.File;
11 import org.proteios.core.FileParameterType;
12 import org.proteios.core.ItemFactory;
13 import org.proteios.core.Job;
14 import org.proteios.core.PluginDefinition;
15 import org.proteios.core.StringParameterType;
16 import org.proteios.gui.form.FormFactory;
17 import se.lu.thep.waf.ActionException;
18 import se.lu.thep.waf.constraints.InvalidParameterValue;
19
20 /**
21  * Creates an ImportXFile job for each file id
22  *
23  * @author gregory
24  */
25 public class CreateXFileImportJob
26     extends ProteiosAction<CreateXFileImportJob>
27 {
28   @Override
29   protected void runMe()
30       throws ActionException, InvalidParameterValue
31   {
32     /*
33      * Get a valid list of file ids
34      */
35     List<Integer> files = getValidIntegerList(FormFactory.VID);
36     /*
37      * Get a valid prefix
38      */
39     String prefix = getValidString(ImportXFileForm.VPREFIX);
40     /*
41      * Get the plugin to execute by getting it's PluginDefinition. Make sure
42      * the plugin is installed first.
43      */
44     DbControl dc = newDbControl();
45     PluginDefinition plugin = PluginDefinition.getByClassName(dc,
46       ImportXFile.class.getName());
47     /*
48      * Get the files
49      */
50     ItemFactory factory = new ItemFactory(dc);
51     int jobCount = 0;
52     for (Integer fileId : files)
53     {
54       Job job = factory.createJob(plugin, null);
55       job.setName(plugin.getName());
56       // Tell the job which file to work with
57       File file = factory.getById(File.class, fileId);
58       job
59         .setDescription("Import of " + file.getName() + " [" + fileId + "]");
60       // Tell the job which file to work with
61       FileParameterType fileParam = new FileParameterType();
62       job.setParameterValue(ImportXFile.FILEPARAM, fileParam, file);
63       // Tell the job which prefix to add
64       StringParameterType prefixParam = new StringParameterType();
65       job.setParameterValue(ImportXFile.PREFIXPARAM, prefixParam, prefix);
66       //
67       dc.saveItem(job);
68       jobCount++;
69     }
70     dc.commit();
71     setMessage(jobCount + " jobs created");
72     setForwardTo(ListJobs.class);
73   }
74 }