source: branches/new-datamodel/src/ajax/javascripts/tmcl_tools.js

Last change on this file was 62, checked in by lgiessmann, 15 years ago

ajax-cient: fixed a bug with remove-buttons in role-frames created from otherrole-constraints; added the functionality of creating role-frames from existing content - so there will be all existing roles selected as default-value - if there exist any constraints for the content

File size: 8.9 KB
Line 
1//+-----------------------------------------------------------------------------
2//+  Isidorus
3//+  (c) 2008-2009 Marc Kuester, Christoph Ludwig, Lukas Giessmann
4//+
5//+  Isidorus is freely distributable under the LGPL license.
6//+  This ajax module uses the frameworks PrototypeJs and Scriptaculous, both
7//+  are distributed under the MIT license.
8//+  You can find a detailed description in trunk/docs/LGPL-LICENSE.txt and
9//+  in trunk/src/ajax/javascripts/external/MIT-LICENSE.txt.
10//+-----------------------------------------------------------------------------
11
12
13// --- Returns an Array of the type [<boolean>, <string>].
14// --- If there are exclusive-instance-constraints, the return value is an array
15// --- of the form [false, "message"] otherwise [true, ""].
16function checkExclusiveInstances(constraints, psis){
17    try{
18        var exc = constraints.topicConstraints.exclusiveInstances;
19        var ret = new Array();
20        for(var i = 0; i !== psis.length; ++i){
21            var currentArray = new Array(psis[i]);
22            for(var j = 0; j !== exc.length; ++j){
23                for(var k = 0; k !== exc[j].exclusives.length; ++k){
24                    for(var l = 0; l !== exc[j].exclusives[k].length; ++l){
25                        if(exc[j].exclusives[k][l] === psis[i]){
26                            for(var m = 0; m != exc[j].owner.length; ++m){
27                                currentArray.push(exc[j].owner[m]);
28                            }
29                            break;
30                        }
31                    }
32                }
33            }
34
35            if(currentArray.length > 1)ret.push(currentArray);
36        }
37        if(ret.length === 0) return null;
38        return ret;
39    }
40    catch(err){
41        return null;
42    }
43}
44
45
46// --- checks SubjectLocator and SubjectIdentifier contraints and contents
47function checkIdentifierConstraints(contents, constraints)
48{
49    var innerConstents = (!contents ? new Array() : contents);
50    if((!constraints || constraints.length === 0) && innerConstraints.length === 0) return false;
51
52    for(var i = 0; i != constraints.length; ++i){
53        var regexp = constraints[i].regexp;
54        var min = constraints[i].cardMin;
55        var max = constraints[i].cardMax;
56
57        var foundContents = 0;
58        for(var j = 0; j != innerContents.length; ++j){
59            var rex = new RegExp(regexp);
60            if(rex.match(innerContents[j]) === true) ++foundContents;
61        }
62
63        if(foundContents < min || foundContents > max) return false;
64    }
65    return true;
66}
67
68
69// --- Returns an array of rolePlayerConstraints belonging to the given type in roleType.
70// --- roleType is of the form [["psi-1", "psi-2", "..."], ["sub-psi-1", "..."], <...>]
71function getRolePlayerConstraintsForRole(roleType, rolePlayerConstraints){
72    if(!roleType || roleType.length === 0 || !rolePlayerConstraints || rolePlayerConstraints.length === 0) return new Array();
73   
74    var foundConstraints = new Array();
75    var allRoleTypes = roleType.flatten();
76    for(var i = 0; i !== rolePlayerConstraints.length; ++i){
77        var allCRoleTypes = rolePlayerConstraints[i].roleType.flatten();
78        for(var j = 0; j !== allRoleTypes.length; ++j){
79            if(allCRoleTypes.indexOf(allRoleTypes[j]) !== -1){
80                foundConstraints.push(rolePlayerConstraints[i]);
81                break;
82            }
83        }
84    }
85    return foundConstraints.uniq();
86}
87
88
89// --- Returns an array of otherRoleConstraints belonging to the given roleType and players.
90// --- roleType is of the form [["psi-1", "psi-2", "..."], ["sub-psi-1", "..."], <...>]
91// --- players is of the form [["t1-psi-1", "t1-psi-2", "..."], ["t2-psi-1", "..."], <...>]
92function getOtherRoleConstraintsForRole(roleType, players, otherRoleConstraints){
93    if(!roleType || roleType.length === 0 || !players || players.length === 0 || !otherRoleConstraints || otherRoleConstraints.length === 0) return new Array();
94       
95    var foundConstraints = new Array();
96    var allRoleTypes = roleType.flatten();
97    var allPlayers = players.flatten();
98    for(var i = 0; i !== otherRoleConstraints.length; ++i){
99        var roleTypeMatches = false;
100        var allCRoleTypes = otherRoleConstraints[i].roleType.flatten();
101        for(var j = 0; j !== allPlayers.length; ++j){
102            if(allCRoleTypes.indexOf(allRoleTypes[j]) !== -1){
103                var allCPlayers = otherRoleConstraints[i].players.flatten();
104                for(var k = 0; k !== allPlayers.length; ++k){
105                    if(allCPlayers.indexOf(allPlayers[k]) !== -1){
106                        foundConstraints.push(otherRoleConstraints[i]);
107                        break;
108                    }
109                }
110                break;
111            }
112        }
113    }
114    return foundConstraints;
115}
116
117
118// --- Returns the sum of all cardMin values of all rolePlayerConstraints.
119function getRolePlayerMinForRole(anyRoleConstraints){
120    if(!anyRoleConstraints || anyRoleConstraints === 0) return 0;
121    var min = 0;
122    for(var i = 0; i !== anyRoleConstraints.length; ++i){
123        min += parseInt(anyRoleConstraints[i].cardMin);
124    }
125    return min;
126}
127
128
129// --- Returns the sum of all cardMax values of all rolePlayerConstraints.
130function getRolePlayerMaxForRole(anyRoleConstraints){
131    if(!anyRoleConstraints || anyRoleConstraints === 0) return 0;
132    var max = 0;
133    for(var i = 0; i !== anyRoleConstraints.length; ++i){
134        if(anyRoleConstraints[i].cardMax === "MAX_INT") return "*";
135        else max += parseInt(anyRoleConstraints[i].cardMax);
136    }
137    return max;
138}
139
140
141// --- checks the cardinalities of all rolePlayerconstraints depending on a
142// --- given associationRoleConstraint
143function checkCardinalitiesARC_RPC(associationRoleConstraint, rolePlayerConstraints){
144    if(!associationRoleConstraint) throw "From checkCardinalitiesARC(): associationRoleConstraint is not set!";
145    if(!rolePlayerConstraints || rolePlayerConstraints.length === 0) throw "From checkCardinalitiesARC(): rolePlayerConstraints is not set!";
146    var arMin = parseInt(associationRoleConstraint.cardMin);
147    var arMax = associationRoleConstraint.cardMax === "MAX_INT" ? "*" : parseInt(associationRoleConstraint.cardMax);
148    var rpcs = getRolePlayerConstraintsForRole(associationRoleConstraint.roleType, rolePlayerConstraints);
149    var rpMin = getRolePlayerMinForRole(rpcs);
150    var rpMax = getRolePlayerMaxForRole(rpcs);
151    var type = associationRoleConstraint.roleType.flatten()[0];
152
153    if(rpMax !== "*" && rpMax < arMin) throw "Not enough players available for roletype \"" + type + "\" (rpMax=" + rpMax + ", arMin=" + arMin + ")";
154    if(arMax !== "*" && rpMin > arMax) throw "Too much players for the roletype \"" + type + "\" (rpMin=" + rpMin + ", arMax=" + arMax + ")";
155}
156
157
158// --- Returns all listed players of a constraint of the type
159// --- roleplayer-constraint or otherrole-constraint and returns them
160// --- as an array.
161function extractPlayersOfConstraints(anyConstraints){
162    var players = new Array();
163    if(!anyConstraints || anyConstraints.length === 0) return players;
164
165    for(var i = 0; i !== anyConstraints.length; ++i){
166        for(var j = 0; j !== anyConstraints[i].players.length; ++j){
167            players.push(anyConstraints[i].players[j])
168        }
169    }
170
171    return players;
172}
173
174
175// --- Returns an array of players where the players from playersToClean will
176// --- be deleted from allPlayers.
177function cleanPlayers(allPlayers, playersToClean){
178    var cleanedPlayers = new Array();
179    if(!allPlayers) return cleanedPlayers;
180    if(!playersToClean) return allPlayers;
181
182    for(var i = 0; i !== allPlayers.length; ++i){
183        var toDel = false;
184        for(var j = 0; j !== allPlayers[i].length; ++j){
185            for(var k = 0; k !== playersToClean.length; ++k){
186                if(playersToClean[k].indexOf(allPlayers[i][j]) !== -1){
187                    toDel = true;
188                    break;
189                }
190            }
191            if(toDel === true) break;
192        }
193        if(toDel === false) cleanedPlayers.push(allPlayers[i]);
194    }
195    return cleanedPlayers;
196}
197
198
199// --- Ads the new created topic as a player to all association constraints
200// --- where the player-type is one of the topic's instanceOf-topics.
201function addTopicAsPlayer(associationsConstraints, topicInstanceOfs){
202    if(!associationsConstraints || associationsConstraints.length === 0 || !topicInstanceOfs || topicInstanceOfs.length === 0) return;
203    var instanceOfsPsis = topicInstanceOfs.flatten();
204    for(var i = 0; i !== associationsConstraints.length; ++i){
205        // --- associationrole-constraints
206        if(associationsConstraints[i].rolePlayerConstraints){
207            rpcs = associationsConstraints[i].rolePlayerConstraints;
208            for(var j = 0; j !== rpcs.length; ++j){
209                for(var k = 0; k !== rpcs[j].playerType.length; ++k){
210                    for(var l = 0; l !== rpcs[j].playerType[k].length; ++l){
211                        if(instanceOfsPsis.indexOf(rpcs[j].playerType[k][l]) !== -1){
212                            rpcs[j].players.push(new Array(CURRENT_TOPIC));
213                            break;
214                        }
215                    }
216                }
217            }
218        }
219
220        // --- otherrole-constraints
221        if(associationsConstraints[i].otherRoleConstraints){
222            orcs = associationsConstraints[i].otherRoleConstraints;
223            for(var j = 0; j !== orcs.length; ++j){
224                for(var k = 0; k !== orcs[j].playerType.length; ++k){
225                    for(var l = 0; l !== orcs[j].playerType[k].length; ++l){
226                        if(instanceOfsPsis.indexOf(orcs[j].playerType[k][l]) !== -1){
227                            orcs[j].players.push(new Array(CURRENT_TOPIC));
228                            break;
229                        }
230                    }
231                }
232                for(var k = 0; k !== orcs[j].otherPlayerType.length; ++k){
233                    for(var l = 0; l !== orcs[j].otherPlayerType[k].length; ++l){
234                        if(instanceOfsPsis.indexOf(orcs[j].otherPlayerType[k][l]) !== -1){
235                            orcs[j].otherPlayers.push(new Array(CURRENT_TOPIC));
236                            break;
237                        }
238                    }
239                }
240            }
241        }
242    }
243}
Note: See TracBrowser for help on using the repository browser.