bool SQLiteDocumentDAO::saveOrUpdate(Document::Ptr pub)
{
    if(!pub) {
        return false;
    }

    const qulonglong invalidId = (qulonglong) -1;

    if(pub->id() == invalidId) {

        return save(pub);
    } else {

        return update(pub);
    }
}
bool SQLiteDocumentDAO::remove(Document::Ptr entity)
{
    if(!entity) {
        return false;
    }

    bool ret = QueryExecutor(database()).remove("document", makeQueryParameters("id", entity->id()));

    if(ret) {
        repository()->publications()->remove(entity->id());

        emit dataChanged();
    }

    return ret;
}
bool SQLiteDocumentDAO::update(Document::Ptr pub)
{
    if(!pub) {
        return false;
    }

    repository()->journalDAO()->saveOrUpdate(pub->journal());

    QueryParameters params;

    params.insert("title", pub->title());
    params.insert("abstract", pub->abstract());
    params.insert("year", pub->year());
    params.insert("month", pub->month());
    params.insert("conference", pub->conference());
    params.insert("publisher", pub->publisher());
    params.insert("volume", pub->volume());
    params.insert("number", pub->number());
    params.insert("url", pub->url());
    params.insert("doi", pub->doi());
    params.insert("isbn", pub->isbn());
    params.insert("localURL", pub->localUrl());
    params.insert("type", static_cast<uint>(pub->type()));
    params.insert("isPublished", pub->isPublished());
    params.insert("isPeerReviewed", pub->isPeerReviewed());
    //params.insert("keywords", pub->keywords());
    params.insert("series", pub->series());
    params.insert("subTitle", pub->subTitle());
    params.insert("pages", pub->pages().toString());
    params.insert("authors", StringUtils::serialize(pub->authors()));
    params.insert("editors", StringUtils::serialize(pub->editors()));
    params.insert("citeKey", pub->citeKey());

    if(pub->journal()) {
        params.insert("journalId", pub->journal()->id());
    } else {
        params.insert("journalId", QVariant());
    }

    bool ok = QueryExecutor().update("document", makeQueryParameters("id", pub->id()), params);

    if(ok) {
        emit dataChanged();
    }

    return ok;
}