Révision 2730

tmp/org.txm.groovy.core/src/groovy/org/txm/macro/edition/RenameEditionMacro.groovy (revision 2730)
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

  
10
if (!(corpusViewSelection instanceof MainCorpus)) {
11
	println "Please select a main corpus to update the editions"
12
	return false
13
}
14
@Field @Option(name="oldName", usage="the old edition name", widget="String", required=false, def="default")
15
def oldName
16

  
17
@Field @Option(name="newName", usage="the old edition name", widget="String", required=false, def="default")
18
def newName
19

  
20
// Open the parameters input dialog box
21
if (!ParametersDialog.open(this)) return;
22

  
23
// END OF PARAMETERS
24

  
25
MainCorpus corpus = corpusViewSelection
26
Project project = corpus.getProject()
27
String pname = project.getName()
28

  
29
def declaredEditions = project.getEditionNames()
30

  
31
// 0- ensure parameters
32
if (!declaredEditions.contains(oldName)) {
33
	println "Error: the $oldName edition is not declared in the corpus: $declaredEditions"
34
	return false
35
}
36
if (declaredEditions.contains(newName)) {
37
	println "Error: the $newName edition is already declared in the corpus: $declaredEditions"
38
	return false
39
}
40

  
41
File oldEditionDirectory = new File(project.getProjectDirectory(), "HTML/$pname/$oldName")
42
if (!oldEditionDirectory.exists()) {
43
	println "Error: the $oldEditionDirectory edition directory doesn't exist."
44
	return false
45
}
46
File newEditionDirectory = new File(project.getProjectDirectory(), "HTML/$pname/$newName")
47
if (newEditionDirectory.exists()) {
48
	println "Error: the $newEditionDirectory edition directory already exists."
49
	return false
50
}
51

  
52
// 1- move the HTML directories
53
if (!oldEditionDirectory.renameTo(newEditionDirectory)) {
54
	println "Error: the $oldEditionDirectory could not be moved to the $newEditionDirectory directory"
55
	return false
56
}
57

  
58
// 2- rename the edition declaration
59
EditionDefinition oldEditionDeclaration = project.getEditionDefinition(oldName)
60
EditionDefinition newEditionDeclaration = project.getEditionDefinition(newName)
61

  
62
//later use oldEditionDeclaration.copyParametersTo(newEditionDeclaration)
63
newEditionDeclaration.setPageBreakTag(oldEditionDeclaration.getPageElement())
64
newEditionDeclaration.setWordsPerPage(oldEditionDeclaration.getWordsPerPage())
65
newEditionDeclaration.setBuildEdition(oldEditionDeclaration.getBuildEdition())
66
if (oldEditionDeclaration.getImagesDirectory() != null) newEditionDeclaration.setImagesDirectory(oldEditionDeclaration.getImagesDirectory())
67

  
68
oldEditionDeclaration.delete() // later this method will return false if the deletion failed
69

  
70
// 3- update the Text editions
71

  
72
int n = 0;
73
for (Text text : project.getTexts()) {
74
	Edition edition = text.getEdition(oldName)
75
	if (edition == null) continue // nothing to do for this text
76
	
77
	edition.setName(newName)
78
	edition.compute(false)
79
	n++
80
}
81

  
82
if (n > 0) {
83
	println "$n text editions have been updated"
84
}
85

  
86
// 4- update default editions if necessary
87
def defaultEditions = project.getDefaultEditionName().split(",") as List
88
int idx = defaultEditions.indexOf(oldName)
89
if (idx >= 0) {
90
	defaultEditions[idx] = newName
91
	project.setDefaultEditionName(defaultEditions.join(","))
92
	println "Default editions updated to: "+project.getDefaultEditionName()
93
}
94

  
95

  
96
// 5- finally save preferences changes
97
project.save()
98
println "Done."

Formats disponibles : Unified diff