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

//@include lib_Class.js
//@include lib_sol.common.Config.js
//@include lib_sol.common.HttpUtils.js
//@include lib_sol.common.Template.js
//@include lib_sol.common.WfUtils.js
//@include lib_sol.common.AsUtils.js
//@include lib_sol.common.JsonUtils.js
//@include lib_sol.common.ix.RfUtils.js
//@include lib_sol.common.ix.FunctionBase.js

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

/**
 * Internally triggers the ELOas function sol.common.as.functions.SendMail in order to execute a notification, e.g. by e-mail.
 *
 * This function won't stop the workflow execution if an email configuration isn't setup properly.
 * Compared to the as function the workflow must not be forwarded by an AS rule after a defined period of time.
 * Therefore it can be used if workflow processing times matters.
 *
 * # As workflow node
 *
 *     {
 *       "mode": "run",
 *       "from": "elo@contelo.com",
 *       "to": "solutions@elo.com",
 *       "subject": "Neue Aufgabe",
 *       "body": {
 *       "type": "html",
 *       "tplObjId": "ARCPATH:/Administration/Business Solutions Custom/notify/Configuration/Mail templates/Example",
 *       "data": {
 *         "person": {
 *         "salutation": "Mr",
 *           "firstName": "Peter",
 *           "lastName": "Smith"
 *         }
 *       }
 *    }
 *
 * # As IX function call
 *
 * In addition to the workflow node configuration the objId must be passed.
 *
 *     sol.common.IxUtils.execute("RF_sol_function_Notify", {
 *       objId: "4711",
 *       mode: "run",
 *       from: "elo@contelo.com",
 *       to: {ยด
 *         type: "CURRENT"
 *       },
 *       subject: "Neue Aufgabe",
 *       body: {
 *         type: "html",
 *         tplObjId: "ARCPATH:/Administration/Business Solutions Custom/contract/Configuration/Mail templates/Notification"
 *       },
 *       data: {
 *         person": {
 *           salutation: "Mr",
 *           firstName: "Peter",
 *           lastName: "Smith"
 *         }
 *       }
 *     }
 *
 * For more details see {@link sol.common.as.functions.SendMail}
 *
 * @author MW, ELO Digital Office GmbH
 * @version 1.0
 *
 * @eloix
 * @requires sol.common.Config
 * @requires sol.common.HttpUtils
 * @requires sol.common.Template
 * @requires sol.common.WfUtils
 * @requires sol.common.AsUtils
 * @requires sol.common.ix.RfUtils
 * @requires sol.common.ix.FunctionBase
 */
sol.define("sol.common.ix.functions.Notify", {
  extend: "sol.common.ix.FunctionBase",

  /**
   * @cfg {String} type
   * Notification type. Default is "e-mail"
   */

  /**
   * @cfg {Object} config
   * Configuration object for the notification
   * see {@link sol.common.as.Mail}
   */

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

  process: function () {
    var me = this,
        config = {},
        result, resultStr;

    config.cmd = me.param2Obj.mode || "get";
    config.ruleName = "sol.common.as.SendMail";
    config.param2Obj = me.param2Obj;

    config.ticket = ixConnectAdmin.loginResult.clientInfo.ticket + "";

    result = sol.common.AsUtils.callAs(config);

    resultStr = sol.common.JsonUtils.stringifyAll(result);
    return resultStr;
  }
});

/**
 * @member sol.common.ix.functions.Notify
 * @static
 * @inheritdoc sol.common.ix.FunctionBase#onEnterNode
 */
function onEnterNode(clInfo, userId, wfDiagram, nodeId) {
  var module, param2Obj;

  logger.enter("onEnterNode_Notify", { flowId: wfDiagram.id, nodeId: nodeId });
  param2Obj = sol.common.WfUtils.parseAndCheckParams(wfDiagram, nodeId);

  param2Obj.objId = wfDiagram.objId;
  param2Obj.flowId = wfDiagram.id;
  param2Obj.nodeId = nodeId;

  sol.common.WfUtils.checkMainAdminWf(wfDiagram);

  module = sol.create("sol.common.ix.functions.Notify", { param2Obj: param2Obj });
  module.process();

  logger.exit("onEnterNode_Notify");
}

/**
 * @member sol.common.ix.functions.Notify
 * @static
 * @inheritdoc sol.common.ix.FunctionBase#onExitNode
 */
function onExitNode(ci, userId, wfDiagram, nodeId) {
  var module, param2Obj;

  logger.enter("onExitNode_Notify", { flowId: wfDiagram.id, nodeId: nodeId });
  param2Obj = sol.common.WfUtils.parseAndCheckParams(wfDiagram, nodeId);

  param2Obj.objId = wfDiagram.objId;
  param2Obj.flowId = wfDiagram.id;
  param2Obj.nodeId = nodeId;

  sol.common.WfUtils.checkMainAdminWf(wfDiagram);

  module = sol.create("sol.common.ix.functions.Notify", { param2Obj: param2Obj });
  module.process();

  logger.exit("onExitNode_CheckForward");
}

/**
 * @member sol.common.ix.functions.Notify
 * @method RF_sol_function_Notify
 * @static
 * @inheritdoc sol.common.ix.FunctionBase#RF_FunctionName
 */
function RF_sol_function_Notify(ec, configAny) {
  var module, param2Obj, result;

  logger.enter("RF_sol_function_Notify", configAny);
  param2Obj = sol.common.ix.RfUtils.parseAndCheckParams(ec, arguments.callee.name, configAny);

  sol.common.ix.RfUtils.checkMainAdminRights(ec.user, param2Obj);

  module = sol.create("sol.common.ix.functions.Notify", { param2Obj: param2Obj });
  result = module.process();

  logger.exit("RF_sol_function_Notify");

  return result;
}