/// <summary> /// Example of how to read a PFS file /// </summary> /// <param name="filePath">File and path to the pfsExample.pfs file in the TestData folder</param> public static void OpenPFSFile(string filePath) { // Loading/Reading the file PFSFile pfsFile = new PFSFile(filePath, false); // Outmost section PFSSection target = pfsFile.GetTarget("Run11", 1); // Sub-sections PFSSection section1 = target.GetSection("Results", 1); PFSSection section2 = section1.GetSection("Result", 1); // Keywords and paramters string bridge = section2.GetKeyword("outid", 1).GetParameter(1).ToStringValue(); // Get the second key3 keyword from the run11 target PFSKeyword kwKey3 = target.GetKeyword("key3", 2); // Get the CLOB parameter PFSClob clob = kwKey3.GetParameter(1).ToClob(); // Extract values from the CLOB for (int i = 0; i < 5; i++) { double d = clob.GetDouble(); Console.Out.WriteLine("clob data: {0}", d); } }
public FileInfo GetVariableFileInfoResult(string Path, string Keyword, int ParameterIndex) { PFSFile pfsFile = null; PFSKeyword pfsKeyword = null; if (!CheckAll(Path, Keyword, out pfsFile, out pfsKeyword)) { return(null); } try { FileInfo fileInfo = new FileInfo(pfsKeyword.GetParameter(ParameterIndex).ToResultFileName()); pfsFile.Close(); return(fileInfo); } catch (Exception ex) { ErrorMessage = string.Format(CSSPDHIRes.ParameterIndex_DoesNotExistForSectionPath_AndKeyword_Error_, ParameterIndex, Path, Keyword, ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : "")); OnCSSPDHIChanged(new CSSPDHIEventArgs(new CSSPDHIMessage("Error", -1, false, ErrorMessage))); pfsFile.Close(); return(null); } }
public DateTime?GetVariableDateTime(string Path, string Keyword) { PFSFile pfsFile = null; PFSKeyword pfsKeyword = null; if (!CheckAll(Path, Keyword, out pfsFile, out pfsKeyword)) { return(null); } try { int Year = pfsKeyword.GetParameter(1).ToInt(); int Month = pfsKeyword.GetParameter(2).ToInt(); int Day = pfsKeyword.GetParameter(3).ToInt(); int Hour = pfsKeyword.GetParameter(4).ToInt(); int Minute = pfsKeyword.GetParameter(5).ToInt(); int Second = pfsKeyword.GetParameter(6).ToInt(); DateTime dateTime = new DateTime(Year, Month, Day, Hour, Minute, Second); pfsFile.Close(); return(dateTime); } catch (Exception ex) { ErrorMessage = string.Format(CSSPDHIRes.CouldNotReadValueWithPath_AndKeyword_Error_, Path, Keyword, ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : "")); OnCSSPDHIChanged(new CSSPDHIEventArgs(new CSSPDHIMessage("Error", -1, false, ErrorMessage))); pfsFile.Close(); return(null); } }
public string SetVariableString(string Path, string Keyword, int ParameterIndex, string value) { PFSFile pfsFile = null; PFSKeyword pfsKeyword = null; if (!CheckAll(Path, Keyword, out pfsFile, out pfsKeyword)) { return(null); } try { pfsKeyword.DeleteParameter(ParameterIndex); pfsKeyword.InsertNewParameterString(value, ParameterIndex); string strValue = pfsKeyword.GetParameter(ParameterIndex).ToString(); pfsFile.Write(fi.FullName); pfsFile.Close(); return(strValue); } catch (Exception ex) { ErrorMessage = string.Format(CSSPDHIRes.ParameterIndex_DoesNotExistForSectionPath_AndKeyword_Error_, ParameterIndex, Path, Keyword, ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : "")); OnCSSPDHIChanged(new CSSPDHIEventArgs(new CSSPDHIMessage("Error", -1, false, ErrorMessage))); return(null); } }
private bool TryToFindSection(PFSFile pfsFile, out PFSSection pfsSection, string Path) { pfsSection = pfsFile.GetSectionFromHandle(Path); if (pfsSection == null) { ErrorMessage = string.Format(CSSPDHIRes.CouldNotFindSectionForPath_, Path); OnCSSPDHIChanged(new CSSPDHIEventArgs(new CSSPDHIMessage("Error", -1, false, ErrorMessage))); pfsFile.Close(); return(false); } return(true); }
private bool TryToOpenPFSFile(out PFSFile pfsFile) { try { pfsFile = new PFSFile(fi.FullName, true); } catch (Exception ex) { ErrorMessage = string.Format(CSSPDHIRes.PotentiallyWrongFileTypeLookingFor_Error_, "[.m21fm or .m3fm]", ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : "")); OnCSSPDHIChanged(new CSSPDHIEventArgs(new CSSPDHIMessage("Error", -1, false, ErrorMessage))); pfsFile = null; return(false); } return(true); }
public DateTime?SetVariableDateTime(string Path, string Keyword, DateTime value) { PFSFile pfsFile = null; PFSKeyword pfsKeyword = null; if (!CheckAll(Path, Keyword, out pfsFile, out pfsKeyword)) { return(null); } try { pfsKeyword.DeleteParameter(6); pfsKeyword.DeleteParameter(5); pfsKeyword.DeleteParameter(4); pfsKeyword.DeleteParameter(3); pfsKeyword.DeleteParameter(2); pfsKeyword.DeleteParameter(1); pfsKeyword.InsertNewParameterInt(value.Year, 1); pfsKeyword.InsertNewParameterInt(value.Month, 2); pfsKeyword.InsertNewParameterInt(value.Day, 3); pfsKeyword.InsertNewParameterInt(value.Hour, 4); pfsKeyword.InsertNewParameterInt(value.Minute, 5); pfsKeyword.InsertNewParameterInt(value.Second, 6); int Year = pfsKeyword.GetParameter(1).ToInt(); int Month = pfsKeyword.GetParameter(2).ToInt(); int Day = pfsKeyword.GetParameter(3).ToInt(); int Hour = pfsKeyword.GetParameter(4).ToInt(); int Minute = pfsKeyword.GetParameter(5).ToInt(); int Second = pfsKeyword.GetParameter(6).ToInt(); DateTime dateTime = new DateTime(Year, Month, Day, Hour, Minute, Second); pfsFile.Write(fi.FullName); pfsFile.Close(); return(dateTime); } catch (Exception ex) { // need to write test code for this part ErrorMessage = string.Format(CSSPDHIRes.CouldNotSetStartTimeWithPath_Keyword_Error_, Path, Keyword, ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : "")); OnCSSPDHIChanged(new CSSPDHIEventArgs(new CSSPDHIMessage("Error", -1, false, ErrorMessage))); return(null); } }
//private string GetFileNameText(PFSFile pfsFile, string Path, string Keyword) //{ // string FileName = ""; // PFSSection pfsSectionFileName = pfsFile.GetSectionFromHandle(Path); // if (pfsSectionFileName != null) // { // PFSKeyword keyword = null; // try // { // keyword = pfsSectionFileName.GetKeyword(Keyword); // } // catch (Exception ex) // { // UpdateTaskError(AppTaskID, "PFS GetKeyword. Error [" + ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : "") + "]"); // richTextBoxStatus.AppendText("PFS GetKeyword. Error [" + ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : "") + "]\r\n"); // return FileName; // } // if (keyword != null) // { // try // { // FileName = keyword.GetParameter(1).ToFileName(); // } // catch (Exception ex) // { // UpdateTaskError(AppTaskID, "PFS GetParameter. Error [" + ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : "") + "]"); // richTextBoxStatus.AppendText("PFS GetParameter. Error [" + ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : "") + "]\r\n"); // return FileName; // } // } // } // return FileName; //} private string GetParameterResultFileName(PFSFile pfsFile, string Path, string Keyword) { //string NotUsed = ""; string FileName = ""; PFSSection pfsSection = pfsFile.GetSectionFromHandle(Path); if (pfsSection == null) { UpdateTaskError(AppTaskID, $"pfsSection not found for path {Path}"); richTextBoxStatus.AppendText($"pfsSection not found for path {Path}\r\n"); return(FileName); } PFSKeyword keyword = null; try { keyword = pfsSection.GetKeyword(Keyword); } catch (Exception ex) { UpdateTaskError(AppTaskID, "PFS GetKeyword. Error [" + ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : "") + "]"); richTextBoxStatus.AppendText("PFS GetKeyword. Error [" + ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : "") + "]\r\n"); return(FileName); } if (keyword != null) { try { FileName = keyword.GetParameter(1).ToResultFileName(); } catch (Exception ex) { UpdateTaskError(AppTaskID, "PFS GetParameter. Error [" + ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : "") + "]"); richTextBoxStatus.AppendText("PFS GetParameter. Error [" + ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : "") + "]\r\n"); return(FileName); } } return(FileName); }
private bool CheckAll(string path, string keyword, out PFSFile pfsFile, out PFSKeyword pfsKeyword) { if (!CheckFileExtension()) { // ErrorMessage set in CheckFileExtension pfsFile = null; pfsKeyword = null; return(false); } if (!TryToOpenPFSFile(out pfsFile)) { // ErrorMessage set in CheckFileExtension pfsFile = null; pfsKeyword = null; return(false); } PFSSection pfsSection = null; if (!TryToFindSection(pfsFile, out pfsSection, path)) { // ErrorMessage set in CheckFileExtension pfsFile.Close(); pfsFile = null; pfsKeyword = null; return(false); } if (!TryToGetKeyword(pfsSection, out pfsKeyword, keyword, path)) { // ErrorMessage set in CheckFileExtension pfsFile.Close(); pfsFile = null; pfsKeyword = null; return(false); } return(true); }
/// <summary> /// Example of different modifications to a PFS file, storing the result /// in a new file name /// </summary> /// <param name="filePath">File and path to the pfsExample.pfs file in the TestData folder</param> /// <param name="newFilePath">File and path to the new modified file</param> public static void ModifyPFSFile(string filePath, string newFilePath) { // Loading/Reading the file PFSFile pfsFile = new PFSFile(filePath, false); // Outmost section PFSSection target = pfsFile.GetTarget("Run11", 1); // Sub-section PFSSection section1 = target.GetSection("Results", 1); // Rename section section1.Name = "HDResults"; // Add another section after the HDResults section. The new section // is returned. PFSSection sectionRR = section1.InsertNewSection("RRResults", 2); // Add a keyword to the new section. The new keyword is returned. PFSKeyword kwOutid = sectionRR.InsertNewKeyword("outid", 1); // Add a parameter kwOutid.InsertNewParameterString("rr out", 1); // Add yet another keyword to the new section. The new keyword is returned. PFSKeyword kwFile = sectionRR.InsertNewKeyword("file", 2); // Add a parameter kwFile.InsertNewParameterFileName(@".\outputRR.res11", 1); PFSSection section2 = section1.GetSection("Result", 1); // Modify first parameter (string) of keyword section2.GetKeyword("outid", 1).GetParameter(1).ModifyStringParameter("hd out"); // Delete the first keyword in the outer-most section target.DeleteKeyword("key1", 1); // Write back file pfsFile.Write(newFilePath); }
public Nullable <T> GetVariable <T>(string Path, string Keyword, int ParameterIndex) where T : struct { PFSFile pfsFile = null; PFSKeyword pfsKeyword = null; if (!CheckAll(Path, Keyword, out pfsFile, out pfsKeyword)) { return(null); } try { switch (typeof(T).ToString()) { case "System.Boolean": { object value = pfsKeyword.GetParameter(ParameterIndex).ToBoolean(); pfsFile.Close(); return((T?)value); } case "System.Double": { object value = pfsKeyword.GetParameter(ParameterIndex).ToDouble(); pfsFile.Close(); return((T?)value); } case "System.Int32": case "System.Int64": { object value = pfsKeyword.GetParameter(ParameterIndex).ToInt(); pfsFile.Close(); return((T?)value); } case "System.Single": { object value = pfsKeyword.GetParameter(ParameterIndex).ToSingle(); pfsFile.Close(); return((T?)value); } default: { return(null); } } } catch (Exception ex) { ErrorMessage = string.Format(CSSPDHIRes.ParameterIndex_DoesNotExistForSectionPath_AndKeyword_Error_, ParameterIndex, Path, Keyword, ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : "")); OnCSSPDHIChanged(new CSSPDHIEventArgs(new CSSPDHIMessage("Error", -1, false, ErrorMessage))); pfsFile.Close(); return(null); } }
private void UpdateAllFileInfoAndAppTask() { StringBuilder sbBody = new StringBuilder(); StringBuilder sbSubject = new StringBuilder(); TVFile tvFilem21_3fm = (from c in db.TVItems from ms in db.MikeScenarios from f in db.TVFiles from cc in db.TVItems where c.TVItemID == ms.MikeScenarioTVItemID && f.TVFileTVItemID == cc.TVItemID && cc.ParentID == c.TVItemID && (f.FileType == (int)FileTypeEnum.M21FM || f.FileType == (int)FileTypeEnum.M3FM) && ms.MikeScenarioTVItemID == MikeScenarioTVItemID select f).FirstOrDefault <TVFile>(); if (tvFilem21_3fm == null) { UpdateTaskError(AppTaskID, "Error: Could not find CSSPFile of type .m21fm or .m3fm and MikeScenarioID [" + MikeScenarioTVItemID + "]"); richTextBoxStatus.AppendText("Error: Could not find CSSPFile of type .m21fm or .m3fm and MikeScenarioID [" + MikeScenarioTVItemID + "]\r\n"); return; } FileInfo fiServer = new FileInfo(tvFilem21_3fm.ServerFilePath + tvFilem21_3fm.ServerFileName); if (!fiServer.Exists) { UpdateTaskError(AppTaskID, "File [" + fiServer.FullName + "] could not be found on the server."); richTextBoxStatus.AppendText("File [" + fiServer.FullName + "] could not be found on the server.\r\n"); return; } PFSFile pfsFile = new PFSFile(tvFilem21_3fm.ServerFilePath + tvFilem21_3fm.ServerFileName); if (pfsFile == null) { UpdateTaskError(AppTaskID, "File [" + fiServer.FullName + "] could not be parsed properly"); richTextBoxStatus.AppendText("File [" + fiServer.FullName + "] could not be parsed properly\r\n"); return; } LogFileName = fiServer.FullName.Replace(".m21fm", ".log").Replace(".m3fm", ".log"); FileInfo fiLog = new FileInfo(LogFileName); if (!fiLog.Exists) { UpdateTaskError(AppTaskID, "File [" + fiLog.FullName + "] could not be found on the server."); richTextBoxStatus.AppendText("File [" + fiLog.FullName + "] could not be found on the server.\r\n"); return; } List <int> TVItemIDToDeleteList = new List <int>(); // doing the log file TVFile tvFileLog = (from c in db.TVItems from ms in db.MikeScenarios from f in db.TVFiles from cc in db.TVItems where c.TVItemID == ms.MikeScenarioTVItemID && f.TVFileTVItemID == cc.TVItemID && cc.ParentID == c.TVItemID && f.FileType == (int)FileTypeEnum.LOG && f.ServerFileName == fiLog.Name && ms.MikeScenarioTVItemID == MikeScenarioTVItemID select f).FirstOrDefault <TVFile>(); if (tvFileLog == null) { string retStr = "Error: Could not find log result file belonging to the excecuted MikeScenarioID [" + MikeScenarioTVItemID + "]."; UpdateTaskError(AppTaskID, retStr); richTextBoxStatus.AppendText(retStr + "\r\n"); return; } fiLog = new FileInfo(LogFileName); tvFileLog.FileSize_kb = (int)(fiLog.Length / 1024); tvFileLog.FileCreatedDate_UTC = DateTime.UtcNow; tvFileLog.LastUpdateDate_UTC = DateTime.UtcNow; tvFileLog.LastUpdateContactTVItemID = ContactTVItemID; try { db.SaveChanges(); } catch (Exception ex) { UpdateTaskError(AppTaskID, "Error: Could not save modified log file belonging to the excecuted MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n Error Message: [" + ex.Message + "]"); richTextBoxStatus.AppendText("Error: Could not save modified log file belonging to the excecuted MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n Error Message: [" + ex.Message + "]\r\n"); return; } // doing the hydro file .dfsu FileInfo fiHydro = new FileInfo(HydroFileName); TVFile tvFileHydro = (from c in db.TVItems from ms in db.MikeScenarios from f in db.TVFiles from cc in db.TVItems where c.TVItemID == ms.MikeScenarioTVItemID && f.TVFileTVItemID == cc.TVItemID && cc.ParentID == c.TVItemID && f.FileType == (int)FileTypeEnum.DFSU && f.ServerFileName == fiHydro.Name && ms.MikeScenarioTVItemID == MikeScenarioTVItemID select f).FirstOrDefault <TVFile>(); if (tvFileHydro == null) { UpdateTaskError(AppTaskID, "Error: Could not find one hydrodynamic result file belonging to the excecuted MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n"); richTextBoxStatus.AppendText("Error: Could not find one hydrodynamic result file belonging to the excecuted MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n"); return; } fiHydro = new FileInfo(HydroFileName); tvFileHydro.FileSize_kb = (int)(fiHydro.Length / 1024); tvFileHydro.FileCreatedDate_UTC = DateTime.UtcNow; tvFileHydro.LastUpdateDate_UTC = DateTime.UtcNow; tvFileHydro.LastUpdateContactTVItemID = ContactTVItemID; try { db.SaveChanges(); } catch (Exception ex) { UpdateTaskError(AppTaskID, "Error: Could not save modified hydrodynamic result file belonging to the excecuted MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n Error Message: [" + ex.Message + "]"); richTextBoxStatus.AppendText("Error: Could not save hydrodynamic result file belonging to the excecuted MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n Error Message: [" + ex.Message + "]\r\n"); return; } // doing the hydro file .dfsu FileInfo fiTrans = new FileInfo(TransFileName); TVFile tvFileTrans = (from c in db.TVItems from ms in db.MikeScenarios from f in db.TVFiles from cc in db.TVItems where c.TVItemID == ms.MikeScenarioTVItemID && f.TVFileTVItemID == cc.TVItemID && cc.ParentID == c.TVItemID && f.FileType == (int)FileTypeEnum.DFSU && f.ServerFileName == fiTrans.Name && ms.MikeScenarioTVItemID == MikeScenarioTVItemID select f).FirstOrDefault <TVFile>(); if (tvFileTrans == null) { UpdateTaskError(AppTaskID, "Error: Could not find one transport result file belonging to the excecuted MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n"); richTextBoxStatus.AppendText("Error: Could not find one transport result file belonging to the excecuted MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n"); return; } fiTrans = new FileInfo(TransFileName); tvFileTrans.FileSize_kb = (int)(fiTrans.Length / 1024); tvFileTrans.FileCreatedDate_UTC = DateTime.UtcNow; tvFileTrans.LastUpdateDate_UTC = DateTime.UtcNow; tvFileTrans.LastUpdateContactTVItemID = ContactTVItemID; try { db.SaveChanges(); } catch (Exception ex) { UpdateTaskError(AppTaskID, "Error: Could not save modified transport result file belonging to the excecuted MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n Error Message: [" + ex.Message + "]"); richTextBoxStatus.AppendText("Error: Could not save transport result file belonging to the excecuted MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n Error Message: [" + ex.Message + "]\r\n"); return; } MikeScenario mikeScenario = (from c in db.MikeScenarios where c.MikeScenarioTVItemID == MikeScenarioTVItemID select c).FirstOrDefault(); if (mikeScenario == null) { UpdateTaskError(AppTaskID, "Error: Could not find MikeScenario with MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n"); richTextBoxStatus.AppendText("Error: Could not find MikeScenario with MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n"); return; } if (mikeScenario.GenerateDecouplingFiles == true) { string TVTextMikeScenario = (from c in db.TVItemLanguages where c.TVItemID == MikeScenarioTVItemID && c.Language == (int)LanguageEnum.en select c.TVText).FirstOrDefault(); if (string.IsNullOrWhiteSpace(TVTextMikeScenario)) { UpdateTaskError(AppTaskID, "Error: Could not find TVTextMikeScenario with MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n"); richTextBoxStatus.AppendText("Error: Could not find TVTextMikeScenario with MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n"); return; } // doing the {TVTextMikeScenario}_Decoupled.m21fm or .m3fm FileInfo fiDecoupled = new FileInfo(FileNameM21_3fm.Replace(".m21fm", "_Decoupled.m21fm").Replace(".m3fm", "_Decoupled.m3fm")); FileTypeEnum fileType = (FileNameM21_3fm.EndsWith(".m21fm") ? FileTypeEnum.M21FM : FileTypeEnum.M3FM); TVFile tvFileDecoupled = (from c in db.TVItems from ms in db.MikeScenarios from f in db.TVFiles from cc in db.TVItems where c.TVItemID == ms.MikeScenarioTVItemID && f.TVFileTVItemID == cc.TVItemID && cc.ParentID == c.TVItemID && f.FileType == (int)fileType && f.ServerFileName == fiDecoupled.Name && ms.MikeScenarioTVItemID == MikeScenarioTVItemID select f).FirstOrDefault <TVFile>(); if (tvFileDecoupled == null) { UpdateTaskError(AppTaskID, "Error: Could not find file [" + fiDecoupled.FullName + " under MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n"); richTextBoxStatus.AppendText("Error: Could not find file [" + fiDecoupled.FullName + " under MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n"); return; } fiDecoupled = new FileInfo(fiDecoupled.FullName); tvFileDecoupled.FileSize_kb = (int)(fiDecoupled.Length / 1024); tvFileDecoupled.FileCreatedDate_UTC = DateTime.UtcNow; tvFileDecoupled.LastUpdateDate_UTC = DateTime.UtcNow; tvFileDecoupled.LastUpdateContactTVItemID = ContactTVItemID; try { db.SaveChanges(); } catch (Exception ex) { UpdateTaskError(AppTaskID, "Error: Could not save TVFile info of [" + fiDecoupled.FullName + "] MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n Error Message: [" + ex.Message + "]"); richTextBoxStatus.AppendText("Error: Could not save TVFile info of [" + fiDecoupled.FullName + "] MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n Error Message: [" + ex.Message + "]\r\n"); return; } // doing the {TVTextMikeScenario}_DecouplingArea.dfsu FileInfo fiDecouplingArea = new FileInfo(FileNameM21_3fm.Replace(".m21fm", "_DecouplingArea.dfsu").Replace(".m3fm", "_DecouplingArea.dfsu")); TVFile tvFileDecouplingArea = (from c in db.TVItems from ms in db.MikeScenarios from f in db.TVFiles from cc in db.TVItems where c.TVItemID == ms.MikeScenarioTVItemID && f.TVFileTVItemID == cc.TVItemID && cc.ParentID == c.TVItemID && f.FileType == (int)FileTypeEnum.DFSU && f.ServerFileName == fiDecouplingArea.Name && ms.MikeScenarioTVItemID == MikeScenarioTVItemID select f).FirstOrDefault <TVFile>(); if (fiDecouplingArea == null) { UpdateTaskError(AppTaskID, "Error: Could not find file [" + fiDecouplingArea.FullName + " under MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n"); richTextBoxStatus.AppendText("Error: Could not find file [" + fiDecouplingArea.FullName + " under MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n"); return; } fiDecouplingArea = new FileInfo(fiDecouplingArea.FullName); tvFileDecouplingArea.FileSize_kb = (int)(fiDecouplingArea.Length / 1024); tvFileDecouplingArea.FileCreatedDate_UTC = DateTime.UtcNow; tvFileDecouplingArea.LastUpdateDate_UTC = DateTime.UtcNow; tvFileDecouplingArea.LastUpdateContactTVItemID = ContactTVItemID; try { db.SaveChanges(); } catch (Exception ex) { UpdateTaskError(AppTaskID, "Error: Could not save TVFile info of [" + fiDecouplingArea.FullName + "] MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n Error Message: [" + ex.Message + "]"); richTextBoxStatus.AppendText("Error: Could not save TVFile info of [" + fiDecouplingArea.FullName + "] MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n Error Message: [" + ex.Message + "]\r\n"); return; } // doing the {TVTextMikeScenario}_DecouplingFlux.dfsu FileInfo fiDecouplingFlux = new FileInfo(FileNameM21_3fm.Replace(".m21fm", "_DecouplingFlux.dfsu").Replace(".m3fm", "_DecouplingFlux.dfsu")); TVFile tvFileDecouplingFlux = (from c in db.TVItems from ms in db.MikeScenarios from f in db.TVFiles from cc in db.TVItems where c.TVItemID == ms.MikeScenarioTVItemID && f.TVFileTVItemID == cc.TVItemID && cc.ParentID == c.TVItemID && f.FileType == (int)FileTypeEnum.DFSU && f.ServerFileName == fiDecouplingFlux.Name && ms.MikeScenarioTVItemID == MikeScenarioTVItemID select f).FirstOrDefault <TVFile>(); if (fiDecouplingFlux == null) { UpdateTaskError(AppTaskID, "Error: Could not find file [" + fiDecouplingFlux.FullName + " under MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n"); richTextBoxStatus.AppendText("Error: Could not find file [" + fiDecouplingFlux.FullName + " under MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n"); return; } fiDecouplingFlux = new FileInfo(fiDecouplingFlux.FullName); tvFileDecouplingFlux.FileSize_kb = (int)(fiDecouplingFlux.Length / 1024); tvFileDecouplingFlux.FileCreatedDate_UTC = DateTime.UtcNow; tvFileDecouplingFlux.LastUpdateDate_UTC = DateTime.UtcNow; tvFileDecouplingFlux.LastUpdateContactTVItemID = ContactTVItemID; try { db.SaveChanges(); } catch (Exception ex) { UpdateTaskError(AppTaskID, "Error: Could not save TVFile info of [" + fiDecouplingFlux.FullName + "] MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n Error Message: [" + ex.Message + "]"); richTextBoxStatus.AppendText("Error: Could not save TVFile info of [" + fiDecouplingFlux.FullName + "] MikeScenarioID [" + MikeScenarioTVItemID + "]. \r\n Error Message: [" + ex.Message + "]\r\n"); return; } } }
private string Setup() { string retStr = ""; lblAppTaskIDValue.Text = "..."; lblMikeScenarioIDValue.Text = "..."; AppTask appTask = (from c in db.AppTasks from t in db.TVItems from m in db.MikeScenarios where c.TVItemID == t.TVItemID && t.TVItemID == m.MikeScenarioTVItemID && c.AppTaskCommand == (int)AppTaskCommandEnum.MikeScenarioWaitingToRun && c.AppTaskStatus == (int)AppTaskStatusEnum.Running && t.TVType == (int)TVTypeEnum.MikeScenario && m.ScenarioStatus == (int)ScenarioStatusEnum.AskToRun select c).FirstOrDefault(); if (appTask == null) { this.Close(); } AppTaskID = appTask.AppTaskID; MikeScenarioTVItemID = appTask.TVItemID; ContactTVItemID = appTask.LastUpdateContactTVItemID; lblAppTaskIDValue.Text = AppTaskID.ToString(); lblMikeScenarioIDValue.Text = MikeScenarioTVItemID.ToString(); if (!DongleIsPlugged()) { UpdateTaskError(AppTaskID, UpdaterRes.Mike21_3DongleNotFound); return(UpdaterRes.Mike21_3DongleNotFound); } this.Text = "Mike Scenario Updater " + MikeScenarioTVItemID; Contact contact = (from c in db.Contacts where c.ContactTVItemID == ContactTVItemID select c).FirstOrDefault(); if (contact == null) { retStr = "Could not find Contact with ContactTVItemID [" + ContactTVItemID + "]"; UpdateTaskError(AppTaskID, retStr); richTextBoxStatus.AppendText(retStr + "\r\n"); return(retStr); } lblContactValue.Text = contact.FirstName + (string.IsNullOrWhiteSpace(contact.Initial) ? "" : " " + contact.Initial) + " " + contact.LastName; MikeScenario mikeScenario = (from c in db.MikeScenarios where c.MikeScenarioTVItemID == MikeScenarioTVItemID select c).FirstOrDefault(); if (mikeScenario == null) { retStr = "Could not find MikeScenario with MikeScenarioTVItemID [" + MikeScenarioTVItemID + "]"; UpdateTaskError(AppTaskID, retStr); richTextBoxStatus.AppendText(retStr + "\r\n"); return(retStr); } EstimatedHydroFileSize = (long)mikeScenario.EstimatedHydroFileSize; EstimatedTransFileSize = (long)mikeScenario.EstimatedTransFileSize; lblEstimatedHydroFileSizeValue.Text = EstimatedHydroFileSize.ToString(); lblEstimatedTransFileSizeValue.Text = EstimatedTransFileSize.ToString(); // should get m21fm or m3fm file name List <TVFile> tvFilem21_3fmList = (from c in db.TVItems from ms in db.MikeScenarios from f in db.TVFiles from cc in db.TVItems where c.TVItemID == ms.MikeScenarioTVItemID && f.TVFileTVItemID == cc.TVItemID && cc.ParentID == c.TVItemID && (f.FileType == (int)FileTypeEnum.M21FM || f.FileType == (int)FileTypeEnum.M3FM) && ms.MikeScenarioTVItemID == MikeScenarioTVItemID select f).ToList <TVFile>(); if (tvFilem21_3fmList.Count == 0) { retStr = "Error: Could not find CSSPFile of type .m21fm or .m3fm and MikeScenarioID [" + MikeScenarioTVItemID + "]"; UpdateTaskError(AppTaskID, retStr); richTextBoxStatus.AppendText(retStr + "\r\n"); return(retStr); } TVFile tvFilem21_3fm = null; foreach (TVFile tvFile in tvFilem21_3fmList) { if (mikeScenario.UseDecouplingFiles == true) { if (tvFile.ServerFileName.Contains("_Decoupled")) { tvFilem21_3fm = tvFile; } } else { if (!tvFile.ServerFileName.Contains("_Decoupled")) { tvFilem21_3fm = tvFile; } } } if (tvFilem21_3fm == null) { retStr = "Error: Could not find CSSPFile of type .m21fm or .m3fm and MikeScenarioID [" + MikeScenarioTVItemID + "]"; UpdateTaskError(AppTaskID, retStr); richTextBoxStatus.AppendText(retStr + "\r\n"); return(retStr); } FileNameM21_3fm = tvFilem21_3fm.ServerFilePath + tvFilem21_3fm.ServerFileName; FileInfo fim21_3fm = new FileInfo(FileNameM21_3fm); if (!fim21_3fm.Exists) { retStr = "Error: Could not find file [" + fim21_3fm.FullName + " with MikeScenarioID [" + MikeScenarioTVItemID + "]"; UpdateTaskError(AppTaskID, retStr); richTextBoxStatus.AppendText(retStr + "\r\n"); return(retStr); } lblCurrentFileValue.Text = @"...\" + MikeScenarioTVItemID + @"\" + fim21_3fm.Name; LogFileName = fim21_3fm.FullName.Replace(".m21fm", ".log").Replace(".m21fm", ".log"); PFSFile pfsFile = new PFSFile(FileNameM21_3fm); if (pfsFile == null) { retStr = "File [" + FileNameM21_3fm + "] could not be parsed properly"; UpdateTaskError(AppTaskID, retStr); richTextBoxStatus.AppendText(retStr + "\r\n"); return(retStr); } HydroFileName = GetParameterResultFileName(pfsFile, "FemEngineHD/HYDRODYNAMIC_MODULE/OUTPUTS/OUTPUT_1", "file_name"); if (string.IsNullOrWhiteSpace(HydroFileName)) { retStr = "HydroFileName could not be found"; UpdateTaskError(AppTaskID, retStr); richTextBoxStatus.AppendText(retStr + "\r\n"); return(retStr); } FileInfo fileHydro = new FileInfo(tvFilem21_3fm.ServerFilePath + HydroFileName); if (!fileHydro.Exists) { retStr = "Error: Could not find file [" + fileHydro.FullName + "]"; UpdateTaskError(AppTaskID, retStr); richTextBoxStatus.AppendText(retStr + "\r\n"); return(retStr); } HydroFileName = fileHydro.FullName; lblHydroFileNameValue.Text = @"...\" + MikeScenarioTVItemID + @"\" + fileHydro.Name; TransFileName = GetParameterResultFileName(pfsFile, "FemEngineHD/TRANSPORT_MODULE/OUTPUTS/OUTPUT_1", "file_name"); if (string.IsNullOrWhiteSpace(TransFileName)) { retStr = "TransFileName could not be found"; UpdateTaskError(AppTaskID, retStr); richTextBoxStatus.AppendText(retStr + "\r\n"); return(retStr); } FileInfo fileTrans = new FileInfo(tvFilem21_3fm.ServerFilePath + TransFileName); if (!fileTrans.Exists) { retStr = "Error: Could not find file [" + fileTrans.FullName + "]"; UpdateTaskError(AppTaskID, retStr); richTextBoxStatus.AppendText(retStr + "\r\n"); return(retStr); } TransFileName = fileTrans.FullName; lblTransFileNameValue.Text = @"...\" + MikeScenarioTVItemID + @"\" + fileTrans.Name; return(""); }