0% found this document useful (0 votes)
56 views6 pages

Code Parsing and Field Retrieval Logic

Uploaded by

treekengmanow
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views6 pages

Code Parsing and Field Retrieval Logic

Uploaded by

treekengmanow
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

using System;

using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Data;

namespace ETS.Core.Scripting.Modules
{
public class CodeParsing
{
private ETS.Core.Api.ApiService _api;
private int FREEFORMTEXT_MAX = 16;
private int TOTALTEXT_MAX = 17;
/// ******************************************************************
public CodeParsing(ETS.Core.Api.ApiService api)
{
_api = api;
}

///*************************************************************
/// CODE PARSING LOGIC BELOW
/// *** ANY CHANGES MADE MUST BE MADE TO DBC Print Dialog Print Preview AND
VICE VERSA ***
///*************************************************************
public List<string> ParseString(string format, Dictionary<string, object>
fields)
{
var batch = fields["BATCH"];
var line = fields["LINE"];
var mfd = fields["MFD"];
var exp = fields["EXP"];

format = format.Trim();
bool hasQuotes = Regex.IsMatch(format, "\"");
string replacement = format;
//if(hasQuotes) replacement = this.GetCleanFOPMoP(format);

var output = new StringBuilder();


List<string> outputList = new List<string>();

string[] keywords = Regex.Split(replacement, "(;|,|[\"](?<=\")[^\"]+[\"])");

bool isEnd = false;


string lineString;
foreach(var key in keywords)
{
if(key.StartsWith("\"") && key.EndsWith("\""))
output.Append(key.Trim('"'));
switch(key)
{
case "BATCH":
if(batch == "" || batch == null) { output.Append("");}
else { output.Append(batch); }
break;
case "LINE":
if(line == "" || batch == null) { output.Append("");}
else { output.Append(line); }
break;
case "MFD":
if( mfd == "" || mfd == null) { output.Append("");}
else { output.Append(mfd); }
break;
case "EXP":
if(exp == "" || exp == null) { output.Append(""); }
else { output.Append(exp); }
break;
case "TIME":
output.Append(DateTime.Now.ToString("HH:mm"));
break;
case ";":
if(output.Length > this.TOTALTEXT_MAX) { lineString =
output.ToString(0, this.TOTALTEXT_MAX);}
else { lineString = output.ToString(); }
outputList.Add(lineString);
output.Clear();
break;
case ",":
output.Append(" ");
break;
default:
break;
}
}
if(output.Length > this.TOTALTEXT_MAX) { lineString = output.ToString(0,
this.TOTALTEXT_MAX); }
else { lineString = output.ToString(); }
outputList.Add(lineString);

//Ignore any strings that exceed the 4th line limit


if(outputList.Count > 4) { outputList = outputList.GetRange(0,4); }

while(outputList.Count < 4)
{
outputList.Add(string.Empty);
}

return outputList;
}
/// ******************************************************************
public Dictionary<string, object> GetFields(int systemID)
{
var outputDict = new Dictionary<string, object>();

string sql = string.Format(@"


DECLARE @SystemID INT = {0}
DECLARE @ExternalID NVARCHAR(200) = (SELECT ExternalID FROM tSystem WHERE ID =
@SystemID)

DECLARE @JobID INT =


(
SELECT TOP 1 JobID
FROM tJobSystemActual
WHERE SystemID = @SystemID
AND EndDateTime IS NULL
ORDER BY StartDateTime DESC
)
if @JobID IS NULL SET @JobID = -1
if (@JobID = -1)
BEGIN
SELECT
@SystemID AS [SystemID]
, @SystemID AS [Line]
, @JobID AS [JobID]
, '' AS [JobName]
, -1 AS [BatchID]
, '' AS [BatchNumber]
, '' AS [BatchExpiryDateFormatted]
, '' AS [BatchProductionDateFormatted]
, -1 AS [IsExpiryCalculated]
END
ELSE

BEGIN

DECLARE @JobName NVARCHAR(200) =


(
SELECT TOP 1 Name
FROM tJob
WHERE ID = @JobID
)
if @JobName IS NULL SET @JobName = ''

SELECT TOP 1
@SystemID AS [SystemID]
, @SystemID AS [Line]
, @JobID AS [JobID]
, @JobName AS [JobName]
, tBatchJob.ID AS [BatchID]
, tBatchJob.name AS [BatchNumber]
, ISNULL(tBatchJob.[Capture05] + tBatchJob.[Capture06], '') AS
[BatchExpiryDateFormatted]
, ISNULL(tBatchJob.[Capture07] + tBatchJob.[Capture08], '') AS
[BatchProductionDateFormatted]
, ISNULL(tBatchJob.[Capture19], -1) AS [IsExpiryCalculated]
FROM tJob tBatchJob

WHERE ParentJobID = @JobID


ORDER BY ID DESC
END"
, systemID);

var result = _api.Util.Db.GetDataTable(sql);

var line = result.Return.Rows[0].Field<string>("Line");


line = line.Remove(line.Length-1);
int index = line.IndexOf(".");

outputDict.Add("LINE", line.Remove(0, index+1));


outputDict.Add("BATCH", result.Return.Rows[0].Field<string>("BatchNumber"));
outputDict.Add("MFD",
result.Return.Rows[0].Field<string>("BatchProductionDateFormatted"));
outputDict.Add("EXP",
result.Return.Rows[0].Field<string>("BatchExpiryDateFormatted"));

return outputDict;
}

/// ******************************************************************
private string GetCleanFOPMoP(string format)
{
int startIndex = format.IndexOf('"');
Match mc = Regex.Match(format, "[\"](?<=\")[^\"]+[\"]");
string oldFreeText = mc.Value.ToString();

string freeText = this.Recursively_RemoveDelimiter(oldFreeText);

int oldFreeTextLength = oldFreeText.Length;

format = format.Remove(startIndex, oldFreeTextLength);

string replacement = format.Substring(0, startIndex) + freeText +


format.Substring(startIndex);

return replacement;
}

/// ******************************************************************
private string Recursively_RemoveDelimiter(string freeText)
{
if(freeText.Contains(";"))
{
int index = freeText.IndexOf(';');
freeText = freeText.Remove(index, 1);
}
else if(freeText.Contains(","))
{
int index = freeText.IndexOf(',');
freeText = freeText.Remove(index, 1);
}
else
{
return freeText;
}

freeText = this.Recursively_RemoveDelimiter(freeText);
return freeText;
}

/// ******************************************************************
public string GetIpAddr(int systemID)
{
string sql = @"
DECLARE @SystemID INT = {0};
SELECT
em.Host
FROM
tEventMonitorGroup emg
JOIN
tEventMonitor em
ON em.EventMonitorGroupID = emg.ID
JOIN
vwCustomPropertyEventMonitor v
ON v.ID = em.ID
WHERE
em.PrimarySystemID = @SystemID
AND emg.Name = 'Direct Batch Coders'
AND em.Enabled = 1".FormatWith(_api.Util.Db.FormatSql<int>(systemID));

var result = _api.Util.Db.ExecuteScalar<string>(sql);


if(!result.Success)
{
_api.Util.Log.WriteWarningsFromResultObject(result, "IpAddr Retrieve");
}

return result.Return;
}

/// ******************************************************************
/// Potentially need to fix the 'v.Port' and return statement
public int GetPortNumber(int systemID)
{
string sql = @"
DECLARE @SystemID INT = {0};
SELECT
v.[ET.DEVICE.PORT]
FROM
tEventMonitorGroup emg
JOIN
tEventMonitor em
ON em.EventMonitorGroupID = emg.ID
JOIN
vwCustomPropertyEventMonitor v
ON v.ID = em.ID
WHERE
em.PrimarySystemID = @SystemID
AND emg.Name = 'Direct Batch Coders'
AND em.Enabled = 1".FormatWith(_api.Util.Db.FormatSql<int>(systemID));

var result = _api.Util.Db.ExecuteScalar<int>(sql);


if(!result.Success)
{
_api.Util.Log.WriteWarningsFromResultObject(result, "Port Retrieve");
}

return result.Return;
}

/// ******************************************************************
public List<string> DeserializeFopmop(string printString)
{
List<string> output = new List<string>();
var mc = Regex.Matches(printString, "[\\|][^\\|]+[\\|]");
foreach(Match item in mc)
{
var itemVal = item.ToString();
itemVal = itemVal.Trim('|');
_api.Util.Log.WriteInformation(itemVal, "deserialize");
output.Add(itemVal);
}
return output;
}

/// ******************************************************************
public string SerializeFopmop(List<string> printStringList)
{
string output = string.Empty;
foreach(var line in printStringList)
{
output = output + "|" + line + "|";
}

return output;
}

/// ******************************************************************
public int HasTime(string fopmop)
{
string format = fopmop.Trim();
string[] keywords = Regex.Split(format, "(;|,|[\"](?<=\")[^\"]+[\"])");

foreach(var key in keywords)


{
if(key == "TIME")
{
return 1;
}
}

return 0;
}

/// ******************************************************************
}
}

You might also like