<commit_before>define(function (require) {
  'use strict';

  //
  // Dependencies
  //

  var React                  = require('react');
  var AppDispatcher          = require('dispatchers/AppDispatcher');
  var stores                 = require('stores');
  var URL                    = require('url');
  var NotificationController = require('controllers/NotificationController');

  // Constants
  var NullProjectInstanceConstants = require('constants/NullProjectInstanceConstants');
  var NullProjectVolumeConstants   = require('constants/NullProjectVolumeConstants');
  var ProjectInstanceConstants     = require('constants/ProjectInstanceConstants');
  var ProjectVolumeConstants       = require('constants/ProjectVolumeConstants');
  var ProjectConstants             = require('constants/ProjectConstants');

  // Actions
  var ProjectActions         = require('actions/ProjectActions');

  // Models
  var Project  = require('models/Project');
  var Instance = require('models/Instance');
  var Volume   = require('models/Volume');

  // Modals
  var ModalHelpers                        = require('components/modals/ModalHelpers');
  var NullProjectMoveAttachedVolumesModal = require('components/modals/nullProject/NullProjectMoveAttachedVolumesModal.react');
  var NullProjectMigrateResourceModal     = require('components/modals/nullProject/NullProjectMigrateResourceModal.react');


  //
  // Module
  //

  return {

    dispatch: function(actionType, payload, options){
      options = options || {};
      AppDispatcher.handleRouteAction({
        actionType: actionType,
        payload: payload,
        options: options
      });
    },

    // ------------------------
    // Standard CRUD Operations
    // ------------------------

    _migrateResourceIntoProject: function(resource, project){
      ProjectActions.addResourceToProject(resource, project);

      if(resource instanceof Instance){
        this.dispatch(NullProjectInstanceConstants.REMOVE_INSTANCE_FROM_NULL_PROJECT, {
          instance: resource
        });
      }else if(resource instanceof Volume){
        this.dispatch(NullProjectVolumeConstants.REMOVE_VOLUME_FROM_NULL_PROJECT, {
          volume: resource
        });
      }
    },

    _migrateResourceIntoRealProject: function(resource, oldProject, newProject){
      ProjectActions.addResourceToProject(resource, newProject);

      if(oldProject) {
        if (resource instanceof Instance) {
          this.dispatch(ProjectInstanceConstants.REMOVE_INSTANCE_FROM_PROJECT, {
            instance: resource,
            project: oldProject
          });
        } else if (resource instanceof Volume) {
          this.dispatch(ProjectVolumeConstants.REMOVE_VOLUME_FROM_PROJECT, {
            volume: resource,
            project: oldProject
          });
        }
      }
    },

    _migrateResourcesIntoProject: function(resources, project){
      resources.map(function(resource){
        this._migrateResourceIntoProject(resource, project);
      }.bind(this));

      var redirectUrl = URL.projectResources(project, {relative: true});
      Backbone.history.navigate(redirectUrl, {trigger: true});
    },

    // synchronize project resource state
    // 1. If resource not in a project, force user to put it into one
    // 2. If volume is attached
    // Uh oh!  Looks like you have some resources that aren't in a project.
    //
    // This can occur the first time you use
    // the new Atmosphere interface, or by switching back and forth between the old and new UI
    //
    moveAttachedVolumesIntoCorrectProject: function(){
      var projects = stores.ProjectStore.getAll();
      var instances = stores.InstanceStore.getAll(projects);
      var volumes = stores.VolumeStore.getAll(projects);

      // Move volumes into correct project
      var volumesInWrongProject = [];
      volumes.each(function(volume){
        var volumeProjectId = volume.get('projects')[0];
        var volumeProject = stores.ProjectStore.get(volumeProjectId);
        var instanceId = volume.get('attach_data').instance_id;

        if (instanceId) {
          var instance = instances.get(instanceId);
          var instanceProjectId = instance.get('projects')[0];
          if(volumeProjectId !== instanceProjectId){
            var project = stores.ProjectStore.get(instanceProjectId);
            this._migrateResourceIntoRealProject(volume, volumeProject, project);
            volumesInWrongProject.push({
              volume: volume,
              instance: instance,<commit_msg>bug fix: prevent app from exploding when instance not found<commit_after>define(function (require) {
  'use strict';

  //
  // Dependencies
  //

  var React                  = require('react');
  var AppDispatcher          = require('dispatchers/AppDispatcher');
  var stores                 = require('stores');
  var URL                    = require('url');
  var NotificationController = require('controllers/NotificationController');

  // Constants
  var NullProjectInstanceConstants = require('constants/NullProjectInstanceConstants');
  var NullProjectVolumeConstants   = require('constants/NullProjectVolumeConstants');
  var ProjectInstanceConstants     = require('constants/ProjectInstanceConstants');
  var ProjectVolumeConstants       = require('constants/ProjectVolumeConstants');
  var ProjectConstants             = require('constants/ProjectConstants');

  // Actions
  var ProjectActions         = require('actions/ProjectActions');

  // Models
  var Project  = require('models/Project');
  var Instance = require('models/Instance');
  var Volume   = require('models/Volume');

  // Modals
  var ModalHelpers                        = require('components/modals/ModalHelpers');
  var NullProjectMoveAttachedVolumesModal = require('components/modals/nullProject/NullProjectMoveAttachedVolumesModal.react');
  var NullProjectMigrateResourceModal     = require('components/modals/nullProject/NullProjectMigrateResourceModal.react');


  //
  // Module
  //

  return {

    dispatch: function(actionType, payload, options){
      options = options || {};
      AppDispatcher.handleRouteAction({
        actionType: actionType,
        payload: payload,
        options: options
      });
    },

    // ------------------------
    // Standard CRUD Operations
    // ------------------------

    _migrateResourceIntoProject: function(resource, project){
      ProjectActions.addResourceToProject(resource, project);

      if(resource instanceof Instance){
        this.dispatch(NullProjectInstanceConstants.REMOVE_INSTANCE_FROM_NULL_PROJECT, {
          instance: resource
        });
      }else if(resource instanceof Volume){
        this.dispatch(NullProjectVolumeConstants.REMOVE_VOLUME_FROM_NULL_PROJECT, {
          volume: resource
        });
      }
    },

    _migrateResourceIntoRealProject: function(resource, oldProject, newProject){
      ProjectActions.addResourceToProject(resource, newProject);

      if(oldProject) {
        if (resource instanceof Instance) {
          this.dispatch(ProjectInstanceConstants.REMOVE_INSTANCE_FROM_PROJECT, {
            instance: resource,
            project: oldProject
          });
        } else if (resource instanceof Volume) {
          this.dispatch(ProjectVolumeConstants.REMOVE_VOLUME_FROM_PROJECT, {
            volume: resource,
            project: oldProject
          });
        }
      }
    },

    _migrateResourcesIntoProject: function(resources, project){
      resources.map(function(resource){
        this._migrateResourceIntoProject(resource, project);
      }.bind(this));

      var redirectUrl = URL.projectResources(project, {relative: true});
      Backbone.history.navigate(redirectUrl, {trigger: true});
    },

    // synchronize project resource state
    // 1. If resource not in a project, force user to put it into one
    // 2. If volume is attached
    // Uh oh!  Looks like you have some resources that aren't in a project.
    //
    // This can occur the first time you use
    // the new Atmosphere interface, or by switching back and forth between the old and new UI
    //
    moveAttachedVolumesIntoCorrectProject: function(){
      var projects = stores.ProjectStore.getAll();
      var instances = stores.InstanceStore.getAll(projects);
      var volumes = stores.VolumeStore.getAll(projects);

      // Move volumes into correct project
      var volumesInWrongProject = [];
      volumes.each(function(volume){
        var volumeProjectId = volume.get('projects')[0];
        var volumeProject = stores.ProjectStore.get(volumeProjectId);
        var instanceId = volume.get('attach_data').instance_id;

        if (instanceId) {
          var instance = instances.get(instanceId);
          if(!instance){
            console.warn("Instance with id: " + instanceId + " was not found.");
            return;
          }
          var instanceProjectId = instance.get('projects')[0];
          if(volumeProjectId !== instanceProjectId){
            var project = stores.ProjectStore.get(instanceProjectId);
            this._migrateResourceIntoRealProject(volume, volumeProject, project);
            volumesInWrongProject.push({
              volume: volume,
              instance: instance,
{'fraction_non_alphanumeric': 0.10588362535150335, 'fraction_numerical': 0.0008652390222799049, 'mean_word_length': 2.743724696356275, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2554425', 'n_tokens_mistral': 2297, 'n_tokens_neox': 2264, 'n_words': 509}