/// <summary> /// Copy the LOCAL structure of files and directories TO the original remote location, /// eliminating superflous Excel files. /// </summary> /// <param name="FileElaborati">List of files originally intended to be elaborated</param> /// <returns>State of the elaboration</returns> public static bool RemoteSet(List <string> FileElaborati) { Logger.PrintLC("## Copying back files from local to remote.", 2, ConfigFile.INFO); //ConfigFile.ROOT = ConfigFile.TEMP_REMOTE_ROOT; try { DirOps.Copy(ConfigFile.LOCAL_DIR_FULL, ConfigFile.TEMP_REMOTE_ROOT); } catch { Logger.PrintLC("Error while copying local temporary structure to remote structure.", 2, ConfigFile.ERROR); return(false); } DirectoryInfo local = new DirectoryInfo(ConfigFile.LOCAL_DIR_FULL); if (local != null) { if (ConfigFile.REMOVE_LOCAL) { DirOps.TraverseDirectory(local); } } foreach (var elem in FileElaborati) { string file = elem.Replace(ConfigFile.LOCAL_DIR_FULL + @"\", ConfigFile.TEMP_REMOTE_ROOT); FileInfo fileI = new FileInfo(file); FileInfo fileIU = null; switch (fileI.Extension) { case ".xls": fileIU = new FileInfo(Path.Combine(fileI.DirectoryName, Path.GetFileNameWithoutExtension(fileI.FullName) + ".XLS")); //fileI = fileIU; break; case ".xlsx": fileIU = new FileInfo(Path.Combine(fileI.DirectoryName, Path.GetFileNameWithoutExtension(fileI.FullName) + ".XLSX")); //fileI = fileIU; break; } if (fileI.Exists || fileIU.Exists) { if (fileIU.Exists) { //fileI = fileIU; } string name = fileI.Name; string dir = fileI.DirectoryName; string estensione = string.Empty; string textNameOK = string.Empty; string textNameKO = string.Empty; FileInfo fileTestoOK = null; FileInfo fileTestoKO = null; try { if (fileI.Extension == ".xls") { estensione = ".xls"; textNameOK = name.Replace(".xls", "_OK.txt"); textNameKO = name.Replace(".xls", "_KO.txt"); } if (fileI.Extension == ".xlsx") { estensione = ".xlsx"; textNameOK = name.Replace(".xlsx", "_OK.txt"); textNameKO = name.Replace(".xlsx", "_KO.txt"); } if (fileI.Extension == ".XLS") { estensione = ".XLS"; textNameOK = name.Replace(".XLS", "_OK.txt"); textNameKO = name.Replace(".XLS", "_KO.txt"); } if (fileI.Extension == ".XLSX") { estensione = ".XLSX"; textNameOK = name.Replace(".XLS", "_OK.txt"); textNameKO = name.Replace(".XLSX", "_KO.txt"); } fileTestoOK = new FileInfo(Path.Combine(dir, textNameOK)); fileTestoKO = new FileInfo(Path.Combine(dir, textNameKO)); } catch (Exception exp) { Logger.PrintLC("Errore 9: " + exp.Message); continue; } if (fileTestoKO.Exists) { if (!ConfigFile.DEST_FOLD_UNIQUE) { try { string dirDestinationKO = Funct.GetFolderDestination(fileI.FullName, estensione); FileInfo fileCopiare = new FileInfo(dirDestinationKO); try { fileI.IsReadOnly = false; //fileI.MoveTo(fileCopiare.DirectoryName); fileI.MoveTo(dirDestinationKO); //fileI.Delete(); } catch (Exception exp) { try { if (exp.HResult == -2147024713) { fileI.Delete(); } //if (exp.Message == "Impossibile creare un file, se il file esiste giĆ .\r\n") // fileI.Delete(); } catch { } } try { fileIU.IsReadOnly = false; fileIU.MoveTo(fileCopiare.DirectoryName + @"\"); fileIU.Delete(); } catch (Exception exp) { try { //fileIU.Delete(); } catch { } } //if (!fileCopiare.Exists) //{ // fileI.MoveTo(fileCopiare.DirectoryName); //} //else //{ // fileCopiare.Delete(); // fileI.MoveTo(fileCopiare.DirectoryName); //} } catch (Exception exp) { Logger.PrintLC("Errore 10: " + exp.Message); } } else { try { fileI.MoveTo(Path.Combine(ConfigFile.ROOT, ConfigFile.DEST_FOLD_NAME)); } catch (Exception exp) { Logger.PrintLC("Errore 11: " + exp.Message); } } } if (fileTestoOK.Exists) { try { fileI.Delete(); } catch (Exception exp) { Logger.PrintLC("Errore 12: " + exp.Message); } try { string fileXLSX = Path.GetFileNameWithoutExtension(fileI.FullName); FileInfo fileIxls = new FileInfo(Path.Combine(fileI.DirectoryName, fileXLSX + ".xlsx")); FileInfo fileIXLSX = new FileInfo(Path.Combine(fileI.DirectoryName, fileXLSX + ".XLSX")); try { fileIxls.Delete(); } catch { } try { fileIXLSX.Delete(); } catch { } } catch { } } } } return(true); }
/// <summary> /// Reads and processes Attributes data from excel's 'ATTRIBUTI' sheet /// </summary> /// <param name="fileDaAprire"></param> /// <param name="sheet"></param> /// <returns></returns> public static List <AttributeT> ReadXFileAttribute(FileInfo fileDaAprire, string db, string sheet = ConfigFile.ATTRIBUTI) { string file = fileDaAprire.FullName; List <AttributeT> listaFile = new List <AttributeT>(); if (!File.Exists(file)) { Logger.PrintLC("Reading Attributes. File " + fileDaAprire.Name + " doesn't exist.", 2); return(listaFile = null); } FileOps.RemoveAttributes(file); if (fileDaAprire.Extension == ".xls") { if (!ConvertXLStoXLSX(file)) { return(listaFile = null); } file = Path.ChangeExtension(file, ".xlsx"); fileDaAprire = new FileInfo(file); } ExcelPackage p = null; ExcelWorkbook WB = null; ExcelWorksheets ws = null; try { p = new ExcelPackage(fileDaAprire); WB = p.Workbook; ws = WB.Worksheets; //.Add(wsName + wsNumber.ToString()); } catch (Exception exp) { Logger.PrintLC("Reading Attributes. Could not open file " + fileDaAprire.Name + "in location " + fileDaAprire.DirectoryName, 2); return(listaFile = null); } bool FilesEnd = false; int EmptyRow = 0; foreach (var worksheet in ws) { if (worksheet.Name == sheet) { FilesEnd = false; for (int RowPos = ConfigFile.HEADER_RIGA + 1; FilesEnd != true; RowPos++) { bool incorrect = false; string nomeTabella = worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Nome Tabella Legacy"]].Text; string nomeCampo = worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Nome Campo Legacy"]].Text; string dataType = worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Datatype"]].Text; string chiave = worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Chiave"]].Text; string unique = worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Unique"]].Text; string chiaveLogica = worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Chiave Logica"]].Text; string mandatoryFlag = worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Mandatory Flag"]].Text; string dominio = worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Dominio"]].Text; string storica = worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Storica"]].Text; string datoSensibile = worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Dato Sensibile"]].Text; worksheet.Cells[RowPos, ConfigFile.HEADER_COLONNA_MAX_ATTRIBUTI + ConfigFile.ATTRIBUTI_EXCEL_COL_OFFSET2].Value = ""; string error = ""; //Check Nome Tabella Legacy if (string.IsNullOrWhiteSpace(nomeTabella)) { incorrect = true; error += "NOME TABELLA LEGACY mancante."; } //Check Nome Campo Legacy if (string.IsNullOrWhiteSpace(nomeCampo)) { incorrect = true; if (!string.IsNullOrWhiteSpace(error)) { error += " "; } error += "NOME CAMPO LEGACY mancante."; } //Check DataType if (string.IsNullOrWhiteSpace(dataType)) { incorrect = true; if (!string.IsNullOrWhiteSpace(error)) { error += " "; } error += "DATATYPE mancante."; } else { if (!Funct.ParseDataType(dataType, db)) { incorrect = true; if (!string.IsNullOrWhiteSpace(error)) { error += " "; } error += "DATATYPE non conforme."; } } //Check Chiave if (!(string.Equals(chiave, "S", StringComparison.OrdinalIgnoreCase) || string.Equals(chiave, "N", StringComparison.OrdinalIgnoreCase))) { incorrect = true; if (!string.IsNullOrWhiteSpace(error)) { error += " "; } error += "CHIAVE non conforme."; } //Check Unique if (!(string.Equals(unique, "S", StringComparison.OrdinalIgnoreCase) || string.Equals(unique, "N", StringComparison.OrdinalIgnoreCase))) { incorrect = true; if (!string.IsNullOrWhiteSpace(error)) { error += " "; } error += "UNIQUE non conforme."; } //Check Chiave Logica if (!(string.Equals(chiaveLogica, "S", StringComparison.OrdinalIgnoreCase) || string.Equals(chiaveLogica, "N", StringComparison.OrdinalIgnoreCase))) { incorrect = true; if (!string.IsNullOrWhiteSpace(error)) { error += " "; } error += "CHIAVE LOGICA non conforme."; } //Check Mandatory Flag if (!(string.Equals(mandatoryFlag, "S", StringComparison.OrdinalIgnoreCase) || string.Equals(mandatoryFlag, "N", StringComparison.OrdinalIgnoreCase))) { incorrect = true; if (!string.IsNullOrWhiteSpace(error)) { error += " "; } error += "MANDATORY FLAG non conforme."; } //Check Dominio if (!(string.Equals(dominio, "S", StringComparison.OrdinalIgnoreCase) || string.Equals(dominio, "N", StringComparison.OrdinalIgnoreCase))) { incorrect = true; if (!string.IsNullOrWhiteSpace(error)) { error += " "; } error += "DOMINIO non conforme."; } //Check Storica if (!(string.Equals(storica, "S", StringComparison.OrdinalIgnoreCase) || string.Equals(storica, "N", StringComparison.OrdinalIgnoreCase))) { incorrect = true; if (!string.IsNullOrWhiteSpace(error)) { error += " "; } error += "STORICA non conforme."; } //Check Dato Sensibile if (!(string.Equals(datoSensibile, "S", StringComparison.OrdinalIgnoreCase) || string.Equals(datoSensibile, "N", StringComparison.OrdinalIgnoreCase))) { incorrect = true; if (!string.IsNullOrWhiteSpace(error)) { error += " "; } error += "DATO SENSIBILE non conforme."; } if (incorrect == false) { EmptyRow = 0; AttributeT ValRiga = new AttributeT(row: RowPos, db: db, nomeTabellaLegacy: nomeTabella); // Assegnazione valori checkati ValRiga.NomeTabellaLegacy = nomeTabella; ValRiga.NomeCampoLegacy = nomeCampo; ValRiga.DataType = dataType; if (string.Equals(chiave.ToUpper(), "S")) { ValRiga.Chiave = 0; } else { ValRiga.Chiave = 100; } ValRiga.Unique = unique; ValRiga.ChiaveLogica = chiaveLogica; if (string.Equals(mandatoryFlag.ToUpper(), "S")) { ValRiga.MandatoryFlag = 1; } else { ValRiga.MandatoryFlag = 0; } ValRiga.Dominio = dominio; ValRiga.Storica = storica; ValRiga.DatoSensibile = datoSensibile; //Assegnazione valori opzionali if (!string.IsNullOrWhiteSpace(worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["SSA"]].Text)) { ValRiga.SSA = worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["SSA"]].Text; } if (!string.IsNullOrWhiteSpace(worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Area"]].Text)) { ValRiga.Area = worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Area"]].Text; } if (!string.IsNullOrWhiteSpace(worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Definizione Campo"]].Text)) { ValRiga.DefinizioneCampo = worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Definizione Campo"]].Text; } if (!string.IsNullOrWhiteSpace(worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Tipologia Tabella \n(dal DOC. LEGACY) \nEs: Dominio,Storica,\nDati"]].Text)) { ValRiga.TipologiaTabella = worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Tipologia Tabella \n(dal DOC. LEGACY) \nEs: Dominio,Storica,\nDati"]].Text; } int t; //Funzionale all'assegnazione di 'Lunghezza' e 'Decimali' if (!string.IsNullOrWhiteSpace(worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Lunghezza"]].Text)) { if (int.TryParse(worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Lunghezza"]].Text, out t)) { ValRiga.Lunghezza = t; } } if (!string.IsNullOrWhiteSpace(worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Decimali"]].Text)) { if (int.TryParse(worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Decimali"]].Text, out t)) { ValRiga.Decimali = t; } } if (!string.IsNullOrWhiteSpace(worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Provenienza dominio "]].Text)) { ValRiga.ProvenienzaDominio = worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Provenienza dominio "]].Text; } if (!string.IsNullOrWhiteSpace(worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Note"]].Text)) { ValRiga.Note = worksheet.Cells[RowPos, ConfigFile._ATTRIBUTI["Note"]].Text; } listaFile.Add(ValRiga); worksheet.Column(ConfigFile.HEADER_COLONNA_MAX_ATTRIBUTI + ConfigFile.ATTRIBUTI_EXCEL_COL_OFFSET1).Width = 10; worksheet.Cells[RowPos, ConfigFile.HEADER_COLONNA_MAX_ATTRIBUTI + ConfigFile.ATTRIBUTI_EXCEL_COL_OFFSET1].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells[RowPos, ConfigFile.HEADER_COLONNA_MAX_ATTRIBUTI + ConfigFile.ATTRIBUTI_EXCEL_COL_OFFSET1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(34, 255, 0)); worksheet.Cells[RowPos, ConfigFile.HEADER_COLONNA_MAX_ATTRIBUTI + ConfigFile.ATTRIBUTI_EXCEL_COL_OFFSET1].Style.Font.Bold = true; worksheet.Cells[RowPos, ConfigFile.HEADER_COLONNA_MAX_ATTRIBUTI + ConfigFile.ATTRIBUTI_EXCEL_COL_OFFSET1].Value = "OK"; } else { worksheet.Column(ConfigFile.HEADER_COLONNA_MAX_ATTRIBUTI + ConfigFile.ATTRIBUTI_EXCEL_COL_OFFSET1).Width = 10; worksheet.Column(ConfigFile.HEADER_COLONNA_MAX_ATTRIBUTI + ConfigFile.ATTRIBUTI_EXCEL_COL_OFFSET2).Width = 50; worksheet.Cells[RowPos, ConfigFile.HEADER_COLONNA_MAX_ATTRIBUTI + ConfigFile.ATTRIBUTI_EXCEL_COL_OFFSET1].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells[RowPos, ConfigFile.HEADER_COLONNA_MAX_ATTRIBUTI + ConfigFile.ATTRIBUTI_EXCEL_COL_OFFSET1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(255, 0, 0)); worksheet.Cells[RowPos, ConfigFile.HEADER_COLONNA_MAX_ATTRIBUTI + ConfigFile.ATTRIBUTI_EXCEL_COL_OFFSET1].Style.Font.Bold = true; worksheet.Cells[RowPos, ConfigFile.HEADER_COLONNA_MAX_ATTRIBUTI + ConfigFile.ATTRIBUTI_EXCEL_COL_OFFSET1].Value = "KO"; worksheet.Cells[RowPos, ConfigFile.HEADER_COLONNA_MAX_ATTRIBUTI + ConfigFile.ATTRIBUTI_EXCEL_COL_OFFSET2].Value = error; EmptyRow += 1; if (EmptyRow >= 10) { FilesEnd = true; } } //****************************************** // Verifica lo stato delle successive 10 righe per determinare la fine della tabella. int prossime = 0; for (int i = 1; i < 11; i++) { if (string.IsNullOrWhiteSpace(worksheet.Cells[RowPos + i, ConfigFile._ATTRIBUTI["Nome Tabella Legacy"]].Text)) { prossime++; } } if (prossime == 10) { FilesEnd = true; } //****************************************** } p.SaveAs(new FileInfo(Path.Combine(ConfigFile.FOLDERDESTINATION, fileDaAprire.Name))); return(listaFile); } } return(listaFile = null); }