274 lines
11 KiB
C#
274 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.IO;
|
|
using OliviaAddInPro.Model;
|
|
using OliviaAddInPro.Helper;
|
|
using System.Windows;
|
|
|
|
namespace OliviaAddInPro.Services
|
|
{
|
|
public class FinProcServ
|
|
{
|
|
public void finEjecuta(Respuesta<TiposEjecucion> res, TratamientoComun inst)
|
|
{
|
|
//REVISAR CUANDO ACABA, SE CAMBIE EL MARCHANDO PARA QUE SE LE DE A FINALIZAR Y TERMINA OK
|
|
|
|
/*String msg=string.Empty;
|
|
//gestiona los flags, el estado de finok o finnok va en res.Vale
|
|
if (res.HasError)
|
|
{
|
|
msg = res.Error.First();
|
|
//actualiza los flags
|
|
OliviaGlob.RemoveFlagTipEjec(TiposEjecucion.FinEjecNOk);
|
|
}
|
|
else
|
|
{
|
|
//actualiza los flags
|
|
OliviaGlob.RemoveFlagTipEjec(TiposEjecucion.FinEjecOk);
|
|
//importa resultados
|
|
var resp = IniImport();
|
|
if (resp.HasError)
|
|
msg = resp.Error.First();
|
|
else
|
|
{
|
|
string GdbFileName = resp.Value;
|
|
var resp2 = new Respuesta<bool> { Value=false};
|
|
if (OliviaGlob.HasFlagTipEjec(TiposEjecucion.EjecSecto)) //Ha terminado bien la sectorización
|
|
{
|
|
resp2 = inst.ServCom.ImportSecto(GdbFileName);
|
|
}
|
|
else if (OliviaGlob.HasFlagTipEjec(TiposEjecucion.EjecPlanif)) //Ha terminado bien la planificación
|
|
{
|
|
GuardaCsv(inst);
|
|
resp2 = inst.ServCom.ImportPlanif(GdbFileName);
|
|
}
|
|
if (!resp2.Value)
|
|
{
|
|
msg = resp2.Error.First();
|
|
}
|
|
else
|
|
{
|
|
msg = Resource1.String_exito;
|
|
//pone modo config2
|
|
OliviaGlob.AddFlagTipEjec(TiposEjecucion.Config2);
|
|
}
|
|
}
|
|
}
|
|
//borra los archivos que le toca borrar
|
|
BorraFiles();
|
|
HelperGlobal.ponMsg(msg);
|
|
//Application.Current.Dispatcher.Invoke(new Action<Respuesta<TiposEjecucion>, TratamientoComun>(((p, v)) => { finEjecuta2(p, v); }));*/
|
|
Application.Current.Dispatcher.BeginInvoke(new Action<Respuesta<TiposEjecucion>, TratamientoComun>((p, v) => finEjecuta2(p, v)),res,inst);
|
|
|
|
//borra los archivos que le toca borrar
|
|
//BorraFiles();
|
|
}
|
|
|
|
public void finEjecuta2(Respuesta<TiposEjecucion> res, TratamientoComun inst)
|
|
{
|
|
String msg = string.Empty;
|
|
String msg_err = "Errores en el proceso de importación de resultados";
|
|
bool mal = false;
|
|
try
|
|
{
|
|
|
|
|
|
//gestiona los flags, el estado de finok o finnok va en res.Vale
|
|
if (res.HasError)
|
|
{
|
|
msg = res.Error.First();
|
|
//actualiza los flags
|
|
OliviaGlob.RemoveFlagTipEjec(TiposEjecucion.FinEjecNOk);
|
|
}
|
|
else
|
|
{
|
|
//actualiza los flags
|
|
OliviaGlob.RemoveFlagTipEjec(TiposEjecucion.FinEjecOk);
|
|
//importa resultados
|
|
var resp = IniImport();
|
|
if (resp.HasError)
|
|
{
|
|
msg = resp.Error.First();
|
|
mal = true;
|
|
}
|
|
else
|
|
{
|
|
string GdbFileName = resp.Value;
|
|
var resp2 = new Respuesta<string> { Value = string.Empty };
|
|
if (OliviaGlob.HasFlagTipEjec(TiposEjecucion.EjecSecto)) //Ha terminado bien la sectorización
|
|
{
|
|
//actualiza los flags
|
|
OliviaGlob.RemoveFlagTipEjec(TiposEjecucion.EjecSecto);
|
|
//acciones de importación
|
|
resp2 = inst.ServCom.ImportSecto(GdbFileName);
|
|
}
|
|
else if (OliviaGlob.HasFlagTipEjec(TiposEjecucion.EjecPlanif)) //Ha terminado bien la planificación
|
|
{
|
|
//actualiza los flags
|
|
OliviaGlob.RemoveFlagTipEjec(TiposEjecucion.EjecPlanif);
|
|
//guarda csv
|
|
GuardaCsv(inst);
|
|
//acciones de importación
|
|
resp2 = inst.ServCom.ImportPlanif(GdbFileName);
|
|
|
|
}
|
|
if (string.IsNullOrEmpty(resp2.Value))
|
|
{
|
|
if (resp2.HasError)
|
|
{
|
|
msg = resp2.Error.First();
|
|
}
|
|
else
|
|
msg = msg_err;
|
|
mal = true;
|
|
}
|
|
else
|
|
{
|
|
msg = Resource1.String_exito;
|
|
//pone modo config2
|
|
OliviaGlob.AddFlagTipEjec(TiposEjecucion.Config2);
|
|
//actualiza la capa de la que tiene que leer ahora para planificar
|
|
inst.CapaElems = resp.Value;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
msg = msg_err + ": "+msg+": "+ex.Message;
|
|
mal = true;
|
|
}
|
|
if (mal)
|
|
{
|
|
//reinicia flags
|
|
if(OliviaGlob.IsLimp())
|
|
OliviaGlob.SetFlagTipEjec(TiposEjecucion.Limp);
|
|
else if(OliviaGlob.IsReco())
|
|
OliviaGlob.SetFlagTipEjec(TiposEjecucion.Reco);
|
|
|
|
}
|
|
HelperGlobal.ponMsg(msg);
|
|
OliviaGlob.progrDialog.Hide();
|
|
//muestra la ventana
|
|
OliviaGlob.ShowHidePane(true);
|
|
var task = ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
|
|
{
|
|
//borra los archivos que le toca borrar
|
|
BorraFiles();
|
|
});
|
|
//task.Wait();
|
|
//borra los archivos que le toca borrar
|
|
//BorraFiles();
|
|
}
|
|
|
|
/**
|
|
* Borra los archivos exportados para el proceso
|
|
*/
|
|
public static void BorraFiles()
|
|
{
|
|
string[] list = null;
|
|
string capa_principal = System.IO.Path.GetFileNameWithoutExtension(OliviaGlob.Paths.PathData);
|
|
string capa_principal_nw = System.IO.Path.GetFileNameWithoutExtension(OliviaGlob.Paths.PathNW);
|
|
|
|
try
|
|
{
|
|
if (capa_principal != null && HelperGdb.CloseLayer(capa_principal))
|
|
{
|
|
list = System.IO.Directory.GetFiles(OliviaGlob.Paths.DirData, capa_principal + "*");
|
|
if (list.Length > 0)
|
|
{
|
|
foreach (string f in list)
|
|
{
|
|
if (System.IO.Path.GetExtension(f) == ".lock")
|
|
continue;
|
|
System.IO.File.Delete(f);
|
|
}
|
|
}
|
|
}
|
|
if (capa_principal_nw != null && HelperGdb.CloseLayer(capa_principal_nw))
|
|
{
|
|
list = System.IO.Directory.GetFiles(OliviaGlob.Paths.DirData, capa_principal_nw + "*");
|
|
if (list.Length > 0)
|
|
{
|
|
foreach (string f in list)
|
|
{
|
|
if (System.IO.Path.GetExtension(f) == ".lock")
|
|
continue;
|
|
System.IO.File.Delete(f);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
}
|
|
}
|
|
/*
|
|
* Permite guardar el archivo CSV que contiene la secuencia que se ha llevado a cabo en las rutas en la planificación.
|
|
*/
|
|
public static void GuardaCsv(TratamientoComun inst)
|
|
{
|
|
string auxi, nombre = null;
|
|
string[] nameDokL;
|
|
string DirData = System.IO.Path.GetDirectoryName(OliviaGlob.Paths.PathData);
|
|
nameDokL = Directory.GetFiles(DirData,
|
|
System.IO.Path.GetFileNameWithoutExtension(OliviaGlob.Paths.PathData) + inst.ServCom.NAME_CSV + ".*");
|
|
|
|
string Title = "Guardar Secuencia de la Planificación";
|
|
string Filter = "Secuencia en formato CSV (*.csv)|*.csv";
|
|
inst.decode_gdb(System.IO.Path.GetFileNameWithoutExtension(OliviaGlob.Paths.PathData),
|
|
out auxi, out nombre);
|
|
string InitialDirectory = System.IO.Path.Combine(DirData, nombre);
|
|
|
|
|
|
string FileName = HelperGdb.SaveFileDlg(Title, InitialDirectory, inst.ServCom.EXT_CSV, Filter);
|
|
|
|
if (string.IsNullOrEmpty(FileName) || FileName.Length == 0)
|
|
{
|
|
bool ok = HelperGlobal.ponMsg("Se va a perder el archivo que contiene la secuencia en planificación, ¿está seguro?",
|
|
MessageBoxImage.Question,"OLIVIA",MessageBoxButton.YesNo);
|
|
if (!ok)
|
|
GuardaCsv(inst);
|
|
}
|
|
else
|
|
{
|
|
if (File.Exists(FileName))
|
|
File.Delete(FileName);
|
|
File.Move(nameDokL[0], FileName);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Inicializa la importación
|
|
*/
|
|
public Respuesta<string> IniImport()
|
|
{
|
|
var res = new Respuesta<string>() { Value = string.Empty };
|
|
//Lanza ventana para elegir gdb a la que importar resultados
|
|
bool sal = true;
|
|
string GdbFileName="";
|
|
do
|
|
{
|
|
sal = true;
|
|
//repite por si se ha equivocado hasta que elige la gdb
|
|
GdbFileName = HelperGdb.OpenFileDialog(HelperGdb.TiposOpenFileDlg.OpenGdb,"", "Seleccionar GDB a la que importar los resultados");
|
|
if(string.IsNullOrEmpty(GdbFileName))
|
|
sal= HelperGlobal.ponMsg("¿Desea cancelar el proceso de imporación?",
|
|
MessageBoxImage.Question, "OLIVIA", MessageBoxButton.YesNo);
|
|
} while (!sal);
|
|
|
|
if (string.IsNullOrEmpty(GdbFileName) || !Directory.Exists(GdbFileName))
|
|
{
|
|
res.Error.Add("Se ha cancelado la importación de resultados. Se cancela la elección de GDB.");
|
|
return res;
|
|
}
|
|
res.Value = GdbFileName;
|
|
return res;
|
|
}
|
|
}
|
|
}
|