summaryrefslogtreecommitdiffstats
path: root/dummyresource/domainadaptor.cpp
blob: 00af3fe267d914ed70297b15c22133af6ac67747 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "domainadaptor.h"

#include <QDebug>
#include <functional>

#include "dummycalendar_generated.h"
#include "event_generated.h"
#include "entity_generated.h"
#include "metadata_generated.h"
#include "domainadaptor.h"
#include <common/entitybuffer.h>

using namespace DummyCalendar;
using namespace flatbuffers;

/**
 * Defines how to convert qt primitives to flatbuffer ones
 * TODO: rename to createProperty or so?
 */
template <class T>
uoffset_t extractProperty(const QVariant &, flatbuffers::FlatBufferBuilder &fbb);

template <>
uoffset_t extractProperty<QString>(const QVariant &property, flatbuffers::FlatBufferBuilder &fbb)
{
    if (property.isValid()) {
        return fbb.CreateString(property.toString().toStdString()).o;
    }
    return 0;
}

/**
 * Create a buffer from a domain object using the provided mappings
 */
template <class Builder>
void createBufferPart(const Akonadi2::ApplicationDomain::ApplicationDomainType &domainObject, flatbuffers::FlatBufferBuilder &fbb, const WritePropertyMapper<Builder> &mapper)
{
    //First create a primitives such as strings using the mappings
    QList<std::function<void(Builder &)> > propertiesToAddToResource;
    for (const auto &property : domainObject.changedProperties()) {
        const auto value = domainObject.getProperty(property);
        if (mapper.hasMapping(property)) {
            mapper.setProperty(property, domainObject.getProperty(property), propertiesToAddToResource, fbb);
        }
    }

    //Then create all porperties using the above generated builderCalls
    Builder builder(fbb);
    for (auto propertyBuilder : propertiesToAddToResource) {
        propertyBuilder(builder);
    }
    builder.Finish();
}



DummyEventAdaptorFactory::DummyEventAdaptorFactory()
    : DomainTypeAdaptorFactory()
{
    //TODO turn this into initializeReadPropertyMapper as well?
    mResourceMapper = QSharedPointer<ReadPropertyMapper<DummyEvent> >::create();
    mResourceMapper->addMapping("summary", [](DummyEvent const *buffer) -> QVariant {
        if (buffer->summary()) {
            return QString::fromStdString(buffer->summary()->c_str());
        }
        return QVariant();
    });

    mResourceWriteMapper->addMapping("summary", [](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(DummyEventBuilder &)> {
        auto offset = extractProperty<QString>(value, fbb);
        return [offset](DummyEventBuilder &builder) { builder.add_summary(offset); };
    });
}


void DummyEventAdaptorFactory::createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb)
{
    // flatbuffers::FlatBufferBuilder resFbb;
    // flatbuffers::FlatBufferBuilder localFbb;

    // QList<std::function<void(DummyEventBuilder &)> > propertiesToAddToResource;
    // QList<std::function<void(Akonadi2::ApplicationDomain::Buffer::EventBuilder &)> > propertiesToAddToLocal;
    // for (const auto &property : event.changedProperties()) {
    //     const auto value = event.getProperty(property);
    //     if (mResourceWriteMapper && mResourceWriteMapper->hasMapping(property)) {
    //         mResourceWriteMapper->setProperty(property, value, propertiesToAddToResource, resFbb);
    //     } if (mLocalWriteMapper && mLocalWriteMapper->hasMapping(property)) {
    //         mLocalWriteMapper->setProperty(property, value, propertiesToAddToLocal, localFbb);
    //     }
    // }

    // DummyEventBuilder resBuilder(resFbb);
    // for (auto propertyBuilder : propertiesToAddToResource) {
    //     propertyBuilder(resBuilder);
    // }
    // resBuilder.Finish();

    // DummyEventBuilder localBuilder(localFbb);
    // for (auto propertyBuilder : propertiesToAddToResource) {
    //     propertyBuilder(localBuilder);
    // }
    // localBuilder.Finish();

    // TODO: how does a resource specify what goes to a local buffer and what it stores separately?
    // flatbuffers::FlatBufferBuilder eventFbb;
    // {
    //     auto summary = extractProperty<QString>(event.getProperty("summary"), fbb);
    //     DummyCalendar::DummyEventBuilder eventBuilder(eventFbb);
    //     eventBuilder.add_summary(summary);
    //     auto eventLocation = eventBuilder.Finish();
    //     DummyCalendar::FinishDummyEventBuffer(eventFbb, eventLocation);
    // }

    //TODO we should only copy values into the local buffer that haven't already been copied by the resource buffer
    flatbuffers::FlatBufferBuilder localFbb;
    if (mLocalWriteMapper) {
        createBufferPart<Akonadi2::ApplicationDomain::Buffer::EventBuilder>(event, localFbb, *mLocalWriteMapper);
    }

    flatbuffers::FlatBufferBuilder resFbb;
    if (mResourceWriteMapper) {
        createBufferPart<DummyEventBuilder>(event, resFbb, *mResourceWriteMapper);
    }

    Akonadi2::EntityBuffer::assembleEntityBuffer(fbb, 0, 0, resFbb.GetBufferPointer(), resFbb.GetSize(), localFbb.GetBufferPointer(), localFbb.GetSize());
}