Placeholder Image

ADN Informatique

Consultant Ingénierie Informatique

Saguenay (Jonquière), Québec, Canada. Tél: (581) 683-9887 Téléc: (581) 221-0874

Placeholder Picture

Retour

IMAGEM PACS Image retrieve

2005-04-01 20:58:59 (ID: 66, Parent: 0, Type: post)
/****************************************************************************************    Class: download    Author: Jean-Luc Cyr    Copyrights: IMAGEM medicals systems    Date: 2004-11-24    Description: Connect to imagem pacs database, fetch a study list to download                 fetch anterior list for those studies and images list for study and                 anterior.  After that, connect by ftp to the server and fetch the                 images.                  FTP Connexion was released between each study.                 Database Connecion was persistant. ****************************************************************************************/  //FTP import cz.dhl.io.*; import cz.dhl.ftp.*; //ORACLE import java.sql.*; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.PreparedStatement; import oracle.jdbc.pool.OracleDataSource; //JAVA import java.io.IOException; import java.io.File; import java.util.Vector;  public class download {     //Internal exception     public class downloadException extends Exception     { 	public downloadException(){} 	public downloadException(String s){super(s);}     }      //Global variables     Connection Database;                ///< Database connection     Vector StudyList = new Vector();    ///< Study list to download     Vector AnteriorList = new Vector(); ///< Current processing study anterior list     Vector ImageList = new Vector();    ///< Current study and antorior image list      //Configuration section     String base_path = "c:/imagem/images"; ///< Local base path to store images     //String DB_Name = "jdbc:oracle:thin:@10.16.163.3:1521:orcl"; ///< Database connexion string     String DB_Name = "jdbc:oracle:thin:@192.168.1.6:1521:rpdev";     String DB_User = "ris_pacs"; ///< Database username     String DB_Pass = "password"; ///< Database password     String FTP_Name = "ftp://10.16.163.3"; ///< FTP connexion string     String FTP_User = "jlcyr"; ///< FTP Username     String FTP_Pass = "Limos1"; ///< FTP Password      ////////////////////////////////////////////////////////////////     ///Main program     public static void main( String[] args ) { 	System.out.println("Demarrage du service de transfert"); 	while (true) { 	    download test = new download(); 	    test.fetchImages(); 	}     }      ////////////////////////////////////////////////////////////////     ///Connexion à la base de donnee     public boolean dbConnect(){ 	try{ 	    DriverManager.registerDriver(new oracle.jdbc.OracleDriver()); 	} catch (SQLException ex){ 	    System.out.println("Erreur durant l'initialisation des connections "+'\n'+ex.toString()); 	} 	try{ 	    Database = DriverManager.getConnection(DB_Name,DB_User,DB_Pass); 	    Database.setAutoCommit(false); 	} catch (SQLException ex){ 	    System.out.println("Erreur durant la connection a la db ("+DB_Name+")\n"+ex.toString()); 	    return false; 	} 	return true;     }      ////////////////////////////////////////////////////////////////     ///Deconnexion de la base de donnee     public void dbDisconnect(){ 	try{ 	    Database.close(); 	} catch (Exception ex){ 	    System.out.println("Erreur a la fermeture de la connection a la db"); 	}     }      ///////////////////////////////////////////////////////////////     ///Sortir la liste des etudes a lire     public void dbSelectStudy(){ 	StudyList.clear(); 	try{ 	    ResultSet study; 	    Statement stmt1 = Database.createStatement(); 	    study = stmt1.executeQuery( 	       "SELECT STU_INSTANCE_UID FROM STUDY WHERE "+ 	       "STU_OTHER_NUMBER IN ('8255','8256','8262','8263','8264','8265','8266','8267','8268','8269') AND "+ 	       "STU_STATUS='VERIFIED' AND STU_DATE>'20041101' "+ 	       "ORDER BY STU_DATE ASC"); 	    int count = 1; 	    while (study.next()) { 		String stu = study.getString("STU_INSTANCE_UID"); 		StudyList.add(stu); 		count++; 	    }  	} catch(SQLException ex){ 	    System.out.println("Erreur a la demande de la liste d'etudes: "+ex.toString()); 	}     }      ///////////////////////////////////////////////////////////////     ///Process study list     ///Fetch anterior study     ///Fetch image list for study and anterior     ///Fetch images     public void fetchImages() { 	boolean connected = false; 	System.out.println("Connection au serveur de db"); 	if (dbConnect()) { 	    connected = true; 	    System.out.println("Connection etablie"); 	    System.out.println("Demande de la liste d'etudes"); 	    dbSelectStudy(); 	} else { 	    connected = false; 	    System.out.println("Pas de connection à la base de donnée, on arrête."); 	    return; 	}  	for (int x=0; x<StudyList.size(); x++) { 	    if (!connected) { 		System.out.println("Connection au serveur de db"); 		if (dbConnect()) { 		    connected = true; 		    System.out.println("Connection etablie"); 		} else { 		    connected = false; 		    System.out.println("Pas de connection à la base de donnée, on arrête."); 		    return; 		} 	    } 	    System.out.println("-----------------------------------------"); 	    System.out.println(StudyList.get(x)); 	    //Fetch Anterior Study List 	    System.out.println("Fetching anterior"); 	    dbSelectAnt((String)StudyList.get(x)); 	    //Fetch Image List 	    dbSelectImages((String)StudyList.get(x),false); 	    //Fetch Anterior Image List 	    int count; 	    for (count=0; count<AnteriorList.size(); count++) { 		dbSelectImages((String)AnteriorList.get(count),true); 	    }  	    System.out.println("Traitement de la liste d'etudes"); 	    //Process image list 	    if (ImageList.size()>0) { 		boolean conn = false; 		//Initialise Ftp Connexion but open it only if needed 		FtpConnect ftpc = FtpConnect.newConnect(FTP_Name); 		ftpc.setUserName(FTP_User); 		ftpc.setPassWord(FTP_Pass); 		Ftp ftp = new Ftp(); 		//Retrieve Images (if not present locally) 		File TestFile; 		String[] COI = new String[4]; 		for (count=0; count<ImageList.size(); count++) { 		    COI = ((String[])ImageList.get(count)); 		    //System.out.println(count+" "+COI[3]); 		    //System.out.println("FILE:"+base_path+"/"+COI[1]+"/"+COI[2]+"/"+COI[3]+".dcm"); 		    TestFile = new File(base_path+"/"+COI[1]+"/"+COI[2]+"/"+COI[3]+".dcm"); 		    if (!TestFile.exists()) { 			//Si on a besoin de downloader on se déconnecte de la db 			//pour eviter de faire un timeout du reseau 			if (connected) { 			    System.out.println("Deconnection de la base de donnee"); 			    dbDisconnect(); 			    connected = false; 			    System.out.println("Connection terminee"); 			} 			if (!conn) { 			    try { 				//Open ftp connexion we need it 				ftp.connect(ftpc); 				conn = true; 			    } catch (IOException e) { 				System.out.println(e); 			    } 			} 			if (ftp.isConnected()) { 			    //Retrieve image 			    retrieveImages(((String[])ImageList.get(count)),ftp); 			} 			else { 			    System.out.println("On ne peut pas transferer l'image, on a pas de connexion"); 			    conn = false; 			} 		    } 		    else { 			System.out.println("o "+TestFile.getName()); 		    } 		} 		//Close Ftp Connexion 		ftp.disconnect(); 		conn = false; 	    } 	} 	if (connected) { 	    System.out.println("Deconnection de la base de donnee"); 	    dbDisconnect(); 	    connected = false; 	    System.out.println("Connection terminee"); 	}     }      //////////////////////////////////////////////////////     ///Find anterior studies for a specific study     public void dbSelectAnt(String stu_uid) { 	AnteriorList.clear(); 	try{ 	    ResultSet study; 	    Statement stmt1 = Database.createStatement(); 	    study = stmt1.executeQuery( 				       "SELECT STU_PAT_ID,STU_OTHER_NUMBER,STU_INSTANCE_UID,STU_DATE,STU_TIME FROM STUDY WHERE "+ 				       "STU_INSTANCE_UID = '"+stu_uid+"'"); 	    study.next();  	    String pat_id = study.getString("STU_PAT_ID"); 	    String stu_on = study.getString("STU_OTHER_NUMBER"); 	    String stu_date = study.getString("STU_DATE"); 	    String stu_time = study.getString("STU_TIME");  	    System.out.println("=================="); 	    System.out.println("PATIENT: "+pat_id); 	    System.out.println("=================="); 	    //Demande les anterieurs 	    //Pour le meme patient 	    //Dans la liste de code HISTORICAL_IMAGE 	    //Le premier (plus recent) pour chaque code 	    // 	    study = stmt1.executeQuery( 				       "SELECT STU_INSTANCE_UID,STU_OTHER_NUMBER FROM STUDY WHERE "+ 				       "STU_PAT_ID = '"+pat_id+"' AND "+ 				       "STU_INSTANCE_UID != '"+stu_uid+"' AND "+ 				       "(STU_DATE<'"+stu_date+"' OR (STU_DATE='"+stu_date+"' AND STU_TIME<'"+stu_time+"')) AND "+ 				       "STU_OTHER_NUMBER IN (SELECT HI_EXAMCODE FROM HISTORICAL_IMAGES WHERE HI_REF_EXAMCODE='"+stu_on+"') "+ 				       "ORDER BY STU_OTHER_NUMBER ASC, STU_DATE DESC, STU_TIME DESC");  	    int count = 1; 	    String code = ""; 	    String stu_code = ""; 	    while (study.next()) { 		stu_code = study.getString("STU_OTHER_NUMBER"); 		if (code.compareTo(stu_code)!=0) { 		    String stu = study.getString("STU_INSTANCE_UID"); 		    System.out.println("Trouve "+stu_code+" - "+stu); 		    AnteriorList.add(stu); 		    code = stu_code; 		} 		count++; 	    }  	} catch(SQLException ex){ 	    System.out.println("Erreur a la demande de la liste d'anterieur: "+ex.toString()); 	}     }      ///////////////////////////////////////////////////////     ///Find images for a study and put them in ImageList     ///is keep is true, doesn't clear the list, simply add to it     public void dbSelectImages(String stu_uid, boolean keep) { 	if (!keep) 	    ImageList.clear(); 	try{ 	    ResultSet study; 	    Statement stmt1 = Database.createStatement();     	    study = stmt1.executeQuery( 				       "SELECT * FROM COMPOSITE_OBJECT_INSTANCE,MEDIA_FILE_SET WHERE "+ 				       "COI_STUDY_INSTANCE_UID = '"+stu_uid+"' AND "+ 				       "COI_STO_MEDIA_FSET_UID = MFS_UID");  	    int count = 0; 	    String COI[]; 	    while (study.next()) { 		COI = new String[4]; 		COI[0] = study.getString("MFS_MOUNT_POINT"); 		COI[1] = study.getString("COI_STUDY_INSTANCE_UID"); 		COI[2] = study.getString("COI_SERIES_INSTANCE_UID"); 		COI[3] = study.getString("COI_SOP_INSTANCE_UID"); 		String remote_filename = COI[0]+"/"+COI[1]+"/"+COI[2]+"/"+COI[3]+".dcm"; 		System.out.println("Must retrieve: "+COI[3]); 		ImageList.add(COI); 		count++; 	    } 	    if (count==0) 		System.out.println("Aucune images");  	} catch(SQLException ex){ 	    System.out.println("Erreur a la demande de la liste d'images: "+ex.toString()); 	}     }      ///////////////////////////////////////////////////////     ///Retrieve images     public void retrieveImages(String COI[], Ftp ftplink) { 	// source FtpFile remote file 	String remote_filename = COI[0]+"/"+COI[1]+"/"+COI[2]+"/"+COI[3]+".dcm"; 	CoFile file = new FtpFile(remote_filename,ftplink); 	// Check if local directory exists and create them if needed 	File testDir = new File(base_path+"/"+COI[1]); 	if (!testDir.exists()) 	    testDir.mkdir(); 	testDir = new File(base_path+"/"+COI[1]+"/"+COI[2]); 	if (!testDir.exists()) 	    testDir.mkdir(); 	// destination LocalFile home-dir/Welcome 	CoFile to = new LocalFile(base_path+"/"+COI[1]+"/"+COI[2],COI[3]+".dcm"); 	// download /Welcome file to home-dir/Welcome 	CoLoad.copy(to,file);     } }  

Document Informatif

ADN Informatique

2015

Rev. 1

Jean-Luc Cyr