1 /**
  2 * @fileOverview  
  3 * Copyright (c) 2013 Regione Autonoma della Sardegna
  4 * Published under the GPL license.<br>
  5 * See <a href="https://sardegnageoportale.it/webgis/license.txt">https://sardegnageoportale.it/webgis/license.txt</a>
  6 * for the full text of the license.
  7 * @version 0.1
  8 */
  9 
 10 /**
 11  * @namespace framework.data
 12  */
 13 Ext.namespace("framework.data");
 14 
 15 /**
 16 * json reader for bonitaTasks of cruscotto idt  of Regione Sardegna
 17 * @name_ BonitaTasksReader
 18 * @class json reader for bonitaTasks of cruscotto idt  of Regione Sardegna
 19 * @extends Ext.data.JsonReader - See <a href="http://dev.sencha.com/deploy/ext-3.3.1/docs?class=Ext.data.JsonReader">Ext.data.JsonReader</a>
 20 * @constructor
 21 * @param {object} meta object configuration 
 22 * @param {Ext.data.Record} recordType object definition
 23 */
 24 
 25 framework.data.BonitaTasksReader = Ext.extend(Ext.data.JsonReader, {
 26     /**
 27      * @cfg {String} successProperty
 28      * @hide
 29      */
 30     /**
 31      * @cfg {Number} id (optional) The subscript within row Array that provides an ID for the Record.
 32      * Deprecated. Use {@link #idIndex} instead.
 33      */
 34     /**
 35      * @cfg {Number} idIndex (optional) The subscript within row Array that provides an ID for the Record.
 36      */
 37     
 38     
 39     /**
 40     * number of record on result.
 41     * @public
 42     * @type [Number] number of record on result.
 43     */                    
 44    dimResul: 4,
 45    /**
 46     * object of groups for merge results.
 47     * @public
 48     * @type [Object] object of groups for merge results.
 49     */  
 50    vetGroups:null,
 51     
 52     
 53     /**
 54      * Create a data block containing Ext.data.Records from an Array.
 55      * @param {Object} o An Array of row objects which represents the dataset.
 56      * @return {Object} data A data block which is used by an Ext.data.Store object as
 57      * a cache of Ext.data.Records.
 58      */
 59     readRecords : function(o){
 60         if(!o.remove){
 61             o = Ext.util.JSON.decode(o);
 62         }
 63         if(!o.remove){
 64             o = Ext.util.JSON.decode(o);
 65         }
 66         this.arrayData = o;
 67         this.vetGroups = this.meta.vetGroups;
 68         var s = this.meta,
 69             sid = s ? Ext.num(s.idIndex, s.id) : null,
 70             recordType = this.recordType,
 71             fields = recordType.prototype.fields,
 72             vetGroups = this.meta.vetGroups,
 73             records = [],
 74             success = true,
 75             v;
 76 
 77         var root = this.getRoot(o);
 78         
 79         var cleanVet = {};//oggetto con tutte le istanze dei dati aggregati. Utilizzato per facilitare le ricerche.
 80         var array = []; //array dei risultati finali.
 81         //inizializzo il vettore degli aggregati e vettore risultati
 82         for(var j in vetGroups){
 83             cleanVet[j]={
 84                 typeFull:j,
 85                 type:(j.length > 8? j.substr(0,6) + '..':j),
 86                 total:0,
 87                 items:[]
 88             };
 89         }
 90         var found;
 91         var notFound = [];
 92         for(var i=0;i< root.length;i++){
 93 	    found = false;
 94             for(var j in vetGroups){
 95                 if(vetGroups[j][root[i].name]){
 96                     cleanVet[j].items.push(root[i]);
 97                     cleanVet[j].total = cleanVet[j].total + 1;
 98 		    found = true;
 99 		    if(array.indexOf(cleanVet[j]) == -1)
100 			array.push(cleanVet[j]);
101                 }
102             }
103 	    if(found == false){
104                 //dato di test per vedere se rimangono elementi fuori dagli aggregati
105 		notFound.push(root[i].name);
106 	    }
107         }
108         
109         array.sort(function(a, b){return b.total - a.total});
110         if(array.length > this.dimResul){
111             var dimAltri = 0;
112             for(var i = this.dimResul - 1; i < array.length; i++) {
113                 dimAltri = dimAltri + array[i].total;
114             }
115             array.splice(this.dimResul - 1,array.length);
116             array.push({type:'Altri',typeFull:'Altri',total:dimAltri});
117             
118         }
119         root = array;
120 
121         for(var i = 0, len = root.length; i < len; i++) {
122             var n = root[i],
123                 values = {},
124                 id = ((sid || sid === 0) && n[sid] !== undefined && n[sid] !== "" ? n[sid] : null);
125             for(var j = 0, jlen = fields.length; j < jlen; j++) {
126                 var f = fields.items[j],
127                     k = f.mapping !== undefined && f.mapping !== null ? f.mapping : j;
128                 v = n[k] !== undefined ? n[k] : f.defaultValue;
129                 v = f.convert(v, n);
130                 values[f.name] = v;
131             }
132             var record = new recordType(values, id);
133             record.json = n;
134             records[records.length] = record;
135         }
136 
137         var totalRecords = records.length;
138 
139         if(s.totalProperty) {
140             v = parseInt(this.getTotal(o), 10);
141             if(!isNaN(v)) {
142                 totalRecords = v;
143             }
144         }
145         if(s.successProperty){
146             v = this.getSuccess(o);
147             if(v === false || v === 'false'){
148                 success = false;
149             }
150         }
151 
152         return {
153             success : success,
154             records : records,
155             totalRecords : totalRecords
156         };
157     }
158 });
159