src-db/database/model/triggers/C_ELEMENTVALUE_TRG.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Wed, 03 Sep 2008 17:55:37 +0000
changeset 1605 8a0fe0193bef
parent 1414 1d5dbe3d9693
child 2078 cf88ca44cdd2
permissions -rw-r--r--
Merge r2.5x intro trunk
<?xml version="1.0"?>
  <database name="TRIGGER C_ELEMENTVALUE_TRG">
    <trigger name="C_ELEMENTVALUE_TRG" table="C_ELEMENTVALUE" fires="after" insert="true" update="true" delete="true" foreach="row">
      <body><![CDATA[
/*************************************************************************
* The contents of this file are subject to the Openbravo  Public  License
* Version  1.0  (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 SL
* All portions are Copyright (C) 2001-2008 Openbravo SL
* All Rights Reserved.
* Contributor(s):  ______________________________________.
************************************************************************/




  v_xTree_ID VARCHAR2(32);
  v_xParent_ID VARCHAR2(32);
  v_NextNo     VARCHAR2(32);
BEGIN
  -- Insert C_ElementValue Trigger
  --  for Translation
  --  and TreeNode
  IF INSERTING THEN
    --  Create Translation Row
    INSERT
    INTO C_ElementValue_Trl
      (
        C_ElementValue_Trl_ID, C_ElementValue_ID, AD_Language, AD_Client_ID,
        AD_Org_ID, IsActive, Created,
        CreatedBy, Updated, UpdatedBy,
        Name, IsTranslated
      )
    SELECT get_uuid(), :new.C_ElementValue_ID,
      AD_Language, :new.AD_Client_ID, :new.AD_Org_ID,
      :new.IsActive, :new.Created, :new.CreatedBy,
      :new.Updated, :new.UpdatedBy, :new.Name,
       'N'
    FROM AD_Language
    WHERE IsActive='Y'
      AND IsSystemLanguage='Y';
    --  Create TreeNode --
    --  get AD_Tree_ID + ParentID
    SELECT e.AD_Tree_ID,
      n.Node_ID
    INTO v_xTree_ID,
      v_xParent_ID
    FROM C_Element e,
      AD_TreeNode n
    WHERE e.AD_Tree_ID=n.AD_Tree_ID
      AND n.Parent_ID IS NULL
      AND e.C_Element_ID=:new.C_Element_ID;
    --  DBMS_OUTPUT.PUT_LINE('Tree='||v_xTree_ID||'  Node='||:new.C_ElementValue_ID||'  Parent='||v_xParent_ID);
    --  Insert into TreeNode
    INSERT
    INTO AD_TreeNode
      (
        AD_TreeNode_ID, AD_Client_ID, AD_Org_ID, IsActive,
        Created, CreatedBy, Updated,
        UpdatedBy, AD_Tree_ID, Node_ID,
        Parent_ID, SeqNo
      )
      VALUES
      (
        get_uuid(), :new.AD_Client_ID, :new.AD_Org_ID, :new.IsActive,
        :new.Created, :new.CreatedBy, :new.Updated,
        :new.UpdatedBy, v_xTree_ID, :new.C_ElementValue_ID,
        v_xParent_ID,(
        CASE :new.IsSummary
          WHEN 'Y'
          THEN 100
          ELSE 999
        END
        )
      )
      ;
    -- Summary Nodes first
  END IF;
  -- Inserting
  -- C_ElementValue update trigger
  --  synchronize name,...
  IF UPDATING THEN
    IF COALESCE(:old.Name, '.') <> COALESCE(:NEW.Name, '.') THEN
      UPDATE C_ElementValue_Trl
        SET IsTranslated='N',
        Updated=now()
      WHERE C_ElementValue_ID=:new.C_ElementValue_ID;
      /** Mutating Trigger
      --   Update Valid Combination Description
      UPDATE    C_ValidCombination
      SET    Updated=now()
      WHERE    Account_ID=:new.C_ElementValue_ID
      OR    User1_ID=:new.C_ElementValue_ID OR User2_ID=:new.C_ElementValue_ID;
      **/
    END IF;
  END IF;
  -- Updating
  IF DELETING THEN
    --  Delete TreeNode --
    --  get AD_Tree_ID
    SELECT e.AD_Tree_ID
    INTO v_xTree_ID
    FROM C_Element e
    WHERE e.C_Element_ID=:old.C_Element_ID;
    DELETE
    FROM AD_TREENODE
    WHERE AD_CLIENT_ID=:old.AD_Client_ID
      AND AD_Tree_ID=v_xTree_ID
      AND Node_ID=:old.C_ElementValue_ID;
  END IF;
  -- Deleting
EXCEPTION
WHEN NO_DATA_FOUND THEN
  RAISE_APPLICATION_ERROR(-20013, 'C_ElementValue InsertTrigger Error: No ClientInfo or parent TreeNode') ;
END C_ELEMENTVALUE_TRG
]]></body>
    </trigger>
  </database>