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

const { Builder, FilterOperator } = Query;

/*
 * Returns promise to load model specified by modelName and projectionName in the context of store and stagePk.
 */
function getPromise(store, stagePk, modelName, projectionName) {
  let stagePkPredicate = new Query.SimplePredicate('stage.id', FilterOperator.Eq, stagePk);
  let q = new Builder(store, modelName)
    .selectByProjection(projectionName)
    .where(stagePkPredicate)
    .build();

  return store.query(modelName, q);
}

/**
  Returns promise to preload stage metadata specified by stagePk in the context of store.
  @param {DS.Store} store
  @param {String} stagePk
  @return {Ember.RSVP.Promise} promise
 */
export default function fdPreloadStageMetadata(store, stagePk) {

  return new Ember.RSVP.Promise(function(resolve, reject) {
    let promises = [];

    const projectionName = 'FdPreloadMetadata';

    // stage promise
    let modelName = 'fd-dev-stage';
    let q = new Builder(store, modelName)
      .selectByProjection(projectionName)
      .byId(stagePk)
      .build();
    promises.push(store.query(modelName, q));

    // classes promise
    promises.push(getPromise(store, stagePk, 'fd-dev-class', projectionName));

    // associations promise
    promises.push(getPromise(store, stagePk, 'fd-dev-association', projectionName));

    // agregations promise
    promises.push(getPromise(store, stagePk, 'fd-dev-aggregation', projectionName));

    // inheritances promise
    promises.push(getPromise(store, stagePk, 'fd-dev-inheritance', projectionName));

    // resolve, reject
    Ember.RSVP.all(promises).then(resolve, reject);
  });
}

@@ -554,12 +554,279 @@
 %0A%7D%0A%0A
+/*%0A * Returns true if record is loaded to store and related to specified stage.%0A */%0Afunction tryPeekRecord(store, modelName, recordId, stagePk) %7B%0A  let record = store.peekRecord(modelName, recordId);%0A  return record !== null && record.get('stage.id') === stagePk;%0A%7D%0A%0A
 /**%0A
-
   Re
@@ -1931,23 +1931,1206 @@
 s).then(
-resolve
+() =%3E %7B%0A      let userService = Ember.getOwner(store).lookup('service:user');%0A      let userName = userService.getCurrentUserName();%0A      let lockModelName = 'new-platform-flexberry-services-lock';%0A%0A      let lockPredicate = new Query.SimplePredicate('userName', FilterOperator.Eq, userName);%0A      let q = new Builder(store, lockModelName)%0A        .selectByProjection('LockL')%0A        .where(lockPredicate)%0A        .build();%0A%0A      // delete locks for objects in current stage for current user.%0A      store.query(lockModelName, q).then((lockObjects) =%3E %7B%0A        let deleteLocksPromises = %5B%5D;%0A        lockObjects.forEach((item) =%3E %7B%0A          let needToDeleteLock = tryPeekRecord(store, 'fd-dev-class', item.get('id'), stagePk) %7C%7C%0A            tryPeekRecord(store, 'fd-dev-association', item.get('id'), stagePk) %7C%7C%0A            tryPeekRecord(store, 'fd-dev-aggregation', item.get('id'), stagePk) %7C%7C%0A            tryPeekRecord(store, 'fd-dev-inheritance', item.get('id'), stagePk);%0A%0A          if (needToDeleteLock) %7B%0A            deleteLocksPromises.push(item.destroyRecord());%0A          %7D%0A        %7D);%0A%0A        Ember.RSVP.all(deleteLocksPromises).then(resolve, reject);%0A      %7D, reject);%0A    %7D
 , reject

Delete locks of current user