src-db/database/model/triggers/AD_ROLE_ORGACCESS_TRG.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Wed, 03 Sep 2008 17:55:37 +0000
changeset 1605 8a0fe0193bef
parent 785 8dba91261590
child 2078 cf88ca44cdd2
permissions -rw-r--r--
Merge r2.5x intro trunk
<?xml version="1.0"?>
  <database name="TRIGGER AD_ROLE_ORGACCESS_TRG">
    <trigger name="AD_ROLE_ORGACCESS_TRG" table="AD_ROLE_ORGACCESS" fires="after" insert="true" update="true" delete="true" foreach="statement">
      <body><![CDATA[
    /*************************************************************************
    * The contents of this file are subject to the Compiere Public
    * License 1.1 ("License"); You may not use this file except in
    * compliance with the License. You may obtain a copy of the License in
    * the legal folder of your Openbravo installation.
    * 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  Compiere  ERP &  Business Solution
    * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
    * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
    * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
    * All Rights Reserved.
    * Contributor(s): Openbravo SL
    * Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
    *
    * Specifically, this derivative work is based upon the following Compiere
    * file and version.
    *************************************************************************
    * $Id: AD_Role_OrgAccess_Trg.sql,v 1.3 2003/05/04 06:46:07 jjanke Exp $
    ***
    * Title: Update AD_Role.OrgList / ClientList
    *  for all Roles as otherwise mutating trigger
    * Description:
    ************************************************************************/
    TYPE RECORD IS REF CURSOR;
  CUR_Role RECORD;
  Cur_Org RECORD;
  v_ClientList VARCHAR2(2000):='';
  v_OrgList    VARCHAR2(2000):='';
  v_Client_ID VARCHAR2(32):=-1;
BEGIN
  -- For each Role
  FOR CUR_Role IN
    (SELECT *  FROM AD_Role  FOR UPDATE)
  LOOP
    v_ClientList:='';
    v_OrgList:='';
    v_Client_ID:=-1;
    -- Assemble Client/OrgList
    FOR Cur_Org IN
      (
      SELECT AD_Client_ID,
        AD_Org_ID
      FROM AD_Role_OrgAccess
      WHERE AD_Role_ID=CUR_Role.AD_Role_ID
        AND IsActive='Y'
      ORDER BY AD_Client_ID,
        AD_Org_ID
      )
    LOOP
      IF(v_Client_ID <> Cur_Org.AD_Client_ID) THEN
        v_Client_ID:=Cur_Org.AD_Client_ID;
        IF(LENGTH(v_ClientList) <> 0) THEN
          v_ClientList:=v_ClientList || ',';
        END IF;
        v_ClientList:=v_ClientList || Cur_Org.AD_Client_ID;
      END IF;
      -- Org
      IF(LENGTH(v_OrgList) <> 0) THEN
        v_OrgList:=v_OrgList || ',';
      END IF;
      v_OrgList:=v_OrgList || Cur_Org.AD_Org_ID;
    END LOOP;
    -- Org
    --
    IF(v_ClientList IS NULL OR LENGTH(v_ClientList)=0) THEN
      v_ClientList:=' ';
    END IF;
    IF(v_OrgList IS NULL OR LENGTH(v_OrgList)=0) THEN
      v_OrgList:=' ';
    END IF;
    DBMS_OUTPUT.PUT_LINE(CUR_Role.Name || ': Client=' || CUR_Role.ClientList || '->' || v_ClientList || ' - Org= ' || CUR_Role.OrgList || '->' || v_OrgList) ;
    -- Update Role
    UPDATE AD_Role
      SET ClientList=v_ClientList,
      OrgList=v_OrgList
    WHERE AD_ROLE.AD_ROLE_ID=CUR_Role.AD_ROLE_ID;
  END LOOP;
  -- Role
END AD_ROLE_ORGACCESS_TRG
]]></body>
    </trigger>
  </database>