Code coverage report for api/datum/Session.js

Statements: 55.84% (153 / 274)      Branches: 57.79% (115 / 199)      Functions: 60.76% (48 / 79)      Lines: 55.84% (153 / 274)      Ignored: none     

All files » api/datum/ » Session.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 8111 1 1 1   1                                                                                                                                                 1 99 99     99 99 99   92     99 14   99 99 99     1                                               1     2 2             25 9   16       36     3   36     36           1                     2 2     2 1   2                                                                           3     1 1           1207 1207     189     189           99     99                                                 1 1                                                                         7 7 7 18 13     7 2     7     6                     6 1 1       1 3 3             6     6 6 5     5   5 5     6           4 4 4 17 9     4       4     3       3 2 1   1   2 4 4               3     3 3 5     5   5 5     3                   189                                                                                                                                                     29 25         16 2   16           44 43       1         1 1     1     1 1     1 1 1 1       1 1 1                       2                 2                 2                 2                 2                 2                 2                 4                                   11       11 11 8     3 3     3 3 3   3             24 24                     59 59               59 23                 59   59 5     59 59       59           21 17               682       682     36                                                                                                                                                     16 16 16               16 16         1  
var Confidential = require("./../confidentiality_encryption/Confidential").Confidential;
var DatumFields = require("./DatumFields").DatumFields;
var DataList = require("./../data_list/DataList").DataList;
var FieldDBObject = require("./../FieldDBObject").FieldDBObject;
 
var DEFAULT_CORPUS_MODEL = require("./../corpus/corpus.json");
/**
  * @class The Session widget is the place where information which is generally
  * shared by many datum (due to being part of an elicitiation session)
  * @property {Number} sessionID The session ID is an automatically generated
  *           number which will uniquely identify the session.
  * @property {String} user The user is the person inputting the data for
  *           that session.
  * @property {String} team The team is the team that the user belongs to.
  * @property {String} consultant The consultant is the native speaker of the
  *           language under investigation that has verified the data in the
  *           session.
  * @property {String} language The language is the language under
  *           investigation in the particular session.
  * @property {String} languageFamily The language family is an attribute
  *           which users can use to group languages.
  * @property {String} dialect The dialect specifies the dialect of the
  *           language under investigation.
  * @property {String} date The date is the date that the data was elicited.
  * @property {String} goal The goal is the particular linguistic goal that
  *           the researcher was pursuing during that session.
  *
  *  new DatumField({
        label : "user",
        shouldBeEncrypted: "",
        userchooseable: "disabled"
      }),
      new DatumField({
        label : "consultants",
        shouldBeEncrypted: "",
        userchooseable: "disabled"
      }),
      new DatumField({
        label : "language",
        shouldBeEncrypted: "",
        userchooseable: "disabled",
        help: "This is the langauge (or language family) if you would like to use it."
      }),
      new DatumField({
        label : "dialect",
        shouldBeEncrypted: "",
        userchooseable: "disabled",
        help: "You can use this field to be as precise as you would like about the dialect of this session."
      }),
      new DatumField({
        label : "dateElicited",
        shouldBeEncrypted: "",
        userchooseable: "disabled",
        help: "This is the date in which the session took place."
      }),
      new DatumField({
        label : "dateSEntered",
        shouldBeEncrypted: "",
        userchooseable: "disabled",
        help: "This is the date in which the session was entered."
      }),
      new DatumField({
        label : "goal",
        shouldBeEncrypted: "",
        userchooseable: "disabled",
        help: "This describes the goals of the session."
      }),
  *
  *
  *
  * @description The initialize function brings up a page in which the user
  *              can fill out the details corresponding to the session. These
  *              details will be linked to each datum submitted in the
  *              session.
  * @name  Session
  * @extends FieldDBObject
  * @constructs
*/
var Session = function Session(options) {
  Eif (!this._fieldDBtype) {
    this._fieldDBtype = "Session";
  }
  // this.debugMode = true;
  this.debug("Constructing Session: ", options);
  options = options || {};
  if (!options || (!options._rev && !options.fields)) {
    //If its a new session with out a revision and without fields use the defaults
    options.fields = this.defaults.fields;
  }
  // Dont Initialize the datalist to avoid each datum causing each session to run queries on the server needlessly. and docs to ensure its ready incase we want to use them.
  if (options.docIds || options.docs) {
    this.initializeDatalist();
  }
  this.debug(" -> ", options.datalist);
  FieldDBObject.apply(this, arguments);
  this.debug("   after construction: ", this._datalist);
};
 
