[76] | 1 | /** |
---|
| 2 | * Copyright (c) 2008-2010 The Open Source Geospatial Foundation |
---|
| 3 | * |
---|
| 4 | * Published under the BSD license. |
---|
| 5 | * See http://svn.geoext.org/core/trunk/geoext/license.txt for the full text |
---|
| 6 | * of the license. |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | /** |
---|
| 10 | * @include GeoExt/data/LayerRecord.js |
---|
| 11 | */ |
---|
| 12 | |
---|
| 13 | /** api: (define) |
---|
| 14 | * module = GeoExt.data |
---|
| 15 | * class = WFSCapabilitiesReader |
---|
| 16 | * base_link = `Ext.data.DataReader <http://dev.sencha.com/deploy/dev/docs/?class=Ext.data.DataReader>`_ |
---|
| 17 | */ |
---|
| 18 | Ext.namespace("GeoExt.data"); |
---|
| 19 | |
---|
| 20 | /** api: constructor |
---|
| 21 | * .. class:: WFSCapabilitiesReader(meta, recordType) |
---|
| 22 | * |
---|
| 23 | * :param meta: ``Object`` Reader configuration. |
---|
| 24 | * :param recordType: ``Array | Ext.data.Record`` An array of field |
---|
| 25 | * configuration objects or a record object. Default is |
---|
| 26 | * :class:`GeoExt.data.LayerRecord`. |
---|
| 27 | * |
---|
| 28 | * Data reader class to create an array of |
---|
| 29 | * :class:`GeoExt.data.LayerRecord` objects from a WFS GetCapabilities |
---|
| 30 | * response. |
---|
| 31 | */ |
---|
| 32 | GeoExt.data.WFSCapabilitiesReader = function(meta, recordType) { |
---|
| 33 | meta = meta || {}; |
---|
| 34 | if(!meta.format) { |
---|
| 35 | meta.format = new OpenLayers.Format.WFSCapabilities(); |
---|
| 36 | } |
---|
| 37 | if(!(typeof recordType === "function")) { |
---|
| 38 | recordType = GeoExt.data.LayerRecord.create( |
---|
| 39 | recordType || meta.fields || [ |
---|
| 40 | {name: "name", type: "string"}, |
---|
| 41 | {name: "title", type: "string"}, |
---|
| 42 | {name: "namespace", type: "string", mapping: "featureNS"}, |
---|
| 43 | {name: "abstract", type: "string"} |
---|
| 44 | ] |
---|
| 45 | ); |
---|
| 46 | } |
---|
| 47 | GeoExt.data.WFSCapabilitiesReader.superclass.constructor.call( |
---|
| 48 | this, meta, recordType |
---|
| 49 | ); |
---|
| 50 | }; |
---|
| 51 | |
---|
| 52 | Ext.extend(GeoExt.data.WFSCapabilitiesReader, Ext.data.DataReader, { |
---|
| 53 | |
---|
| 54 | /** private: method[read] |
---|
| 55 | * :param request: ``Object`` The XHR object which contains the parsed XML |
---|
| 56 | * document. |
---|
| 57 | * :return: ``Object`` A data block which is used by an ``Ext.data.Store`` |
---|
| 58 | * as a cache of ``Ext.data.Record`` objects. |
---|
| 59 | */ |
---|
| 60 | read: function(request) { |
---|
| 61 | var data = request.responseXML; |
---|
| 62 | if(!data || !data.documentElement) { |
---|
| 63 | data = request.responseText; |
---|
| 64 | } |
---|
| 65 | return this.readRecords(data); |
---|
| 66 | }, |
---|
| 67 | |
---|
| 68 | /** private: method[readRecords] |
---|
| 69 | * :param data: ``DOMElement | String | Object`` A document element or XHR |
---|
| 70 | * response string. As an alternative to fetching capabilities data |
---|
| 71 | * from a remote source, an object representing the capabilities can |
---|
| 72 | * be provided given that the structure mirrors that returned from the |
---|
| 73 | * capabilities parser. |
---|
| 74 | * :return: ``Object`` A data block which is used by an ``Ext.data.Store`` |
---|
| 75 | * as a cache of ``Ext.data.Record`` objects. |
---|
| 76 | * |
---|
| 77 | * Create a data block containing Ext.data.Records from an XML document. |
---|
| 78 | */ |
---|
| 79 | readRecords: function(data) { |
---|
| 80 | if(typeof data === "string" || data.nodeType) { |
---|
| 81 | data = this.meta.format.read(data); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | var featureTypes = data.featureTypeList.featureTypes; |
---|
| 85 | var fields = this.recordType.prototype.fields; |
---|
| 86 | |
---|
| 87 | var featureType, values, field, v, parts, layer, values; |
---|
| 88 | var layerOptions, protocolOptions; |
---|
| 89 | |
---|
| 90 | var protocolDefaults = { |
---|
| 91 | url: data.capability.request.getfeature.href.post |
---|
| 92 | }; |
---|
| 93 | |
---|
| 94 | var records = []; |
---|
| 95 | |
---|
| 96 | for(var i=0, lenI=featureTypes.length; i<lenI; i++) { |
---|
| 97 | featureType = featureTypes[i]; |
---|
| 98 | if(featureType.name) { |
---|
| 99 | values = {}; |
---|
| 100 | |
---|
| 101 | for(var j=0, lenJ=fields.length; j<lenJ; j++) { |
---|
| 102 | field = fields.items[j]; |
---|
| 103 | v = featureType[field.mapping || field.name] || |
---|
| 104 | field.defaultValue; |
---|
| 105 | v = field.convert(v); |
---|
| 106 | values[field.name] = v; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | protocolOptions = { |
---|
| 110 | featureType: featureType.name, |
---|
| 111 | featureNS: featureType.featureNS |
---|
| 112 | }; |
---|
| 113 | if(this.meta.protocolOptions) { |
---|
| 114 | Ext.apply(protocolOptions, this.meta.protocolOptions, |
---|
| 115 | protocolDefaults); |
---|
| 116 | } else { |
---|
| 117 | Ext.apply(protocolOptions, {}, protocolDefaults); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | layerOptions = { |
---|
| 121 | protocol: new OpenLayers.Protocol.WFS(protocolOptions), |
---|
| 122 | strategies: [new OpenLayers.Strategy.Fixed()] |
---|
| 123 | }; |
---|
| 124 | if(this.meta.layerOptions) { |
---|
| 125 | Ext.apply(layerOptions, this.meta.layerOptions); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | values.layer = new OpenLayers.Layer.Vector( |
---|
| 129 | featureType.title || featureType.name, |
---|
| 130 | layerOptions |
---|
| 131 | ); |
---|
| 132 | |
---|
| 133 | records.push(new this.recordType(values, values.layer.id)); |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | return { |
---|
| 137 | totalRecords: records.length, |
---|
| 138 | success: true, |
---|
| 139 | records: records |
---|
| 140 | }; |
---|
| 141 | } |
---|
| 142 | }); |
---|