Implemented initial deleteDataObject(id) in Db-layer. Implemented
[emufront] / src / db / dbmediaimagecontainer.cpp
1 // EmuFront
2 // Copyright 2010 Mikko Keinänen
3 //
4 // This file is part of EmuFront.
5 //
6 //
7 // EmuFront is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // EmuFront is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
19
20 #include <QDebug>
21 #include <QSqlRecord>
22 #include <QSqlQuery>
23 #include <QSqlRelationalTableModel>
24 #include <QSqlError>
25 #include "dbmediaimagecontainer.h"
26
27 DbMediaImageContainer::DbMediaImageContainer(QObject *parent)
28     : DbFile(parent) // DbQueryModelManager(parent)
29 {
30     dbMediaImage = new DbMediaImage(parent);
31     //dbFile = new DbFile(parent);
32 }
33
34 bool DbMediaImageContainer::updateDataObjectToModel(const EmuFrontObject *efo)
35 {
36     // TODO
37     return false;
38 }
39
40 int DbMediaImageContainer::insertDataObjectToModel(const EmuFrontObject *efo)
41 {
42     const MediaImageContainer *mic
43         = dynamic_cast<const MediaImageContainer *>(efo);
44
45     // check if this media image container is already in the database
46     int fileId = -1;
47     if ((fileId = getMediaImageContainer(mic->getCheckSum())) >= 0)
48         return fileId;
49
50     if (!mic->getFilePath())
51         throw new EmuFrontException("Cannot install media image "
52             "container to database without a file path object!");
53
54     QList<MediaImage*> images = mic->getMediaImages();
55     QList<int> ids = dbMediaImage->storeMediaImages(images);
56
57     if (ids.count() <= 0)
58         return -1;
59
60     /* Contained Media images successfully stored to db,
61         storing media image container also */
62
63     try {
64
65         // Insert MediaImageContainer first as a EmuFrontFile object to file table.
66
67         // File id is used to store the media image container instance to database,
68         // file id is also the media image container id
69         fileId = DbFile::insertDataObjectToModel(mic);
70
71         if (fileId < 0) {
72             // TODO: note we most surely need to catch the exception
73             // in the calling code block and clean
74             // all the media image and ...containers from
75             // the memory!
76             throw new EmuFrontException(
77                     QString(tr("Inserting media image container %1 to file database failed"))
78                     .arg(mic->getName()));
79         }
80
81         // Insert to mediaimagecontainer table
82
83         QSqlQuery q;
84         q.prepare("INSERT INTO mediaimagecontainer "
85                   "(fileid, filepathid, updatetime) "
86                   "VALUES (:fileid, :filepathid, :updatetime)");
87         q.bindValue(":fileid", fileId);
88         q.bindValue(":filepathid", mic->getFilePath()->getId());
89         q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
90         if (!q.exec()){
91             DbFile::deleteDataObject(fileId);
92             throw new EmuFrontException("Failed inserting media image to database!");
93         }
94         linkMediaImagesWithContainer(fileId, ids);
95     } catch (EmuFrontException e) {
96         dbMediaImage->removeOrphanedMediaImages(ids);
97         throw e;
98     }
99
100     return fileId;
101 }
102
103 bool DbMediaImageContainer::deleteDataObjectFromModel(QModelIndex *i)
104 {
105     // TODO
106     return false;
107 }
108
109 int DbMediaImageContainer::countDataObjectRefs(int id) const
110 {
111     // TODO
112     return -1;
113 }
114
115 QString DbMediaImageContainer::constructSelect(QString whereClause) const
116 {
117     // TODO
118     return "";
119 }
120
121 QString DbMediaImageContainer::constructFilterById(int id) const
122 {
123     // TODO
124     return "";
125 }
126
127 QString DbMediaImageContainer::constructSelectById(int id) const
128 {
129     // TODO
130     return "";
131 }
132
133 EmuFrontObject* DbMediaImageContainer::recordToDataObject(const QSqlRecord *)
134 {
135     // TODO
136     return 0;
137 }
138
139 QSqlQueryModel* DbMediaImageContainer::getData()
140 {
141     // TODO
142     return 0;
143 }
144
145 /* Returns the id of a media image container with a given cheksum or -1 if not found */
146 int DbMediaImageContainer::getMediaImageContainer(QString checksum) const
147 {
148     // TODO
149     return -1;
150 }
151
152
153 /**
154 * Stores media image containers, including the media images included
155 * to database.
156 */
157 void DbMediaImageContainer::storeContainers(QList<MediaImageContainer *> lst, FilePathObject *fpo)
158 {
159     qDebug() << "Storing media image containers to database.";
160     foreach(MediaImageContainer *mic, lst)
161     {
162         qDebug() << "Media image container " << mic->getName();
163         int micFileId = insertDataObjectToModel(mic);
164     }
165 }
166
167 void DbMediaImageContainer::linkMediaImagesWithContainer(int micId, QList<int> miIds)
168 {
169     if (micId < 0 || miIds.count() <= 0)
170         return;
171
172     QSqlQuery q;
173     q.prepare("INSERT INTO mediaimagecontainer_mediaimage "
174         "(mediaimagecontainerid, mediaimageid) "
175         "VALUES (:micid, :miid) ");
176     q.bindValue(":micid", micId);
177
178     foreach(int miid, miIds) {
179         q.bindValue(":miid", miid);
180         if (!q.exec()) {
181             throw new EmuFrontException(QString("Failed linking media "
182                 "image container %1 to a media image %2").arg(micId).arg(miid));
183         }
184     }
185 }