Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

FODBLabelFactory.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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  *  2004 Creation P.Delrieu
00026  *  2005 Modification F.Salque
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 }

Generated on Wed Dec 14 21:05:33 2005 for OpenMobileIS by  doxygen 1.4.4