package net.ihe.swfid.tools; import java.io.IOException; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import org.apache.commons.io.HexDump; import org.apache.xerces.impl.dv.util.HexBin; import org.dcm4che2.data.DicomObject; import org.dcm4che2.data.Tag; import org.dcm4che2.net.ConfigurationException; import org.dcm4che2.tool.dcmqr.DcmQR; import org.jfree.util.Log; import com.sun.xml.fastinfoset.algorithm.HexadecimalEncodingAlgorithm; /** * * @author abderrazek boufahja * */ public class DcmQRSimulator { public DcmQRSimulator(){ } /** * @param host : HostName to connect to the service * @param port : System port * @param aet : Called AET * @param queryValue : Search criteria PatientName, the begin of the name with character * at the end * @return return the list of result * @throws IOException * @throws ConfigurationException * @throws InterruptedException */ public List getListDicomObjectFromPatientName(String host, int port, String aet, String queryValue) throws IOException, ConfigurationException, InterruptedException{ return this.getListDicomObjectFromTag(host, port, aet, "PatientName", queryValue); } /** * @param host : HostName to connect to the service * @param port : port of the service * @param aet : Called AET * @param mapOfQuery List of query * @return * @throws IOException * @throws ConfigurationException * @throws InterruptedException * @throws IllegalArgumentException * @throws IllegalAccessException */ public List getListDicomObjectFromTags(String host, int port, String aet, HashMap mapOfQuery) throws IOException, ConfigurationException, InterruptedException, IllegalArgumentException, IllegalAccessException{ if ( (host == null) || (aet == null) ){ return null; } DcmQR pQR = initializeQuery(host,port, aet); if (mapOfQuery != null){ for (String querr:mapOfQuery.keySet()){ Field[] lf = Tag.class.getFields(); boolean isInt = false; boolean isTagged = false; Integer t = null; try{ t = Integer.parseInt(querr,16); isInt = true; } catch(NumberFormatException e){ isInt = false; } for (Field f:lf){ if (f.getName().equals(querr)){ pQR.addMatchingKey(Tag.toTagPath(querr), mapOfQuery.get(querr)); isTagged = true; break; } if (isInt){ int sint = f.getInt(null); if (sint == t.intValue()){ pQR.addMatchingKey(Tag.toTagPath(f.getName()), mapOfQuery.get(querr)); isTagged = true; break; } } } if (isTagged == false) return new ArrayList(); } } pQR.start(); pQR.open(); List listPQR = pQR.query(); pQR.close(); return listPQR; } /** * @param host : HostName to connect to the service * @param port : port of the service * @param aet : Called AET * @param query : query * @param queryValue : Search criteria PatientName, the begin of the name with character * at the end * @return * @throws IOException * @throws ConfigurationException * @throws InterruptedException */ public List getListDicomObjectFromTag(String host, int port, String aet, String query, String queryValue) throws IOException, ConfigurationException, InterruptedException{ if ( (host == null) || (aet == null) || (queryValue == null) || (query == null) ){ return null; } DcmQR pQR = initializeQuery(host,port, aet); pQR.addMatchingKey(Tag.toTagPath(query), queryValue); pQR.start(); pQR.open(); List listPQR = pQR.query(); pQR.close(); return listPQR; } /** * @param host : HostName to connect to the service * @param port : port of the service * @param aet : Called AET * @param queryValue : Search criteria PatientName, the begin of the name with character * at the end * @return DcmQR - Query sent to DCM4chee */ private DcmQR initializeQuery(String host,int port,String aet) { DcmQR pQR = new DcmQR(host); pQR.setRemoteHost(host); pQR.setRemotePort(port); pQR.setCalledAET(aet,true); pQR.setCalling(aet); pQR.setCGet(false); pQR.setPackPDV(true); pQR.setTcpNoDelay(true); pQR.setMaxOpsInvoked(1); pQR.setMaxOpsPerformed(0); pQR.configureTransferCapability(false); pQR.setQueryLevel(DcmQR.QueryRetrieveLevel.PATIENT); return pQR; } /** * @param ldo : List of DicomObject * @return */ public static String formatListDicomObject(List ldo){ String res = new String(); res = res + "## Number of Patients : " ; if (ldo != null){ res = res + ldo.size() + "\n\n"; } for (DicomObject dobj:ldo){ res = res + dobj.toString() + "\n\n"; } return res; } /** * @return */ public List getListTagName() { List listTag = new ArrayList(); Field[] lf = Tag.class.getFields(); for (Field f: lf){ listTag.add(f.getName()); } Collections.sort(listTag); return listTag; } /** * @return */ public List getListTagValue() { List listTag = new ArrayList(); Field[] lf = Tag.class.getFields(); for (Field f: lf){ try { listTag.add((String) f.get(null)); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } Collections.sort(listTag); return listTag; } /** * @param hexString * @return */ public static String getNameOfTag(String hexString){ Integer t = null; try{ t = Integer.parseInt(hexString,16); Field[] lf = Tag.class.getFields(); for (Field f:lf){ int sint = f.getInt(null); if (sint == t.intValue()){ return f.getName(); } } } catch(Exception e){ return null; } return null; } }