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