Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / macroproto / RunImportMacro.groovy @ 148

History | View | Annotate | Download (2.9 kB)

1
package org.txm.macroproto
2
import groovy.transform.Field
3

    
4
import org.kohsuke.args4j.*
5
import org.txm.Toolbox
6
import org.txm.objects.BaseParameters
7
import org.txm.rcpapplication.commands.*
8
import org.txm.rcpapplication.swt.widget.parameters.*
9

    
10
// PARAMETERS
11
@Field @Option(name="inputDirectory", usage="Dossier qui contient les fichiers à importer", widget="Folder", required=true, def='')
12
File inputDirectory
13

    
14
@Field @Option(name="importName", usage="Nom du module d'import", widget="String", required=true, def='TXT+CSV | XML/w+CSV | XML-Factiva | XML-TEI-BFM | XML-TEI-Frantext | XML-TEI-TXM | XML-Transcriber+CSV | XML-TMX | CNR+CSV | Hyperbase | Alceste | CWB | DOC')
15
// choose between : 
16
def importName
17

    
18
if (!ParametersDialog.open(this)) return;
19
// END OF PARAMETERS
20
// TXT+CSV | XML/w+CSV | XML-Factiva | XML-TEI-BFM | XML-TEI-Frantext | XML-TEI-TXM | XML-Transcriber+CSV | XML-TMX | CNR+CSV | Hyperbase | Alceste | CWB
21

    
22
// fix possible miss write
23
importName = importName.toUpperCase()
24
importName = importName.replaceAll(" ", "")
25
importName = importName.trim()
26
importName = importName.replaceAll("\\+", "")
27
importName = importName.replaceAll("-", "")
28
importName = importName.replaceAll("/", "")
29

    
30
def scripts = [:]; // 12
31
scripts["TXTCSV"] = "quickLoader.groovy"
32
scripts["XMLWCSV"] = "xmlLoader.groovy"
33
scripts["XMLFACTIVA"] = "factivaLoader.groovy"
34
scripts["XMLTEIBFM"] = "bfmLoader.groovy"
35
scripts["XMLTEIFrantex"] = "frantextLoader.groovy"
36
scripts["XMLTEITXM"] = "xmltxmLoader.groovy"
37
scripts["XMLTranscriberCSV"] = "transcriberLoader.groovy"
38
scripts["XMLTMX"] = "tmxLoader.groovy"
39
scripts["CNRCSV"] = "discoursLoader.groovy"
40
scripts["HYPERBASE"] = "hyperbaseLoader.groovy"
41
scripts["ALCESTE"] = "alcesteLoader.groovy"
42
scripts["CWB"] = "wtcLoader.groovy"
43
scripts["DOC"] = "docLoader.groovy"
44

    
45
def scriptName = scripts.get(importName)
46
if (scriptName == null) {
47
        println "Error: no import script for name $importName"
48
        return
49
}
50

    
51
String txmhome = Toolbox.getParam(Toolbox.USER_TXM_HOME);
52
File scriptsdir = new File(txmhome, "scripts/import")
53
File script = new File(scriptsdir, scriptName)
54

    
55
if (!script.exists()) {
56
        println "Error: no script found for import name ${importName}: $script"
57
        return
58
}
59

    
60
if (inputDirectory.exists()) {
61
        File paramFile = new File(inputDirectory, "import.xml")
62
        if (!paramFile.exists()) { // create, if it does not exists
63
                BaseParameters.createEmptyParams(paramFile, inputDirectory.getName())
64
        }
65

    
66
        final BaseParameters params = new BaseParameters(paramFile);
67
        params.load();
68
        params.rootDir = inputDirectory.getAbsolutePath(); // fix
69
        params.saveToElement();
70

    
71
        if (!params.save()) {
72
                System.out.println("ERROR: could not save import configuration: "+ params.paramFile);
73
                return;
74
        }
75

    
76
        monitor.syncExec(new Runnable() {
77
                public void run() {
78
                        ExecuteImportScript.executeScript(script.getAbsolutePath(), params);
79
                }
80
        });
81
} else {
82
        System.out.println("Warning: Missing inputDirectory directory: "+ inputDirectory);
83
}
84

    
85