Utilitary functions


Detailed Description


Functions

template<typename Iterator>
vector< stringgenerateItemsInClauses (Iterator &begin, Iterator &end, TeTheme *theme)
TLUTILS_DLL vector
< string
generateItemsInClauseVec (TeTheme *theme, string &where)
TLUTILS_DLL map
< string, vector
< string > > 
getObject2ItemsMap (TeTheme *theme, vector< string > &itens)
TLUTILS_DLL bool isTerralibModel (TeDatabase *db)
 Returns true whether a database contains the TerraLib data model.
TLUTILS_DLL bool loadViewSetAndThemeGroups (TeDatabase *db, const std::string &userName)
TLUTILS_DLL bool TeCopyDatabase (TeDatabase *dbFrom, TeDatabase *dbTo)
 This function copies the contents of a TerraLib database to another TerraLib database.
TLUTILS_DLL bool TeCopyTable (TeDatabase *inputDatabase, const std::string &inputTable, TeDatabase *outputDatabase, const std::string &outputTable)
TLUTILS_DLL 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)
TLUTILS_DLL TeViewNodeTeCopyViewNode (TeViewNode *node, TeDatabase *inputDatabase, TeDatabase *outputDatabase, TeView *newView)


Function Documentation

template<typename Iterator>
vector<string> generateItemsInClauses ( Iterator &  begin,
Iterator &  end,
TeTheme theme 
) [inline]

Definition at line 64 of file TeDatabaseUtils.h.

References getObject2ItemsMap().

Referenced by plotPieBars().

00065 {
00066         vector<string> itenVec;
00067         Iterator temp = begin;
00068         while (temp != end)
00069         {
00070                 itenVec.push_back(*temp);
00071                 temp++;
00072         }
00073 
00074         map<string, vector<string> > objMap = getObject2ItemsMap(theme, itenVec);
00075         map<string, vector<string> >::iterator mit;
00076         vector<string>::iterator it;
00077 
00078         int i, chunkSize = 200;
00079         string inClause;
00080         vector<string> inClauseVector;
00081         
00082         i = 0;
00083         bool chunk = true;
00084         for(mit=objMap.begin(); mit!=objMap.end(); ++mit)
00085         {
00086                 if (chunk == true)
00087                 {
00088                         chunk = false;
00089                         if (!inClause.empty())
00090                         {
00091                                 inClause[inClause.size() - 1] = ')';
00092                                 inClauseVector.push_back(inClause);
00093                                 inClause.clear();
00094                         }
00095                         inClause = "(";
00096                 }
00097                 for(it=mit->second.begin(); it!=mit->second.end(); ++it)
00098                 {
00099                         inClause += *it + ",";
00100                         i++;
00101                         if (i%chunkSize == 0)
00102                                 chunk = true;
00103                 }
00104         }
00105         if (!inClause.empty())
00106         {
00107                 inClause[inClause.size() - 1] = ')';
00108                 inClauseVector.push_back(inClause);
00109         }
00110         return inClauseVector;
00111 }

TLUTILS_DLL vector<string> generateItemsInClauseVec ( TeTheme theme,
string where 
)

Definition at line 513 of file TeDatabaseUtils.cpp.

References TeTheme::collectionAuxTable(), TeTheme::collectionTable(), db, TeDatabasePortal::fetchRow(), TeDatabasePortal::getData(), TeDatabase::getPortal(), TeViewNode::getProductId(), TeTheme::layer(), TeDatabasePortal::query(), TeEXTERNALTHEME, and TeTHEME.

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 }

TLUTILS_DLL map<string, vector<string> > getObject2ItemsMap ( TeTheme theme,
vector< string > &  itens 
)

Definition at line 464 of file TeDatabaseUtils.cpp.

References TeTheme::collectionAuxTable(), TeTheme::collectionTable(), db, TeDatabasePortal::fetchRow(), TeDatabasePortal::freeResult(), generateInClauses(), TeDatabasePortal::getData(), TeDatabase::getPortal(), TeViewNode::getProductId(), TeDatabasePortal::query(), TeEXTERNALTHEME, and TeTHEME.

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 }

