Code coverage report for api/corpus/CorpusMask.js

Statements: 72.28% (219 / 303)      Branches: 66.3% (179 / 270)      Functions: 68.66% (46 / 67)      Lines: 72.28% (219 / 303)      Ignored: none     

All files » api/corpus/ » CorpusMask.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 7741 1 1 1 1 1 1 1 1 1 1 1 1 1 1   1                                                                                                       1 80 14   80 80     1             42 42                                   3 3                                                                               39 22   39   39     39       39 39           182 17   182     48 39     9 1               351 287   64       60 22   60 60 32   28               23 17   6       18 13   18             1 1                                     36 4         4 2 2   4       36     16           63 63 5 5 5 5   63 5   63     41 41 41 7     41                                                                                             5 1   4       2 1   2 1 1     2               7 1   6           2 1 1   2         2             5 1   4       2 2   2                                                             8 8   8 8 8 8 8 8 3 3 3 3 3 3   5     8                 24     11                                                                                                                     2385 2385     58 55   58           62 62     46 43   46           77 77     34 31   34           21 21     6 5   6           49 49     37 32   37           1436 1436     87 78   87           102 9   102     23           6                   13     13 7     7   6 6 6     6             6     6       6 6 6 6 6 6 1   5 1   4 1   3 1   2 2 2 2 2     6                   6 44 44 13   13 13 13   44 30   14   14     6 67   6   6                     1     1           1 1                     1     1           1 1                                 3   3 3         3 3 3 3 3 3   3 3         1  
var Confidential = require("./../confidentiality_encryption/Confidential").Confidential;
var Activities = require("./../activity/Activities").Activities;
var Database = require("./Database").Database;
var Connection = require("./Connection").Connection;
var DatumFields = require("./../datum/DatumFields").DatumFields;
var DatumStates = require("./../datum/DatumStates").DatumStates;
var DatumTags = require("./../datum/DatumTags").DatumTags;
var Comments = require("./../comment/Comments").Comments;
var FieldDBObject = require("./../FieldDBObject").FieldDBObject;
var Sessions = require("./../datum/DocumentCollection").DocumentCollection;
var DataLists = require("./../datum/DocumentCollection").DocumentCollection;
var TeamPreference = require("./../user/UserPreference").UserPreference;
var Team = require("./../user/Team").Team;
var Permissions = require("./../permission/Permissions").Permissions;
var Q = require("q");
 
var DEFAULT_CORPUS_MODEL = require("./corpus_mask.json");
/**
 * @class The CorpusMask is saved as corpus in the Couch repository,
 *        it is the publicly visible version of a corpus. By default it just says "This
 *        corpus is private," when users decide to make some aspects of their corpsu
 *        public they can customize the fields in their "corpus" object to display
 *        only certain sorts of data, even though the corpus is publicly discoverable.
 *
 * @property {String} title This is the title of the corpus, as set by the corpus
 *           team. It can contain any UTF-8 character.
 * @property {String} titleAsUrl This is what appears in the url on the main website.
 *           It is based on the title of the corpus and can be changed and looks
 *           nicer than the dbname which cannot be changed. eg
 *           http://fieldlinguist.com/LingLlama/SampleFieldLinguisticsCorpus
 * @property {String} description This is a short description that
 *           appears on the corpus details page
 * @property {Object} termsOfUse Terms of use set by the corpus team, includes
 *           a field for humanReadable terms which are displayed on the corpus
 *           and included in corpus exports.
 * @property {Object} license License set by the corpus team, includes a field
 *           for humanReadable terms which are displayed on the corpus and
 *           included in corpus exports, as well as a link to the license, imageUrl
 *           for the image/logo of the license for easy recognition and
 *           title of the license.
 * @property {Object} copyright Who owns the copyright to the corpus,
 *           by default it is set to the corpus team"s name but teams can customize
 *           it for example to make the corpus copyright of the language community
 *           or speakers who contributed to the corpus.
 * @property {Object} location GPS location of the corpus (longitude, latitude and accuracy)
 *           The corpus can be plotted on a map using the accuracy as a radius
 *           of roughly where the data is from.
 * @property {String} remote The url of the remote eg:
 *           git@fieldlinguist.com:LingLlama/SampleFieldLinguisticsCorpus.git
 * @property {Array} connection The url of remote server(s) where the
 *           corpus is replicated or backed up as well as other connection it is connected to.
 *
 * @property {Array} members Collection of public browsable/search engine
 *           discoverable members associated to the corpus
 * @property {Array} datumstates Collection of datum states for which data are
 *           will be public browsable/search engine discoverable in the corpus. This
 *           can be used to only show polished data or "checked" data on the public
 *           page for example.
 * @property {Array} datumfields Collection of datum fields which will be
 *           public browsable/search engine discoverable  on public datum in the corpus
 * @property {Array} sessions Collection of public browsable/search engine
 *           discoverable sessions that belong to the corpus
 * @property {Array} datalists Collection of public browsable/search engine
 *           discoverable data lists created under the corpus
 *
 * @extends Database
 * @tutorial tests/CorpusMaskTest.js
 */
