Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

Sync4jFODBSyncSource.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  *  2004 Modified by Romain Beaugrand
00027  * 
00028  */
00029 package org.openmobileis.synchro.sync4j.client;
00030 
00031 import java.io.ByteArrayInputStream;
00032 import java.io.ByteArrayOutputStream;
00033 import java.io.IOException;
00034 import java.io.ObjectInputStream;
00035 import java.io.ObjectOutputStream;
00036 import java.security.Principal;
00037 import java.util.Date;
00038 
00039 import org.openmobileis.common.util.collection.Array;
00040 import org.openmobileis.common.util.log.LogManager;
00041 import org.openmobileis.database.fastobjectdb.FastObjectDBManager;
00042 import org.openmobileis.database.fastobjectdb.synchro.client.SynchroFODBReturnListener;
00043 import org.openmobileis.database.fastobjectdb.synchro.client.SynchroFastObjectDB;
00044 import org.openmobileis.synchro.openmsp.client.db.ActionDB;
00045 import org.openmobileis.synchro.openmsp.client.db.SyncActionDBManager;
00046 
00047 import sync4j.framework.tools.Base64;
00048 import sync4j.syncclient.spds.SyncException;
00049 import sync4j.syncclient.spds.engine.SyncItem;
00050 import sync4j.syncclient.spds.engine.SyncItemImpl;
00051 import sync4j.syncclient.spds.engine.SyncItemProperty;
00052 import sync4j.syncclient.spds.engine.SyncItemState;
00053 import sync4j.syncclient.spds.engine.SyncSource;
00054 
00063 public final class Sync4jFODBSyncSource implements SyncSource {
00064   private static final int UNKNOWN=-1;
00065   
00066         private SynchroFODBReturnListener synchroListener;
00067   private String name;
00068   private String type;
00069   private String sourceURI;
00070 
00074   public Sync4jFODBSyncSource() {
00075     super();
00076     // TODO Auto-generated constructor stub
00077   }
00078   
00079   public void registerSynchroListener(SynchroFODBReturnListener listener)       {
00080         this.synchroListener = listener;
00081   }
00082 
00083   public String getName() {
00084       return name;
00085   }
00086   public void setName(String name) {
00087       this.name = name;
00088   }
00089 
00090 
00094   public String getSourceURI() {
00095       return sourceURI;
00096   }
00097 
00101   public void setSourceURI(String sourceURI) {
00102       this.sourceURI = sourceURI;
00103   }
00104 
00105   public String getType() {
00106       return this.type;
00107   }
00108   
00109   public void setType(String type) {
00110       this.type = type;
00111   }
00112 
00113   /* (non-Javadoc)
00114    * @see sync4j.syncclient.spds.engine.SyncSource#getAllSyncItems(java.security.Principal)
00115    */
00116   public SyncItem[] getAllSyncItems(Principal arg0) throws SyncException {
00117     return this.getSyncItemForState(UNKNOWN);
00118   }
00119 
00120   /* (non-Javadoc)
00121    * @see sync4j.syncclient.spds.engine.SyncSource#getDeletedSyncItems(java.security.Principal, java.util.Date)
00122    */
00123   public SyncItem[] getDeletedSyncItems(Principal arg0, Date arg1) throws SyncException {
00124     return this.getSyncItemForState(ActionDB.DELETE_ACTION);
00125   }
00126 
00127   /* (non-Javadoc)
00128    * @see sync4j.syncclient.spds.engine.SyncSource#getNewSyncItems(java.security.Principal, java.util.Date)
00129    */
00130   public SyncItem[] getNewSyncItems(Principal arg0, Date arg1) throws SyncException {
00131     return this.getSyncItemForState(ActionDB.ADD_ACTION);
00132   }
00133 
00134   /* (non-Javadoc)
00135    * @see sync4j.syncclient.spds.engine.SyncSource#getUpdatedSyncItems(java.security.Principal, java.util.Date)
00136    */
00137   public SyncItem[] getUpdatedSyncItems(Principal arg0, Date arg1) throws SyncException {
00138     return this.getSyncItemForState(ActionDB.UPDATE_ACTION);
00139   }
00140 
00141   /* (non-Javadoc)
00142    * @see sync4j.syncclient.spds.engine.SyncSource#removeSyncItem(java.security.Principal, sync4j.syncclient.spds.engine.SyncItem)
00143    */
00144   public void removeSyncItem(Principal arg0, SyncItem syncItem) throws SyncException {
00145     try {
00146             String colid = syncItem.getKey().getKeyAsString();
00147             String colname = null;
00148             int index = colid.indexOf('/');
00149             if (index != -1)    {
00150               colname = colid.substring(0, index);
00151               colid = colid.substring(index+1);
00152             } else      {
00153               throw new SyncException("Bad UID encoding for removeSyncItem.");
00154             }
00155                         SynchroFastObjectDB db = (SynchroFastObjectDB)FastObjectDBManager.getCurrentFODB();
00156                         db.deleteWithIdFODB(colname, colid);
00157                 } catch (Throwable ex)  {
00158                         LogManager.traceError(0,ex);
00159                         throw new SyncException(ex.getMessage());
00160                 }       
00161  }
00162 
00163   /* (non-Javadoc)
00164    * @see sync4j.syncclient.spds.engine.SyncSource#setSyncItem(java.security.Principal, sync4j.syncclient.spds.engine.SyncItem)
00165    */
00166   public SyncItem setSyncItem(Principal arg0, SyncItem syncItem) throws SyncException {
00167     try {
00168             String colid = syncItem.getKey().getKeyAsString();
00169             String colname = null;
00170             int index = colid.indexOf('/');
00171             if (index != -1)    {
00172               colname = colid.substring(0, index);
00173               colid = colid.substring(index+1);
00174             } else      {
00175               throw new SyncException("Bad UID encoding for setSyncItem.");
00176             }
00177             byte[] fileContent =
00178                 (byte[])syncItem.getPropertyValue(SyncItem.PROPERTY_BINARY_CONTENT);
00179             fileContent = Base64.decode(fileContent);
00180             Object newObj = convertSyncDataToObject(fileContent);
00181                         if (this.synchroListener !=null)        {
00182                                 Object newOne = this.synchroListener.notifyBeginDBUpdate(newObj);
00183                                 if (newOne != null)     {
00184                                  newObj = newOne;
00185                                 }
00186                         }
00187                         SynchroFastObjectDB db = (SynchroFastObjectDB)FastObjectDBManager.getCurrentFODB();
00188                         boolean exist = db.isObjectInCollection(colname, newObj);
00189                         if (exist)      {
00190                                 db.replaceFODB(colname, newObj);
00191                         } else  {
00192                                 db.addFODB(colname, newObj);
00193                         }
00194       SyncItem newSyncItem = new SyncItemImpl(this, colid, SyncItemState.NEW);
00195       newSyncItem.setProperties(syncItem.getProperties());
00196 
00197       return newSyncItem;               
00198     } catch (Throwable ex)      {
00199                         LogManager.traceError(0,ex);
00200                         throw new SyncException(ex.getMessage());
00201                 }       
00202   }
00203 
00204   /* (non-Javadoc)
00205    * @see sync4j.syncclient.spds.engine.SyncSource#beginSync(int)
00206    */
00207   public void beginSync(int arg0) throws SyncException {
00208 
00209   }
00210 
00211   /* (non-Javadoc)
00212    * @see sync4j.syncclient.spds.engine.SyncSource#commitSync()
00213    */
00214   public void commitSync() throws SyncException {
00215 
00216   }
00217   
00218   protected char convertActionStateToSyncItem(ActionDB action)  {
00219     switch (action.getActionType())     {
00220         case ActionDB.ADD_ACTION :
00221           return SyncItemState.NEW;
00222         case ActionDB.UPDATE_ACTION :
00223           return SyncItemState.UPDATED;
00224         case ActionDB.DELETE_ACTION :
00225           return SyncItemState.DELETED;
00226         default :
00227           return SyncItemState.UNKNOWN;
00228     }
00229   }
00230   
00231   protected SyncItem encodeActionInSyncItem(ActionDB action) throws SyncException       {
00232     String objectUID = action.getActionObjectUID();
00233     SyncItemImpl syncItem = new SyncItemImpl(this, action.getActionServiceName()+"/"+objectUID, this.convertActionStateToSyncItem(action));
00234         try     {
00235           if (action.getActionType() != ActionDB.DELETE_ACTION) {
00236                         Object obj = ((SynchroFastObjectDB)FastObjectDBManager.getCurrentFODB()).getObjectFromCollectionWithId(action.getActionServiceName(), objectUID);
00237                                 ByteArrayOutputStream bstr = new ByteArrayOutputStream();
00238                                 ObjectOutputStream ostr = new ObjectOutputStream(bstr);
00239                                 ostr.writeObject(obj);
00240                                 byte[] b = bstr.toByteArray();
00241               syncItem.setProperty(
00242                   new SyncItemProperty(SyncItem.PROPERTY_BINARY_CONTENT         ,
00243                                        Base64.encode(b))
00244               );
00245           }
00246                 } catch (Throwable ex)  {
00247                         LogManager.traceError(0,ex);
00248                         throw new SyncException(ex.getMessage());
00249                 }       
00250                 return syncItem;
00251   }
00252   
00253   protected SyncItem[] getSyncItemForState(int state)   throws SyncException    {
00254     ActionDB[] actionArray = SyncActionDBManager.getManager().getActionsForService(this.getName());
00255     Array syncItemList = new Array(actionArray.length);
00256     for (int i=0; i<actionArray.length; i++)    {
00257       if ((state == UNKNOWN) || (actionArray[i].getActionType() == state)) {
00258         syncItemList.add(encodeActionInSyncItem(actionArray[i]));
00259       }
00260     }
00261     SyncItem[] ret = new SyncItem[syncItemList.size()];
00262     syncItemList.toArray(ret);
00263     return ret;
00264   }
00265   
00266         private Object convertSyncDataToObject(byte[] data) throws IOException , ClassNotFoundException {
00267                 ByteArrayInputStream bstr = new ByteArrayInputStream(data);
00268                 ObjectInputStream ostr = new ObjectInputStream(bstr);
00269                 return ostr.readObject();
00270         }
00271 
00272 }

Generated on Wed Dec 14 21:05:35 2005 for OpenMobileIS by  doxygen 1.4.4