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.tool.dcmqrscp;
040
041import java.io.IOException;
042
043import org.dcm4che3.data.Attributes;
044import org.dcm4che3.data.Tag;
045import org.dcm4che3.data.VR;
046import org.dcm4che3.media.DicomDirReader;
047import org.dcm4che3.net.Association;
048import org.dcm4che3.net.Status;
049import org.dcm4che3.net.pdu.PresentationContext;
050import org.dcm4che3.net.service.BasicQueryTask;
051import org.dcm4che3.net.service.DicomServiceException;
052
053class PatientQueryTask extends BasicQueryTask {
054
055    protected final DicomDirReader ddr;
056    protected final String availability;
057    protected final String[] patIDs;
058    protected Attributes patRec;
059
060    public PatientQueryTask(Association as, PresentationContext pc, Attributes rq, Attributes keys,
061            DicomDirReader ddr, String availability) throws DicomServiceException {
062        super(as, pc, rq, keys);
063        this.ddr = ddr;
064        this.availability = availability;
065        this.patIDs = keys.getStrings(Tag.PatientID);
066        wrappedFindNextPatient();
067    }
068
069    @Override
070    public boolean hasMoreMatches() throws DicomServiceException {
071        return patRec != null;
072    }
073
074    @Override
075    public Attributes nextMatch() throws DicomServiceException {
076        Attributes tmp = patRec;
077        wrappedFindNextPatient();
078        return tmp;
079    }
080
081    @Override
082    protected Attributes adjust(Attributes match) throws DicomServiceException {
083        Attributes adjust = super.adjust(match);
084        adjust.remove(Tag.DirectoryRecordType);
085        if (keys.contains(Tag.SOPClassUID))
086             adjust.setString(Tag.SOPClassUID, VR.UI,
087                     match.getString(Tag.ReferencedSOPClassUIDInFile));
088        if (keys.contains(Tag.SOPInstanceUID))
089             adjust.setString(Tag.SOPInstanceUID, VR.UI,
090                     match.getString(Tag.ReferencedSOPInstanceUIDInFile));
091        adjust.setString(Tag.QueryRetrieveLevel, VR.CS,
092                keys.getString(Tag.QueryRetrieveLevel));
093        adjust.setString(Tag.RetrieveAETitle, VR.AE, as.getCalledAET());
094        if (availability != null)
095            adjust.setString(Tag.InstanceAvailability, VR.CS, availability);
096        adjust.setString(Tag.StorageMediaFileSetID, VR.SH, ddr.getFileSetID());
097        adjust.setString(Tag.StorageMediaFileSetUID, VR.UI, ddr.getFileSetUID());
098        match.setString(Tag.SOPClassUID, VR.UI,
099                match.getString(Tag.ReferencedSOPClassUIDInFile));
100        match.setString(Tag.SOPInstanceUID, VR.UI,
101                match.getString(Tag.ReferencedSOPInstanceUIDInFile));
102
103        return adjust;
104    }
105
106    private void wrappedFindNextPatient() throws DicomServiceException {
107        try {
108            findNextPatient();
109        } catch (IOException e) {
110            throw new DicomServiceException(Status.UnableToProcess, e);
111        }
112    }
113
114    protected boolean findNextPatient() throws IOException {
115        if (patRec == null)
116            patRec = ddr.findPatientRecord(patIDs);
117        else if (patIDs != null && patIDs.length == 1)
118            patRec = null;
119        else
120            patRec = ddr.findNextPatientRecord(patRec, patIDs);
121
122        return patRec != null;
123    }
124}