MetaTableWrapper.java

/*
** Module   : MetaTableWrapper.java
** Abstract : Wraps data for operations with meta table
**
** Copyright (c) 2013-2024, Golden Code Development Corporation.
**
** -#- -I- --Date-- --------------------------------Description----------------------------------
** 001 IAS 20200819 Created initial version.
** 002 CA  20200924 Replaced Method.invoke with ReflectASM.
**     IAS 20210401 Re-worked for new ORM.
**     ECF 20210910 Code cleanup.
** 003 GBB 20230512 Logging methods replaced by CentralLogger/ConversionStatus.
** 004 TJD 20240123 Java 17 compatibility updates
*/

/*
** 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.meta;

import java.lang.reflect.*;
import java.util.*;

import com.goldencode.p2j.persist.Record;
import com.goldencode.p2j.persist.meta.UserTableStatUpdater.*;
import com.goldencode.p2j.persist.orm.*;
import com.goldencode.p2j.util.*;
import com.goldencode.p2j.util.logging.*;
import com.goldencode.proxy.*;

/**
 * Wraps data for operations with meta table 
 * @param <T> meta table DMO interface
 */
public class MetaTableWrapper<T> 
{
   /** Logger */
   private static final CentralLogger LOG = CentralLogger.get(MetaTableWrapper.class);
   
   /** DMO class */
   private final Class<? extends Record> dmoClass;
   
   /** DMO methods map by name */
   private final Map<Method, Method> methodMap = new HashMap<>();
   
   /** DMO Impl object */
   private Object dmoImpl;
   
   /** Meta table DMO proxy */
   private final T metaTableProxy;
   
   /** Dummy empty class to be used as the super class for proxy. */
   public static class Empty {}
   
   /** DMO operations <code>InvocationHandler</code> */  
   private final InvocationHandler handler = new InvocationHandler() 
   {
      /**
       * Processes a method invocation on a proxy instance and returns the result.  
       */
      @Override
      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
      {
         Method dmoMethod = methodMap.get(method);
         if (dmoMethod == null) {
             throw new IllegalArgumentException("Unknown method: " + method.getName());
         }
         try 
         {
             return Utils.invoke(dmoMethod, dmoImpl, args);
         }
         catch(InvocationTargetException e)
         {
             throw e.getTargetException();
         }
      }
   };
   
   /**
    * Constructor
    * 
    * @param   facet
    *          DMO interface class
    * @param   ifName
    *          DMO interface class simple name 
    */
   public MetaTableWrapper(Class<T> facet, String ifName)
   {
      String dmoName = String.format("%s._meta.%s", DmoMetadataManager.getDmoBasePackage(), ifName);
      Class<? extends Record> implementingClass = null;
      try
      {
         DmoMeta dmoInfo = DmoMetadataManager.getDmoInfo(Class.forName(dmoName));
         implementingClass = dmoInfo.getImplementationClass();
         this.metaTableProxy = ProxyFactory.getProxy(Empty.class, new Class<?>[]{facet}, handler);
         for (Method ifcMeth : MinimalUsertablestat.class.getMethods())
         {
            Method dmoMeth = implementingClass.getMethod(ifcMeth.getName(), ifcMeth.getParameterTypes());
            methodMap.put(ifcMeth, dmoMeth);
         }
      } 
      catch (ClassNotFoundException | IllegalArgumentException | NoSuchMethodException e)
      {
         throw new RuntimeException("Error preparing MetaUsertablestat implementation class", e);
      }
      this.dmoClass = implementingClass;
   }
   
   /**
    * Get DMO interface class
    * 
    * @return DMO interface class
    */
   public Class<? extends Record> getDmoClass()
   {
      return dmoClass;
   }
   
   /**
    * Get DMO methods map by name
    * 
    * @return DMO methods map by name
    */
   public Map<Method, Method> getMethodMap()
   {
      return methodMap;
   }
   
   /**
    * Get meta table DMO proxy
    * 
    * @return meta table DMO proxy
    */
   public T getMetaTableProxy()
   {
      return metaTableProxy;
   }
   
   /**
    * Set DMO Impl object
    * 
    * @param   dmoImpl
    *          DMO Impl object
    */
   public void setDelegate(Object dmoImpl)
   {
      this.dmoImpl = dmoImpl;
   }
}