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