OfflineUpAndDownOneDrive_Impl.cs
1 using sage.ew.enumerations;
2 using sage.ew.global;
4 using sage.ew.interficies;
5 using System;
6 using System.Collections.Generic;
7 using System.IO;
8 
9 namespace sage.addons.offline.Negocio.Clases
10 {
14  internal class OfflineUpAndDownOneDrive_Impl : IOfflineUpAndDown
15  {
16  private IFileDialog365 _oFileDialog365;
17  readonly TipoConexion _oTipoConexionServicio = TipoConexion.OneDrive;
18 
19  #region Constructores
20 
21 
25  public OfflineUpAndDownOneDrive_Impl(int idConf = 0)
26  {
27  //Hay que indicarle un IdConf para poder recuperar las credenciales
28  EW_GLOBAL.ValorEnClave_VarGlob("wn_offlineIdConf365", idConf);
29 
31  _oFileDialog365 = factory._NewFileDialog365(_oTipoConexionServicio, TipoOneDrive.ComunicacionesOffline);
32  }
33 
34 
35  #endregion Constructores
36 
37  public bool Comprobar_Conexion(FtpUserData toFtpUserData, bool lbAsistConfSig)
38  {
39  //No hay operación de comprobación para OneDrive como tal. Si se invoca algún método se procede al login fuera del programa y se pierde el control
40  //y el resultado... No obtante probamos de ejecutar alguna operación.
41 
42  var nombreDirectorio = @"od:\UnitTestBorrar\";
43  var resultado = false;
44  if (_oFileDialog365._DirectoryExists(nombreDirectorio))
45  {
46  _oFileDialog365._DeleteFolder(nombreDirectorio);
47  }
48  resultado =_oFileDialog365._CreateFolder(nombreDirectorio);
49  resultado = resultado && _oFileDialog365._DeleteFolder(nombreDirectorio);
50 
51  if (!resultado)
52  {
53  if (!lbAsistConfSig)
54  {
55  toFtpUserData.error_message = "Se ha producido un error al obtener datos del OneDrive.";
56  }
57  else
58  {
59  toFtpUserData.error_message = "Para avanzar al siguiente paso debe definir una configuración de conexión. Configure Servidor FTP o OneDrive para poder continuar.";
60  }
61  }
62 
63  return resultado;
64  }
65 
66 
67 
77  public bool Descargar_Fichero(string path, string fileName, string carpeta, int idConf, bool deleteSource)
78  {
79  try
80  {
81  //Verificar donde debe descargar los ficheros
82  var rutaServidor = Convert.ToString(EW_GLOBAL._GetVariable("wc_iniservidor")).Trim();
83  rutaServidor = Path.Combine(rutaServidor, "MODULOS\\offline\\descargas");
84 
85  string rutaFicherosServidor = Path.Combine(rutaServidor, path);
86  string fullLocalFileName = Path.Combine(rutaFicherosServidor, fileName);
87 
88  if (Directory.Exists(rutaFicherosServidor))
89  {
90  if (File.Exists(fullLocalFileName))
91  File.Delete(fullLocalFileName);
92  }
93  else
94  {
95  Directory.CreateDirectory(path);
96  }
97 
98  var oneDriveFileName = Path.Combine(carpeta, fileName);
99 
100  var llOk = DescargarArchivoOneDrive(oneDriveFileName, fullLocalFileName, true, deleteSource);
101 
102  return llOk;
103 
104  }
105  catch (Exception loEx)
106  {
107  string error = "Se ha producido un error al obtener datos de OneDrive (" + loEx.Message + ").";
108  //callback?.Invoke(error);
109  return false;
110  }
111  }
112 
113 
122  private bool DescargarArchivoOneDrive(string oneDriveFileName, string fullLocalFileName, bool uncompressZip, bool deleteSource)
123  {
124 
125  var llOk = _oFileDialog365._DownLoadFile(oneDriveFileName, fullLocalFileName);
126 
127  if (uncompressZip && File.Exists(fullLocalFileName))
128  {
129  clsOffLine utilsOffline = new clsOffLine();
130  utilsOffline.Fichero_Descomprimir(fullLocalFileName, Path.GetDirectoryName(fullLocalFileName), false);
131  }
132 
133  if (deleteSource)
134  {
135  _oFileDialog365._DeleteFile(oneDriveFileName);
136  }
137 
138  return llOk;
139  }
140 
150  public bool Subir_Fichero(string archivo, string zipFilename, string carpeta, int idConf, ref long uploadedFileSize)
151  {
152  var llOk = false;
153 
154  try
155  {
156  //Hay dos llamadas distintas a este método. Una de ellas (el envio general) sin valor para este parámetro y presuntament en archivo llega una ruta
157  //completa
158  if (!string.IsNullOrWhiteSpace(zipFilename))
159  {
160  if (File.Exists(archivo))
161  {
162  clsOffLine utilsOffline = new clsOffLine();
163  string pathArchivoComprimido = utilsOffline.Fichero_Comprimir(archivo, zipFilename);
164 
165  archivo = Path.Combine(Path.GetDirectoryName(archivo), pathArchivoComprimido);
166  archivo = Path.Combine(archivo, zipFilename);
167  }
168  }
169 
170  if (carpeta != "/") //Raiz... lo ignoro
171  llOk = GarantizarDirectorioDestino(carpeta);
172  else
173  llOk = true;
174 
175  if (llOk)
176  {
177  var destino = Path.Combine(carpeta, Path.GetFileName(archivo));
178  llOk = _oFileDialog365._UploadFile(archivo, destino);
179  if(!llOk && !string.IsNullOrWhiteSpace(_oFileDialog365._ErrorMessage))
180  {
181  //callback?.Invoke(_oFileDialog365._ErrorMessage);
182  return false;
183  }
184  }
185 
186  uploadedFileSize = new FileInfo(archivo).Length;
187 
188  return llOk;
189  }
190  catch (Exception loEx)
191  {
192  string error = "Se ha producido un error al enviar datos a OneDrive (" + loEx.Message + ").";
193  //callback?.Invoke(error);
194  return false;
195  }
196  }
197 
198  #region Métodos privados
199 
205  private bool GarantizarDirectorioDestino(string tcFolderUpload)
206  {
207  bool llOk = true;
208 
209  if (!_oFileDialog365._DirectoryExists(tcFolderUpload))
210  {
211  llOk = _oFileDialog365._CreateFolder(tcFolderUpload);
212 
213  llOk = _oFileDialog365._DirectoryExists(tcFolderUpload);
214 
215  if (!llOk)
216  {
217  // TODO: Gestión de errores
218  }
219  }
220 
221  return llOk;
222  }
223 
224  public List<string> GetListFiles(string carpetaserv, int idConf)
225  {
226  if (_oFileDialog365._DirectoryExists(carpetaserv))
227  {
228  var ficheros = _oFileDialog365._GetFolderFilenamesList(carpetaserv);
229  return ficheros;
230  }
231  else
232  {
233  return new List<string>();
234  }
235  }
236 
237 
238  #endregion Métodos privados
239  }
240 }
Interficie del gestor de ficheros o365
Interfície de la clase de instanciación de objetos y clases O365
Definition: IO365Factory.cs:9
object Retrieve(Type T)
Resuelve objeto de tipo T
Es como el tipo de entrada asientos pero por negocio, sin formulario, pq quiero que me haga las propu...
TipoOneDrive
Tipos de OneDrive dentro de Sage50
IFileDialog365 _NewFileDialog365(TipoConexion conexion, TipoOneDrive tipoOneDrive)
Obtiene una nueva instancia de tipo IFileDialog365 con los parámetros indicados
TipoConexion
Tipo de conexión
Clase Inyector de dependencias para resolver las dependencias