aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/data/mapping.js
blob: b10c1a328616d48589990ea7eb12b80fdef1c9e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/* <copyright>
Copyright (c) 2012, Motorola Mobility LLC.
All Rights Reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

* Neither the name of Motorola Mobility LLC nor the names of its
  contributors may be used to endorse or promote products derived from this
  software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
</copyright> */
/**
 @module montage/data/mapping
 @requires montage/data/blueprint
 @requires montage/data/store-connection-information
 @requires montage/core/core
 @requires montage/core/logger
 */
var Montage = require("montage").Montage;
var Blueprint = require("data/blueprint").Blueprint;
var BlueprintBinder = require("data/blueprint").BlueprintBinder;
var Attribute = require("data/blueprint").Attribute;
var Association = require("data/blueprint").Association;
var StoreConnectionInformation = require("data/store-connection-information").StoreConnectionInformation;
var logger = require("core/logger").logger("mapping");
/**
 * A mapping is an abstract class that is defined by each access store to represent the way to map an object or a property in the backing store.
 @class module:montage/data/mapping.Mapping
 @extends module:montage/core/core.Montage
 */
var Mapping = exports.Mapping = Montage.create(Montage, /** @lends module:montage/data/mapping.Mapping# */ {

    /**
     * @private
     */
    _owner:{
        serializable:true,
        enumerable:false,
        value:null
    },


    /**
     * The owner is the blueprint object (binder, blueprint attribute or association) that is supported by this mapping
     */
    owner:{
        get:function () {
            return this._owner;
        }
    },

    /**
     * @private
     */
    _parent:{
        serializable:true,
        enumerable:false,
        value:null
    },

    /**
     * The parent is the mapping object that contains this mapping
     */
    parent:{
        get:function () {
            return this._parent;
        }
    },

    /**
     Name of the mapping. The name is used to identify a mapping.
     @function
     @returns {String} this.parent.name
     */
    name:{
        get:function () {
            return this.parent.name;
        }
    },


    /**
     Store module Id associated with this binder mapping.
     @type {Property}
     @default {ID} montage/data/store
     */
    storeModuleId:{
        get:function () {
            return this.parent.storeModuleId;
        }
    },

    /**
     Store prototype name associated with this binder mapping.
     @type {Property}
     @default {String} "Store"
     */
    storePrototypeName:{
        get:function () {
            return this.parent.storePrototypeName;
        }
    },


    /**
     Initialize a newly allocated mapping.
     @function
     @param {owner} owner of this mapping
     @param {parent} container of this mapping
     @returns itself
     */
    initWithOwnerAndParent:{
        value:function (owner, parent) {
            this._owner = owner;
            this._parent = parent;
            return this;
        }
    }

});

/**
 * A mapping folder is a group of binder mappings that all relate to the same binder.<br/>
 * Together they provide exactly one mapping for each blueprint, attribute and association described in the binder.<br/>
 *
 * @class module:montage/data/mapping.MappingFolder
 * @extends module:montage/data/mapping.Mapping
 */
