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 born for Comuni and province service of Regione Sardegna but configurable for every wms/wfs service
 32  * @name_ GenericWfs_XLSReader
 33  * @class XSL reader for wms/wfs services
 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.GenericWfs_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.GenericWfs_XLSReader.superclass.constructor.call(this, meta, recordType || meta.fields);
 49 };
 50 
 51 Ext.extend(framework.data.GenericWfs_XLSReader, Ext.data.XmlReader,
 52         /**
 53          * @lends framework.data.GenericWfs_XLSReader.prototype
 54          */
 55                 {
 56                     
 57                     /**
 58                      * framework_logoff plugin type.
 59                      * @public
 60                      * @type [Array] fields names that need to be elaborate to be transformed on OpenLayers.Bounds
 61                      */                    
 62                     geometryFields: null,
 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 records = this.extractData(root);
109 
110                         return {
111                             success: true,
112                             records: records,
113                             totalRecords: records[0].data.numresult
114                         };
115                     },
116                     /** create array of record extracting data from a xml
117                      * @public create records from xml content
118                      * @param {string} root xml document
119                      * @returns {array} record 
120                      */
121                     extractData: function(root) {
122                         var opts = {
123                             /**
124                              * Property: namespaces
125                              * {Object} Mapping of namespace aliases to namespace URIs.
126                              */
127                             namespaces: {
128                                 gml: "http://www.opengis.net/gml",
129                                 wfs: "http://www.opengis.net/wfs"
130 
131                             }
132                         };
133                         var records = [];
134 //                        var saveRes = [];
135                         var recordType = Ext.data.Record.create(this.fields);
136                         var format = new OpenLayers.Format.XML(opts);
137                         var dato = format.getElementsByTagNameNS(root, this.meta.namespace, this.layerName);
138                         var self = this;
139                         Ext.each(dato, function(point, index) {
140                             var values = {};
141                             var node = null;
142                             var dim = 0;
143                             for (var i = 0; i < point.childNodes.length; i++) {
144                                 node = point.childNodes[i];
145                                 if (self.fields.indexOf(node.localName) > -1) {
146                                     if (self.meta.geometryField != node.localName)
147                                         values[node.localName] = node.textContent;
148                                     else
149                                         values[node.localName] = self.convertGeometryToExtent(node.textContent);
150 
151                                     dim++;
152                                 }
153                             }
154                             ;
155                             if (self.fields.length == dim) {
156                                 var record = new recordType(values, records.length);
157 //                                saveRes.push(values);
158                                 records.push(record);
159                             } else {
160                                 console.log('Errore durante inserimento record. Campi recond incompletti!')
161                             }
162                         });
163                         return records;
164                     },
165                     /** create object extent from a list of points
166                      * @private create object extent from a list of points
167                      * @param {string} points with sintax: X1,Y1 X2,Y2 X3,Y3 ... Xn,Yn
168                      * @returns {OpenLayers.Bounds} bound extent 
169                      */
170                     convertGeometryToExtent: function(points) {
171                         var vetPoints = points.split(" ");
172                         var points = [];
173                         var point = null;
174                         for (var i = 0; i < vetPoints.length; i++) {
175                             point = vetPoints[i].split(',');
176                             points.push(new OpenLayers.Geometry.Point(point[0], point[1]));
177                             }
178                         var ring = new OpenLayers.Geometry.LinearRing(points);
179                         var polygon = new OpenLayers.Geometry.Polygon([ring]);
180                         return polygon.getBounds();
181                         }
182                         
183                         //test version
184 //                        convertGeometryToExtent: function(points) {
185 //                        var vetPoints = points.split(" ");
186 //                        var points = [];
187 //                        var point = null;
188 //                        var left = '';
189 //                        var right = '';
190 //                        var bottom = '';
191 //                        var top = '';
192 //                        for (var i = 0; i < vetPoints.length; i++) {
193 //                            point = vetPoints[i].split(',');
194 //                            if(left == ''){
195 //                                left = point[0];
196 //                                bottom = point[1];
197 //                                right = point[0];
198 //                                top = point[1];
199 //                            }else{
200 //                                if(point[0] < left)
201 //                                    left = point[0];
202 //                                if(point[1] < bottom)
203 //                                    bottom = point[1];
204 //                                if(point[0] > right)
205 //                                    right = point[0];
206 //                                if(point[1] > top)
207 //                                    top = point[1];
208 //                            }
209 //                        }
210 //                        return  new OpenLayers.Bounds(left,bottom,right,top);
211 //                    }
212                 });
213 
214