/**
* @fileOverview
* Copyright (c) 2013 Regione Autonoma della Sardegna
* Published under the GPL license.
* See https://sardegnageoportale.it/webgis/license.txt
* for the full text of the license.
* @version 0.1
*/
/**
* @namespace framework.data
*/
Ext.namespace("framework.data");
/**
* json reader for bonitaTasks of cruscotto idt of Regione Sardegna
* @name_ BonitaTasksReader
* @class json reader for bonitaTasks of cruscotto idt of Regione Sardegna
* @extends Ext.data.JsonReader - See Ext.data.JsonReader
* @constructor
* @param {object} meta object configuration
* @param {Ext.data.Record} recordType object definition
*/
framework.data.BonitaTasksReader = Ext.extend(Ext.data.JsonReader, {
/**
* @cfg {String} successProperty
* @hide
*/
/**
* @cfg {Number} id (optional) The subscript within row Array that provides an ID for the Record.
* Deprecated. Use {@link #idIndex} instead.
*/
/**
* @cfg {Number} idIndex (optional) The subscript within row Array that provides an ID for the Record.
*/
/**
* number of record on result.
* @public
* @type [Number] number of record on result.
*/
dimResul: 4,
/**
* object of groups for merge results.
* @public
* @type [Object] object of groups for merge results.
*/
vetGroups:null,
/**
* Create a data block containing Ext.data.Records from an Array.
* @param {Object} o An Array of row objects which represents the dataset.
* @return {Object} data A data block which is used by an Ext.data.Store object as
* a cache of Ext.data.Records.
*/
readRecords : function(o){
if(!o.remove){
o = Ext.util.JSON.decode(o);
}
if(!o.remove){
o = Ext.util.JSON.decode(o);
}
this.arrayData = o;
this.vetGroups = this.meta.vetGroups;
var s = this.meta,
sid = s ? Ext.num(s.idIndex, s.id) : null,
recordType = this.recordType,
fields = recordType.prototype.fields,
vetGroups = this.meta.vetGroups,
records = [],
success = true,
v;
var root = this.getRoot(o);
var cleanVet = {};//oggetto con tutte le istanze dei dati aggregati. Utilizzato per facilitare le ricerche.
var array = []; //array dei risultati finali.
//inizializzo il vettore degli aggregati e vettore risultati
for(var j in vetGroups){
cleanVet[j]={
typeFull:j,
type:(j.length > 8? j.substr(0,6) + '..':j),
total:0,
items:[]
};
}
var found;
var notFound = [];
for(var i=0;i< root.length;i++){
found = false;
for(var j in vetGroups){
if(vetGroups[j][root[i].name]){
cleanVet[j].items.push(root[i]);
cleanVet[j].total = cleanVet[j].total + 1;
found = true;
if(array.indexOf(cleanVet[j]) == -1)
array.push(cleanVet[j]);
}
}
if(found == false){
//dato di test per vedere se rimangono elementi fuori dagli aggregati
notFound.push(root[i].name);
}
}
array.sort(function(a, b){return b.total - a.total});
if(array.length > this.dimResul){
var dimAltri = 0;
for(var i = this.dimResul - 1; i < array.length; i++) {
dimAltri = dimAltri + array[i].total;
}
array.splice(this.dimResul - 1,array.length);
array.push({type:'Altri',typeFull:'Altri',total:dimAltri});
}
root = array;
for(var i = 0, len = root.length; i < len; i++) {
var n = root[i],
values = {},
id = ((sid || sid === 0) && n[sid] !== undefined && n[sid] !== "" ? n[sid] : null);
for(var j = 0, jlen = fields.length; j < jlen; j++) {
var f = fields.items[j],
k = f.mapping !== undefined && f.mapping !== null ? f.mapping : j;
v = n[k] !== undefined ? n[k] : f.defaultValue;
v = f.convert(v, n);
values[f.name] = v;
}
var record = new recordType(values, id);
record.json = n;
records[records.length] = record;
}
var totalRecords = records.length;
if(s.totalProperty) {
v = parseInt(this.getTotal(o), 10);
if(!isNaN(v)) {
totalRecords = v;
}
}
if(s.successProperty){
v = this.getSuccess(o);
if(v === false || v === 'false'){
success = false;
}
}
return {
success : success,
records : records,
totalRecords : totalRecords
};
}
});