<commit_before>/**
  @module ember-flexberry
*/

import Ember from 'ember';
import { Query } from 'ember-flexberry-data';

/**
  Mixin for {{#crossLink "EditFormRoute"}}{{/crossLink}}, which provides support locking.

  @class LockRouteMixin
  @uses <a href="http://emberjs.com/api/classes/Ember.Mixin.html">Ember.Mixin</a>
*/
export default Ember.Mixin.create({
  /**
    @property _currentLock
    @type DS.Model
    @default null
    @private
  */
  _currentLock: null,

  /**
    @property _readonly
    @type Boolean
    @default false
    @private
  */
  _readonly: false,

  /**
    This property uses for setting user name.

    @example
      ```javascript
      // app/routes/user-edit.js
      import EditFormRoute from 'ember-flexberry/routes/edit-form';
      export default EditFormRoute.extend({
        ...
        userName: Ember.computed(function() {
          let userService = Ember.getOwner(this).lookup('service:user');
          return userService.getCurrentUserName();
        }),
        ...
      });
      ```

    @property userName
    @type String
    @default 'admin'
    @for EditFormRoute
  */
  userName: 'admin',

  /**
    This object contains answers which will corresponding functions resolved.

    @property defaultBehaviorLock
    @type Object
    @default { openReadOnly: true, unlockObject: true }
    @for EditFormRoute
  */
  defaultBehaviorLock: {
    openReadOnly: true,
    unlockObject: true,
  },

  actions: {
    /**
      The willTransition action is fired at the beginning of any attempted transition with a Transition object as the sole argument.
      [More info](http://emberjs.com/api/classes/Ember.Route.html#event_willTransition).

      @method actions.willTransition
      @param {Transition} transition
    */
    willTransition(transition) {
      this.set('_readonly', false);
      this.controller.set('readonly', false);
      let lock = this.get('_currentLock');
      if (lock) {
        transition.abort();
        this.unlockObject().then((answer) => {
          (answer ? lock.destroyRecord() : new Ember.RSVP.resolve()).then(() => {
            this.set('_currentLock', null);
            transition.retry();
          });
        });
      }
    },
  },

  /**
    This hook is the first of the route entry validation hooks called when an attempt is made to transition into a route or one of its children.
    [More info](http://emberjs.com/api/classes/Ember.Route.html#method_beforeModel).

    @method beforeModel
    @param {Transition} transition
    @return {Promise}
  */
  beforeModel(transition) {
    let params = this.paramsFor(this.routeName);
    return new Ember.RSVP.Promise((resolve, reject) => {
      if (params.id && !params.readonly) {
        let builder = new Query.Builder(this.store)
          .from('new-platform-flexberry-services-lock')
          .selectByProjection('LockL')
          .where('lockKey', 'eq', params.id);
        this.store.queryRecord('new-platform-flexberry-services-lock', builder.build()).then((lock) => {
          if (!lock) {
            this.store.createRecord('new-platform-flexberry-services-lock', {
              lockKey: params.id,
              userName: this.get('userName'),
              lockDate: new Date(),
            }).save().then((lock) => {
              this.set('_currentLock', lock);
              resolve();
            }).catch((reason) => {
              this.openReadOnly().then((answer) => {
                this._openReadOnly(answer, resolve, reject);
              });
            });
          } else if (lock.get('userName') === this.get('userName')) {
            this.set('_currentLock', lock);<commit_msg>Fix lock-route mixin filter by id<commit_after>/**
  @module ember-flexberry
*/

import Ember from 'ember';
import { Query } from 'ember-flexberry-data';

/**
  Mixin for {{#crossLink "EditFormRoute"}}{{/crossLink}}, which provides support locking.

  @class LockRouteMixin
  @uses <a href="http://emberjs.com/api/classes/Ember.Mixin.html">Ember.Mixin</a>
*/
export default Ember.Mixin.create({
  /**
    @property _currentLock
    @type DS.Model
    @default null
    @private
  */
  _currentLock: null,

  /**
    @property _readonly
    @type Boolean
    @default false
    @private
  */
  _readonly: false,

  /**
    This property uses for setting user name.

    @example
      ```javascript
      // app/routes/user-edit.js
      import EditFormRoute from 'ember-flexberry/routes/edit-form';
      export default EditFormRoute.extend({
        ...
        userName: Ember.computed(function() {
          let userService = Ember.getOwner(this).lookup('service:user');
          return userService.getCurrentUserName();
        }),
        ...
      });
      ```

    @property userName
    @type String
    @default 'admin'
    @for EditFormRoute
  */
  userName: 'admin',

  /**
    This object contains answers which will corresponding functions resolved.

    @property defaultBehaviorLock
    @type Object
    @default { openReadOnly: true, unlockObject: true }
    @for EditFormRoute
  */
  defaultBehaviorLock: {
    openReadOnly: true,
    unlockObject: true,
  },

  actions: {
    /**
      The willTransition action is fired at the beginning of any attempted transition with a Transition object as the sole argument.
      [More info](http://emberjs.com/api/classes/Ember.Route.html#event_willTransition).

      @method actions.willTransition
      @param {Transition} transition
    */
    willTransition(transition) {
      this.set('_readonly', false);
      this.controller.set('readonly', false);
      let lock = this.get('_currentLock');
      if (lock) {
        transition.abort();
        this.unlockObject().then((answer) => {
          (answer ? lock.destroyRecord() : new Ember.RSVP.resolve()).then(() => {
            this.set('_currentLock', null);
            transition.retry();
          });
        });
      }
    },
  },

  /**
    This hook is the first of the route entry validation hooks called when an attempt is made to transition into a route or one of its children.
    [More info](http://emberjs.com/api/classes/Ember.Route.html#method_beforeModel).

    @method beforeModel
    @param {Transition} transition
    @return {Promise}
  */
  beforeModel(transition) {
    let params = this.paramsFor(this.routeName);
    return new Ember.RSVP.Promise((resolve, reject) => {
      if (params.id && !params.readonly) {
        let builder = new Query.Builder(this.store)
          .from('new-platform-flexberry-services-lock')
          .selectByProjection('LockL')
          .byId(`'${params.id}'`);
        this.store.queryRecord('new-platform-flexberry-services-lock', builder.build()).then((lock) => {
          if (!lock) {
            this.store.createRecord('new-platform-flexberry-services-lock', {
              lockKey: params.id,
              userName: this.get('userName'),
              lockDate: new Date(),
            }).save().then((lock) => {
              this.set('_currentLock', lock);
              resolve();
            }).catch((reason) => {
              this.openReadOnly().then((answer) => {
                this._openReadOnly(answer, resolve, reject);
              });
            });
          } else if (lock.get('userName') === this.get('userName')) {
            this.set('_currentLock', lock);
{'fraction_non_alphanumeric': 0.13718805400245465, 'fraction_numerical': 0.0, 'mean_word_length': 2.409576940957694, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, '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': '5664060', 'n_tokens_mistral': 2091, 'n_tokens_neox': 2071, 'n_words': 470}