def delete_fcs(workspace_local):
    arcpy.env.workspace = workspace_local
    print workspace_local
    for fc in mc.get_gps_fcs(workspace_local, True):
        print fc
        if fc != 'NISOURCE.GPS_CustomerConnection_G':
            delete_fc(workspace_local, fc)
def clear_workspace_gisids(env):
    for fc in MC.get_gps_fcs(env, True):
        print fc
        MC.disable_editor_tracking(env, fc)
        arcpy.env.workspace = env
        clear_fc_gisids(env, fc)
        MC.enable_editor_tracking(env, fc)
def copy_fcs_by_state(logger_local_fcs, source_workspace, target_workspace, state_of_interest):
    for fc in MC.get_gps_fcs(source_workspace):
        print fc
        sourceFC = os.path.join(source_workspace, fc)
        targetFC = os.path.join(target_workspace, fc)

        # MC.disable_editor_tracking(target_workspace, fc)
        # MC.disable_spatial_index(target_workspace, fc)

        arcpy.env.workspace = target_workspace
        copy_fc(logger_local_fcs, target_workspace, sourceFC, targetFC, state_of_interest)
    for k in prod_shape_dict:
        if k in pp_shape_dict:
            for coord in prod_shape_dict[k]:
                if coord not in pp_shape_dict[k]:
                    # print fc, k, coord, "has moved!"
                    for point in pp_point_dict[k]:
                        print ",".join(map(str, point)).replace("(", "").replace(")", "")
                    for point in prod_point_dict[k]:
                        print ",".join(map(str, point)).replace("(", "").replace(")", "")


pp_workspace = r"connections\z_PROD_xs08_sde.sde"  # mc.get_preprod_workspace()
p_workspace = r"connections\z_PROD_xs09_sde.sde"  # mc.get_prod_workspace()

# fc = "NISOURCE.GPS_CUSTOMERCONNECTION_G"

for fc in mc.get_gps_fcs(p_workspace):
    find_diff(pp_workspace, p_workspace, fc)


# d = dict((k, v) for k, v in pp_d1.items() if len(v) > 1)
# for s in d:
#     print s, d[s]
#
# print 'Prod now....'


# d = dict((k, v) for k, v in prod_d1.items() if len(v) > 1)
# for s in d:
#     print s, d[s]
def assign_workspace_gisids(workspace):
    for fc in MC.get_gps_fcs(workspace, True):
        print fc
        # MC.disable_editor_tracking(workspace, fc)
        arcpy.env.workspace = workspace
        assign_fc_gisids(workspace, fc)

#
# start of program
#
logger = MC.setup_logging('migrate1')
logger.info("Starting program...")

# setting this as it should start with this number for Migration 1
gisid_seed = 70000001  # MC.read_gisid_seed()
MC.write_gisid_seed(gisid_seed)

pp_workspace = MC.get_preprod_workspace()
p_workspace = MC.get_prod_workspace()

pp_final_list = []
p_final_list = []

total_matched_count = 0

with open("results/mig1_results.txt", "w+") as p_f:
    for fc in MC.get_gps_fcs(p_workspace):
        # fc = 'GPS_ControllableFitting'
        logger.info(fc)
        gisid_seed, fc_count = assign_gisid_matches(pp_workspace, p_workspace, fc, pp_final_list, p_final_list,
                                                    gisid_seed, p_f)
        total_matched_count += fc_count

logger.info("Total Matched|" + str(total_matched_count))
logger.info("Finished program")
import Migrate2 as M2
import MigrationCommon as MC

#
# start of program
#
log_name = 'migrate2_PP'
logger = MC.setup_logging(log_name)
logger.info("Starting program...")

pp_workspace = MC.get_preprod_workspace()

results_file = "results/mig1_results.txt"
pp_dict = M2.load_dict(results_file, True)

logger.info(pp_workspace)
pp_where = '"POINTRECORD_ID" IS NOT NULL AND "QASTATUS" = 600 AND "GISID" IS NULL'
M2.update_fcs_gisids(logger, pp_workspace, MC.get_gps_fcs(pp_workspace), pp_dict, pp_where)

MC.send_finished_email(log_name)

logger.info("Finished program")
#
print str(datetime.datetime.now())

# ArcFM licensing
import win32com.client

app = win32com.client.Dispatch("Miner.Framework.Dispatch.MMAppInitializeDispatch")
runtime = win32com.client.Dispatch("Miner.Framework.Dispatch.MMRuntimeEnvironmentDispatch")
au = win32com.client.Dispatch('Miner.Framework.Dispatch.MMAutoupdaterDispatch')
runtime.RuntimeMode = 0x4  # mmRuntimeModeArcServer
app.Initialize(0x5)
au.AutoUpdaterMode = 0x8
# end ArcFm licensing

r2d2_preprod = MC.get_r2d2_preprod_workspace_nisource('OH')
print r2d2_preprod
arcpy.env.workspace = r2d2_preprod

# arcpy.UnregisterAsVersioned_management("NISOURCE.GPSDataset", "NO_KEEP_EDIT", "COMPRESS_DEFAULT")

for fc in MC.get_gps_fcs(r2d2_preprod, True):
    truncate_fc(r2d2_preprod, fc)

# arcpy.RegisterAsVersioned_management("NISOURCE.GPSDataset", "NO_EDITS_TO_BASE")

app.Shutdown

del app, runtime, au

print str(datetime.datetime.now())
app.Initialize(0x5)
au.AutoUpdaterMode = 0x8
# end ArcFm licensing

r2d2_prod = MC.get_r2d2_prod_workspace('VA')
print r2d2_prod
arcpy.env.workspace = r2d2_prod

# fc = r"NISOURCE.GPS_GasValve"

# oidValueList = [r[0] for r in arcpy.da.SearchCursor(fc, ["OID@"])]
# oidValueList.sort()

# where = """"OBJECTID" >= {0}"""

for fc in MC.get_gps_fcs(r2d2_prod):
    delete_fc(r2d2_prod, fc)

delete_fc(r2d2_prod, "NISOURCE.GPSWorkAreaPolygon")

# table = os.path.join(r2d2_preprod, fc)

# for table in MC.get_gps_fcs(r2d2_preprod):
#    print 'Deleting rows from ' + table
#    arcpy.DeleteRows_management(table)


app.Shutdown

del app, runtime, au