var MappingFolder = exports.MappingFolder = Montage.create(Mapping, /** @lends module:montage/data/mapping.MappingFolder# */ {

    /**
     @private
     */
    _name:{
        serializable:true,
        enumerable:false,
        value:null
    },

    /**
     Name of the mapping. The name is used to identify a mapping.
     @function
     @returns {String} this._name
     */
    name:{
        get:function () {
            return this._name;
        }
    },

    /**
     Binder that owns this mapping.
     @type {Property}
     @default {ID} montage/data/blueprint.BlueprintBinder
     */
    binder:{
        get:function () {
            return this._owner;
        }
    },

    /**
     Initialize a newly allocated mapping.
     @function
     @param {binder} owner of this mapping
     @param {name} name of this mapping. The name should be unique for a binder.
     @returns itself
     */
    initWithBinderAndName:{
        value:function (binder, name) {
            var newMapping = this.initWithOwnerAndParent(binder, null);
            newMapping._name = name;
            return newMapping;
        }
    },

    /**
     The identifier is the name of the blueprint, dot, the name of the
     attribute, and is used to make the serialization of attributes more
     readable.
     @type {Property}
     @default {String} this.name
     */
    identifier:{
        get:function () {
            return [
                this.name,
                this.owner.name
            ].join("_");
        }
    },

    /**
     @private
     */
    _mappings:{
        serializable:true,
        enumerable:false,
        distinct:true,
        value:new Array(5)
    },

    /**
     @private
     */
    _mappingForStoreId:{
        value:new Object(),
        serializable:false,
        distinct:true,
        enumerable:false,
        writable:false
    },

    deserializedFromSerialization:{
        value:function () {
            var aMapping, index;
            for (index = 0; typeof (aMapping = this._mappings[index]) !== "undefined"; index++) {
                var key = [
                    aMapping.storePrototypeName,
                    aMapping.storeModuleId
                ].join("_");
                this._mappingForStoreId[key] = aMapping;
            }
        }
    },

    /**
     List of mappings attached to this object.
     @function
     @returns mappings
     */
    mappings:{
        get:function () {
            return this._mappings;
        }
    },

    /**
     Add a mapping to the list of mappings.
     @function
     @param {mapping} mapping to add.
     @returns mapping
     */
    addMapping:{
        value:function (mapping) {
            if (mapping !== null) {
                var index = this.mappings.indexOf(mapping);
                if (index < 0) {
                    if (mapping.parent !== this) {
                        throw new Error(
                            "Mapping already owned: " + mapping.storeModuleId + "/" + mapping.storePrototypeName + " parent: " + JSON.stringify(mapping.parent.name));
                    }
                    this.mappings.push(mapping);
                    //
                    var key = [
                        mapping.storePrototypeName,
                        mapping.storeModuleId
                    ].join("_");
                    this._mappingForStoreId[key] = mapping;
                }
            }
            return mapping;
        }
    },

    /**
     Remove a mapping to the list of mappings.
     @function
     @param {mapping} mapping to remove.
     @returns mapping
     */
    removeMapping:{
        value:function (mapping) {
            if (mapping !== null) {
                var index = this.mappings.indexOf(mapping);
                if (index >= 0) {
                    this.mappings.splice(index, 1);
                    // Remove the cached entry
                    var key = [
                        mapping.storePrototypeName,
                        mapping.storeModuleId
                    ].join("_");
                    delete this._mappingForStoreId[key];
                }
            }
            return mapping;
        }
    },

    /*
     * @private
     */
    _storeKeyForStore:{
        value:function (store) {
            var metadata = Montage.getInfoForObject(store);
            return [
                metadata.objectName,
                metadata.moduleId
            ].join("_");
        }
    },

    /**
     Retrieve a mapping from the list of mappings.<br/>
     @function
     @param {storePrototypeName} name of the store to retrieve.
     @param {storeModuleId} module Id of the store to retrieve.
     @returns mapping
     */
    mappingForStoreId:{
        value:function (storePrototypeName, storeModuleId) {
            var key = [
                storePrototypeName,
                storeModuleId
            ].join("_");
            return this._mappingForStoreId[key];
        }
    }

});

/**
 * A binder mapping is an abstract class that is defined by each access store to represent the way to way to access a the backing store.
 @class module:montage/data/mapping.BinderMapping
 @extends module:montage/data/mapping.Mapping
 */
var BinderMapping = exports.BinderMapping = Montage.create(Mapping, /** @lends module:montage/data/mapping.BinderMapping# */ {

    /**
     Binder that owns this mapping.
     @type {Property}
     @default {ID} montage/data/blueprint.BlueprintBinder
     */
    binder:{
        get:function () {
            return this._owner;
        }
    },

    /**
     The identifier is the name of the blueprint, dot, the name of the
     attribute, and is used to make the serialization of attributes more
     readable.
     @type {Property}
     @default {String} this.name
     */
    identifier:{
        get:function () {
            return [
                this.parent.identifier,
                this.storeModuleId,
                this.storePrototypeName,
                this.owner.name
            ].join("_");
        }
    },

    /**
     Store module Id associated with this binder mapping.
     @type {Property}
     @default {ID} montage/data/store
     */
    storeModuleId:{
        get:function () {
            return "data/store";
        }
    },

    /**
     Store prototype name associated with this binder mapping.
     @type {Property}
     @default {String} "Store"
     */
    storePrototypeName:{
        get:function () {
            return "Store";
        }
    },


    /**
     @private
     */
    _connectionInformations:{
        serializable:true,
        enumerable:false,
        value:{}
    },


    /**
     List of connection information attached to this binder mapping.
     @function
     @returns mappings
     */
    connectionInformations:{
        get:function () {
            return this._connectionInformations;
        }
    },


    /**
     @private
     */
    _connectionInfoForName:{
        value:{},
        serializable:false,
        distinct:true,
        enumerable:false,
        writable:false
    },

    deserializedFromSerialization:{
        value:function () {
            var anInfo, index;
            for (index = 0; typeof (anInfo = this.connectionInformations[index]) !== "undefined"; index++) {
                this._connectionInfoForName[anInfo.name] = anInfo;
            }
        }
    },

    /*
     * Create a new connection information object. This can be overwritten by subclass if necessary.
     * @function
     * @returns connection information
     */
    createConnectionInformation:{
        value:function () {
            return StoreConnectionInformation.create();
        }
    },

    /**
     Add a connection information to the list of connection information.
     @function
     @param {info} connection information to add.
     @returns info
     */
    addConnectionInformation:{
        value:function (info) {
            if (info !== null) {
                var index = this.connectionInformations.indexOf(info);
                if (index < 0) {
                    this.connectionInformations.push(info);
                    // Add the cache
                    this._connectionInfoForName[info.name] = info;
                    // Fix the default
                    if (this._defaultConnectionInformationName.length == 0) {
                        this._defaultConnectionInformationName = info.name;
                    }
                }
            }
            return mapping;
        }
    },

    /**
     Remove a connection information to the list of connection information.
     @function
     @param {info} connection information to remove.
     @returns info
     */
    removeConnectionInformation:{
        value:function (info) {
            if (info !== null) {
                var index = this.connectionInformations.indexOf(info);
                if (index >= 0) {
                    this.connectionInformations.splice(index, 1);
                    // Remove the cached entry
                    delete this._connectionInfoForName[info.name];
                    // Fix the default
                    if ((this._defaultConnectionInformationName.length > 0) && (this._defaultConnectionInformationName === info.name)) {
                        if (this.connectionInformations.length > 0) {
                            this._defaultConnectionInformationName = this.connectionInformations[0].name;
                        } else {
                            this._defaultConnectionInformationName = "";
                        }
                    }
                }
            }
            return mapping;
        }
    },

    /**
     Retrieve a connection information object from the list of connection information.<br/>
     @function
     @param {name} name of the connection information to retrieve.
     @returns connection information
     */
    connectionInformationForName:{
        value:function (name) {
            return this._connectionInfoForName[name];
        }
    },


    _defaultConnectionInformationName:{
        serializable:true,
        enumerable:false,
        value:""
    },

    defaultConnectionInformationName:{
        get:function () {
            if (this._defaultConnectionInformationName.length == 0) {
                if (this.connectionInformations.length > 0) {
                    this._defaultConnectionInformationName = this.connectionInformations[0].name;
                }
            }
            return this._defaultConnectionInformationName;
        },
        set:function (name) {
            this._defaultConnectionInformationName = name;
        }
    },

    defaultConnectionInformation:{
        get:function () {
            return this.connectionInformationForName(this.defaultConnectionInformationName);
        }
    }


});


