Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / scripts / FixPortalProfiles.groovy @ 187

History | View | Annotate | Download (3.5 kB)

1
package org.txm.scripts
2

    
3
import groovy.xml.StreamingMarkupBuilder
4

    
5
def importxml = new File("/home/mdecorde/TEMP/import.xml")
6
def usersdir = new File("/home/mdecorde/TEMP/users")
7
def usersdir2 = new File("/home/mdecorde/TEMP/users2")
8
usersdir2.deleteDir()
9
usersdir2.mkdir()
10

    
11
// 1- get subcorpus & partitions per user
12
def entities = [:];
13
corpusInfo = [:];
14
def root = new XmlSlurper().parse(importxml)
15

    
16
def corpusNode = null
17
for (def tmp : root.corpora.corpus) corpusNode = tmp
18
def prefix = "/"+corpusNode.@name.toString()
19

    
20
for (def preBuild : corpusNode.preBuild) {
21
        for (def child : preBuild.children()) {
22
                def tmp_entities = getEntities(prefix, child)
23
                def user = "";
24
                for (def entity : tmp_entities) {
25
                        String name = entity;
26
                        user = entity.substring(prefix.length()+1, entity.indexOf(":"))
27
                        //println user
28
                        break;
29
                }
30

    
31
                if (!entities.keySet().contains(user)) entities[user] = new HashSet<String>();
32
                for (def entity : tmp_entities) {
33
                        entities[user] << entity
34
                }
35
        }
36
}
37
println entities
38
println corpusInfo
39

    
40
// 2- fix user_xxxx.xml
41
for (def file : usersdir.listFiles()) {
42
        if (!file.getName().endsWith(".xml")) continue;
43

    
44
        def rootuser = new XmlParser().parse(file);
45
        String login = rootuser.@login
46
        if (entities.keySet().contains(login)) {
47
                def user_entities = entities[login]
48

    
49
                def existingEntities = new HashSet();
50
                for (def entityNode : rootuser.permissions.entity) {
51
                        existingEntities << entityNode.@path.toString()
52
                }
53
                println "FIXING $file with $user_entities existing: $existingEntities"
54
                for (def newentity : user_entities) {
55
                        if (!existingEntities.contains(newentity)) {
56
                                def permissionNode = null
57
                                for( def tmp : rootuser.permissions) permissionNode = tmp
58
                                // new
59
                                println " append $newentity "
60
                                def newnode = new Node(null, "entity");
61
                                newnode.@path = newentity
62
                                addPermissions(newnode, corpusInfo[newentity])
63
                                permissionNode.children().add(newnode)
64
                        }
65
                }
66

    
67
                new File(usersdir2, file.getName()).withWriter("UTF-8") { writer ->
68
                        new XmlNodePrinter(new PrintWriter(writer)).print(rootuser)
69
                }
70
        }
71
}
72

    
73

    
74

    
75

    
76
def addPermissions(def node, def type) {
77
        
78
        def subcorpusPerm = ["EditionPermission", "SpecificitiesPermission","IndexPermission","DictionnaryPermission",
79
                "TextSelectionPermission", "CreateSubcorpusPermission", "DeletePermission", "DimensionsPermission",
80
                "CreatePartitionPermission","DisplayPermission"]
81
        
82
        def partitionPerm = ["SpecificitiesPermission","IndexPermission","DictionnaryPermission",
83
                "AFCPermission", "DeletePermission", "DimensionsPermission","DisplayPermission"]
84
        
85
        def children = node.children();
86

    
87
        if (type == "partition") {
88
                for( def perm : partitionPerm) {
89
                        def newnode = new Node(null, "permission");
90
                        newnode.@anti = "false"
91
                        newnode.@value = perm
92
                        children.add(newnode);
93
                }
94
        } else if (type == "subcorpus") {
95
                def concnode = new Node(null, "permission");
96
                concnode.@anti = "false"
97
                concnode.@value = "ConcordancePermission"
98
                concnode.@leftContext = 9999
99
                concnode.@rightContext = 9999
100
                children.add(concnode);
101
                for( def perm : subcorpusPerm) {
102
                        def newnode = new Node(null, "permission");
103
                        newnode.@anti = "false"
104
                        newnode.@value = perm
105
                        children.add(newnode);
106
                }
107
        }
108
}
109

    
110
def getEntities(String prefix, def node) {
111
        def list = []
112

    
113
        String name = node.@name
114
        String tmp = prefix +"/"+name
115
        list << tmp
116

    
117
        corpusInfo[tmp] = node.name()
118

    
119
        for (def child : node.children()) {
120
                if (child.name().toString() == "partition" | child.name().toString() == "subcorpus") {
121
                        list.addAll(getEntities(tmp,child))
122
                }
123
        }
124
        return list
125
}