phpgroupware-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Phpgroupware-cvs] sync/sync4j/de/probusiness/pbgroupware/sync/PHPGwSyn


From: nomail
Subject: [Phpgroupware-cvs] sync/sync4j/de/probusiness/pbgroupware/sync/PHPGwSyncStrategy.java, 1.1
Date: Thu, 20 May 2004 14:49:07 -0000

Update of /sync/sync4j/de/probusiness/pbgroupware/sync
Added Files:
        Branch: 
          PHPGwSyncStrategy.java

date: 2004/04/14 17:49:25;  author: mkaemmerer;  state: Exp;

Log Message:
- added file
=====================================================================
/*
 * Created on 04.04.2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package de.probusiness.pbgroupware.sync;

import java.security.*;
import java.util.*;
import java.util.logging.*;

import sync4j.framework.core.*;
import sync4j.framework.engine.*;
import sync4j.framework.engine.source.*;
import sync4j.framework.logging.*;
import sync4j.framework.security.*;
import sync4j.server.engine.*;

/**
 * @author ChristianW
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class PHPGwSyncStrategy extends Sync4jStrategy {

        private static interface ConflictAction {
                public Sync4jOperationStatus handleConflict(
                                final SyncOperationImpl op, final Principal 
owner,
                                SyncItem a, SyncItem b,
                                final SyncSource source0, final SyncSource 
source1
                );
        }
        
        private static final class AWinsCOnflictAction implements 
ConflictAction {
                public Sync4jOperationStatus handleConflict(final 
SyncOperationImpl op, final Principal owner,
                                SyncItem a, SyncItem b,
                                final SyncSource source0, final SyncSource 
source1) {
                        final ModificationCommand cmd = (ModificationCommand) 
a.getPropertyValue(SyncItemHelper.PROPERTY_COMMAND);
                        try {
                                a = source1.setSyncItem(owner, b);
                                op.setSyncItemA(a);
                                op.setAOperation(true);
                                return new Sync4jOperationStatusConflict(op, 
source1, cmd, StatusCode.CONFLICT_RESOLVED_WITH_SERVER_DATA);
                        } catch (SyncException e) {
                                log.severe("Error executing sync operation: " + 
e.getMessage());
                                log.throwing(getClass().getName(), 
"execSyncOperation", e);
                                return new Sync4jOperationStatusError(op, 
source1, cmd, e);
                        }
                }
        }
        
        private static final class BWinsCOnflictAction implements 
ConflictAction {
                public Sync4jOperationStatus handleConflict(
                                final SyncOperationImpl op, final Principal 
owner,
                                SyncItem a, SyncItem b,
                                final SyncSource source0, final SyncSource 
source1) {
                        final ModificationCommand cmd = (ModificationCommand) 
b.getPropertyValue(SyncItemHelper.PROPERTY_COMMAND);
                        
b.setProperty(a.getProperty(SyncItem.PROPERTY_TIMESTAMP)); // this contains the
                                                                                
   // current sync
                        try {
                                b = source0.setSyncItem(owner, a);
                                op.setSyncItemB(b);
                                op.setBOperation(true);
                                return new Sync4jOperationStatusConflict(op, 
source0, cmd, StatusCode.CONFLICT_RESOLVED_WITH_CLIENT_COMMAND_WINNING);
                        } catch (SyncException e) {
                                log.severe("Error executing sync operation: " + 
e.getMessage());
                                log.throwing(getClass().getName(), 
"execSyncOperation", e);
                                return new Sync4jOperationStatusError(op, 
source0, cmd, e);
                        }
                }
        }

        private static final Map CONFLICT_ACTIONS;
        static {
                final Map m = new HashMap();
                m.put("serverwins", new AWinsCOnflictAction());
                m.put("clientwins", new BWinsCOnflictAction());
                CONFLICT_ACTIONS = Collections.unmodifiableMap(m);
        }

        protected static transient Logger log = Sync4jLogger.getLogger();

        /**
         * 
         */
        public PHPGwSyncStrategy() {
                super();
                // TODO Auto-generated constructor stub
        }

        /**
         * @param syncSources
         */
        public PHPGwSyncStrategy(SyncSource[] syncSources) {
                super(syncSources);
                // TODO Auto-generated constructor stub
        }

        /* (non-Javadoc)
         * @see 
sync4j.server.engine.Sync4jStrategy#execSyncOperation(sync4j.framework.engine.SyncOperationImpl)
         */
        protected SyncOperationStatus[] execSyncOperation(SyncOperationImpl 
operation) {
                SyncItem syncItemA = operation.getSyncItemA(); 
                SyncItem syncItemB = operation.getSyncItemB();
                final Principal owner = operation.getOwner();
                final SyncSource[] sources = getSources();
                if (operation.getOperation() == SyncOperation.CONFLICT) {
                        final Vector params = new Vector();
                        params.add(syncItemA.getKey().getKeyAsString());
                        params.add(syncItemB.getKey().getKeyAsString());
                        Object ret = null;
                        try {
                                ret = executeXMLRPC(owner, "handleConflict", 
params);
                        }
                        catch (final SyncSourceException e) {
                                // TODO logging
                        }
                        if (ret != null) {
                                final ConflictAction action = (ConflictAction) 
CONFLICT_ACTIONS.get(ret);
                                if (action != null) {
                                        return new SyncOperationStatus[] {
                                                
action.handleConflict(operation, owner, syncItemA, syncItemB, sources[0], 
sources[1])
                                        };
                                }
                        }
                }
                return super.execSyncOperation(operation);
        }

        private final Object executeXMLRPC(final Principal principal, final 
String methodname, final Vector params) throws SyncSourceException {
                // prepend device_id to parameter-list
                params.add(0, ((Sync4jPrincipal) principal).getId());
                // prepare ipc-dispatch
                final Vector fparams = new Vector();
                fparams.add("sync." + methodname);
                fparams.add(params);
                return 
PHPGwSyncXMLRPCClient.getInstance().execute("phpgwapi.ipc_manager.execIPC", 
fparams);
        }

}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]