var CorpusMask = function CorpusMask(options) {
  if (!this._fieldDBtype) {
    this._fieldDBtype = "CorpusMask";
  }
  this.debug(options);
  Database.apply(this, arguments);
};
 
CorpusMask.prototype = Object.create(Database.prototype, /** @lends CorpusMask.prototype */ {
  constructor: {
    value: CorpusMask
  },
 
  id: {
    get: function() {
      this._id = "corpus";
      return this._id;
    },
    set: function(value) {
      if (value === this._id) {
        return;
      }
      this.warn("CorpusMask id cannot be set, it is \"corpus\" by default." + value);
      value = "corpus";
      this._id = value;
    }
  },
 
  api: {
    value: "corpora"
  },
 
  defaults: {
    get: function() {
      var filteredCorpus = JSON.parse(JSON.stringify(DEFAULT_CORPUS_MODEL));
      return filteredCorpus;
    }
  },
 
  INTERNAL_MODELS: {
    value: {
      _id: FieldDBObject.DEFAULT_STRING,
      _rev: FieldDBObject.DEFAULT_STRING,
      dbname: FieldDBObject.DEFAULT_STRING,
      version: FieldDBObject.DEFAULT_STRING,
      dateCreated: FieldDBObject.DEFAULT_DATE,
      dateModified: FieldDBObject.DEFAULT_DATE,
      comments: Comments,
      sessions: Sessions,
      datalists: DataLists,
 
      titleAsUrl: FieldDBObject.DEFAULT_STRING,
      connection: Connection,
      activityConnection: Activities,
      publicCorpus: FieldDBObject.DEFAULT_STRING,
      confidential: Confidential,
 
      validationStati: DatumStates,
      tags: DatumTags,
 
      fields: DatumFields,
      datumFields: DatumFields,
      speakerFields: DatumFields,
      participantFields: DatumFields,
      conversationFields: DatumFields,
      sessionFields: DatumFields,
 
      prefs: TeamPreference,
      team: Team,
      permissions: Permissions
    }
  },
 
  addField: {
    value: function(field) {
      if (!this.fields) {
        this.fields = [];
      }
      Eif (typeof field === "string") {
        // this.fields.debugMode = true;
        Iif (this.fields[field]) {
          return this.fields[field];
        }
        field = {
          id: field
        };
      }
      this.debug("adding field", field);
      return this.fields.add(field);
    }
  },
 
  titleAsUrl: {
    get: function() {
      if (!this._titleAsUrl && this.title) {
        this._titleAsUrl = this.sanitizeStringForFileSystem(this.title, "_").toLowerCase();
      }
      return this._titleAsUrl;
    },
    set: function(value) {
      if (value === this._titleAsUrl) {
        return;
      }
      // If an app is explicity trying to overwrite a titleAsUrl, complain.
      if (this._titleAsUrl) {
        this.warn("titleAsUrl cannot be set directly, setting the title will cause it to be set.");
      }
    }
  },
 
  title: {
    configurable: true,
    get: function() {
      if (this.fields && this.fields.title) {
        return this.fields.title.value;
      } else {
        return FieldDBObject.DEFAULT_STRING;
      }
    },
    set: function(value) {
      if (!this.fields || !this.fields.title) {
        this.addField("title");
      }
      this.fields.title.value = value;
      if (value) {
        this._titleAsUrl = this.sanitizeStringForFileSystem(value, "_").toLowerCase();
      } else {
        this._titleAsUrl = this.dbname;
      }
    }
  },
 
  description: {
    configurable: true,
    get: function() {
      if (this.fields && this.fields.description) {
        return this.fields.description.value;
      } else {
        return FieldDBObject.DEFAULT_STRING;
      }
    },
    set: function(value) {
      if (!this.fields || !this.fields.description) {
        this.addField("description");
      }
      this.fields.description.value = value;
    }
  },
 
  keywords: {
    configurable: true,
    get: function() {
      Eif (this.fields && this.fields.keywords) {
        return this.fields.keywords.value;
      } else {
        return FieldDBObject.DEFAULT_STRING;
      }
    },
    set: function(value) {
      if (!this.fields || !this.fields.keywords) {
        this.addField("keywords");
      }
      this.fields.keywords.value = value;
    }
  },
 
  /**
   * TODO decide if we want to fetch these from the server, and keep a fossil in the object?
   * @type {Object}
   */
  team: {
    get: function() {
      if (!this._team) {
        this.team = {
          gravatar: "",
          username: "",
          _id: "team"
        };
        if (this.dbname) {
          this._team.gravatar = this._team.gravatar || this.gravatar || this._team.buildGravatar(this.dbname);
          this._team.username = this._team.username || this.dbname.split("-")[0];
        }
        Iif (this.id && this.rev) {
          this._team.fetch();
        }
      }
      return this._team;
    },
    set: function(value) {
      this.ensureSetViaAppropriateType("team", value);
    }
  },
 
  activityConnection: {
    get: function() {
      this.debug("getting activityConnection");
      if (!this._activityConnection && this.rev && this._connection) {
        this.warn("This corpus is missing its activity connection for some reason.");
        var copy = this._connection.toJSON();
        copy.dbname = copy.dbname + "-activity_feed";
        this.activityConnection = copy;
      }
      if (this._activityConnection && this._activityConnection.parent !== this) {
        this._activityConnection.parent = this;
      }
      return this._activityConnection;
    },
    set: function(value) {
      Eif (value) {
        value.parent = this;
        if (!value.confidential && this.confidential) {
          value.confidential = this.confidential;
        }
      }
      this.ensureSetViaAppropriateType("activityConnection", value);
    }
  },
 
  replicatedCorpusUrls: {
    get: function() {
      if (this._connection && this._connection.replicatedCorpusUrls) {
        return this._connection.replicatedCorpusUrls;
      }
      return FieldDBObject.DEFAULT_COLLECTION;
    },
    set: function(value) {
      if (this._connection) {
        this._connection.replicatedCorpusUrls = value;
      }
    }
  },
 
  olacExportConnections: {
    get: function() {
      if (this._connection && this._connection.olacExportConnections) {
        return this._connection.olacExportConnections;
      }
      return FieldDBObject.DEFAULT_COLLECTION;
    },
    set: function(value) {
      if (this._connection) {
        this._connection.olacExportConnections = value;
      }
    }
  },
 
  terms: {
    get: function() {
      this.warn("terms is deprecated, use termsOfUse instead.");
    },
    set: function(value) {
      this.warn("terms is deprecated, use termsOfUse instead.");
      if (!this.termsOfUse) {
        this.termsOfUse = value;
      }
    }
  },
 
  termsOfUse: {
    configurable: true,
    get: function() {
      if (this.fields && this.fields.rights) {
        return this.fields.rights.value;
      } else {
        return FieldDBObject.DEFAULT_STRING;
      }
    },
    set: function(value) {
      if (!this.fields || !this.fields.rights) {
        this.addField("rights");
      }
      if (value.humanReadable) {
        value = value.humanReadable;
      } else Iif (typeof value === "object") {
        value = "";
      }
      this.fields.rights.value = value;
    }
  },
 
  license: {
    configurable: true,
    get: function() {
      // console.log('get license', this.fields);
      if (this.fields && this.fields.rights && this.fields.rights.json && this.fields.rights.json.license) {
        return this.fields.rights.json.license;
      } else {
        return FieldDBObject.DEFAULT_OBJECT;
      }
    },
    set: function(value) {
      // console.log('set license', this.fields, value);
      // throw (new Error('something is setting license'))
      if (!this.fields || !this.fields.rights) {
        this.addField("rights");
        this.fields.rights.json = {};
      }
      Iif (typeof value === "string") {
        value = {
          humanReadable: value
        };
      }
      this.fields.rights.json.license = value;
    }
  },
 
  copyright: {
    configurable: true,
    get: function() {
      if (this.fields && this.fields.rightsHolder) {
        return this.fields.rightsHolder.value;
      } else {
        return FieldDBObject.DEFAULT_STRING;
      }
    },
    set: function(value) {
      Eif (!this.fields || !this.fields.rightsHolder) {
        this.addField("rightsHolder");
      }
      this.fields.rightsHolder.value = value;
    }
  },
 
  startYear: {
    get: function() {
      if (this.parent && this.parent.dateCreated && this.parent._rev && this.parent._fieldDBtype === "Corpus") {
        this.dateCreated = this.parent.dateCreated;
      }
      if (this.dateCreated) {
        var year = new Date(this.dateCreated).getFullYear();
        if (year < new Date().getFullYear()) {
          this._startYear = year;
        }
      }
      return this._startYear;
    },
    set: function(value) {
      if (value === this._startYear) {
        return;
      }
      if (!value) {
        // delete this._startYear;
        return;
      }
      this._startYear = value;
    }
  },
 
  fetch: {
    value: function(optionalUrl) {
      var self = this;
      var deferred = Q.defer();
 
      this.debug("Customizing fetch ", optionalUrl);
      var fetchPromise = Database.prototype.fetch.apply(this, arguments);
      fetchPromise.then(deferred.resolve);
      fetchPromise.fail(function(err) {
        self.debug("Fetching CorpusMask failed", err);
        if (err.status === 404 && /deleted|missing/.test(err.userFriendlyErrors[0])) {
          self.warn("Repairing corpus which is missing its corpus doc: " + err.userFriendlyErrors.join(" "));
          self.loaded = true;
          var newCorpusMask = FieldDBObject.convertDocIntoItsType(self.defaults);
          newCorpusMask.dbname = self.dbname;
          self.merge("self", newCorpusMask, "overwrite");
          return deferred.resolve(self);
        }
        deferred.reject(err);
      });
 
      return deferred.promise;
    }
  },
 
  // unserializedSessions: {
  //   value: null
  // },
  sessions: {
    get: function() {
      return this.unserializedSessions || FieldDBObject.DEFAULT_COLLECTION;
    },
    set: function(value) {
      this.ensureSetViaAppropriateType("sessions", value, "unserializedSessions");
    }
  },
 
  // unserializedDatalists: {
  //   value: null
  // },
  datalists: {
    get: function() {
      return this.unserializedDatalists || FieldDBObject.DEFAULT_COLLECTION;
    },
    set: function(value) {
      this.ensureSetViaAppropriateType("datalists", value, "unserializedDatalists");
    }
  },
 
  permissions: {
    get: function() {
      if (!this._permissions) {
        this.permissions = {
          dbname: this.dbname,
          parent: this
        };
      }
      return this._permissions || FieldDBObject.DEFAULT_COLLECTION;
    },
    set: function(value) {
      this.ensureSetViaAppropriateType("permissions", value);
    }
  },
 
  loadPermissions: {
    value: function(dataToPost) {
      var deferred = Q.defer(),
        self = this;
 
      Q.nextTick(function() {
 
        if (!self.permissions || !(self.permissions instanceof Permissions)) {
          self.permissions = new Permissions(self.permissions);
        }
        if (!self.permissions.dbname) {
          self.permissions.dbname = self.dbname;
        }
        self.permissions.parent = self;
        self.permissions.fetch(dataToPost)
          .then(deferred.resolve, deferred.reject)
          .fail(function(error) {
            console.error(error.stack, self);
            deferred.reject(error);
          });
 
      });
      return deferred.promise;
    }
  },
 
  datumFields: {
    get: function() {
      this.debug("getting datumFields");
      return this._datumFields;
    },
    set: function(value) {
      if (value && !value.confidential && this.confidential) {
        value.confidential = this.confidential;
      }
      this.ensureSetViaAppropriateType("datumFields", value);
    }
  },
 
  sessionFields: {
    get: function() {
      this.debug("getting sessionFields");
      return this._sessionFields;
    },
    set: function(value) {
      if (value && !value.confidential && this.confidential) {
        value.confidential = this.confidential;
      }
      this.ensureSetViaAppropriateType("sessionFields", value);
    }
  },
 
  speakerFields: {
    get: function() {
      this.debug("getting speakerFields");
      return this._speakerFields;
    },
    set: function(value) {
      if (value && !value.confidential && this.confidential) {
        value.confidential = this.confidential;
      }
      this.ensureSetViaAppropriateType("speakerFields", value);
    }
  },
 
  participantFields: {
    get: function() {
      this.debug("getting participantFields");
      return this._participantFields;
    },
    set: function(value) {
      if (value && !value.confidential && this.confidential) {
        value.confidential = this.confidential;
      }
      this.ensureSetViaAppropriateType("participantFields", value);
    }
  },
 
  conversationFields: {
    get: function() {
      this.debug("getting conversationFields");
      return this._conversationFields;
    },
    set: function(value) {
      if (value && !value.confidential && this.confidential) {
        value.confidential = this.confidential;
      }
      this.ensureSetViaAppropriateType("conversationFields", value);
    }
  },
 
  fields: {
    get: function() {
      this.debug("getting fields");
      return this._fields;
    },
    set: function(value) {
      if (value && !value.confidential && this.confidential) {
        value.confidential = this.confidential;
      }
      this.ensureSetViaAppropriateType("fields", value);
    }
  },
 
  prefs: {
    get: function() {
      if (!this._prefs && this.INTERNAL_MODELS && this.INTERNAL_MODELS.prefs && this.INTERNAL_MODELS.prefs.prototype && this.INTERNAL_MODELS.prefs.prototype.defaults) {
        this.prefs = this.INTERNAL_MODELS.prefs.prototype.defaults;
      }
      return this._prefs;
    },
    set: function(value) {
      this.ensureSetViaAppropriateType("prefs", value);
    }
  },
 
  preferredDatumTemplate: {
    get: function() {
      Iif (this.prefs && this.prefs.preferredDatumTemplate) {
        var upgradeSucess = this.upgradeCorpusFieldsToMatchDatumTemplate(this.prefs.preferredDatumTemplate);
        if (!upgradeSucess) {
          return this.prefs.preferredDatumTemplate;
        } else {
          this.warn("preferredDatumTemplate is no longer needed");
        }
      }
    },
    set: function(value) {
      Iif (this.prefs && this.prefs.preferredDatumTemplate && value === this.prefs.preferredDatumTemplate) {
        return;
      }
      if (!value || value === "default") {
        Iif (this.prefs && this.prefs.preferredDatumTemplate) {
          delete this.prefs.preferredDatumTemplate;
        }
        return;
      }
      this.prefs = this.prefs || {};
      var upgradeSucess = this.upgradeCorpusFieldsToMatchDatumTemplate(value.trim());
      Iif (!upgradeSucess) {
        this.prefs.preferredDatumTemplate = value.trim();
      } else {
        this.warn("preferredDatumTemplate is no longer needed");
      }
    }
  },
 
  upgradeCorpusFieldsToMatchDatumTemplate: {
    value: function(value) {
      Iif (this.preferredDatumTemplateAtVersion) {
        return true;
      }
      Iif (!this.datumFields.reorder) {
        this.warn("could not upgrade corpus fields order to match data entry template for the spreadsheet app ");
        return false;
      }
      this.preferredDatumTemplateAtVersion = this.version;
      this.preferredDatumTemplate = null;
      this.prefs = this.prefs || {};
      this.prefs.debug(" this.prefs.preferredSpreadsheetShape.rows ", this.prefs.preferredSpreadsheetShape.rows);
      var order = ["judgement", "utterance", "morphemes", "gloss", "translation", "context", "documentation"];
      if (value === "compacttemplate") {
        order = ["judgement", "utterance", "morphemes", "gloss", "translation"];
 
      } else if (value === "fulltemplate") {
        order = ["judgement", "utterance", "morphemes", "gloss", "translation", "context", "documentation"];
 
      } else if (value === "mcgillfieldmethodsspring2014template") {
        order = ["judgement", "utterance", "morphemes", "gloss", "translation", "validationStatus", "tags"];
 
      } else if (value === "mcgillfieldmethodsfall2014template") {
        order = ["judgement", "utterance", "morphemes", "gloss", "translation", "phonetic", "notes"];
 
      } else Eif (value === "yalefieldmethodsspring2014template") {
        order = ["judgement", "orthography", "utterance", "morphemes", "gloss", "translation", "spanish", "Housekeeping", "tags"];
        this.prefs.debug("setting to yalefieldmethodsspring2014template");
        this.prefs.preferredSpreadsheetShape = this.prefs.preferredSpreadsheetShape;
        this.prefs.preferredSpreadsheetShape.rows = 4;
      }
 
      var fieldTemplate = {
        "label": "",
        "shouldBeEncrypted": false,
        "showToUserTypes": "all",
        "defaultfield": false,
        "value": "",
        "mask": "",
        "json": {},
        "help": "You can add help/conventions here which explain what this data entry field is for your teammates."
      };
      for (var expectedPosition = 0; expectedPosition < order.length; expectedPosition++) {
        var currentPosition = this.datumFields.indexOf(order[expectedPosition]);
        if (currentPosition === -1) {
          var newField = JSON.parse(JSON.stringify(fieldTemplate));
          // newField.id = order[expectedPosition];
          newField.label = order[expectedPosition];
          this.datumFields.add(newField);
          currentPosition = this.datumFields.length - 1;
        }
        if (currentPosition === expectedPosition) {
          this.debug(order[expectedPosition] + " Field was in the correct order. ");
        } else {
          this.debug(currentPosition + " moving to" + expectedPosition);
 
          this.datumFields.reorder(currentPosition, expectedPosition);
        }
      }
      var resultingOrder = this.datumFields.map(function(field) {
        return field.id;
      });
      this.warn("reordered fields to " + resultingOrder);
 
      return true;
    }
  },
 
  preferredLocale: {
    get: function() {
      if (this.prefs && this.prefs.preferredLocale) {
        return this.prefs.preferredLocale;
      }
    },
    set: function(value) {
      Iif (this.prefs && this.prefs.preferredLocale && value === this.prefs.preferredLocale) {
        return;
      }
      Iif (!value || value === "default") {
        if (this.prefs && this.prefs.preferredLocale) {
          delete this.prefs.preferredLocale;
        }
        return;
      }
      this.prefs = this.prefs || {};
      this.prefs.preferredLocale = value.trim();
    }
  },
 
  preferredDashboardLayout: {
    get: function() {
      if (this.prefs && this.prefs.preferredDashboardLayout) {
        return this.prefs.preferredDashboardLayout;
      }
    },
    set: function(value) {
      Iif (this.prefs && this.prefs.preferredDashboardLayout && value === this.prefs.preferredDashboardLayout) {
        return;
      }
      Iif (!value || value === "default") {
        if (this.prefs && this.prefs.preferredDashboardLayout) {
          delete this.prefs.preferredDashboardLayout;
        }
        return;
      }
      this.prefs = this.prefs || {};
      this.prefs.preferredDashboardLayout = value.trim();
    }
  },
 
  preferredTemplate: {
    get: function() {
      this.warn("preferredTemplate is deprecated, use preferredDatumTemplate instead.");
      return this.preferredDatumTemplate;
    },
    set: function(value) {
      this.warn("preferredTemplate is deprecated, please use preferredDatumTemplate instead.");
      this.preferredDatumTemplate = value;
    }
  },
 
  toJSON: {
    value: function(includeEvenEmptyAttributes, removeEmptyAttributes) {
      this.debug("Customizing toJSON ", includeEvenEmptyAttributes, removeEmptyAttributes);
 
      var json = Database.prototype.toJSON.apply(this, arguments);
      Iif (!json) {
        this.warn("Not returning json right now.");
        return;
      }
 
      this.debug("saving description, title, termsOfUse, license  as the deprecated attribute");
      json.title = this.title;
      json.description = this.description;
      json.termsOfUse = this.termsOfUse;
      json.license = this.license;
      json.copyright = this.copyright;
 
      this.debug(json);
      return json;
    }
  }
 
});
exports.CorpusMask = CorpusMask;