
package org.mule.module.paypal.config.spring;

import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.mule.config.spring.MuleHierarchicalBeanDefinitionParserDelegate;
import org.mule.config.spring.util.SpringXMLUtils;
import org.mule.module.paypal.config.CaptureMessageProcessor;
import org.mule.util.TemplateParser;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

public class CaptureDefinitionParser
    implements BeanDefinitionParser
{

    /**
     * Mule Pattern Info
     * 
     */
    private TemplateParser.PatternInfo patternInfo;

    public CaptureDefinitionParser() {
        patternInfo = TemplateParser.createMuleStyleParser().getStyle();
    }

    public BeanDefinition parse(Element element, ParserContext parserContent) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(CaptureMessageProcessor.class.getName());
        String configRef = element.getAttribute("config-ref");
        if ((configRef!= null)&&(!StringUtils.isBlank(configRef))) {
            builder.addPropertyValue("moduleObject", configRef);
        }
        if ((element.getAttribute("authorizationId")!= null)&&(!StringUtils.isBlank(element.getAttribute("authorizationId")))) {
            builder.addPropertyValue("authorizationId", element.getAttribute("authorizationId"));
        }
        if ((element.getAttribute("complete")!= null)&&(!StringUtils.isBlank(element.getAttribute("complete")))) {
            builder.addPropertyValue("complete", element.getAttribute("complete"));
        }
        if ((element.getAttribute("amount")!= null)&&(!StringUtils.isBlank(element.getAttribute("amount")))) {
            builder.addPropertyValue("amount", element.getAttribute("amount"));
        }
        if (element.hasAttribute("amountCurrency")) {
            builder.addPropertyValue("amountCurrency", element.getAttribute("amountCurrency"));
        }
        if ((element.getAttribute("invoiceId")!= null)&&(!StringUtils.isBlank(element.getAttribute("invoiceId")))) {
            builder.addPropertyValue("invoiceId", element.getAttribute("invoiceId"));
        }
        if ((element.getAttribute("note")!= null)&&(!StringUtils.isBlank(element.getAttribute("note")))) {
            builder.addPropertyValue("note", element.getAttribute("note"));
        }
        if ((element.getAttribute("softDescriptor")!= null)&&(!StringUtils.isBlank(element.getAttribute("softDescriptor")))) {
            builder.addPropertyValue("softDescriptor", element.getAttribute("softDescriptor"));
        }
        BeanDefinition definition = builder.getBeanDefinition();
        definition.setAttribute(MuleHierarchicalBeanDefinitionParserDelegate.MULE_NO_RECURSE, Boolean.TRUE);
        MutablePropertyValues propertyValues = parserContent.getContainingBeanDefinition().getPropertyValues();
        if (parserContent.getContainingBeanDefinition().getBeanClassName().equals("org.mule.config.spring.factories.PollingMessageSourceFactoryBean")) {
            propertyValues.addPropertyValue("messageProcessor", definition);
        } else {
            PropertyValue messageProcessors = propertyValues.getPropertyValue("messageProcessors");
            if ((messageProcessors == null)||(messageProcessors.getValue() == null)) {
                propertyValues.addPropertyValue("messageProcessors", new ManagedList());
            }
            List listMessageProcessors = ((List) propertyValues.getPropertyValue("messageProcessors").getValue());
            listMessageProcessors.add(definition);
        }
        return definition;
    }

    protected String getAttributeValue(Element element, String attributeName) {
        if (!StringUtils.isEmpty(element.getAttribute(attributeName))) {
            return element.getAttribute(attributeName);
        }
        return null;
    }

    private String generateChildBeanName(Element element) {
        String id = SpringXMLUtils.getNameOrId(element);
        if (StringUtils.isBlank(id)) {
            String parentId = SpringXMLUtils.getNameOrId(((Element) element.getParentNode()));
            return ((("."+ parentId)+":")+ element.getLocalName());
        } else {
            return id;
        }
    }

}
