OpenAB  1.0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Sync.hpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5  */
10 #ifndef SYNC_HPP_
11 #define SYNC_HPP_
12 
13 #include <map>
14 #include <string>
15 #include <vector>
16 #include <plugin/Plugin.hpp>
18 #include <plugin/source/Source.hpp>
20 
24 namespace OpenAB_Sync {
25 
83 {
84  public:
88  Parameters();
89 
94  Parameters(const std::string& json);
95 
99  ~Parameters();
100 
105 
110 
116 
121 };
122 
126 class Sync
127 {
128  public:
132  Sync(){}
133 
137  virtual ~Sync(){}
138 
142  enum eInit{
145  };
146 
151  virtual enum eInit init() = 0;
152 
156  enum eSync{
162  };
163 
167  virtual void synchronize() = 0;
168 
172  enum eCancel {
176  };
177 
182  virtual enum eCancel cancel() = 0;
183 
187  enum eSuspend {
191  };
192 
197  virtual enum eSuspend suspend() = 0;
198 
202  enum eResume {
206  };
207 
212  virtual enum eResume resume() = 0;
213 
223  virtual void getStats(unsigned int& locallyAdded,
224  unsigned int& locallyModified,
225  unsigned int& locallyRemoved,
226  unsigned int& remotelyAdded,
227  unsigned int& remotelyModified,
228  unsigned int& remotelyRemoved) = 0;
229 
239  bool addPhase(const std::string& name, const std::vector<std::string>& ignoreFields);
240 
244  void clearPhases();
245 
250  {
251  public:
253  virtual ~SyncCallback(){};
258  virtual void print(const std::string& msg) = 0;
259 
264  virtual void syncFinished(const enum OpenAB_Sync::Sync::eSync& result) = 0;
265 
271  virtual void syncProgress(const std::string& phaseName, double progress, unsigned int numProcessedItems) = 0;
272 
277  virtual void syncPhaseStarted(const std::string& name) = 0;
278 
283  virtual void syncPhaseFinished(const std::string& name) = 0;
284 
289  virtual void metadataUpdated(const std::string& ){}
290  };
291 
292  protected:
293  struct Phase
294  {
295  Phase(const std::string& n, const std::vector<std::string> & ignore) :
296  name(n),
297  ignoredFields(ignore) {}
298 
299  std::string name;
300  std::vector<std::string> ignoredFields;
301  };
302  std::vector<Phase> phases;
303 
304  private:
308  Sync(Sync const &other);
309 
313  Sync& operator=(Sync const &other);
314 };
315 
316 //typedef OpenAB_Plugin::Factory< Sync , Parameters > Factory;
317 
323 {
324  public:
325  SyncMetadata();
326  ~SyncMetadata();
327 
336  void addItem(const std::string& remoteId, const std::string& remoteRevision,
337  const std::string& localId, const std::string& localRevision);
338 
345  void removeItem(const std::string& remoteId, const std::string& localId);
346 
352  void updateRemoteRevision(const std::string& uid, const std::string& revision);
353 
359  void updateLocalRevision(const std::string& uid, const std::string& revision);
360 
367  std::string getRemoteRevision(const std::string& uid) const;
368 
375  std::string getLocalRevision(const std::string& uid) const;
376 
382  bool hasLocalId(const std::string& uid) const;
383 
389  bool hasRemoteId(const std::string& uid) const;
390 
395  std::string toJSON() const;
396 
402  bool fromJSON(const std::string& json);
403 
411  };
412 
413 
419 
425 
431  void setRemoteState(const std::string& uid, SyncMetadataState state);
432 
438  void setLocalState(const std::string& uid, SyncMetadataState state);
439 
446  std::map<std::string, std::string> getItemsWithState(SyncMetadataState localState, SyncMetadataState remoteState);
447 
452  std::string getLocalSyncToken() const;
453 
458  void setLocalSyncToken(const std::string& token);
459 
464  std::string getRemoteSyncToken() const;
465 
470  void setRemoteSyncToken(const std::string& token);
471 
472  private:
473  std::string remoteSyncToken;
474  std::string localSyncToken;
475  std::map<std::string, std::string> remoteRevisions;
476  std::map<std::string, std::string> localRevisions;
477  std::map<std::string, std::string> remoteToLocalIdMapping;
478 
479  std::map<std::string, SyncMetadataState> remoteState;
480  std::map<std::string, SyncMetadataState> localState;
481 };
482 } // namespace OpenAB_Sync
483 
484 DECLARE_PLUGIN_INTERFACE(OpenAB_Sync, Sync, Parameters);
485 
486 #endif // SYNC_HPP_
eInit
Definition: Sync.hpp:142
std::map< std::string, std::string > getItemsWithState(SyncMetadataState localState, SyncMetadataState remoteState)
Queries all metadata record which items are in given states.
Definition: Sync.cpp:310
SyncMetadataState
Definition: Sync.hpp:407
void setLocalState(const std::string &uid, SyncMetadataState state)
Sets state of remote item.
Definition: Sync.cpp:305
Definition: Sync.hpp:159
Generic storage for Sync plugin parameters. Allows to store map of parameters with different types (O...
Definition: Sync.hpp:82
virtual void getStats(unsigned int &locallyAdded, unsigned int &locallyModified, unsigned int &locallyRemoved, unsigned int &remotelyAdded, unsigned int &remotelyModified, unsigned int &remotelyRemoved)=0
Returns statistics of synchronization.
virtual enum eSuspend suspend()=0
Suspends synchronization (if used OpenAB_Source::Source plugin supports it)
virtual void synchronize()=0
Synchronizes data.
void setLocalSyncToken(const std::string &token)
Sets sync token of local storage to be stored in metadata.
Definition: Sync.cpp:167
bool hasRemoteId(const std::string &uid) const
Checks if there is metadata information for remote item with given id.
Definition: Sync.cpp:143
void updateRemoteRevision(const std::string &uid, const std::string &revision)
Updates revision of remote item.
Definition: Sync.cpp:105
Definition: Sync.hpp:173
std::vector< std::string > ignoredFields
Definition: Sync.hpp:300
virtual void syncProgress(const std::string &phaseName, double progress, unsigned int numProcessedItems)=0
Informs about progress of synchronization.
Definition: Sync.hpp:143
std::string getRemoteSyncToken() const
Returns sync token of remote storage after last synchronization.
Definition: Sync.cpp:152
std::string getRemoteRevision(const std::string &uid) const
Returns revision of remote item.
Definition: Sync.cpp:110
eCancel
Definition: Sync.hpp:172
bool hasLocalId(const std::string &uid) const
Checks if there is metadata information for local item with given id.
Definition: Sync.cpp:134
Definition: Sync.hpp:203
void setRemoteState(const std::string &uid, SyncMetadataState state)
Sets state of remote item.
Definition: Sync.cpp:300
void addItem(const std::string &remoteId, const std::string &remoteRevision, const std::string &localId, const std::string &localRevision)
Adds new record to metadata. It will bind remote and local items, detected as identical.
Definition: Sync.cpp:68
virtual ~SyncCallback()
Definition: Sync.hpp:253
void setRemoteSyncToken(const std::string &token)
Sets sync token of remote storage to be stored in metadata.
Definition: Sync.cpp:157
OpenAB_Source::Parameters localSourcePluginParams
Parameters to be used by local OpenAB_Source::Source plugin.
Definition: Sync.hpp:104
virtual void metadataUpdated(const std::string &)
Informs that synchronization metadata has been updated.
Definition: Sync.hpp:289
virtual void syncPhaseFinished(const std::string &name)=0
Informs about end of synchronization phase.
eResume
Definition: Sync.hpp:202
std::string toJSON() const
Converts metadata into JSON format, so it can be passed around as string.
Definition: Sync.cpp:172
Definition: Sync.hpp:410
Definition: Sync.hpp:144
OpenAB_Storage::Parameters remoteStoragePluginParams
Parameters to be used by remote OpenAB_Storage::Storage plugin.
Definition: Sync.hpp:120
DECLARE_PLUGIN_INTERFACE(OpenAB_Sync, Sync, Parameters)
bool addPhase(const std::string &name, const std::vector< std::string > &ignoreFields)
Adds new phase to synchronization process.
Definition: Sync.cpp:34
Virtual class that provide all the callback routines to control the synchronization.
Definition: Sync.hpp:249
Definition: Sync.hpp:161
OpenAB_Source::Parameters remoteSourcePluginParams
Parameters to be used by remote OpenAB_Source::Source plugin.
Definition: Sync.hpp:109
SyncCallback()
Definition: Sync.hpp:252
void removeItem(const std::string &remoteId, const std::string &localId)
Removes record from metadata. It will remove information about remote and local items binding...
Definition: Sync.cpp:77
Parameters()
Default constructor.
Definition: Sync.cpp:17
~Parameters()
Destructor, virtual by default.
Definition: Sync.cpp:29
Phase(const std::string &n, const std::vector< std::string > &ignore)
Definition: Sync.hpp:295
Definition: Sync.hpp:190
Documentation for class SyncMetadata. This class is designed to store and keep track of synchronizati...
Definition: Sync.hpp:322
std::string name
Definition: Sync.hpp:299
Definition: Sync.hpp:175
OpenAB_Plugin::GenericParameters Parameters
Use generic parameters.
Definition: Source.hpp:70
std::vector< Phase > phases
Definition: Sync.hpp:302
~SyncMetadata()
Definition: Sync.cpp:61
virtual ~Sync()
Destructor, virtual by default.
Definition: Sync.hpp:137
Documentation for class Sync plugin interface.
Definition: Sync.hpp:126
virtual void syncFinished(const enum OpenAB_Sync::Sync::eSync &result)=0
Informs about end of synchronization.
virtual enum eInit init()=0
Initializes Sync.
bool fromJSON(const std::string &json)
Load metadata from JSON format.
Definition: Sync.cpp:221
virtual enum eResume resume()=0
Resumes synchronization (if used OpenAB_Source::Source plugin supports it)
SyncMetadata()
Definition: Sync.cpp:56
eSuspend
Definition: Sync.hpp:187
Definition: Sync.hpp:188
Sync()
Constructor.
Definition: Sync.hpp:132
void resetRemoteState(SyncMetadataState st)
Resets state of all remote items.
Definition: Sync.cpp:289
void updateLocalRevision(const std::string &uid, const std::string &revision)
Updates revision of local item.
Definition: Sync.cpp:100
std::string getLocalRevision(const std::string &uid) const
Returns revision of local item.
Definition: Sync.cpp:122
virtual enum eCancel cancel()=0
Cancels synchronization.
Definition: Sync.hpp:205
virtual void syncPhaseStarted(const std::string &name)=0
Informs about start of next synchronization phase.
OpenAB_Storage::Parameters localStoragePluginParams
parameters to be used by local OpenAB_Storage::Storage plugin
Definition: Sync.hpp:115
std::string getLocalSyncToken() const
Returns sync token of local storage after last synchronization.
Definition: Sync.cpp:162
Generic storage for plugin parameters. Allows to store map of parameters with different types (OpenAB...
Definition: GenericParameters.hpp:25
void resetLocalState(SyncMetadataState st)
Resets state of all local items.
Definition: Sync.cpp:278
void clearPhases()
Removes all previously defined synchronization phases.
Definition: Sync.cpp:51
eSync
Definition: Sync.hpp:156
Definition: Sync.hpp:293
virtual void print(const std::string &msg)=0
Dummy funtion used to provide debug messages from Sync.