# ---------------------------------------------------------------------------
# UniqueValToDomain.py
# Created on: 2012-11-06 10:18:49.00000
#   (generated by ArcGIS/ModelBuilder)
# Usage: UniqueValToDomain <Workspace> <Table> <Frequency_Field_s_> <Field_Name> <Domain_Update_Option> <Domain_Name>
# Description:
# ---------------------------------------------------------------------------

# Set the necessary product code
# import arcinfo


# Import arcpy module
import arcpy, UniqueValToDomain, os

if __name__ == '__main__':
    ##Table = "E:/GIS_DATA/LADOT/LADOT@local.sde/LADOT.DBO.TAHI_STLFILE"
    ##FieldList = ["HIGHWAY_CLASS", "SPECIAL_SYSTEM"]
    ##FromMeasureField = "LOGMILE_FROM"
    ##ToMeasureField = "LOGMILE_TO"
    ##RoutIDField = "LRS_ID"

    Table = arcpy.GetParameterAsText(0)
    FieldList = arcpy.GetParameterAsText(1).split(';')
    FromMeasureField = arcpy.GetParameterAsText(2)
    ToMeasureField = arcpy.GetParameterAsText(3)
    RoutIDField = arcpy.GetParameterAsText(4)
    TablePrefix = arcpy.GetParameterAsText(5)

    Workspace = os.path.split(Table)[0] #"E:/GIS_DATA/LADOT/LADOT@local.sde"
    arcpy.env.overwriteOutput = True

    EventDissolveString = RoutIDField + " LINE " + FromMeasureField + " " + ToMeasureField
    arcpy.AddMessage(FieldList)

    for fieldName in FieldList:
        InputField = UniqueValToDomain.FindField(Table, str(fieldName))
        outTable = os.path.join(Workspace, TablePrefix + fieldName)
        print "Dissolving events for " + outTable + " using " + EventDissolveString + " by field " + fieldName
        arcpy.AddMessage("Dissolving events for " + outTable + " using " + EventDissolveString + " by field " + fieldName)
        arcpy.DissolveRouteEvents_lr(Table,EventDissolveString,fieldName,outTable,EventDissolveString,"DISSOLVE","INDEX")
        print "Adding date fields"
        arcpy.AddMessage("Adding date fields")
        #Add from and to date fields
        if (UniqueValToDomain.FindField(outTable, "FromDate") == None):
            arcpy.AddField_management(outTable, "FromDate", "DATE")
        if (UniqueValToDomain.FindField(outTable, "ToDate") == None):
            arcpy.AddField_management(outTable, "ToDate", "DATE")
        print "Creating domain"
        arcpy.AddMessage("Creating domain")
        UniqueValToDomain.UniqueValueToDomain(Workspace, outTable, fieldName, "d" + fieldName)
        print "Registering as versioned"
        arcpy.AddMessage("Registering as versioned")
        try:
            arcpy.RegisterAsVersioned_management(outTable,"NO_EDITS_TO_BASE")
        except:
            arcpy.AddMessage("Unable to register table " + outTable + " as versioned")


