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 Catasto of Regione Sardegna
 32  * @name_ Catasto_XLSReader
 33  * @class XSL reader for Catasto 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.Catasto_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.Catasto_XLSReader.superclass.constructor.call(this, meta, recordType || meta.fields);
 49 };
 50 
 51 Ext.extend(framework.data.Catasto_XLSReader, Ext.data.XmlReader,
 52         /**
 53          * @lends framework.data.Catasto_XLSReader.prototype
 54          */
 55                 {
 56                     /**
 57                      * framework_logoff plugin type.
 58                      * @public
 59                      * @type [Array] fields names that need to be elaborate to be transformed on OpenLayers.Bounds
 60                      */                    
 61                     geometryFields: null,
 62                     
 63                     
 64                     /** This method is only used by a DataProxy which has retrieved data from a remote server. 
 65                      * Create responseXML if the responseText is a XML format
 66                      * @public
 67                      * @param {Object} response The XHR object which contains the parsed XML document. 
 68                      * The response is expected to contain a method called 'responseXML' that returns an XML document object.
 69                      * @returns {Object} records A data block which is used by an {@link Ext.data.Store} as
 70                      * a cache of Ext.data.Records.
 71                      */
 72                     read: function(response) {
 73                         if (!response.responseXML) {
 74                             var text = response.responseText;
 75                             try { // code for IE
 76                                 var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
 77                                 xmlDoc.async = "false";
 78                                 xmlDoc.loadXML(text);
 79                                 response.responseXML = xmlDoc;
 80                             } catch (error) { // code for Mozilla, Firefox, Opera, etc.
 81                                 try {
 82                                     var parser = new DOMParser();
 83                                     var xmlDoc = parser.parseFromString(text, "text/xml");
 84                                     response.responseXML = xmlDoc;
 85                                 } catch (error) {
 86                                     alert(error.message);
 87                                     return;
 88                                 }
 89                             }
 90                         }
 91                         var doc = response.responseXML;
 92                         if (!doc) {
 93                             throw {message: "XmlReader.read: XML Document not available"};
 94                         }
 95                         return this.readRecords(doc);
 96                     },
 97                     /** fill up a object with query result
 98                      * @public
 99                      * @param {string} doc xml document
100                      * @returns {object} array of record plus search informations
101                      */
102                     readRecords: function(doc) {
103 
104                         this.xmlData = doc;
105 
106                         var root = doc.documentElement || doc;
107                         
108                         var success = true;
109                         
110                         if(root.tagName.indexOf("Exception") > -1)
111                             success = false;
112 
113                         var records = this.extractData(root);
114 
115                         return {
116                             success: success,
117                             records: records,
118                             totalRecords: records.length
119                         };
120                     },
121                     /** create array of record extracting data from a xml
122                      * @public create records from xml content
123                      * @param {string} root xml document
124                      * @returns {array} record 
125                      */
126                     extractData: function(root) {
127                         var opts = {
128                             /**
129                              * Property: namespaces
130                              * {Object} Mapping of namespace aliases to namespace URIs.
131                              */
132                             namespaces: {
133                                 gml: "http://www.opengis.net/gml",
134                                 wfs: "http://www.opengis.net/wfs"
135 
136                             }
137                         };
138                         var records = [];
139                         this.fields = ["CODICECOMUNE", "FOGLIO", "SEZIONE", "NUMERO", "POLIGONO"];
140                         var recordType = Ext.data.Record.create(this.fields);
141                         var format = new OpenLayers.Format.XML(opts);
142                         var dato = format.getElementsByTagNameNS(root, this.meta.namespace, this.meta.layerName);
143                         var self = this;
144                         Ext.each(dato, function(point, index) {
145                             var values = {};
146                             var node = null;
147                             var dim = 0;
148                             for (var i = 0; i < point.childNodes.length; i++) {
149                                 node = point.childNodes[i];
150                                 if (self.fields.indexOf(node.localName) > -1) {
151                                     if (self.meta.geometryFields.indexOf(node.localName) < 0)
152                                         values[node.localName] = node.textContent;
153                                     else
154                                         values[node.localName] = self.convertGeometry(node.textContent);
155                                     dim++;
156                                 }
157                             }
158                             ;
159                             if (self.fields.length == dim) {
160                                 var record = new recordType(values, records.length);
161                                 records.push(record);
162                             } else {
163                                 console.log('Errore durante inserimento record. Campi recond incompleti!')
164                             }
165                         });
166                         return records;
167                     },
168                     
169                     /** create object extent from a list of points
170                      * @private create object extent from a list of points
171                      * @param {string} points with sintax: X1,Y1 X2,Y2 X3,Y3 ... Xn,Yn
172                      * @returns {OpenLayers.Bounds} bound extent 
173                      */
174                     convertGeometry: function(points) {
175                         var vetPoints = points.split(" ");
176                         var points = [];
177                         var point = null;
178                         for (var i = 0; i < vetPoints.length; i++) {
179                             point = vetPoints[i].split(',');
180                             points.push(new OpenLayers.Geometry.Point(point[0], point[1]));
181                         }
182                         if (points.length == 1){//se la geometria รจ un punto viene restituito come punto
183                             return points[0];
184                         }else if (points.length >1) {//se la geometria ha 2 punti viene restituito un bound
185                             var ring = new OpenLayers.Geometry.LinearRing(points);
186                             var polygon = new OpenLayers.Geometry.Polygon([ring]);
187                             return polygon.getBounds();
188                         }else
189                             return null;
190                     }
191                 });
192 
193