Package org.strecks.bind.factory

Source Code of org.strecks.bind.factory.BindSelectFactory

/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package org.strecks.bind.factory;

import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import org.strecks.bind.annotation.BindSelect;
import org.strecks.bind.handler.BindHandler;
import org.strecks.bind.handler.BindSelectHandler;
import org.strecks.converter.Converter;
import org.strecks.converter.handler.ConversionHandler;
import org.strecks.exceptions.ApplicationConfigurationException;
import org.strecks.util.BeanUtils;
import org.strecks.util.ReflectHelper;

/**
* <code>BindHandlerFactory</code> implementation for the <code>BindSelect</code> annotation
* @see org.strecks.bind.annotation.BindSelect
* @author Phil Zoio
*/
public class BindSelectFactory implements BindHandlerFactory
{

  public BindHandler createHandler(Annotation annotation, Method getterMethod, Converter converter, ConversionHandler conversionHandler)
  {

    BindSelect bindAnnotation = (BindSelect) annotation;
    BindSelectHandler handler = new BindSelectHandler();

    String expression = bindAnnotation.targetBeanExpression();
    handler.setTargetBeanExpression(expression);

    String beanLocatingExpression = null;
    String propertyName = null;

    int lastDot = expression.lastIndexOf('.');
    if (lastDot == -1)
    {
      propertyName = expression;
    }
    else
    {
      beanLocatingExpression = expression.substring(0, lastDot);
      propertyName = expression.substring(lastDot + 1);
    }

    handler.setBeanLocatingExpression(beanLocatingExpression);
    handler.setBeanPropertyName(propertyName);
    handler.setBeanPropertyClass(bindAnnotation.idClass());
    handler.setBeanLookupExpression(bindAnnotation.lookupMapExpression());
    handler.setBeanPropertyIdName(bindAnnotation.targetBeanIdProperty());

    if (converter != null)
    {
      handler.setConverter(converter);
    }
    else
    {
      Class<? extends Converter> converterClass = bindAnnotation.idConverter();
      converter = ReflectHelper.createInstance(converterClass, Converter.class);
      converter.setTargetClass(bindAnnotation.idClass());
      handler.setConverter(converter);
    }

    PropertyDescriptor property = BeanUtils.findPropertyForMethod(getterMethod);

    if (property == null)
    {
      throw new ApplicationConfigurationException("Method " + getterMethod + " is not a valid JavaBean property");
    }

    handler.setPropertyDescriptor(property);
    handler.setConversionHandler(conversionHandler);

    return handler;
  }
}
TOP

Related Classes of org.strecks.bind.factory.BindSelectFactory

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.