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.modules.crm.database.common.fodb;
00030
00031 import org.odbms.Constraint;
00032 import org.odbms.ObjectSet;
00033 import org.odbms.Query;
00034 import org.openmobileis.database.DatabaseException;
00035 import org.openmobileis.database.fastobjectdb.FODBIndexDescriptor;
00036 import org.openmobileis.database.fastobjectdb.FODBStringIndexDescriptor;
00037 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00038 import org.openmobileis.database.fastobjectdb.FastObjectDBManager;
00039 import org.openmobileis.database.fastobjectdb.db.exception.FODBException;
00040 import org.openmobileis.database.fastobjectdb.synchro.client.SynchroFastObjectDB;
00041 import org.openmobileis.embedded.util.ServicePropertiesManager;
00042 import org.openmobileis.common.context.SessionContext;
00043 import org.openmobileis.common.context.SessionContextManager;
00044 import org.openmobileis.common.util.PropertiesManager;
00045 import org.openmobileis.modules.crm.data.common.Representant;
00046 import org.openmobileis.modules.crm.data.common.RepresentantFactory;
00047 import org.openmobileis.common.util.exception.BadDataFormatException;
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 FODBRepresentantFactory implements RepresentantFactory {
00053
00054 private String[] repIds;
00055
00056
00060 public FODBRepresentantFactory() {
00061 super();
00062 String dbPath = PropertiesManager.getManager().getProperty("fastobjectdb.database.path");
00063 if (dbPath == null) {
00064 LogManager.traceError(0, "Representant Factory FODB ERROR : FODB path property is not set. Can't create db");
00065 }
00066 try {
00067 FastObjectDB db = FastObjectDBManager.getCurrentFODB();
00068 if (!db.isCollectionExist(this.getCollectionName())) {
00069 db.createCollection(this.getCollectionName(), this.getDataType());
00070 FODBStringIndexDescriptor IDDescriptor = new FODBStringIndexDescriptor("ID", FODBIndexDescriptor.UNIQUE, "getRepresentantId()", 12, this.getMaxRepresentantIdLength());
00071 db.addIndex(this.getCollectionName(), IDDescriptor);
00072
00073 this.initDB(db);
00074 }
00075
00076 ((SynchroFastObjectDB)db).registerSynchroFODBReturnListener(this.getCollectionName(), this.getSynchroListener());
00077
00078 ((SynchroFastObjectDB)db).registerJournalLogRenderer(this.getCollectionName(), "Utilisateurs :");
00079 this.updateSessionId();
00080
00081 } catch (Throwable ex) {
00082 LogManager.traceError(0, "Representant Factory FODB ERROR : Unknown error during creation");
00083 LogManager.traceError(0, ex);
00084 }
00085 }
00086
00087 private void updateSessionId() {
00088 SessionContext context = (SessionContext) SessionContextManager.getManager().getSessionContext();
00089 Representant rep =this.getInstallRepresentant();
00090 if (rep != null) {
00091 context.setAttribute("userId",rep.getRepresentantId());
00092 }
00093 }
00094
00095 public String[] getRepIdsList() {
00096 try {
00097 Representant[] repList = this.getAllRepresentants();
00098 String[] repIds = new String[repList.length];
00099 for (int i=0; i<repList.length; i++) {
00100 repIds[i] = repList[i].getRepresentantId();
00101 }
00102 return repIds;
00103 } catch (Throwable ex) {
00104 LogManager.traceError(0, ex);
00105 }
00106 return new String[0];
00107 }
00108
00109 public Representant getRepresentant(String id) {
00110 try {
00111 Query q = FastObjectDBManager.getCurrentFODB().query();
00112 q.constrain(this.getDataType());
00113 Query subq = q.descend("getRepresentantId()");
00114 Constraint c = subq.constrain(id).equal();
00115 ObjectSet set = q.execute();
00116 if (set.hasNext()) {
00117 return (Representant)set.next();
00118 } else {
00119 return null;
00120 }
00121 } catch (Exception e) {
00122 LogManager.traceError(LogServices.DATABASESERVICE, e);
00123 }
00124 return null;
00125 }
00126
00127 public Representant getSelectedRepresentant() {
00128 String repId = ServicePropertiesManager.getManager().getProperty("FWKRepresentant","selectedrepid");
00129 if (repId != null) {
00130 Representant rep = this.getRepresentant(repId);
00131 return rep;
00132 }
00133 return null;
00134 }
00135
00136 public void setSelectedRepresentant(String id) throws ServiceException, DatabaseException {
00137 ServicePropertiesManager.getManager().saveProperty("FWKRepresentant","selectedrepid", id);
00138 }
00139
00140 public Representant getInstallRepresentant() {
00141 String repId = ServicePropertiesManager.getManager().getProperty("FWKRepresentant","installrepid");
00142 if (repId != null) {
00143 Representant rep = this.getRepresentant(repId);
00144 return rep;
00145 }
00146 return null;
00147 }
00148
00149 public void setInstallRepresentant(String id) throws ServiceException, DatabaseException {
00150 ServicePropertiesManager.getManager().saveProperty("FWKRepresentant","installrepid", id);
00151 }
00152
00153 public Representant[] getAllRepresentants() throws DatabaseException {
00154 try {
00155 Query q = FastObjectDBManager.getCurrentFODB().query();
00156 q.constrain(this.getDataType());
00157 Query subq = q.descend("getRepresentantId()");
00158 ObjectSet set = q.execute();
00159 Representant[] repList = new Representant[set.size()];
00160 int i=0;
00161 while (set.hasNext()) {
00162 repList[i++] = (Representant) set.next();
00163 }
00164 return repList;
00165 } catch (Exception e) {
00166 LogManager.traceError(LogServices.DATABASESERVICE, e);
00167 }
00168 return new Representant[0];
00169 }
00170
00174 public void addRepresentant(Representant rep) throws ServiceException, DatabaseException {
00175 Representant dbrep = this.getRepresentant(rep.getRepresentantId());
00176 try {
00177 if (dbrep == null) {
00178 FastObjectDBManager.getCurrentFODB().add(this.getCollectionName(), rep);
00179 } else {
00180 FastObjectDBManager.getCurrentFODB().replace(this.getCollectionName(), rep);
00181 }
00182 } catch (Throwable ex) {
00183 throw new ServiceException(ex);
00184 }
00185 }
00186
00187 public void removeRepresentant(String repId) throws ServiceException, DatabaseException {
00188 try {
00189
00190 FastObjectDBManager.getCurrentFODB().deleteWithId(this.getCollectionName(), repId);
00191 } catch (Throwable ex) {
00192 throw new ServiceException(ex);
00193 }
00194 }
00195
00196 protected RepresentantSynchroFODBReturnListener getSynchroListener() {
00197 return new RepresentantSynchroFODBReturnListener();
00198 }
00199
00200 public String getCollectionName() {
00201 return "Representant";
00202 }
00203
00204 protected abstract int getMaxRepresentantIdLength();
00205
00206 protected abstract Class getDataType();
00207
00208 protected abstract void initDB(FastObjectDB db) throws FODBException, BadDataFormatException;
00209 }