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

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