|
1 | 1 | #include "RebrickableMinifigThemeImporter.h" |
| 2 | +#include "RebrickableThemeCatalogImporter.h" |
2 | 3 |
|
3 | 4 | #include "RebrickableCsvInputResolver.h" |
4 | 5 | #include "../database/DatabaseManager.h" |
@@ -131,25 +132,14 @@ RebrickableMinifigThemeImporter::importDirectory(const QString& directoryPath) |
131 | 132 | catalogCheck.finish(); |
132 | 133 | if(!db.transaction()){result.message="Unable to begin Theme import transaction.";return result;} |
133 | 134 | auto fail=[&](const QString& m){db.rollback();result.message=m;return result;}; |
134 | | - const QString now=QDateTime::currentDateTimeUtc().toString(Qt::ISODateWithMs); |
135 | | - QHash<QString,ExistingTheme> existing; QSqlQuery q(db); |
136 | | - q.prepare("SELECT tc.id,tei.id,tei.external_id,tc.name,COALESCE(tc.parent_theme_catalog_id,0),tei.is_active FROM theme_external_identifier tei JOIN theme_catalog tc ON tc.id=tei.theme_catalog_id WHERE tei.provider=:p");q.bindValue(":p",Provider); |
137 | | - if(!q.exec()) return fail(q.lastError().text()); while(q.next()) existing.insert(q.value(2).toString(),{q.value(0).toInt(),q.value(1).toInt(),q.value(3).toString(),q.value(4).toInt(),q.value(5).toBool()}); |
138 | | - QHash<QString,int> internal; QSet<QString> updatedThemes; QSqlQuery ic(db),ii(db),uc(db),ui(db); |
139 | | - ic.prepare("INSERT INTO theme_catalog(name,parent_theme_catalog_id,is_active,created_utc,modified_utc) VALUES(:n,NULL,1,:c,:m)"); |
140 | | - ii.prepare("INSERT INTO theme_external_identifier(theme_catalog_id,provider,external_id,source,is_active,created_utc,modified_utc) VALUES(:id,:p,:e,:s,1,:c,:m)"); |
141 | | - uc.prepare("UPDATE theme_catalog SET name=:n,is_active=1,modified_utc=:m WHERE id=:id"); |
142 | | - ui.prepare("UPDATE theme_external_identifier SET source=:s,is_active=1,modified_utc=:m WHERE id=:id"); |
143 | | - for(const ThemeRow& t:themes){ |
144 | | - if(existing.contains(t.id)){auto e=existing.value(t.id);internal.insert(t.id,e.catalogId);uc.bindValue(":n",t.name);uc.bindValue(":m",now);uc.bindValue(":id",e.catalogId);if(!uc.exec())return fail(uc.lastError().text()); |
145 | | - if(e.name!=t.name)updatedThemes.insert(t.id);if(!e.active){ui.bindValue(":s",Source);ui.bindValue(":m",now);ui.bindValue(":id",e.identityId);if(!ui.exec())return fail(ui.lastError().text());++result.themesReactivated;}} |
146 | | - else{ic.bindValue(":n",t.name);ic.bindValue(":c",now);ic.bindValue(":m",now);if(!ic.exec())return fail(ic.lastError().text());int id=ic.lastInsertId().toInt();internal.insert(t.id,id);ii.bindValue(":id",id);ii.bindValue(":p",Provider);ii.bindValue(":e",t.id);ii.bindValue(":s",Source);ii.bindValue(":c",now);ii.bindValue(":m",now);if(!ii.exec())return fail(ii.lastError().text());++result.themesInserted;}} |
147 | | - QSqlQuery parent(db);parent.prepare("UPDATE theme_catalog SET parent_theme_catalog_id=:parent,modified_utc=:m WHERE id=:id AND COALESCE(parent_theme_catalog_id,0)<>:parent_compare"); |
148 | | - for(const ThemeRow&t:themes){int pid=t.parentId.isEmpty()?0:internal.value(t.parentId);parent.bindValue(":parent",pid?QVariant(pid):QVariant());parent.bindValue(":parent_compare",pid);parent.bindValue(":m",now);parent.bindValue(":id",internal.value(t.id));if(!parent.exec())return fail(parent.lastError().text());if(parent.numRowsAffected()>0&&existing.contains(t.id))updatedThemes.insert(t.id);} |
149 | | - result.themesUpdated = updatedThemes.size(); |
150 | | - QSqlQuery deactivate(db);deactivate.prepare("UPDATE theme_external_identifier SET is_active=0,modified_utc=:m WHERE id=:id AND is_active=1"); |
151 | | - for(auto it=existing.cbegin();it!=existing.cend();++it)if(!themes.contains(it.key())&&it.value().active){deactivate.bindValue(":m",now);deactivate.bindValue(":id",it.value().identityId);if(!deactivate.exec())return fail(deactivate.lastError().text());++result.themesDeactivated;} |
152 | | - QSqlQuery sync(db);sync.prepare("UPDATE theme_catalog SET is_active=CASE WHEN EXISTS(SELECT 1 FROM theme_external_identifier tei WHERE tei.theme_catalog_id=theme_catalog.id AND tei.is_active=1) THEN 1 ELSE 0 END,modified_utc=:m");sync.bindValue(":m",now);if(!sync.exec())return fail(sync.lastError().text()); |
| 135 | + const auto themeResult = RebrickableThemeCatalogImporter().importFile( |
| 136 | + themePath, db, nullptr, false, Source); |
| 137 | + if (!themeResult.success) return fail(themeResult.message); |
| 138 | + result.themesInserted=themeResult.inserted; result.themesUpdated=themeResult.updated; |
| 139 | + result.themesReactivated=themeResult.reactivated; result.themesDeactivated=themeResult.deactivated; |
| 140 | + QHash<QString,int> internal; QSqlQuery q(db); |
| 141 | + q.prepare("SELECT tei.external_id,tei.theme_catalog_id FROM theme_external_identifier tei WHERE tei.provider=:p AND tei.is_active=1");q.bindValue(":p",Provider); |
| 142 | + if(!q.exec())return fail(q.lastError().text());while(q.next())internal.insert(q.value(0).toString(),q.value(1).toInt()); |
153 | 143 | QHash<QString,int> minifigs; q.prepare("SELECT mei.external_id,mei.minifig_catalog_id FROM minifig_external_identifier mei JOIN minifig_catalog mc ON mc.id=mei.minifig_catalog_id WHERE mei.provider=:p AND mei.is_active=1 AND mc.is_active=1");q.bindValue(":p",Provider);if(!q.exec())return fail(q.lastError().text());while(q.next())minifigs.insert(q.value(0).toString().toCaseFolded(),q.value(1).toInt()); |
154 | 144 | QSet<QString> unresolved, associations; int resolvableRows = 0; for(const auto&r:relationships){int mid=minifigs.value(r.first.toCaseFolded());if(!mid){unresolved.insert(r.first.toCaseFolded());continue;}++resolvableRows;associations.insert(QString::number(mid)+QChar(0x1f)+QString::number(internal.value(r.second)));} |
155 | 145 | result.unresolvedMinifigs=unresolved.size();result.associations=associations.size();result.duplicateRelationshipsCollapsed=resolvableRows-result.associations; |
|
0 commit comments