TLUTILS_DLL bool isTerralibModel ( TeDatabase db  ) 

Parameters:
db pointer do a TerraLib database

Definition at line 28 of file TeDatabaseUtils.cpp.

References NULL, and TeDatabase::tableExist().

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 }

TLUTILS_DLL bool loadViewSetAndThemeGroups ( TeDatabase db,
const std::string userName 
)

Definition at line 627 of file TeDatabaseUtils.cpp.

References TeView::add(), aux, TeAbstractTheme::defaultLegend(), TeDatabasePortal::fetchRow(), TeDatabasePortal::getData(), TeDatabasePortal::getGrouping(), TeDatabasePortal::getInt(), TeDatabasePortal::getLegend(), TeDatabase::getPortal(), TeDatabasePortal::getProjection(), TeDatabasePortal::getRasterVisual(), TeDatabasePortal::getTheme(), TeDatabasePortal::getView(), TeDatabasePortal::getVisual(), TeLegendEntry::group(), TeAbstractTheme::grouping(), TeLegendEntry::id(), TeViewNode::id(), TeView::id(), TeDatabase::invalidThemeMap(), TeDatabase::layerMap(), TeAbstractTheme::legend(), TeDatabase::legendMap(), TeDatabase::loadExternalThemes(), TeDatabase::loadLayerSet(), TeAbstractTheme::loadMetadata(), TeDatabase::loadThemeTable(), TeAbstractFactory< PRODUCT, PARAMS, PRODUCTIDTYPE >::make(), TeAbstractFactory< TeViewNode, TeViewNodeParams, int >::make(), TeViewNode::name(), NULL, TeAbstractTheme::outOfCollectionLegend(), TeViewNode::parentId(), TeAbstractTheme::pointingLegend(), TeViewNode::priority(), TeView::projection(), TeDatabasePortal::query(), TeAbstractTheme::queryAndPointingLegend(), TeAbstractTheme::queryLegend(), TeAbstractTheme::rasterVisual(), TeView::remove(), TeLegendEntry::setVisual(), TeEXTERNALTHEME, TeTHEME, TeTREE, TeDatabase::themeMap(), TeViewNode::type(), updateNodesTrees(), TeDatabase::user(), TeViewNode::view(), TeDatabase::viewMap(), and TeAbstractTheme::withoutDataConnectionLegend().

00628 {
00629         std::vector<TeAbstractTheme*> vecExternalThemes;
00630         std::set<TeViewNode*> orphanNodes;
00631 
00632         //clear view map
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         //clear theme map
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         //clear invalid theme map
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         //load view, projection, themes and grouping
00664         string sql = " SELECT ";
00665         sql += " te_view.*, "; //0-9  (10 columns)
00666         sql += " te_projection.*, "; //10 - 21 (12 columns)
00667         sql += " te_datum.radius, te_datum.flattening, te_datum.dx, te_datum.dy, te_datum.dz, "; //22 - 26 (5 columns)
00668         sql += " te_theme.*, "; // 27 - 46 (20 columns)
00669         sql += " te_grouping.*, "; //47 - 56 (10 columns)
00670         sql += " te_legend.*,  "; // 57 - 63    (7 columns)
00671         sql += " te_visual.*, "; //  64 - 87 (24 columns)
00672         sql += " te_visual_raster.* "; //  88 (7 columns)
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(); //idicates that this portal was fetched 
00704         while(hasNewRow)
00705         {
00706                 //load view and its projection 
00707                 if(lastViewId!=atoi(portal->getData(0)))
00708                 {
00709                         //store the last view
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)) //load projection
00718                         {
00719                                 delete portal;
00720                                 return false;
00721                         }
00722                         view = new TeView();
00723                         if(!portal->getView(*view, 0)) //load view
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                 //make the rigth object
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                                 //load layer
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                         //load grouping 
00802                         TeGrouping group;
00803                         if(portal->getGrouping(group, 47))
00804                                 theme->grouping(group);
00805 
00806                         //load all legends of this theme
00807                         //and its visual
00808                         bool hasLegsToThisTheme = true;
00809                         while(hasLegsToThisTheme)
00810                         {
00811                                 //legend
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                                 //visual
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                                 //Set raster visual to this theme
00845                                 if(hasRasterVisual)
00846                                         theme->rasterVisual(rasterVisual);
00847                                 else
00848                                         delete rasterVisual;
00849 
00850                                 //Set legend to this theme
00851                                 theme->legend(legend); 
00852                                                                                                                                 
00853                                 //fill legend buffer
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) //own legend
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                                 //load theme table
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                                 //load specific theme parameters
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                         //verifica se o tema pertence a algum agrupamento.
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         //store the last view
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                         //todos se tornam invalidos
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 }

