# TriggerManagerHandle.java

    /*
    ** Module   : TriggerManagerHandle.java
    ** Abstract : A handle for avoiding accessing the TriggerManager via static API.
    **
    ** Copyright (c) 2023, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- -------------------------------------- Description --------------------------------------
    ** 001 OM  20230915 Added support for generic session triggers.
    ** 002 OM  20231127 Removed unused 'override' parameter. Updated javadoc.
    */

    /*
    ** This program is free software: you can redistribute it and/or modify
    ** it under the terms of the GNU Affero General Public License as
    ** published by the Free Software Foundation, either version 3 of the
    ** License, or (at your option) any later version.
    **
    ** This program is distributed in the hope that it will be useful,
    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    ** GNU Affero General Public License for more details.
    **
    ** You may find a copy of the GNU Affero GPL version 3 at the following
    ** location: https://www.gnu.org/licenses/agpl-3.0.en.html
    **
    ** Additional terms under GNU Affero GPL version 3 section 7:
    **
    **   Under Section 7 of the GNU Affero GPL version 3, the following additional
    **   terms apply to the works covered under the License.  These additional terms
    **   are non-permissive additional terms allowed under Section 7 of the GNU
    **   Affero GPL version 3 and may not be removed by you.
    **
    **   0. Attribution Requirement.
    **
    **     You must preserve all legal notices or author attributions in the covered
    **     work or Appropriate Legal Notices displayed by works containing the covered
    **     work.  You may not remove from the covered work any author or developer
    **     credit already included within the covered work.
    **
    **   1. No License To Use Trademarks.
    **
    **     This license does not grant any license or rights to use the trademarks
    **     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
    **     of Golden Code Development Corporation. You are not authorized to use the
    **     name Golden Code, FWD, or the names of any author or contributor, for
    **     publicity purposes without written authorization.
    **
    **   2. No Misrepresentation of Affiliation.
    **
    **     You may not represent yourself as Golden Code Development Corporation or FWD.
    **
    **     You may not represent yourself for publicity purposes as associated with
    **     Golden Code Development Corporation, FWD, or any author or contributor to
    **     the covered work, without written authorization.
    **
    **   3. No Misrepresentation of Source or Origin.
    **
    **     You may not represent the covered work as solely your work.  All modified
    **     versions of the covered work must be marked in a reasonable way to make it
    **     clear that the modified work is not originating from Golden Code Development
    **     Corporation or FWD.  All modified versions must contain the notices of
    **     attribution required in this license.
    */

    package com.goldencode.p2j.persist.trigger;

    /**
     * A handle for avoiding accessing the TriggerManager directly via its public API. Objects of this class can
     * be used for setting up generic triggers in a similar manner how session triggers are registered from
     * converted code.
     * <p>
     * The generic triggers are Java callback methods which are invoked BEFORE the session database triggers, but
     * they are configured for <strong>all</strong> tables (we may add a mask/regex pattern to filter only
     * specific tables) of a database. The implementer is responsible to write the code for handling the event at
     * a lower level, including table filtering (for the moment) but must take care of full 4GL state:
     * transactions, locking, buffers, undo ability.
     */
    public class TriggerManagerHandle
    {
       /** The manager instance. Uses lazy instantiation. */
       private DatabaseTriggerManager manager = null;

       /** Default c'tor does nothing */
       public TriggerManagerHandle()
       {
       }

       /**
        * Register a generic trigger for a specific event. When such trigger is registered, it will be fired for
        * all events of that type, of all tables from specified database before any other configured session or
        * schema trigger. The execution of the latter two is unaffected of the presence or execution of the
        * generic trigger.
        *
        * @param   eventName
        *          The event as case-insensitive string.
        * @param   database
        *          The database to apply to. (In future revisions, this parameter might be replaced by a table
        *          mask / pattern.)
        * @param   triggerClassName
        *          The trigger class which implements the method for handling the respective event in a generic
        *          manner.
        *
        * @return  {@code null} if the operation is successful and a short description of the problem otherwise.
        */
       public String registerGenericTrigger(String eventName, String database, String triggerClassName)
       {
          return getManager().registerGenericTrigger(eventName, database, triggerClassName);
       }

       /**
        * Register a generic trigger for a ASSIGN event. When such trigger is registered, it will be fired for all
        * ASSIGN events, of all tables from specified database BEFORE any other configured session or schema
        * triggers. The execution of the latter two is unaffected of the presence or execution of the generic
        * trigger.
        *
        * @param   eventName
        *          The event as case-insensitive string.
        * @param   database
        *          The database to apply to. (In future revisions, this parameter might be replaced by a table
        *          mask / pattern.)
        * @param   triggerClassName
        *          The trigger class which implements the method for handling the respective event in a generic
        *          manner.
        * @param   property
        *          The <strong>property</strong> name of the affected field. (Also subject for replacing with a
        *          mask / pattern.)
        *
        * @return  {@code null} if the operation is successful and a short description of the problem otherwise.
        */
       public String registerGenericTrigger(String eventName,
                                            String database,
                                            String triggerClassName,
                                            String property)
       {
          return getManager().registerGenericTrigger(eventName, database, triggerClassName, property);
       }

       /**
        * Unregister a previously set generic trigger given the event type and database/.
        * This is the equivalent of {@code ON <event> OF <table> REVERT} but for generic triggers.
        *
        * @param   eventName
        *          The event as case-insensitive string.
        * @param   database
        *          The database to apply to. In future revisions, this parameter might be replaced by a table
        *          pattern.
        *
        * @return  {@code null} if the operation is successful and a short description of the problem otherwise.
        */
       public String deregisterGenericTrigger(String eventName, String database)
       {
          return getManager().deregisterGenericTrigger(eventName, database);
       }

       /**
        * Unregister a session database trigger for the given event type and buffer class.
        * This is the equivalent of {@code ON Assign OF <table> REVERT} but for generic triggers.
        *
        * @param   eventName
        *          The event as case-insensitive string. Recommended {@code ASSIGN} but this variant of the method
        *          ignores the parameter.
        * @param   database
        *          The database to apply to. In future revisions, this parameter might be replaced by a table
        *          pattern.
        * @param   property
        *          The property that is deregistered from {@code ASSIGN} trigger.
        *
        * @return  {@code null} if the operation is successful and a short description of the problem otherwise.
        */
       public String deregisterGenericTrigger(String eventName, String database, String property)
       {
          return getManager().deregisterGenericAssignTrigger(database, property);
       }

       /**
        * Obtain and caches the manager instance locally.
        *
        * @return  the manager instance.
        */
       private DatabaseTriggerManager getManager()
       {
          if (manager == null)
          {
             manager = DatabaseTriggerManager.get();
          }

          return manager;
       }
    }