
# HG changeset patch
# User Gorka Gil <gorka.gil@openbravo.com>
# Date 1565096957 -7200
# Node ID 74cff7c7a4469593b37761ca694e2977c3a141ae
# Parent  e761289583dceec55570c8ec9b21a687c6b0fe98
Related to issue 41027: Create modulescript to mantain refresh on login in existing instances

diff -r e761289583dc -r 74cff7c7a446 src-util/modulescript/src/org/openbravo/mobile/core/modulescript/MantainMasterdataRefreshBehaviour41027.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-util/modulescript/src/org/openbravo/mobile/core/modulescript/MantainMasterdataRefreshBehaviour41027.java	Tue Aug 06 15:09:17 2019 +0200
@@ -0,0 +1,55 @@
+/*
+ ************************************************************************************
+ * Copyright (C) 2019 Openbravo S.L.U.
+ * Licensed under the Openbravo Commercial License version 1.0
+ * You may obtain a copy of the License at http://www.openbravo.com/legal/obcl.html
+ * or in the legal folder of this module distribution.
+ ************************************************************************************
+ */
+
+package org.openbravo.mobile.core.modulescript;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.openbravo.database.ConnectionProvider;
+import org.openbravo.modulescript.ModuleScript;
+import org.openbravo.modulescript.ModuleScriptExecutionLimits;
+import org.openbravo.modulescript.OpenbravoVersion;
+
+/**
+ * 
+ * Previous to 19Q4 masterdata is refreshed in each login or F5.
+ * From 19Q4, this preference OBMOBC_NotAutoLoadIncrementalAtLogin comes set by default to 'Y'.
+ * This module script will mantain the old behaviour for current instalations.
+ *
+ */
+public class MantainMasterdataRefreshBehaviour41027 extends ModuleScript {
+
+  private static final Logger log4j = LogManager.getLogger();
+  private static final String RETAIL_PACK_MODULE_ID = "03FAB282A7BF47D3B1B242AC67F7845B";
+
+  @Override
+  public void execute() {
+    try {
+      ConnectionProvider cp = getConnectionProvider();
+      final boolean preferenceExists = MantainMasterdataRefreshBehaviour41027Data.preferenceCreated(cp);
+      if ( ! preferenceExists ) {
+        MantainMasterdataRefreshBehaviour41027Data.createPreference(cp);
+      }
+    } catch (Exception e) {
+      handleError(e);
+    }
+  }
+
+  @Override
+  protected ModuleScriptExecutionLimits getModuleScriptExecutionLimits() {
+    return new ModuleScriptExecutionLimits(RETAIL_PACK_MODULE_ID, null,
+        new OpenbravoVersion(1, 8, 4700)); // execute when updating till 19Q4
+  }
+
+  @Override
+  protected boolean executeOnInstall() {
+    return false;
+  }
+
+}
diff -r e761289583dc -r 74cff7c7a446 src-util/modulescript/src/org/openbravo/mobile/core/modulescript/MantainMasterdataRefreshBehaviour41027_Data.xsql
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-util/modulescript/src/org/openbravo/mobile/core/modulescript/MantainMasterdataRefreshBehaviour41027_Data.xsql	Tue Aug 06 15:09:17 2019 +0200
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+/*
+ ************************************************************************************
+ * Copyright (C) 2019 Openbravo S.L.U.
+ * Licensed under the Openbravo Commercial License version 1.0
+ * You may obtain a copy of the License at http://www.openbravo.com/legal/obcl.html
+ * or in the legal folder of this module distribution.
+ ************************************************************************************
+ */
+-->
+<SqlClass name="MantainMasterdataRefreshBehaviour41027Data" package="org.openbravo.mobile.core.modulescript">
+  <SqlMethod name="preferenceCreated" type="preparedStatement" return="boolean">
+      <Sql>
+        <![CDATA[
+        SELECT count(*) as exist 
+        FROM DUAL 
+        WHERE EXISTS ( 
+          select 1 as num 
+          from ad_preference 
+          where property='OBMOBC_NotAutoLoadIncrementalAtLogin' 
+          and ad_module_id is null
+        )
+        ]]>
+      </Sql>
+  </SqlMethod>
+  <SqlMethod name="createPreference" type="preparedStatement" return="rowcount">
+       <Sql> 
+       <![CDATA[ 
+           INSERT INTO ad_preference (
+           ad_preference_id, ad_client_id, ad_org_id, isactive,
+           createdby, created, updatedby, updated, property, value, selected, ispropertylist
+           ) VALUES (
+           get_uuid(), '0', '0', 'Y', '0', NOW(), '0', NOW(),'OBMOBC_NotAutoLoadIncrementalAtLogin', 'N', 'Y', 'Y')
+         ]]>
+        </Sql>
+   </SqlMethod>
+</SqlClass>
diff -r e761289583dc -r 74cff7c7a446 src-util/modulescript/src/src/org/openbravo/mobile/core/modulescript/MantainMasterdataRefreshBehaviour41027Data.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-util/modulescript/src/src/org/openbravo/mobile/core/modulescript/MantainMasterdataRefreshBehaviour41027Data.java	Tue Aug 06 15:09:17 2019 +0200
@@ -0,0 +1,123 @@
+//Sqlc generated V1.O00-1
+package org.openbravo.mobile.core.modulescript;
+
+import java.sql.*;
+
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.LogManager;
+
+import javax.servlet.ServletException;
+
+import org.openbravo.data.FieldProvider;
+import org.openbravo.database.ConnectionProvider;
+import org.openbravo.data.UtilSql;
+import org.openbravo.database.SessionInfo;
+
+@SuppressWarnings("serial")
+class MantainMasterdataRefreshBehaviour41027Data implements FieldProvider {
+static Logger log4j = LogManager.getLogger();
+  private String InitRecordNumber="0";
+  public String exist;
+
+  public String getInitRecordNumber() {
+    return InitRecordNumber;
+  }
+
+  public String getField(String fieldName) {
+    if (fieldName.equalsIgnoreCase("exist"))
+      return exist;
+   else {
+     log4j.debug("Field does not exist: " + fieldName);
+     return null;
+   }
+ }
+
+  public static boolean preferenceCreated(ConnectionProvider connectionProvider)    throws ServletException {
+    String strSql = "";
+    strSql = strSql + 
+      "        SELECT count(*) as exist " +
+      "        FROM DUAL " +
+      "        WHERE EXISTS ( " +
+      "          select 1 as num " +
+      "          from ad_preference " +
+      "          where property='OBMOBC_NotAutoLoadIncrementalAtLogin' " +
+      "          and ad_module_id is null" +
+      "        )";
+
+    ResultSet result;
+    boolean boolReturn = false;
+    PreparedStatement st = null;
+
+    try {
+    st = connectionProvider.getPreparedStatement(strSql);
+
+      result = st.executeQuery();
+      if(result.next()) {
+        boolReturn = !UtilSql.getValue(result, "exist").equals("0");
+      }
+      result.close();
+    } catch(SQLException e){
+      if (log4j.isDebugEnabled()) {
+        log4j.error("SQL error in query: " + strSql, e);
+      } else {
+        log4j.error("SQL error in query: " + strSql + " :" + e);
+      }
+      throw new ServletException("@CODE=" + Integer.toString(e.getErrorCode()) + "@" + e.getMessage());
+    } catch(Exception ex){
+      if (log4j.isDebugEnabled()) {
+        log4j.error("Exception in query: " + strSql, ex);
+      } else {
+        log4j.error("Exception in query: " + strSql + " :" + ex);
+      }
+      throw new ServletException("@CODE=@" + ex.getMessage());
+    } finally {
+      try {
+        connectionProvider.releasePreparedStatement(st);
+      } catch(Exception e){
+        log4j.error("Error during release*Statement of query: " + strSql, e);
+      }
+    }
+    return(boolReturn);
+  }
+
+  public static int createPreference(ConnectionProvider connectionProvider)    throws ServletException {
+    String strSql = "";
+    strSql = strSql + 
+      "           INSERT INTO ad_preference (" +
+      "           ad_preference_id, ad_client_id, ad_org_id, isactive," +
+      "           createdby, created, updatedby, updated, property, value, selected, ispropertylist" +
+      "           ) VALUES (" +
+      "           get_uuid(), '0', '0', 'Y', '0', NOW(), '0', NOW(),'OBMOBC_NotAutoLoadIncrementalAtLogin', 'N', 'Y', 'Y')";
+
+    int updateCount = 0;
+    PreparedStatement st = null;
+
+    try {
+    st = connectionProvider.getPreparedStatement(strSql);
+
+      SessionInfo.saveContextInfoIntoDB(connectionProvider.getConnection());
+      updateCount = st.executeUpdate();
+    } catch(SQLException e){
+      if (log4j.isDebugEnabled()) {
+        log4j.error("SQL error in query: " + strSql, e);
+      } else {
+        log4j.error("SQL error in query: " + strSql + " :" + e);
+      }
+      throw new ServletException("@CODE=" + Integer.toString(e.getErrorCode()) + "@" + e.getMessage());
+    } catch(Exception ex){
+      if (log4j.isDebugEnabled()) {
+        log4j.error("Exception in query: " + strSql, ex);
+      } else {
+        log4j.error("Exception in query: " + strSql + " :" + ex);
+      }
+      throw new ServletException("@CODE=@" + ex.getMessage());
+    } finally {
+      try {
+        connectionProvider.releasePreparedStatement(st);
+      } catch(Exception e){
+        log4j.error("Error during release*Statement of query: " + strSql, e);
+      }
+    }
+    return(updateCount);
+  }
+}

