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 count layers inside a set of mapServers
 17 * @name_ MapServerReader
 18 * @class json reader for count layers inside a set of mapServers
 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.MapServerReader = 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    /**
 47      * This method is only used by a DataProxy which has retrieved data from a remote server.
 48      * @param {Object} response The XHR object which contains the JSON data in its responseText.
 49      * @return {Object} data A data block which is used by an Ext.data.Store object as
 50      * a cache of Ext.data.Records.
 51      */
 52     read : function(response){
 53         var json = response.responseText;
 54         var o = Ext.decode(json);
 55         if(!o) {
 56             throw {message: 'JsonReader.read: Json object not found'};
 57         }
 58         return this.prepareRecords(o);
 59     },
 60     
 61     /**
 62      * Create a data block containing Ext.data.Records from an Array.
 63      * @param {Object} o An Array of row objects which represents the dataset.
 64      * @return {Object} data A data block which is used by an Ext.data.Store object as
 65      * a cache of Ext.data.Records.
 66      */
 67     prepareRecords : function(o){
 68         this.arrayData = o;
 69         this.proxy = this.meta.proxy;
 70 
 71         var root = this.getRoot(o);
 72         this.elemToProcess = root.length;
 73         this.array = [];
 74         for(var i in root){
 75             if(root[i].mapserver == true){
 76                 var elem = {type:(root[i].nome.length > 6? root[i].nome.substr(0,5) + '..':root[i].nome),typeFull:root[i].nome,total:0}
 77                 this.array.push(elem);
 78                 this.getTotalCount(elem,root[i].url);
 79             }
 80         }
 81     },
 82     
 83     getTotalCount : function(obj,url){
 84         if(url != null){
 85            Ext.Ajax.request({
 86             url: (this.proxy?this.proxy:'') + url,
 87             success: function(response) {
 88                 var count = 0;
 89                 if (response.responseText) {
 90                     var xml = response.responseText;
 91                     console.log(this.proxy);
 92                     var index = xml.indexOf('</Layer>');
 93                     while(index > -1){
 94                         xml = xml.substr(index + 8);
 95                         index = xml.indexOf('</Layer>');
 96                         count = count +1;
 97                     }
 98                 }  
 99                 obj.total = count > 0? count -1 : 0;
100                 this.elemToProcess = this.elemToProcess -1;
101                 if(this.elemToProcess == 0)
102                     this.readRecords(this.arrayData);
103                 
104             },
105             failure: function(){
106                 obj.total = 0;
107                 this.elemToProcess = this.elemToProcess -1;
108                 if(this.elemToProcess == 0)
109                     createStore();
110                 alert('Errore!!!');
111             },scope:this
112          }); 
113             
114             
115         }
116             
117     },
118     
119     readRecords : function(o){
120         console.log('Start createStore');
121         var s = this.meta,
122             array = this.array,
123             sid = s ? Ext.num(s.idIndex, s.id) : null,
124             recordType = this.recordType,
125             fields = recordType.prototype.fields,
126             records = [],
127             success = true,
128             v;
129     
130             array.sort(function(a, b){return b.total - a.total});
131         if(array.length > this.dimResul){
132             var dimAltri = 0;
133             for(var i = this.dimResul - 2; i < array.length; i++) {
134                 dimAltri = dimAltri + array[i].total;
135             }
136             array.splice(this.dimResul - 1,array.length);
137             array.push({type:'Altri',typeFull:'Altri',total:dimAltri});
138             
139         }
140         var root = array;
141         
142         console.log('ordered createStore');
143 //*****************************************************************************************
144         for(var i = 0, len = root.length; i < len; i++) {
145             var n = root[i],
146                 values = {},
147                 id = ((sid || sid === 0) && n[sid] !== undefined && n[sid] !== "" ? n[sid] : null);
148             for(var j = 0, jlen = fields.length; j < jlen; j++) {
149                 var f = fields.items[j],
150                     k = f.mapping !== undefined && f.mapping !== null ? f.mapping : j;
151                 v = n[k] !== undefined ? n[k] : f.defaultValue;
152                 v = f.convert(v, n);
153                 values[f.name] = v;
154             }
155             var record = new recordType(values, id);
156             record.json = n;
157             records[records.length] = record;
158         }
159 
160         var totalRecords = records.length;
161 
162         if(s.totalProperty) {
163             v = parseInt(this.getTotal(o), 10);
164             if(!isNaN(v)) {
165                 totalRecords = v;
166             }
167         }
168         if(s.successProperty){
169             v = this.getSuccess(o);
170             if(v === false || v === 'false'){
171                 success = false;
172             }
173         }
174         console.log('end createStore');
175         return {
176             success : success,
177             records : records,
178             totalRecords : totalRecords
179         };
180         
181     }
182 });
183 
184