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.database.fastobjectdb.synchro.client;
00031
00032
00033
00034 import org.odbms.Constraint;
00035 import org.odbms.ObjectSet;
00036 import org.odbms.Query;
00037
00038 import org.openmobileis.common.util.collection.Array;
00039 import org.openmobileis.database.DatabaseException;
00040 import org.openmobileis.database.fastobjectdb.FODBIndexDescriptor;
00041 import org.openmobileis.database.fastobjectdb.FODBStringIndexDescriptor;
00042 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00043 import org.openmobileis.database.fastobjectdb.FastObjectDBManager;
00044 import org.openmobileis.database.fastobjectdb.db.query.soda.FODBSodaObjectSet;
00045
00046
00054 public class FODBSyncActionDBQueryManager implements SyncActionDBQueryManager {
00055
00056 private static int SERVICE_NAME_MAX_LENGTH = 30;
00057
00058 private static int uidkeylength;
00059
00060
00061
00062
00063
00064 public FODBSyncActionDBQueryManager() {
00065 uidkeylength = 100;
00066 }
00067
00068 public FODBSyncActionDBQueryManager(int uidlength) {
00069 uidkeylength = uidlength;
00070 }
00071
00072
00073
00074
00075
00076
00077
00078
00079 public void initDB() throws DatabaseException {
00080
00081 try {
00082 FastObjectDB db = FastObjectDBManager.getCurrentFODB();
00083 if (!db.isCollectionExist("FWKACTIONDB")) {
00084 db.createCollection("FWKACTIONDB", ActionDB.class, false);
00085 FODBStringIndexDescriptor KeyDescriptor = new FODBStringIndexDescriptor("ID", FODBIndexDescriptor.UNIQUE, "getActionID()", 10, SERVICE_NAME_MAX_LENGTH);
00086 db.addIndex("FWKACTIONDB", KeyDescriptor);
00087 FODBStringIndexDescriptor debKeyDescriptor = new FODBStringIndexDescriptor("SERV", FODBIndexDescriptor.MULTIPLE, "getActionServiceName()", 10, SERVICE_NAME_MAX_LENGTH, 10);
00088 db.addIndex("FWKACTIONDB", debKeyDescriptor);
00089 }
00090 } catch (Throwable ex) {
00091 throw new DatabaseException(ex);
00092 }
00093
00094 }
00095
00096 public Array getActionsForService(String serviceName) throws DatabaseException {
00097 if ((serviceName== null) || (serviceName.length() > SERVICE_NAME_MAX_LENGTH)) {
00098 throw new DatabaseException("SODA SyncActionDBQueryManager getActionsForService ServiceName to long or null abort "+serviceName);
00099 }
00100
00101 Query q = FastObjectDBManager.getCurrentFODB().query();
00102 q.constrain(ActionDB.class);
00103 Query subq = q.descend("getActionServiceName()");
00104 Constraint c = subq.constrain(serviceName).equal();
00105 FODBSodaObjectSet set = (FODBSodaObjectSet)q.execute();
00106 return set.getArrayFromSet();
00107 }
00108
00109 public ActionDB getActionDB(String serviceName, String objectUID) throws DatabaseException {
00110 if ((serviceName== null) || (serviceName.length() > SERVICE_NAME_MAX_LENGTH)) {
00111 throw new DatabaseException("SODA SyncActionDBQueryManager getAction ServiceName to long or null abort "+serviceName);
00112 }
00113 String actionID = ActionDB.getActionIdFromServiceAndUID(serviceName, objectUID);
00114 Query q = FastObjectDBManager.getCurrentFODB().query();
00115 q.constrain(ActionDB.class);
00116 Query subq = q.descend("getActionID()");
00117 Constraint c = subq.constrain(actionID).equal();
00118 ObjectSet set = q.execute();
00119 if (set.hasNext()) {
00120 return (ActionDB) set.next();
00121 }
00122 return null;
00123 }
00124
00125
00126
00127
00128
00129 public void create (ActionDB action) throws DatabaseException {
00130 if ((action.getActionServiceName()== null) || (action.getActionServiceName().length() > SERVICE_NAME_MAX_LENGTH)) {
00131 throw new DatabaseException("SODA SyncActionDBQueryManager create ServiceName to long or null abort "+action.getActionServiceName());
00132 }
00133 if ((action.getActionObjectUID()== null) || (action.getActionObjectUID().length() > uidkeylength)) {
00134 throw new DatabaseException("SODA SyncActionDBQueryManager create Action uid to long or null abort "+action.getActionObjectUID());
00135 }
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 ActionDB oldAction = this.getActionDB(action.getActionServiceName(), action.getActionObjectUID());
00149 if (oldAction != null) {
00150 switch (action.getActionType()) {
00151 case ActionDB.ADD_ACTION :
00152 FastObjectDBManager.getCurrentFODB().replace("FWKACTIONDB", action);
00153 break;
00154 case ActionDB.DELETE_ACTION :
00155 if (oldAction.getActionType() == ActionDB.ADD_ACTION) {
00156 FastObjectDBManager.getCurrentFODB().delete("FWKACTIONDB", oldAction);
00157 } else {
00158 FastObjectDBManager.getCurrentFODB().replace("FWKACTIONDB", action);
00159 }
00160 break;
00161 case ActionDB.UPDATE_ACTION :
00162 default :
00163 break;
00164 }
00165 } else {
00166 FastObjectDBManager.getCurrentFODB().add("FWKACTIONDB", action);
00167 }
00168 }
00169
00170 public void deleteAllActionForService(String serviceName) throws DatabaseException {
00171 if ((serviceName== null) || (serviceName.length() > SERVICE_NAME_MAX_LENGTH)) {
00172 throw new DatabaseException("SODA SyncActionDBQueryManager getActionsForService ServiceName to long or null abort "+serviceName);
00173 }
00174 Array array = this.getActionsForService(serviceName);
00175 for (int i=0; i<array.size(); i++) {
00176 ActionDB action = (ActionDB) array.get(i);
00177 FastObjectDBManager.getCurrentFODB().delete("FWKACTIONDB", action);
00178 }
00179 }
00180
00181
00182
00183 public void delete (String serviceName, String uid) throws DatabaseException {
00184 FastObjectDBManager.getCurrentFODB().delete("FWKACTIONDB", new ActionDB(serviceName, uid, 0));
00185 }
00186
00187 }