relationremove.cpp 3.31 KB
Newer Older
Christian Mollekopf's avatar
Christian Mollekopf committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
    Copyright (c) 2014 Christian Mollekopf <mollekopf@kolabsys.com>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "relationremove.h"

#include "connection.h"
#include "storage/querybuilder.h"
#include "storage/selectquerybuilder.h"
#include "storage/queryhelper.h"
#include "storage/datastore.h"

using namespace Akonadi;
using namespace Akonadi::Server;

bool RelationRemove::parseStream()
{
33
    Protocol::RemoveRelationsCommand cmd(m_command);
Christian Mollekopf's avatar
Christian Mollekopf committed
34

35
    if (cmd.left() < 0 || cmd.right() < 0) {
36
        return failureResponse("Invalid relation id's provided");
Christian Mollekopf's avatar
Christian Mollekopf committed
37 38 39
    }

    RelationType relType;
40
    if (!cmd.type().isEmpty()) {
41
        relType = RelationType::retrieveByName(QString::fromUtf8(cmd.type()));
Christian Mollekopf's avatar
Christian Mollekopf committed
42
        if (!relType.isValid()) {
43
            return failureResponse("Failed to load relation type");
Christian Mollekopf's avatar
Christian Mollekopf committed
44 45 46 47
        }
    }

    SelectQueryBuilder<Relation> relationQuery;
48 49
    relationQuery.addValueCondition(Relation::leftIdFullColumnName(), Query::Equals, cmd.left());
    relationQuery.addValueCondition(Relation::rightIdFullColumnName(), Query::Equals, cmd.right());
Christian Mollekopf's avatar
Christian Mollekopf committed
50 51 52 53 54
    if (relType.isValid()) {
        relationQuery.addValueCondition(Relation::typeIdFullColumnName(), Query::Equals, relType.id());
    }

    if (!relationQuery.exec()) {
55
        return failureResponse("Failed to obtain relations");
Christian Mollekopf's avatar
Christian Mollekopf committed
56 57
    }
    const Relation::List relations = relationQuery.result();
Laurent Montel's avatar
Laurent Montel committed
58
    for (const Relation &relation : relations) {
Christian Mollekopf's avatar
Christian Mollekopf committed
59 60 61
        DataStore::self()->notificationCollector()->relationRemoved(relation);
    }

Yuri Chornoivan's avatar
Yuri Chornoivan committed
62
    // Get all PIM items that are part of the relation
Christian Mollekopf's avatar
Christian Mollekopf committed
63 64
    SelectQueryBuilder<PimItem> itemsQuery;
    itemsQuery.setSubQueryMode(Query::Or);
65 66
    itemsQuery.addValueCondition(PimItem::idColumn(), Query::Equals, cmd.left());
    itemsQuery.addValueCondition(PimItem::idColumn(), Query::Equals, cmd.right());
Christian Mollekopf's avatar
Christian Mollekopf committed
67 68

    if (!itemsQuery.exec()) {
69
        throw failureResponse("Removing relation failed");
Christian Mollekopf's avatar
Christian Mollekopf committed
70 71 72 73 74 75 76
    }
    const PimItem::List items = itemsQuery.result();
    if (!items.isEmpty()) {
        DataStore::self()->notificationCollector()->itemsRelationsChanged(items, Relation::List(), relations);
    }

    QueryBuilder qb(Relation::tableName(), QueryBuilder::Delete);
77 78
    qb.addValueCondition(Relation::leftIdFullColumnName(), Query::Equals, cmd.left());
    qb.addValueCondition(Relation::rightIdFullColumnName(), Query::Equals, cmd.right());
Christian Mollekopf's avatar
Christian Mollekopf committed
79 80 81 82
    if (relType.isValid()) {
        qb.addValueCondition(Relation::typeIdFullColumnName(), Query::Equals, relType.id());
    }
    if (!qb.exec()) {
83
        return failureResponse("Failed to remove relations");
Christian Mollekopf's avatar
Christian Mollekopf committed
84 85
    }

86
    return successResponse<Protocol::RemoveRelationsResponse>();
Christian Mollekopf's avatar
Christian Mollekopf committed
87 88
}