Rev 1786 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1576 | jmachado | 1 | /** |
2 | * Created by jorgemachado on 28/12/16. |
||
3 | */ |
||
4 | /** |
||
5 | * |
||
6 | * @param serviceName |
||
7 | * @param form |
||
8 | * @param handlerOk(responseObjJson) |
||
9 | * @param handlerFail(responseObjJson) |
||
10 | */ |
||
11 | function widgetCall(serviceName,form,handlerOk,handlerFail) |
||
12 | { |
||
1579 | jmachado | 13 | var data = $(form).serialize().replace(/%0D%0A/g, '%0A').replace(/\r?\n/g, "\r\n"); |
14 | |||
1686 | jmachado | 15 | widgetCallWithActionParameters($(form).attr("action"),serviceName,data,form,handlerOk,handlerFail); |
1579 | jmachado | 16 | } |
17 | |||
18 | /** |
||
19 | * Use parameters instead of serialize form |
||
20 | * @param serviceName |
||
21 | * @param parameters |
||
22 | * @param form |
||
23 | * @param handlerOk |
||
24 | * @param handlerFail |
||
25 | */ |
||
26 | function widgetCallWithParameters(serviceName,parameters,form,handlerOk,handlerFail) |
||
27 | { |
||
1686 | jmachado | 28 | widgetCallWithActionParameters($(form).attr("action"),serviceName,parameters,form,handlerOk,handlerFail); |
1579 | jmachado | 29 | } |
1675 | jmachado | 30 | |
1579 | jmachado | 31 | /** |
32 | * Created by jorgemachado on 28/12/16. |
||
33 | */ |
||
34 | /** |
||
35 | *Use parameters instead of serialize form and the action provided |
||
36 | * @param serviceName |
||
1586 | jmachado | 37 | * @param webMessagesContainer |
1579 | jmachado | 38 | * @param handlerOk(responseObjJson) |
39 | * @param handlerFail(responseObjJson) |
||
40 | */ |
||
1586 | jmachado | 41 | function widgetCallWithActionParameters(action,serviceName,parameters,webMessagesContainer,handlerOk,handlerFail) |
1579 | jmachado | 42 | { |
1586 | jmachado | 43 | $(webMessagesContainer).find(".web-messages").html('<div class="alert alert-info">Enviando ...</div>'); |
1576 | jmachado | 44 | |
1579 | jmachado | 45 | var data = parameters; |
1576 | jmachado | 46 | $.ajax({ |
1579 | jmachado | 47 | url: action + "?dispatch=executeService&serviceJson=" + serviceName, |
1576 | jmachado | 48 | type: "POST", |
49 | data: data, |
||
50 | dataType: "json", |
||
1686 | jmachado | 51 | statusCode: { |
52 | 401:function() { |
||
53 | $("#modalAuthentication").modal({ show : 'true'}); |
||
54 | } |
||
55 | }, |
||
1576 | jmachado | 56 | success: function(resposta) |
57 | { |
||
1579 | jmachado | 58 | |
1586 | jmachado | 59 | $(webMessagesContainer).find(".web-messages").html(""); |
1576 | jmachado | 60 | if(resposta.service == "error") |
61 | { |
||
1586 | jmachado | 62 | $(webMessagesContainer).find(".web-messages").html('<div class="alert alert-danger">' + resposta.exception + '</div>'); |
1711 | jmachado | 63 | if(handlerFail != undefined) |
1728 | jmachado | 64 | { |
1711 | jmachado | 65 | handlerFail(resposta.exception); |
1728 | jmachado | 66 | } |
1731 | jmachado | 67 | alert("Erro de chamada ao Serviço, veja os erros"); |
1711 | jmachado | 68 | |
1576 | jmachado | 69 | } |
70 | else if(resposta.service == "fail") |
||
71 | { |
||
72 | |||
73 | for(var msg in resposta.messages) |
||
74 | { |
||
1586 | jmachado | 75 | $(webMessagesContainer).find(".web-messages").append('<div class="alert alert-success">' + resposta.messages[msg] + '</div>'); |
1576 | jmachado | 76 | } |
77 | for(var error in resposta.errors) |
||
78 | { |
||
1586 | jmachado | 79 | $(webMessagesContainer).find(".web-messages").append('<div class="alert alert-warning">' + resposta.errors[error] + '</div>'); |
1576 | jmachado | 80 | } |
81 | if(handlerFail != undefined) |
||
1728 | jmachado | 82 | { |
1576 | jmachado | 83 | handlerFail(resposta.response); |
1728 | jmachado | 84 | } |
1731 | jmachado | 85 | alert("Erro de chamada ao Serviço, veja os erros"); |
1576 | jmachado | 86 | |
87 | } |
||
88 | else if(resposta.service == "ok") |
||
89 | { |
||
90 | for(var msg in resposta.messages) |
||
91 | { |
||
1586 | jmachado | 92 | $(webMessagesContainer).find(".web-messages").append('<div class="alert alert-success">' + resposta.messages[msg] + '</div>'); |
1576 | jmachado | 93 | } |
94 | if(handlerOk != undefined) |
||
95 | handlerOk(resposta.response); |
||
96 | } |
||
97 | }, |
||
1724 | jmachado | 98 | error: function(resposta) { |
1586 | jmachado | 99 | $(webMessagesContainer).find(".web-messages").html('<div class="alert alert-danger">Erro de comunicação, por favor tente novamente</div>'); |
1711 | jmachado | 100 | if(handlerFail != undefined) |
1728 | jmachado | 101 | { |
1711 | jmachado | 102 | handlerFail(resposta.exception); |
1728 | jmachado | 103 | } |
1731 | jmachado | 104 | alert("Erro de chamada ao Serviço, veja os erros"); |
1711 | jmachado | 105 | |
1576 | jmachado | 106 | } |
107 | }); |
||
108 | } |
||
109 | |||
1579 | jmachado | 110 | |
1586 | jmachado | 111 | function widgetSimpleCall(serviceName,form,handlerOk,handlerFail){ |
112 | var data = $(form).serialize().replace(/%0D%0A/g, '%0A').replace(/\r?\n/g, "\r\n"); |
||
1686 | jmachado | 113 | widgetSimpleCallWithActionParameters($(form).attr("action"),serviceName,data,form,handlerOk,handlerFail); |
1586 | jmachado | 114 | } |
1576 | jmachado | 115 | /** |
116 | * |
||
117 | * @param serviceName |
||
118 | * @param form |
||
119 | * @param handlerOk() |
||
120 | * @param handlerFail() |
||
121 | */ |
||
1586 | jmachado | 122 | function widgetSimpleCallWithActionParameters(action,serviceName,data,webMessagesContainer,handlerOk,handlerFail) |
1576 | jmachado | 123 | { |
1586 | jmachado | 124 | $(webMessagesContainer).find(".web-messages").html('<div class="alert alert-info">Enviando ...</div>'); |
1576 | jmachado | 125 | |
126 | $.ajax({ |
||
1586 | jmachado | 127 | url: action + "?dispatch=executeSimpleServiceOkFail&serviceJson=" + serviceName, |
1576 | jmachado | 128 | type: "POST", |
129 | data: data, |
||
130 | dataType: "json", |
||
1686 | jmachado | 131 | statusCode: { |
132 | 401:function() { |
||
133 | $('.modal').modal("hide"); |
||
134 | $("#modalAuthentication").modal({ show : 'true'}); |
||
135 | } |
||
136 | }, |
||
1576 | jmachado | 137 | success: function(resposta) |
138 | { |
||
1586 | jmachado | 139 | $(webMessagesContainer).find(".web-messages").html(""); |
1576 | jmachado | 140 | if(resposta.service == "error") |
141 | { |
||
1586 | jmachado | 142 | $(webMessagesContainer).find(".web-messages").html('<div class="alert alert-danger">' + resposta.exception + '</div>'); |
1711 | jmachado | 143 | if(handlerFail != undefined) |
1728 | jmachado | 144 | { |
1711 | jmachado | 145 | handlerFail(); |
1728 | jmachado | 146 | } |
1731 | jmachado | 147 | alert("Erro de chamada ao Serviço, veja os erros"); |
1576 | jmachado | 148 | } |
149 | else if(resposta.service == "fail") |
||
150 | { |
||
151 | |||
152 | for(var msg in resposta.messages) |
||
153 | { |
||
1586 | jmachado | 154 | $(webMessagesContainer).find(".web-messages").append('<div class="alert alert-success">' + resposta.messages[msg] + '</div>'); |
1576 | jmachado | 155 | } |
156 | for(var error in resposta.errors) |
||
157 | { |
||
1586 | jmachado | 158 | $(webMessagesContainer).find(".web-messages").append('<div class="alert alert-warning">' + resposta.errors[error] + '</div>'); |
1576 | jmachado | 159 | } |
160 | if(handlerFail != undefined) |
||
1728 | jmachado | 161 | { |
1576 | jmachado | 162 | handlerFail(); |
1728 | jmachado | 163 | } |
1731 | jmachado | 164 | alert("Erro de chamada ao Serviço, veja os erros"); |
1576 | jmachado | 165 | } |
166 | else if(resposta.service == "ok") |
||
167 | { |
||
168 | for(var msg in resposta.messages) |
||
169 | { |
||
1586 | jmachado | 170 | $(webMessagesContainer).find(".web-messages").append('<div class="alert alert-success">' + resposta.messages[msg] + '</div>'); |
1576 | jmachado | 171 | } |
1686 | jmachado | 172 | for(var error in resposta.errors) |
173 | { |
||
174 | $(webMessagesContainer).find(".web-messages").append('<div class="alert alert-warning">' + resposta.errors[error] + '</div>'); |
||
175 | } |
||
1576 | jmachado | 176 | if(handlerOk != undefined) |
177 | handlerOk(); |
||
178 | } |
||
179 | }, |
||
1686 | jmachado | 180 | error: function(resposta) |
181 | { |
||
1586 | jmachado | 182 | $(webMessagesContainer).find(".web-messages").html('<div class="alert alert-danger">Erro de comunicação, por favor tente novamente</div>'); |
1711 | jmachado | 183 | if(handlerFail != undefined) |
1728 | jmachado | 184 | { |
1724 | jmachado | 185 | handlerFail(resposta.exception); |
1728 | jmachado | 186 | } |
1731 | jmachado | 187 | alert("Erro de chamada ao Serviço, veja os erros"); |
1711 | jmachado | 188 | |
1576 | jmachado | 189 | } |
190 | }); |
||
1786 | jmachado | 191 | } |
192 | |||
193 | |||
194 | |||
195 | var BacoJS = (function () { |
||
196 | |||
197 | function orderKeys(obj, expected) { |
||
198 | var keys = Object.keys(obj).sort(function keyOrder(k1, k2) |
||
199 | { |
||
200 | if(k1 == "@class") |
||
201 | return -1; |
||
202 | else if(k2 == "@class") |
||
203 | return 1 |
||
204 | else |
||
205 | return 0; |
||
206 | //if (k1 < k2) return -1; |
||
207 | //else if (k1 > k2) return +1; |
||
208 | //else return 0; |
||
209 | }); |
||
210 | |||
211 | var i, after = {}; |
||
212 | for (i = 0; i < keys.length; i++) { |
||
213 | after[keys[i]] = obj[keys[i]]; |
||
214 | delete obj[keys[i]]; |
||
215 | } |
||
216 | |||
217 | for (i = 0; i < keys.length; i++) { |
||
218 | obj[keys[i]] = after[keys[i]]; |
||
219 | } |
||
220 | return obj; |
||
221 | } |
||
222 | function orderKeysRecur(obj, expected) |
||
223 | { |
||
224 | if(typeof obj == "object") |
||
225 | orderKeys(obj, expected); |
||
226 | for (var k in obj) |
||
227 | { |
||
228 | if (typeof obj[k] == "object" && obj[k] !== null) |
||
229 | orderKeysRecur(obj[k]); |
||
230 | } |
||
231 | } |
||
232 | |||
233 | return { |
||
234 | stringifyOrdered: function (jsonObj) { |
||
235 | orderKeysRecur(jsonObj) |
||
236 | //return JSON.stringify(jsonObj); |
||
237 | return angular.toJson(jsonObj); |
||
238 | } |
||
239 | }; |
||
240 | })(); |
||
241 | |||
1791 | jmachado | 242 | /*var exampleBacoJs = |
243 | { |
||
244 | "xpto" : 1, |
||
245 | "@class" : "pt.estgp.estgweb.domain.XptoSchool", |
||
246 | "zgt" : { |
||
247 | "zgt1" : 1, |
||
248 | "zgt2" : 2, |
||
249 | "@class" : "pt.estgp.estgweb.domain.ZgtDepartment" |
||
250 | } |
||
251 | }*/ |
||
252 | //does not affect angular JS lib |
||
253 | //you can use-it with angular |
||
254 | //var ordered = BacoJS.stringifyOrdered(exampleBacoJs); |
||
1786 | jmachado | 255 | //console.log(orderedExample); |
256 | |||
257 |