00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "TeDatabaseUtils.h"
00022 #include "../functions/TeLayerFunctions.h"
00023 #include "../kernel/TeDBConnectionsPool.h"
00024 #include "../kernel/TeFileTheme.h"
00025
00026 #include "../kernel/TeRasterTransform.h"
00027
00028 bool isTerralibModel(TeDatabase* db)
00029 {
00030 if(db == NULL)
00031 {
00032 return false;
00033 }
00034 if(!db->tableExist("te_projection"))
00035 return false;
00036 if(!db->tableExist("te_layer"))
00037 return false;
00038 if(!db->tableExist("te_layer_table"))
00039 return false;
00040 if(!db->tableExist("te_tables_relation"))
00041 return false;
00042 if(!db->tableExist("te_representation"))
00043 return false;
00044 if(!db->tableExist("te_view"))
00045 return false;
00046 if(!db->tableExist("te_theme"))
00047 return false;
00048 if(!db->tableExist("te_grouping"))
00049 return false;
00050 if(!db->tableExist("te_theme_table"))
00051 return false;
00052 if(!db->tableExist("te_legend"))
00053 return false;
00054 if(!db->tableExist("te_visual"))
00055 return false;
00056 if(!db->tableExist("te_visual_raster"))
00057 return false;
00058 if(!db->tableExist("te_database"))
00059 return false;
00060
00061 return true;
00062 }
00063
00064
00065 bool TeCopyDatabase(TeDatabase* dbFrom, TeDatabase* dbTo)
00066 {
00067
00068 TeAttrTableVector externs;
00069 dbFrom->getAttrTables(externs,TeAttrExternal);
00070 if (!externs.empty())
00071 {
00072 TeDatabasePortal* portal = dbFrom->getPortal();
00073 if (!portal)
00074 {
00075
00076 return false;
00077 }
00078
00079 for (unsigned int nt=0; nt < externs.size(); nt++)
00080 {
00081 TeTable table = externs[nt];
00082 table.setId(-1);
00083 string sql = "select * from " + table.name();
00084 if (!portal->query(sql) || portal->numRows() == 0)
00085 {
00086 portal->freeResult();
00087 continue;
00088 }
00089 if (!dbTo->createTable(table.name(),table.attributeList()))
00090 {
00091 portal->freeResult();
00092 continue;
00093 }
00094 unsigned int nr=0;
00095 while (portal->fetchRow())
00096 {
00097 TeTableRow row;
00098 for(int i = 0; i < portal->numFields(); i++)
00099 row.push_back(portal->getData(i));
00100 table.add(row);
00101 nr++;
00102 if (nr % 200)
00103 {
00104 dbTo->insertTable(table);
00105 table.clear();
00106 nr = 0;
00107 }
00108 }
00109 if (table.size() >0)
00110 dbTo->insertTable(table);
00111 table.clear();
00112 dbTo->insertTableInfo(-1,table);
00113 portal->freeResult();
00114 }
00115 delete portal;
00116 }
00117
00118
00119 if(!dbFrom->loadLayerSet())
00120 {
00121
00122 return false;
00123 }
00124
00125 TeLayerMap& fromLayerMap = dbFrom->layerMap();
00126 TeLayerMap::iterator itFrom = fromLayerMap.begin();
00127
00128 while(itFrom != fromLayerMap.end())
00129 {
00130 TeLayer* fromLayer = (*itFrom).second;
00131 TeLayer* toLayer = new TeLayer(fromLayer->name(), dbTo, fromLayer->projection());
00132 map<string, string> tables;
00133 if (!TeCopyLayerToLayer(fromLayer, toLayer, &tables))
00134 {
00135
00136 }
00137 ++itFrom;
00138 }
00139
00140 if(!dbFrom->loadViewSet(dbFrom->user()))
00141 {
00142
00143 return false;
00144 }
00145
00146 if(!dbTo->loadLayerSet())
00147 {
00148
00149 return false;
00150 }
00151
00152 TeViewMap& fromViewMap = dbFrom->viewMap();
00153 TeViewMap::iterator itvFrom = fromViewMap.begin();
00154 while (itvFrom != fromViewMap.end())
00155 {
00156 TeView* fromView = (*itvFrom).second;
00157 TeView* toView = new TeView();
00158
00159 TeProjection* toViewProjection = 0;
00160 if (fromView->projection())
00161 toViewProjection = TeProjectionFactory::make(fromView->projection()->params());
00162
00163 toView->projection(toViewProjection);
00164 toView->name(fromView->name());
00165 toView->user(dbTo->user());
00166 toView->isVisible(fromView->isVisible());
00167
00168 TeBox b;
00169 toView->setCurrentBox(b);
00170 toView->setCurrentTheme(-1);
00171
00172 if (!dbTo->insertView(toView))
00173 {
00174
00175 ++itvFrom;
00176 continue;
00177 }
00178 dbTo->insertView(toView);
00179 if(dbTo->projectMap().empty() == false)
00180 {
00181 TeProjectMap& pm = dbTo->projectMap();
00182 TeProject* project = pm.begin()->second;
00183 project->addView(toView->id());
00184 }
00185 dbTo->insertProjectViewRel(1, toView->id());
00186
00187 TeLayerMap& toLayerMap = dbTo->layerMap();
00188 vector<TeViewNode*>& themeVec = fromView->themes();
00189 for (unsigned int i = 0; i < themeVec.size(); ++i)
00190 {
00191 TeTheme* themeFrom = (TeTheme*) themeVec[i];
00192 string fromLayerName = themeFrom->layer()->name();
00193 TeLayer* toLayer = 0;
00194 TeLayerMap::iterator itTo = toLayerMap.begin();
00195 while(itTo != toLayerMap.end())
00196 {
00197 if(itTo->second->name() == fromLayerName)
00198 {
00199 toLayer = itTo->second;
00200 break;
00201 }
00202 ++itTo;
00203 }
00204
00205 if (!toLayer )
00206 {
00207
00208 continue;
00209 }
00210
00211 TeTheme* themeTo = new TeTheme(themeFrom->name(), toLayer);
00212 toView->add(themeTo);
00213
00214 themeTo->outOfCollectionLegend(themeFrom->outOfCollectionLegend());
00215 themeTo->withoutDataConnectionLegend(themeFrom->withoutDataConnectionLegend ());
00216 themeTo->defaultLegend(themeFrom->defaultLegend());
00217 themeTo->pointingLegend(themeFrom->pointingLegend());
00218 themeTo->queryLegend(themeFrom->queryLegend());
00219
00220 TeAttrTableVector tftablevec = themeFrom->attrTables();
00221 TeAttrTableVector tttablevec;
00222
00223 for (unsigned int nt=0; nt<tftablevec.size(); nt++)
00224 {
00225 TeTable attTable(tftablevec[nt].name());
00226 dbTo->loadTableInfo(attTable);
00227 tttablevec.push_back(attTable);
00228 }
00229 themeTo->setAttTables(tttablevec);
00230 themeTo->attributeRest(themeFrom->attributeRest());
00231 themeTo->temporalRest(themeFrom->temporalRest());
00232 themeTo->spatialRest(themeFrom->spatialRest());
00233 themeTo->visibleRep(themeFrom->visibleRep());
00234 if(!themeTo->save() || !themeTo->buildCollection())
00235 {
00236
00237 continue;
00238 }
00239 themeTo->generateLabelPositions();
00240
00241 if(themeFrom->grouping().groupMode_ != TeNoGrouping)
00242 {
00243 TeGrouping grouping;
00244 grouping = themeFrom->grouping();
00245 themeTo->buildGrouping(grouping);
00246 TeLegendEntryVector& legends = themeFrom->legend();
00247 for (unsigned int nl=0; nl<legends.size(); nl++)
00248 themeTo->setGroupingVisual(nl+1,legends[nl].getVisualMap());
00249 if (!themeTo->saveGrouping())
00250 {
00251
00252 }
00253 }
00254 }
00255 ++itvFrom;
00256 }
00257 return true;
00258 }
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464 map<string, vector<string> > getObject2ItemsMap(TeTheme* theme, vector<string>& itens)
00465 {
00466 map<string, vector<string> > outMap;
00467 if(!theme)
00468 return outMap;
00469
00470 TeDatabase* db = 0;
00471 if(theme->getProductId() == TeEXTERNALTHEME)
00472 db = static_cast<TeExternalTheme*>(theme)->getSourceDatabase();
00473 else if(theme->getProductId() == TeTHEME)
00474 db = static_cast<TeTheme*>(theme)->layer()->database();
00475
00476 if(db == 0)
00477 return outMap;
00478
00479 TeDatabasePortal* portal = db->getPortal();
00480 string C = theme->collectionTable();
00481 string CA = theme->collectionAuxTable();
00482
00483 string query = "SELECT " + C + ".c_object_id, " + CA + ".unique_id FROM " + C + " LEFT JOIN " + CA;
00484 query += " ON " + C + ".c_object_id = " + CA + ".object_id";
00485 query += " WHERE " + CA + ".unique_id IN ";
00486
00487 vector< string >::iterator it_begin = itens.begin();
00488 vector< string >::iterator it_end = itens.end();
00489
00490 vector<string> inVec = generateInClauses(it_begin, it_end, db, false);
00491 vector<string>::iterator it;
00492 for(it=inVec.begin(); it!=inVec.end(); ++it)
00493 {
00494 if((*it).empty() == false)
00495 {
00496 string sel = query + *it;
00497 if (portal->query(sel) == false)
00498 {
00499 delete portal;
00500 return outMap;
00501 }
00502 while (portal->fetchRow())
00503 outMap[portal->getData(0)].push_back(portal->getData(1));
00504 portal->freeResult();
00505 }
00506 }
00507 delete portal;
00508
00509 return outMap;
00510 }
00511
00512 vector<string>
00513 generateItemsInClauseVec(TeTheme* theme, string& where)
00514 {
00515 vector<string> inClauseVector;
00516 TeDatabase* db = 0;
00517 string CT = theme->collectionTable();
00518 string CA = theme->collectionAuxTable();
00519 string from = " FROM " + CT + " LEFT JOIN " + CA + " ON " + CT + ".c_object_id = " + CA + ".object_id";
00520
00521 if(theme->getProductId()==TeTHEME)
00522 db = theme->layer()->database();
00523 else if(theme->getProductId()==TeEXTERNALTHEME)
00524 db = ((TeExternalTheme*) theme)->getSourceDatabase();
00525
00526 if(db == 0)
00527 return inClauseVector;
00528
00529 TeDatabasePortal* portal = db->getPortal();
00530 string sel = "SELECT " + CT + ".c_object_id, " + CA + ".unique_id " + from;
00531
00532 if(where.empty() == false)
00533 sel += " " + where;
00534
00535 map<string, vector<string> > objMap;
00536 if(portal->query(sel))
00537 {
00538 while(portal->fetchRow())
00539 objMap[portal->getData(0)].push_back(portal->getData(1));
00540 }
00541
00542 map<string, vector<string> >::iterator mit;
00543 vector<string>::iterator it;
00544
00545 int i, chunkSize = 200;
00546 string inClause;
00547
00548 i = 0;
00549 bool chunk = true;
00550 for(mit=objMap.begin(); mit!=objMap.end(); ++mit)
00551 {
00552 if (chunk == true)
00553 {
00554 chunk = false;
00555 if (!inClause.empty())
00556 {
00557 inClause[inClause.size() - 1] = ')';
00558 inClauseVector.push_back(inClause);
00559 inClause.clear();
00560 }
00561 inClause = "(";
00562 }
00563 for(it=mit->second.begin(); it!=mit->second.end(); ++it)
00564 {
00565 inClause += *it + ",";
00566 i++;
00567 if (i%chunkSize == 0)
00568 chunk = true;
00569 }
00570 }
00571 if (!inClause.empty())
00572 {
00573 inClause[inClause.size() - 1] = ')';
00574 inClauseVector.push_back(inClause);
00575 }
00576 return inClauseVector;
00577 }
00578
00579 TeViewNode* findNode(const std::set<TeViewNode*>& orphanNodes, const int nodeId)
00580 {
00581 TeViewNode* node = NULL;
00582 std::set<TeViewNode*>::const_iterator it;
00583
00584 for(it = orphanNodes.begin(); it != orphanNodes.end(); ++it)
00585 {
00586 node = ((*it)->id() == nodeId) ? *it :
00587 (*it)->type() == TeTREE ?
00588 ((TeViewTree*)(*it))->find(nodeId) :
00589 NULL;
00590
00591 if(node != NULL)
00592 {
00593 break;
00594 }
00595 }
00596
00597 return node;
00598 }
00599
00600 void updateNodesTrees(std::set<TeViewNode*>& orphanNodes, TeView* view)
00601 {
00602 std::set<TeViewNode*>::iterator it;
00603
00604 while(!orphanNodes.empty())
00605 {
00606 it = orphanNodes.begin();
00607 TeViewNode* node = *it;
00608
00609 TeViewNode* group = findNode(orphanNodes, node->parentId());
00610
00611 if(group == NULL)
00612 {
00613 group = view->root()->find(node->parentId());
00614 }
00615
00616 if(group != NULL)
00617 {
00618 int pri = node->priority();
00619 group->add(node, false);
00620 node->priority(pri);
00621 }
00622
00623 orphanNodes.erase(node);
00624 }
00625 }
00626
00627 bool loadViewSetAndThemeGroups(TeDatabase* db, const std::string& userName)
00628 {
00629 std::vector<TeAbstractTheme*> vecExternalThemes;
00630 std::set<TeViewNode*> orphanNodes;
00631
00632
00633 TeViewMap::iterator it = db->viewMap().begin();
00634 while (it != db->viewMap().end())
00635 {
00636 if(it->second)
00637 delete it->second;
00638 ++it;
00639 }
00640 db->viewMap().clear();
00641
00642
00643 TeThemeMap::iterator itTheme = db->themeMap().begin();
00644 while (itTheme != db->themeMap().end())
00645 {
00646 if(itTheme->second)
00647 delete itTheme->second;
00648 ++itTheme;
00649 }
00650
00651
00652 itTheme = db->invalidThemeMap().begin();
00653 while (itTheme != db->invalidThemeMap().end())
00654 {
00655 if(itTheme->second)
00656 delete itTheme->second;
00657 ++itTheme;
00658 }
00659 db->invalidThemeMap().clear();
00660 db->themeMap().clear();
00661 db->legendMap().clear();
00662
00663
00664 string sql = " SELECT ";
00665 sql += " te_view.*, ";
00666 sql += " te_projection.*, ";
00667 sql += " te_datum.radius, te_datum.flattening, te_datum.dx, te_datum.dy, te_datum.dz, ";
00668 sql += " te_theme.*, ";
00669 sql += " te_grouping.*, ";
00670 sql += " te_legend.*, ";
00671 sql += " te_visual.*, ";
00672 sql += " te_visual_raster.* ";
00673
00674 sql += " FROM (((((((te_view INNER JOIN te_projection ";
00675 sql += " ON te_view.projection_id = te_projection.projection_id) ";
00676 sql += " LEFT JOIN te_theme ON te_view.view_id = te_theme.view_id ) ";
00677 sql += " LEFT JOIN te_grouping ON te_theme.theme_id = te_grouping.theme_id) ";
00678 sql += " LEFT JOIN te_legend ON te_theme.theme_id = te_legend.theme_id) ";
00679 sql += " LEFT JOIN te_visual ON te_visual.legend_id = te_legend.legend_id) ";
00680 sql += " LEFT JOIN te_visual_raster ON te_theme.theme_id = te_visual_raster.theme_id) ";
00681 sql += " LEFT JOIN te_datum ON te_projection.datum = te_datum.name) ";
00682
00683 sql += " WHERE ";
00684 if (!userName.empty())
00685 sql += " te_view.user_name = '" + userName + "'";
00686 else
00687 sql += " te_view.user_name = '" + db->user() + "'";
00688 sql += " ORDER BY te_view.view_id, te_theme.priority, te_theme.theme_id, te_legend.legend_id, ";
00689 sql += " te_legend.group_id, te_visual.geom_type, te_visual_raster.band_in ";
00690
00691 TeDatabasePortal* portal = db->getPortal();
00692 if (!portal)
00693 return false;
00694
00695 if (!portal->query(sql))
00696 {
00697 delete portal;
00698 return false;
00699 }
00700
00701 int lastViewId = -1;
00702 TeView *view = 0;
00703 bool hasNewRow = portal->fetchRow();
00704 while(hasNewRow)
00705 {
00706
00707 if(lastViewId!=atoi(portal->getData(0)))
00708 {
00709
00710 if(view)
00711 {
00712 updateNodesTrees(orphanNodes, view);
00713 db->viewMap()[view->id()] = view;
00714 view = 0;
00715 }
00716 TeProjection* proj = 0;
00717 if(!portal->getProjection(&proj, 10))
00718 {
00719 delete portal;
00720 return false;
00721 }
00722 view = new TeView();
00723 if(!portal->getView(*view, 0))
00724 {
00725 delete portal;
00726 delete view;
00727 return false;
00728 }
00729 if (proj != 0)
00730 view->projection(proj);
00731 lastViewId = view->id();
00732 }
00733
00734
00735 string aux = portal->getData(27);
00736 if (aux.empty())
00737 {
00738 hasNewRow = portal->fetchRow();
00739 continue;
00740 }
00741
00742 TeViewNodeType viewNodeType = (TeViewNodeType)portal->getInt(33);
00743
00744 if(viewNodeType == TeTREE)
00745 {
00746 TeViewTree* vTree = new TeViewTree;
00747 vTree->id(portal->getInt(27));
00748 vTree->view(portal->getInt(29));
00749 vTree->name(portal->getData(30));
00750 int parentId = portal->getInt(31);
00751 vTree->priority(portal->getInt(32));
00752
00753 if(vTree->id() == parentId)
00754 {
00755 view->add(vTree, false);
00756 }
00757 else
00758 {
00759 orphanNodes.insert(vTree);
00760 }
00761
00762 vTree->parentId(parentId);
00763
00764 hasNewRow = portal->fetchRow();
00765 continue;
00766 }
00767 else
00768 {
00769 TeViewNode* viewNode = TeViewNodeFactory::make(viewNodeType);
00770
00771 if(!viewNode)
00772 {
00773 int currentThemeId = portal->getInt(27);
00774
00775 while((hasNewRow = portal->fetchRow()) && (portal->getInt(27) == currentThemeId))
00776 ;
00777
00778 continue;
00779 }
00780
00781 if(!portal->getTheme(static_cast<TeAbstractTheme&>(*viewNode), 27))
00782 {
00783 delete viewNode;
00784 delete portal;
00785 return false;
00786 }
00787
00788 if(viewNodeType == TeTHEME)
00789 {
00790
00791 int id = static_cast<TeTheme*>(viewNode)->layerId();
00792 TeLayerMap::iterator it = db->layerMap().find(id);
00793 if (it == db->layerMap().end())
00794 db->loadLayerSet();
00795
00796 static_cast<TeTheme*>(viewNode)->layer(db->layerMap()[id]);
00797 }
00798
00799 TeAbstractTheme* theme = static_cast<TeAbstractTheme*>(viewNode);
00800
00801
00802 TeGrouping group;
00803 if(portal->getGrouping(group, 47))
00804 theme->grouping(group);
00805
00806
00807
00808 bool hasLegsToThisTheme = true;
00809 while(hasLegsToThisTheme)
00810 {
00811
00812 TeLegendEntry legend;
00813 if(!portal->getLegend(legend, 57))
00814 {
00815 delete theme;
00816 delete view;
00817 delete portal;
00818 return false;
00819 }
00820
00821
00822 TeRasterVisual* rasterVisual = theme->rasterVisual();
00823 if(rasterVisual == NULL)
00824 {
00825 rasterVisual = new TeRasterVisual();
00826 }
00827 bool hasVisualToThisLeg = true;
00828 bool hasRasterVisual = false;
00829 while(hasVisualToThisLeg)
00830 {
00831 TeVisual* visual = TeVisualFactory::make("tevisual");
00832 TeGeomRep geomRep;
00833 if(portal->getVisual(visual, geomRep, 64))
00834 legend.setVisual(visual, geomRep);
00835
00836 if(rasterVisual != NULL && portal->getRasterVisual(*rasterVisual, 88))
00837 hasRasterVisual=true;
00838
00839 hasNewRow = portal->fetchRow();
00840 if(!hasNewRow || portal->getInt(59)!= legend.group() || portal->getInt(57)!= legend.id())
00841 hasVisualToThisLeg = false;
00842 }
00843
00844
00845 if(hasRasterVisual)
00846 theme->rasterVisual(rasterVisual);
00847 else
00848 delete rasterVisual;
00849
00850
00851 theme->legend(legend);
00852
00853
00854 if(legend.group() == -6)
00855 db->legendMap()[legend.id()] = &theme->queryAndPointingLegend();
00856 else if(legend.group() == -5)
00857 db->legendMap()[legend.id()] = &theme->queryLegend();
00858 else if (legend.group() == -4)
00859 db->legendMap()[legend.id()] = &theme->pointingLegend();
00860 else if (legend.group() == -3)
00861 db->legendMap()[legend.id()] = &theme->defaultLegend();
00862 else if (legend.group() == -2)
00863 db->legendMap()[legend.id()] = &theme->withoutDataConnectionLegend();
00864 else if (legend.group() == -1)
00865 db->legendMap()[legend.id()] = &theme->outOfCollectionLegend();
00866 else if (legend.group() == -10)
00867 {
00868 TeLegendEntry* legendTemp = new TeLegendEntry(legend);
00869 db->legendMap()[legend.id()] = legendTemp;
00870 }
00871
00872 if(!hasNewRow || portal->getInt(27)!= theme->id())
00873 hasLegsToThisTheme = false;
00874 }
00875
00876 for (unsigned int i = 0; i < theme->legend().size(); ++i)
00877 db->legendMap()[theme->legend()[i].id()] = &theme->legend()[i];
00878
00879 if(viewNode->type()==(int)TeTHEME)
00880 {
00881
00882 if(!db->loadThemeTable(static_cast<TeTheme*>(theme)))
00883 {
00884 delete portal;
00885 return false;
00886 }
00887 }
00888
00889 if(viewNode->type() != (int)TeEXTERNALTHEME)
00890 {
00891
00892 if(!theme->loadMetadata(db))
00893 {
00894 db->invalidThemeMap()[viewNode->id()] = theme;
00895 continue;
00896 }
00897 db->themeMap()[viewNode->id()] = theme;
00898 }
00899 else
00900 {
00901 vecExternalThemes.push_back(theme);
00902 }
00903
00904 int pri = theme->priority();
00905
00906
00907 if(theme->parentId() != 0 && theme->id() != theme->parentId())
00908 {
00909 orphanNodes.insert(theme);
00910 }
00911 else
00912 {
00913 view->add(theme, false);
00914 }
00915
00916 theme->priority(pri);
00917 }
00918 }
00919
00920
00921 if(view)
00922 {
00923 updateNodesTrees(orphanNodes, view);
00924 db->viewMap()[view->id()] = view;
00925 view = 0;
00926 }
00927
00928 if(!vecExternalThemes.empty())
00929 {
00930 if(!db->loadExternalThemes(vecExternalThemes))
00931 {
00932
00933 for(unsigned int i = 0; i < vecExternalThemes.size(); ++i)
00934 {
00935 TeView* view = db->viewMap()[vecExternalThemes[i]->view()];
00936
00937 db->invalidThemeMap()[vecExternalThemes[i]->id()] = vecExternalThemes[i];
00938 view->remove(vecExternalThemes[i]->id());
00939 }
00940 return true;
00941 }
00942
00943 TeViewMap::iterator itView = db->viewMap().begin();
00944 while(itView != db->viewMap().end())
00945 {
00946 unsigned int i = 0;
00947 while(i < itView->second->themes().size())
00948 {
00949 unsigned int id = itView->second->themes()[i]->id();
00950 if(db->invalidThemeMap().find(id) != db->invalidThemeMap().end())
00951 itView->second->remove(id);
00952 else
00953 ++i;
00954 }
00955 ++itView;
00956 }
00957 }
00958
00959 delete portal;
00960 return true;
00961 }
00962
00963 bool TeCopyView(TeView* view, TeDatabase* inputDatabase, const std::string& newViewName, const std::string& newViewUser, const std::set<int>& setThemeIds, TeDatabase* outputDatabase, std::vector<bool>& vecResult)
00964 {
00965 TeDatabase* dbIn = inputDatabase;
00966 TeDatabase* dbOut = outputDatabase;
00967 if(dbOut == NULL)
00968 {
00969 dbOut = dbIn;
00970 }
00971
00972
00973
00974
00975
00976
00977 TeProjection* proj = view->projection();
00978 TeProjection* newProjection = TeProjectionFactory::make(proj->params());
00979
00980 TeView* newView = new TeView(newViewName, newViewUser);
00981 newView->setCurrentBox(view->getCurrentBox());
00982 newView->projection(newProjection);
00983
00984
00985
00986
00987
00988
00989
00990
00991 if(!dbOut->insertView(newView))
00992 {
00993 delete newView;
00994 return false;
00995 }
00996
00997 for(unsigned int th = 0; th < view->size(); ++th)
00998 {
00999 TeViewNode* node = view->get(th);
01000
01001 if(!setThemeIds.empty())
01002 {
01003 if(setThemeIds.find(node->id()) == setThemeIds.end())
01004 {
01005 continue;
01006 }
01007 }
01008
01009 TeViewNode* viewNode = TeCopyViewNode(node, dbIn, dbOut, newView);
01010 bool result = false;
01011 if(viewNode != NULL)
01012 {
01013 result = true;
01014 }
01015 vecResult.push_back(result);
01016 }
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036 return true;
01037 }
01038
01039 TeViewNode* TeCopyViewNode(TeViewNode* node, TeDatabase* inputDatabase, TeDatabase* outputDatabase, TeView* newView)
01040 {
01041 if(node == NULL)
01042 {
01043 return NULL;
01044 }
01045
01046 if(!outputDatabase->beginTransaction())
01047 {
01048 return NULL;
01049 }
01050
01051 TeViewNode* newViewNode = NULL;
01052 if (node->type() == TeTREE)
01053 {
01054 TeViewTree* tree = dynamic_cast<TeViewTree*>(node);
01055 if(tree == NULL)
01056 {
01057 outputDatabase->rollbackTransaction();
01058 return NULL;
01059 }
01060
01061 TeViewTree* newTree = new TeViewTree(tree->viewNodeParams());
01062 newTree->id(0);
01063
01064 if(!outputDatabase->insertViewTree(newTree))
01065 {
01066 outputDatabase->rollbackTransaction();
01067 delete newTree;
01068 return NULL;
01069 }
01070
01071 std::vector<TeViewNode*>& vecNodes = tree->nodes();
01072 for(unsigned int i = 0; i < vecNodes.size(); ++i)
01073 {
01074 int parentId = vecNodes[i]->parentId();
01075 vecNodes[i]->parentId(newTree->id());
01076
01077 TeViewNode* newChild = TeCopyViewNode(vecNodes[i], inputDatabase, outputDatabase, newView);
01078
01079 vecNodes[i]->parentId(parentId);
01080
01081 if(newChild == NULL)
01082 {
01083 outputDatabase->rollbackTransaction();
01084 delete newTree;
01085 return NULL;
01086 }
01087 newTree->add(newChild);
01088 }
01089
01090 newViewNode = newTree;
01091 }
01092 else
01093 {
01094 TeAbstractTheme* absTheme = dynamic_cast<TeAbstractTheme*>(node);
01095
01096 TeAbstractTheme* newAbsTheme = absTheme->copyTo(outputDatabase, newView);
01097 if(newAbsTheme == 0)
01098 {
01099 outputDatabase->rollbackTransaction();
01100 return NULL;
01101 }
01102
01103 newViewNode = newAbsTheme;
01104
01105 if(newAbsTheme->type() == TeTHEME)
01106 {
01107 TeTheme* theme = dynamic_cast<TeTheme*>(node);
01108 TeTheme* newTheme = dynamic_cast<TeTheme*>(newAbsTheme);
01109 if(newTheme != NULL)
01110 {
01111 TeAttrTableVector tableVectorInExternal;
01112 inputDatabase->getAttrTables(tableVectorInExternal, TeAttrExternal);
01113
01114 for(unsigned int i = 0; i < tableVectorInExternal.size(); ++i)
01115 {
01116 if(!outputDatabase->tableExist(tableVectorInExternal[i].name()))
01117 {
01118 if(!outputDatabase->createTable(tableVectorInExternal[i].name(), tableVectorInExternal[i].attributeList()))
01119 {
01120 outputDatabase->rollbackTransaction();
01121 delete newViewNode;
01122 return NULL;
01123 }
01124 TeTable& table = tableVectorInExternal[i];
01125 table.setId(-1);
01126 if(!outputDatabase->insertTableInfo(-1, table))
01127 {
01128 outputDatabase->rollbackTransaction();
01129 delete newViewNode;
01130 return NULL;
01131 }
01132
01133 if(!TeCopyTable(inputDatabase, tableVectorInExternal[i].name(), outputDatabase, tableVectorInExternal[i].name()))
01134 {
01135 outputDatabase->rollbackTransaction();
01136 delete newViewNode;
01137 return NULL;
01138 }
01139 }
01140 }
01141
01142 TeAttrTableVector& tableVectorIn = theme->attrTables();
01143 TeAttrTableVector tableVectorOut;
01144
01145 for(unsigned int i = 0; i < tableVectorIn.size(); ++i)
01146 {
01147 TeTable attTable(tableVectorIn[i].name());
01148 outputDatabase->loadTableInfo(attTable);
01149 if(tableVectorIn[i].tableType() == TeAttrExternal)
01150 {
01151
01152 std::string relatedTableName = tableVectorIn[i].relatedTableName();
01153 TeTable relatedTable(relatedTableName);
01154 outputDatabase->loadTableInfo(relatedTable);
01155
01156 attTable.setTableType(TeAttrExternal, relatedTable.id(), tableVectorIn[i].relatedAttribute());
01157 attTable.relatedTableName(relatedTableName);
01158 attTable.setLinkName(tableVectorIn[i].linkName());
01159 }
01160 tableVectorOut.push_back(attTable);
01161 }
01162 newTheme->setAttTables(tableVectorOut);
01163 }
01164 }
01165 }
01166
01167 if(newViewNode != NULL)
01168 {
01169 if(!outputDatabase->commitTransaction())
01170 {
01171 outputDatabase->rollbackTransaction();
01172 }
01173 }
01174 else
01175 {
01176 outputDatabase->rollbackTransaction();
01177 }
01178
01179 return newViewNode;
01180 }
01181
01182 bool TeCopyTable(TeDatabase* inputDatabase, const std::string& inputTable, TeDatabase* outputDatabase, const std::string& outputTable)
01183 {
01184 if(!inputDatabase->tableExist(inputTable) || !outputDatabase->tableExist(outputTable))
01185 {
01186 return false;
01187 }
01188
01189 TeDatabasePortal* portal = inputDatabase->getPortal();
01190 std::string sql = "SELECT * FROM " + inputTable;
01191 if(!portal->query(sql))
01192 {
01193 return false;
01194 }
01195
01196 TeAttributeList& attrList = portal->getAttributeList();
01197
01198 unsigned int numColumns = attrList.size();
01199
01200 TeTable table(outputTable);
01201 table.setAttributeList(attrList);
01202
01203 while(portal->fetchRow())
01204 {
01205 TeTableRow row;
01206 for(unsigned int i = 0; i < numColumns; ++i)
01207 {
01208 row.push_back(portal->getData(i));
01209 }
01210 table.add(row);
01211
01212 if(table.size() % 100 == 0)
01213 {
01214 if(!outputDatabase->insertTable(table))
01215 {
01216 delete portal;
01217 return false;
01218 }
01219 table.clear();
01220 }
01221 }
01222
01223 if(table.size() > 0)
01224 {
01225 if(!outputDatabase->insertTable(table))
01226 {
01227 delete portal;
01228 return false;
01229 }
01230 }
01231 table.clear();
01232
01233 delete portal;
01234
01235 return true;
01236 }
01237
01238