View Javadoc
1   package com.srv4pos.server.api.sync.sample;
2   
3   import com.srv4pos.server.api.product.ProductInfo;
4   import com.srv4pos.server.api.sync.EntitySyncAdapter;
5   import com.srv4pos.server.api.sync.Uploader;
6   
7   import java.util.List;
8   
9   public class ProductSyncAdapter implements EntitySyncAdapter<ProductInfo> {
10      private final MemoryLocalStorage storage;
11  
12      public ProductSyncAdapter(MemoryLocalStorage storage) {
13          this.storage = storage;
14      }
15  
16      public void upload(Uploader<ProductInfo> uploader) {
17          List<SyncProduct> products = storage.getProducts();
18          for (SyncProduct product : products) {
19              if (!product.isSynced()) {
20                  doUpload(uploader, product);
21              }
22          }
23      }
24  
25      private void doUpload(Uploader<ProductInfo> uploader, SyncProduct product) {
26          final ProductInfo productInfo = product.getInfo();
27  
28          // syncing entity only if it was already synced at least once (has identifier) OR is not deleted
29          //if (productInfo.getIdentifier() != null || !productInfo.isDeleted()) {
30          productInfo.setTax(product.getTax().getInfo().getIdentifier());
31  
32          final String newIdentifier = uploader.upload(productInfo, product.getPictureInputStream());
33          if (newIdentifier != null && newIdentifier.length() > 0) {
34              productInfo.setIdentifier(newIdentifier);
35          }
36          //}
37          product.setSynced(true);
38      }
39  
40      public void createOrUpdateEntityFromInfo(ProductInfo info) {
41          SyncTax tax = storage.getTaxByIdentifier(info.getTax());
42  
43          if (tax == null) {
44              info.setTax(null); // clearing tax identifier if the storage does not have tax with identifier from server
45          }
46  
47          SyncProduct product = storage.getProductByIdentifier(info.getIdentifier());
48          if (product == null) {
49              product = new SyncProduct(info, tax);
50              storage.add(product); // adding even if info is in "deleted" state (info.isDeleted() == true)
51          } else {
52              product.setInfo(info);
53              product.setTax(tax);
54          }
55          product.setSynced(true);
56      }
57  }