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 this.updateSessionId();
00078
00079 } catch (Throwable ex) {
00080 LogManager.traceError(0, "Representant Factory FODB ERROR : Unknown error during creation");
00081 LogManager.traceError(0, ex);
00082 }
00083 }
00084
00085 private void updateSessionId() {
00086 SessionContext context = (SessionContext) SessionContextManager.getManager().getSessionContext();
00087 Representant rep =this.getInstallRepresentant();
00088 if (rep != null) {
00089 context.setAttribute("userId",rep.getRepresentantId());
00090 }
00091 }
00092
00093 public String[] getRepIdsList() {
00094 try {
00095 Representant[] repList = this.getAllRepresentants();
00096 String[] repIds = new String[repList.length];
00097 for (int i=0; i<repList.length; i++) {
00098 repIds[i] = repList[i].getRepresentantId();
00099 }
00100 return repIds;
00101 } catch (Throwable ex) {
00102 LogManager.traceError(0, ex);
00103 }
00104 return new String[0];
00105 }
00106
00107 public Representant getRepresentant(String id) {
00108 try {
00109 Query q = FastObjectDBManager.getCurrentFODB().query();
00110 q.constrain(this.getDataType());
00111 Query subq = q.descend("getRepresentantId()");
00112 Constraint c = subq.constrain(id).equal();
00113 ObjectSet set = q.execute();
00114 if (set.hasNext()) {
00115 return (Representant)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 Representant getSelectedRepresentant() {
00126 String repId = ServicePropertiesManager.getManager().getProperty("FWKRepresentant","selectedrepid");
00127 if (repId != null) {
00128 Representant rep = this.getRepresentant(repId);
00129 return rep;
00130 }
00131 return null;
00132 }
00133
00134 public void setSelectedRepresentant(String id) throws ServiceException, DatabaseException {
00135 ServicePropertiesManager.getManager().saveProperty("FWKRepresentant","selectedrepid", id);
00136 }
00137
00138 public Representant getInstallRepresentant() {
00139 String repId = ServicePropertiesManager.getManager().getProperty("FWKRepresentant","installrepid");
00140 if (repId != null) {
00141 Representant rep = this.getRepresentant(repId);
00142 return rep;
00143 }
00144 return null;
00145 }
00146
00147 public void setInstallRepresentant(String id) throws ServiceException, DatabaseException {
00148 ServicePropertiesManager.getManager().saveProperty("FWKRepresentant","installrepid", id);
00149 }
00150
00151 public Representant[] getAllRepresentants() throws DatabaseException {
00152 try {
00153 Query q = FastObjectDBManager.getCurrentFODB().query();
00154 q.constrain(this.getDataType());
00155 Query subq = q.descend("getRepresentantId()");
00156 ObjectSet set = q.execute();
00157 Representant[] repList = new Representant[set.size()];
00158 int i=0;
00159 while (set.hasNext()) {
00160 repList[i++] = (Representant) set.next();
00161 }
00162 return repList;
00163 } catch (Exception e) {
00164 LogManager.traceError(LogServices.DATABASESERVICE, e);
00165 }
00166 return new Representant[0];
00167 }
00168
00172 public void addRepresentant(Representant rep) throws ServiceException, DatabaseException {
00173 Representant dbrep = this.getRepresentant(rep.getRepresentantId());
00174 try {
00175 if (dbrep == null) {
00176 FastObjectDBManager.getCurrentFODB().add(this.getCollectionName(), rep);
00177 } else {
00178 FastObjectDBManager.getCurrentFODB().replace(this.getCollectionName(), rep);
00179 }
00180 } catch (Throwable ex) {
00181 throw new ServiceException(ex);
00182 }
00183 }
00184
00185 public void removeRepresentant(String repId) throws ServiceException, DatabaseException {
00186 try {
00187
00188 FastObjectDBManager.getCurrentFODB().deleteWithId(this.getCollectionName(), repId);
00189 } catch (Throwable ex) {
00190 throw new ServiceException(ex);
00191 }
00192 }
00193
00194 protected RepresentantSynchroFODBReturnListener getSynchroListener() {
00195 return new RepresentantSynchroFODBReturnListener();
00196 }
00197
00198 public String getCollectionName() {
00199 return "Representant";
00200 }
00201
00202 protected abstract int getMaxRepresentantIdLength();
00203
00204 protected abstract Class getDataType();
00205
00206 protected abstract void initDB(FastObjectDB db) throws FODBException, BadDataFormatException;
00207 }