private DicomObject toSCN(DicomObject ian) { DicomObject scn = new BasicDicomObject(); scn.putString(Tag.PatientID, VR.LO, ian.getString(Tag.PatientID)); scn.putString(Tag.PatientName, VR.PN, ian.getString(Tag.PatientName)); scn.putString(Tag.StudyID, VR.SH, ian.getString(Tag.StudyID)); scn.putString(Tag.StudyInstanceUID, VR.UI, ian.getString(Tag.StudyInstanceUID)); DicomElement ianSeriesSeq = ian.get(Tag.ReferencedSeriesSequence); DicomElement scnSeriesSeq = scn.putSequence(Tag.ReferencedSeriesSequence); DicomObject ianSeriesItem, scnSeriesItem, scnSOPItem; DicomElement ianSOPSeq, scnSOPSeq; for (int i = 0, n = ianSeriesSeq.countItems(); i < n; ++i) { ianSeriesItem = ianSeriesSeq.getDicomObject(i); scnSeriesItem = new BasicDicomObject(); scnSeriesItem.putString( Tag.SeriesInstanceUID, VR.UI, ianSeriesItem.getString(Tag.SeriesInstanceUID)); scnSeriesSeq.addDicomObject(scnSeriesItem); ianSOPSeq = ianSeriesItem.get(Tag.ReferencedSOPSequence); scnSOPSeq = scnSeriesItem.putSequence(Tag.ReferencedImageSequence); for (int j = 0, m = ianSOPSeq.countItems(); j < m; ++j) { scnSOPItem = new BasicDicomObject(); ianSOPSeq.getDicomObject(j).exclude(INSTANCE_AVAILABILITY).copyTo(scnSOPItem); scnSOPSeq.addDicomObject(scnSOPItem); } } return scn; }
private DicomObject mkStgCmtResult(Association as, DicomObject rqdata) { DicomObject result = new BasicDicomObject(); result.putString(Tag.TransactionUID, VR.UI, rqdata.getString(Tag.TransactionUID)); DicomElement rqsq = rqdata.get(Tag.ReferencedSOPSequence); DicomElement resultsq = result.putSequence(Tag.ReferencedSOPSequence); if (stgcmtRetrieveAET != null) { result.putString(Tag.RetrieveAETitle, VR.AE, stgcmtRetrieveAET); } DicomElement failedsq = null; File dir = getDir(as); for (int i = 0, n = rqsq.countItems(); i < n; i++) { DicomObject rqItem = rqsq.getDicomObject(i); String uid = rqItem.getString(Tag.ReferencedSOPInstanceUID); DicomObject resultItem = new BasicDicomObject(); rqItem.copyTo(resultItem); if (stgcmtRetrieveAETs != null) { resultItem.putString(Tag.RetrieveAETitle, VR.AE, stgcmtRetrieveAETs); } File f = new File(dir, uid); if (f.isFile()) { resultsq.addDicomObject(resultItem); } else { resultItem.putInt(Tag.FailureReason, VR.US, NO_SUCH_OBJECT_INSTANCE); if (failedsq == null) { failedsq = result.putSequence(Tag.FailedSOPSequence); } failedsq.addDicomObject(resultItem); } } return result; }
@Override public void addMedia(MediaElement media) { if (media instanceof DicomVideoElement) { if (media.getMediaReader() instanceof DicomMediaIO) { DicomMediaIO dicomImageLoader = (DicomMediaIO) media.getMediaReader(); frames = dicomImageLoader.getMediaElementNumber(); byte[] mpeg = null; try { width = dicomImageLoader.getWidth(0); height = dicomImageLoader.getHeight(0); DicomObject pixData = dicomImageLoader.readPixelData(); mpeg = pixData.get(TagW.PixelData.getId()).getFragment(1); } catch (Exception e) { e.printStackTrace(); } if (mpeg != null) { OutputStream tempFileStream = null; try { File videoFile = File.createTempFile( "video_", ".mpg", AbstractProperties.APP_TEMP_DIR); // $NON-NLS-1$ //$NON-NLS-2$ tempFileStream = new BufferedOutputStream(new FileOutputStream(videoFile)); tempFileStream.write(mpeg); DicomVideoElement dicom = (DicomVideoElement) media; dicom.setVideoFile(videoFile); this.add(dicom); } catch (Exception e) { e.printStackTrace(); } finally { // Close file. if (tempFileStream != null) { try { tempFileStream.close(); } catch (Exception e) { } } } } // DataExplorerModel model = (DataExplorerModel) getTagValue(TagW.ExplorerModel); // if (model != null) { // model.firePropertyChange(new ObservableEvent(ObservableEvent.BasicAction.Add, model, // null, // new SeriesEvent(SeriesEvent.Action.AddImage, this, insertIndex + frames))); // } } } }
/** * insert the given file into the database * * @param f * @param insertDCMSeriesStmt * @param insertDCMObjectStmt * @param insertDCMTagsStmt * @param currentDbConnection */ private void insertFile2DB( File f, PreparedStatement insertDCMSeriesStmt, PreparedStatement insertDCMObjectStmt, PreparedStatement insertDCMTagsStmt, Connection currentDbConnection) { logger.info( " \r\n\r\n" + "#######################################################\r\n" + "#\r\n" + "# STARTING IMPORT FOR " + f.getName() + "\r\n" + "#\r\n" + "#######################################################"); used_files++; long start = System.currentTimeMillis(); boolean importFile = true; try { DicomHeader dh = new DicomHeader(f); DicomObject dcmObjectHeader = dh.getDicomObject(); // if a rule to filter exist -> no import if (!filterTags.isEmpty()) { for (Iterator<String> iter = filterTags.keySet().iterator(); iter.hasNext(); ) { String tag = (String) iter.next(); // check comma separated filter e. g. 0008,0060=MR, UR, NM, US if (filterTags.get(tag).contains(",")) { String[] splittedValue = filterTags.get(tag).split(", "); for (int i = 0; i < splittedValue.length; i++) { if (splittedValue[i].equals(dh.getHeaderStringValue(tag))) { importFile = false; logger.info("NOT importing File, matches Filtertags: " + filterTags.get(tag)); break; } } } else if (dh.getHeaderStringValue(tag).equals(filterTags.get(tag))) { importFile = false; logger.info("NOT importing File, matches Filtertags: " + filterTags.get(tag)); break; } } } // filter the reconstruction-images if (importFile && CHECK_RECONSTRUCTION) { importFile = checkRecontruction(currentDbConnection, dh); } if (importFile) { // insert dcmSeries if it not exists if (!seriesExists(currentDbConnection, dh)) { try { int i = 1; // index for database-fields in series for (Iterator<String> iter = dicomTags.keySet().iterator(); iter.hasNext(); i++) { String tag = (String) iter.next(); String VR = DicomHeader.getHeaderFieldType(tag); // if Date if ("DA".equals(VR)) { try { Date d = dh.getHeaderDateValue(tag); insertDCMSeriesStmt.setDate(i, new java.sql.Date(d.getTime())); } catch (Exception e) { insertDCMSeriesStmt.setDate(i, null); } // if Time } else if ("TM".equals(VR)) { try { Date d = dh.getHeaderDateValue(tag); insertDCMSeriesStmt.setTime(i, new java.sql.Time(d.getTime())); } catch (Exception e) { insertDCMSeriesStmt.setTime(i, null); } // else String } else { String s = dh.getHeaderStringValue(tag); s = s.replaceAll("\0", ""); insertDCMSeriesStmt.setString(i, s); } } insertDCMSeriesStmt.execute(); // Exception if Seriesuid exist } catch (SQLException sqle) { if (sqle.getMessage().indexOf("duplicate key") < 0) { logger.error("Error adding series: ", sqle); } else { logger.warn("Series allready in DB"); } currentDbConnection.rollback(); } } // insert the dicom-object and the tags try { int j = 1; // index for database-fields in dicom-objects insertDCMObjectStmt.setString(j++, dh.getDicomObject().getString(Tag.SeriesInstanceUID)); insertDCMObjectStmt.setString(j++, dh.getDicomObject().getString(Tag.SOPInstanceUID)); insertDCMObjectStmt.setTimestamp(j++, new Timestamp(System.currentTimeMillis())); // storing the md5 of pixel-data in database, if CHECKSUM is true if (CHECKSUM) { DicomInputStream dis = new DicomInputStream(f); DicomObject dcmObj = new BasicDicomObject(); dis.readDicomObject(dcmObj, -1); DicomElement de = dcmObj.get(Tag.PixelData); byte[] bytes = de.getBytes(); MessageDigest md = MessageDigest.getInstance("MD5"); md.update(bytes); byte[] md5 = md.digest(); BigInteger bi = new BigInteger(1, md5); insertDCMObjectStmt.setString(j++, bi.toString(16)); } else { insertDCMObjectStmt.setString(j++, null); } insertDCMObjectStmt.setString(j++, SOFTWARE_VERSION); insertDCMObjectStmt.execute(); // currentDbConnection.commit(); int parentID = getDCMObjectID( currentDbConnection, dh.getDicomObject().getString(Tag.SOPInstanceUID)); itemOrder = 1; // recursive insert of dicom-tags insertDCMTagsStmt.clearBatch(); insertDicomTags(parentID, dcmObjectHeader, null, insertDCMTagsStmt); insertDCMTagsStmt.executeBatch(); currentDbConnection.commit(); logger.info("File inserted"); imported_files++; } catch (SQLException sqle) { currentDbConnection.rollback(); if (sqle.getMessage().indexOf("duplicate key") < 0) { logger.error("Error importing image: ", sqle); } else { logger.warn( "File with UID=" + dh.getDicomObject().getString(Tag.SOPInstanceUID) + " allready in db"); } } } if (DELETE_FILES_AFTER_IMPORT) { f.delete(); } else { boolean success = f.renameTo(new File(OK_DIR, f.getName())); if (success) { logger.info("moved file to: " + OK_DIR + File.separator + f.getName()); } else { logger.warn("unable to move file to: " + OK_DIR + File.separator + f.getName()); } } } catch (Exception e) { boolean success = f.renameTo(new File(ERROR_DIR, f.getName())); logger.log( Level.WARN, "Failed to insert file: " + f.getAbsolutePath() + " " + e.getMessage(), e); if (success) { logger.info("moved file to: " + ERROR_DIR + File.separator + f.getName()); } else { logger.warn("unable to move file to: " + ERROR_DIR + File.separator + f.getName()); } try { currentDbConnection.rollback(); } catch (SQLException e1) { logger.log(Level.ERROR, e1.getMessage(), e1); } } logger.info( "END OF IMPORT FOR " + f.getName() + " took " + (System.currentTimeMillis() - start) + "µsec. \r\n" + "Files used: " + used_files + " Files imported: " + imported_files + " \r\n" + "#######################################################"); }