Пример #1
0
 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;
 }
Пример #2
0
  private void insertDicomTags(
      int dcmobject_id, DicomObject obj, Integer parentId, PreparedStatement stmt)
      throws Exception {

    for (Iterator<DicomElement> it = obj.iterator(); it.hasNext(); ) {
      DicomElement e = it.next();

      StringBuffer sb = new StringBuffer();
      try {
        String[] values = e.getStrings(obj.getSpecificCharacterSet(), false);
        sb.append(values[0].replaceAll("\0", ""));
        int i;
        for (i = 1; i < values.length; i++) {
          sb.append("/").append(values[i].replaceAll("\0", ""));
        }

      } catch (Exception ee) {
        try {
          sb.append(e.getString(obj.getSpecificCharacterSet(), false));
        } catch (Exception e2) {

        }
      }
      stmt.setInt(1, dcmobject_id);
      stmt.setString(2, DicomHeader.toTagString(e.tag()));
      stmt.setString(3, e.vr().toString());

      // only the first 150 chars are saved if the tag-value is longer
      if (sb.length() > 150) {
        stmt.setString(
            4, sb.toString().substring(0, 150).concat("*** end of tag-value not applicable ***"));
      } else {
        stmt.setString(4, sb.toString());
      }

      stmt.setInt(5, itemOrder);
      if (parentId == null) {
        stmt.setNull(6, Types.INTEGER);
      } else {
        stmt.setInt(6, parentId);
      }
      stmt.addBatch();
      // System.out.println( stmt );

      itemOrder++;
      if (e.countItems() > 0) {
        if (e.hasDicomObjects()) {
          for (int i = 0; i < e.countItems(); i++) {
            insertDicomTags(dcmobject_id, e.getDicomObject(i), itemOrder - 1, stmt);
          }
        }
      }
    }
  }
Пример #3
0
 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;
 }
Пример #4
0
  /**
   * 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"
            + "#######################################################");
  }