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;
020
021import java.util.ArrayList;
022import java.util.Enumeration;
023import java.util.Hashtable;
024import java.util.List;
025
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029import net.sf.json.JSONArray;
030import net.sf.json.JSONObject;
031import org.dcm4che2.data.UID;
032
033import pt.ua.dicoogle.server.web.management.Dicoogle.SOPClassSettings;
034
035/**
036 * Support class for keeping SOPClass/TransferSyntax association
037 * @author Marco Pereira
038 */
039
040public class SOPList {
041
042    private static SOPList instance = null;
043    private static Logger logger = LoggerFactory.getLogger(SOPList.class);
044
045    private Hashtable<String, TransfersStorage> table;    
046    
047    private String [] SOP = {
048        UID.BasicStudyContentNotificationSOPClassRetired,
049        UID.StoredPrintStorageSOPClassRetired,
050        UID.HardcopyGrayscaleImageStorageSOPClassRetired,
051        UID.HardcopyColorImageStorageSOPClassRetired,
052        UID.ComputedRadiographyImageStorage,
053        UID.DigitalXRayImageStorageForPresentation,
054        UID.DigitalXRayImageStorageForProcessing,
055        UID.DigitalMammographyXRayImageStorageForPresentation,
056        //UID.DigitalMammographyXRayImageStorageForProcessing,
057        UID.DigitalIntraOralXRayImageStorageForPresentation,
058        UID.DigitalIntraOralXRayImageStorageForProcessing,
059        UID.StandaloneModalityLUTStorageRetired,
060        UID.EncapsulatedPDFStorage,
061        UID.StandaloneVOILUTStorageRetired,
062        UID.GrayscaleSoftcopyPresentationStateStorageSOPClass,
063        UID.ColorSoftcopyPresentationStateStorageSOPClass,
064        UID.PseudoColorSoftcopyPresentationStateStorageSOPClass,
065        UID.BlendingSoftcopyPresentationStateStorageSOPClass,
066        UID.XRayAngiographicImageStorage,
067        UID.EnhancedXAImageStorage,
068        UID.XRayRadiofluoroscopicImageStorage,
069        UID.EnhancedXRFImageStorage,
070        UID.XRayAngiographicBiPlaneImageStorageRetired,
071        UID.PositronEmissionTomographyImageStorage,
072        UID.StandalonePETCurveStorageRetired,
073        UID.CTImageStorage,
074        UID.EnhancedCTImageStorage,
075        UID.NuclearMedicineImageStorage,
076        UID.UltrasoundMultiFrameImageStorageRetired,
077        UID.UltrasoundMultiFrameImageStorage,
078        UID.MRImageStorage,
079        UID.EnhancedMRImageStorage,
080        UID.MRSpectroscopyStorage,
081        UID.RTImageStorage,
082        UID.RTDoseStorage,
083        UID.RTStructureSetStorage,
084        UID.RTBeamsTreatmentRecordStorage,
085        UID.RTPlanStorage,
086        UID.RTBrachyTreatmentRecordStorage,
087        UID.RTTreatmentSummaryRecordStorage,
088        UID.NuclearMedicineImageStorageRetired,
089        UID.UltrasoundImageStorageRetired,
090        UID.UltrasoundImageStorage,
091        UID.RawDataStorage,
092        UID.SpatialRegistrationStorage,
093        UID.SpatialFiducialsStorage,
094        UID.RealWorldValueMappingStorage,
095        UID.SecondaryCaptureImageStorage,
096        UID.MultiFrameSingleBitSecondaryCaptureImageStorage,        
097        UID.MultiFrameGrayscaleByteSecondaryCaptureImageStorage,
098        UID.MultiFrameGrayscaleWordSecondaryCaptureImageStorage,
099        UID.MultiFrameTrueColorSecondaryCaptureImageStorage,
100        UID.VLImageStorageTrialRetired, /** updated */ 
101        UID.VLEndoscopicImageStorage,
102        UID.VideoEndoscopicImageStorage,
103        UID.VLMicroscopicImageStorage,
104        UID.VideoMicroscopicImageStorage,
105        UID.VLSlideCoordinatesMicroscopicImageStorage,
106        UID.VLPhotographicImageStorage,
107        UID.VideoPhotographicImageStorage,
108        UID.OphthalmicPhotography8BitImageStorage,
109        UID.OphthalmicPhotography16BitImageStorage,
110        UID.StereometricRelationshipStorage,
111        UID.VLMultiFrameImageStorageTrialRetired, /** updated */ 
112        UID.StandaloneOverlayStorageRetired,
113        UID.BasicTextSRStorage, /** updated  */ 
114        UID.EnhancedSRStorage, /** updated */ 
115        UID.ComprehensiveSRStorage, /** updated */ 
116        UID.ProcedureLogStorage,
117        UID.MammographyCADSRStorage, /** updated */
118        UID.KeyObjectSelectionDocumentStorage, /** updated */ 
119        UID.ChestCADSRStorage, /** updated */ 
120        UID.StandaloneCurveStorageRetired,
121        //UID._12leadECGWaveformStorage,
122        UID.GeneralECGWaveformStorage,
123        UID.AmbulatoryECGWaveformStorage,
124        UID.HemodynamicWaveformStorage,
125        UID.CardiacElectrophysiologyWaveformStorage,
126        UID.BasicVoiceAudioWaveformStorage,
127        UID.HangingProtocolStorage,
128        UID.SiemensCSANonImageStorage,
129        UID.VLWholeSlideMicroscopyImageStorage,
130        UID.BreastTomosynthesisImageStorage
131        };
132
133    public static synchronized SOPList getInstance()
134    {
135        if (instance == null) {
136            instance = new SOPList();
137        }
138        return instance;
139    }
140    
141    /**
142     * Creates a new list 
143     */
144    private SOPList() {
145        table = new Hashtable<String, TransfersStorage>();
146        
147        table.put(UID.CTImageStorage, new TransfersStorage());
148        table.put(UID.UltrasoundImageStorage, new TransfersStorage());
149        
150        for(int i=0; i<SOP.length; i++)
151        {
152            table.put(SOP[i], new TransfersStorage());
153        }        
154    }     
155    
156    /**
157     * If configuration file has no information, assume default settings
158     */
159    public synchronized void setDefaultSettings()
160    {
161        TransfersStorage TS;
162        for(int i=0; i<SOP.length; i++)
163        {
164            TS = table.get(SOP[i]);
165            TS.setDefaultSettings();
166        }
167    }
168    
169    /**
170     * Add a SOP Class to the list 
171     * @param UID SOP Class
172     * @return -1 if something went wrong, 1 otherwise
173     */
174    public synchronized int registerSOP(String UID) {
175                
176        if(table.containsKey(UID)) {
177            return -1;
178        }
179        
180        table.put(UID, new TransfersStorage());
181        
182        return 1;
183    }
184    
185    /**
186     * Given a SOP Class returns it's Tranfer Syntaxes
187     * @param UID SOP Class
188     * @return List of tranfer syntaxes for the given SOP Class
189     */
190    public synchronized  TransfersStorage getTS(String UID) {
191        TransfersStorage TS;
192        boolean [] p = null;
193        
194        TS = table.get(UID);
195        return TS;        
196    }    
197    
198    /**
199     * Updates a given SOP Class accepted Tranfer Syntaxes
200     * @param UID SOP Class
201     * @param p Transfer Syntaxes accepted on a boolean array
202     * @param a Globaly accept/reject this SOP Class
203     * @return -1 if something went wrong, 1 otherwise
204     */
205    public synchronized int updateTS(String UID, boolean [] p, boolean a) {
206        TransfersStorage TS;
207        TS = table.get(UID);
208        
209        if (TS != null) {
210            if(TS.setTS(p) != 0)
211            {
212                return -1;
213            }
214            TS.setAccepted(a);
215        }        
216        return 0;    
217    }   
218    /**
219     * Updates a given SOP Class accepted Tranfer Syntaxes
220     * @param UID SOP Class
221     * @param name
222     * @param vale
223     * @return -1 if something went wrong, 1 otherwise
224     */
225    public synchronized int updateTSField(String UID, String name, boolean value) {
226        logger.debug("UID: {}, name: {}, value: {}", UID, name, value);
227
228        TransfersStorage TS;
229        TS = table.get(UID);
230        
231        int index = -1;
232        for (int i = 0; i < TransfersStorage.globalTransferMap.size(); i++) {
233                        if (TransfersStorage.globalTransferMap.get(i).equals(name)) {
234                                index = i;
235                                break;
236                        }
237                }
238        if(TS !=null && index != -1)
239        {
240                if(TS.setTS(value, index) != 0)
241                {
242                        return -1;
243                }
244        }
245        logger.debug("UID: {}, name: {}, value: {}", UID, name, value);
246      
247        return 0;    
248    }   
249    
250    /**
251     * Remove a SOP Class from List
252     * @param UID SOP Class
253     */
254     public synchronized void RemoveSOP(String UID) {
255         table.remove(UID);
256     }
257     
258     /**
259      * Removes selected services that do not have accepted transfers syntaxes 
260      */
261     public synchronized void CleanList()
262     {
263        List l = new ArrayList();
264        Enumeration e = table.keys();
265        TransfersStorage TS;        
266        boolean [] p;
267        boolean unused;
268        int i;
269        int j;
270        while(e.hasMoreElements())
271        {
272            l.add(e.nextElement().toString());
273        }        
274        for(i=0;i<l.size();i++)
275        {        
276            unused = true;
277            TS = table.get(l.get(i).toString());
278            if(TS.getAccepted())
279            {
280                p = TS.getTS();
281                for(j=0; j<p.length; j++)
282                {
283                    if(p[j])
284                    {
285                        unused = false;
286                        break;
287                    }
288                }
289                if(unused)
290                {
291                    TS.setAccepted(false);
292                }
293            }              
294            
295        }        
296     }
297     
298     /**
299      * Get the name of all the SOP Classes used in list
300      * @return List with all the identifiers of SOP Class currently in use
301      */ 
302     public synchronized List getKeys()
303     {
304        List l = new ArrayList();        
305        Enumeration e = table.keys();
306        
307        while(e.hasMoreElements())
308        {            
309            l.add(e.nextElement().toString());
310        }        
311        return l;
312     }
313     
314     /**
315      * Get the number of SOP Classes that are marked as accepted
316      * @return The number of SOP Classes that are actually marked as accepted
317      */
318     public synchronized int getAccepted()
319     {
320        List l = new ArrayList();    
321        TransfersStorage local;
322        Enumeration e = table.keys();
323        
324        while(e.hasMoreElements())
325        {            
326            l.add(e.nextElement().toString());
327        }
328        
329        int count = 0;
330        for(int i =0; i<l.size();i++)
331        {
332            local = table.get(l.get(i));
333            if (local != null)
334            {
335                if(local.getAccepted())
336                {
337                    count++;
338                }
339            }
340        }
341        return count;
342     }
343    
344     public String getSOPList(){
345         JSONArray sopList = new JSONArray();
346         for(String uid : SOP)
347         {       JSONObject elem = new JSONObject();
348                 elem.put("uid", uid);
349                 elem.put("sop_name", SOPClassSettings.getInstance().getSOPClasses().get(uid));
350                 JSONArray options = new JSONArray();
351                 TransfersStorage ts = getTS(uid);
352                 for(int i = 0; i< ts.getTS().length; i++)
353                 {
354                         JSONObject tsobj = new JSONObject();
355                         String name = ts.globalTransferMap.get(i);
356                         boolean value = ts.getTS()[i];
357                         tsobj.put("name", name);
358                         tsobj.put("value", value);
359                         
360                         options.add(tsobj);
361                 }
362                 elem.put("options", options);
363                 sopList.add(elem);
364                 
365                 
366         }
367         return sopList.toString();
368         
369     }
370    
371            
372}