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 com.google.common.io.ByteStreams;
022import java.io.File;
023import java.io.IOException;
024import java.io.InputStream;
025import java.net.URI;
026import java.nio.ByteBuffer;
027import java.util.ArrayList;
028import java.util.List;
029import org.dcm4che2.io.DicomInputStream;
030import pt.ua.dicoogle.plugins.PluginController;
031import pt.ua.dicoogle.sdk.StorageInputStream;
032import pt.ua.dicoogle.sdk.StorageInterface;
033
034/**
035 *
036 * @author Luís A. Bastião Silva <bastiao@ua.pt>
037 */
038public class CallDCMSend
039{
040
041    public CallDCMSend(ArrayList<File> files, int port, String hostname, String AETitle, String cmoveID) throws Exception
042    {
043
044             DcmSnd dcmsnd = new DcmSnd();
045             
046
047            dcmsnd.setRemoteHost(hostname);
048            dcmsnd.setRemotePort(port);
049            
050            for (File fx : files)
051            {
052                File f = fx;
053                dcmsnd.addFile(f);
054            }
055            dcmsnd.setCalledAET(AETitle);
056     
057        dcmsnd.configureTransferCapability();
058//            try {
059//                dcmsnd.initTLS();
060//            } catch (Exception e) {
061//                System.err.println("ERROR: Failed to initialize TLS context:"
062//                        + e.getMessage());
063//                System.exit(2);
064//            }
065        
066         dcmsnd.setMoveOriginatorMessageID(cmoveID);
067         dcmsnd.start();
068         dcmsnd.open();
069         dcmsnd.send();
070         dcmsnd.close();
071         
072      
073        }
074
075     public CallDCMSend(List<URI> files, int port, String hostname, String AETitle, String cmoveID) throws Exception
076    {
077
078             DcmSndV2 dcmsnd = new DcmSndV2();
079             
080
081            dcmsnd.setRemoteHost(hostname);
082            dcmsnd.setRemotePort(port);
083            
084            for (URI rui : files)
085            {                
086                System.out.println("Entered Retrieving: "+rui.toString());
087                StorageInterface plugin = PluginController.getInstance().getStorageForSchema(rui);
088                System.out.println("Plkugin: " +  plugin);
089                System.out.println("rui.toString: " +  plugin);
090                
091                
092                if(plugin != null)
093                {
094                    System.out.println("Retrieving: "+rui.toString());
095                    try{
096                    System.out.println("Retrieving: "+rui.toString());
097                    Iterable<StorageInputStream> it = plugin.at(rui);
098                    
099                    for( StorageInputStream iStream : it)
100                    {
101                        byte[] byteArr = ByteStreams.toByteArray(new DicomInputStream(iStream.getInputStream()));
102                        dcmsnd.addFile(ByteBuffer.wrap(byteArr));
103                        System.out.println("Added NewFile: "+rui.toString());
104                    }
105                    
106                    /*InputStream retrievedFile = plugin.retrieve(rui); 
107                    byte[] byteArr = ByteStreams.toByteArray(retrievedFile);
108                    dcmsnd.addFile(ByteBuffer.wrap(byteArr));
109                    System.out.println("Added NewFile: "+rui.toString());*/
110                    }catch(IOException ex){
111                        ex.printStackTrace();
112                    }
113                }           
114            }
115            dcmsnd.setCalledAET(AETitle);
116     
117        dcmsnd.configureTransferCapability();
118//            try {
119//                dcmsnd.initTLS();
120//            } catch (Exception e) {
121//                System.err.println("ERROR: Failed to initialize TLS context:"
122//                        + e.getMessage());
123//                System.exit(2);
124//            }
125        
126         dcmsnd.setMoveOriginatorMessageID(cmoveID);
127         dcmsnd.start();
128         dcmsnd.open();
129         dcmsnd.send();
130         dcmsnd.close();
131         
132      
133        }
134
135
136}