Example Naming Rule Script: Trains

The following script generates a name for trains using the format R-YY-####, where:

  • R= Name prefix
  • YY = 2-digit year
  • #### = 4-digit number with leading zeros, starting at 1

For example, R-15-0001 is the name for the first train generated in the year 2015.

Copy
using System;
using Mincom.MineMarket.DAL;
using Mincom.MineMarket;
using System.Collections;
using Mincom.MineMarket.Server;

/* 
This file includes standard naming scripts for the following objects:
Train TrainNaming : Using the following format R-YY-####

Version 1.0
Date last modified : 16/07/2015 to compile and run with MineMarket 4.7
Modified by : Datamine
*/

/// <summary>
/// This is an example or template class and should not be used as is.
/// </summary>

public class TrainNaming : INameGenerator
{
    public TrainNaming() { }

    public string[] GetParameterMap()
    {
        return null;
    }

    public string GetName(string[] paramList)
    {
        return null;
    }

    public string GetName(BTNamedBusinessObject obj, ValidationInfo info, out 
        ScriptResult scriptResult)
    {
        Train train = obj as Train;
        if (train != null && train.Route != null)
        {
            scriptResult = ScriptResult.NameGenerated;
            string trainName = "R-" + DateTime.Now.ToString("yy") + "-" ;

            ArrayList temp;
            Criteria crit1 = new Criteria(typeof(Train), "Name"
                QueryOperator.Like, trainName + "%");
            QueryBuilder qryb = new QueryBuilder(crit1,null);
            qryb.OrderByList.Add(new OrderByClause(typeof(Train), "Name"
                false));

            temp = Train.GetFactory().GetByCriteria(qryb);
            int number = 0;
            if (temp.Count > 0)
            {
                string nameTemp = ((Train)temp[0]).Name;
                int.TryParse(nameTemp.Substring(trainName.Length, 
                nameTemp.Length - trainName.Length),out number);
            }

            number++;
            trainName = trainName + number.ToString("0000");
            return trainName;
        }
        else if (info != null && train != null && train.Route == null
        {
            info.AddError(obj, "Train Route cannot be null");
        }

        scriptResult = ScriptResult.NameNotGenerated;
        return null;
    }
}