FODBLabelFactory.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  *  Creation P.Delrieu
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         public 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.equals(FODBLabelFactory.emptyLabel))        {
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 }

Generated on Mon Jan 14 17:29:46 2008 for OpenMobileIS by  doxygen 1.5.4