⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sync4jstrategy.java

📁 实现了SyncML无线同步协议
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                    case SyncItemState.SYNCHRONIZED:                        return new SyncOperationImpl(principal, syncItemA, syncItemB, SyncOperation.NEW, true, false);                    case SyncItemState.NOT_EXISTING:                    case SyncItemState.DELETED:                        return new SyncOperationImpl(principal, syncItemA, syncItemB, SyncOperation.NOP, false, false);                }  // end inner switch            //            // CONFLICT            // In this case itemA has a mapped twin and itemB not existing            //            case SyncItemState.CONFLICT:                return new SyncConflict(syncItemA, syncItemB, "CX");        }  // end switch                return new SyncOperationImpl(principal, syncItemA, syncItemB, SyncOperation.NOP, false, false);    }        /**     * Executes the given SyncOperation. Note that conflicts are ignored!     * <p>     * Note also that the number of status returned is equal to the number of     * sources affected by the operation (0 for NOP, 1 for NEW and UPDATE and      * 1 or 2 for DELETE).     *     * @param operation     the SyncOperation to execute     *     * @return an array of <i>SyncOperationStatus</i> objects representing the      *         status of the executed operation. For instance, in case of error,     *         <i>SyncOperation.error</i> will be set to the catched exception.     */    protected SyncOperationStatus[] execSyncOperation(SyncOperationImpl operation) {                SyncItem syncItemA = operation.getSyncItemA(),                  syncItemB = operation.getSyncItemB();                Principal owner = operation.getOwner();        SyncOperationStatus[] status = null;        ModificationCommand cmd = null;                int size = 0, s = 0;        switch (operation.getOperation()) {            case SyncOperation.NEW:                status = new SyncOperationStatus[1];                if (operation.isAOperation()) {                    cmd = (ModificationCommand)syncItemA.getPropertyValue(SyncItemHelper.PROPERTY_COMMAND);                    try {                        syncItemA = sources[1].setSyncItem(owner, syncItemB);                        operation.setSyncItemA(syncItemA);                                                status[0] = new Sync4jOperationStatusOK(operation, sources[1], cmd, StatusCode.ITEM_ADDED);                                            } catch (SyncException e) {                        log.severe("Error executing sync operation: " + e.getMessage());                        log.throwing(getClass().getName(), "execSyncOperation", e);                        status[0] = new Sync4jOperationStatusError(operation, sources[1], cmd, e);                    }                } else if (operation.isBOperation()) {                    cmd = (ModificationCommand)syncItemA.getPropertyValue(SyncItemHelper.PROPERTY_COMMAND);                    syncItemB.setProperty(syncItemA.getProperty(SyncItem.PROPERTY_TIMESTAMP)); // this contains the                                                                                               // current sync                    try {                        syncItemB = sources[0].setSyncItem(owner, syncItemA);                        operation.setSyncItemB(syncItemB);                                                status[0] = new Sync4jOperationStatusOK(operation, sources[1], cmd, StatusCode.ITEM_ADDED);                                            } catch (SyncException e) {                        log.severe("Error executing sync operation: " + e.getMessage());                        log.throwing(getClass().getName(), "execSyncOperation", e);                        status[0] = new Sync4jOperationStatusError(operation, sources[0], cmd, e);                    }                }                break;            case SyncOperation.UPDATE:                status = new SyncOperationStatus[1];                if (operation.isAOperation()) {                    cmd = (ModificationCommand)syncItemA.getPropertyValue(SyncItemHelper.PROPERTY_COMMAND);                    try {                        syncItemA = sources[1].setSyncItem(owner, syncItemB);                        operation.setSyncItemA(syncItemA);                        status[0] = new Sync4jOperationStatusOK(operation, sources[1], cmd);                    } catch (SyncException e) {                        log.severe("Error executing sync operation: " + e.getMessage());                        log.throwing(getClass().getName(), "execSyncOperation", e);                        status[0] = new Sync4jOperationStatusError(operation, sources[1], cmd, e);                    }                } else if (operation.isBOperation()) {                    cmd = (ModificationCommand)syncItemA.getPropertyValue(SyncItemHelper.PROPERTY_COMMAND);                    syncItemB.setProperty(syncItemA.getProperty(SyncItem.PROPERTY_TIMESTAMP)); // this contains the                                                                                               // current sync                    try {                        syncItemB = sources[0].setSyncItem(owner, syncItemA);                        operation.setSyncItemB(syncItemB);                        status[0] = new Sync4jOperationStatusOK(operation, sources[0], cmd);                    } catch (SyncException e) {                        log.severe("Error executing sync operation: " + e.getMessage());                        log.throwing(getClass().getName(), "execSyncOperation", e);                        status[0] = new Sync4jOperationStatusError(operation, sources[0], cmd, e);                    }                }                break;            case SyncOperation.DELETE:                //                // How many status we need? One if we have to delete the item                // from only one source, two if we have to delete it from both                // sources                //                size = 1;                if (operation.isAOperation() && operation.isBOperation()) {                    size = 2;                }                status = new SyncOperationStatus[size];                                                s = 0;                if (operation.isBOperation()) {                    cmd = (ModificationCommand)syncItemA.getPropertyValue(SyncItemHelper.PROPERTY_COMMAND);                    syncItemB.setProperty(syncItemA.getProperty(SyncItem.PROPERTY_TIMESTAMP)); // this contains the                                                                                               // timestamp of the                                                                                                // current sync                    try {                        sources[0].removeSyncItem(owner, syncItemB);                        status[s++] = new Sync4jOperationStatusOK(operation, sources[0], cmd);                    } catch (SyncException e) {                        log.severe("Error executing sync operation: " + e.getMessage());                        log.throwing(getClass().getName(), "execSyncOperation", e);                        status[s++] = new Sync4jOperationStatusError(operation, sources[0], cmd, e);                    }                }                 if (operation.isAOperation()) {                    cmd = (ModificationCommand)syncItemA.getPropertyValue(SyncItemHelper.PROPERTY_COMMAND);                                                           try {                        sources[1].removeSyncItem(owner, syncItemA);                        status[s++] = new Sync4jOperationStatusOK(operation, sources[1], cmd);                    } catch (SyncException e) {                        log.severe("Error executing sync operation: " + e.getMessage());                        log.throwing(getClass().getName(), "execSyncOperation", e);                        status[s++] = new Sync4jOperationStatusError(operation, sources[1], cmd, e);                    }                }                break;            case SyncOperation.NOP:                //                // A NOP operation can be due by one of the following conditions                // (see checkOperation(...)):                //                // 1. both items are flagged as "Deleted"                // 2. both items are flagged "Synchronized"                // 3. itemA is "Not existing" and itemB is "Not existing" or "Deleted"                //                // In case 1. we want a status is returned, thought no real                 // action is performed.                //                s = 0;                if (operation.isAOperation()) ++s;                if (operation.isBOperation()) ++s;                                status = new SyncOperationStatus[size];                                if (operation.isBOperation()) {                    cmd = (ModificationCommand)syncItemA.getPropertyValue(SyncItemHelper.PROPERTY_COMMAND);                    status[s++] = new Sync4jOperationStatusOK(operation, sources[0], cmd);                }                                if (operation.isAOperation()) {                    cmd = (ModificationCommand)syncItemA.getPropertyValue(SyncItemHelper.PROPERTY_COMMAND);                    status[s++] = new Sync4jOperationStatusOK(operation, sources[1], cmd);                }                break;            case SyncOperation.CONFLICT:                //                // The conflic is solved with data from server                //                try {                    SyncConflict sc = (SyncConflict)operation;                                        cmd = (ModificationCommand)syncItemA.getPropertyValue(SyncItemHelper.PROPERTY_COMMAND);                    //                    //Type CX means that itemA is in state CONFLICT and itemB is in state NOT_EXISTING                    //This situation happen when itemA has already a twin mapped                    //                    if (sc.getType().equals("CX")) {                                              sources[1].removeSyncItem(owner, syncItemA);                    } else {                        syncItemA = sources[1].setSyncItem(owner, syncItemB);                        operation.setSyncItemA(syncItemA);                    }                    operation.setAOperation(true);                                        status = new SyncOperationStatus[1];                    status[0] = new Sync4jOperationStatusConflict(                                    operation,                                     sources[1],                                     cmd,                                     StatusCode.CONFLICT_RESOLVED_WITH_SERVER_DATA                                );                } catch (SyncException e) {                    log.severe("Error executing sync operation: " + e.getMessage());                    log.throwing(getClass().getName(), "execSyncOperation", e);                    status[0] = new Sync4jOperationStatusError(operation, sources[1], cmd, e);                }                                break;        }  // end switch                return status;    }        // --------------------------------------------------------- Private methods        /**     * If any item from the client has not a corresponding mapping, the      * server source must be queried for the item, since the client item      * could be the same of an existing server item. In this case,  the old     * unmapped item is replaced in Ma by the newly mapped item.     *     * @param newlyMappedItems the collection that contains all the checked items     * @param source the source that has to be queried for twins     * @param syncItems the items to be searched if not mapped     * @param principal the principal items are belonging to     *     */    private void fixMappedItems(Collection newlyMappedItems,                                SyncItem[] syncItems       ,                                 SyncSource source          ,                                Principal  principal       ,                                boolean    isSlowSync      ) {                SyncItem itemB = null;                for (int i = 0; ((syncItems != null) && (i<syncItems.length)); ++i) {            itemB = null;            if (!syncItems[i].isMapped()) {                try {                    itemB = source.getSyncItemFromTwin(principal, syncItems[i]);                } catch (SyncSourceException e) {                      String msg = "Error retrieving the twin item of "                                 + syncItems[i].getKey()                                 + " from source "                                 + source                                 + ": "                                 + e.getMessage();                      log.severe(msg);                      log.throwing(getClass().getName(), "fixSyncMapping", e);                }                if (itemB != null) {                    if (isSlowSync) {                        //                        //if slow sync and if there is a SyncItem twin of the input SyncItem                        //verify if the mapping already exist into list of array mapped                        //if mapping exists then there is a conflict                        //                        if(!isContained(newlyMappedItems, itemB)) {                            SyncItemMapping mapping = new SyncItemMapping(itemB.getKey());                            mapping.setMapping(syncItems[i], itemB);                            newlyMappedItems.add(mapping);                            syncItems[i] = SyncItemHelper.newMappedSyncItem(itemB.getKey(), syncItems[i]);                        } else {                            itemB = syncItems[i];                            itemB.setState(SyncItemState.CONFLICT);                            SyncItemMapping mapping = new SyncItemMapping(itemB.getKey());                            mapping.setMapping(syncItems[i], itemB);                            newlyMappedItems.add(mapping);                                                        syncItems[i] = SyncItemHelper.newMappedSyncItem(itemB.getKey(), syncItems[i]);                        }                    } else {                        SyncItemMapping mapping = new SyncItemMapping(itemB.getKey());                        mapping.setMapping(syncItems[i], itemB);                        newlyMappedItems.add(mapping);                        syncItems[i] = SyncItemHelper.newMappedSyncItem(itemB.getKey(), syncItems[i]);                    }                }            }        }  // next i    }        /**     * Verify if input item is already present into list of mapped items     *      * @param newlyMappedItems list updated of items     * @param itemTwin item to research     *     * @return true if item presents into list else false      */    private boolean isContained(Collection newlyMappedItems, SyncItem itemTwin) {        Iterator it2 = newlyMappedItems.iterator();        while (it2.hasNext()) {            SyncItemMapping map = (SyncItemMapping)it2.next();            SyncItem itemB = map.getSyncItemB();                                        if (itemB.equals(itemTwin))                return true;        }        return false;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -