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 int pop(ISession session, int maxRecords) {
       return 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;
    }

}

@@ -785,24 +785,25 @@
 s) %7B%0A       
+ 
 return queue
@@ -831,24 +831,24 @@
 axRecords);%0A
-
     %7D%0A%0A    @
@@ -1498,28 +1498,24 @@
 ) %7B%0A        
-    
 case Append:
@@ -1515,20 +1515,16 @@
 Append:%0A
-    
         
@@ -1572,36 +1572,32 @@
 d);%0A            
-    
 break;%0A         
@@ -1591,28 +1591,24 @@
 ak;%0A        
-  
-  
 case Delete:
@@ -1604,28 +1604,24 @@
 ase Delete:%0A
-    
             
@@ -1669,39 +1669,31 @@
             
-    
 break;%0A
-    
         case
@@ -1705,36 +1705,32 @@
 te:%0A            
-    
 result = process
@@ -1762,39 +1762,31 @@
             
-    
 break;%0A
-    
         case
@@ -1797,36 +1797,32 @@
 et:%0A            
-  
-  
 result = process
@@ -1849,36 +1849,32 @@
 d);%0A            
-    
 break;%0A         
@@ -1872,20 +1872,16 @@
         
-  
-  
 default:
@@ -1881,20 +1881,16 @@
 efault:%0A
-    
         
@@ -2251,16 +2251,27 @@
 , times 
+%7B%7D, record 
 %7B%7D%22, tab
@@ -2290,16 +2290,24 @@
 a, error
+, record
 );%0A     

Update SyncDatabase.java