~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/mapfish/core/Protocol/MergeFilterDecorator.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007  Camptocamp
 
3
 *
 
4
 * This file is part of MapFish Client
 
5
 *
 
6
 * MapFish Client is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * MapFish Client is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with MapFish Client.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
/*
 
21
 * @requires OpenLayers/Util.js
 
22
 * @requires OpenLayers/Protocol.js
 
23
 * @requires core/Searcher.js
 
24
 */
 
25
 
 
26
/**
 
27
 * Class: mapfish.Protocol.MergeFilterDecorator
 
28
 * Instances of this class decorate a protocol by merging filters provided
 
29
 * by searchers before invoking the decorated protocol's read method.
 
30
 * 
 
31
 * Inherits from:
 
32
 * - <OpenLayers.Protocol>
 
33
 */
 
34
 
 
35
mapfish.Protocol.MergeFilterDecorator = OpenLayers.Class(OpenLayers.Protocol, {
 
36
    /**
 
37
     * Property: searchers
 
38
     * Array({<mapfish.Searcher>} Array of searchers from which the merge
 
39
     *     filter decorator gets filters.
 
40
     */
 
41
    searchers: null,
 
42
 
 
43
    /**
 
44
     * APIProperty: protocol
 
45
     * {<OpenLayers.Protocol>} The decorated protocol.
 
46
     */
 
47
    protocol: null,
 
48
 
 
49
    /**
 
50
     * Constructor: mapfish.Protocol.MergeFilterDecorator
 
51
     *
 
52
     * Parameters:
 
53
     * options - {Object}
 
54
     */
 
55
    initialize: function(options) {
 
56
        this.searchers = [];
 
57
        OpenLayers.Protocol.prototype.initialize.call(this, options);
 
58
    },
 
59
 
 
60
    /**
 
61
     * APIMethod: register
 
62
     * Register a searcher.
 
63
     */
 
64
    register: function(searcher) {
 
65
        this.searchers.push(searcher);
 
66
    },
 
67
 
 
68
    /**
 
69
     * APIMethod: unregister
 
70
     * Unregister a searcher.
 
71
     */
 
72
    unregister: function(searcher) {
 
73
        OpenLayers.Util.removeItem(searcher, this.searchers);
 
74
    },
 
75
 
 
76
    /**
 
77
     * APIMethod: create
 
78
     * Create features, this method does nothing more than calling
 
79
     * the decorator protocol's create method.
 
80
     *
 
81
     * Parameters:
 
82
     * features - {Array({<OpenLayers.Feature.Vector>})} or
 
83
     *            {<OpenLayers.Feature.Vector>}
 
84
     * options - {Object} Optional object for configuring the request.
 
85
     *     This object is modified and should not be reused.
 
86
     *
 
87
     * Returns:
 
88
     * {<OpenLayers.Protocol.Response>} An <OpenLayers.Protocol.Response>
 
89
     *      object, this object is also passed to the callback function when 
 
90
     *      the request completes.
 
91
     */
 
92
    "create": function(features, options) {
 
93
        return this.protocol.create(features, options);
 
94
    },
 
95
 
 
96
    /**
 
97
     * APIMethod: read
 
98
     * Merge filters provided by searchers, and call the decorated
 
99
     * protocol's read method, passing it the merged filter.
 
100
     *
 
101
     * Parameters:
 
102
     * options - {Object} Optional object for configuring the request.
 
103
     *     This object is modified and should not be reused.
 
104
     *
 
105
     * Returns:
 
106
     * {<OpenLayers.Protocol.Response>} An <OpenLayers.Protocol.Response>
 
107
     *      object, this object is also passed to the callback function when
 
108
     *      the request completes.
 
109
     */
 
110
    "read": function(options) {
 
111
        options.filter = this.mergeFilters(
 
112
            options.filter || options.params, options.searcher);
 
113
        delete options.searcher;
 
114
        return this.protocol.read(options);
 
115
    },
 
116
 
 
117
    /**
 
118
     * Method: mergeFilters
 
119
     * Merge filters provided by searchers.
 
120
     *
 
121
     * Parameters:
 
122
     * filter - {<OpenLayers.Filter>}|{Object}
 
123
     * searcher - {<mapfish.Searcher>}
 
124
     *
 
125
     * Returns:
 
126
     * {<OpenLayers.Filter>} The resulting filter.
 
127
     */
 
128
    mergeFilters: function(filter, searcher) {
 
129
        var i, len, s;
 
130
        for (i = 0, len = this.searchers.length; i < len; i++) {
 
131
            s = this.searchers[i];
 
132
            if (s != searcher) {
 
133
                filter = this.toFilter(s.getFilter(), filter);
 
134
            }
 
135
        }
 
136
        return filter;
 
137
    },
 
138
 
 
139
    /**
 
140
     * Method: toFilter
 
141
     *
 
142
     * Parameters:
 
143
     * obj - {Object}
 
144
     * filter - {<OpenLayers.Filter>}
 
145
     *
 
146
     * Returns:
 
147
     * {<OpenLayers.Filter.Logical>}
 
148
     */
 
149
    toFilter: function(obj, filter) {
 
150
        if (!obj) {
 
151
            return filter;
 
152
        }
 
153
        if (!filter) {
 
154
            filter = new OpenLayers.Filter.Logical({
 
155
                type: OpenLayers.Filter.Logical.AND
 
156
            });
 
157
        } else if (!this.isFilter(filter)) {
 
158
            filter = this.fromObjToFilter(filter);
 
159
        } else if (!this.isLogicalFilter(filter)) {
 
160
            filter = new OpenLayers.Filter.Logical({
 
161
                type: OpenLayers.Filter.Logical.AND,
 
162
                filters: [filter]
 
163
            });
 
164
        }
 
165
        var filters = filter.filters;
 
166
        if (this.isFilter(obj)) {
 
167
            filters.push(obj);
 
168
        } else {
 
169
            filters.push(this.fromObjToFilter(obj));
 
170
        }
 
171
        return filter;
 
172
    },
 
173
 
 
174
    /**
 
175
     * Method: fromObjToFilter
 
176
     *
 
177
     * Paremeters:
 
178
     * obj - {Object}
 
179
     *
 
180
     * Returns:
 
181
     * {<OpenLayers.Filter.Logical>}
 
182
     */
 
183
    fromObjToFilter: function(obj) {
 
184
        var filters = [];
 
185
        for (var key in obj) {
 
186
            filters.push(
 
187
                new OpenLayers.Filter.Comparison({
 
188
                    type: OpenLayers.Filter.Comparison.EQUAL_TO,
 
189
                    property: key,
 
190
                    value: obj[key]
 
191
                })
 
192
            );
 
193
        }
 
194
        return new OpenLayers.Filter.Logical({
 
195
            type: OpenLayers.Filter.Logical.AND,
 
196
            filters: filters
 
197
        });
 
198
    },
 
199
 
 
200
    /**
 
201
     * Method: isLogicalFilter
 
202
     * Check if the object passed is a <OpenLayers.Filter.Logical> object.
 
203
     *
 
204
     * Parameters:
 
205
     * obj - {Object}
 
206
     *
 
207
     * Returns:
 
208
     * {Boolean}
 
209
     */
 
210
    isLogicalFilter: function(obj) {
 
211
        return !!obj.CLASS_NAME &&
 
212
               !!obj.CLASS_NAME.match(/^OpenLayers\.Filter\.Logical/);
 
213
    },
 
214
 
 
215
    /**
 
216
     * Method: isFilter
 
217
     * Check if the object passed is a <OpenLayers.Filter> object.
 
218
     *
 
219
     * Parameters:
 
220
     * obj - {Object}
 
221
     *
 
222
     * Returns:
 
223
     * {Boolean}
 
224
     */
 
225
    isFilter: function(obj) {
 
226
        return !!obj.CLASS_NAME &&
 
227
               !!obj.CLASS_NAME.match(/^OpenLayers\.Filter/);
 
228
    },
 
229
 
 
230
    /**
 
231
     * APIMethod: update
 
232
     * Update features, this method does nothing more than calling
 
233
     * the decorator protocol's update method.
 
234
     *
 
235
     * Parameters:
 
236
     * features - {Array({<OpenLayers.Feature.Vector>})} or
 
237
     *            {<OpenLayers.Feature.Vector>}
 
238
     * options - {Object} Optional object for configuring the request.
 
239
     *     This object is modified and should not be reused.
 
240
     *
 
241
     * Returns:
 
242
     * {<OpenLayers.Protocol.Response>} An <OpenLayers.Protocol.Response>
 
243
     *      object, this object is also passed to the callback function when 
 
244
     *      the request completes.
 
245
     */
 
246
    "update": function(features, options) {
 
247
        return this.protocol.update(features, options);
 
248
    },
 
249
 
 
250
    /**
 
251
     * APIMethod: delete
 
252
     * Delete features, this method does nothing more than calling
 
253
     * the decorator protocol's delete method.
 
254
     *
 
255
     * Parameters:
 
256
     * features - {Array({<OpenLayers.Feature.Vector>})} or 
 
257
     *            {<OpenLayers.Feature.Vector>}
 
258
     * options - {Object} Optional object for configuring the request.
 
259
     *     This object is modified and should not be reused.
 
260
     *
 
261
     * Returns:
 
262
     * {<OpenLayers.Protocol.Response>} An <OpenLayers.Protocol.Response>
 
263
     *      object, this object is also passed to the callback function when 
 
264
     *      the request completes.
 
265
     */
 
266
    "delete": function(features, options) {
 
267
        return this.protocol["delete"](features, options);
 
268
    },
 
269
 
 
270
    /**
 
271
     * Method: commit
 
272
     * Commit created, updated and deleted features, this method does nothing
 
273
     * more than calling the decorated protocol's commit method.
 
274
     *
 
275
     * Parameters:
 
276
     * features - {Array({<OpenLayers.Feature.Vector>})}
 
277
     * options - {Object} Object whose possible keys are "create", "update",
 
278
     *      "delete", "callback" and "scope", the values referenced by the
 
279
     *      first three are objects as passed to the "create", "update", and
 
280
     *      "delete" methods, the value referenced by the "callback" key is
 
281
     *      a function which is called when the commit operation is complete
 
282
     *      using the scope referenced by the "scope" key.
 
283
     *
 
284
     * Returns:
 
285
     * {Array({<OpenLayers.Protocol.Response>})} An array of
 
286
     * <OpenLayers.Protocol.Response> objects.
 
287
     */
 
288
    commit: function(features, options) {
 
289
        return this.protocol.commit(features, options);
 
290
    },
 
291
 
 
292
    CLASS_NAME: "mapfish.Protocol.MergeFilterDecorator"
 
293
});