# HG changeset patch
# User Rafa Alonso <rafael.alonso@openbravo.com>
# Date 1413902533 -7200
#      Tue Oct 21 16:42:13 2014 +0200
# Node ID a596a3fc2fa2ee56aa276662ce4893c79e8cd43c
# Parent  23602848fef846cc99db22af8c22f5a4464c53fe
Fixes issue 27936: The suffix of the generated javascript file is the application name

diff -r 23602848fef8 -r a596a3fc2fa2 src/org/openbravo/mobile/core/MobileStaticResourceComponent.java
--- a/src/org/openbravo/mobile/core/MobileStaticResourceComponent.java	Mon Oct 20 16:17:31 2014 +0200
+++ b/src/org/openbravo/mobile/core/MobileStaticResourceComponent.java	Tue Oct 21 16:42:13 2014 +0200
@@ -40,7 +40,7 @@
    * synchronized because its quite heavy to do and race conditions would be nasty
    */
   private static synchronized String generateStaticResources(StaticResourceComponent src,
-      String realPath) {
+      String realPath, String appName) {
     final String currentModulesVersionMd5 = getModulesVersionMd5();
     final boolean isModuleVersionChange = currentModulesVersionMd5 == modulesVersionMd5;
     // explicitely calling the io subsystem gives tomcat the ability to not fail in the first
@@ -62,13 +62,13 @@
       // generate a new javascript file. get the name of the new file, and remember it
       // staticResourceComponent creates the directory structure
       final String generatedJavascriptMd5 = src.getStaticResourceFileName();
-      File newGeneratedFile = new File(realPath + "/" + generatedJavascriptMd5 + ".js");
+      final File newGeneratedFile = new File(realPath + "/" + generatedJavascriptMd5 + ".js");
       // get the modules version md5
       modulesVersionMd5 = currentModulesVersionMd5;
       // generate the final filename
-      String newGeneratedJavascriptFilename = DigestUtils.md5Hex(generatedJavascriptMd5
+      final String newGeneratedJavascriptFilename = DigestUtils.md5Hex(generatedJavascriptMd5
           + modulesVersionMd5)
-          + "_" + MobileCoreConstants.RETAIL_CORE;
+          + "_" + appName;
       // if the filename has changed, use it. otherwise, delete the generated javascript file
       if (newGeneratedJavascriptFilename.equals(generatedJavascriptFilename) && isFileExists) {
         // delete the generated file, as we don't need it and we don't want to use it because the
@@ -84,8 +84,8 @@
       }
       // delete all other mobile generated files, as no terminal should use them anymore
       if (new File(realPath).exists()) {
-        for (File file : new File(realPath + "/").listFiles()) {
-          if (file.getName().contains(MobileCoreConstants.RETAIL_CORE)
+        for (final File file : new File(realPath + "/").listFiles()) {
+          if (file.getName().contains(appName)
               && !file.getName().contains(generatedJavascriptFilename)) {
             file.delete();
           }
@@ -96,9 +96,9 @@
   }
 
   private static String getModulesVersionMd5() {
-    StringBuffer versionString = new StringBuffer();
+    final StringBuffer versionString = new StringBuffer();
     final List<Module> modules = KernelUtils.getInstance().getModulesOrderedByDependency();
-    for (Module module : modules) {
+    for (final Module module : modules) {
       versionString.append(module.getVersion());
     }
     return DigestUtils.md5Hex(versionString.toString());
@@ -116,13 +116,18 @@
   /**
    * the exposed method to get the generated javascript filename if the file does not exists, the
    * file is created before this method returns. this can take several milliseconds
+   * 
+   * @param appName
    */
   public String getGeneratedJavascriptFilename() {
     staticResourceComponent.setParameters(getParameters());
     final ServletContext context = (ServletContext) getParameters().get(
         KernelConstants.SERVLET_CONTEXT);
+    final String appName = getParameters().containsKey("_appName")
+        ? (String) getParameters().get("_appName")
+        : MobileCoreConstants.RETAIL_CORE;
     return generateStaticResources(staticResourceComponent,
-        context.getRealPath(StaticResourceComponent.GEN_TARGET_LOCATION) + "/");
+        context.getRealPath(StaticResourceComponent.GEN_TARGET_LOCATION) + "/", appName);
   }
 
   @Override
@@ -131,7 +136,7 @@
   }
 
   public static boolean isDevelopment() {
-    for (Module module : KernelUtils.getInstance().getModulesOrderedByDependency()) {
+    for (final Module module : KernelUtils.getInstance().getModulesOrderedByDependency()) {
       if (module.isInDevelopment()) {
         return true;
       }
