source: trunk/src/ajax/javascripts/tmcl_tools.js

Last change on this file was 427, checked in by lgiessmann, 14 years ago

JSON-Interface: all / that are not escaped will be escaped after calling prototypes toJSON method, because prototype does not escape /; if no topics for a player-constraint or other-player-constraint exist there is no error message thrown, instead the constraint is ignored as long as there are to few topics; the backend now escapes all /, too

File size: 9.1 KB
Line 
1//+-----------------------------------------------------------------------------
2//+  Isidorus
3//+  (c) 2008-2010 Marc Kuester, Christoph Ludwig, Lukas Georgieff
4//+
5//+  Isidorus is freely distributable under the LLGPL 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/LLGPL-LICENSE.txt, and
9//+  trunk/docs/LGPL-LICENSE.txt in
10//+  trunk/src/ajax/javascripts/external/MIT-LICENSE.txt.
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        if(!anyConstraints[i].players) return players;
167        for(var j = 0; j !== anyConstraints[i].players.length; ++j){
168            players.push(anyConstraints[i].players[j])
169        }
170    }
171
172    return players;
173}
174
175
176// --- Returns an array of players where the players from playersToClean will
177// --- be deleted from allPlayers.
178function cleanPlayers(allPlayers, playersToClean){
179    var cleanedPlayers = new Array();
180    if(!allPlayers) return cleanedPlayers;
181    if(!playersToClean) return allPlayers;
182
183    for(var i = 0; i !== allPlayers.length; ++i){
184        var toDel = false;
185        for(var j = 0; j !== allPlayers[i].length; ++j){
186            for(var k = 0; k !== playersToClean.length; ++k){
187                if(playersToClean[k].indexOf(allPlayers[i][j]) !== -1){
188                    toDel = true;
189                    break;
190                }
191            }
192            if(toDel === true) break;
193        }
194        if(toDel === false) cleanedPlayers.push(allPlayers[i]);
195    }
196    return cleanedPlayers;
197}
198
199
200// --- Ads the new created topic as a player to all association constraints
201// --- where the player-type is one of the topic's instanceOf-topics.
202function addTopicAsPlayer(associationsConstraints, topicInstanceOfs){
203    if(!associationsConstraints || associationsConstraints.length === 0 || !topicInstanceOfs || topicInstanceOfs.length === 0) return;
204    var instanceOfsPsis = topicInstanceOfs.flatten();
205    for(var i = 0; i !== associationsConstraints.length; ++i){
206        // --- associationrole-constraints
207        if(associationsConstraints[i].rolePlayerConstraints){
208            rpcs = associationsConstraints[i].rolePlayerConstraints;
209            for(var j = 0; j !== rpcs.length; ++j){
210                for(var k = 0; k !== rpcs[j].playerType.length; ++k){
211                    for(var l = 0; l !== rpcs[j].playerType[k].length; ++l){
212                        if(instanceOfsPsis.indexOf(rpcs[j].playerType[k][l]) !== -1){
213                            if(!rpcs[j].players) rpcs[j].players = new Array();
214                            rpcs[j].players.push(new Array(CURRENT_TOPIC));
215                            break;
216                        }
217                    }
218                }
219            }
220        }
221
222        // --- otherrole-constraints
223        if(associationsConstraints[i].otherRoleConstraints){
224            orcs = associationsConstraints[i].otherRoleConstraints;
225            for(var j = 0; j !== orcs.length; ++j){
226                for(var k = 0; k !== orcs[j].playerType.length; ++k){
227                    for(var l = 0; l !== orcs[j].playerType[k].length; ++l){
228                        if(instanceOfsPsis.indexOf(orcs[j].playerType[k][l]) !== -1){
229                            if(!orcs[j].players) orcs[j].players = new Array();
230                            orcs[j].players.push(new Array(CURRENT_TOPIC));
231                            break;
232                        }
233                    }
234                }
235                for(var k = 0; k !== orcs[j].otherPlayerType.length; ++k){
236                    for(var l = 0; l !== orcs[j].otherPlayerType[k].length; ++l){
237                        if(instanceOfsPsis.indexOf(orcs[j].otherPlayerType[k][l]) !== -1){
238                            if (!orcs[j].otherPlayers) orcs[j].otherPlayers = new Array();
239                            orcs[j].otherPlayers.push(new Array(CURRENT_TOPIC));
240                            break;
241                        }
242                    }
243                }
244            }
245        }
246    }
247}
Note: See TracBrowser for help on using the repository browser.