///<summary>Returns true if the communications were successful, and false if they failed. Both sends and retrieves.</summary> public static bool Launch(Clearinghouse clearinghouseClin, int batchNum, IODProgressExtended progress = null) //called from Eclaims and FormClaimReports.cs. Clinic-level clearinghouse passed in. { progress = progress ?? new ODProgressExtendedNull(); _clearinghouseClin = clearinghouseClin; //Before this function is called, the X12 file for the current batch has already been generated in //the clearinghouse export folder. The export folder will also contain batch files which have failed //to upload from previous attempts and we must attempt to upload these older batch files again if //there are any. //Step 1: Retrieve reports regarding the existing pending claim statuses. //Step 2: Send new claims in a new batch. bool success = true; //Connect to the MDE SFTP server. Session session = null; Channel channel = null; ChannelSftp ch = null; JSch jsch = new JSch(); progress.UpdateProgress(Lans.g(progress.LanThis, "Contacting web server"), "reports", "17%", 17); if (progress.IsPauseOrCancel()) { progress.UpdateProgress(Lans.g(progress.LanThis, "Canceled by user.")); return(false); } try{ session = jsch.getSession(_clearinghouseClin.LoginID, remoteHost, 22); session.setPassword(_clearinghouseClin.Password); Hashtable config = new Hashtable(); config.Add("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); channel = session.openChannel("sftp"); channel.connect(); ch = (ChannelSftp)channel; } catch (Exception ex) { progress.UpdateProgress(Lans.g(progress.LanThis, "Connection Failed")); ErrorMessage = Lans.g("MercuryDE", "Connection Failed") + ": " + ex.Message; return(false); } progress.UpdateProgress(Lans.g(progress.LanThis, "Web server contact successful.")); try{ //At this point we are connected to the MDE SFTP server. if (batchNum == 0) { if (!Directory.Exists(clearinghouseClin.ResponsePath)) { throw new Exception(Lans.g(progress.LanThis, "Clearinghouse response path is invalid.")); } progress.UpdateProgress(Lans.g(progress.LanThis, "Getting files"), "reports", "33%", 33); if (progress.IsPauseOrCancel()) { progress.UpdateProgress(Lans.g(progress.LanThis, "Canceled by user.")); return(false); } //Only retrieving reports so do not send new claims. string retrievePath = "/" + rootFolderName + "/Out/997/"; Tamir.SharpSsh.java.util.Vector fileList = ch.ls(retrievePath); for (int i = 0; i < fileList.Count; i++) { int percent = (i / fileList.Count) * 100; //We re-use the bar again for importing later, hence the tag. progress.UpdateProgress(Lans.g(progress.LanThis, "Getting file:") + i + " / " + fileList.Count, "import", percent + "%", percent); if (progress.IsPauseOrCancel()) { progress.UpdateProgress(Lans.g(progress.LanThis, "Canceled by user.")); return(false); } string listItem = fileList[i].ToString().Trim(); if (listItem[0] == 'd') { continue; //Skip directories and focus on files. } Match fileNameMatch = Regex.Match(listItem, ".*\\s+(.*)$"); string getFileName = fileNameMatch.Result("$1"); string getFilePath = retrievePath + getFileName; string exportFilePath = CodeBase.ODFileUtils.CombinePaths(clearinghouseClin.ResponsePath, getFileName); Tamir.SharpSsh.java.io.InputStream fileStream = null; FileStream exportFileStream = null; try{ fileStream = ch.get(getFilePath); exportFileStream = File.Open(exportFilePath, FileMode.Create, FileAccess.Write); //Creates or overwrites. byte[] dataBytes = new byte[4096]; int numBytes = fileStream.Read(dataBytes, 0, dataBytes.Length); while (numBytes > 0) { exportFileStream.Write(dataBytes, 0, numBytes); numBytes = fileStream.Read(dataBytes, 0, dataBytes.Length); } float overallpercent = 33 + (i / fileList.Count) * 17; //33 is starting point. 17 is the amount of bar space we have before our next major spot (50%) progress.UpdateProgress(Lans.g(progress.LanThis, "Getting files"), "reports", overallpercent + "%", (int)overallpercent); if (progress.IsPauseOrCancel()) { progress.UpdateProgress(Lans.g(progress.LanThis, "Canceled by user.")); return(false); } } catch (Exception ex) { ErrorMessage = ex.Message; success = false; } finally { if (exportFileStream != null) { exportFileStream.Dispose(); } if (fileStream != null) { fileStream.Dispose(); } } string archiveFilePath = retrievePath + "Archive/" + getFileName; try{ ch.rm(archiveFilePath); } catch { //Remove any destination files by the same exact name. The file will most likely not be present. } ch.rename(getFilePath, archiveFilePath); } } else { if (!Directory.Exists(clearinghouseClin.ExportPath)) { throw new Exception(Lans.g(progress.LanThis, "Clearinghouse export path is invalid.")); } //First upload the batch to the temporary directory. progress.UpdateProgress(Lans.g(progress.LanThis, "Uploading files to temp directory"), "reports", "33%", 33); if (progress.IsPauseOrCancel()) { progress.UpdateProgress(Lans.g(progress.LanThis, "Canceled by user.")); return(false); } string[] files = Directory.GetFiles(clearinghouseClin.ExportPath); for (int i = 0; i < files.Length; i++) { int percent = (i / files.Length) * 100; //We re-use the bar again for importing later, hence the tag. progress.UpdateProgress(Lans.g(progress.LanThis, "Uploading file:") + i + " / " + files.Length, "import", percent + "%", percent); if (progress.IsPauseOrCancel()) { progress.UpdateProgress(Lans.g(progress.LanThis, "Canceled by user.")); return(false); } string accountNumber = _clearinghouseClin.ISA08; string dateTimeStr = DateTime.Now.ToString("yyyyMMddhhmmss"); string remoteFileName = accountNumber + dateTimeStr + i.ToString().PadLeft(3, '0') + ".837D.txt"; string remoteTempFilePath = "/" + rootFolderName + "/In/837D/Temp/" + remoteFileName; ch.put(files[i], remoteTempFilePath); //Read, Write and Execute permissions for everyone. This appears to cause no effect. ch.chmod((((7 << 3) | 7) << 3) | 7, remoteTempFilePath); string remoteFilePath = "/" + rootFolderName + "/In/837D/" + remoteFileName; ch.rename(remoteTempFilePath, remoteFilePath); File.Delete(files[i]); //Remove the processed file. float overallpercent = 33 + (i / files.Length) * 17; //33 is starting point. 17 is the amount of bar space we have before our next major spot (50%) progress.UpdateProgress(Lans.g(progress.LanThis, "Uploading files"), "reports", overallpercent + "%", (int)overallpercent); if (progress.IsPauseOrCancel()) { progress.UpdateProgress(Lans.g(progress.LanThis, "Canceled by user.")); return(false); } } } } catch (Exception ex) { if (ErrorMessage != "") { ErrorMessage += "\r\n"; } ErrorMessage = ex.Message; success = false; } finally { //Disconnect from the MDE SFTP server. progress.UpdateProgress("", "import", ""); //Clear import bar for now. channel.disconnect(); ch.disconnect(); session.disconnect(); } return(success); }
public DataTable GetFileList(string remotePath) { try { Connect(); if (remotePath == "") { remotePath = "."; } if (remotePath.StartsWith("/")) { remotePath = remotePath.Substring(1, remotePath.Length - 1); } Tamir.SharpSsh.java.util.Vector vvv = m_sftp.ls(remotePath); if (vvv.Count == 0) { return(null); } DataTable dt = GetFileListDateTable(); foreach (Tamir.SharpSsh.jsch.ChannelSftp.LsEntry qqq in vvv) { string filename = qqq.getFilename();//System.Text.Encoding.UTF8.GetString(qqq.getFilename().getBytes()); if (filename == ".." || filename == ".") { continue; } DataRow dr = dt.NewRow(); dr["File Name"] = filename; dr["FileFullPath"] = remotePath + "/" + filename; dr["Size"] = qqq.getAttrs().getSize(); dr["Modify Date"] = qqq.getAttrs().getMtimeString(); if (qqq.getLongname().ToString().StartsWith("d")) { dr["Type"] = "File Folder"; dr["IsFolder"] = "Y"; } else { if (filename.LastIndexOf(".") > 0) { dr["Type"] = filename.Substring(filename.LastIndexOf("."), filename.Length - filename.LastIndexOf(".")); } else { dr["Type"] = ""; } //dr["Type"] = Tool.GetFileType(al[8].ToString().Substring(al[8].ToString().Length - 4, 4)); dr["IsFolder"] = "N"; } dt.Rows.Add(dr); } return(dt); } catch { return(null); } }
///<summary>Returns true if the communications were successful, and false if they failed. Both sends and retrieves.</summary> public static bool Launch(Clearinghouse clearhouse, int batchNum) { clearinghouse = clearhouse; //Before this function is called, the X12 file for the current batch has already been generated in //the clearinghouse export folder. The export folder will also contain batch files which have failed //to upload from previous attempts and we must attempt to upload these older batch files again if //there are any. //Step 1: Retrieve reports regarding the existing pending claim statuses. //Step 2: Send new claims in a new batch. bool success = true; //Connect to the Denti-Cal SFTP server. Session session = null; Channel channel = null; ChannelSftp ch = null; JSch jsch = new JSch(); try { session = jsch.getSession(clearinghouse.LoginID, remoteHost); session.setPassword(clearinghouse.Password); Hashtable config = new Hashtable(); config.Add("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); channel = session.openChannel("sftp"); channel.connect(); ch = (ChannelSftp)channel; } catch (Exception ex) { MessageBox.Show(Lan.g("DentiCal", "Connection Failed") + ": " + ex.Message); return(false); } try { string homeDir = "/Home/" + clearhouse.LoginID + "/"; //At this point we are connected to the Denti-Cal SFTP server. if (batchNum == 0) //Retrieve reports. { if (!Directory.Exists(clearhouse.ResponsePath)) { throw new Exception("Clearinghouse response path is invalid."); } //Only retrieving reports so do not send new claims. string retrievePath = homeDir + "out/"; Tamir.SharpSsh.java.util.Vector fileList = ch.ls(retrievePath); for (int i = 0; i < fileList.Count; i++) { string listItem = fileList[i].ToString().Trim(); if (listItem[0] == 'd') { continue; //Skip directories and focus on files. } Match fileNameMatch = Regex.Match(listItem, ".*\\s+(.*)$"); string getFileName = fileNameMatch.Result("$1"); string getFilePath = retrievePath + getFileName; string exportFilePath = CodeBase.ODFileUtils.CombinePaths(clearhouse.ResponsePath, getFileName); Tamir.SharpSsh.java.io.InputStream fileStream = null; FileStream exportFileStream = null; try { fileStream = ch.get(getFilePath); exportFileStream = File.Open(exportFilePath, FileMode.Create, FileAccess.Write); //Creates or overwrites. byte[] dataBytes = new byte[4096]; int numBytes = fileStream.Read(dataBytes, 0, dataBytes.Length); while (numBytes > 0) { exportFileStream.Write(dataBytes, 0, numBytes); numBytes = fileStream.Read(dataBytes, 0, dataBytes.Length); } } catch { success = false; } finally { if (exportFileStream != null) { exportFileStream.Dispose(); } if (fileStream != null) { fileStream.Dispose(); } } if (success) { //Removed the processed report from the Denti-Cal SFTP so it does not get processed again in the future. try { ch.rm(getFilePath); } catch { } } } } else //Send batch of claims. { if (!Directory.Exists(clearhouse.ExportPath)) { throw new Exception("Clearinghouse export path is invalid."); } string[] files = Directory.GetFiles(clearhouse.ExportPath); for (int i = 0; i < files.Length; i++) { //First upload the batch file to a temporary file name. Denti-Cal does not process file names unless they start with the Login ID. //Uploading to a temporary file and then renaming the file allows us to avoid partial file uploads if there is connection loss. string tempRemoteFilePath = homeDir + "in/temp_" + Path.GetFileName(files[i]); ch.put(files[i], tempRemoteFilePath); //Denti-Cal requires the file name to start with the Login ID followed by a period and end with a .txt extension. //The middle part of the file name can be anything. string remoteFilePath = homeDir + "in/" + clearhouse.LoginID + "." + Path.GetFileName(files[i]); ch.rename(tempRemoteFilePath, remoteFilePath); File.Delete(files[i]); //Remove the processed file. } } } catch { success = false; } finally { //Disconnect from the Denti-Cal SFTP server. channel.disconnect(); ch.disconnect(); session.disconnect(); } return(success); }
///<summary>Returns true if the communications were successful, and false if they failed. Both sends and retrieves.</summary> public static bool Launch(Clearinghouse clearhouse, int batchNum) { clearinghouse = clearhouse; //Before this function is called, the X12 file for the current batch has already been generated in //the clearinghouse export folder. The export folder will also contain batch files which have failed //to upload from previous attempts and we must attempt to upload these older batch files again if //there are any. //Step 1: Retrieve reports regarding the existing pending claim statuses. //Step 2: Send new claims in a new batch. bool success = true; //Connect to the MDE SFTP server. Session session = null; Channel channel = null; ChannelSftp ch = null; JSch jsch = new JSch(); try{ session = jsch.getSession(clearinghouse.LoginID, remoteHost, 22); session.setPassword(clearinghouse.Password); Hashtable config = new Hashtable(); config.Add("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); channel = session.openChannel("sftp"); channel.connect(); ch = (ChannelSftp)channel; }catch (Exception ex) { MessageBox.Show(Lan.g("MercuryDE", "Connection Failed") + ": " + ex.Message); return(false); } try{ //At this point we are connected to the MDE SFTP server. if (batchNum == 0) { if (!Directory.Exists(clearhouse.ResponsePath)) { throw new Exception("Clearinghouse response path is invalid."); } //Only retrieving reports so do not send new claims. string retrievePath = "/" + rootFolderName + "/Out/997/"; Tamir.SharpSsh.java.util.Vector fileList = ch.ls(retrievePath); for (int i = 0; i < fileList.Count; i++) { string listItem = fileList[i].ToString().Trim(); if (listItem[0] == 'd') { continue; //Skip directories and focus on files. } Match fileNameMatch = Regex.Match(listItem, ".*\\s+(.*)$"); string getFileName = fileNameMatch.Result("$1"); string getFilePath = retrievePath + getFileName; string exportFilePath = CodeBase.ODFileUtils.CombinePaths(clearhouse.ResponsePath, getFileName); Tamir.SharpSsh.java.io.InputStream fileStream = null; FileStream exportFileStream = null; try{ fileStream = ch.get(getFilePath); exportFileStream = File.Open(exportFilePath, FileMode.Create, FileAccess.Write); //Creates or overwrites. byte[] dataBytes = new byte[4096]; int numBytes = fileStream.Read(dataBytes, 0, dataBytes.Length); while (numBytes > 0) { exportFileStream.Write(dataBytes, 0, numBytes); numBytes = fileStream.Read(dataBytes, 0, dataBytes.Length); } }catch { success = false; }finally{ if (exportFileStream != null) { exportFileStream.Dispose(); } if (fileStream != null) { fileStream.Dispose(); } } string archiveFilePath = retrievePath + "Archive/" + getFileName; try{ ch.rm(archiveFilePath); }catch { //Remove any destination files by the same exact name. The file will most likely not be present. } ch.rename(getFilePath, archiveFilePath); } } else { if (!Directory.Exists(clearhouse.ExportPath)) { throw new Exception("Clearinghouse export path is invalid."); } //First upload the batch to the temporary directory. string[] files = Directory.GetFiles(clearhouse.ExportPath); for (int i = 0; i < files.Length; i++) { string accountNumber = clearinghouse.ISA08; string dateTimeStr = DateTime.Now.ToString("yyyyMMddhhmmss"); string remoteFileName = accountNumber + dateTimeStr + i.ToString().PadLeft(3, '0') + ".837D.txt"; string remoteTempFilePath = "/" + rootFolderName + "/In/837D/Temp/" + remoteFileName; ch.put(files[i], remoteTempFilePath); //Read, Write and Execute permissions for everyone. This appears to cause no effect. ch.chmod((((7 << 3) | 7) << 3) | 7, remoteTempFilePath); string remoteFilePath = "/" + rootFolderName + "/In/837D/" + remoteFileName; ch.rename(remoteTempFilePath, remoteFilePath); File.Delete(files[i]); //Remove the processed file. } } }catch { success = false; }finally{ //Disconnect from the MDE SFTP server. channel.disconnect(); ch.disconnect(); session.disconnect(); } return(success); }