This repository was archived by the owner on Feb 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 278
/
Copy pathbase_model.h
188 lines (149 loc) · 4.35 KB
/
base_model.h
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2014 Toggl Desktop developers.
#ifndef SRC_BASE_MODEL_H_
#define SRC_BASE_MODEL_H_
#include <string>
#include <vector>
#include <cstring>
#include <ctime>
#include <json/json.h> // NOLINT
#include "const.h"
#include "types.h"
#include "util/logger.h"
#include <Poco/Types.h>
namespace toggl {
class BatchUpdateResult;
class TOGGL_INTERNAL_EXPORT BaseModel {
public:
BaseModel()
: local_id_(0)
, id_(0)
, guid_("")
, ui_modified_at_(0)
, uid_(0)
, dirty_(false)
, deleted_at_(0)
, is_marked_as_deleted_on_server_(false)
, updated_at_(0)
, validation_error_(error::kNoError)
, unsynced_(false) {}
virtual ~BaseModel() {}
const Poco::Int64 &LocalID() const {
return local_id_;
}
void SetLocalID(const Poco::Int64 value) {
local_id_ = value;
}
const Poco::UInt64 &ID() const {
return id_;
}
void SetID(const Poco::UInt64 value);
const Poco::Int64 &UIModifiedAt() const {
return ui_modified_at_;
}
void SetUIModifiedAt(const Poco::Int64 value);
void SetUIModified() {
SetUIModifiedAt(time(nullptr));
}
const std::string &GUID() const {
return guid_;
}
void SetGUID(const std::string &value);
const Poco::UInt64 &UID() const {
return uid_;
}
void SetUID(const Poco::UInt64 value);
void SetDirty();
const bool &Dirty() const {
return dirty_;
}
void ClearDirty() {
dirty_ = false;
}
const bool &Unsynced() const {
return unsynced_;
}
void SetUnsynced();
void ClearUnsynced() {
unsynced_ = false;
}
// Deleting a time entry hides it from
// UI and flags it for removal from server:
const Poco::Int64 &DeletedAt() const {
return deleted_at_;
}
void SetDeletedAt(const Poco::Int64 value);
const Poco::Int64 &UpdatedAt() const {
return updated_at_;
}
void SetUpdatedAt(const Poco::Int64 value);
std::string UpdatedAtString() const;
void SetUpdatedAtString(const std::string &value);
// When a model is deleted
// on server, it will be removed from local
// DB using this flag:
bool IsMarkedAsDeletedOnServer() const {
return is_marked_as_deleted_on_server_;
}
void MarkAsDeletedOnServer() {
is_marked_as_deleted_on_server_ = true;
SetDirty();
}
bool NeedsPush() const;
bool NeedsPOST() const;
bool NeedsPUT() const;
bool NeedsDELETE() const;
bool NeedsToBeSaved() const;
void EnsureGUID();
void ClearValidationError();
void SetValidationError(const error &value);
const error &ValidationError() const {
return validation_error_;
}
virtual std::string String() const = 0;
virtual std::string ModelName() const = 0;
virtual std::string ModelURL() const = 0;
virtual void LoadFromJSON(Json::Value value) {}
virtual Json::Value SaveToJSON() const {
return 0;
}
virtual bool DuplicateResource(const toggl::error &err) const {
return false;
}
virtual bool ResourceCannotBeCreated(const toggl::error &err) const {
return false;
}
virtual bool ResolveError(const toggl::error &err) {
return false;
}
error LoadFromDataString(const std::string &);
void Delete();
error ApplyBatchUpdateResult(BatchUpdateResult * const);
// Convert model JSON into batch update format.
error BatchUpdateJSON(Json::Value *result) const;
protected:
Logger logger() const;
bool userCannotAccessWorkspace(const toggl::error &err) const;
private:
std::string batchUpdateRelativeURL() const;
std::string batchUpdateMethod() const;
Poco::Int64 local_id_;
Poco::UInt64 id_;
guid guid_;
Poco::Int64 ui_modified_at_;
Poco::UInt64 uid_;
bool dirty_;
Poco::Int64 deleted_at_;
bool is_marked_as_deleted_on_server_;
Poco::Int64 updated_at_;
// If model push to backend results in an error,
// the error is attached to the model for later inspection.
error validation_error_;
// Flag is set only when sync fails.
// Its for viewing purposes only. It should not
// be used to check if a model needs to be
// pushed to backend. It only means that some
// attempt to push failed somewhere.
bool unsynced_;
};
} // namespace toggl
#endif // SRC_BASE_MODEL_H_