void Migration_20100216124307_rename_columns::renameColumn (const QString &table, const QString &oldName, const QString &newName, const QString &type, Migration::Direction direction)
{
	switch (direction)
	{
		case dirUp:   renameColumn (table, oldName, newName, type); break;
		case dirDown: renameColumn (table, newName, oldName, type); break;
	}
}
bool MysqlRenameColumnService::execute(const Commands::ConstCommandPtr &command,
                                       CommandExecution::CommandExecutionContext &context) const
{
    QSharedPointer<const Commands::RenameColumn> renameColumn(command.staticCast<const Commands::RenameColumn>());
    Q_ASSERT(renameColumn);

    const Structure::Column originalColumn( context.helperRepository().sqlStructureService()
                                            .getTableDefinition(renameColumn->tableName(), context.database())
                                            .fetchColumnByName(renameColumn->oldName()) );
    if (!originalColumn.isValid()) {
        ::qWarning() << "could not find column" << renameColumn->tableName() << renameColumn->oldName();
        return false;
    }

    bool success = execute(*renameColumn, originalColumn, context);

    if (success && context.isUndoUsed()) {
        context.setUndoCommand(renameColumn->reverse());
    }
    return success;
}
void Migration_20100216124307_rename_columns::renameColumns (Direction direction)
{
	// *** People
	// id is not changed
	renameColumn ("people", "nachname"  , "last_name" , dataTypeString (), direction);
	renameColumn ("people", "vorname"   , "first_name", dataTypeString (), direction);
	renameColumn ("people", "verein"    , "club"      , dataTypeString (), direction);
	renameColumn ("people", "spitzname" , "nickname"  , dataTypeString (), direction);
	renameColumn ("people", "vereins_id", "club_id"   , dataTypeString (), direction);
	renameColumn ("people", "bemerkung" , "comments"  , dataTypeString (), direction);

	// *** Planes
	// id is not changed
	renameColumn ("planes", "kennzeichen"           , "registration"              ,  dataTypeString    (), direction);
	renameColumn ("planes", "verein"                , "club"                      ,  dataTypeString    (), direction);
	renameColumn ("planes", "sitze"                 , "num_seats"                 ,  dataTypeInteger   (), direction);
	renameColumn ("planes", "typ"                   , "type"                      ,  dataTypeString    (), direction);
	renameColumn ("planes", "gattung"               , "category"                  ,  dataTypeCharacter (), direction);
	renameColumn ("planes", "wettbewerbskennzeichen", "callsign"                  ,  dataTypeString    (), direction);
	renameColumn ("planes", "bemerkung"             , "comments"                  ,  dataTypeString    (), direction);

	// *** Flights
	// id is not changed
	renameColumn ("flights", "flugzeug"             , "plane_id"                  ,  dataTypeId        (), direction);
	renameColumn ("flights", "pilot"                , "pilot_id"                  ,  dataTypeId        (), direction);
	renameColumn ("flights", "begleiter"            , "copilot_id"                ,  dataTypeId        (), direction);
	// Flight settings
	renameColumn ("flights", "typ"                  , "type"                      ,  dataTypeInteger   (), direction);
	renameColumn ("flights", "modus"                , "mode"                      ,  dataTypeCharacter (), direction);
	// Departure and landing
	renameColumn ("flights", "status"               , "status"                    ,  dataTypeInteger   (), direction);
	renameColumn ("flights", "startart"             , "launch_method_id"          ,  dataTypeId        (), direction);
	renameColumn ("flights", "startort"             , "departure_location"        ,  dataTypeString    (), direction);
	renameColumn ("flights", "zielort"              , "landing_location"          ,  dataTypeString    (), direction);
	renameColumn ("flights", "anzahl_landungen"     , "num_landings"              ,  dataTypeInteger   (), direction);
	renameColumn ("flights", "startzeit"            , "departure_time"            ,  dataTypeDatetime  (), direction);
	renameColumn ("flights", "landezeit"            , "landing_time"              ,  dataTypeDatetime  (), direction);
	// Towflight
	renameColumn ("flights", "towplane"             , "towplane_id"               ,  dataTypeId        (), direction);
	renameColumn ("flights", "modus_sfz"            , "towflight_mode"            ,  dataTypeCharacter (), direction);
	renameColumn ("flights", "zielort_sfz"          , "towflight_landing_location",  dataTypeString    (), direction);
	renameColumn ("flights", "land_schlepp"         , "towflight_landing_time"    ,  dataTypeDatetime  (), direction);
	renameColumn ("flights", "towpilot"             , "towpilot_id"               ,  dataTypeId        (), direction);
	// Incomplete names
	renameColumn ("flights", "pnn"                  , "pilot_last_name"           ,  dataTypeString    (), direction);
	renameColumn ("flights", "pvn"                  , "pilot_first_name"          ,  dataTypeString    (), direction);
	renameColumn ("flights", "bnn"                  , "copilot_last_name"         ,  dataTypeString    (), direction);
	renameColumn ("flights", "bvn"                  , "copilot_first_name"        ,  dataTypeString    (), direction);
	renameColumn ("flights", "tpnn"                 , "towpilot_last_name"        ,  dataTypeString    (), direction);
	renameColumn ("flights", "tpvn"                 , "towpilot_first_name"       ,  dataTypeString    (), direction);
	// Comments
	renameColumn ("flights", "bemerkung"            , "comments"                  ,  dataTypeString    (), direction);
	renameColumn ("flights", "abrechnungshinweis"   , "accounting_notes"          ,  dataTypeString    (), direction);

	// *** Users
	// username, password, perm_club_admin, perm_read_flight_db and club are not changed
	renameColumn ("users", "person"                 , "person_id"                 ,  dataTypeId        (), direction);
}
void PlotDataSelectWidget::tableContextMenu(const QPoint& pos) {
    PlotDataSelect* plotDataSelect = dynamic_cast<PlotDataSelect*>(processor_);
    QModelIndex index = table_->indexAt(pos);
    std::vector<int> selectedColumns = table_->selectedColumns();
    std::vector<int> selectedRows = table_->selectedRows();
    std::pair<QPoint,QPoint> selectedArea = table_->selectedArea();
    contextMenuTable_->clear();
    PlotData* data = const_cast<PlotData*>(plotDataSelect->getPlotData());
    QAction* newAct;
    QList<QVariant>* qlist = new QList<QVariant>();
    std::stringstream str;
    std::string menuString;
    // create popupmenu
    if (function_ == FunctionLibrary::NONE) {
        str.str("");
        str.clear();
        str << "Select";
        newAct = new QAction(QString::fromStdString(str.str()),this);
        newAct->setData(QVariant(static_cast<int>(FunctionLibrary::SELECT)));
        contextMenuTable_->addAction(newAct);
        QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(functionSelect()));

        menuString = "Column Order";
        newAct = new QAction(QString::fromStdString(menuString),this);
        newAct->setData(QVariant(static_cast<int>(FunctionLibrary::COLUMNORDER)));
        contextMenuTable_->addAction(newAct);
        QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(functionSelect()));

        menuString = "Filter";
        newAct = new QAction(QString::fromStdString(menuString),this);
        newAct->setData(QVariant(static_cast<int>(FunctionLibrary::FILTER)));
        contextMenuTable_->addAction(newAct);
        QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(functionSelect()));

        contextMenuTable_->popup(table_->mapToGlobal(pos));
    }
    else if (function_ != FunctionLibrary::NONE) {

        menuString = "Deactivate Function";
        newAct = new QAction(QString::fromStdString(menuString),this);
        newAct->setData(QVariant(0));
        contextMenuTable_->addAction(newAct);
        QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(functionSelect()));

        if (function_ == FunctionLibrary::SELECT || function_ == FunctionLibrary::COLUMNORDER) {
            contextMenuTable_->addSeparator();

            str.str("");
            str.clear();
            if (plotDataSelect->getResetListSize() > 0) {
                str << plotDataSelect->getResetListSize();
                menuString = "Reset All (" + str.str() + " Actions)";
            }
            else
                menuString = "Reset All";
            newAct = new QAction(QString::fromStdString(menuString),this);
            if (plotDataSelect->getResetListSize() == 0)
                newAct->setEnabled(false);
            contextMenuTable_->addAction(newAct);
            QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(selectResetAll()));

            menuString = "Reset Last";
            newAct = new QAction(QString::fromStdString(menuString),this);
            if (plotDataSelect->getResetListSize() == 0)
                newAct->setEnabled(false);
            contextMenuTable_->addAction(newAct);
            QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(selectResetLast()));

            contextMenuTable_->addSeparator();

            if (function_ == FunctionLibrary::SELECT) {
                if (index.column() >= 0) {
                    menuString = "Disable \""+data->getColumnLabel(index.column())+"\" Column";
                }
                else {
                    menuString = "Selecting not Well-Defined";
                }
                newAct = new QAction(QString::fromStdString(menuString),this);
                if (index.column() >= 0) {
                    newAct->setData(QVariant(index.column()));
                }
                else {
                    newAct->setEnabled(false);
                }
                contextMenuTable_->addAction(newAct);
                QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(selectChange()));

                // only show this part of the Menu, if something is selected
                if (selectedColumns.size() > 0 || selectedRows.size() > 0) {
                    if (selectedColumns.size() > 1) {
                        qlist->clear();
                        str.str("");
                        str.clear();
                        str << selectedColumns.size();
                        menuString = "Disable Selected Columns ("+str.str()+")";
                        newAct = new QAction(QString::fromStdString(menuString),this);
                        for (size_t j = 0; j < selectedColumns.size(); ++j) {
                            qlist->push_back(QVariant(selectedColumns.at(j)));
                        }
                        newAct->setData(QVariant(*qlist));
                        contextMenuTable_->addAction(newAct);
                        QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(selectChange()));
                    }
                    qlist->clear();
                    str.str("");
                    str.clear();
                    str << selectedColumns.size();
                    menuString = "Select Columns ("+str.str()+")";
                    newAct = new QAction(QString::fromStdString(menuString),this);
                    for (size_t j = 0; j < selectedColumns.size(); ++j) {
                        qlist->push_back(QVariant(selectedColumns.at(j)));
                    }
                    newAct->setData(QVariant(*qlist));
                    contextMenuTable_->addAction(newAct);
                    QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(selectColumnChange()));

                    qlist->clear();
                    str.str("");
                    str.clear();
                    str << selectedRows.size();
                    menuString = "Select Rows ("+str.str()+")";
                    newAct = new QAction(QString::fromStdString(menuString),this);
                    for (size_t j = 0; j < selectedRows.size(); ++j) {
                        qlist->push_back(QVariant(selectedRows.at(j)-proxyModel_->getAddLines()));
                    }
                    newAct->setData(QVariant(*qlist));
                    contextMenuTable_->addAction(newAct);
                    QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(selectRowChange()));

                    qlist->clear();
                    str.str("");
                    str.clear();
                    str << selectedRows.size();
                    menuString = "Select Columns && Rows ("+str.str()+")";
                    newAct = new QAction(QString::fromStdString(menuString),this);
                    QList<QVariant>* qlist1 = new QList<QVariant>();
                    QList<QVariant>* qlist2 = new QList<QVariant>();
                    for (size_t j = 0; j < selectedColumns.size(); ++j) {
                        qlist1->push_back(QVariant(selectedColumns.at(j)));
                    }
                    for (size_t j = 0; j < selectedRows.size(); ++j) {
                        qlist2->push_back(QVariant(selectedRows.at(j)-proxyModel_->getAddLines()));
                    }
                    qlist->push_back(QVariant(*qlist1));
                    qlist->push_back(QVariant(*qlist2));
                    newAct->setData(QVariant(*qlist));
                    contextMenuTable_->addAction(newAct);
                    QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(selectColumnRowChange()));

                    qlist->clear();
                    str.str("");
                    str.clear();
                    str << selectedArea.second.x()- selectedArea.first.x() + 1  << "x" << selectedArea.second.y() - selectedArea.first.y() + 1;
                    menuString = "Select Area ("+str.str()+")";
                    newAct = new QAction(QString::fromStdString(menuString),this);
                    qlist->push_back(QVariant(QPoint(selectedArea.first.x(),selectedArea.first.y()-proxyModel_->getAddLines())));
                    qlist->push_back(QVariant(QPoint(selectedArea.second.x(),selectedArea.second.y()-proxyModel_->getAddLines())));
                    newAct->setData(QVariant(*qlist));
                    contextMenuTable_->addAction(newAct);
                    QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(selectAreaChange()));

                }

                contextMenuTable_->addSeparator();

                if (index.column() >= 0) {
                    menuString = "Rename \""+data->getColumnLabel(index.column())+"\"";
                    newAct = new QAction(QString::fromStdString(menuString),this);
                    newAct->setData(QVariant(index.column()));
                    contextMenuTable_->addAction(newAct);
                    QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(renameColumn()));
                }

                const std::vector<Property*>& properties = plotDataSelect->getProperties();
                PlotPredicateProperty* pPVProperty = NULL;
                for (size_t i = 0; i < properties.size(); ++i) {
                    if (dynamic_cast<PlotPredicateProperty*>(properties[i])) {
                        pPVProperty = dynamic_cast<PlotPredicateProperty*>(properties[i]);
                        break;
                    }
                }
                menuString = "Set ";
                if (index.column() >= 0) {
                    for (size_t i = 0; i < pPVProperty->get().size(); ++i) {
                        if (index.column() == pPVProperty->get().at(i).first) {
                            menuString = "Change ";
                        }
                    }
                }
                menuString += "Predicate";
                newAct = new QAction(QString::fromStdString(menuString),this);
                if (index.column() >= 0) {
                    newAct->setData(QVariant(index.column()));
                }
                else {
                    newAct->setEnabled(false);
                }
                contextMenuTable_->addAction(newAct);
                QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(predicateChange()));
            }
            beforeMenu_->clear();
            for (int i = 0; i < data->getColumnCount(); ++i) {
                if (i != index.column()) {
                    qlist->clear();
                    menuString = "\""+data->getColumnLabel(i)+"\" Column";
                    newAct = new QAction(QString::fromStdString(menuString),this);
                    qlist->push_back(QVariant(index.column()));
                    qlist->push_back(QVariant(i));
                    newAct->setData(QVariant(*qlist));
                    beforeMenu_->addAction(newAct);
                    QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(sortChangeBefore()));
                }
            }
            if (index.column() >= 0) {
                beforeMenu_->setEnabled(true);
            }
            else {
                beforeMenu_->setEnabled(false);
            }
            contextMenuTable_->addMenu(beforeMenu_);
            afterMenu_->clear();
            for (int i = 0; i < data->getColumnCount(); ++i) {
                if (i != index.column()) {
                    qlist->clear();
                    menuString = "\""+data->getColumnLabel(i)+"\" Column";
                    newAct = new QAction(QString::fromStdString(menuString),this);
                    qlist->push_back(QVariant(index.column()));
                    qlist->push_back(QVariant(i));
                    newAct->setData(QVariant(*qlist));
                    afterMenu_->addAction(newAct);
                    QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(sortChangeAfter()));
                }
            }
            if (index.column() >= 0) {
                afterMenu_->setEnabled(true);
            }
            else {
                afterMenu_->setEnabled(false);
            }
            contextMenuTable_->addMenu(afterMenu_);
            switchMenu_->clear();
            for (int i = 0; i < data->getColumnCount(); ++i) {
                if (i != index.column()) {
                    qlist->clear();
                    menuString = "\""+data->getColumnLabel(i)+"\" Column";
                    newAct = new QAction(QString::fromStdString(menuString),this);
                    qlist->push_back(QVariant(index.column()));
                    qlist->push_back(QVariant(i));
                    newAct->setData(QVariant(*qlist));
                    switchMenu_->addAction(newAct);
                    QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(sortChangeSwitch()));
                }
            }
            if (index.column() >= 0) {
                switchMenu_->setEnabled(true);
            }
            else {
                switchMenu_->setEnabled(false);
            }
            contextMenuTable_->addMenu(switchMenu_);

            if (function_ == FunctionLibrary::SELECT) {
                copyColumnMenu_->clear();
                for (int i = 0; i < data->getColumnCount(); ++i) {
                    qlist->clear();
                    menuString = "\""+data->getColumnLabel(i)+"\" Column";
                    newAct = new QAction(QString::fromStdString(menuString),this);
                    qlist->push_back(QVariant(index.column()));
                    qlist->push_back(QVariant(i));
                    newAct->setData(QVariant(*qlist));
                    copyColumnMenu_->addAction(newAct);
                    QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(copyColumn()));
                }

                contextMenuTable_->addMenu(copyColumnMenu_);
            }
        }
        else if (function_ == FunctionLibrary::FILTER) {

            contextMenuTable_->addSeparator();

            str.str("");
            str.clear();
            if (plotDataSelect->getResetListSize() > 0) {
                str << plotDataSelect->getResetListSize();
                menuString = "Reset All (" + str.str() + " Actions)";
            }
            else
                menuString = "Reset All";
            newAct = new QAction(QString::fromStdString(menuString),this);
            if (plotDataSelect->getResetListSize() == 0)
                newAct->setEnabled(false);
            contextMenuTable_->addAction(newAct);
            QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(selectResetAll()));

            menuString = "Reset Last";
            newAct = new QAction(QString::fromStdString(menuString),this);
            if (plotDataSelect->getResetListSize() == 0)
                newAct->setEnabled(false);
            contextMenuTable_->addAction(newAct);
            QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(selectResetLast()));


            contextMenuTable_->addSeparator();

            menuString = "Delete All Rows With EMPTY Cells";
            newAct = new QAction(QString::fromStdString(menuString),this);
            contextMenuTable_->addAction(newAct);
            QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(filterEmptyCells()));

            menuString = "Delete All Rows With Non Alpha-Numeric Cells";
            newAct = new QAction(QString::fromStdString(menuString),this);
            contextMenuTable_->addAction(newAct);
            QObject::connect(newAct,SIGNAL(triggered()),this,SLOT(filterAlphaNumeric()));
        }
        contextMenuTable_->popup(table_->mapToGlobal(pos));
    }
    delete qlist;
}