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

Generated on Mon Dec 4 11:03:27 2006 for OpenMobileIS by  doxygen 1.5.1-p1