ObjectStore cache is working with StorableObjectEntities
[mir.git] / source / mir / storage / Database.java
index 1ed463c..43dcad9 100755 (executable)
@@ -57,7 +57,7 @@ public class Database implements StorageObject {
                // always same object saves a little space
                POPUP_EMTYLINE.put("key", ""); POPUP_EMTYLINE.put("value", "--");
     try {
-      GENERIC_ENTITY_CLASS = Class.forName("mir.entity.GenericEntity");
+      GENERIC_ENTITY_CLASS = Class.forName("mir.entity.StorableObjectEntity");
       STORABLE_OBJECT_ENTITY_CLASS = Class.forName("mir.entity.StorableObjectEntity");
     }
     catch (Exception e) {
@@ -433,7 +433,7 @@ public class Database implements StorageObject {
       StoreIdentifier search_sid =
         new StoreIdentifier( theEntityClass,
                              StoreContainerType.STOC_TYPE_ENTITYLIST,
-                             StoreUtil.getEntityListUniqueIdentifierFor(wc,ob,offset,limit) );
+                             StoreUtil.getEntityListUniqueIdentifierFor(theTable,wc,ob,offset,limit) );
       EntityList hit = (EntityList)o_store.use(search_sid);
       if ( hit!=null ) {
         theLog.printDebugInfo("CACHE (hit): " + search_sid.toString());
@@ -518,7 +518,7 @@ public class Database implements StorageObject {
                                theReturnList.setOffset(offset);
                                theReturnList.setWhere(wc);
                                theReturnList.setOrder(ob);
-        theReturnList.setEntityClass(theEntityClass);
+        theReturnList.setStorage(this);
         theReturnList.setLimit(limit);
                                if ( offset >= limit )
                                        theReturnList.setPrevBatch(offset - limit);
@@ -613,14 +613,21 @@ public class Database implements StorageObject {
         * @return der Wert des Primary-keys der eingefügten Entity
         */
        public String insert (Entity theEntity) throws StorageObjectException {
-               String returnId = null;
-               Connection con = null;
-               PreparedStatement pstmt = null;
                //cache
                invalidatePopupCache();
-               /** @todo OS: if Entity is StorableObject, invalidate in ObjectStore
-                *  careful: Entity has no id yet! */
-               try {
+
+    // invalidating all EntityLists corresponding with theEntityClass
+    if ( StoreUtil.implementsStorableObject(theEntityClass) ) {
+      StoreContainerType stoc_type =
+        StoreContainerType.valueOf( theEntityClass,
+                                    StoreContainerType.STOC_TYPE_ENTITYLIST);
+      o_store.invalidate(stoc_type);
+    }
+
+               String returnId = null;
+               Connection con = null; PreparedStatement pstmt = null;
+
+    try {
                        ArrayList streamedInput = theEntity.streamedInput();
                        StringBuffer f = new StringBuffer();
                        StringBuffer v = new StringBuffer();
@@ -693,6 +700,7 @@ public class Database implements StorageObject {
                        }
                        freeConnection(con, pstmt);
                }
+    /** @todo store entity in o_store */
                return  returnId;
        }
 
@@ -712,8 +720,16 @@ public class Database implements StorageObject {
                 *  have a primary key in the entity. i don't know if we
                 *  still need the streamed input fields. // rk  */
 
-               /** @todo extension: check if Entity did change, otherwise we don't need */
-               /** @todo OS: invalidate in Ostore if Entity is StorableObject */
+               /** @todo extension: check if Entity did change, otherwise we don't need
+     *  the roundtrip to the database */
+
+               /** invalidating corresponding entitylists in o_store*/
+    if ( StoreUtil.implementsStorableObject(theEntityClass) ) {
+      StoreContainerType stoc_type =
+        StoreContainerType.valueOf( theEntityClass,
+                                    StoreContainerType.STOC_TYPE_ENTITYLIST);
+      o_store.invalidate(stoc_type);
+    }
 
                ArrayList streamedInput = theEntity.streamedInput();
                String id = theEntity.getId();
@@ -784,10 +800,13 @@ public class Database implements StorageObject {
 
                invalidatePopupCache();
     // ostore send notification
-    if (StoreUtil.implementsStorableObject(theEntityClass) ) {
+    if ( StoreUtil.implementsStorableObject(theEntityClass) ) {
+      String uniqueId = id;
+      if ( theEntityClass.equals(StorableObjectEntity.class) )
+        uniqueId+="@"+theTable;
                        theLog.printInfo("CACHE: (del) " + id);
                        StoreIdentifier search_sid =
-        new StoreIdentifier(theEntityClass, StoreContainerType.STOC_TYPE_ENTITY, id);
+        new StoreIdentifier(theEntityClass, StoreContainerType.STOC_TYPE_ENTITY, uniqueId);
       o_store.invalidate(search_sid);
                }