001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package pt.ua.dicoogle.server.queryretrieve;
020
021import aclmanager.core.LuceneQueryACLManager;
022
023import java.text.DateFormat;
024import java.text.SimpleDateFormat;
025import java.util.Date;
026import java.util.Iterator;
027import java.util.concurrent.Executor;
028
029import org.dcm4che2.data.*;
030
031
032import javax.xml.transform.TransformerConfigurationException;
033
034import org.dcm4che2.net.Association;
035import org.dcm4che2.net.DicomServiceException;
036import org.dcm4che2.net.DimseRSP;
037import org.dcm4che2.net.service.CFindService;
038
039
040import pt.ua.dicoogle.DicomLog.LogDICOM;
041import pt.ua.dicoogle.DicomLog.LogLine;
042
043
044import pt.ua.dicoogle.DicomLog.LogXML;
045import pt.ua.dicoogle.core.ServerSettings;
046import pt.ua.dicoogle.rGUI.server.controllers.Logs;
047
048
049
050import pt.ua.dicoogle.server.DicomNetwork;
051
052/**
053 *
054 * @author Luís A. Bastião Silva <bastiao@ua.pt>
055 */
056public class CFindServiceSCP extends CFindService {
057
058    private ServerSettings s = ServerSettings.getInstance();
059    private int rspdelay = ServerSettings.getInstance().getRspDelay();
060    
061    private DicomNetwork service = null;
062    private LuceneQueryACLManager luke = null;
063
064    private boolean superSpeed = false;
065    
066
067    public CFindServiceSCP(String[] multiSop, Executor e) {
068        super(multiSop, e);
069        this.luke = null;        
070    }
071
072    public CFindServiceSCP(String[] multiSop, Executor e, LuceneQueryACLManager luke) {
073        super(multiSop, e);
074        this.luke = luke;        
075    }
076    
077    /*** CFIND */
078    @Override
079    protected synchronized DimseRSP doCFind(Association as, int pcid,
080            DicomObject cmd, DicomObject keys, DicomObject rsp)
081            throws DicomServiceException {
082
083        //DebugManager.getInstance().debug("doCFind? -- > working on it");
084
085
086        DimseRSP replay = null;
087
088        /**
089         * ///  How create a new Connection? Detect new connections..
090        if (MainWindow.getMw() != null) {
091        MainWindow.getMw().newClientConnection(as);
092        }
093         */
094        /**
095         * Verify Permited AETs
096         */
097        //DebugManager.getInstance().debug(":: Verify Permited AETs @??C-FIND Action ");
098        boolean permited = false;
099
100        if (s.getPermitAllAETitles()) {
101            permited = true;
102        } else {
103            String permitedAETs[] = s.getCAET();
104
105            for (int i = 0; i < permitedAETs.length; i++) {
106                if (permitedAETs[i].equals(as.getCallingAET())) {
107                    permited = true;
108                    break;
109                }
110            }
111        }
112
113
114        if (!permited) {
115            //DebugManager.getInstance().debug("Client association NOT permited: " + as.getCallingAET() + "!");
116            //as.abort();
117
118            //return new FindRSP(keys, rsp, null);
119        } else {
120            //DebugManager.getInstance().debug("Client association permited: " + as.getCallingAET() + "!");
121        }
122
123
124        if (this.rspdelay > 0) {
125            try {
126                this.wait(this.rspdelay);
127            } catch (Exception e) {
128                e.printStackTrace();
129            }
130        }
131
132
133        /**
134         * Search information at Lucene Indexer
135         * So the FindRSP will fill the DimRSP
136         */
137        replay = new FindRSP(keys, rsp,  as.getCallingAET(), luke);
138
139
140        DicomElement e = keys.get(Tag.PatientName);
141        String add = "";
142        if (e != null) {
143            add = new String(e.getBytes());
144        }
145
146        String queryParams = "";
147
148        for (Iterator<DicomElement> iterator = keys.iterator(); iterator.hasNext();)
149        {
150            DicomElement element = iterator.next();
151
152            if (!element.isEmpty())
153            {
154                if (!ElementDictionary.getDictionary().nameOf(element.tag()).contains("Sequence"))
155                    queryParams += ElementDictionary.getDictionary().nameOf(element.tag()) + " - " + element.getValueAsString(new SpecificCharacterSet("UTF-8"), 0) + " ";
156            }
157        }
158        if (!superSpeed)
159        {
160            LogLine ll = new LogLine("cfind", getDateTime(), as.getCallingAET(),
161                    as.toString() + " -- " + add, queryParams);
162            LogDICOM.getInstance().addLine(ll);
163
164            synchronized (LogDICOM.getInstance()) {
165                LogXML l = new LogXML();
166                try {
167                    l.printXML();
168                } catch (TransformerConfigurationException ex) {
169                    ex.printStackTrace();
170                }
171            }
172
173        }
174        //Logs.getInstance().addLog(ll);
175
176
177        return replay;
178    }
179
180    private String getDateTime() {
181        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
182        Date date = new Date();
183        return dateFormat.format(date);
184    }
185
186    /**
187     * @return the service
188     */
189    public DicomNetwork getService() {
190        return service;
191    }
192
193    /**<
194     * @param service the service to set
195     */
196    public void setService(DicomNetwork service) {
197        this.service = service;
198    }
199}