001/* ***** BEGIN LICENSE BLOCK ***** 002 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 003 * 004 * The contents of this file are subject to the Mozilla Public License Version 005 * 1.1 (the "License"); you may not use this file except in compliance with 006 * the License. You may obtain a copy of the License at 007 * http://www.mozilla.org/MPL/ 008 * 009 * Software distributed under the License is distributed on an "AS IS" basis, 010 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 011 * for the specific language governing rights and limitations under the 012 * License. 013 * 014 * The Original Code is part of dcm4che, an implementation of DICOM(TM) in 015 * Java(TM), hosted at https://github.com/gunterze/dcm4che. 016 * 017 * The Initial Developer of the Original Code is 018 * Agfa Healthcare. 019 * Portions created by the Initial Developer are Copyright (C) 2011 020 * the Initial Developer. All Rights Reserved. 021 * 022 * Contributor(s): 023 * See @authors listed below 024 * 025 * Alternatively, the contents of this file may be used under the terms of 026 * either the GNU General Public License Version 2 or later (the "GPL"), or 027 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 028 * in which case the provisions of the GPL or the LGPL are applicable instead 029 * of those above. If you wish to allow use of your version of this file only 030 * under the terms of either the GPL or the LGPL, and not to allow others to 031 * use your version of this file under the terms of the MPL, indicate your 032 * decision by deleting the provisions above and replace them with the notice 033 * and other provisions required by the GPL or the LGPL. If you do not delete 034 * the provisions above, a recipient may use your version of this file under 035 * the terms of any one of the MPL, the GPL or the LGPL. 036 * 037 * ***** END LICENSE BLOCK ***** */ 038 039package org.dcm4che3.net.service; 040 041import java.io.IOException; 042import java.util.NoSuchElementException; 043 044import org.dcm4che3.data.Attributes; 045import org.dcm4che3.data.Tag; 046import org.dcm4che3.data.VR; 047import org.dcm4che3.net.Association; 048import org.dcm4che3.net.Commands; 049import org.dcm4che3.net.Status; 050import org.dcm4che3.net.pdu.PresentationContext; 051import org.slf4j.Logger; 052import org.slf4j.LoggerFactory; 053 054/** 055 * @author Gunter Zeilinger <gunterze@gmail.com> 056 */ 057public class BasicQueryTask implements QueryTask { 058 059 private static Logger log = LoggerFactory.getLogger(BasicQueryTask.class); 060 061 protected final Association as; 062 protected final PresentationContext pc; 063 protected final Attributes rq; 064 protected final Attributes keys; 065 protected volatile boolean canceled; 066 protected boolean optionalKeysNotSupported = false; 067 068 public BasicQueryTask(Association as, PresentationContext pc, 069 Attributes rq, Attributes keys) { 070 this.as = as; 071 this.pc = pc; 072 this.rq = rq; 073 this.keys = keys; 074 } 075 076 public boolean isOptionalKeysNotSupported() { 077 return optionalKeysNotSupported; 078 } 079 080 public void setOptionalKeysNotSupported(boolean optionalKeysNotSupported) { 081 this.optionalKeysNotSupported = optionalKeysNotSupported; 082 } 083 084 @Override 085 public void onCancelRQ(Association as) { 086 canceled = true; 087 } 088 089 @Override 090 public void run() { 091 try { 092 int msgId = rq.getInt(Tag.MessageID, -1); 093 as.addCancelRQHandler(msgId, this); 094 try { 095 while (!canceled && hasMoreMatches()) { 096 Attributes match = adjust(nextMatch()); 097 if (match != null) { 098 int status = optionalKeysNotSupported 099 ? Status.PendingWarning 100 : Status.Pending; 101 as.writeDimseRSP(pc, Commands.mkCFindRSP(rq, status), match); 102 } 103 } 104 int status = canceled ? Status.Cancel : Status.Success; 105 as.writeDimseRSP(pc, Commands.mkCFindRSP(rq, status)); 106 } catch (DicomServiceException e) { 107 log.error("Query Error",e); 108 Attributes rsp = e.mkRSP(0x8020, msgId); 109 as.writeDimseRSP(pc, rsp, e.getDataset()); 110 } finally { 111 as.removeCancelRQHandler(msgId); 112 close(); 113 } 114 } catch (IOException e) { 115 // handled by Association 116 } 117 } 118 119 protected void close() { 120 } 121 122 protected Attributes nextMatch() throws DicomServiceException { 123 throw new NoSuchElementException(); 124 } 125 126 protected boolean hasMoreMatches() throws DicomServiceException { 127 return false; 128 } 129 130 protected Attributes adjust(Attributes match) throws DicomServiceException { 131 if (match == null) 132 return null; 133 134 Attributes filtered = new Attributes(match.size()); 135 // include SpecificCharacterSet also if not in keys 136 if (!keys.contains(Tag.SpecificCharacterSet)) { 137 String[] ss = match.getStrings(Tag.SpecificCharacterSet); 138 if (ss != null) 139 filtered.setString(Tag.SpecificCharacterSet, VR.CS, ss); 140 } 141 filtered.addSelected(match, keys); 142 return filtered; 143 } 144}