FODBTerminalUserFactory.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
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  *  2005 Modification F.Salque
00027  *
00028  */
00029 package org.openmobileis.modules.common.database.fodb;
00030 
00031 import org.odbms.ObjectSet;
00032 import org.odbms.Query;
00033 import org.openmobileis.database.fastobjectdb.FODBIndexDescriptor;
00034 import org.openmobileis.database.fastobjectdb.FODBStringIndexDescriptor;
00035 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00036 import org.openmobileis.database.fastobjectdb.FastObjectDBManager;
00037 import org.openmobileis.database.fastobjectdb.db.exception.FODBException;
00038 import org.openmobileis.database.fastobjectdb.synchro.client.SynchroFastObjectDB;
00039 import org.openmobileis.common.context.SessionContext;
00040 import org.openmobileis.common.context.SessionContextManager;
00041 import org.openmobileis.common.util.PropertiesManager;
00042 import org.openmobileis.common.util.PersistentPropertiesManager;
00043 import org.openmobileis.modules.common.data.TerminalUser;
00044 import org.openmobileis.modules.common.data.TerminalUserFactory;
00045 import org.openmobileis.common.util.exception.BadDataFormatException;
00046 import org.openmobileis.common.util.exception.DatabaseException;
00047 import org.openmobileis.common.util.exception.ServiceException;
00048 import org.openmobileis.common.util.log.LogManager;
00049 import org.openmobileis.common.util.log.LogServices;
00050 
00051 public abstract class FODBTerminalUserFactory implements TerminalUserFactory {
00052 
00056   public FODBTerminalUserFactory() {
00057     super();
00058     String dbPath = PropertiesManager.getManager().getProperty("fastobjectdb.database.path");
00059     if (dbPath == null) {
00060       LogManager.traceError(0, "TerminalUser Factory FODB ERROR : FODB path property is not set. Can't create db");
00061     }
00062     try {
00063       FastObjectDB db = FastObjectDBManager.getManager().getCurrentFODB();
00064       if (!db.isCollectionExist(this.getCollectionName())) {
00065         db.createCollection(this.getCollectionName(), this.getDataType());
00066         FODBStringIndexDescriptor IDDescriptor = new FODBStringIndexDescriptor("ID", FODBIndexDescriptor.UNIQUE, "getTerminalUserId()", 12, this
00067             .getMaxTerminalUserIdLength());
00068         db.addIndex(this.getCollectionName(), IDDescriptor);
00069 
00070         this.initDB(db);
00071       }
00072       // always register notification listener because it's not persistent.
00073       ((SynchroFastObjectDB) db).registerSynchroFODBReturnListener(this.getCollectionName(), this.getSynchroListener());
00074       // register the log renderer.
00075       ((SynchroFastObjectDB) db).registerJournalLogRenderer(this.getCollectionName(), "Users");
00076       this.updateSession();
00077 
00078     } catch (Throwable ex) {
00079       LogManager.traceError(0, "TerminalUser Factory FODB ERROR : Unknown error during creation");
00080       LogManager.traceError(0, ex);
00081     }
00082   }
00083 
00084   private void updateSession() {
00085     SessionContext context = (SessionContext) SessionContextManager.getManager().getSessionContext();
00086     TerminalUser rep = this.getInstallTerminalUser();
00087     if (rep != null) {
00088       context.setUserId(rep.getTerminalUserId());
00089       context.setUserLocale(rep.getLangue());
00090     }
00091   }
00092 
00093   public String[] getTerminalUserIdsList() {
00094     try {
00095       TerminalUser[] repList = this.getAllTerminalUsers();
00096       String[] repIds = new String[repList.length];
00097       for (int i = 0; i < repList.length; i++) {
00098         repIds[i] = repList[i].getTerminalUserId();
00099       }
00100       return repIds;
00101     } catch (Throwable ex) {
00102       LogManager.traceError(0, ex);
00103     }
00104     return new String[0];
00105   }
00106 
00107   public TerminalUser getTerminalUser(String id) {
00108     try {
00109       Query q = FastObjectDBManager.getManager().getCurrentFODB().query();
00110       q.constrain(this.getDataType());
00111       Query subq = q.descend("getTerminalUserId()");
00112       subq.constrain(id).equal();
00113       ObjectSet set = q.execute();
00114       if (set.hasNext()) {
00115         return (TerminalUser) set.next();
00116       } else {
00117         return null;
00118       }
00119     } catch (Exception e) {
00120       LogManager.traceError(LogServices.DATABASESERVICE, e);
00121     }
00122     return null;
00123   }
00124 
00125   public TerminalUser getInstallTerminalUser() {
00126     String repId = PersistentPropertiesManager.getManager().getProperty("FWKTerminalUser", "installrepid");
00127     if (repId != null) {
00128       TerminalUser rep = this.getTerminalUser(repId);
00129       return rep;
00130     }
00131     return null;
00132   }
00133 
00134   public void setInstallTerminalUser(String id) throws ServiceException, DatabaseException {
00135     PersistentPropertiesManager.getManager().saveProperty("FWKTerminalUser", "installrepid", id);
00136   }
00137 
00138   public TerminalUser[] getAllTerminalUsers() throws DatabaseException {
00139     try {
00140       Query q = FastObjectDBManager.getManager().getCurrentFODB().query();
00141       q.constrain(this.getDataType());
00142       q.descend("getTerminalUserId()");
00143       ObjectSet set = q.execute();
00144       TerminalUser[] repList = new TerminalUser[set.size()];
00145       int i = 0;
00146       while (set.hasNext()) {
00147         repList[i++] = (TerminalUser) set.next();
00148       }
00149       return repList;
00150     } catch (Exception e) {
00151       LogManager.traceError(LogServices.DATABASESERVICE, e);
00152     }
00153     return new TerminalUser[0];
00154   }
00155 
00159   public void addTerminalUser(TerminalUser rep) throws ServiceException, DatabaseException {
00160     TerminalUser dbrep = this.getTerminalUser(rep.getTerminalUserId());
00161     try {
00162       if (dbrep == null) {
00163         FastObjectDBManager.getManager().getCurrentFODB().add(this.getCollectionName(), rep);
00164       } else {
00165         FastObjectDBManager.getManager().getCurrentFODB().replace(this.getCollectionName(), rep);
00166       }
00167     } catch (Throwable ex) {
00168       throw new ServiceException(ex);
00169     }
00170   }
00171 
00172   public void removeTerminalUser(String repId) throws ServiceException, DatabaseException {
00173     try {
00174       FastObjectDBManager.getManager().getCurrentFODB().deleteWithId(this.getCollectionName(), repId);
00175     } catch (Throwable ex) {
00176       throw new ServiceException(ex);
00177     }
00178   }
00179 
00180   protected TerminalUserSynchroFODBReturnListener getSynchroListener() {
00181     return new TerminalUserSynchroFODBReturnListener();
00182   }
00183 
00184   public String getCollectionName() {
00185     return "terminaluser";
00186   }
00187 
00188   protected abstract int getMaxTerminalUserIdLength();
00189 
00190   protected abstract Class getDataType();
00191 
00192   protected abstract void initDB(FastObjectDB db) throws FODBException, BadDataFormatException;
00193 }

Generated on Mon Jan 14 17:29:46 2008 for OpenMobileIS by  doxygen 1.5.4