Related to issue 33650:Modulescript to set persist org info
authorAtul Gaware <atul.gaware@openbravo.com>
Thu, 26 Apr 2018 23:45:59 +0530
changeset 33887 793f3d0ba3f5
parent 33886 55af0c670202
child 33888 8c8a72032173
Related to issue 33650:Modulescript to set persist org info

** Added Modulescript to populate AD_Org persist info columns
** Variables are used to get persist org info and then sent
to update method to avoid trigger mutating error in orcl
instance
src-util/modulescript/build/classes/org/openbravo/modulescript/UpdatePersistOrgInfo.class
src-util/modulescript/build/classes/org/openbravo/modulescript/UpdatePersistOrgInfoData.class
src-util/modulescript/src/org/openbravo/modulescript/UpdatePersistOrgInfo.java
src-util/modulescript/src/org/openbravo/modulescript/UpdatePersistOrgInfo_data.xsql
Binary file src-util/modulescript/build/classes/org/openbravo/modulescript/UpdatePersistOrgInfo.class has changed
Binary file src-util/modulescript/build/classes/org/openbravo/modulescript/UpdatePersistOrgInfoData.class has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-util/modulescript/src/org/openbravo/modulescript/UpdatePersistOrgInfo.java	Thu Apr 26 23:45:59 2018 +0530
@@ -0,0 +1,63 @@
+/*
+ *************************************************************************
+ * 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) 2018 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+package org.openbravo.modulescript;
+
+import org.apache.log4j.Logger;
+import org.openbravo.database.ConnectionProvider;
+
+public class UpdatePersistOrgInfo extends ModuleScript {
+
+  private static final Logger log4j = Logger.getLogger(UpdatePersistOrgInfo.class);
+
+  @Override
+  public void execute() {
+    try {
+      ConnectionProvider cp = getConnectionProvider();
+      boolean isPersistOrgInfoUpdated = UpdatePersistOrgInfoData.isPersistOrgInfoUpdated(cp);
+      if (!isPersistOrgInfoUpdated) {
+    	long init = System.currentTimeMillis();
+    	int orgUpdated = 0;
+    	UpdatePersistOrgInfoData[] organizations = UpdatePersistOrgInfoData.selectOrgs(cp);
+    	for (UpdatePersistOrgInfoData org: organizations) {
+          String periodControlAllowedOrg = UpdatePersistOrgInfoData.getPeriodControlAllowedOrg(cp, org.adOrgId);
+          String calendarOwnerOrg = UpdatePersistOrgInfoData.getCalendarOwnerOrg(cp, org.adOrgId);
+          String calendar = UpdatePersistOrgInfoData.getCalendar(cp, calendarOwnerOrg);
+          String legalEntity = UpdatePersistOrgInfoData.getLegalEntityBusinessUnitOrg(cp, org.adOrgId, "LE");
+          String businessUnit = UpdatePersistOrgInfoData.getLegalEntityBusinessUnitOrg(cp, org.adOrgId, "BU");
+          UpdatePersistOrgInfoData.updatePersistOrgInfo(cp, periodControlAllowedOrg, calendarOwnerOrg, calendar, legalEntity, businessUnit, org.adOrgId);
+          orgUpdated ++;
+    	}
+        log4j.info(orgUpdated + " organization/s updated with Persist Info in "+(System.currentTimeMillis() - init)+" ms.");
+    	UpdatePersistOrgInfoData.createPreferencePersistOrgInfoUpdated(cp);
+      }
+    } catch (Exception e) {
+      handleError(e);
+    }
+  }
+
+  @Override
+  protected boolean executeOnInstall() {
+    return true;
+  }
+
+  @Override
+  protected ModuleScriptExecutionLimits getModuleScriptExecutionLimits() {
+    return new ModuleScriptExecutionLimits("0", null, new OpenbravoVersion(3, 0, 33862));
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-util/modulescript/src/org/openbravo/modulescript/UpdatePersistOrgInfo_data.xsql	Thu Apr 26 23:45:59 2018 +0530
@@ -0,0 +1,117 @@
+<?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) 2018 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+-->
+<SqlClass name="UpdatePersistOrgInfoData" package="org.openbravo.modulescript">
+   <SqlClassComment></SqlClassComment>
+   <SqlMethod name="select" type="preparedStatement" return="multiple">
+    <SqlMethodComment></SqlMethodComment>
+    <Sql><![CDATA[
+      SELECT 1 as dummy, '' as AD_Org_ID, '' as C_CALENDAR_ID FROM DUAL
+      ]]>
+    </Sql>
+   </SqlMethod>
+  <SqlMethod name="selectOrgs" type="preparedStatement" return="multiple">
+    <SqlMethodComment></SqlMethodComment>
+    <Sql><![CDATA[
+        SELECT AD_Org_ID
+        FROM AD_Org
+        WHERE isready = 'Y'
+    ]]></Sql>
+    <Field name="dummy" value="void"/>
+  </SqlMethod>
+  <SqlMethod name="updatePersistOrgInfo" type="preparedStatement" return="rowcount">
+    <SqlMethodComment></SqlMethodComment>
+    <Sql><![CDATA[
+        UPDATE AD_Org
+        SET AD_PeriodControlAllowed_Org_ID = ?,
+            AD_CalendarOwner_Org_ID = ?,
+            AD_InheritedCalendar_ID = ?,
+            AD_LegalEntity_Org_ID = ?,
+            AD_BusinessUnit_Org_ID = ?
+        WHERE AD_Org_ID = ?
+    ]]></Sql>
+    <Parameter name="periodControlAllowedOrg"/>
+    <Parameter name="calendarOwnerOrg"/>
+    <Parameter name="inheritedCalendarOrg"/>
+    <Parameter name="legalEntityOrg"/>
+    <Parameter name="businessUnitOrg"/>
+    <Parameter name="orgId"/>
+  </SqlMethod>
+  <SqlMethod name="getPeriodControlAllowedOrg" type="preparedStatement" return="String">
+    <SqlMethodComment></SqlMethodComment>
+    <Sql><![CDATA[
+        SELECT ad_org_getperiodcontrolallowtn(?) AS AD_Org_ID
+        FROM DUAL
+    ]]></Sql>
+    <Parameter name="orgId"/>
+  </SqlMethod>
+  <SqlMethod name="getCalendarOwnerOrg" type="preparedStatement" return="String">
+    <SqlMethodComment></SqlMethodComment>
+    <Sql><![CDATA[
+        SELECT ad_org_getcalendarownertn(?) AS AD_Org_ID
+        FROM DUAL
+    ]]></Sql>
+    <Parameter name="orgId"/>
+  </SqlMethod>
+  <SqlMethod name="getCalendar" type="preparedStatement" return="String">
+    <SqlMethodComment></SqlMethodComment>
+    <Sql><![CDATA[
+        SELECT C_CALENDAR_ID
+        FROM AD_ORG
+        WHERE AD_ORG_ID = ?
+    ]]></Sql>
+    <Parameter name="orgId"/>
+  </SqlMethod>
+  <SqlMethod name="getLegalEntityBusinessUnitOrg" type="preparedStatement" return="String">
+    <SqlMethodComment></SqlMethodComment>
+    <Sql><![CDATA[
+        SELECT ad_get_org_le_bu_treenode(?, ?) AS AD_Org_ID
+        FROM DUAL
+    ]]></Sql>
+    <Parameter name="orgId"/>
+    <Parameter name="orgType"/>
+  </SqlMethod>
+  <SqlMethod name="isPersistOrgInfoUpdated" type="preparedStatement" return="boolean">
+    <SqlMethodComment></SqlMethodComment>
+    <Sql>
+      <![CDATA[
+        SELECT count(*) as exist
+        FROM DUAL
+        WHERE EXISTS (SELECT 1 FROM ad_preference
+                      WHERE attribute = 'PersistOrgInfoUpdated')
+      ]]>
+    </Sql>
+  </SqlMethod>
+  <SqlMethod name="createPreferencePersistOrgInfoUpdated" type="preparedStatement" return="rowcount">
+    <SqlMethodComment></SqlMethodComment>
+    <Sql>
+      <![CDATA[
+        INSERT INTO ad_preference (
+          ad_preference_id, ad_client_id, ad_org_id, isactive,
+          createdby, created, updatedby, updated,
+          attribute, value
+        ) VALUES (
+          get_uuid(), '0', '0', 'Y',
+          '0', NOW(), '0', NOW(),
+          'PersistOrgInfoUpdated', 'Y'
+        )
+      ]]>
+    </Sql>
+  </SqlMethod>
+ </SqlClass>