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
00030 package org.openmobileis.modules.common.database.jdbc;
00031
00032 import java.sql.ResultSet;
00033 import java.util.HashMap;
00034
00035 import org.openmobileis.common.util.exception.DatabaseException;
00036 import org.openmobileis.common.util.exception.ServiceException;
00037 import org.openmobileis.common.util.exception.SynchroException;
00038 import org.openmobileis.common.util.log.LogManager;
00039 import org.openmobileis.common.util.log.LogServices;
00040 import org.openmobileis.modules.common.data.DefaultLabel;
00041 import org.openmobileis.modules.common.data.Label;
00042 import org.openmobileis.modules.common.data.LabelFactory;
00043 import org.openmobileis.modules.common.data.LabelManager;
00044 import org.openmobileis.common.util.collection.Array;
00045
00046
00055 public abstract class JDBCLabelFactory implements LabelFactory {
00056 protected static final Label emptyLabel = new DefaultLabel ("", "", "");
00057 protected LabelJDBCQuery query;
00058 private HashMap categoriesMap;
00062 public JDBCLabelFactory(LabelJDBCQuery q) {
00063 super();
00064 query = q;
00065 this.initLabelDB();
00066 this.loadLabelList();
00067 }
00068
00069 private void loadLabelList() {
00070 try {
00071 categoriesMap = new HashMap(10);
00072 String[][] catList = this.getCategoriesList();
00073 Label l = null;
00074 for (int i=0; i<catList.length; i++) {
00075 HashMap labelMap = new HashMap(20);
00076 categoriesMap.put(catList[i][1], labelMap);
00077 Array labelList = this.getDBLabelListForCategorie(catList[i][1]);
00078 for (int j=0; j<labelList.size(); j++) {
00079 l = (Label)labelList.get(j);
00080 labelMap.put(l.getLabelId(), l);
00081 }
00082 }
00083 } catch (Exception ex) {
00084 LogManager.traceError(0, ex);
00085 }
00086 }
00087
00088 public Array getLabelListForCategorie(String categorie) {
00089
00090 if (categoriesMap == null) this.loadLabelList();
00091 Array labelList = new Array();
00092 HashMap map = (HashMap) categoriesMap.get(categorie);
00093 if (map!= null) {
00094 labelList = new Array(map.values().toArray());
00095 }
00096 LabelManager.getManager().sortLabelArray(labelList);
00097 return labelList;
00098 }
00099
00100 protected Array getDBLabelListForCategorie(String categorie) {
00101 Array labelList = new Array();
00102 try {
00103 String[] param = new String[]{categorie};
00104 ResultSet result = query.getLabelListForCategorie(param);
00105 while (result.next()) {
00106 Label label = query.convertResultSetToLabel(result);
00107 if (label != null) {
00108 labelList.add(label);
00109 } else {
00110 LogManager.traceWarning(LogServices.DATABASESERVICE, "JDBCLabelFactory : getDBLabelListForCategorie : Null Label");
00111 }
00112 }
00113 result.close();
00114 } catch (Exception ex) {
00115 LogManager.traceError(0, ex);
00116 } finally {
00117 query.getDbManager().garbageOpenedConnection();
00118 }
00119 return labelList;
00120 }
00121
00122 public Label getLabelWithIds(String id, String category) {
00123 if (categoriesMap == null) this.loadLabelList();
00124 Label label = emptyLabel;
00125 HashMap map = (HashMap) categoriesMap.get(category);
00126 if (map!= null) {
00127 label = (Label)map.get(id);
00128 }
00129 return label;
00130 }
00131
00132 protected Label getDBLabelWithIds(String id, String category) {
00133 Label label = emptyLabel;
00134 try {
00135 String[] param = new String[]{id, category};
00136 ResultSet result = query.getLabelWithIds(param);
00137 if (result.next()) {
00138 label = query.convertResultSetToLabel(result);
00139 }
00140 result.close();
00141 } catch (Exception ex) {
00142 LogManager.traceError(0, ex);
00143 } finally {
00144 query.getDbManager().garbageOpenedConnection();
00145 }
00146 return label;
00147 }
00148
00149 public String convertIdsToLabelObjectIds(String labelid, String category) {
00150 StringBuffer str = new StringBuffer(labelid);
00151 str.append('%');
00152 str.append(category);
00153 return str.toString();
00154 }
00155
00156
00160 public void storeLabel(Label label) throws ServiceException, DatabaseException {
00161 try {
00162
00163 Label dblabel = this.getLabelWithIds(label.getLabelId(), label.getCategory());
00164 if (dblabel != null) {
00165 String[] param = query.getUpdateParamFromLabel(label);
00166 query.updateLabel(param);
00167 } else {
00168 String[] param = query.getInsertParamFromLabel(label);
00169 query.insertLabel(param);
00170 }
00171 this.notifyLabelUpdate(label);
00172
00173 this.loadLabelList();
00174 } catch (Throwable ex) {
00175 throw new ServiceException(ex);
00176 } finally {
00177 query.getDbManager().garbageOpenedConnection();
00178 }
00179 }
00180
00181 public void deleteLabel(String id, String category) throws ServiceException, DatabaseException {
00182 try {
00183
00184 Label dblabel = this.getLabelWithIds(id, category);
00185 if (dblabel != null) {
00186
00187 String[] param = {id, category};
00188 query.deleteLabel(param);
00189
00190
00191 this.notifyLabeldelete(id, category);
00192 this.loadLabelList();
00193 }
00194 } catch (Throwable ex) {
00195 throw new ServiceException(ex);
00196 } finally {
00197 query.getDbManager().garbageOpenedConnection();
00198 }
00199 }
00200
00201 public abstract String[][] getCategoriesList();
00202 public abstract void initLabelDB();
00203
00204 public abstract void notifyLabelUpdate(Label label) throws SynchroException;
00205 public abstract void notifyLabeldelete(String labelId, String categoryId) throws SynchroException;
00206 }