00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 package org.openmobileis.database.fastobjectdb.synchro.client;
00030
00031 import java.io.File;
00032 import java.io.IOException;
00033
00034 import org.odbms.ObjectSet;
00035 import org.odbms.Query;
00036 import org.openmobileis.common.util.exception.BadDataFormatException;
00037 import org.openmobileis.database.DatabaseException;
00038 import org.openmobileis.database.fastobjectdb.FODBCollectionDescriptor;
00039 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00040 import org.openmobileis.database.fastobjectdb.db.FODBCollection;
00041 import org.openmobileis.database.fastobjectdb.db.exception.FODBException;
00042 import org.openmobileis.database.fastobjectdb.db.index.FODBIndex;
00043 import org.openmobileis.database.fastobjectdb.db.index.FODBStringIndex;
00044 import org.openmobileis.database.fastobjectdb.db.index.FODBUniqueIndex;
00045 import org.openmobileis.database.fastobjectdb.db.index.node.Node;
00046 import org.openmobileis.database.fastobjectdb.db.store.FODBMainFile;
00047 import org.openmobileis.synchro.client.SynchroManager;
00048 import org.openmobileis.synchro.client.SynchroProcessor;
00049 import org.openmobileis.synchro.journal.JournalManager;
00050 import org.openmobileis.synchro.journal.SimpleLogRenderer;
00051 import org.openmobileis.synchro.openmsp.client.OpenMSPSynchroManager;
00052 import org.openmobileis.synchro.openmsp.client.db.ActionDB;
00053 import org.openmobileis.synchro.openmsp.client.db.SyncActionDBManager;
00054
00063 public class SynchroFastObjectDB extends FastObjectDB {
00064 private static SynchroFODBConnector syncConnector;
00074 private SynchroFastObjectDB(String dbRootDir, String dbName, FODBMainFile fmf, int action)
00075 throws BadDataFormatException, FODBException {
00076 super(dbRootDir, dbName, fmf, action);
00077 }
00078
00091 public static FastObjectDB open(String dbRootDir, String dbName) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00092 String dirPath = "" + dbRootDir + "/" + dbName;
00093 String completeFilePath = dirPath + "/" + dbName + ".db";
00094 File file = new File(completeFilePath);
00095 FastObjectDB db = null;
00096 SynchroFastObjectDB.initDefaultOpenMSPSynchro();
00097 if (file.exists()) {
00098 FODBMainFile fmf = FODBMainFile.openDbFile(completeFilePath);
00099 db = new SynchroFastObjectDB(dbRootDir, dbName, fmf, ACTION_OPEN);
00100 } else {
00101
00102 File dirFile = new File(dirPath);
00103 if (!dirFile.exists()) {
00104 dirFile.mkdirs();
00105 }
00106 FODBMainFile fmf = FODBMainFile.createDbFile(completeFilePath);
00107 db = new SynchroFastObjectDB(dbRootDir, dbName, fmf, ACTION_CREATE);
00108 }
00109 return db;
00110 }
00111
00112 public static void registerSynchroFODBConnector(SynchroFODBConnector connector) {
00113 SynchroFastObjectDB.syncConnector = connector;
00114 }
00115
00129 public boolean createCollection(FODBCollectionDescriptor descriptor) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00130 boolean ret = super.createCollection(descriptor);
00131 if (ret) {
00132 if (descriptor.isSynchronize()) {
00133 syncConnector.registerCollection(descriptor.getCollectionName());
00134 }
00135 }
00136 return ret;
00137 }
00138
00151 public boolean createCollection(String collectionName, Class objectType) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00152 return this.createCollection(new FODBCollectionDescriptor(collectionName, objectType));
00153 }
00154
00167 protected boolean openCollection(String collectionName) throws IOException, ClassNotFoundException, BadDataFormatException, FODBException {
00168 boolean ret = super.openCollection(collectionName);
00169 if (ret) {
00170 FODBCollection collection = this.getCollection(collectionName);
00171 if (collection.isSynchronized()) {
00172 syncConnector.registerCollection(collectionName);
00173 }
00174 }
00175 return ret;
00176 }
00177
00184 public void registerSynchroFODBReturnListener(String collectionName, SynchroFODBReturnListener listener) {
00185 syncConnector.registerSynchroFODBReturnListener(collectionName, listener);
00186 }
00187
00195 public void registerJournalLogRenderer(String collectionName, String journalinfo) {
00196 JournalManager.getManager().registerJournalLogRenderer(new SimpleLogRenderer(collectionName, journalinfo));
00197 }
00198
00207 public boolean isObjectInCollection(String colName, Object obj) throws DatabaseException {
00208 FODBCollection collect = colManager.getCollectionByName(colName);
00209 if (collect != null) {
00210 try {
00211 this.getTransactionManager().begin();
00212 try {
00213 this.getTransactionManager().enterTransaction(collect.getName());
00214 FODBUniqueIndex index = collect.getCollectionIdIndex();
00215 Object key = ((FODBIndex)index).getKey(obj);
00216 long pos = index.getKeyPosition(key);
00217 return (pos != Node.NO_NODE);
00218 } finally {
00219 this.getTransactionManager().commit();
00220 }
00221 } catch (Throwable ex) {
00222 throw new DatabaseException(ex);
00223 }
00224 }
00225 return false;
00226 }
00227
00237 public Object getObjectFromCollectionWithId(String colName, String id) throws DatabaseException {
00238 FODBCollection collect = colManager.getCollectionByName(colName);
00239 if (collect != null) {
00240 try {
00241 FODBUniqueIndex index = collect.getCollectionIdIndex();
00242 Query q = this.query();
00243 q.constrain(collect.getCollectionObjectClass());
00244 Query subq = q.descend(((FODBIndex)index).getIndexHeader().getMemberName());
00245 subq.constrain(id).equal();
00246 ObjectSet set = q.execute();
00247 if (set.hasNext()) {
00248 return set.next();
00249 } else {
00250 return null;
00251 }
00252 } catch (Throwable ex) {
00253 throw new DatabaseException(ex);
00254 }
00255 }
00256 return null;
00257 }
00258
00272 public boolean addFODB(String colName, Object obj) throws FODBException {
00273 return super.add(colName, obj);
00274 }
00275
00284 public boolean add(String colName, Object obj) throws FODBException {
00285 boolean ret = false;
00286 this.getTransactionManager().begin();
00287 try {
00288 ret = super.add(colName, obj);
00289 if (ret) {
00290 FODBCollection col = this.colManager.getCollectionByName(colName);
00291 if (col.isSynchronized()) {
00292 this.getTransactionManager().enterTransaction("FWKACTIONDB");
00293 FODBIndex index = (FODBIndex)col.getCollectionIdIndex();
00294 if ((index != null) && (index instanceof FODBStringIndex)){
00295 String key = (String)((FODBStringIndex)index).getKeySensitive(obj, false);
00296 SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key, ActionDB.ADD_ACTION));
00297 } else {
00298 col.deleteSingleElement(obj);
00299 throw new FODBException("No string unique index. Object not Synchronized, cancel db writing");
00300 }
00301 }
00302 }
00303 } finally {
00304 this.getTransactionManager().commit();
00305 }
00306 return ret;
00307 }
00308
00319 public boolean replaceFODB(String colName, Object obj) throws FODBException {
00320 return super.replace(colName, obj);
00321 }
00322
00331 public boolean replace(String colName, Object obj) throws FODBException {
00332 boolean ret = false;
00333 this.getTransactionManager().begin();
00334 try {
00335 ret = super.replace(colName, obj);
00336 if (ret) {
00337 FODBCollection col = this.colManager.getCollectionByName(colName);
00338 if (col.isSynchronized()) {
00339 this.getTransactionManager().enterTransaction("FWKACTIONDB");
00340 FODBIndex index = (FODBIndex)col.getCollectionIdIndex();
00341 if ((index != null) && (index instanceof FODBStringIndex)){
00342 String key = (String)((FODBStringIndex)index).getKeySensitive(obj, false);
00343 SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key, ActionDB.UPDATE_ACTION));
00344 } else {
00345 col.deleteSingleElement(obj);
00346 throw new FODBException("No string unique index. Object not Synchronized, cancel db writing");
00347 }
00348 }
00349 }
00350 } finally {
00351 this.getTransactionManager().commit();
00352 }
00353 return ret;
00354 }
00355
00365 public boolean delete(String colName, Object obj) throws FODBException {
00366 boolean ret = false;
00367 this.getTransactionManager().begin();
00368 try {
00369 ret = super.delete(colName, obj);
00370 if (ret) {
00371 FODBCollection col = this.colManager.getCollectionByName(colName);
00372 if (col.isSynchronized()) {
00373 this.getTransactionManager().enterTransaction("FWKACTIONDB");
00374 FODBIndex index = (FODBIndex)col.getCollectionIdIndex();
00375 if ((index != null) && (index instanceof FODBStringIndex)){
00376 String key = (String)((FODBStringIndex)index).getKeySensitive(obj, false);
00377 SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, key, ActionDB.DELETE_ACTION));
00378 } else {
00379 throw new FODBException("No string unique index. Object not Synchronized, cancel db writing");
00380 }
00381 }
00382 }
00383 } finally {
00384 this.getTransactionManager().commit();
00385 }
00386 return ret;
00387 }
00388
00399 public boolean deleteWithIdFODB(String colName, Object id) throws FODBException {
00400 return super.deleteWithId(colName, id);
00401 }
00411 public boolean deleteWithId(String colName, Object id) throws FODBException {
00412 boolean ret = false;
00413 this.getTransactionManager().begin();
00414 try {
00415 ret = super.deleteWithId(colName, id);
00416 if (ret) {
00417 FODBCollection col = this.colManager.getCollectionByName(colName);
00418 if (col.isSynchronized()) {
00419 this.getTransactionManager().enterTransaction("FWKACTIONDB");
00420 FODBIndex index = (FODBIndex)col.getCollectionIdIndex();
00421 if ((index != null) && (index instanceof FODBStringIndex)){
00422 SyncActionDBManager.getManager().addActionDB(new ActionDB(colName, (String)id, ActionDB.DELETE_ACTION));
00423 } else {
00424 throw new FODBException("No string unique index. Object not Synchronized, cancel db writing");
00425 }
00426 }
00427 }
00428 } finally {
00429 this.getTransactionManager().commit();
00430 }
00431 return ret;
00432 }
00433
00434 private static void initDefaultOpenMSPSynchro() {
00435 SynchroProcessor openMSPproc = SynchroManager.getManager().getSynchroProcessor(OpenMSPSynchroManager.OPENMSPPROCESSORNAME);
00436 if (openMSPproc == null) {
00437 SynchroManager.getManager().registerSynchroProcessor(OpenMSPSynchroManager.getManager());
00438 }
00439 if (syncConnector == null) {
00440 syncConnector = new DefaultOpenMSPSynchroFODBConnector();
00441 }
00442
00443 }
00444
00445 }