fixed issue 31975: detect situation created by issue 31959
authorAsier Lostalé <asier.lostale@openbravo.com>
Thu, 21 Jan 2016 13:57:04 +0100
changeset 28742 51a2befdaed1
parent 28741 08ab0b08fa66
child 28743 b6c50283d4a8
fixed issue 31975: detect situation created by issue 31959

Added module script that creates alert when in this situation
src-util/modulescript/build/classes/org/openbravo/modulescript/PgJdbcDatesIssue.class
src-util/modulescript/build/classes/org/openbravo/modulescript/PgJdbcDatesIssueData.class
src-util/modulescript/src/org/openbravo/modulescript/PgJdbcDatesIssue.java
src-util/modulescript/src/org/openbravo/modulescript/PgJdbcDatesIssue_data.xsql
Binary file src-util/modulescript/build/classes/org/openbravo/modulescript/PgJdbcDatesIssue.class has changed
Binary file src-util/modulescript/build/classes/org/openbravo/modulescript/PgJdbcDatesIssueData.class has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-util/modulescript/src/org/openbravo/modulescript/PgJdbcDatesIssue.java	Thu Jan 21 13:57:04 2016 +0100
@@ -0,0 +1,100 @@
+/*
+ *************************************************************************
+ * 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) 2016 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+
+package org.openbravo.modulescript;
+
+import java.io.FileInputStream;
+import java.util.Properties;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.log4j.Logger;
+import org.openbravo.database.ConnectionProvider;
+
+/**
+ * Creates an alert for instances that might be affected by issue 31959
+ *
+ * @author alostale
+ *
+ */
+public class PgJdbcDatesIssue extends ModuleScript {
+
+  private static final Logger log = Logger.getLogger(PgJdbcDatesIssue.class);
+  private static final String CORE = "0";
+  private static final OpenbravoVersion PR15Q4 = new OpenbravoVersion("3.0.27657");
+  private static final OpenbravoVersion FIXED_VERSION = new OpenbravoVersion("3.0.27666");
+  private static final String ALERT_NAME = "Updating from 15PRQ4 potential problems with dates";
+  private static final String ALERT_TXT = "Some dates can be corrupted. Check http://wiki.openbravo.com/wiki/Release_Notes/Issue31959";
+
+  @Override
+  public void execute() {
+    if (!shouldExecute()) {
+      return;
+    }
+
+    try {
+      createAlert();
+    } catch (Exception e) {
+      log.error("Error processing alert", e);
+    }
+  }
+
+  private boolean shouldExecute() {
+    ConnectionProvider cp = getConnectionProvider();
+    if (!"POSTGRE".equals(cp.getRDBMS())) {
+      return false;
+    }
+
+    Properties properties = new Properties();
+    try {
+      properties.load(new FileInputStream(getPropertiesFile()));
+    } catch (Exception e) {
+      log.error("Could not load Openbravo.properties");
+      return false;
+    }
+
+    boolean usingCommonsDBCPPool = StringUtils.isBlank(properties
+        .getProperty("db.externalPoolClassName"));
+
+    if (!usingCommonsDBCPPool) {
+      return false;
+    }
+
+    try {
+      boolean alertAlreadyCraeted = PgJdbcDatesIssueData.existsAlert(cp, ALERT_NAME);
+      return !alertAlreadyCraeted;
+    } catch (Exception e) {
+      log.error("Error processing alert", e);
+      return false;
+    }
+  }
+
+  private void createAlert() throws Exception {
+    log.warn(ALERT_NAME + " - " + ALERT_TXT);
+    ConnectionProvider cp = getConnectionProvider();
+    PgJdbcDatesIssueData.insertAlertRule(cp, ALERT_NAME);
+    String ruleId = PgJdbcDatesIssueData.getAlertRuleId(cp, ALERT_NAME);
+    PgJdbcDatesIssueData.insertAlertRecipient(cp, ruleId);
+    PgJdbcDatesIssueData.insertAlert(cp, ALERT_TXT, ruleId);
+  }
+
+  @Override
+  protected ModuleScriptExecutionLimits getModuleScriptExecutionLimits() {
+    return new ModuleScriptExecutionLimits(CORE, PR15Q4, FIXED_VERSION);
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-util/modulescript/src/org/openbravo/modulescript/PgJdbcDatesIssue_data.xsql	Thu Jan 21 13:57:04 2016 +0100
@@ -0,0 +1,98 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+ *************************************************************************
+ * 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) 2016 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+-->
+<SqlClass name="PgJdbcDatesIssueData" package="org.openbravo.modulescript">
+  <SqlMethod name="existsAlertRule" type="preparedStatement" return="boolean">
+    <Sql><![CDATA[
+       SELECT COUNT(*) AS EXISTING
+       FROM AD_ALERTRULE
+       WHERE NAME = ?
+      ]]>
+    </Sql>
+    <Parameter name="alertRule"/>
+  </SqlMethod>
+  
+  <SqlMethod name="existsAlert" type="preparedStatement" return="boolean">
+    <Sql><![CDATA[
+       SELECT COUNT(*) AS EXISTING
+       FROM AD_ALERTRULE
+       WHERE NAME = ?
+      ]]>
+    </Sql>
+    <Parameter name="name"/>
+  </SqlMethod>
+  
+  <SqlMethod name="getAlertRuleId" type="preparedStatement" return="string">
+    <SqlMethodComment></SqlMethodComment>
+    <Sql><![CDATA[
+       SELECT MAX(ad_alertrule_id) AS id
+       FROM AD_ALERTRULE
+       WHERE NAME = ?
+      ]]></Sql>
+    <Parameter name="name"/>
+  </SqlMethod>
+  
+  <SqlMethod name="insertAlertRule" type="preparedStatement" return="rowcount">
+    <SqlMethodComment></SqlMethodComment>
+    <Sql><![CDATA[
+      INSERT INTO AD_ALERTRULE (
+        AD_ALERTRULE_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
+        CREATED, CREATEDBY,  UPDATED, UPDATEDBY,
+        NAME, TYPE
+      ) VALUES (
+        get_uuid(), '0', '0', 'Y',
+        now(), '0', now(), '0',
+        ?, 'E'
+      )
+    ]]></Sql>
+    <Parameter name="name"/>
+  </SqlMethod>
+  
+  <SqlMethod name="insertAlert" type="preparedStatement" return="rowcount">
+    <Sql><![CDATA[
+      INSERT INTO AD_Alert (
+        AD_Alert_ID, AD_Client_ID, AD_Org_ID, IsActive,
+        Created, CreatedBy, Updated, UpdatedBy,
+        Description, AD_AlertRule_ID, Referencekey_ID
+      ) VALUES (
+        get_uuid(), '0', '0', 'Y',
+        NOW(), '0', NOW(), '0',
+        ?, ?, '0')
+      ]]>
+    </Sql>
+    <Parameter name="description" />
+    <Parameter name="adAlertRuleId" />
+  </SqlMethod>
+  
+  <SqlMethod name="insertAlertRecipient" type="preparedStatement" return="rowcount">
+    <Sql><![CDATA[
+    INSERT INTO ad_alertrecipient(
+            ad_client_id, ad_org_id, isactive, created, createdby, 
+            updated, updatedby, ad_alertrecipient_id, ad_alertrule_id, ad_role_id, 
+            sendemail)
+    select ad_client_id, ad_org_id, 'Y', now(), '0',
+           now(), '0', get_uuid(), ?, ad_role_id, 'N'
+      from ad_role
+     where isactive = 'Y'
+      ]]>
+    </Sql>
+    <Parameter name="adAlertRuleId" />
+  </SqlMethod>
+
+</SqlClass>
\ No newline at end of file