/**
 * A blueprint mapping is an abstract class that is defined by each access store to represent the way to way to access a the backing store.
 @class module:montage/data/mapping.BlueprintMapping
 @extends module:montage/data/mapping.Mapping
 */
var BlueprintMapping = exports.BlueprintMapping = Montage.create(Mapping, /** @lends module:montage/data/mapping.BlueprintMapping# */ {

    /**
     Blueprint that owns this mapping.
     @type {Property}
     @default {ID} montage/data/blueprint.Blueprint
     */
    blueprint:{
        get:function () {
            return this._owner;
        }
    },

    /**
     The identifier is the name of the blueprint, dot, the name of the
     attribute, and is used to make the serialization of attributes more
     readable.
     @type {Property}
     @default {String} this.name
     */
    identifier:{
        get:function () {
            return [
                this.parent.identifier,
                this.owner.name
            ].join("_");
        }
    }

});


/**
 * An attribute mapping is an abstract class that is defined by each access store to represent the way to way to access a the backing store.
 @class module:montage/data/mapping.AttributeMapping
 @extends module:montage/data/mapping.Mapping
 */
var AttributeMapping = exports.AttributeMapping = Montage.create(Mapping, /** @lends module:montage/data/mapping.AttributeMapping# */ {

    /**
     Attribute that owns this mapping.
     @type {Property}
     @default {ID} montage/data/blueprint.Attribute
     */
    attribute:{
        get:function () {
            return this._owner;
        }
    },

    /**
     The identifier is the name of the blueprint, dot, the name of the
     attribute, and is used to make the serialization of attributes more
     readable.
     @type {Property}
     @default {String} this.name
     */
    identifier:{
        get:function () {
            return [
                this.parent.identifier,
                this.owner.name
            ].join("_");
        }
    }

});


/**
 * An association mapping is an abstract class that is defined by each access store to represent the way to way to access a the backing store.
 @class module:montage/data/mapping.AssociationMapping
 @extends module:montage/data/mapping.Mapping
 */
var AssociationMapping = exports.AssociationMapping = Montage.create(Mapping, /** @lends module:montage/data/mapping.AssociationMapping# */ {

    /**
     Association that owns this mapping.
     @type {Property}
     @default {ID} montage/data/blueprint.Association
     */
    association:{
        get:function () {
            return this._owner;
        }
    },

    /**
     The identifier is the name of the blueprint, dot, the name of the
     attribute, and is used to make the serialization of attributes more
     readable.
     @type {Property}
     @default {String} this.name
     */
    identifier:{
        get:function () {
            return [
                this.parent.identifier,
                this.owner.name
            ].join("_");
        }
    }

});