TLUTILS_DLL bool TeCopyDatabase ( TeDatabase dbFrom,
TeDatabase dbTo 
)

Parameters:
dbFrom A pointer to the source database
dbTo A pointer to the destination database

Definition at line 65 of file TeDatabaseUtils.cpp.

References TeView::add(), TeTable::add(), TeProject::addView(), TeTable::attributeList(), TeAbstractTheme::attributeRest(), TeTheme::attrTables(), TeTheme::buildCollection(), TeTheme::buildGrouping(), TeTable::clear(), TeDatabase::createTable(), TeAbstractTheme::defaultLegend(), TeDatabasePortal::fetchRow(), TeDatabasePortal::freeResult(), TeTheme::generateLabelPositions(), TeDatabase::getAttrTables(), TeDatabasePortal::getData(), TeDatabase::getPortal(), TeAbstractTheme::grouping(), TeGrouping::groupMode_, TeView::id(), TeDatabase::insertProjectViewRel(), TeDatabase::insertTable(), TeDatabase::insertTableInfo(), TeDatabase::insertView(), TeView::isVisible(), TeTheme::layer(), TeDatabase::layerMap(), TeAbstractTheme::legend(), TeDatabase::loadLayerSet(), TeDatabase::loadTableInfo(), TeDatabase::loadViewSet(), TeProjectionFactory::make(), TeViewNode::name(), TeView::name(), TeLayer::name(), TeTable::name(), TeDatabasePortal::numFields(), TeDatabasePortal::numRows(), TeAbstractTheme::outOfCollectionLegend(), TeProjection::params(), TeAbstractTheme::pointingLegend(), TeView::projection(), TeLayer::projection(), TeDatabase::projectMap(), TeDatabasePortal::query(), TeAbstractTheme::queryLegend(), TeTheme::save(), TeTheme::saveGrouping(), TeTheme::setAttTables(), TeView::setCurrentBox(), TeView::setCurrentTheme(), TeAbstractTheme::setGroupingVisual(), TeTable::setId(), TeTable::size(), TeAbstractTheme::spatialRest(), TeAttrExternal, TeCopyLayerToLayer(), TeAbstractTheme::temporalRest(), TeNoGrouping, TeView::themes(), TeView::user(), TeDatabase::user(), TeDatabase::viewMap(), TeAbstractTheme::visibleRep(), and TeAbstractTheme::withoutDataConnectionLegend().

00066 {
00067         // -- Step 1 : copies the external tables
00068         TeAttrTableVector externs;
00069         dbFrom->getAttrTables(externs,TeAttrExternal);
00070         if (!externs.empty())
00071         {
00072                 TeDatabasePortal* portal = dbFrom->getPortal();
00073                 if (!portal)
00074                 {
00075                         // log error here
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         // -- Step 2: Copies the layers
00119         if(!dbFrom->loadLayerSet())
00120         {
00121                 // log error here
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                         // log error here: layer couldn�t be copied
00136                 }
00137                 ++itFrom;
00138         }
00139         // -- Set 3: Copies the views and themes
00140         if(!dbFrom->loadViewSet(dbFrom->user()))
00141         {
00142                 // log error here
00143                 return false;
00144         }
00145 
00146         if(!dbTo->loadLayerSet())
00147         {
00148                 // log error here
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                         // log error here
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                                 // log error here
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                                 // log error here
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                                                 // log error here
00252                                 }
00253                         }
00254                 } // end for each theme
00255                 ++itvFrom;
00256         } // end for each view
00257         return true;
00258 }

