<commit_before>/**
 * Created by jsandoe on 10/20/14.
 */
// ==========================================================================
//                        DG.DataContextRecord
//
//  Copyright (c) 2014 by The Concord Consortium, Inc. All rights reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// ==========================================================================

sc_require('models/model_store');
sc_require('models/base_model');

/** @class

  Represents a user document.

 @extends SC.BaseModel
 */
DG.DataContextRecord = DG.BaseModel.extend(
  /** @scope DG.DataContextRecord.prototype */ {

    type: 'DG.DataContext',

    /**
     * A relational link back to the parent DG.Document
     * @property {DG.Document}
     */
    document: null,

    /**
     * A relational link to the collections in this document.
     * @property {Array of DG.CollectionRecord}
     */
    collections: null,

    /**
     * Per-component storage, in a component specific format.
     * @property {JSON}
     */
    contextStorage: null,

    _savedShadowCopy: null,

    init: function () {
      this.collections = {};
      sc_super();
    },

    verify: function () {
      if (SC.empty(this.document)) {
        DG.logWarn('Unattached data context: ' + this.id);
      }
      if (typeof this.document === 'number') {
        DG.logWarn('Unresolved reference to document id, ' + this.document +
          ', in data context: ' + this.id);
      }
    },

    destroy: function() {
      if (this.collections) {
        DG.ObjectMap.forEach(this.collections, function( iCollection) {
          DG.CollectionRecord.destroyCollection( iCollection);
        });
      }
      delete this.document.contexts[this.id];
      sc_super();
    },

    createCollection: function( iProperties) {
      iProperties = iProperties || {};
      iProperties.context = this;
      return DG.CollectionRecord.createCollection( iProperties);
    },

    toArchive: function (fullData) {
      var obj;
      fullData = fullData || false;
      if ( fullData || SC.none(this.externalDocumentId) ) {
        obj = {
            type: this.type,
            document: this.document && this.document.id || undefined,
            guid: this.id,
            collections: [],
            contextStorage: this.contextStorage
          };
        DG.ObjectMap.forEach(this.collections, function (collectionKey){
          obj.collections.push(this.collections[collectionKey].toArchive());
        }.bind(this));
      } else {
        obj = {
          externalDocumentId: this.externalDocumentId
        };
      }
      return obj;
    },

    savedShadowCopy: function() {
      return this._savedShadowCopy;
    },

    updateSavedShadowCopy: function(shadow) {
      this._savedShadowCopy = shadow;
    }

  });

DG.DataContextRecord.createContext = function( iProperties) {
  var tContext, shadowCopy = {};
  if( SC.none( iProperties)) iProperties = {};
  if( !SC.none( iProperties.externalDocumentId)) {
    // We should be loading this info from an external document.
    var response = DG.authorizationController.openDocumentSynchronously(iProperties.externalDocumentId);

    if (SC.ok(response)) {
      shadowCopy = $.extend(true, shadowCopy, response.get('body'));
      iProperties = $.extend(response.get('body'), iProperties);
    } else {
      // FIXME What do we do for an error?<commit_msg>Fix a bug where data context ids weren't reset when opening someone else's document<commit_after>/**
 * Created by jsandoe on 10/20/14.
 */
// ==========================================================================
//                        DG.DataContextRecord
//
//  Copyright (c) 2014 by The Concord Consortium, Inc. All rights reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// ==========================================================================

sc_require('models/model_store');
sc_require('models/base_model');

/** @class

  Represents a user document.

 @extends SC.BaseModel
 */
DG.DataContextRecord = DG.BaseModel.extend(
  /** @scope DG.DataContextRecord.prototype */ {

    type: 'DG.DataContext',

    /**
     * A relational link back to the parent DG.Document
     * @property {DG.Document}
     */
    document: null,

    /**
     * A relational link to the collections in this document.
     * @property {Array of DG.CollectionRecord}
     */
    collections: null,

    /**
     * Per-component storage, in a component specific format.
     * @property {JSON}
     */
    contextStorage: null,

    _savedShadowCopy: null,

    init: function () {
      this.collections = {};
      sc_super();
    },

    verify: function () {
      if (SC.empty(this.document)) {
        DG.logWarn('Unattached data context: ' + this.id);
      }
      if (typeof this.document === 'number') {
        DG.logWarn('Unresolved reference to document id, ' + this.document +
          ', in data context: ' + this.id);
      }
    },

    destroy: function() {
      if (this.collections) {
        DG.ObjectMap.forEach(this.collections, function( iCollection) {
          DG.CollectionRecord.destroyCollection( iCollection);
        });
      }
      delete this.document.contexts[this.id];
      sc_super();
    },

    createCollection: function( iProperties) {
      iProperties = iProperties || {};
      iProperties.context = this;
      return DG.CollectionRecord.createCollection( iProperties);
    },

    toArchive: function (fullData) {
      var obj;
      fullData = fullData || false;
      if ( fullData || SC.none(this.externalDocumentId) ) {
        obj = {
            type: this.type,
            document: this.document && this.document.id || undefined,
            guid: this.id,
            collections: [],
            contextStorage: this.contextStorage
          };
        DG.ObjectMap.forEach(this.collections, function (collectionKey){
          obj.collections.push(this.collections[collectionKey].toArchive());
        }.bind(this));
      } else {
        obj = {
          externalDocumentId: this.externalDocumentId
        };
      }
      return obj;
    },

    savedShadowCopy: function() {
      return this._savedShadowCopy;
    },

    updateSavedShadowCopy: function(shadow) {
      this._savedShadowCopy = shadow;
    }

  });

DG.DataContextRecord.createContext = function( iProperties) {
  var tContext, shadowCopy = {};
  if( SC.none( iProperties)) iProperties = {};
  if( !SC.none( iProperties.externalDocumentId)) {
    // We should be loading this info from an external document.
    var response = DG.authorizationController.openDocumentSynchronously(iProperties.externalDocumentId);

    if (SC.ok(response)) {
      var docId = response.headers()['Document-Id'];
      if (docId) {
        // make sure we always have the most up-to-date externalDocumentId,
        // since the document server can change it for permissions reasons.
        iProperties.externalDocumentId = ''+docId;
      }
      shadowCopy = $.extend(true, shadowCopy, response.get('body'));
      iProperties = $.extend(response.get('body'), iProperties);
    } else {
      // FIXME What do we do for an error?
{'fraction_non_alphanumeric': 0.15040898547185935, 'fraction_numerical': 0.003418386033451349, 'mean_word_length': 2.6279893711248894, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2871989', 'n_tokens_mistral': 2283, 'n_tokens_neox': 2176, 'n_words': 608}