Code coverage report for api/datum/DatumField.js

Statements: 70.38% (278 / 395)      Branches: 63.4% (239 / 377)      Functions: 81.03% (47 / 58)      Lines: 70.38% (278 / 395)      Ignored: none     

All files » api/datum/ » DatumField.js
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 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 9381 1                                         1 114982 114982     114982   114982 21   114982 1936   114982     1             13                                                                   1799850     109645 36   109609       109609 109596   109609 109609 109609       109609           468 468     110627 110627 2022 2016   6     110627 49667 49661   6     110627 322                     265 265 191   265 74   265           390     111382 2   111380 13 13   111367                 13     13 13 13               26     7223 2   7221 13 13   7208           160     63716     63716 217 217   63499           118     63176     63176 170 170   63006                 13     13 13 13               474     102665 20   102645 15 15   102630           261     111946   111946     111946 157   111946 111946 111946 1 1   111945           568423 3   568423     1144     1144 279 279   865 2   865           292     109414     109414       109414           366     111823     111823 100 100   111723                     870606 870606 368811     501795 663   501132 501078   54 44 44   44   10                       10   10       10 10 1 1 1   9           9 9                 125003 6   124997 124975   124997 109539   15458 293 293 293 293 293 293               15165 15165   2657 2657 2657   12508 12393 12393 12393 12393     115   113 17 1 1 1   16   17   96 96 96 96       2 1 1   1     1                             1       1 1 1           238606     109594 103   109491 109368 109368   123 123 123   123           10     150 129   21     21           382     111304 5   111299       111299 451   111299             386     113152     113152       113152 113152 113152             216     82864     82864       82864                 113165 13   113152       113152                 13     13       13                                           122     75485     75485       75485                                                                                                                                                                                                                                                                                                                                                                                             97 97 97                           236     3126 335   2791 3   2791           238290 238290 238290 238290 238290 238290 238290     238290   238290 238290     238290 238290   238290 28 28   28 2   28     238290 238290   238290 888   238290 942   238290 252   238290 864     238290 40088   238290     238290 51732   238290 171   238290 393   238290 393       238290 238290   238290 238290   238290 238290           1  
var FieldDBObject = require("./../FieldDBObject").FieldDBObject;
var Confidential = require("./../confidentiality_encryption/Confidential").Confidential;
 
/**
 * @class The datum fields are the fields in the datum and session models.
 *        They can be freely added and should show up in the datum view
 *        according to frequency.
 *  @name  DatumField
 * @property size The size of the datum field refers to the width of the
 *           text area. Some of them, such as the judgment one will be very
 *           short, while others context can be infinitely long.
 * @property label The label that is associated with the field, such as
 *           Transcription, Morphemes, etc.
 * @property value This is what the user will enter when entering data into
 *           the data fields.
 * @property mask This allows users to mask fields for confidentiality.
 * @property shouldBeEncrypted This is whether the field is masked or not.
 * @property help This is a pop up that tells other users how to use the
 *           field the user has created.
 * @extends FieldDBObject
 * @constructs
 */
var DatumField = function DatumField(options) {
  Eif (!this._fieldDBtype) {
    this._fieldDBtype = "DatumField";
  }
 
  this.debug("Constructing DatumField ", options);
  // Let encryptedValue and value from serialization be set
  if (options && options.encryptedValue) {
    options._encryptedValue = options.encryptedValue;
  }
  if (options && options.value) {
    options._value = options.value;
  }
  FieldDBObject.apply(this, arguments);
};
 