Session.prototype = Object.create(FieldDBObject.prototype, /** @lends Session.prototype */ {
  constructor: {
    value: Session
  },
 
  dateAndGoalSnippet: {
    get: function() {
      var goal = this.goal;
      if (goal.length > 31) {
        goal = goal.substr(0, 30) + "...";
      }
      var dateElicited = this.dateElicited;
      if (dateElicited.length > 16) {
        dateElicited = dateElicited.substr(0, 15) + "...";
      }
      return dateElicited + " : " + goal;
    },
    set: function() {
      this.debug("cant set the snippet.");
    }
  },
 
  title: {
    get: function() {
      return this.goal;
    },
    set: function(value) {
      this.warn("title is syntactic sugar for goal, if it was used this will make it overwrite the goal field");
      return this.goal = value;
    }
  },
 
  goal: {
    configurable: true,
    get: function() {
      if (this.fields && this.fields.goal) {
        return this.fields.goal.value;
      } else {
        return FieldDBObject.DEFAULT_STRING;
      }
    },
    set: function(value) {
      if (this.fields && this.fields.goal) {
        // this.fields.debugMode = true;
      } else {
        this.fields = this.defaults.fields;
      }
      Iif (!value || (value.indexOf && value.indexOf("Change this session") > -1)) {
        value = "Practice collecting linguistic utterances or words";
      }
      this.fields.goal.value = value;
    }
  },
 
  date: {
    get: function() {
      return this.dateElicited;
    },
    set: function(value) {
      this.warn("date is syntactic sugar for dateElicited");
      return this.dateElicited = value;
    }
  },
 
  dateElicited: {
    configurable: true,
    get: function() {
      Eif (this.fields && this.fields.dateElicited) {
        Iif (this.fields.dateElicited.value && this.fields.dateElicited.value.indexOf && this.fields.dateElicited.value.indexOf("Change this to a tim") > -1) {
          this.fields.dateElicited.value = "Probably prior to " + new Date(this.dateCreated);
        }
        if (!this.fields.dateElicited.value && this.dateCreated) {
          this.fields.dateElicited.value = new Date(this.dateCreated) + "";
        }
        return this.fields.dateElicited.value;
      } else {
        return FieldDBObject.DEFAULT_STRING;
      }
    },
    set: function(value) {
      if (this.fields) {
        // this.fields.debugMode = true;
      } else {
        this.fields = this.defaults.fields;
      }
      if (value && value.indexOf && value.indexOf("Change this to a tim") > -1) {
        value = "Probably prior to " + new Date(this.dateCreated);
      }
      this.fields.dateElicited.value = value;
    }
  },
 
  // The couchdb-connector is capable of mapping the url scheme
  // proposed by the authors of Backbone to documents in your database,
  // so that you don't have to change existing apps when you switch the sync-strategy
  api: {
    value: "sessions"
  },
 
  // Internal models: used by the parse function
  INTERNAL_MODELS: {
    value: {
      fields: DatumFields,
      confidential: Confidential,
      docs: FieldDBObject.DEFAULT_COLLECTION,
      docIds: FieldDBObject.DEFAULT_COLLECTION,
      datalist: DataList
    }
  },
 
  sessionFields: {
    get: function() {
      return this.fields;
    },
    set: function(value) {
      this.warn("sessionFields are depreacted, use fields instead " + this.id);
      return this.fields = value;
    }
  },
 
  fields: {
    get: function() {
      this.debug("getting fields " + this.id);
      return this._fields;
    },
    set: function(value) {
      Iif (value && !value.confidential && this.confidential) {
        value.confidential = this.confidential;
      }
      this.ensureSetViaAppropriateType("fields", value);
    }
  },
 
  defaults: {
    get: function() {
      var doc = {
        fields: DEFAULT_CORPUS_MODEL.sessionFields
      };
      return JSON.parse(JSON.stringify(doc));
    },
    set: function() {}
  },
 
  confidentiality: {
    get: function() {
      if (this.fields) {
        return this.fields.confidentiality.value;
      } else {
        return;
      }
    },
    set: function(value) {
      if (!this.fields) {
        this.fields = this.defaults.fields;
      }
      // this.warn("Cannot change the public/private of " + this.collection + " (it must be anonymous). " + value);
      this.fields.confidentiality.value = value;
    }
  },
 
  participants: {
    configurable: true,
    get: function() {
      Eif (this.fields && this.fields.participants && this.fields.participants.json && this.fields.participants.json.users) {
        return this.fields.participants.json.users;
      } else {
        return;
      }
    },
    set: function(value) {
      if (!this.fields) {
        this.fields = this.defaults.fields;
      }
      if (typeof value === "string") {
        if (value.indexOf(",") > -1) {
          value = value.split();
        } else {
          value = [value];
        }
        value = value.map(function(username) {
          username = username.trim();
          return {
            username: username,
            name: username,
            gravatar: ""
          };
        });
      }
      if (Object.prototype.toString.call(value) === "[object Array]") {
        var self = this;
        value.map(function(usermask) {
          self.fields.participants.json.users.unshift(usermask);
        });
      } else {
        this.fields.participants.json.users.unshift(value);
      }
    }
  },
 
  consultants: {
    get: function() {
      var dataEntryPeople = [];
      Eif (this.fields && this.fields.participants && this.fields.participants.json && this.fields.participants.json.users) {
        this.fields.participants.json.users.map(function(usermask) {
          if (usermask.role && usermask.role.indexOf("speaker") > -1) {
            dataEntryPeople.push(usermask);
          }
        });
        if (dataEntryPeople.length === 0) {
          dataEntryPeople = this.fields.participants.json.users;
        }
      }
      return dataEntryPeople;
    },
    set: function(value) {
      Iif (!this.fields) {
        this.fields = this.defaults.fields;
      }
 
      // if (Object.prototype.toString.call(value) === "[object Array]") {
      //   var self = this;
      //   value.map(function(usermask) {
      //     self.consultants = usermask;
      //   });
      //   return;
      // }
      if (typeof value === "string") {
        Eif (value.indexOf(",") > -1) {
          value = value.split(",");
        } else {
          value = [value];
        }
        value = value.map(function(username) {
          username = username.trim();
          return {
            username: username,
            name: username,
            gravatar: ""
          };
        });
      }
      Iif (Object.prototype.toString.call(value) !== "[object Array]") {
        value = [value];
      }
      var self = this;
      value = value.map(function(userMask) {
        Iif (userMask.role) {
          userMask.role = "speaker," + userMask.role;
        } else {
          userMask.role = "speaker";
        }
        self.fields.participants.json.users.unshift(userMask);
        return userMask;
      });
 
      this.debug("adding consultant", value);
    }
  },
 
  user: {
    get: function() {
      var dataEntryPeople = [];
      Eif (this.fields && this.fields.participants && this.fields.participants.json && this.fields.participants.json.users) {
        this.fields.participants.json.users.map(function(usermask) {
          if (usermask.role && usermask.role.indexOf("dataEntry") > -1) {
            dataEntryPeople.push(usermask);
          }
        });
        Iif (dataEntryPeople.length === 0) {
          dataEntryPeople = this.fields.participants.json.users;
        }
      }
      return dataEntryPeople[0];
    },
    set: function(value) {
      Iif (!this.fields) {
        this.fields = this.defaults.fields;
      }
 
      if (typeof value === "string") {
        if (value.indexOf(",") > -1) {
          value = value.split(",");
        } else {
          value = [value];
        }
        value = value.map(function(username) {
          username = username.trim();
          return {
            username: username,
            name: username,
            gravatar: ""
          };
        });
      }
 
      Iif (Object.prototype.toString.call(value) !== "[object Array]") {
        value = [value];
      }
      var self = this;
      value = value.map(function(userMask) {
        Iif (userMask.role) {
          userMask.role = "dataEntry," + userMask.role;
        } else {
          userMask.role = "dataEntry";
        }
        self.fields.participants.json.users.unshift(userMask);
        return userMask;
      });
 
      this.debug("adding user", value);
    }
  },
 
  encryptByCorpus: {
    value: true
  },
 
  confidential: {
    get: function() {
      return this.confidentialEncrypter;
    },
    set: function(value) {
      this.ensureSetViaAppropriateType("confidential", value, "confidentialEncrypter");
      if (this._fields && this.confidentialEncrypter) {
        // this.debug("setting session fields confidential in the Session.confidential set function.");
        this._fields.confidential = this.confidentialEncrypter;
      }
    }
  },
 
  languages: {
    get: function() {
      if (this.fields) {
        return this.fields.languages.value;
      } else {
        return;
      }
    },
    set: function(value) {
      var stringvalue;
      var objectvalue;
      if (typeof value === "string") {
        this.debug("User set the languages with a string");
        if (this.fields.languages && this.fields.languages && this.fields.languages.json) {
          this.confirm("Do you want to set the languages from " + JSON.stringify(this.fields.languages.json) + " to " + value);
        }
        stringvalue = value;
        objectvalue = {
          value: value,
          label: "languages",
          json: {
            languages: value.split(",")
          }
        };
        objectvalue.json.languages = objectvalue.json.languages.map(function(languageName) {
          return {
            iso: languageName.toLowerCase().trim(),
            name: languageName.trim(),
            nativeName: languageName.trim()
          };
        });
      } else {
        objectvalue = value;
      }
 
      if (!this.fields) {
        this.fields = this.defaults.fields;
      }
      if (stringvalue) {
        this.fields.languages.value = stringvalue;
      }
      this.debug("setting language ", objectvalue);
 
      for (var property in objectvalue) {
        if (!objectvalue.hasOwnProperty(property)) {
          continue;
        }
        this.debug("looking at " + property);
        this.fields.languages[property] = objectvalue[property];
      }
    }
  },
 
  dialects: {
    get: function() {
      return this.languages;
    },
    set: function(value) {
      return this.languages = value;
    }
  },
 
  docIds: {
    get: function() {
      if (this._datalist && this._datalist.docIds) {
        return this._datalist.docIds;
      }
      // return FieldDBObject.DEFAULT_COLLECTION;
    },
    set: function(value) {
      if (!this._datalist) {
        this.initializeDatalist();
      }
      this._datalist.docIds = value;
    }
  },
 
  docs: {
    get: function() {
      if (this._datalist && this._datalist._docs) {
        return this._datalist._docs;
      }
    },
    set: function(value) {
      Iif (!value || value.length === 0) {
        this.warn("cant clear the docs of a session like this.", value);
        return;
      }
 
      Eif (!this.whenReindexedFromApi) {
        this.initializeDatalist();
      }
 
      var self = this,
        docPrimaryKey;
 
      this.whenReindexedFromApi.fail(function(error) {
        console.log(error.stack);
      }).done(function() {
 
        self.warn("  making sure " + value.length + " items are in the list of data.");
        value.map(function(tempDoc) {
          docPrimaryKey = tempDoc[self._datalist.primaryKey];
          Iif (!docPrimaryKey) {
            self.warn("This doc has no primary key, trying to add it anyway.", tempDoc);
            self._datalist._docs.add(tempDoc);
          } else {
            Eif (!self._datalist._docs[docPrimaryKey]) {
              self.warn("Adding " + tempDoc + " to the list. ");
              self._datalist._docs.add(tempDoc);
            }
          }
        });
 
      });
 
    }
  },
 
  utterances: {
    get: function() {
      return this.docs;
    },
    set: function(value) {
      return this.docs = value;
    }
  },
 
  transcriptions: {
    get: function() {
      return this.docs;
    },
    set: function(value) {
      return this.docs = value;
    }
  },
 
  items: {
    get: function() {
      return this.docs;
    },
    set: function(value) {
      return this.docs = value;
    }
  },
 
  datum: {
    get: function() {
      return this.docs;
    },
    set: function(value) {
      return this.docs = value;
    }
  },
 
  data: {
    get: function() {
      return this.docs;
    },
    set: function(value) {
      return this.docs = value;
    }
  },
 
  records: {
    get: function() {
      return this.docs;
    },
    set: function(value) {
      return this.docs = value;
    }
  },
 
  entries: {
    get: function() {
      return this.docs;
    },
    set: function(value) {
      return this.docs = value;
    }
  },
 
  examples: {
    get: function() {
      return this.docs;
    },
    set: function(value) {
      return this.docs = value;
    }
  },
 
  cards: {
    get: function() {
      return this.docs;
    },
    set: function(value) {
      return this.docs = value;
    }
  },
 
  add: {
    value: function(value) {
      Iif (!value) {
        return;
      }
 
      this.debug("Adding to the " + this.id + " session", value);
      if (this._datalist && this._datalist.docs && typeof this._datalist.docs.add === "function") {
        return this._datalist.docs.add(value);
      }
 
      Eif (!this.whenReindexedFromApi) {
        this.initializeDatalist();
      }
 
      var self = this;
      this.whenReindexedFromApi.fail(function(error) {
        console.log(error.stack);
      }).done(function() {
        self._datalist.docs.add(value);
      });
    }
  },
 
  length: {
    get: function() {
      Eif (this._datalist) {
        return this._datalist.length || 0;
      }
      return 0;
    },
    set: function(value) {
      this.warn("Cant set the lenght of a session. It's automatically determined by the number of items in its data list.", value);
    }
  },
 
  initializeDatalist: {
    value: function(datalistJson) {
      this.debug(" initializeDatalist ", this._datalist);
      Iif (this._datalist && this._datalist.whenReindexedFromApi) {
        if (datalistJson) {
          this.todo("the caller asked for a speciific data list to be created, ignoring it. ");
        }
        this.debug("Datalist is already ready.");
        return;
      }
 
      if (!datalistJson) {
        datalistJson = {
          title: {
            default: "All data in " + this.goal
          },
          dbname: this.dbname,
          docs: [],
          // debugMode: true
        };
      }
      this._datalist = new DataList(datalistJson);
 
      if (this.id) {
        this._datalist.api = "_design/data/_list/as_data_list/by_session_or_user?key=%22" + this.id + "%22";
      }
 
      this.debug("Asking the datalist to check for changes in the database.");
      Iif (typeof this._datalist.reindexFromApi !== "function") {
        this.warn("This datalist isnt real,  throwing error... ", this, this._datalist);
        throw new Error("this DataList isnt real.");
      }
      this._datalist.reindexFromApi();
    }
  },
 
  whenReindexedFromApi: {
    get: function() {
      if (this._datalist && this._datalist.whenReindexedFromApi) {
        return this._datalist.whenReindexedFromApi;
      }
    },
    set: function() {}
  },
 
  datalist: {
    get: function() {
      this.debug("Getting datalist " + this.id, this._datalist);
      // if (!this._datalist || !(this._datalist instanceof DataList) || typeof this._datalist.reindexFromApi !== "function") {
      //   this.initializeDatalist();
      // }
      return this._datalist;
    },
    set: function(value) {
      this.debug("Cant set the datalist on a session", this._datalist, value);
      // this.ensureSetViaAppropriateType("datalist", value);
    }
  },
 
  /**
   * Accepts two functions to call back when save is successful or
   * fails. If the fail callback is not overridden it will alert
   * failure to the user.
   *
   * - Adds the session to the corpus if it is in the right corpus, and wasnt already there
   * - Adds the session to the user if it wasn't already there
   * - Adds an activity to the logged in user with diff in what the user changed.
   *
   * @param successcallback
   * @param failurecallback
   */
  saveAndInterConnectInApp: {
    value: function(successcallback) {
      if (typeof successcallback === "function") {
        successcallback();
      }
    }
  },
  /**
   * Accepts two functions success will be called if sucessfull,
   * otherwise it will attempt to render the current session views. If
   * the session isn't in the current corpus it will call the fail
   * callback or it will alert a bug to the user. Override the fail
   * callback if you don't want the alert.
   *
   * @param successcallback
   * @param failurecallback
   */
  setAsCurrentSession: {
    value: function(successcallback, failurecallback) {
      var self = this;
      if (self.application.corpus.dbname !== this.dbname) {
        if (typeof failurecallback === "function") {
          failurecallback();
        } else {
          self.bug("This is a bug, cannot load the session you asked for, it is not in this corpus.");
        }
        return;
      }
 
      if (self.application.get("currentSession").id !== this.id) {
        self.application.set("currentSession", this); //This results in a non-identical session in the currentsession with the one live in the corpus sessions collection.
        //      self.application.set("currentSession", app.corpus.sessions.get(this.id)); //this is a bad idea too, use above instead
      }
      self.application.get("authentication").get("userPrivate").get("mostRecentIds").sessionid = this.id;
      self.application.get("authentication").saveAndInterConnectInApp(); //saving users is cheep
 
      if (self.application) {
        self.application.setUpAndAssociateViewsAndModelsWithCurrentSession(function() {
          if (typeof successcallback === "function") {
            successcallback();
          } else {
            self.application.currentSessionReadView.format = "leftSide";
            self.application.currentSessionReadView.render();
            self.application.toastUser("Sucessfully connected all views up to session: " + this.id, "alert-success", "Connected!");
            //          self.application.renderEditableSessionViews("leftSide");
            //          self.application.renderReadonlySessionViews("leftSide");
          }
        });
      } else {
        if (typeof successcallback === "function") {
          successcallback();
        }
      }
    }
  },
 
  toJSON: {
    value: function(includeEvenEmptyAttributes, removeEmptyAttributes) {
      this.debug("Customizing toJSON ", includeEvenEmptyAttributes, removeEmptyAttributes);
      var attributesNotToJsonify = ["_docs", "_docIds", "_datalist", "docs", "docIds", "datalist", "whenReindexedFromApi"];
      var json = FieldDBObject.prototype.toJSON.apply(this, [includeEvenEmptyAttributes, removeEmptyAttributes, attributesNotToJsonify]);
 
      // Dont keep docids in a session, this would cause the datum to have sessions iwth data lists, of themselves. which is very heavy. instead sessions docs alwasy come from a reindinxing operation on the db, and only if the session is not just a stub.
      // delete json._datalist;
      // if (this._datalist && this._datalist.docIds && this._datalist.docIds.length > 0) {
      //   json.docIds = this.docIds;
      // }
 
      this.debug(json);
      return json;
    }
  }
 
});
exports.Session = Session;