all files / montage/core/meta/ property-descriptor.js

47.76% Statements 32/67
42.86% Branches 12/28
22.22% Functions 4/18
47.76% Lines 32/67
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                                                                                                                                                                                                                             87× 87× 87× 87×   87× 87× 87× 87× 87× 87× 87× 87× 87× 87× 87× 87×   87× 87×                               1131×   1131× 1218×   1131×                                                                                                                                                                                                                                                                                                                                                                                               87× 87×                                                                                            
var Montage = require("../core").Montage,
    ObjectDescriptorReference = require("./object-descriptor-reference").ObjectDescriptorReference,
    deprecate = require("../deprecate"),
    logger = require("../logger").logger("objectDescriptor");
 
// TODO change Defaults[*] to Defaults.* throughout. Needless performance
// degradations.
var Defaults = {
    name: "default",
    cardinality: 1,
    mandatory: false,
    readOnly: false,
    denyDelete: false,
    valueType: "string",
    collectionValueType: "list",
    valueObjectPrototypeName: "",
    valueObjectModuleId: "",
    valueDescriptor: void 0,
    enumValues: [],
    defaultValue: void 0,
    helpKey: ""
};
 
 
/* TypeDescriptor */
/* DeleteRules */
 
/*
 Deny
 If there is at least one object at the relationship destination (employees), do not delete the source object (department).
 
 For example, if you want to remove a department, you must ensure that all the employees in that department are first transferred elsewhere (or fired!); otherwise, the department cannot be deleted.
 
 Nullify
 Remove the relationship between the objects but do not delete either object.
 
 This only makes sense if the department relationship for an employee is optional, or if you ensure that you set a new department for each of the employees before the next save operation.
 
 Cascade
 Delete the objects at the destination of the relationship when you delete the source.
 
 For example, if you delete a department, fire all the employees in that department at the same time.
 
 No Action
 Do nothing to the object at the destination of the relationship.
 
 Default
 Value that will be assigned ?
 
 */
 
 
/**
 * @class PropertyDescriptor
 */