TLUTILS_DLL bool TeCopyTable ( TeDatabase inputDatabase,
const std::string inputTable,
TeDatabase outputDatabase,
const std::string outputTable 
)

Definition at line 1182 of file TeDatabaseUtils.cpp.

References TeTable::add(), TeTable::clear(), TeDatabasePortal::fetchRow(), TeDatabasePortal::getAttributeList(), TeDatabasePortal::getData(), TeDatabase::getPortal(), TeDatabase::insertTable(), TeDatabasePortal::query(), TeTable::setAttributeList(), TeTable::size(), and TeDatabase::tableExist().

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 }

TLUTILS_DLL 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 
)

Definition at line 963 of file TeDatabaseUtils.cpp.

References TeView::get(), TeView::getCurrentBox(), TeViewNode::id(), TeDatabase::insertView(), TeProjectionFactory::make(), NULL, TeProjection::params(), TeView::projection(), TeView::size(), and TeCopyViewNode().

00964 {
00965         TeDatabase* dbIn = inputDatabase;
00966         TeDatabase* dbOut = outputDatabase;
00967         if(dbOut == NULL)
00968         {
00969                 dbOut = dbIn;
00970         }
00971 
00972         /*if(!dbOut->beginTransaction())
00973         {
00974                 return false;
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         /*if(!dbOut->insertView(newView))
00985         {
00986                 delete newView;
00987                 dbOut->rollbackTransaction();
00988                 return false;
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         /*TeViewMap& viewMap = outputDatabase->viewMap();
01019         TeViewMap::iterator viewIt = viewMap.find(newView->id());
01020         if(viewIt != viewMap.end())
01021         {
01022                 if(viewIt->second)
01023                 {
01024                         delete viewIt->second;
01025                 }
01026         }
01027         viewMap.erase(viewIt);
01028         delete newView;*/
01029 
01030         /*if(!dbOut->commitTransaction())
01031         {
01032                 dbOut->rollbackTransaction();
01033                 return false;
01034         }*/
01035 
01036         return true;
01037 }

TLUTILS_DLL TeViewNode* TeCopyViewNode ( TeViewNode node,
TeDatabase inputDatabase,
TeDatabase outputDatabase,
TeView newView 
)

Definition at line 1039 of file TeDatabaseUtils.cpp.

References TeViewTree::add(), TeTheme::attrTables(), TeDatabase::beginTransaction(), TeDatabase::commitTransaction(), TeAbstractTheme::copyTo(), TeDatabase::createTable(), TeDatabase::getAttrTables(), TeTable::id(), TeViewNode::id(), TeDatabase::insertTableInfo(), TeDatabase::insertViewTree(), TeDatabase::loadTableInfo(), TeViewTree::nodes(), NULL, TeTable::relatedTableName(), TeDatabase::rollbackTransaction(), TeTheme::setAttTables(), TeTable::setId(), TeTable::setLinkName(), TeTable::setTableType(), TeDatabase::tableExist(), TeAttrExternal, TeCopyTable(), TeCopyViewNode(), TeTHEME, TeTREE, TeViewNode::type(), and TeViewNode::viewNodeParams().

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 //realiza a copia do tema
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                                                 // Gets the related table id from output database
01152                                                 std::string relatedTableName = tableVectorIn[i].relatedTableName();
01153                                                 TeTable relatedTable(relatedTableName);
01154                                                 outputDatabase->loadTableInfo(relatedTable);                                                    
01155                                                 // Updates the external table
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 }


Generated on Sun Jul 29 04:04:41 2012 for TerraLib - Development Source by  doxygen 1.5.3