1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
// Méthode 'Check' : Code Partagé utlisé par les 3 Actions de Check + Utilisé partiellement par d'autres Bloc
namespace ZennoLab.SharedCode
{
// ======= Partie 1 - Définition de la classe FilePathHelper pour la gestion des chemins de fichiers =======
public static class FilePathHelper
{
// 1.2 Construction des chemins de fichiers personnalisés en fonction du type de fichier et de l'extension.
public static string[] BuildFilePaths(IZennoPosterProjectModel project, string fileType, string fileExtension)
{
string baseDirectory = project.Directory + @"\02 - The Project\";
string baseFilePath = baseDirectory + project.Variables["P_Language_3_Name_Plus_ID"].Value + @"\" + project.Variables["P_Theme_3_Name_Plus_ID"].Value;
string suffix = " - ";
string customFolder = @"\01 - In\" + fileType + suffix;
string shareSubFolder = @"\000 - Share\" + customFolder;
string commonPath = baseFilePath + @"\" + project.Variables["P_Sub_3_Theme_Plus_ID"].Value;
// 1.2.2 Chemins pour les différents types de fichiers.
string customFilePath = commonPath + @"\" + project.Variables["M_Compte_3_Name_Plus_ID"].Value + customFolder + project.Variables["M_Compte_2_Name"].Value + suffix + "Custom." + fileExtension;
string shareSubFilePath = commonPath + shareSubFolder + project.Variables["P_Sub_2_Theme_Name"].Value + suffix + "Share Sub." + fileExtension;
string shareThemeFilePath = baseFilePath + shareSubFolder + project.Variables["P_Theme_2_Name"].Value + suffix + "Share Theme." + fileExtension;
// Supprime les doubles barres obliques inversées (\\) dans les chemins de fichiers revenir sur ce code plus tard
customFilePath = RemoveDoubleBackslashes(customFilePath);
shareSubFilePath = RemoveDoubleBackslashes(shareSubFilePath);
shareThemeFilePath = RemoveDoubleBackslashes(shareThemeFilePath);
return new[] { customFilePath, shareSubFilePath, shareThemeFilePath };
}
// 1.3 Vérification de l'existence du fichier et retour du chemin si trouvé.
public static string FindExistingFilePath(IZennoPosterProjectModel project, string[] filePaths)
{
foreach (string filePath in filePaths)
{
if (File.Exists(filePath))
{
LogHelper.LogFileFound(project, filePath);
return filePath;
}
}
LogHelper.LogFileNotFoundAndThrow(project);
return null;
}
// 1.4 Lecture et retour du contenu d'un fichier.
public static string ReadFileContent(IZennoPosterProjectModel project, string filePathVariableName)
{
string filePath = project.Variables[filePathVariableName].Value;
try
{ return File.ReadAllText(filePath); }
catch (FileNotFoundException)
{ LogHelper.LogFileNotFoundAndThrow(project); return null; }
}
// 1.5 Supprime les doubles barres obliques inversées (\\) dans le chemin du fichier.
public static string RemoveDoubleBackslashes(string filePath)
{
return filePath.Replace("\\\\", "\\");
}
}
// ======= Partie 2 - Classe Action pour effectuer différentes actions liées aux fichiers =======
public static class Action
{
// Méthode générique pour lier une ressource ZennoPoster (table ou liste) à un fichier
// ou lire le contenu du fichier en fonction du type T.
public static void Check<T>(IZennoPosterProjectModel project, string fileType, string fileExtension, string variableName, string resourceName, string variablePathName, string variableContentName)
{
string foundFilePath = FilePathHelper.FindExistingFilePath(project, FilePathHelper.BuildFilePaths(project, fileType, fileExtension));
project.Variables[variableName].Value = foundFilePath;
if (typeof(T) == typeof(IZennoList))
{
IZennoList resource = project.Lists[resourceName] as IZennoList;
resource?.Bind(foundFilePath);
}
else if (typeof(T) == typeof(IZennoTable))
{
IZennoTable resource = project.Tables[resourceName] as IZennoTable;
resource?.Bind(foundFilePath);
}
else
{
// Utilisez la méthode ReadFileContent de la classe FilePathHelper pour la lecture du contenu.
project.Variables[variableContentName].Value = FilePathHelper.ReadFileContent(project, variablePathName);
}
LogHelper.LogFileBound(project, resourceName, foundFilePath);
}
}
// Fin de la Partie 2
// ======= Partie 3 - Classe LogHelper pour la journalisation des événements liés aux fichiers =======
public static class LogHelper
{
public static void LogFileFound(IZennoPosterProjectModel project, string filePath)
{
// 3.1 Journalisation que le fichier est trouvé sur une ligne de log.
project.SendInfoToLog("File found at:");
project.SendInfoToLog(filePath);
}
public static void LogFileNotFoundAndThrow(IZennoPosterProjectModel project)
{
// 3.2 Journalisation en jaune et exception si aucun fichier n'est trouvé.
string errorMessage = "No files were found at the 3 specified locations.";
project.SendToLog(errorMessage, ZennoLab.InterfacesLibrary.Enums.Log.LogType.Info, false, ZennoLab.InterfacesLibrary.Enums.Log.LogColor.Red);
throw new FileNotFoundException(errorMessage);
}
public static void LogFileBound(IZennoPosterProjectModel project, string resourceName, string filePath)
{
// 3.3 Journalisation si le fichier est lié avec succès à une table.
project.SendInfoToLog("File successfully bound to resource: " + resourceName);
}
}
// Fin de la Partie 3
} |
Partager