/*
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
 * Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 * or from the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 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 Lesser General Public License for more details.
 *
 * Copyright (c) 2006 - 2009 Pentaho Corporation and Contributors.  All rights reserved.
 */

package org.pentaho.reporting.libraries.formula.operators;

import java.util.HashMap;
import java.util.Iterator;

import org.pentaho.reporting.libraries.base.config.Configuration;
import org.pentaho.reporting.libraries.base.util.ObjectUtilities;

/**
 * Creation-Date: 02.11.2006, 12:29:27
 *
 * @author Thomas Morgner
 */
public class DefaultOperatorFactory implements OperatorFactory
{
  private static final String INFIX_PREFIX = "org.pentaho.reporting.libraries.formula.operators.infix.";
  private static final String PREFIX_PREFIX = "org.pentaho.reporting.libraries.formula.operators.prefix.";
  private static final String POSTFIX_PREFIX = "org.pentaho.reporting.libraries.formula.operators.postfix.";

  private HashMap infixOperators;
  private HashMap prefixOperators;
  private HashMap postfixOperators;

  public DefaultOperatorFactory()
  {
    infixOperators = new HashMap();
    prefixOperators = new HashMap();
    postfixOperators = new HashMap();
  }

  public void initalize(final Configuration configuration)
  {
    loadInfixOperators(configuration);
    loadPrefixOperators(configuration);
    loadPostfixOperators(configuration);
  }

  private void loadInfixOperators(final Configuration configuration)
  {
    final Iterator infixKeys = configuration.findPropertyKeys(INFIX_PREFIX);
    while (infixKeys.hasNext())
    {
      final String configKey = (String) infixKeys.next();
      if (configKey.endsWith(".class") == false)
      {
        continue;
      }
      final String operatorClass = configuration.getConfigProperty(configKey);
      if (operatorClass == null)
      {
        continue;
      }
      if (operatorClass.length() == 0)
      {
        continue;
      }
      final String tokenKey = configKey.substring
          (0, configKey.length() - ".class".length()) + ".token";
      final String token = configuration.getConfigProperty(tokenKey);
      if (token == null)
      {
        continue;
      }
      final String tokenTrimmed = token.trim();
      
      // this assumption was breaking >=, <=, and <>
      // if (tokenTrimmed.length() != 1)
      // {
      //   continue;
      // }

      final Object operator = ObjectUtilities.loadAndInstantiate
          (operatorClass, DefaultOperatorFactory.class, InfixOperator.class);
      if (operator instanceof InfixOperator)
      {
        infixOperators.put (tokenTrimmed, operator);
      }
    }
  }

  private void loadPrefixOperators(final Configuration configuration)
  {
    final Iterator infixKeys = configuration.findPropertyKeys(PREFIX_PREFIX);
    while (infixKeys.hasNext())
    {
      final String configKey = (String) infixKeys.next();
      if (configKey.endsWith(".class") == false)
      {
        continue;
      }
      final String operatorClass = configuration.getConfigProperty(configKey);
      if (operatorClass == null)
      {
        continue;
      }
      if (operatorClass.length() == 0)
      {
        continue;
      }
      final String tokenKey = configKey.substring
          (0, configKey.length() - ".class".length()) + ".token";
      final String token = configuration.getConfigProperty(tokenKey);
      if (token == null)
      {
        continue;
      }
      final String tokenTrimmed = token.trim();
      
      // this is an invalid assumption
      // if (tokenTrimmed.length() != 1)
      // {
      //  continue;
      // }

      final Object operator = ObjectUtilities.loadAndInstantiate
          (operatorClass, DefaultOperatorFactory.class, PrefixOperator.class);
      if (operator instanceof PrefixOperator)
      {
        prefixOperators.put (tokenTrimmed, operator);
      }
    }
  }

  private void loadPostfixOperators(final Configuration configuration)
  {
    final Iterator infixKeys = configuration.findPropertyKeys(POSTFIX_PREFIX);
    while (infixKeys.hasNext())
    {
      final String configKey = (String) infixKeys.next();
      if (configKey.endsWith(".class") == false)
      {
        continue;
      }
      final String operatorClass = configuration.getConfigProperty(configKey);
      if (operatorClass == null)
      {
        continue;
      }
      if (operatorClass.length() == 0)
      {
        continue;
      }
      final String tokenKey = configKey.substring
          (0, configKey.length() - ".class".length()) + ".token";
      final String token = configuration.getConfigProperty(tokenKey);
      if (token == null)
      {
        continue;
      }
      final String tokenTrimmed = token.trim();
      // this is an invalid assumption
      // if (tokenTrimmed.length() != 1)
      // {
      //   continue;
      // }

      final Object operator = ObjectUtilities.loadAndInstantiate
          (operatorClass, DefaultOperatorFactory.class, PostfixOperator.class);
      if (operator instanceof PostfixOperator)
      {
        postfixOperators.put (tokenTrimmed, operator);
      }
    }
  }

  public InfixOperator createInfixOperator(final String operator)
  {
    return (InfixOperator) infixOperators.get(operator);
  }

  public PostfixOperator createPostfixOperator(final String operator)
  {
    return (PostfixOperator) postfixOperators.get(operator);
  }

  public PrefixOperator createPrefixOperator(final String operator)
  {
    return (PrefixOperator) prefixOperators.get(operator);
  }
}
