source: sandbox/expressoMail1_2/MailArchiver/2.2/expressoMail1_2/js/MAQueryConfig.js @ 5070

Revision 5070, 10.2 KB checked in by fernando-alberto, 13 years ago (diff)

Ticket #1269 - Desenvolvimento da nova solucao de arquivamento local MailArchiver?, ajuste paginacao

  • Property svn:eol-style set to native
Line 
1<!--
2/**
3 *MailArchiver Query Config API
4 *
5 *This api is intended to be used with MailArquiver JavaScript API.
6 *Basicly, it is responsable to provide a standard data format conversion
7 *to handle queryes under MailArquiver web services pooling. *
8 *
9 */
10
11
12//MailArchiver Query Config data structure
13var MAQueryConfig = function(){
14  this.lowerIndex = null;
15  this.upperIndex = null;
16  this.from = null;
17  this.to = null;
18  this.cc = null;
19  this.subject = null;
20  this.body = null;
21  this.date = null;
22  this.tag = null;
23  this.folder = null;
24  this.order = null;
25  this.preview_message = null;
26  this.preview_tooltip = null;
27  this.defaults = null;
28}
29
30//Set up all criteria list received
31MAQueryConfig.prototype.setCriteriaList = function(cl){
32  for(var p in cl){
33    if(p.toLowerCase() == 'from')
34      this.setFrom(cl[p]);
35    if(p.toLowerCase() == 'to')
36      this.setTo(cl[p]);
37    if(p.toLowerCase() == 'cc')
38      this.setCc(cl[p]);
39    if(p.toLowerCase() == 'subject')
40      this.setSubject(cl[p]);
41    if(p.toLowerCase() == 'body')
42      this.setBody(cl[p]);
43    if(p.toLowerCase() == 'date')
44      this.setDate(cl[p]);
45    if(p.toLowerCase() == 'flags')
46      this.setTag(cl[p]);
47    if(p.toLowerCase() == 'folder')
48      this.setFolder(cl[p]);
49    if(p.toLowerCase() == 'order')
50      this.setOrder(cl[p]);
51  }
52}
53
54MAQueryConfig.prototype.setDefaults = function(defaults){
55    this.defaults = defaults;
56}
57
58MAQueryConfig.prototype.setExpressoDefaults = function(data_default){
59    //Expresso defaults came from "messages_controller.js": here, we maps
60    //each array entry from messages_list intended to run by our own behavior
61    if(data_default.length != 7)
62        return;
63   //expresso default argument list => new Array(baseFolder,msg_range_begin,emails_per_page,sort_box_type,sort_box_reverse,preview_msg_subject,preview_msg_tip);
64   this.setFolder(data_default[0]);
65   //window.alert('vai setar defaults low e high com ' + data_default[1] +  ' - ' + data_default[2]);
66   this.setBounds(data_default[1], data_default[2]);
67   
68   if (parseInt(data_default[4]) == 0)
69       var oexpor = 'asc';
70   else
71       var oexpor = 'desc';
72   
73   switch(data_default[3].toLowerCase()){
74       case 'sortfrom':
75           var oexpcrt = 'from';
76           break;
77       case 'sortsubject':
78           var oexpcrt = 'subject';
79           break;
80       case 'sortsize':
81           var oexpcrt = 'size';
82           break;
83       default:
84           var oexpcrt = 'date';
85           break;
86   }
87
88   var oexporder = eval('({"' + oexpcrt + '":"' + oexpor + '"})');
89     
90   this.setOrder(oexporder);
91}
92
93//PharseCriteria receives data to pharse structure format fields from, to, cc and subject
94MAQueryConfig.prototype.pharseCriteria = function(criteria, data){
95  var stdout = '"'+criteria+'":[';
96  if(data.equals)
97    stdout += '{"@equals":"'+data.equals+'"},';
98  if(data.equalsic)
99    stdout += '{"@equalsIgnoreCase":"'+data.equalsic+'"},';
100  if(data.like)
101    stdout += '{"@like":"'+data.like+'"},';
102  if(data.likeic)
103    stdout += '{"@likeIgnoreCase":"'+data.likeic+'"},';
104  var stdout = stdout.substr(0,stdout.length-1) + '],';
105  return(stdout);
106}
107
108//setbounds up and down
109MAQueryConfig.prototype.setBounds = function(low, high){
110  //window.alert('setbounds low = ' + low + ' high = ' + high);
111  this.lowerIndex = '"@lowerIndex":"'+(parseInt(low)-1)+'",';
112  this.upperIndex = '"@upperIndex":"'+(parseInt(high)-1)+'",';
113}
114//From receives a array of structutred data, by the model
115MAQueryConfig.prototype.setFrom = function(data){
116  if(data.length <= 0){
117    this.from = null;
118    return;
119  }
120
121  subdata = this.pharseCriteria('from', data);
122  this.from = subdata;
123}
124
125//To receives a array of structutred data, by the model
126MAQueryConfig.prototype.setTo = function(data){
127  if(data.length <= 0){
128    this.to = null;
129    return;
130  }
131
132  subdata = this.pharseCriteria('to', data);
133  this.to = subdata;
134}
135
136//Cc data
137MAQueryConfig.prototype.setCc = function(data){
138  if(data.length <= 0){
139    this.cc = null;
140    return;
141  }
142
143  subdata = this.pharseCriteria('cc', data);
144  this.cc = subdata;
145}
146
147//Subject data
148MAQueryConfig.prototype.setSubject = function(sub){
149  if(sub.length <= 0){
150    this.subject = null;
151    return;
152  }
153
154  subdata = this.pharseCriteria('subject', sub);
155  this.subject = subdata;
156}
157
158//PREGMatch date format
159MAQueryConfig.prototype.validateDate = function (dat){
160 var dateregex = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
161 if (dateregex.test(dat))
162   return true;
163 else
164   return false;
165}
166
167//PREGMatch time format
168MAQueryConfig.prototype.validateTime = function (tim){
169  //var timeregex = /^([1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$/;
170  //var timeregex = /^[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$/;
171  var timeregex = /^\d{1,2}[:]\d{2}([:]\d{2})?$/;
172  if (timeregex.test(tim))
173    return true;
174  else
175    return false;
176}
177
178//toStdDate returns date time as long. Format input:'DD/MM/YYYY HH:MM:SS'
179MAQueryConfig.prototype.toStdDate = function(dt){
180  if (dt.length <= 0)
181    return;
182
183  var date_time_picker = dt.split(" ");
184  var date_part = date_time_picker[0];
185  var time_part = date_time_picker[1];
186
187  if ((this.validateDate(date_part)) && (this.validateTime(time_part))){
188    var date_split = date_part.split("/");
189    var ddd = date_split[1]+'/'+date_split[0]+'/'+date_split[2] + ' ' + time_part;
190    var ndate = new Date(ddd);   
191    return(Date.parse(ndate));
192  }
193  else
194    return("");
195}
196
197//Date data
198MAQueryConfig.prototype.setDate = function(date_data){
199  if((!date_data.upper) && (!date_data.lower) && (!date_data.day)){
200    this.date = null;
201    return;
202  }
203
204  this.date = '"date":['; 
205  if(date_data.lower){
206    var dt1b = this.toStdDate(date_data.lower);
207    this.date += '{"@lower":"'+dt1b+'"},';
208  }
209  if(date_data.upper){
210    var dt2b = this.toStdDate(date_data.upper);
211    this.date += '{"@upper":"'+dt2b+'"},';
212  }
213  if(date_data.day){
214    var dt3b = this.toStdDate(date_data.day)
215    this.date += '{"@day":"'+dt3b+'"},';
216  }
217  this.date = this.date.substr(0,this.date.length-1) + '],';
218}
219
220//flags data
221MAQueryConfig.prototype.setTag = function(flaglist){
222  if(flaglist.flags.length <= 0){
223    this.tag = null;
224    return;
225  }
226
227  this.tag = '"flag":[';
228  var flag_data = flaglist.flags.split(',');
229  if(flag_data.length > 0){
230    for(var k=0; k<flag_data.length; k++){
231      this.tag += '{"@value":"'+flag_data[k]+'"},'
232    }
233    this.tag = this.tag.substr(0,this.tag.length-1);
234  }
235  else{
236    this.tag += '{"@value":"'+flaglist.flags+'"},';
237  }
238  this.tag += '],';
239}
240
241//Folder data
242MAQueryConfig.prototype.setFolder = function(folderslist){
243  if(folderslist.length <= 0){
244    this.folder = null;
245    return;
246  }
247
248  this.folder = '"folder":[';
249  if (folderslist.indexOf(',') != -1){
250    var folder_data = folderslist.folder.split(',');
251    if(folder_data.length > 0){
252        for(var k=0; k<folder_data.length; k++){
253            this.folder += '{"@id":"'+folder_data[k]+'"},'
254        }
255        this.folder = this.folder.substr(0,this.folder.length-1);
256    }
257    else{
258        this.folder += '{"@id":"'+folderslist+'"}';
259    }
260  }
261  else{
262      this.folder += '{"@id":"' + folderslist + '"}';
263  }
264  this.folder += '],';
265}
266
267//Order criteria
268MAQueryConfig.prototype.setOrder = function(order){
269  if(typeof(order) != 'object'){
270    this.order = null;
271    return;
272  }
273
274  this.order = '"order":[';
275  for(var k in order){
276    if(k.toLowerCase().indexOf("date") != -1)
277      this.order += '{"@'+order[k]+'":"date"},';
278    if(k.toLowerCase().indexOf("from") != -1)
279      this.order += '{"@'+order[k]+'":"from"},';
280    if(k.toLowerCase().indexOf("subject") != -1)
281      this.order += '{"@'+order[k]+'":"subject"},';
282    if(k.toLowerCase().indexOf("size") != -1)
283      this.order += '{"@'+order[k]+'":"size"},';
284  }
285  this.order = this.order.substr(0,this.order.length-1) + ']';
286}
287
288//Body structrure
289MAQueryConfig.prototype.setBody = function(body_data){
290  if(body_data.length <= 0){
291    this.body = null;
292    return;
293  }
294  this.body = '"body":[';
295  for(var k in body_data){
296    if(k.toLowerCase() == "like")
297      this.body += '{"@like":"'+body_data[k]+'"},';
298    if(k.toLowerCase() == "likeic")
299      this.body += '{"@likeIgnoreCase":"'+body_data[k]+'"},';
300  }
301  this.body = this.body.substr(0,this.body.length-1) + '],';
302}
303
304MAQueryConfig.prototype.getFrom = function(){
305    (this.from != null) ? retdata = this.from : retdata = "";
306    return(retdata);
307}
308
309MAQueryConfig.prototype.getTo = function(){
310    (this.to != null) ? retdata = this.to : retdata = "";
311    return(retdata);   
312}
313
314MAQueryConfig.prototype.getCc = function(){
315    (this.cc != null) ? retdata = this.cc : retdata = "";
316    return(retdata);   
317}
318
319MAQueryConfig.prototype.getSubject = function(){
320    (this.subject != null) ? retdata = this.subject : retdata = "";
321    return(retdata);   
322}
323
324MAQueryConfig.prototype.getBody = function(){
325    (this.body != null) ? retdata = this.body : retdata = "";
326    return(retdata);   
327}
328
329MAQueryConfig.prototype.getDate = function(){
330    (this.date != null) ? retdata = this.date : retdata = "";
331    return(retdata);   
332}
333
334MAQueryConfig.prototype.getTag = function(){
335    (this.tag != null) ? retdata = this.tag : retdata = "";
336    return(retdata);   
337}
338
339MAQueryConfig.prototype.getFolder = function(){
340    (this.folder != null) ? retdata = this.folder : retdata = "";
341    return(retdata);   
342}
343
344MAQueryConfig.prototype.getOrder = function(){
345    (this.order != null) ? retdata = this.order : retdata = "";
346    return(retdata);   
347}
348
349//Query criteria (core api)
350MAQueryConfig.prototype.query = function(offsetlow,offsethight, criterialist){
351  //window.alert('MAquery low = ' + offsetlow + ' - hight = ' + offsethight);
352  if ((offsetlow) && (offsethight))
353    this.setBounds(offsetlow, offsethight);
354
355  if (criterialist)
356    if(criterialist.length > 0)
357        this.setCriteriaList(criterialist);
358  //window.alert('em qc com lower = ' + this.lowerIndex + ' e upper = ' + this.upperIndex);
359  var querystring = '{"query":{' + this.lowerIndex + this.upperIndex;
360  //window.alert('queryString init = ' + querystring);
361  querystring += this.getFrom();
362  querystring += this.getTo();
363  querystring += this.getCc();
364  querystring += this.getSubject();
365  querystring += this.getBody();
366  querystring += this.getDate();
367  querystring += this.getTag();
368  querystring += this.getFolder();
369  querystring += this.getOrder();
370  querystring += '}}';
371  return(querystring);
372}
373-->
Note: See TracBrowser for help on using the repository browser.