exports.PropertyDescriptor = Montage.specialize( /** @lends PropertyDescriptor# */ {
 
    /**
     * Initialize a newly allocated property descriptor.
     * @function
     * @param {string} name name of the property descriptor to create
     * @param {ObjectDescriptor} objectDescriptor
     * @param {number} cardinality name of the property descriptor to create
     * @returns itself
     */
    initWithNameObjectDescriptorAndCardinality: {
        value:function (name, objectDescriptor, cardinality) {
            this._name = (name !== null ? name : Defaults["name"]);
            this._owner = objectDescriptor;
            this.cardinality = (cardinality > 0 ? cardinality : Defaults["cardinality"]);
            return this;
        }
    },
 
    /**
     * Initialize a newly allocated property descriptor.
     * @deprecated
     * @function
     * @param {string} name name of the property descriptor to create
     * @param {ObjectDescriptor} objectDescriptor
     * @param {number} cardinality name of the property descriptor to create
     * @returns itself
     */
    initWithNameBlueprintAndCardinality: {
        value: deprecate.deprecateMethod(void 0, function (name, blueprint, cardinality) {
            return this.initWithNameObjectDescriptorAndCardinality(name, blueprint, cardinality);
        }, "new PropertyBlueprint().initWithNameBlueprintAndCardinality", "new PropertyDescriptor().initWithNameObjectDescriptorAndCardinality")
    },
 
    serializeSelf: {
        value:function (serializer) {
            serializer.setProperty("name", this.name);
            serializer.setProperty("objectDescriptor", this._owner, "reference");
            if (this.cardinality === Infinity) {
                serializer.setProperty("cardinality", -1);
            } else {
                this._setPropertyWithDefaults(serializer, "cardinality", this.cardinality);
            }
            this._setPropertyWithDefaults(serializer, "mandatory", this.mandatory);
            this._setPropertyWithDefaults(serializer, "readOnly", this.readOnly);
            this._setPropertyWithDefaults(serializer, "denyDelete", this.denyDelete);
            this._setPropertyWithDefaults(serializer, "valueType", this.valueType);
            this._setPropertyWithDefaults(serializer, "collectionValueType", this.collectionValueType);
            this._setPropertyWithDefaults(serializer, "valueObjectPrototypeName", this.valueObjectPrototypeName);
            this._setPropertyWithDefaults(serializer, "valueObjectModuleId", this.valueObjectModuleId);
            this._setPropertyWithDefaults(serializer, "valueDescriptor", this._valueDescriptorReference);
            if (this.enumValues.length > 0) {
                this._setPropertyWithDefaults(serializer, "enumValues", this.enumValues);
            }
            this._setPropertyWithDefaults(serializer, "defaultValue", this.defaultValue);
            this._setPropertyWithDefaults(serializer, "helpKey", this.helpKey);
            this._setPropertyWithDefaults(serializer, "definition", this.definition);
        }
    },
 
    deserializeSelf: {
        value:function (deserializer) {
            this._name = deserializer.getProperty("name");
            this._owner = deserializer.getProperty("objectDescriptor") || deserializer.getProperty("blueprint");
            this.cardinality = this._getPropertyWithDefaults(deserializer, "cardinality");
            if (this.cardinality === -1) {
                this.cardinality = Infinity;
            }
            this.mandatory = this._getPropertyWithDefaults(deserializer, "mandatory");
            this.readOnly = this._getPropertyWithDefaults(deserializer, "readOnly");
            this.denyDelete = this._getPropertyWithDefaults(deserializer, "denyDelete");
            this.valueType = this._getPropertyWithDefaults(deserializer, "valueType");
            this.collectionValueType = this._getPropertyWithDefaults(deserializer, "collectionValueType");
            this.valueObjectPrototypeName = this._getPropertyWithDefaults(deserializer, "valueObjectPrototypeName");
            this.valueObjectModuleId = this._getPropertyWithDefaults(deserializer, "valueObjectModuleId");
            this._valueDescriptorReference = this._getPropertyWithDefaults(deserializer, "valueDescriptor", "targetBlueprint");
            this.enumValues = this._getPropertyWithDefaults(deserializer, "enumValues");
            this.defaultValue = this._getPropertyWithDefaults(deserializer, "defaultValue");
            this.helpKey = this._getPropertyWithDefaults(deserializer, "helpKey");
            this.definition = this._getPropertyWithDefaults(deserializer, "definition");
            // DO NOT USE FOR DEVELOPMENT ONLY
            var value = deserializer.getProperty("synonym");
            Iif (value) {
                this.synonym = value;
            }
        }
    },
 
    _setPropertyWithDefaults: {
        value:function (serializer, propertyName, value) {
            if (value != Defaults[propertyName]) {
                serializer.setProperty(propertyName, value);
            }
        }
    },
 
    _getPropertyWithDefaults: {
        value:function (deserializer) {
            var propertyNames = Array.prototype.slice.call(arguments).slice(1, Infinity),
                value, i, n;
            for (i = 0, n = propertyNames.length; i < n && !value; i += 1) {
                value = deserializer.getProperty(propertyNames[i]);
            }
            return value || Defaults[propertyNames[0]];
        }
    },
 
    _owner: {
        value:null
    },
 
    /**
     * Component description attached to this property descriptor.
     */
    owner: {
        get:function () {
            return this._owner;
        }
    },
 
    _name: {
        value:null
    },
 
    /**
     * Name of the object. The name is used to define the property on the
     * object.
     * @readonly
     * @type {string}
     */
    name: {
        serializable:false,
        get:function () {
            return this._name;
        }
    },
 
    /**
     * The identifier is the name of the descriptor, dot, the name of the
     * property descriptor, and is used to make the serialization of property
     * descriptors more readable.
     * @readonly
     * @type {string}
     */
    identifier: {
        get:function () {
            return [
                this.owner.identifier,
                this.name
            ].join("_");
        }
    },
 
    /**
     * Cardinality of the property descriptor.
     *
     * The Cardinality of an property descriptor is the number of values that
     * can be stored. A cardinality of one means that only one object can be
     * stored. Only positive values are legal. A value of infinity means that
     * any number of values can be stored.
     *
     * @type {number}
     * @default 1
     */
    cardinality: {
        value: Defaults["cardinality"]
    },
 
    /**
     * @type {boolean}
     * @default false
     */
    mandatory: {
        value: Defaults["mandatory"]
    },
 
    /**
     * @type {boolean}
     * @default false
     */
    denyDelete: {
        value: Defaults["denyDelete"]
    },
 
    /**
     * @type {boolean}
     * @default false
     */
    readOnly: {
        value: Defaults["readOnly"]
    },
 
    /**
     * Returns true if the cardinality is more than one.
     * @readonly
     * @type {boolean}
     * @default false
     */
    isToMany: {
        get:function () {
            return this.cardinality === Infinity || this.cardinality > 1;
        }
    },
 
    /**
     * @type {boolean}
     * @default false
     */
    isDerived: {
        get:function () {
            return false;
        }
    },
 
    /**
     * @type {string}
     * Definition can be used to express a property as the result of evaluating an expression
     * An example would be to flatten/traverse two properties across two objects to make its
     * content accessible as a new property name. For example, in a many to many relaational
     * style, a Movie would have a toDirector property to a "DirectorRole" which itself would
     * point through a toTalent property to the actual Person. A "director" property definition
     * would then be "toDirector.toTalent"
     */
    definition: {
        value:null
    },
 
    /**
     * @type {string}
     * TODO: This is semantically similar to valueDescriptor
     * We should check if valueDescriptor can do the same job and eliminate
     * this.
     */
    valueType: {
        value: Defaults["valueType"]
    },
 
    /**
     * @type {string}
     */
    collectionValueType: {
        value: Defaults["collectionValueType"]
    },
 
    /**
     * @type {string}
     */
    valueObjectPrototypeName: {
        value: Defaults["valueObjectPrototypeName"]
    },
 
    /**
     * @type {string}
     */
    valueObjectModuleId: {
        value: Defaults["valueObjectModuleId"]
    },
 
    /**
     * Promise for the descriptor targeted by this association.
     *
     * **Note**: The setter expects an actual descriptor but the getter will
     * return a promise.
     * @type {string}
     */
    valueDescriptor: {
        serializable: false,
        get: function () {
            return this._valueDescriptorReference && this._valueDescriptorReference.promise(this.require);
        },
        set: function (descriptor) {
            this._valueDescriptorReference = new ObjectDescriptorReference().initWithValue(descriptor);
        }
    },
 
    _targetObjectDescriptorReference: {
        value: null
    },
 
    _enumValues: {
        value:null
    },
 
    /**
     * List of values for enumerated value types
     * @type {Array}
     */
    enumValues: {
        get:function () {
            if (!this._enumValues) {
                return [];
            }
            return this._enumValues;
        },
        set:function (value) {
            Eif (Array.isArray(value)) {
                this._enumValues = value;
            }
        }
    },
 
    defaultValue: {
        value: Defaults["defaultValue"]
    },
 
    helpKey:{
        value: Defaults["helpKey"]
    },
 
    objectDescriptorModuleId:require("../core")._objectDescriptorModuleIdDescriptor,
    objectDescriptor:require("../core")._objectDescriptorDescriptor,
 
    /********************************************************
     * Deprecated functions
     */
 
    /**
     * @deprecated
     * @readonly
     * @type {boolean}
     * @default false
     */
    // TODO: How to handle these case?
    isAssociationBlueprint: {
        get: deprecate.deprecateMethod(void 0, function () {
            return !!this._valueDescriptorReference;
        }, "isAssociationBlueprint", "No analog")
    },
 
    targetBlueprint: {
        get: deprecate.deprecateMethod(void 0, function () {
            return this.valueDescriptor;
        }, "targetBlueprint.get", "valueDescriptor.get"),
        set: deprecate.deprecateMethod(void 0, function (value) {
            this.valueDescriptor = value;
        }, "targetBlueprint.get", "valueDescriptor.set")
    },
 
    blueprintDescriptorModuleId: require("../core")._objectDescriptorModuleIdDescriptor,
    blueprint: require("../core")._objectDescriptorDescriptor
 
});