View Javadoc
1   package com.srv4pos.server.api.sync.sample;
2   
3   import com.srv4pos.server.api.sync.EntitySyncAdapter;
4   import com.srv4pos.server.api.sync.Uploader;
5   import com.srv4pos.server.api.treasurer.TreasurerInfo;
6   
7   /**
8    * Created by ivkosh on 25.05.15.
9    */
10  public class TreasurerSyncAdapter implements EntitySyncAdapter<TreasurerInfo> {
11      private final MemoryLocalStorage storage;
12  
13      public TreasurerSyncAdapter(MemoryLocalStorage storage) {
14          this.storage = storage;
15      }
16  
17      public void upload(Uploader<TreasurerInfo> uploader) {
18          for (SyncTreasurer t : storage.getTreasurers()) {
19              if (!t.isSynced()) {
20                  doUpload(uploader, t);
21              }
22          }
23      }
24  
25      private void doUpload(Uploader<TreasurerInfo> uploader, SyncTreasurer treasurer) {
26          final TreasurerInfo treasurerInfo = treasurer.getInfo();
27  
28          // syncing entity only if it was already synced at least once (has identifier) OR is not deleted
29          //if (treasurerInfo.getIdentifier() != null || !treasurerInfo.isDeleted()) {
30  
31          final String newIdentifier = uploader.upload(treasurerInfo, null);
32          if (newIdentifier != null && newIdentifier.length() > 0) {
33              treasurerInfo.setIdentifier(newIdentifier);
34          }
35          //}
36          treasurer.setSynced(true);
37      }
38  
39      public void createOrUpdateEntityFromInfo(TreasurerInfo info) {
40          SyncTreasurer treasurer = storage.getTreasurerByIdentifier(info.getIdentifier());
41          if (treasurer == null) {
42              treasurer = storage.getTreasurerBySsn(info.getSsn());
43          }
44  
45          if (treasurer == null) {
46              treasurer = new SyncTreasurer(info);
47              storage.add(treasurer); // adding even if info is in "deleted" state (info.isDeleted() == true)
48          } else {
49              treasurer.setInfo(info);
50          }
51          treasurer.setSynced(true);
52      }
53  }