/DICK.Core/JobManagement/ExportJob.cs
C# | 270 lines | 197 code | 18 blank | 55 comment | 16 complexity | dced666b113882dd1ee963f47423ebc2 MD5 | raw file
- /* This file is part of DI Construction Kit.
- *
- * DI Construction Kit is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * DI Construction Kit is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with DI Construction Kit. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- using System;
- using System.Xml;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Text;
- using System.Diagnostics;
- using System.Reflection;
-
- namespace DICK.Core.JobManagement
- {
- /// <summary>
- /// This job type is used when producing export files with a template engine.
- /// Implements the IScriptContainer interface that is used as a base for all job types.
- /// </summary>
- public class ExportJob : IDotNetJob
- {
- #region Fields
- public string SelectionQuery { get; set; }
- public string SelectionQueryType { get; set; }
-
- public DICKAssembly MyAssembly
- {
- get;
- set;
- }
- #endregion
-
- //Convenience handle to the Engine singleton instance
- private Engine engine = Engine.GetInstance();
-
- /// <summary>
- /// Populates the job parameters from the given xml datastructure
- /// </summary>
- /// <param name="jobid">The identifier of the job</param>
- /// <param name="jobdata">The xml structure that contains the parameters of the job.</param>
- protected void InitializeJob(string jobID, XmlElement jobdata)
- {
- SelectionQuery = XmlTools.ParseString(jobdata, "SelectionQuery", "QUERY_NOT_FOUND");
- SelectionQueryType = XmlTools.ParseString(jobdata, "SelectionQueryType", "ADO");
- foreach (XmlElement target in jobdata.SelectNodes("ExportTarget"))
- {
- ExportTarget et = TargetFactory.GetTarget(target);
- this.Targets.Add(et);
- }
- if (dassembly != null)
- {
- initialized = true;
- }
- else
- {
- engine.LogDebug("ExportJob.Initialize: DASSEMBLY IS NULL!! Jobid:" + jobID + " jobdata:" + jobdata != null ? jobdata.ToString() : "NULL!");
- }
- }
-
- /// <summary>
- /// Overrides the standard ToString() method. Used for producing output about system configuration information.
- /// </summary>
- /// <returns></returns>
- public string ToString()
- {
- DateTime dt;
- StringBuilder sb = new StringBuilder();
- sb.AppendLine("Job ID:" + JobID);
- sb.AppendLine("Job Description:" + JobDescription);
- sb.AppendLine("B1 Required:" + B1Required);
- sb.AppendLine("Script:" + ScriptPath);
- sb.AppendLine("Class Name:" + ClassName);
- sb.AppendLine("Method:" + Method);
- sb.AppendLine("Language:" + ScriptLanguage);
- sb.AppendLine("Selection Query:" + SelectionQuery);
- sb.AppendLine("Selection Query Type:" + SelectionQueryType);
- foreach (ExportTarget target in Targets)
- {
- sb.AppendLine("EXPORT TARGET DEFINITION");
- sb.AppendLine(target.ToString());
- }
- //this.Targets
- return sb.ToString();
- }
-
-
-
- public List<ExportTarget> Targets = new List<ExportTarget>();
-
- /// <summary>
- /// Empty constructor for enabling runtime building of new ExportJob instances without a parameter data structure.
- /// </summary>
- protected ExportJob() { }
-
- /// <summary>
- /// The constructor for building an ExportJob from a parameter data structure.
- /// </summary>
- /// <param name="jobid">The identifier of the job</param>
- /// <param name="expdata">The parameter data for the job</param>
- public ExportJob(string jobid, XmlElement expdata)
- : base(jobid, expdata)
- {
- InitializeJob(jobid, expdata);
- }
-
- //**** EXPORT ****//
- //The method called from command line version of DICK
- public MessageToken DoExport()
- {
- List<string> exportkeys = new List<string>();
- PopulateExportKeyList(exportkeys);
- MessageToken result = DoExportByKeys(exportkeys);
- return result;
- }
-
- /// <summary>
- /// Used to export a number of objects based on the given list of keys (for instance itemcode for items and cardcode for business partners).
- /// </summary>
- /// <param name="keys">The list of identifier keys</param>
- /// <returns>MessageToken</returns>
- public MessageToken DoExportByKeys(List<string> keys)
- {
- MessageToken result = new MessageToken();
- int successcounter = 0;
- int failurecounter = 0;
-
- if (!initialized)
- {
- result.AddTextToBody("Job " + jobID + " not properly initialized!");
- result.OK = false;
- return result;
- }
- else
- {
- DICKContext initcontext = GetBatchInitContext();
- try
- {
- InitializeTargets(initcontext);
- engine.LogDebug("Looping through keys...");
- foreach (string key in keys)
- {
- ProcessTargetBody(ref successcounter, ref failurecounter, initcontext, key);
- }
- FinalizeTargets(result, initcontext);
- }
- catch (System.Exception ex)
- {
- result.OK = false;
- result.AddTextToBody("Exception during export: \r" + ex.Message);
- engine.LogError("Exception during export: \r" + ex.Message);
- failurecounter++;
- }
- finally
- {
- Targets.Clear();
- Targets = null;
- }
- }
- result.AddTextToBody("Documents processed successfully:" + successcounter);
- result.AddTextToBody("Documents with problems:" + failurecounter);
- return result;
- }
-
- private void FinalizeTargets(MessageToken result, DICKContext initcontext)
- {
- foreach (ExportTarget target in Targets)
- {
- if (target.IsBatch)
- {
- target.Finalize(initcontext);
- //For batch preview targets, we do the concatenation of results here
- if (target is PreviewTarget)
- {
- PreviewTarget pt = (PreviewTarget)target;
- result.AddPayload(pt.PayLoad.ToString());
- }
- }
- }
- }
-
- private void ProcessTargetBody(ref int successcounter, ref int failurecounter, DICKContext initcontext, string key)
- {
- //INSTANTIATE OBJECT FROM SCRIPT
- if (dassembly == null)
- {
- engine.LogError("EXPORTJOB.EXPORTBYKEYS: DASSEMBLY IS NULL!!");
- throw new DICK.Core.DICKException();
- }
- object o = dassembly.InvokeInstanceMethod(className, "GetContext", new object[] { key });
- if (o != null)
- {
- DICKContext context = (DICKContext)o;
- //We set a link to the batch context within each individual context object.
- context.ParentContext = initcontext;
- //PASS THE RETRIEVED CONTEXT OBJECT TO EACH TARGET
- foreach (ExportTarget target in Targets)
- {
- //IF JOB IS BATCH AND INITCONTEXT IS NOT INITIALIZED ==> BYPASS
- if (!(initcontext == null && target.IsBatch))
- {
- target.Process(context);
- }
- else
- {
- engine.LogError("ExportJob.ExportByKeys: BATCH NOT INITIATED ==> BYPASSING MERGE OF INDIVIDUAL TARGETS.");
- }
- }
- successcounter++;
- }
- else
- {
- engine.LogError("EXPORTJOB.EXPORTBYKEYS: Context object is null! ");
- failurecounter++;
- }
- }
-
- private void InitializeTargets(DICKContext initcontext)
- {
- foreach (ExportTarget target in Targets)
- {
- if (target.IsBatch)
- {
- target.Initialize(initcontext);
- }
- }
- engine.LogDebug("All targets initialized properly for exportjob " + jobID);
- }
-
- public DICKContext GetBatchInitContext()
- {
- engine.LogDebug("ExportJob calling GetBatchInitContext from script...");
- if (dassembly != null)
- {
- DICKContext c = dassembly.InvokeStaticMethod(className, "GetBatchInitContext", new object[] { }) as DICKContext;
- return c;
- }
- else
- {
- engine.LogError("DASSEMBLY IS NULL WHILE CALLING GETBATCHINITCONTEXT FROM EXPORTJOB !!");
- throw new DICKException();
- }
- }
-
- /// <summary>
- /// Overridden ín B1ExportJob so that the data is retrieved from B1 instead of ADO.NET but the rest of the code is unchanged.
- /// </summary>
- /// <param name="exportkeys"></param>
- protected virtual void PopulateExportKeyList(List<string> exportkeys)
- {
- DataTable dt = QueryToolsADO.QueryToDataTable(this.SelectionQuery);
- foreach (DataRow row in dt.Rows)
- {
- exportkeys.Add(row[0].ToString());
- }
- }
- }
- }