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 POI search service of Regione Sardegna
 32 * @name_ DirectoryPOI_XLSReader
 33 * @class XSL Reader for POI 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.DirectoryPOI_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.DirectoryPOI_XLSReader.superclass.constructor.call(this, meta, recordType || meta.fields);
 49 };
 50 
 51 Ext.extend(framework.data.DirectoryPOI_XLSReader, Ext.data.XmlReader, 
 52 /**
 53  * @lends framework.data.DirectoryPOI_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 === 'POIName') {
109                         text = text + sep + '<b>' + str + '</b>';
110                     }
111                     else if (attvaluename === 'POICountry') {
112                         text = text + sep + ' (' + str + ')';
113                     }
114                 }
115             });
116         }
117 
118         return text;
119     },
120             
121    /** get 'POINumResults' 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     addOptXlsPOINumResults: function(format, numresult, node, tagname, sep) {
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 === 'POINumResults') {
138                         numresult = str;
139                     }
140                 }
141             });
142         }
143 
144         return numresult;
145     },
146         
147     /** get 'POILinkTab' info from a xml segment
148     * @public
149     * @param {string}format xml reader and writer
150     * @param {string} link http link
151     * @param {string} node xml node
152     * @param {string} tagname tag from where extract data
153     * @param {string} sep separator character
154     * @returns {string} http link
155     */  
156      addOptXlsPOILinkTab: function(format, link, node, tagname, sep) {
157         var elms = format.getElementsByTagNameNS(node, "http://www.opengis.net/xls", tagname);
158         if (elms) {
159             Ext.each(elms, function(elm, index) {
160                 var str = elm.getAttribute('value');
161                 if (tagname === "POIInfo") {
162                     var attvaluename = elm.getAttribute('name');
163                     if (attvaluename === 'POILinkTab') {
164                         link = str;
165                     }
166                 }
167             });
168         }
169 
170         return link;
171     },
172      
173     /** get 'POIType' info from a xml segment
174    * @public
175     * @param {string}format xml reader and writer
176     * @param {string} type category description
177     * @param {string} node xml node
178     * @param {string} tagname tag from where extract data
179     * @param {string} sep separator character
180     * @returns {number} category description
181     */  
182     addOptXlsPOIType: function(format, type, node, tagname, sep) {
183         var elms = format.getElementsByTagNameNS(node, "http://www.opengis.net/xls", tagname);
184         if (elms) {
185             Ext.each(elms, function(elm, index) {
186                 var str = elm.getAttribute('value');
187                 if (tagname === "POIInfo") {
188                     var attvaluename = elm.getAttribute('name');
189                     if (attvaluename === 'POIType') {
190                         type = str;
191                     }
192                 }
193             });
194         }
195 
196         return type;
197     },
198             
199      /** fill up a object with query result
200     * @public
201     * @param {string} doc xml document
202     * @returns {object} array of record plus search informations
203     */  
204     readRecords: function(doc) {
205 
206         this.xmlData = doc;
207 
208         var root = doc.documentElement || doc;
209 
210         var records = this.extractData(root);
211 
212         return {
213             success: true,
214             records: records,
215             totalRecords: records[0].data.numresult
216         };
217     },
218             
219      /** create array of record extracting data from a xml
220     * @public create records from xml content
221     * @param {string} root xml document
222     * @returns {array} record 
223     */  
224     extractData: function(root) {
225         var opts = {
226             /**
227              * Property: namespaces
228              * {Object} Mapping of namespace aliases to namespace URIs.
229              */
230             namespaces: {
231                 gml: "http://www.opengis.net/gml",
232                 xls: "http://www.opengis.net/xls"
233             }
234         };
235 
236         var records = [];
237         var format = new OpenLayers.Format.XML(opts);
238         var poi = format.getElementsByTagNameNS(root, "http://www.opengis.net/xls", 'POI');
239 
240         // Create record for each address
241         var recordType = Ext.data.Record.create([
242             {name: "lon", type: "number"},
243             {name: "lat", type: "number"},
244             {name: "numresult", type: "number"},
245             "text",
246             "type",
247             "link"
248         ]);
249         var reader = this;
250 
251         Ext.each(poi, function(point, index) {
252             var pos = format.getElementsByTagNameNS(point, "http://www.opengis.net/gml", 'pos');
253             var xy = '';
254             if (pos && pos[0]) {
255                 xy = format.getChildValue(pos[0]);
256             }
257 
258 
259             var xyArr = xy.split(' ');
260 
261             var text = '';
262             var numresult = '';
263             var type = '';
264             var link = '';
265 
266             text = reader.addOptXlsText(format, text, point, 'POIInfo', '');
267             numresult = reader.addOptXlsPOINumResults(format, numresult, point, 'POIInfo', '');
268             type = reader.addOptXlsPOIType(format, type, point, 'POIInfo', '');
269             link = reader.addOptXlsPOILinkTab(format, link, point, 'POIInfo', '');
270             if(link.substring(0,3) == 'www')
271                 link = 'http://' + link;
272 
273             //text = reader.addOptXlsText(format, text, address, 'Place', ' ');
274 
275             var values = {
276                 lon: parseFloat(xyArr[0]),
277                 lat: parseFloat(xyArr[1]),
278                 numresult: numresult,
279                 text: text,
280                 type: type,
281                 link: link
282             };
283             var record = new recordType(values, index);
284             records.push(record);
285         });
286         return records;
287     }
288 });
289 
290