package cn.cerc.mis.sync;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.cerc.core.ISession;
import cn.cerc.core.Record;
import cn.cerc.mis.core.Application;

public class SyncDatabase implements IPopProcesser {
    private static final Logger log = LoggerFactory.getLogger(SyncDatabase.class);
    private ISyncServer queue;

    public SyncDatabase(ISyncServer queue) {
        super();
        this.queue = queue;
    }

    public void push(ISession session, String tableCode, Record record, SyncOpera opera) {
        Record rs = new Record();
        rs.setField("__table", tableCode);
        rs.setField("__opera", opera.ordinal());
        rs.copyValues(record);
        queue.push(session, rs);
    }

    public void pop(ISession session, int maxRecords) {
        queue.pop(session, this, maxRecords);
    }

    @Override
    public boolean popRecord(ISession session, Record record) {
        String tableCode = record.getString("__table");
        int opera = record.getInt("__opera");
        int error = record.getInt("__error");
        record.delete("__table");
        record.delete("__opera");
        record.delete("__error");

        IPushProcesser processer = Application.getBean(IPushProcesser.class, "sync_" + tableCode);
        if (processer == null) {
            processer = new PushTableDefault().setTableCode(tableCode);
        }
        processer.setSession(session);

        boolean result;
        switch (SyncOpera.values()[opera]) {
        case Append:
            result = processer.appendRecord(record);
            break;
        case Delete:
            result = processer.deleteRecord(record);
            break;
        case Update:
            result = processer.updateRecord(record);
            break;
        case Reset:
            result = processer.resetRecord(record);
            break;
        default:
            throw new RuntimeException("not support opera.");
        }

        if (!result) {
            record.setField("__table", tableCode);
            record.setField("__opera", opera);
            record.setField("__error", error + 1);
            if (error < 5) {
                queue.repush(session, record);
                log.warn("sync {}.{} fail, times {}", tableCode, opera, error);
            } else {
                processer.abortRecord(record, SyncOpera.values()[opera]);
            }
        }
        return result;
    }

    public ISyncServer getQueue() {
        return queue;
    }

}

@@ -738,20 +738,19 @@
  public 
-void
+int
  pop(ISe
@@ -781,32 +781,38 @@
 cords) %7B%0A       
+return
  queue.pop(sessi
@@ -1497,24 +1497,28 @@
 ) %7B%0A        
+    
 case Append:
@@ -1518,16 +1518,20 @@
 Append:%0A
+    
         
@@ -1579,32 +1579,36 @@
 d);%0A            
+    
 break;%0A        c
@@ -1602,24 +1602,28 @@
 ak;%0A        
+  
+  
 case Delete:
@@ -1619,24 +1619,28 @@
 ase Delete:%0A
+    
             
@@ -1684,39 +1684,47 @@
 d);%0A            
+    
 break;%0A
+    
         case Upd
@@ -1732,32 +1732,36 @@
 te:%0A            
+    
 result = process
@@ -1789,39 +1789,47 @@
 d);%0A            
+    
 break;%0A
+    
         case Res
@@ -1836,32 +1836,36 @@
 et:%0A            
+  
+  
 result = process
@@ -1892,32 +1892,36 @@
 d);%0A            
+    
 break;%0A        d
@@ -1919,16 +1919,20 @@
         
+  
+  
 default:
@@ -1932,16 +1932,20 @@
 efault:%0A
+    
         

Update SyncDatabase.java