details:   /erp/devel/pi/rev/a509e24e676f
changeset: 11401:a509e24e676f
user:      Asier Lostalé <asier.lostale <at> openbravo.com>
date:      Wed Mar 30 17:20:49 2011 +0200
summary:   fixed bug 16495, fixed bug 16134

  Browser caching for Components has changed:
   -Now ETag is different if there is any module in development. Fixing in this 
way the caching for templates
    as well as caching for modules adding tabs to windows within other modules.
   -ETag in case of no modules in development is composed by all module 
versions and whether they are enable.
   -Removed non used ETag from StandardWindowComponent

diffstat:

 
modules/org.openbravo.client.application/src/org/openbravo/client/application/event/ModuleHandler.java
            |  82 ++++++++++
 
modules/org.openbravo.client.application/src/org/openbravo/client/application/window/StandardWindowComponent.java
 |  38 +----
 
modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/BaseComponent.java
                            |  66 +++++++-
 3 files changed, 145 insertions(+), 41 deletions(-)

diffs (263 lines):

diff -r 46698571afe5 -r a509e24e676f 
modules/org.openbravo.client.application/src/org/openbravo/client/application/event/ModuleHandler.java
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ 
b/modules/org.openbravo.client.application/src/org/openbravo/client/application/event/ModuleHandler.java
    Wed Mar 30 17:20:49 2011 +0200
@@ -0,0 +1,82 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html 
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License. 
+ * The Original Code is Openbravo ERP. 
+ * The Initial Developer of the Original Code is Openbravo SLU 
+ * All portions are Copyright (C) 2011 Openbravo SLU 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+
+package org.openbravo.client.application.event;
+
+import javax.enterprise.event.Observes;
+
+import org.openbravo.base.model.Entity;
+import org.openbravo.base.model.ModelProvider;
+import org.openbravo.base.model.Property;
+import org.openbravo.client.kernel.BaseComponent;
+import org.openbravo.client.kernel.event.EntityNewEvent;
+import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
+import org.openbravo.client.kernel.event.EntityUpdateEvent;
+import org.openbravo.model.ad.module.Module;
+
+/**
+ * This class observes modifications/insertions in ADModule in order to 
invalidate modules cache
+ * used for obtaining views ETags.
+ * 
+ * @author alostale
+ * 
+ */
+public class ModuleHandler extends EntityPersistenceEventObserver {
+  private static final String MODULE_TABLE_ID = 
"9D36D488605044F5A0264D7C8B916657";
+  private static Entity[] entities = { 
ModelProvider.getInstance().getEntityByTableId(
+      MODULE_TABLE_ID) };
+
+  public void onUpdate(@Observes EntityUpdateEvent event) {
+    if (!isValidEvent(event)) {
+      return;
+    }
+    if (!event.getPreviousState(getVersionProperty()).equals(
+        event.getCurrentState(getVersionProperty()))
+        || !event.getPreviousState(getEnabledProperty()).equals(
+            event.getCurrentState(getEnabledProperty()))
+        || !event.getPreviousState(getInDevelopmentProperty()).equals(
+            event.getCurrentState(getInDevelopmentProperty()))) {
+      BaseComponent.nullifyModuleCache();
+    }
+  }
+
+  public void onNew(@Observes EntityNewEvent event) {
+    if (!isValidEvent(event)) {
+      return;
+    }
+    BaseComponent.nullifyModuleCache();
+  }
+
+  @Override
+  protected Entity[] getObservedEntities() {
+    return entities;
+  }
+
+  private Property getInDevelopmentProperty() {
+    return entities[0].getProperty(Module.PROPERTY_INDEVELOPMENT);
+  }
+
+  private Property getVersionProperty() {
+    return entities[0].getProperty(Module.PROPERTY_VERSION);
+  }
+
+  private Property getEnabledProperty() {
+    return entities[0].getProperty(Module.PROPERTY_ENABLED);
+  }
+}
diff -r 46698571afe5 -r a509e24e676f 
modules/org.openbravo.client.application/src/org/openbravo/client/application/window/StandardWindowComponent.java
--- 
a/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/StandardWindowComponent.java
 Wed Mar 30 16:27:11 2011 +0200
+++ 
b/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/StandardWindowComponent.java
 Wed Mar 30 17:20:49 2011 +0200
@@ -11,7 +11,7 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2010 Openbravo SLU
+ * All portions are Copyright (C) 2010-2011 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -21,14 +21,11 @@
 import java.util.ArrayList;
 import java.util.List;
 
-import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.log4j.Logger;
 import org.openbravo.client.kernel.BaseTemplateComponent;
 import org.openbravo.client.kernel.KernelConstants;
-import org.openbravo.client.kernel.KernelUtils;
 import org.openbravo.client.kernel.Template;
 import org.openbravo.dal.service.OBDal;
-import org.openbravo.model.ad.module.Module;
 import org.openbravo.model.ad.ui.Field;
 import org.openbravo.model.ad.ui.Tab;
 import org.openbravo.model.ad.ui.Window;
@@ -47,7 +44,6 @@
   private OBViewTab rootTabComponent = null;
   private Boolean inDevelopment = null;
   private String uniqueString = "" + System.currentTimeMillis();
-  private String windowETag = null;
 
   protected Template getComponentTemplate() {
     return OBDal.getInstance().get(Template.class, TEMPLATE_ID);
@@ -62,38 +58,6 @@
     return KernelConstants.ID_PREFIX + getWindowId();
   }
 
