SAttachmentSeriesReader.cpp 5.91 KB
Newer Older
1
/* ***** BEGIN LICENSE BLOCK *****
2
 * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
 * published by the Free Software Foundation.
 * ****** END LICENSE BLOCK ****** */

#include "ioData/SAttachmentSeriesReader.hpp"

#include <fwData/location/Folder.hpp>
#include <fwData/location/SingleFile.hpp>
#include <fwData/mt/ObjectWriteLock.hpp>

#include <fwGui/dialog/LocationDialog.hpp>

#include <fwMedData/AttachmentSeries.hpp>
#include <fwMedData/Equipment.hpp>
#include <fwMedData/Patient.hpp>
#include <fwMedData/SeriesDB.hpp>
#include <fwMedData/Study.hpp>

#include <fwMedDataTools/helper/SeriesDB.hpp>

#include <fwServices/macros.hpp>

#include <fwTools/dateAndTime.hpp>
26
#include <fwTools/UUID.hpp>
27
28
29
30
31
32
33
34
35
36
37

#include <io/IReader.hpp>

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/filesystem/operations.hpp>

fwServicesRegisterMacro( ::io::IReader, ::ioData::SAttachmentSeriesReader, ::fwMedData::SeriesDB );

namespace ioData
{

38
39
//------------------------------------------------------------------------------

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
std::string getMediaType(const ::boost::filesystem::path& media)
{
    std::string mediaType;
    std::map<std::string, std::string> mimeMap =
    {
        {".txt", "text/plain"},
        {".rtf", "text/rtf"},
        {".pdf", "application/pdf"},
        {".zip", "application/zip"},
        {".mp3", "audio/mpeg"},
        {".wav", "audio/x-wav"},
        {".bmp", "image/bmp"},
        {".jpeg", "image/jpeg"},
        {".mpeg", "video/mpeg"},
        {".mp4", "video/mpeg"},
        {".avi", "video/x-msvideo"}
    };
    const std::string extension = media.extension().string();
    const auto iter             = mimeMap.find(extension);
    if(iter != mimeMap.end())
    {
        mediaType = iter->second;
    }
    return mediaType;
}

66
67
//------------------------------------------------------------------------------

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
void  initSeries(::fwMedData::Series::sptr series, const std::string& instanceUID)
{
    const std::string unknown = "unknown";
    series->setModality("OT");
    ::boost::posix_time::ptime now = ::boost::posix_time::second_clock::local_time();
    const std::string date = ::fwTools::getDate(now);
    const std::string time = ::fwTools::getTime(now);
    series->setDate(date);
    series->setTime(time);
    //series->setDescription(??);
    //series->setPerformingPhysiciansName(??);

    series->getEquipment()->setInstitutionName(unknown);

    series->getPatient()->setName(unknown);
    series->getPatient()->setPatientId(unknown);
    series->getPatient()->setBirthdate(unknown);
    series->getPatient()->setSex(unknown);

    series->getStudy()->setInstanceUID(instanceUID);
    series->getStudy()->setDate(date);
    series->getStudy()->setTime(time);
    series->getStudy()->setReferringPhysicianName(unknown);
    series->getStudy()->setDescription(unknown);
    series->getStudy()->setPatientAge(unknown);
}

//-----------------------------------------------------------------------------

SAttachmentSeriesReader::SAttachmentSeriesReader()
{
}

//-----------------------------------------------------------------------------

void SAttachmentSeriesReader::info(std::ostream& _sstream )
{
    this->SuperClass::info( _sstream );
    _sstream << std::endl << " AttachmentSerie reader";
}

//-----------------------------------------------------------------------------

std::vector< std::string > SAttachmentSeriesReader::getSupportedExtensions()
{
    std::vector< std::string > extensions;
    extensions.push_back(".*");
    return extensions;
}

//------------------------------------------------------------------------------

::io::IOPathType SAttachmentSeriesReader::getIOPathType() const
{
    return ::io::FILE;
}

//------------------------------------------------------------------------------

127
128
129
130
131
132
133
void SAttachmentSeriesReader::configuring() throw(::fwTools::Failed)
{
    ::io::IReader::configuring();
}

//------------------------------------------------------------------------------

134
135
136
137
138
void SAttachmentSeriesReader::configureWithIHM()
{
    static ::boost::filesystem::path _sDefaultPath("");

    ::fwGui::dialog::LocationDialog dialogFile;
139
    dialogFile.setTitle(m_windowTitle.empty() ? "Choose a attachment file" : m_windowTitle);
140
    dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
141
    dialogFile.addFilter("Attachment file", "*.*");
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
    dialogFile.setOption(::fwGui::dialog::ILocationDialog::READ);

    ::fwData::location::SingleFile::sptr result;
    result = ::fwData::location::SingleFile::dynamicCast( dialogFile.show() );
    if (result)
    {
        _sDefaultPath = result->getPath().parent_path();
        dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
        this->setFile(result->getPath());
    }
    else
    {
        this->clearLocations();
    }
}

//------------------------------------------------------------------------------

void SAttachmentSeriesReader::updating() throw(::fwTools::Failed)
{
    if(this->hasLocationDefined())
    {
        // Retrieve object
        ::fwMedData::SeriesDB::sptr seriesDB;
        if (this->isVersion2())
        {
            seriesDB = this->getInOut< ::fwMedData::SeriesDB >(::io::s_DATA_KEY);
        }
        else
        {
            seriesDB = this->getObject< ::fwMedData::SeriesDB >();
        }

        ::fwMedData::AttachmentSeries::sptr series = ::fwMedData::AttachmentSeries::New();
        const std::string instanceUID = ::fwTools::UUID::generateUUID();
        initSeries(series, instanceUID);
        series->setAttachmentPath(this->getFile());
        const std::string mediaType = getMediaType(this->getFile());
        series->setMediaType(mediaType);

        ::fwMedDataTools::helper::SeriesDB seriesDBHelper(seriesDB);

        ::fwData::mt::ObjectWriteLock lock(seriesDB);
        seriesDBHelper.add(series);
        seriesDBHelper.notify();
    }
}

//------------------------------------------------------------------------------

}