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.tax.TaxInfo;
6   
7   import java.util.List;
8   
9   public class TaxSyncAdapter implements EntitySyncAdapter<TaxInfo> {
10  
11      private final MemoryLocalStorage storage;
12  
13      public TaxSyncAdapter(MemoryLocalStorage storage) {
14          this.storage = storage;
15      }
16  
17      public void upload(Uploader<TaxInfo> uploader) {
18          List<SyncTax> taxes = storage.getTaxes();
19          for (SyncTax tax : taxes) {
20              if (!tax.isSynced()) {
21                  doUpload(uploader, tax);
22              }
23          }
24      }
25  
26      private void doUpload(Uploader<TaxInfo> uploader, SyncTax tax) {
27          final TaxInfo taxInfo = tax.getInfo();
28  
29          // syncing entity only if it was already synced at least once (has identifier) OR is not deleted
30  //        if (taxInfo.getIdentifier() != null || !taxInfo.isDeleted()) {
31  
32          final String newIdentifier = uploader.upload(taxInfo, null);
33          if (newIdentifier != null && newIdentifier.length() > 0) {
34              taxInfo.setIdentifier(newIdentifier);
35          }
36  //        }
37          tax.setSynced(true);
38      }
39  
40      public void createOrUpdateEntityFromInfo(TaxInfo info) {
41          SyncTax tax = storage.getTaxByIdentifier(info.getIdentifier());
42  
43          if (tax == null) {
44              if (!info.isDeleted()) {
45                  // gracefully merging taxes with the same vat amount when it is possible, see https://redmine.produktpoolen.se/issues/1688
46                  tax = storage.getTaxByVat(info.getVat());
47              }
48  
49              if (tax != null) {
50                  tax.setInfo(info);
51              } else {
52                  tax = new SyncTax(info);
53                  storage.add(tax); // adding tax even if it was deleted on server (info.isDeleted() == true)
54              }
55          } else {
56              tax.setInfo(info);
57          }
58  
59          tax.setSynced(true);
60      }
61  }