-  @Override
-  public String getETag() {
-    if (windowETag != null) {
-      return windowETag;
-    }
-
-    final List<Module> modules = new ArrayList<Module>();
-    modules.add(window.getModule());
-    for (Tab tab : window.getADTabList()) {
-      if (!tab.isActive() || !tab.getModule().isEnabled()) {
-        continue;
-      }
-      if (!modules.contains(tab.getModule())) {
-        modules.add(tab.getModule());
-      }
-      for (Field field : tab.getADFieldList()) {
-        if (!field.isActive() || !field.getModule().isEnabled()) {
-          continue;
-        }
-        if (!modules.contains(field.getModule())) {
-          modules.add(field.getModule());
-        }
-      }
-    }
-    final StringBuilder sb = new StringBuilder();
-    for (Module module : modules) {
-      sb.append(KernelUtils.getInstance().getVersionParameters(module));
-    }
-    windowETag = DigestUtils.md5Hex(sb.toString());
-    return windowETag;
-  }
-
   public boolean isIndevelopment() {
     if (inDevelopment != null) {
       return inDevelopment;
diff -r 46698571afe5 -r a509e24e676f 
modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/BaseComponent.java
--- 
a/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/BaseComponent.java
    Wed Mar 30 16:27:11 2011 +0200
+++ 
b/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/BaseComponent.java
    Wed Mar 30 17:20:49 2011 +0200
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2010 Openbravo SLU 
+ * All portions are Copyright (C) 2010-2011 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -28,7 +28,13 @@
 import javax.enterprise.inject.Instance;
 import javax.inject.Inject;
 
+import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.log4j.Logger;
+import org.hibernate.criterion.Order;
+import org.hibernate.criterion.Restrictions;
 import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.service.OBCriteria;
+import org.openbravo.dal.service.OBDal;
 import org.openbravo.model.ad.module.Module;
 
 /**
@@ -57,6 +63,11 @@
   // the url of the server and the servlet context
   private static String contextUrl = null;
 
+  private static String moduleVersionHash = null;
+  private static Boolean hasModulesInDevelopment = null;
+
+  private static final Logger log4j = Logger.getLogger(BaseComponent.class);
+
   @Inject
   @Any
   private Instance<Component> components;
@@ -170,14 +181,61 @@
    * @see org.openbravo.client.kernel.Component#getETag()
    */
   public String getETag() {
-    if (getModule().isInDevelopment() != null && 
getModule().isInDevelopment()) {
+    if (hasModulesInDevelopment()) {
+      // if (getModule().isInDevelopment() != null && 
getModule().isInDevelopment()) {
+
       return OBContext.getOBContext().getLanguage().getId() + "_" + 
getLastModified().getTime();
     } else {
-      return OBContext.getOBContext().getLanguage().getId() + "_" + 
getModule().getVersion() + "_"
-          + getModule().isEnabled();
+      return OBContext.getOBContext().getLanguage().getId() + "_" + 
getModuleVersionHash();
+      // return OBContext.getOBContext().getLanguage().getId() + "_" + 
getModule().getVersion() +
+      // "_"
+      // + getModule().isEnabled();
     }
   }
 
+  synchronized private boolean hasModulesInDevelopment() {
+    if (hasModulesInDevelopment == null) {
+      OBContext.setAdminMode();
+      try {
+        OBCriteria<Module> qMod = 
OBDal.getInstance().createCriteria(Module.class);
+        qMod.add(Restrictions.eq(Module.PROPERTY_INDEVELOPMENT, true));
+        hasModulesInDevelopment = qMod.count() > 0;
+        log4j.debug("Calculating whether there are modules in development: "
+            + hasModulesInDevelopment);
+      } finally {
+        OBContext.restorePreviousMode();
+      }
+    }
+    return hasModulesInDevelopment;
+  }
+
+  synchronized private String getModuleVersionHash() {
+    if (moduleVersionHash == null) {
+      String moduleVersions = "";
+      OBContext.setAdminMode();
+      try {
+        OBCriteria<Module> qMod = 
OBDal.getInstance().createCriteria(Module.class);
+        qMod.addOrder(Order.asc(Module.PROPERTY_ID));
+        for (Module mod : qMod.list()) {
+          moduleVersions += mod.getId() + "-" + mod.getVersion() + "-" + 
mod.isEnabled() + "\n";
+        }
+        moduleVersionHash = DigestUtils.md5Hex(moduleVersions);
+        log4j.debug("New moduleVersionHash. Original: " + moduleVersions + " 
hash:"
+            + moduleVersionHash);
+
+      } finally {
+        OBContext.restorePreviousMode();
+      }
+    }
+    return moduleVersionHash;
+  }
+
+  synchronized public static void nullifyModuleCache() {
+    hasModulesInDevelopment = null;
+    moduleVersionHash = null;
+    log4j.debug("Module cache for etag is now null");
+  }
+
   /*
    * (non-Javadoc)
    * 

------------------------------------------------------------------------------
Create and publish websites with WebMatrix
Use the most popular FREE web apps or write code yourself; 
WebMatrix provides all the features you need to develop and 
publish your website. http://p.sf.net/sfu/ms-webmatrix-sf
_______________________________________________
Openbravo-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to