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 java.util.HashMap;
00032
00033 import org.odbms.Constraint;
00034 import org.odbms.ObjectSet;
00035 import org.odbms.Query;
00036 import org.openmobileis.database.DatabaseException;
00037 import org.openmobileis.database.fastobjectdb.FODBIndexDescriptor;
00038 import org.openmobileis.database.fastobjectdb.FODBStringIndexDescriptor;
00039 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00040 import org.openmobileis.database.fastobjectdb.FastObjectDBManager;
00041 import org.openmobileis.database.fastobjectdb.db.exception.FODBException;
00042 import org.openmobileis.database.fastobjectdb.db.query.soda.FODBSodaObjectSet;
00043 import org.openmobileis.common.util.exception.BadDataFormatException;
00044 import org.openmobileis.common.util.exception.ServiceException;
00045 import org.openmobileis.common.util.log.LogManager;
00046 import org.openmobileis.common.util.log.LogServices;
00047 import org.openmobileis.modules.crm.data.common.DefaultLabel;
00048 import org.openmobileis.modules.crm.data.common.Label;
00049 import org.openmobileis.modules.crm.data.common.LabelFactory;
00050 import org.openmobileis.common.util.collection.Array;
00051
00052
00061 public abstract class FODBLabelFactory implements LabelFactory {
00062 private static final Label emptyLabel = new DefaultLabel ("", "", "");
00063 private HashMap categoriesMap;
00067 public FODBLabelFactory() {
00068 super();
00069 try {
00070 FastObjectDB db = FastObjectDBManager.getCurrentFODB();
00071 if (!db.isCollectionExist("label")) {
00072 db.createCollection("label", this.getDataType());
00073 FODBStringIndexDescriptor IDDescriptor = new FODBStringIndexDescriptor("ID", FODBIndexDescriptor.UNIQUE, "getLabelCategoryId()", 12, this.getMaxLabelIdLength()+this.getMaxCategoryIdLength()+1);
00074 db.addIndex("label", IDDescriptor);
00075 FODBStringIndexDescriptor CategoryDescriptor = new FODBStringIndexDescriptor("cat", FODBIndexDescriptor.MULTIPLE, "getCategory()", 10, this.getMaxCategoryIdLength(), 10);
00076 db.addIndex("label", CategoryDescriptor);
00077
00078 this.initDB(db);
00079 }
00080 } catch (Throwable ex) {
00081 LogManager.traceError(0, "Label Factory FODB ERROR : Unknown error during creation");
00082 LogManager.traceError(0, ex);
00083 }
00084 }
00085
00086 public Array getLabelListForCategorie(String categorie) {
00087 try {
00088 Query q = FastObjectDBManager.getCurrentFODB().query();
00089 q.constrain(this.getDataType());
00090 Query subq = q.descend("getCategory()");
00091 Constraint c = subq.constrain(categorie).equal();
00092 ObjectSet set = q.execute();
00093 return ((FODBSodaObjectSet)set).getArrayFromSet();
00094 } catch (Exception e) {
00095 LogManager.traceError(LogServices.DATABASESERVICE, e);
00096 }
00097 return new Array();
00098 }
00099
00100 public Label getLabelWithIds(String id, String category) {
00101 try {
00102 Query q = FastObjectDBManager.getCurrentFODB().query();
00103 q.constrain(this.getDataType());
00104 Query subq = q.descend("getLabelCategoryId()");
00105 Constraint c = subq.constrain(FODBLabel.convertIdsToLabelObjectIds(id, category)).equal();
00106 ObjectSet set = q.execute();
00107 if (set.hasNext()) {
00108 return (Label)set.next();
00109 } else {
00110 return emptyLabel;
00111 }
00112 } catch (Exception e) {
00113 LogManager.traceError(LogServices.DATABASESERVICE, e);
00114 return emptyLabel;
00115 }
00116 }
00117
00121 public void storeLabel(Label label) throws ServiceException, DatabaseException {
00122 try {
00123 Label dblabel = this.getLabelWithIds(label.getLabelId(), label.getCategory());
00124 FastObjectDB db = FastObjectDBManager.getCurrentFODB();
00125 if (dblabel == null) {
00126 db.add("label", label);
00127 } else {
00128 db.replace("label", label);
00129 }
00130 } catch (Throwable ex) {
00131 LogManager.traceError(0, ex);
00132 }
00133 }
00134
00135 public void deleteLabel(String id, String category) throws ServiceException, DatabaseException {
00136 try {
00137 Label dblabel = this.getLabelWithIds(id, category);
00138 FastObjectDB db = FastObjectDBManager.getCurrentFODB();
00139 db.deleteWithId("label", FODBLabel.convertIdsToLabelObjectIds(id, category));
00140 } catch (Throwable ex) {
00141 LogManager.traceError(0, ex);
00142 }
00143 }
00144
00145 public abstract String[][] getCategoriesList();
00146
00150 protected abstract int getMaxLabelIdLength();
00151
00152 protected abstract int getMaxCategoryIdLength();
00153
00154 protected abstract Class getDataType();
00155
00156 protected abstract void initDB(FastObjectDB db) throws FODBException, BadDataFormatException;
00157
00158 }