
package org.mule.modules.quickbooks.config.spring;

import org.apache.commons.lang.StringUtils;
import org.mule.api.lifecycle.Disposable;
import org.mule.api.lifecycle.Initialisable;
import org.mule.config.spring.MuleHierarchicalBeanDefinitionParserDelegate;
import org.mule.config.spring.parsers.generic.AutoIdUtils;
import org.mule.modules.quickbooks.config.QuickBooksModuleOAuth1Adapter;
import org.mule.util.TemplateParser;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

public class QuickBooksModuleConfigDefinitionParser
    implements BeanDefinitionParser
{

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

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

    public BeanDefinition parse(Element element, ParserContext parserContent) {
        String name = element.getAttribute("name");
        if ((name == null)||StringUtils.isBlank(name)) {
            element.setAttribute("name", AutoIdUtils.getUniqueName(element, "mule-bean"));
        }
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(QuickBooksModuleOAuth1Adapter.class.getName());
        if (Initialisable.class.isAssignableFrom(QuickBooksModuleOAuth1Adapter.class)) {
            builder.setInitMethodName(Initialisable.PHASE_NAME);
        }
        if (Disposable.class.isAssignableFrom(QuickBooksModuleOAuth1Adapter.class)) {
            builder.setDestroyMethodName(Disposable.PHASE_NAME);
        }
        if ((element.getAttribute("realmId")!= null)&&(!StringUtils.isBlank(element.getAttribute("realmId")))) {
            builder.addPropertyValue("realmId", element.getAttribute("realmId"));
        }
        if ((element.getAttribute("consumerKey")!= null)&&(!StringUtils.isBlank(element.getAttribute("consumerKey")))) {
            builder.addPropertyValue("consumerKey", element.getAttribute("consumerKey"));
        }
        if ((element.getAttribute("consumerSecret")!= null)&&(!StringUtils.isBlank(element.getAttribute("consumerSecret")))) {
            builder.addPropertyValue("consumerSecret", element.getAttribute("consumerSecret"));
        }
        if ((element.getAttribute("client-ref")!= null)&&(!StringUtils.isBlank(element.getAttribute("client-ref")))) {
            builder.addPropertyValue("client", new RuntimeBeanReference(element.getAttribute("client-ref")));
        }
        Element httpCallbackConfigElement = DomUtils.getChildElementByTagName(element, "http-callback-config");
        if (httpCallbackConfigElement!= null) {
            if ((httpCallbackConfigElement.getAttribute("domain")!= null)&&(!StringUtils.isBlank(httpCallbackConfigElement.getAttribute("domain")))) {
                builder.addPropertyValue("domain", httpCallbackConfigElement.getAttribute("domain"));
            }
            if ((httpCallbackConfigElement.getAttribute("localPort")!= null)&&(!StringUtils.isBlank(httpCallbackConfigElement.getAttribute("localPort")))) {
                builder.addPropertyValue("localPort", httpCallbackConfigElement.getAttribute("localPort"));
            }
            if ((httpCallbackConfigElement.getAttribute("remotePort")!= null)&&(!StringUtils.isBlank(httpCallbackConfigElement.getAttribute("remotePort")))) {
                builder.addPropertyValue("remotePort", httpCallbackConfigElement.getAttribute("remotePort"));
            }
            if ((httpCallbackConfigElement.getAttribute("async")!= null)&&(!StringUtils.isBlank(httpCallbackConfigElement.getAttribute("async")))) {
                builder.addPropertyValue("async", httpCallbackConfigElement.getAttribute("async"));
            }
        }
        BeanDefinition definition = builder.getBeanDefinition();
        definition.setAttribute(MuleHierarchicalBeanDefinitionParserDelegate.MULE_NO_RECURSE, Boolean.TRUE);
        return definition;
    }

}
