View Javadoc
1   package com.srv4pos.server.api.sync.sample;
2   
3   import com.srv4pos.server.api.sale.SaleSlotInfo;
4   import com.srv4pos.server.api.sync.EntitySyncAdapter;
5   import com.srv4pos.server.api.sync.Uploader;
6   
7   import java.util.List;
8   
9   /**
10   * Created by misha on 30.06.16.
11   */
12  public class SaleSlotSyncAdapter implements EntitySyncAdapter<SaleSlotInfo> {
13  
14      private MemoryLocalStorage storage;
15  
16      public SaleSlotSyncAdapter(MemoryLocalStorage storage) {
17          this.storage = storage;
18      }
19  
20      public void upload(Uploader<SaleSlotInfo> uploader) {
21          List<SyncSaleSlot> saleSlots = storage.getSaleSlots();
22          for (SyncSaleSlot saleSlot : saleSlots) {
23              if (!saleSlot.isSynced()) {
24                  doUpload(uploader, saleSlot);
25              }
26          }
27      }
28  
29      private void  doUpload(Uploader<SaleSlotInfo> uploader, SyncSaleSlot saleSlot) {
30          final SaleSlotInfo saleSlotInfo= saleSlot.getInfo();
31  
32          final String newIdentifier = uploader.upload(saleSlotInfo, saleSlot.getPictureInputStream());
33          if (newIdentifier != null && newIdentifier.length() > 0) {
34              saleSlotInfo.setIdentifier(newIdentifier);
35          }
36  
37          saleSlot.setSynced(true);
38      }
39  
40      public void createOrUpdateEntityFromInfo(SaleSlotInfo info) {
41          SyncSaleSlot saleSlot = storage.getSaleSlotByIdentifier(info.getIdentifier());
42  
43          if (saleSlot == null) {
44              saleSlot = new SyncSaleSlot(info);
45              storage.add(saleSlot); // adding even if info is in "deleted" state (info.isDeleted() == true)
46          } else {
47              saleSlot.setInfo(info);
48          }
49  
50          saleSlot.setSynced(true);
51      }
52  }