AccountFactory.java

00001 
00025 package org.openmobileis.examples.mycrm.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.mycrm.data.Account;
00041 
00042 public final class AccountFactory {
00043 
00044   private static AccountFactory manager;
00045 
00046   private AccountFactory() {
00047     super();
00048     this.initDB();
00049   }
00050 
00051   public static AccountFactory getManager() {
00052     if (manager == null) {
00053       synchronized (AccountFactory.class) {
00054         if (manager == null) {
00055           manager = new AccountFactory();
00056           //register the manager to avoid class garbage.
00057           ApplicationContextManager.getManager().addManager(manager);
00058         }
00059       }
00060     }
00061     return manager;
00062   }
00063 
00064   private void initDB() {
00065     try {
00066       FastObjectDB db = FastObjectDBManager.getCurrentFODB();
00067       // test if the collection has already been created.
00068       //if not create it. Created collection are automatiquely 
00069       // loaded when FODB is initialized.
00070       if (!db.isCollectionExist("account")) {
00071         //SimpleOpenMISInit intialize the synchronized version of FODB.
00072         // all created collection are synchronized by default.
00073         // Synchro listener are automatiquely registered when the collection is created or loaded.
00074         // use the FODBCollectionDescriptor to define a collection that is not synchronized.
00075         FODBCollectionDescriptor coldes = new FODBCollectionDescriptor("account", Account.class);
00076         db.createCollection(coldes);
00077         //add index to query the collection by id
00078         FODBStringIndexDescriptor IDDescriptor = new FODBStringIndexDescriptor("ID", FODBIndexDescriptor.UNIQUE, "getId()", 12, 26);
00079         db.addIndex("account", IDDescriptor);
00080         //add index to query the collection by name
00081         FODBStringIndexDescriptor NameDescriptor = new FODBStringIndexDescriptor("NAME", FODBIndexDescriptor.MULTIPLE, "getName()", 12, 50, 14);
00082         db.addIndex("account", NameDescriptor);
00083       }
00084       //register the log renderer.
00085       //logrenderer are use to show synchronization result for this collection.
00086       ((SynchroFastObjectDB) db).registerJournalLogRenderer("account", "Account synchronization :");
00087 
00088     } catch (Throwable ex) {
00089       LogManager.traceError(0, "account Factory FODB ERROR : Unknown error during creation");
00090       LogManager.traceError(0, ex);
00091     }
00092   }
00093 
00094   public Account getAccount(String id) throws DatabaseException {
00095     Query q = FastObjectDBManager.getCurrentFODB().query();
00096     q.constrain(Account.class);
00097     Query subq = q.descend("getId()");
00098     subq.constrain(id).equal();
00099     ObjectSet set = q.execute();
00100     if (set.hasNext()) {
00101       return (Account) set.next();
00102     } else {
00103       return null;
00104     }
00105   }
00106 
00107   public Array getAccountByName(String name) throws DatabaseException {
00108     Query q = FastObjectDBManager.getCurrentFODB().query();
00109     q.constrain(Account.class);
00110     Query subq = q.descend("getName()");
00111     subq.constrain(name).like();
00112     ObjectSet set = q.execute(); 
00113     return ((FODBSodaObjectSet)set).getArrayFromSet();
00114   }
00115 
00119   public void storeAccount(Account account) throws DatabaseException {
00120     Account dbaccount = this.getAccount(account.getId());
00121     if (dbaccount == null) {
00122       FastObjectDBManager.getCurrentFODB().add("account", account);
00123     } else {
00124       FastObjectDBManager.getCurrentFODB().replace("account", account);
00125     }
00126   }
00127 
00128 }

Generated on Mon Dec 4 11:03:25 2006 for OpenMobileIS by  doxygen 1.5.1-p1