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-sdk-ext.
005 *
006 * Dicoogle/dicoogle-sdk-ext 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-sdk-ext 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.sdk.p2p.Messages.Handlers;
020
021import java.io.File;
022import java.awt.Desktop;
023import java.util.HashMap;
024import java.util.List;
025import pt.ua.dicoogle.sdk.NetworkPluginAdapter;
026import pt.ua.dicoogle.sdk.Utils.TaskRequest;
027import pt.ua.dicoogle.sdk.Utils.TaskRequestsConstants;
028import pt.ua.dicoogle.sdk.observables.FileObservable;
029import pt.ua.dicoogle.sdk.p2p.Messages.FileMessage;
030import pt.ua.dicoogle.sdk.p2p.Messages.MessageI;
031
032/**
033 *
034 * @author Carlos Ferreira
035 */
036public class FileResponseHandler implements MessageHandler
037{
038
039    private String DirectoryPath;
040    private NetworkPluginAdapter NPA;
041
042    public FileResponseHandler(String DirectoryPath, NetworkPluginAdapter NPA)
043    {
044        this.NPA = NPA;
045        this.DirectoryPath = DirectoryPath;
046        File directory = new File(this.DirectoryPath);
047
048        //System.out.println(directory.getAbsoluteFile());
049        if (!directory.exists())
050        {
051            directory.mkdir();
052        } else
053        {
054            if (!directory.isDirectory())
055            {
056                //   throw new BadMessage();
057            }
058        }
059    }
060
061    public void handleMessage(MessageI message, String address)
062    {
063        if (!FileMessage.class.isInstance(message))
064        {
065            return;
066        }
067        FileMessage fmessage = (FileMessage) message;
068
069        /**
070         * ATTENTION: the FileMessage sent by the plugin is now only constituted by the filePath...
071         * The plugin is responsible for the storage of the file into the local file system
072         */
073        String filePath = new String(fmessage.getMessage());
074        List<FileObservable> filesRequested = this.NPA.getRequestedFiles();
075        for (FileObservable fo : filesRequested)
076        {
077            if ((fo.getFileOrigin().compareTo(address) == 0) && (fo.getFileName().compareTo(fmessage.getFilename()) == 0))
078            {
079                /**
080                 * Request the local indexing of the new file
081                 */
082                HashMap<Integer, Object> parameters = new HashMap<Integer, Object>();
083                parameters.put(TaskRequestsConstants.P_FILE_PATH, filePath);
084                TaskRequest task = new TaskRequest(TaskRequestsConstants.T_INDEX_FILE, this.NPA.getName(), parameters);
085                this.NPA.getTaskRequestsList().addTask(task);
086
087                /**
088                 * TODO: This must be removed when the GUI is able to deal with the new files and show them
089                 */
090                try
091                {
092                    Desktop.getDesktop().open(new File(filePath.replace('\\', '/')));
093                } catch (Exception e)
094                {
095                    e.printStackTrace();
096                }
097                fo.setFilePath(filePath);
098                break;
099            }
100        }
101
102
103        /*ObjMessage msg = (ObjMessage) message;
104        
105        //File received = (File) msg.getMessage();
106        
107        File newFile = new File(this.DirectoryPath + File.separator + ((FileMessage)message).getFilename() );
108        
109        FileOutputStream fos = null;
110        byte[] bytes = null;
111        bytes = (byte[]) msg.getMessage();
112        try
113        {
114        fos = new FileOutputStream(newFile);
115        } catch (FileNotFoundException ex) {
116        ex.printStackTrace(System.out);
117        }
118        try {
119        fos.write(bytes);
120        } catch (IOException ex) {
121        ex.printStackTrace(System.out);
122        } finally {
123        try {
124        fos.close();
125        } catch (IOException ex) {
126        ex.printStackTrace(System.out);
127        }
128        }
129        IndexEngine.getInstance().index(newFile.getAbsolutePath());
130         */
131    }
132}