Package modTransf.model.extension

Source Code of modTransf.model.extension.ConceptExtensionMngr$MemberPool

//Source file: H:\\temp\\generated\\modTransf\\model\\extension\\ConceptExtensionMngr.java

package modTransf.model.extension;

import java.util.List;
import java.util.ArrayList;
import modTransf.model.ModelHelper;
import modTransf.model.ModelException;
import modTransf.model.NotFoundException;
import modTransf.model.reflect.PropertyDescriptor;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;

/**
* Manager of concept extensions.
* This class allows to get a concept extension for a specified object.
*/
public class ConceptExtensionMngr
{
  /**
   * Map of property extensions by property names. Each entry of the map contain
   * a pool of extensions for a specified name.
   */
  protected Map propertyExtensions;

  /**
   * Map of method extensions by property names. Each entry of the map contain
   * a pool of extensions for a specified name.
   */
  protected Map methodExtensions;

  /**
   * The model helper to which this extension manager is attached.
   */
  protected ModelHelper modelHelper;

  /**
   * Constructor.
   */
  public ConceptExtensionMngr(ModelHelper modelHelper)
  {
    this.modelHelper = modelHelper;
  }

  /**
   * Get the value of the specified property for the specified object. Lookup
   * in registered property extensions.
   * @param bean
   * @param name
   * @return Object
   * @throws NotFoundException If not found.
   * @throws ModelException If an error occur.
   */
  public Object getProperty(Object bean, String name)
    throws ModelException
  {
    // Check if there is a registered property.
    PropertyDescriptor desc = getPropertyExtension(bean, name);
    if(desc==null)
    {
      throw new NotFoundException("No registered extension for property ('"
                                  +name+"', '"+bean.getClass().getName()
                                  +"').");
    }

    // Get the value.
      return desc.get(bean);
  }

  /**
   * Set the value of the specified registered property.
   * @param bean
   * @param name
   * @param value
   * @throws NotFoundException If not found.
   * @throws ModelException If an error occur.
   */
  public void setProperty(Object bean, String name, Object value)
    throws ModelException
  {
    // Check if there is a registered property.
    PropertyDescriptor desc = getPropertyExtension(bean, name);
    if(desc==null)
    {
      throw new NotFoundException("No registered extension for property ('"
                                  +name+"', '"+bean.getClass().getName()
                                  +"').");
    }
    // Set the value.
      desc.set(bean, value);
  }

  /**
   * Get the property extension for the specified object and property name.
   * @param bean
   * @param propertyName
   * @return modTransf.model.extension.ConceptPropertyExtension
   */
  public ConceptPropertyExtension getPropertyExtension(Object bean, String propertyName)
    throws ModelException
  {
    if( propertyExtensions == null )
      return null;

    MemberPool pool = (MemberPool)propertyExtensions.get(propertyName);
    if( pool == null )
      return null;

    return pool.getPropertyExtension(bean);
  }

  /**
   * @param bean
   * @param methodName
   * @return MethodPropertyExtension
   */
  public ConceptMethodExtension getMethodExtension(Object bean, String methodName)
    throws ModelException
  {
    if( methodExtensions == null )
      return null;

    MemberPool pool = (MemberPool)methodExtensions.get(methodName);
    if( pool == null )
      return null;

    return pool.getMethodExtension(bean);
}

  /**
   * @param extension
   */
  public void registerPropertyExtension(ConceptPropertyExtension extension)
  {
    if( propertyExtensions == null )
      propertyExtensions = new HashMap();

    MemberPool pool = (MemberPool)propertyExtensions.get(extension.getName());
    if( pool == null )
    {
      pool = new MemberPool(extension.getName());
      propertyExtensions.put( extension.getName(), pool);
    }
    pool.add(extension);
  }

  /**
   * @param extension
   */
  public void registerMethodExtension(ConceptMethodExtension extension)
  {
    if( methodExtensions == null )
      methodExtensions = new HashMap();

    MemberPool pool = (MemberPool)methodExtensions.get(extension.getName());
    if( pool == null )
    {
      pool = new MemberPool(extension.getName());
      methodExtensions.put( extension.getName(), pool);
    }

    pool.add(extension);
  }

  /**
   * A pool of members searchable by names.
   */
  public class MemberPool extends ArrayList implements List
  {
    private String memberName;

    private ConceptMemberExtension defaultExtension;

    /**
     * Constructor.
     * @param name String
     */
    public MemberPool( String name )
    {
     this.memberName = name;
    }

    public boolean add( Object extension )
    {
      return add((ConceptMemberExtension)extension );
    }

    public boolean add( ConceptMemberExtension extension )
     {
       if( extension.getConceptType() == null )
       {
         // default
         setDefaultExtension( extension );
         return true;
       }
       return super.add(extension );
     }
    /**
     * Access method for the memberName property.
     *
     * @return   the current value of the memberName property
     */
    public String getMemberName()
    {
      return memberName;
    }

    /**
     * Sets the value of the memberName property.
     *
     * @param aMemberName the new value of the memberName property
     */
    public void setMemberName(String aMemberName)
    {
      memberName = aMemberName;
    }

    /**
     * Access method for the defaultPropertyExtension property.
     *
     * @return   the current value of the defaultPropertyExtension property
     */
    public ConceptPropertyExtension getDefaultPropertyExtension()
    {
      return (ConceptPropertyExtension)defaultExtension;
    }

    /**
     * Access method for the defaultMethodExtension property.
     *
     * @return   the current value of the defaultMethodExtension property
     */
    public ConceptMethodExtension getDefaultMethodExtension()
    {
      return (ConceptMethodExtension)defaultExtension;
    }

    /**
     * Sets the value of the defaultMethodExtension property.
     *
     * @param aDefaultMethodExtension the new value of the defaultMethodExtension property
     */
    public void setDefaultExtension(ConceptMemberExtension defaultExtension)
    {
      this.defaultExtension = defaultExtension;
    }

    /**
     * Get an extension by the object type.
     * Return null if not found.
     * @param bean
     * @return ConceptPropertyExtension
     */
    public ConceptPropertyExtension getPropertyExtension(Object bean)
      throws ModelException
    {
      // lookup for the extension in the list
      Iterator iter = iterator();
      while(iter.hasNext())
      {
        ConceptPropertyExtension extension = (ConceptPropertyExtension)iter.next();
        if( modelHelper.isInstanceOf( bean, extension.getConceptType() ) )
          return extension;
      }

      // Not found, try default
      if( defaultExtension != null)
        return (ConceptPropertyExtension)defaultExtension;

//      throw new NotFoundException( "No extension registered for ('"
//                                   + memberName + "', '"
//                                   + bean.getClass().getName() + "'." );
      return null;
    }

    /**
     * @param bean
     * @return ConceptMethodExtension
     */
    public ConceptMethodExtension getMethodExtension(Object bean)
      throws ModelException
    {
      // lookup for the extension in the list
      Iterator iter = iterator();
      while(iter.hasNext())
      {
        ConceptMethodExtension extension = (ConceptMethodExtension)iter.next();
        if( modelHelper.isInstanceOf( bean, extension.getConceptType() ) )
          return extension;
      }

      // Not found, try default
      if( defaultExtension != null)
        return (ConceptMethodExtension)defaultExtension;

//      throw new NotFoundException( "No extension registered for ('"
//                                   + memberName + "', '"
//                                   + bean.getClass().getName() + "'." );
      return null;
    }
  }
}
TOP

Related Classes of modTransf.model.extension.ConceptExtensionMngr$MemberPool

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.