DatumField.prototype = Object.create(FieldDBObject.prototype, /** @lends DatumField.prototype */ {
  constructor: {
    value: DatumField
  },
 
  defaults: {
    get: function() {
      return {
        id: Date.now(),
        labelFieldLinguists: "",
        labelPsychoLinguists: "",
        labelExperimenters: "",
        labelNonLinguists: "",
        labelTranslators: "",
        labelComputationalLinguist: "",
        type: "",
        shouldBeEncrypted: false,
        showToUserTypes: "all",
        defaultfield: false,
        value: "",
        mask: "",
        encrypted: "",
        json: {},
        help: "Put your team's data entry conventions here (if any)...",
        helpLinguists: "Put your team's data entry conventions here (if any)...",
        helpNonLinguists: "Put your team's data entry conventions here (if any)...",
        helpTranslators: "Put your team's data entry conventions here (if any)...",
        ComputationalLinguists: "Put your team's data entry conventions here (if any)...",
        helpDevelopers: "Put your team's data entry conventions here (if any)..."
      };
    },
    set: function() {}
  },
 
  // Internal models: used by the parse function
  internalModels: {
    value: {} // There are no nested models
  },
 
  id: {
    get: function() {
      return this._id || FieldDBObject.DEFAULT_STRING;
    },
    set: function(value) {
      if (value === this._id) {
        return;
      }
      Iif (!value) {
        delete this._id;
        return;
      }
      if (value.trim) {
        value = value.trim();
      }
      var originalValue = value + "";
      value = this.sanitizeStringForPrimaryKey(value); //TODO dont do this on all objects
      Iif (value === null) {
        this.bug("Invalid id, not using " + originalValue + " id remains as " + this._id);
        return;
      }
      this._id = value;
    }
  },
 
  label: {
    get: function() {
      this.debug("label is deprecated, instead automatically contextualize a label for appropriate user eg labelFieldLinguists, labelNonLinguists, labelTranslators, labelComputationalLinguist");
      return this._labelNonLinguists || this._labelFieldLinguists || this.id || FieldDBObject.DEFAULT_STRING;
    },
    set: function(value) {
      this.debug("label is deprecated, instead automatically contextualize a label for appropriate user eg labelFieldLinguists,  labelNonLinguists, labelTranslators, labelComputationalLinguist");
      if (!this._labelFieldLinguists) {
        if (value && value.length > 2) {
          this._labelFieldLinguists = value[0].toUpperCase() + value.substring(1, value.length);
        } else {
          this._labelFieldLinguists = value;
        }
      }
      if (!this._labelNonLinguists) {
        if (value && value.length > 2) {
          this._labelNonLinguists = value[0].toUpperCase() + value.substring(1, value.length);
        } else {
          this._labelNonLinguists = value;
        }
      }
      if (!this._id) {
        this.id = value;
      }
    }
  },
 
  userchooseable: {
    get: function() {
      this.debug("userchooseable is deprecated, instead use defaultfield");
      return this.defaultfield;
    },
    set: function(value) {
      this.debug("userchooseable is deprecated, instead use defaultfield");
      if (value === "disabled") {
        value = true;
      }
      if (!value) {
        value = false;
      }
      this.defaultfield = value;
    }
  },
 
  labelFieldLinguists: {
    get: function() {
      return this._labelFieldLinguists || this.label;
    },
    set: function(value) {
      if (value === this._labelFieldLinguists) {
        return;
      }
      if (!value) {
        delete this._labelFieldLinguists;
        return;
      }
      this._labelFieldLinguists = value.trim();
    }
  },
 
  labelPsychoLinguists: {
    get: function() {
      return this._labelPsychoLinguists || this.labelFieldLinguists;
    },
    set: function(value) {
      Iif (value === this._labelPsychoLinguists) {
        return;
      }
      Eif (!value) {
        delete this._labelPsychoLinguists;
        return;
      }
      this._labelPsychoLinguists = value.trim();
    }
  },
 
  labelExperimenters: {
    get: function() {
      return this._labelExperimenters || this.labelNonLinguists;
    },
    set: function(value) {
      if (value === this._labelExperimenters) {
        return;
      }
      if (!value) {
        delete this._labelExperimenters;
        return;
      }
      this._labelExperimenters = value.trim();
    }
  },
 
  labelNonLinguists: {
    get: function() {
      return this._labelNonLinguists || this.label;
    },
    set: function(value) {
      Iif (value === this._labelNonLinguists) {
        return;
      }
      if (!value) {
        delete this._labelNonLinguists;
        return;
      }
      this._labelNonLinguists = value.trim();
    }
  },
 
  labelTranslators: {
    get: function() {
      return this._labelTranslators || this.labelNonLinguists;
    },
    set: function(value) {
      Iif (value === this._labelTranslators) {
        return;
      }
      if (!value) {
        delete this._labelTranslators;
        return;
      }
      this._labelTranslators = value.trim();
    }
  },
 
  labelComputationalLinguist: {
    get: function() {
      return this._labelComputationalLinguist || this.label;
    },
    set: function(value) {
      Iif (value === this._labelComputationalLinguist) {
        return;
      }
      Eif (!value) {
        delete this._labelComputationalLinguist;
        return;
      }
      this._labelComputationalLinguist = value.trim();
    }
  },
 
  type: {
    get: function() {
      return this._type || FieldDBObject.DEFAULT_STRING;
    },
    set: function(value) {
      if (value === this._type) {
        return;
      }
      if (!value) {
        delete this._type;
        return;
      }
      this._type = value.trim();
    }
  },
 
  shouldBeEncrypted: {
    get: function() {
      return this._shouldBeEncrypted || FieldDBObject.DEFAULT_FALSE;
    },
    set: function(value) {
      this.verbose("Setting shouldBeEncrypted " + value, this);
 
      Iif (value === this._shouldBeEncrypted) {
        return;
      }
      if (value === "checked") {
        value = true;
      }
      value = !!value;
      this.verbose("Setting shouldBeEncrypted " + value, this);
      if (this._shouldBeEncrypted === true && value === false) {
        this.warn("This field's shouldBeEncrypted cannot be undone. Only a corpus administrator can change shouldBeEncrypted to false if it has been true before.");
        return;
      }
      this._shouldBeEncrypted = value;
    }
  },
 
  encrypted: {
    get: function() {
      if (this._value && this._value.indexOf("confidential:") === 0) {
        this._encrypted = true;
      }
      return this._encrypted || FieldDBObject.DEFAULT_FALSE;
    },
    set: function(value) {
      Iif (value === this._encrypted) {
        return;
      }
      if (!value) {
        delete this._encrypted;
        return;
      }
      if (value === "checked") {
        value = true;
      }
      this._encrypted = !!value;
    }
  },
 
  showToUserTypes: {
    get: function() {
      return this._showToUserTypes || "all";
    },
    set: function(value) {
      Iif (value === this._showToUserTypes) {
        return;
      }
      Iif (!value) {
        delete this._showToUserTypes;
        return;
      }
      this._showToUserTypes = value.trim();
    }
  },
 
  defaultfield: {
    get: function() {
      return this._defaultfield || FieldDBObject.DEFAULT_FALSE;
    },
    set: function(value) {
      Iif (value === this._defaultfield) {
        return;
      }
      if (!value) {
        delete this._defaultfield;
        return;
      }
      this._defaultfield = !!value;
    }
  },
 
  repairMissingEncryption: {
    value: true
  },
 
  value: {
    configurable: true,
    get: function() {
      this.debug("looking up the value of this field", this._value);
      if (this._value === undefined || this._value === null || this._value === "") {
        return FieldDBObject.DEFAULT_STRING;
      }
      // If there was a value before, there are extra precautions
      if (!this._shouldBeEncrypted) {
        return this._value;
      } else {
        if (!this.encrypted) {
          return this._value;
        } else {
          if (!this.decryptedMode) {
            Eif (this._encryptedValue) {
              this.warn("User is not able to view the  " + this.mask + "  value of " + this.label + ", it is encrypted and the user isn't in decryptedMode."); //" mask: "+ this._mask +" value: " +this._value);
            }
            return this.mask || FieldDBObject.DEFAULT_STRING;
          } else {
            Iif (!this._encryptedValue || this._encryptedValue.indexOf("confidential:") !== 0) {
              this.warn("The value was supposed to be encrypted but was not encrypted. This should not happen, it might only happen if an app was editing the data and didn't have the encryption implemented. Not overwritting the value.");
              if (this.repairMissingEncryption && this.confidential) {
                var encryptedValue = this.confidential.encrypt(this._value);
                this.warn(" Encrypting the value " + this._value);
                this._encryptedValue = encryptedValue;
                this._mask = this.createMask(this._value);
                this._value = this._mask;
              } else {
                return this._value;
              }
            }
            Eif (this._encryptedValue.indexOf("confidential:") === 0) {
              // All conditions are satisified, decrypt the value and give it to the user
              Iif (!this.confidential) {
                this.warn("This field's encrypter hasnt been set. It cannot be decrypted yet.");
                return this.mask;
              }
              var decryptedValue = this.confidential.decrypt(this._encryptedValue);
              if (!decryptedValue) {
                this.warn("Encryption key is the wrong key, showing the user the mask instead.");
                this.decryptedMode = false;
                return this.mask;
              }
              Iif (this.type && this.type.indexOf("number") > -1) {
                var tryAsNumber = Number(decryptedValue);
                if (!isNaN(tryAsNumber)) {
                  decryptedValue = tryAsNumber;
                }
              }
              this.debug("decryptedValue " + decryptedValue);
              return decryptedValue;
            }
          }
        }
      }
      this.bug("The value wasn't returned, this should never happen and is a bug in the logic.");
 
    },
    set: function(value) {
      if (value === this._value) {
        return;
      }
      if (typeof value.trim === "function") {
        value = value.trim();
      }
      if (value === undefined || value === null || value === "" && !this._value) {
        return;
      }
      if (!value && this._value) {
        var fieldCanBeEmptied = !this.encrypted || !this._shouldBeEncrypted || (this._shouldBeEncrypted && this.encrypted && this.decryptedMode);
        Eif (fieldCanBeEmptied) {
          this._value = "";
          this._mask = "";
          this._encryptedValue = "";
          return;
        } else {
          if (this._value) {
            this.warn("The value " + this._value + " of " + this.id + " was requested to be removed by the user, but they are not able to edit the field currently. No changes were made ");
          }
          return;
        }
      }
      var encryptedValue;
      if (!this._shouldBeEncrypted) {
        // this._encryptedValue = value;
        this._mask = value;
        this._value = this._mask;
        return;
      }
      if (!this.encrypted) {
        this._encryptedValue = value;
        this._mask = value;
        this._value = this._mask;
        return;
      }
 
      if (!this._value) {
        // If there was no value before, set the new value
        if (!this.confidential) {
          if (typeof value.indexOf === "function" && value.indexOf("confidential:") === 0 && !this._encryptedValue) {
            this._encryptedValue = value;
            this._value = this.mask;
            this.debug("This is probably a new field initialization from old data (the value has \"confidential:\" in it, and yet the encryptedValue isn't set");
          } else {
            this.warn("This field's encrypter hasnt been set. It cannot be edited yet.");
          }
          return;
        }
        this._encryptedValue = this.confidential.encrypt(value);
        this._mask = this.createMask(value);
        this._value = this._mask;
        return;
      }
 
      // If there was a value before, there are extra precautions
      if (!this.decryptedMode) {
        Eif (this._encryptedValue) {
          this.warn("User is not able to change the value " + this.mask + " of " + this.label + ", it is encrypted and the user isn't in decryptedMode.");
        }
        return;
      }
 
      Iif (!this._encryptedValue || this._encryptedValue.indexOf("confidential:") !== 0) {
        this.warn("The value was changed, and it was supposed to be encrypted but was not encrypted. This should not happen, it might only happen if an app was editing the data and didn't have the encryption implemented.");
        if (this.repairMissingEncryption && this.confidential) {
          encryptedValue = this.confidential.encrypt(value);
          this._encryptedValue = encryptedValue;
          this._mask = this.createMask(value);
          this._value = this._mask;
          this.warn(" Overwritting the value.");
        } else {
          this.warn(" Not overwritting the value.");
          return;
        }
      }
 
      // All conditions are satisified, accept the new value.
      Iif (!this.confidential) {
        this.warn("This field's encrypter hasnt been set. It cannot be edited yet.");
        return;
      }
      this._encryptedValue = this.confidential.encrypt(value);
      this._mask = this.createMask(value);
      this._value = this._mask;
    }
  },
 
  mask: {
    get: function() {
      return this._mask || FieldDBObject.DEFAULT_STRING;
    },
    set: function(value) {
      if (value === this._mask) {
        return;
      }
      if (!value) {
        delete this._mask;
        return;
      }
      this.debug("Setting datum field mask " + value);
      Eif (typeof value.trim === "function") {
        value = value.trim();
      }
      this._mask = value;
    }
  },
 
  encryptedValue: {
    get: function() {
      return this._encryptedValue || FieldDBObject.DEFAULT_STRING;
    },
    set: function(value) {
      if (!value || value === this._encryptedValue) {
        return;
      }
      Iif (this._encryptedValue) {
        this.warn(this.id + " cannot be changed directly, instead you must be in decryptedMode ");
      }
      this.debug(" not setting " + value);
    }
  },
 
  json: {
    get: function() {
      return this._json || FieldDBObject.DEFAULT_OBJECT;
    },
    set: function(value) {
      if (value === this._json) {
        return;
      }
      Iif (!value) {
        delete this._json;
        return;
      }
      if (value && this._shouldBeEncrypted && this.encrypted && !this.decryptedMode) {
        this.debug("encrypt json also");
      }
      this._json = value;
    }
  },
 
  help: {
    configurable: true,
    get: function() {
      return this._help || this._helpNonLinguists || "Put your team's data entry conventions here (if any)...";
    },
    set: function(value) {
      Iif (value === this._help) {
        return;
      }
      Iif (!value) {
        delete this._help;
        return;
      }
      this._help = value.trim();
      Eif (!this._helpNonLinguists) {
        this.helpNonLinguists = this._help;
      }
    }
  },
 
  helpLinguists: {
    get: function() {
      return this._helpLinguists || this.help;
    },
    set: function(value) {
      Iif (value === this._helpLinguists) {
        return;
      }
      Iif (!value) {
        delete this._helpLinguists;
        return;
      }
      this._helpLinguists = value.trim();
    }
  },
 
  helpNonLinguists: {
    get: function() {
      return this._helpNonLinguists || this.help;
    },
    set: function(value) {
      if (value === this._helpNonLinguists) {
        return;
      }
      Iif (!value) {
        delete this._helpNonLinguists;
        return;
      }
      this._helpNonLinguists = value.trim();
    }
  },
 
  helpTranslators: {
    get: function() {
      return this._helpTranslators || this.help;
    },
    set: function(value) {
      Iif (value === this._helpTranslators) {
        return;
      }
      Iif (!value) {
        delete this._helpTranslators;
        return;
      }
      this._helpTranslators = value.trim();
    }
  },
 
  helpComputationalLinguists: {
    get: function() {
      return this._helpComputationalLinguists || this.help;
    },
    set: function(value) {
      if (value === this._helpComputationalLinguists) {
        return;
      }
      if (!value) {
        delete this._helpComputationalLinguists;
        return;
      }
      this._helpComputationalLinguists = value.trim();
    }
  },
 
  helpDevelopers: {
    get: function() {
      return this._helpDevelopers || this.help;
    },
    set: function(value) {
      Iif (value === this._helpDevelopers) {
        return;
      }
      Iif (!value) {
        delete this._helpDevelopers;
        return;
      }
      this._helpDevelopers = value.trim();
    }
  },
 
  /**
   * Called before set and save, checks the attributes that the
   * user is attempting to set or save. If the user is trying to
   * set a mask on an encrypted datum field that should be encrypted, the only time they can do this is if the data is
   * in tempEncryptedVisible, with decryptedMode on.
   *
   * @param attributes
   */
  validate: {
    value: function(attributes) {
      this.tood("Vaidating is commented out ", attributes);
      //      if(attributes.mask){
      //        if(attributes.shouldBeEncrypted !== "checked" ){
      //          //user can modify the mask, no problem.
      //        }else if(attributes.encrypted !== "checked" ){
      //          //user can modify the mask, no problem.
      //        }else if( attributes.encrypted === "checked" &&
      ////            attributes.tempEncryptedVisible === "checked"  &&
      //            attributes.shouldBeEncrypted === "checked" &&
      //              this.corpus.confidential.decryptedMode ){
      //          //user can modify the mask, no problem.
      //        }else if( attributes.mask !== this.mask ){
      //          return "The datum is presently encrypted, the mask cannot be set by anything other than the model itself.";
      //        }
      //      }
      //      if( attributes.value ){
      //
      //        if(this.value && this.value.indexOf("confidential") === 0){
      //          return "Cannot modify the value of a confidential datum field directly";
      //        }
      //
      //        if(attributes.shouldBeEncrypted !== "checked" ){
      //          //user can modify the value, no problem.
      //        }else if(attributes.encrypted !== "checked" ){
      //          //user can modify the value, no problem.
      //        }else if( attributes.encrypted === "checked" &&
      ////            attributes.tempEncryptedVisible === "checked"  &&
      //            attributes.shouldBeEncrypted === "checked" &&
      //              this.corpus.confidential.decryptedMode ){
      //          //the user/app can modify the value, no problem.
      //        }else if( attributes.value !== this.value ){
      //          return "The value cannot be set by anything other than the model itself, from a mask.";
      //        }
      //      }
    }
  },
 
  /**
   * In the case of the datumfield, if the datum
   * field is not encrypted, then the mask and value are essentially the same.
   * If however the datum is supposed to be encrypted, the value needs to
   * start with confidential, and the mask should be xxxx representign
   * words/morphemes which are allowed to be shown.
   * http://stackoverflow.com/questions/11315844/what-is-the-correct-way-in-backbone-js-to-listen-and-modify-a-model-property
   *
   * @param key
   * @param value
   * @param options
   * @returns null
   */
  upgrade: {
    value: function(attributes, options) {
 
      options = options || {};
      // do any other custom property changes here
      /*
       * Copy the mask, value and shouldBeEncrypted and encrypted from the object if it is not being set.
       */
      if (attributes.mask === undefined && this.mask) {
        attributes.mask = this.mask;
      }
      if (attributes.value === undefined && this.value) {
        attributes.value = this.value;
      }
      if (attributes.shouldBeEncrypted === undefined && this.shouldBeEncrypted) {
        attributes.shouldBeEncrypted = this.shouldBeEncrypted;
      }
      if (attributes.encrypted === undefined && this.encrypted) {
        attributes.encrypted = this.encrypted;
      }
 
      if ((attributes.mask && attributes.mask !== "")) {
 
        if (attributes.shouldBeEncrypted !== "checked") {
          //Don't do anything special, this field doesnt get encrypted when the data is confidential
          attributes.value = attributes.mask;
        } else if (attributes.encrypted !== "checked") {
          //Don't do anything special, this datum isn't confidential
          attributes.value = attributes.mask;
 
          /*
           * A, B, C, D: If we are supposed to be encrypted, and we are encrypted, but we want to let the user see the data to change it.
           *
           */
        } else if (this.corpus.confidential.decryptedMode) {
 
          /*
           * A: If it wasn't encrypted, encrypt the value, and leave the mask as the original value for now,
           * can happen when the user clicks on the lock button for the first time.
           */
          if (attributes.mask.indexOf("confidential:") !== 0) {
            //          attributes.mask = attributes.mask;//leave mask open
            //save the mask encrpyted as the new value, this is triggered when the user modifies the data
            attributes.value = this.corpus.confidential.encrypt(attributes.mask);
            /*
             * B: A strange case which is used by the Datum Save function, to trigger the mask into the xxx version of the current value that it will be saved in the data base with xxx.
             */
          } else if (attributes.mask.indexOf("confidential:") === 0) {
            attributes.mask = this.mask(this.corpus.confidential.decrypt(this.value));
            attributes.value = this.value; //don't let the user modify the value.
          }
 
          /*
           * C & D: this should never be called since the value is supposed to come from the mask only.
           */
 
          /*
           * C: If the value wasn't encrypted, encrypt the value, and leave the mask as the original value since we are in decryptedMode
           */
          if (attributes.value && attributes.value.indexOf("confidential") !== 0) {
            //          attributes.mask = attributes.mask;//leave mask open
            attributes.value = this.corpus.confidential.encrypt(attributes.mask);
            /*
             * D: If the value was encrypted, there is some sort of bug, leave the value as it was, decrypt it and put it in to the mask since we are in decryptedMode
             */
          } else if (attributes.value && attributes.value.indexOf("confidential") === 0) {
            // If it was encrypted, turn the mask into the decrypted version of the current value so the user can see it.
            //this might get called at the same time as the first mask if above
            attributes.mask = this.corpus.confidential.decrypt(this.value);
            attributes.value = this.value; //don't let the user modify the value.
          }
 
          /*
           * E, F, G, H: If we are supposed to be encrypted and we are encrypted, but we are not in decrypted mode.
           */
        } else {
 
          //Don't let the user take off encryption if they are not in decryptedMode
          if (this.encrypted === "checked") {
            if (true && attributes.encrypted !== "checked" && !this.corpus.confidential.decryptedMode) {
              attributes.encrypted = "checked";
            }
          }
 
          /*
           * E: A strange case which is used by the Datum Save function, to trigger the mask into the xxx version of the current value that it will be saved in the data base with xxx.
           *  (Same as B above)
           */
          if (attributes.mask && attributes.mask.indexOf("confidential") === 0) {
            attributes.mask = this.mask(this.corpus.confidential.decrypt(this.value));
            attributes.value = this.value; //don't let the user modify the value.
            /*
             * F: If the value is encrypted, then the mask is probably set, don't let the user change anything since they shouldn't be able to see the data anyway.s
             */
          } else {
            //Don't let user change value of confidential or mask: see validate function
            attributes.mask = this.mask;
            attributes.value = this.value;
          }
 
          /*
           * G: If the data is not encrypted, encrypt it and mask it in the mask. This might be called the first time a user clicks on the lock to first encrypts the value.
           * (Similar to C above, except that we mask the mask)
           */
          if (attributes.value && attributes.value.indexOf("confidential") !== 0) {
            attributes.mask = this.mask(this.value); //use value to make mask
            attributes.value = this.corpus.confidential.encrypt(this.value);
            /*
             * H: If the value is encrypted, then the mask is probably set, don't let the user change anything since they shouldn't be able to see the data anyway.s
             */
          } else {
            //Don't let user change value of confidential or mask: see validate function
            attributes.mask = this.mask;
            attributes.value = this.value;
          }
        }
      } else {
        //        alert("The datum field has no mask, there is a bug somewhere.");
        //        attributes.value ="";
        //        attributes.mask = "";
      }
      return attributes;
    }
  },
 
  createMask: {
    value: function(stringToMask) {
      stringToMask = stringToMask || "";
      stringToMask = stringToMask + "";
      return stringToMask.replace(/[^_=., -]/g, "x");
    }
  },
 
  saveAndInterConnectInApp: {
    value: function(callback) {
      if (typeof callback === "function") {
        callback();
      }
    }
  },
 
  confidential: {
    get: function() {
      return this.confidentialEncrypter;
    },
    set: function(value) {
      if (value === this.confidentialEncrypter) {
        return;
      }
      if (typeof value.encrypt !== "function" && value.secretkey) {
        value = new Confidential(value);
      }
      this.confidentialEncrypter = value;
    }
  },
 
  toJSON: {
    value: function(includeEvenEmptyAttributes, removeEmptyAttributes) {
      this.debug("Customizing toJSON ", includeEvenEmptyAttributes, removeEmptyAttributes);
      includeEvenEmptyAttributes = true;
      var json = FieldDBObject.prototype.toJSON.apply(this, arguments);
      delete json.dateCreated;
      delete json.dateModified;
      delete json.comments;
      delete json.dbname;
 
      // TODO eventually dont include the label and hint but now include it for backward compaitibilty
      json.label = this.id;
      // json.hint = this.hint || "";
      Eif (!json.hint) {
        delete json.hint;
      }
 
      json.value = this.value || "";
      json.mask = this.mask || "";
 
      if (removeEmptyAttributes) {
        Eif (json.mask === json.value) {
          delete json.mask;
        }
        if (json.encryptedValue === json.value) {
          delete json.encryptedValue;
        }
        this.debug("removeEmptyAttributes", json);
      }
 
      Eif (json.helpNonLinguists === json.help) {
        delete json.helpNonLinguists;
      }
      if (json.helpDevelopers === json.help) {
        delete json.helpDevelopers;
      }
      if (json.helpLinguists === json.help) {
        delete json.helpLinguists;
      }
      if (json.helpComputationalLinguists === json.help) {
        delete json.helpComputationalLinguists;
      }
      if (json.helpTranslators === json.help) {
        delete json.helpTranslators;
      }
 
      if (json.labelTranslators === json.label || json.labelTranslators === json.labelFieldLinguists) {
        delete json.labelTranslators;
      }
      Iif (json.labelExperimenters === json.label) {
        delete json.labelExperimenters;
      }
      if (json.labelNonLinguists === json.label || json.labelNonLinguists === json.labelFieldLinguists) {
        delete json.labelNonLinguists;
      }
      if (json.labelFieldLinguists === json.label) {
        delete json.labelFieldLinguists;
      }
      if (json.labelPsychoLinguists === json.label || json.labelPsychoLinguists === json.labelFieldLinguists) {
        delete json.labelPsychoLinguists;
      }
      if (json.labelComputationalLinguist === json.label || json.labelComputationalLinguist === json.labelFieldLinguists) {
        delete json.labelComputationalLinguist;
      }
 
 
      json.id = this.id;
      delete json._id;
 
      json.fieldDBtype = this.fieldDBtype;
      delete json._type;
 
      this.debug("json", json);
      return json;
    }
  }
 
});
 
exports.DatumField = DatumField;