importPackage(Packages.java.io);
importPackage(Packages.de.elo.ix.client);

//@include lib_Class.js
//@include lib_sol.common.ix.ServiceBase.js
//@include lib_handlebars.js

var logger = sol.create("sol.Logger", { scope: "sol.common.ix.services.ScriptVersionReportValidate" });

/**
 * Validates a given script version report based on scripts that are in the current system.
 *
 * Reports can be generated by `sol.common.ix.services.ScriptVersionReportCreate`.
 *
 * # As IX function call
 *
 *     sol.common.IxUtils.execute('RF_sol_common_service_ScriptVersionReportValidate', {
 *       jsonScriptVersionList: jsonSVL
 *     });
 *
 * # Example for jsonSVL
 *
 * The service requires a script version report defined by the following data structure.
 *
 *     {
 *       date: "20150623105541",
 *       files: [{
 *              refPath:  "¶Administration¶Business Solutions¶connector_xml¶resources",
 *              name:     "doc_import_example",
 *              filehash: "9F0FF55B9F675DBD3CE42E9B3B337DA3",
 *              guid:     "(B22ECD56-1537-3B3B-CBBA-5DAE2A66DA81)",
 *              version:  "6",
 *              editor:   "James Bond",
 *              date:     "20150414141537",
 *              deleted:  false
 *          }, {
 *              refPath:  "¶Administration¶Business Solutions¶connector_xml¶resources",
 *              name:     "standard_import_example",
 *              filehash: "6F35A6E25C927442CFD484EBBF6ECAE2",
 *              guid:     "(04089DB6-E667-EDDE-DB86-5FCB6E5F4DFB)",
 *              version:  "1",
 *              editor:   "Batman Superman",
 *              date:     "20150402162230",
 *              deleted:  false
 *          }]
 *     }
 *
 * # Returns data as followed
 *
 * The given data structure is extended by a property `modified` and contains the new information for `filehash`, `version`, `editor`, `date` and `deleted`.
 *
 *     {
 *       date: "20150623105541",
 *       files: [{
 *              refPath:  "¶Administration¶Business Solutions¶connector_xml¶resources",
 *              name:     "doc_import_example",
 *              filehash: "9F0FF55B9F675DBD3CE42E9B3B337DA3",
 *              guid:     "(B22ECD56-1537-3B3B-CBBA-5DAE2A66DA81)",
 *              version:  "6",
 *              editor:   "James Bond",
 *              date:     "20150414141537",
 *              deleted:  false,
 *              modified: true
 *          }, {
 *              refPath:  "¶Administration¶Business Solutions¶connector_xml¶resources",
 *              name:     "standard_import_example",
 *              filehash: "6F35A6E25C927442CFD484EBBF6ECAE2",
 *              guid:     "(04089DB6-E667-EDDE-DB86-5FCB6E5F4DFB)",
 *              version:  "1",
 *              editor:   "Batman Superman",
 *              date:     "20150402162230",
 *              deleted:  false,
 *              modified: false
 *          }]
 *     }
 *
 * @author JHR, ELO Digital Office GmbH
 * @version 1.0
 *
 * @eloix
 * @requires  sol.Logger
 * @requires  sol.common.JsonUtils
 * @requires  sol.common.ix.RfUtils
 * @requires  sol.common.ix.ServiceBase
 *
 */
sol.define("sol.common.ix.services.ScriptVersionReportValidate", {
  extend: "sol.common.ix.ServiceBase",

  /**
   * @cfg {Object} jsonScriptVersionList (required)
   * Version information of documents. Data structure that was created by ScriptVersionReportCreate.
   */
  jsonScriptVersionList: undefined,

  initialize: function (config) {
    var me = this;
    me.$super("sol.common.ix.ServiceBase", "initialize", [config]);
    me.checkMandatoryProperties("jsonScriptVersionList");
  },

  /**
   * Validates the Version Script List in JSON-Format for modified documents, returns the Version Script List in JSON-Format with modification info.
   * @return {Object}
   */
  process: function () {
    var me = this,
        files,
        jsonScriptVersionList = me.jsonScriptVersionList,
        i, fileentry, guid, filehashOld, ed, sord, dv;

    try {
      files = jsonScriptVersionList.files;
      for (i = 0; i < files.length; i++) {
        fileentry = files[i];
        guid = fileentry.guid;
        filehashOld = fileentry.filehash;

        ed = ixConnect.ix().checkoutSord(guid, EditInfoC.mbOnlyId, LockC.NO);
        sord = ed.sord;

        ed = ixConnect.ix().checkoutDoc(sord.id, -1, EditInfoC.mbDocument, LockC.NO);
        dv = me.getWorkVersion(ed.document.docs);
        fileentry.filehash = dv.md5;
        fileentry.version = dv.version;
        fileentry.editor = dv.ownerName;
        fileentry.date = dv.updateDateIso;
        fileentry.deleted = dv.deleted;
        fileentry.modified = (filehashOld != fileentry.filehash);
      }
    } catch (e) {
      me.logger.debug(["function ScriptVersionReportValidate(jsonScriptVersionList) catch (e) name: '{0}' message: '{1}'", e.name, e.message]);
      jsonScriptVersionList = {};
    }
    return jsonScriptVersionList;
  },

  getWorkVersion: function (docs) {
    var dv, i;

    dv = null;
    for (i = 0; i < docs.length; i++) {
      dv = docs[i];
      if (dv.workVersion) {
        return dv;
      }
    }
    return dv;
  }

});

/**
 * @member sol.common.ix.services.ScriptVersionReportValidate
 * @method RF_sol_common_service_ScriptVersionReportValidate
 * @static
 * @inheritdoc sol.common.ix.ServiceBase#RF_ServiceBaseName
 */
function RF_sol_common_service_ScriptVersionReportValidate(iXSEContext, args) {
  logger.enter("RF_sol_common_service_ScriptVersionReportValidate", args);
  var params, module, jsonScriptVersionList;

  params = sol.common.ix.RfUtils.parseAndCheckParams(iXSEContext, arguments.callee.name, args, "jsonScriptVersionList");
  module = sol.create("sol.common.ix.services.ScriptVersionReportValidate", params);
  jsonScriptVersionList = module.process();
  logger.exit("RF_sol_common_service_ScriptVersionReportValidate");
  return sol.common.ix.RfUtils.stringify(jsonScriptVersionList);
}