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 package org.openmobileis.examples.simpleappli.data.FODB;
00026
00027 import org.odbms.ObjectSet;
00028 import org.odbms.Query;
00029 import org.openmobileis.common.context.ApplicationContextManager;
00030 import org.openmobileis.common.util.collection.Array;
00031 import org.openmobileis.common.util.exception.DatabaseException;
00032 import org.openmobileis.common.util.log.LogManager;
00033 import org.openmobileis.database.fastobjectdb.FODBCollectionDescriptor;
00034 import org.openmobileis.database.fastobjectdb.FODBIndexDescriptor;
00035 import org.openmobileis.database.fastobjectdb.FODBStringIndexDescriptor;
00036 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00037 import org.openmobileis.database.fastobjectdb.FastObjectDBManager;
00038 import org.openmobileis.database.fastobjectdb.db.query.soda.FODBSodaObjectSet;
00039 import org.openmobileis.database.fastobjectdb.synchro.client.SynchroFastObjectDB;
00040 import org.openmobileis.examples.simpleappli.data.BaseData;
00041
00051 public final class FODBBaseDataFactory {
00052 private static FODBBaseDataFactory manager;
00053
00057 private FODBBaseDataFactory() {
00058 super();
00059 this.initDB();
00060 }
00061
00062 public static FODBBaseDataFactory getManager() {
00063 if (manager == null) {
00064 synchronized (FODBBaseDataFactory.class) {
00065 if (manager == null) {
00066 manager = new FODBBaseDataFactory();
00067
00068 ApplicationContextManager.getManager().addManager(manager);
00069 }
00070 }
00071 }
00072 return manager;
00073 }
00074
00075 private void initDB() {
00076 try {
00077 FastObjectDB db = FastObjectDBManager.getCurrentFODB();
00078
00079
00080
00081 if (!db.isCollectionExist("basedata")) {
00082
00083
00084
00085
00086 FODBCollectionDescriptor coldes = new FODBCollectionDescriptor("basedata", BaseData.class);
00087 db.createCollection(coldes);
00088
00089 FODBStringIndexDescriptor IDDescriptor = new FODBStringIndexDescriptor("ID", FODBIndexDescriptor.UNIQUE, "getId()", 12, 26);
00090 db.addIndex("basedata", IDDescriptor);
00091 }
00092
00093
00094 ((SynchroFastObjectDB) db).registerJournalLogRenderer("basedata", "Base data synchronization :");
00095
00096 } catch (Throwable ex) {
00097 LogManager.traceError(0, "BaseData Factory FODB ERROR : Unknown error during creation");
00098 LogManager.traceError(0, ex);
00099 }
00100 }
00101
00102 public BaseData[] getAllBaseData() throws DatabaseException {
00103 Query q = FastObjectDBManager.getCurrentFODB().query();
00104 q.constrain(BaseData.class);
00105 q.descend("getId()");
00106 ObjectSet set = q.execute();
00107 Array ret = ((FODBSodaObjectSet)set).getArrayFromSet();
00108 BaseData[] datalist = new BaseData[ret.size()];
00109 ret.toArray(datalist);
00110 return datalist;
00111 }
00112
00113 public BaseData getBaseData(String id) throws DatabaseException {
00114 Query q = FastObjectDBManager.getCurrentFODB().query();
00115 q.constrain(BaseData.class);
00116 Query subq = q.descend("getId()");
00117 subq.constrain(id).equal();
00118 ObjectSet set = q.execute();
00119 if (set.hasNext()) {
00120 return (BaseData) set.next();
00121 } else {
00122 return null;
00123 }
00124 }
00125
00129 public void storeBaseData(BaseData data) throws DatabaseException {
00130 BaseData dbdata = this.getBaseData(data.getId());
00131 if (dbdata == null) {
00132 FastObjectDBManager.getCurrentFODB().add("basedata", data);
00133 } else {
00134 FastObjectDBManager.getCurrentFODB().replace("basedata", data);
00135 }
00136 }
00137
00138
00139 public void deleteBaseData (String id) throws DatabaseException {
00140 try {
00141 FastObjectDB db = FastObjectDBManager.getCurrentFODB();
00142 db.deleteWithId("basedata", id);
00143 } catch (Throwable ex) {
00144 throw new DatabaseException(ex);
00145 }
00146 }
00147 }