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  * This program is free software: you can redistribute it and/or modify
 12  * it under the terms of the GNU General Public License as published by
 13  * the Free Software Foundation, either version 3 of the License, or
 14  * (at your option) any later version.
 15  *
 16  * This program is distributed in the hope that it will be useful,
 17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 19  * GNU General Public License for more details.
 20  *
 21  * You should have received a copy of the GNU General Public License
 22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 23  */
 24 
 25 /**
 26  * @namespace framework.data
 27  */
 28 Ext.namespace("framework.data");
 29 
 30 /**
 31 * XSL reader for toponimi search service of Regione Sardegna
 32 * @name_ DirectoryOpenLS_XLSReader
 33 * @class XSL reader for toponimi search service of Regione Sardegna
 34 * @extends Ext.data.XmlReader - See <a href="http://dev.sencha.com/deploy/ext-3.3.1/docs?class=Ext.data.XmlReader">Ext.data.XmlReader</a>
 35 * @constructor
 36 * @param {object} meta object configuration 
 37 * @param {Ext.data.Record} recordType object definition
 38 */
 39 framework.data.DirectoryOpenLS_XLSReader = function(meta, recordType) {
 40     meta = meta || {};
 41 
 42 
 43     Ext.applyIf(meta, {
 44         idProperty: meta.idProperty || meta.idPath || meta.id,
 45         successProperty: meta.successProperty || meta.success
 46     });
 47 
 48     framework.data.DirectoryOpenLS_XLSReader.superclass.constructor.call(this, meta, recordType || meta.fields);
 49 };
 50 
 51 Ext.extend(framework.data.DirectoryOpenLS_XLSReader, Ext.data.XmlReader, 
 52 /**
 53  * @lends framework.data.DirectoryOpenLS_XLSReader.prototype
 54 */
 55 {
 56     
 57     /** This method is only used by a DataProxy which has retrieved data from a remote server. 
 58     * Create responseXML if the responseText is a XML format
 59     * @public
 60     * @param {Object} response The XHR object which contains the parsed XML document. 
 61     * The response is expected to contain a method called 'responseXML' that returns an XML document object.
 62     * @returns {Object} records A data block which is used by an {@link Ext.data.Store} as
 63     * a cache of Ext.data.Records.
 64     */  
 65      read : function(response){
 66         if(!response.responseXML){
 67             var text = response.responseText;
 68             try { // code for IE
 69                 var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
 70                 xmlDoc.async = "false";
 71                 xmlDoc.loadXML(text);
 72                 response.responseXML = xmlDoc;
 73             }catch(error) { // code for Mozilla, Firefox, Opera, etc.
 74                 try {
 75                     var parser = new DOMParser();
 76                     var xmlDoc = parser.parseFromString(text,"text/xml");
 77                     response.responseXML = xmlDoc;
 78                 }catch(error) {
 79                     alert(error.message);
 80                     return;
 81                 }
 82             }
 83         }
 84         var doc = response.responseXML;
 85         if(!doc) {
 86             throw {message: "XmlReader.read: XML Document not available"};
 87         }
 88         return this.readRecords(doc);
 89     },
 90 
 91     
 92     /** get 'Text' info from a xml segment
 93     * @public
 94     * @param {string}format xml reader and writer
 95     * @param {string} text html text
 96     * @param {string} node xml node
 97     * @param {string} tagname tag from where extract data
 98     * @param {string} sep separator character
 99     * @returns {object} html text
100     */  
101     addOptXlsText: function(format, text, node, tagname, sep) {
102         var elms = format.getElementsByTagNameNS(node, "http://www.opengis.net/xls", tagname);
103         if (elms) {
104             Ext.each(elms, function(elm, index) {
105                 var str = elm.getAttribute('value');
106                 if (tagname === "POIInfo") {
107                     var attvaluename = elm.getAttribute('name');
108                     if (attvaluename === 'Alias') {
109                         text = text + sep + '<b>' + str + '</b>';
110                     }
111                     else if (attvaluename === 'NomeComune') {
112                         text = text + sep + ' (' + str + ')';
113                     }
114                 }
115             });
116         }
117 
118         return text;
119     },
120             
121     /** get 'ID' info from a xml segment
122     * @public
123     * @param {string}format xml reader and writer
124     * @param {number} numresult number of search result
125     * @param {string} node xml node
126     * @param {string} tagname tag from where extract data
127     * @param {string} sep separator character
128     * @returns {number} number of search result
129     */  
130     addOptXlsID: function(format, id, node, tagname) {
131         var elms = format.getElementsByTagNameNS(node, "http://www.opengis.net/xls", tagname);
132         if (elms) {
133             Ext.each(elms, function(elm, index) {
134                 var str = elm.getAttribute('value');
135                 if (tagname === "POIInfo") {
136                     var attvaluename = elm.getAttribute('name');
137                     if (attvaluename === 'ID') {
138                         id = str;
139                     }
140                 }
141             });
142         } 
143         return id;
144     },
145     
146     /** get 'ID' info from a xml segment
147     * @public
148     * @param {string}format xml reader and writer
149     * @param {number} numresult number of search result
150     * @param {string} node xml node
151     * @param {string} tagname tag from where extract data
152     * @param {string} sep separator character
153     * @returns {number} number of search result
154     */  
155     addOptXlsEnabled: function(format, enabled, node, tagname) {
156         var elms = format.getElementsByTagNameNS(node, "http://www.opengis.net/xls", tagname);
157         if (elms) {
158             Ext.each(elms, function(elm, index) {
159                 var str = elm.getAttribute('value');
160                 if (tagname === "POIInfo") {
161                     var attvaluename = elm.getAttribute('name');
162                     if (attvaluename === 'Enabled') {
163                         enabled = str;
164                     }
165                 }
166             });
167         } 
168         return enabled;
169     },
170     
171     /** get 'CodISTATComune' info from a xml segment
172     * @public
173     * @param {string}format xml reader and writer
174     * @param {number} numresult number of search result
175     * @param {string} node xml node
176     * @param {string} tagname tag from where extract data
177     * @param {string} sep separator character
178     * @returns {number} number of search result
179     */  
180     addOptXlsISTAT: function(format, istat, node, tagname) {
181         var elms = format.getElementsByTagNameNS(node, "http://www.opengis.net/xls", tagname);
182         if (elms) {
183             Ext.each(elms, function(elm, index) {
184                 var str = elm.getAttribute('value');
185                 if (tagname === "POIInfo") {
186                     var attvaluename = elm.getAttribute('name');
187                     if (attvaluename === 'CodISTATComune') {
188                         istat = str;
189                     }
190                 }
191             });
192         } 
193         return istat;
194     },
195     
196     
197     /** get 'POINumResults' info from a xml segment
198     * @public
199     * @param {string}format xml reader and writer
200     * @param {number} numresult number of search result
201     * @param {string} node xml node
202     * @param {string} tagname tag from where extract data
203     * @param {string} sep separator character
204     * @returns {number} number of search result
205     */  
206     addOptXlsPOINumResults: function(format, numresult, node, tagname) {
207         var elms = format.getElementsByTagNameNS(node, "http://www.opengis.net/xls", tagname);
208         if (elms) {
209             Ext.each(elms, function(elm, index) {
210                 var str = elm.getAttribute('value');
211                 if (tagname === "POIInfo") {
212                     var attvaluename = elm.getAttribute('name');
213                     if (attvaluename === 'POINumResults') {
214                         numresult = str;
215                     }
216                 }
217             });
218         } 
219 
220         return numresult;
221     },
222             
223     /** get 'POIType' info from a xml segment
224     * @public
225     * @param {string}format xml reader and writer
226     * @param {string} type category description
227     * @param {string} node xml node
228     * @param {string} tagname tag from where extract data
229     * @param {string} sep separator character
230     * @returns {number} category description
231     */  
232     addOptXlsPOIType: function(format, type, node, tagname, sep) {
233         var elms = format.getElementsByTagNameNS(node, "http://www.opengis.net/xls", tagname);
234         if (elms) {
235             Ext.each(elms, function(elm, index) {
236                 var str = elm.getAttribute('value');
237                 if (tagname === "POIInfo") {
238                     var attvaluename = elm.getAttribute('name');
239                     if (attvaluename === 'NomeTipo') {
240                         type = str;
241                     }
242                     else if (attvaluename === 'Punteggio' && str != "0.0") {
243                         type = type + sep + ' [' + str + '%]';
244                     }
245                 }
246             });
247         }
248 
249         return type;
250     },
251             
252      /** fill up a object with query result
253     * @public
254     * @param {string} doc xml document
255     * @returns {object} array of record plus search informations
256     */  
257     readRecords: function(doc) {
258 
259         this.xmlData = doc;
260 
261         var root = doc.documentElement || doc;
262 
263         var records = this.extractData(root);
264 
265         return {
266             success: true,
267             records: records,
268             totalRecords: records[0].data.numresult
269         };
270     },
271             
272      /** create array of record extracting data from a xml
273     * @public create records from xml content
274     * @param {string} root xml document
275     * @returns {array} record 
276     */  
277     extractData: function(root) {
278         var opts = {
279             /**
280              * Property: namespaces
281              * {Object} Mapping of namespace aliases to namespace URIs.
282              */
283             namespaces: {
284                 gml: "http://www.opengis.net/gml",
285                 xls: "http://www.opengis.net/xls"
286             }
287         };
288 
289         var records = [];
290         var format = new OpenLayers.Format.XML(opts);
291         var poi = format.getElementsByTagNameNS(root, "http://www.opengis.net/xls", 'POI');
292 
293         // Create record for each address
294         var recordType = Ext.data.Record.create([
295             "topID",
296             {name: "lon", type: "number"},
297             {name: "lat", type: "number"},
298             {name: "numresult", type: "number"},
299             "text",
300             "type",
301             "istat"
302         ]);
303         var reader = this;
304 
305         Ext.each(poi, function(point, index) {
306             var pos = format.getElementsByTagNameNS(point, "http://www.opengis.net/gml", 'pos');
307             var xy = '';
308             if (pos && pos[0]) {
309                 xy = format.getChildValue(pos[0]);
310             }
311 
312 
313             var xyArr = xy.split(' ');
314 
315             var text = '';
316             var numresult = '';
317             var type = '';
318             var topID = '';
319             var istat = '';
320             var enabled = false;
321 
322             topID = reader.addOptXlsID(format, topID, point, 'POIInfo', '');
323             text = reader.addOptXlsText(format, text, point, 'POIInfo', '');
324             numresult = reader.addOptXlsPOINumResults(format, numresult, point, 'POIInfo', '');
325             type = reader.addOptXlsPOIType(format, type, point, 'POIInfo', '');
326             istat = reader.addOptXlsISTAT(format, istat, point, 'POIInfo', ''); 
327             enabled = reader.addOptXlsEnabled(format, enabled, point, 'POIInfo', ''); 
328 
329             
330             var values = {
331                 topID: topID,
332                 lon: parseFloat(xyArr[0]),
333                 lat: parseFloat(xyArr[1]),
334                 numresult: numresult,
335                 text: text,
336                 type: type,
337                 istat: istat,
338                 enabled: enabled
339             };
340             var record = new recordType(values, index);
341             records.push(record);
342         });
343         return records;
344     }
345 });
346 
347