|
1 |
// STANDARD DECLARATIONS
|
|
2 |
package org.txm.macro.edition
|
|
3 |
|
|
4 |
import org.kohsuke.args4j.*
|
|
5 |
import groovy.transform.Field
|
|
6 |
import org.txm.rcp.swt.widget.parameters.*
|
|
7 |
import org.txm.searchengine.cqp.corpus.MainCorpus
|
|
8 |
import org.txm.objects.*
|
|
9 |
import org.txm.utils.io.FileCopy
|
|
10 |
|
|
11 |
@Field @Option(name="source_corpus", usage="the old edition name", widget="String", required=false, def="CORPUSSRC")
|
|
12 |
def source_corpus
|
|
13 |
|
|
14 |
@Field @Option(name="destination_corpus", usage="the old edition name", widget="String", required=false, def="CORPUSDEST")
|
|
15 |
def destination_corpus
|
|
16 |
|
|
17 |
@Field @Option(name="source_edition_name", usage="the old edition name", widget="String", required=false, def="default")
|
|
18 |
def source_edition_name
|
|
19 |
|
|
20 |
@Field @Option(name="destination_edition_name", usage="the old edition name", widget="String", required=false, def="default2")
|
|
21 |
def destination_edition_name
|
|
22 |
|
|
23 |
@Field @Option(name="default_destination_edition_names", usage="the old edition name", widget="String", required=false, def="default,default2")
|
|
24 |
def default_destination_edition_names
|
|
25 |
|
|
26 |
// Open the parameters input dialog box
|
|
27 |
if (!ParametersDialog.open(this)) return;
|
|
28 |
|
|
29 |
// END OF PARAMETERS
|
|
30 |
|
|
31 |
if (source_corpus == destination_corpus && source_edition_name == destination_edition_name) {
|
|
32 |
println "Error: can't import the same edition in the same corpus"
|
|
33 |
return false
|
|
34 |
}
|
|
35 |
|
|
36 |
Project src_project = Workspace.getInstance().getProject(source_corpus)
|
|
37 |
Project dest_project = Workspace.getInstance().getProject(destination_corpus)
|
|
38 |
|
|
39 |
def srcDeclaredEditions = src_project.getEditionNames()
|
|
40 |
def destDeclaredEditions = dest_project.getEditionNames()
|
|
41 |
// 0- ensure parameters
|
|
42 |
|
|
43 |
if (default_destination_edition_names.length() > 0) {
|
|
44 |
def split = default_destination_edition_names.split(",") as List
|
|
45 |
split.remove(destination_edition_name)
|
|
46 |
if (!srcDeclaredEditions.containsAll(split)) {
|
|
47 |
println "Error: the new default editions ($default_destination_edition_names) does not match the source corpus editions: "+srcDeclaredEditions+" and "+destination_edition_name
|
|
48 |
return false
|
|
49 |
}
|
|
50 |
}
|
|
51 |
|
|
52 |
if (src_project == null) {
|
|
53 |
println "Error: could not find source corpus with name=$source_corpus"
|
|
54 |
return false
|
|
55 |
}
|
|
56 |
|
|
57 |
if (dest_project == null) {
|
|
58 |
println "Error: could not find destination corpus with name=$destination_corpus"
|
|
59 |
return false
|
|
60 |
}
|
|
61 |
|
|
62 |
if (!srcDeclaredEditions.contains(source_edition_name)) {
|
|
63 |
println "Error: the $source_edition_name edition is not declared in the source corpus: $source_corpus"
|
|
64 |
return false
|
|
65 |
}
|
|
66 |
|
|
67 |
if (destDeclaredEditions.contains(destination_edition_name)) {
|
|
68 |
println "Error: the $destination_edition_name edition is already declared in the destination corpus: $destination_corpus"
|
|
69 |
return false
|
|
70 |
}
|
|
71 |
|
|
72 |
File srcEditionDirectory = new File(src_project.getProjectDirectory(), "HTML/$source_corpus/$source_edition_name")
|
|
73 |
if (!srcEditionDirectory.exists()) {
|
|
74 |
println "Error: the $srcEditionDirectory source edition directory doesn't exist."
|
|
75 |
return false
|
|
76 |
}
|
|
77 |
File destEditionDirectory = new File(dest_project.getProjectDirectory(), "HTML/$destination_corpus/$destination_edition_name")
|
|
78 |
if (destEditionDirectory.exists()) {
|
|
79 |
println "Error: the $destEditionDirectory destination edition directory already exists."
|
|
80 |
return false
|
|
81 |
}
|
|
82 |
|
|
83 |
// 1- copy the HTML directories
|
|
84 |
if (FileCopy.copydir(srcEditionDirectory, destEditionDirectory)) {
|
|
85 |
println "Error: the $srcEditionDirectory could not be copied to the $destEditionDirectory directory"
|
|
86 |
return false
|
|
87 |
}
|
|
88 |
|
|
89 |
// 2- copy the edition declaration
|
|
90 |
EditionDefinition srcEditionDeclaration = src_project.getEditionDefinition(source_edition_name)
|
|
91 |
EditionDefinition destEditionDeclaration = dest_project.getEditionDefinition(destination_edition_name)
|
|
92 |
|
|
93 |
//later use srcEditionDeclaration.copyParametersTo(destEditionDeclaration)
|
|
94 |
destEditionDeclaration.setPageBreakTag(srcEditionDeclaration.getPageElement())
|
|
95 |
destEditionDeclaration.setWordsPerPage(srcEditionDeclaration.getWordsPerPage())
|
|
96 |
destEditionDeclaration.setBuildEdition(srcEditionDeclaration.getBuildEdition())
|
|
97 |
if (srcEditionDeclaration.getImagesDirectory() != null) destEditionDeclaration.setImagesDirectory(srcEditionDeclaration.getImagesDirectory())
|
|
98 |
|
|
99 |
// 3- update the dest Text editions
|
|
100 |
|
|
101 |
int n = 0;
|
|
102 |
for (Text src_text : src_project.getTexts()) {
|
|
103 |
Edition src_edition = src_text.getEdition(source_edition_name))
|
|
104 |
if (edition == null) continue // nothing to do for this text
|
|
105 |
|
|
106 |
Text dest_text = dest_project.getText(src_text.getText())
|
|
107 |
if (dest_text == null) {
|
|
108 |
println "Error: cannot find the destination text matching: $src_text"
|
|
109 |
return false;
|
|
110 |
}
|
|
111 |
Edition dest_edition = dest_text.getEdition(destination_edition_name))
|
|
112 |
if (dest_edition != null) {
|
|
113 |
println "Warning: a destination edition already exists for the $dest_text text -> deleted!"
|
|
114 |
dest_edition.delete()
|
|
115 |
}
|
|
116 |
dest_edition = new Edition(dest_text)
|
|
117 |
dest_edition.setName(destination_edition_name)
|
|
118 |
dest_edition.pPageNames = src_edition.pPageNames // replace with getter&setter when published in TXM 0.8.1
|
|
119 |
dest_edition.pPageFirstWordIds = src_edition.pPageFirstWordIds
|
|
120 |
dest_edition.compute(false)
|
|
121 |
|
|
122 |
n++
|
|
123 |
}
|
|
124 |
|
|
125 |
if (n > 0) {
|
|
126 |
println "$n text editions have been created"
|
|
127 |
}
|
|
128 |
|
|
129 |
// 4- update default editions if necessary
|
|
130 |
if (default_destination_edition_names.length() > 0) {
|
|
131 |
def split = default_destination_edition_names.split(",") as List
|
|
132 |
if (split.size() > 0) {
|
|
133 |
dest_project.setDefaultEditionName(defaultEditions.join(","))
|
|
134 |
println "Default editions updated to: "+dest_project.getDefaultEditionName()
|
|
135 |
}
|
|
136 |
}
|
|
137 |
// 5- finally save preferences changes
|
|
138 |
dest_project.save()
|
|
139 |
println "Done."
|