001/*
002  GRANITE DATA SERVICES
003  Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005  This file is part of Granite Data Services.
006
007  Granite Data Services is free software; you can redistribute it and/or modify
008  it under the terms of the GNU Library General Public License as published by
009  the Free Software Foundation; either version 2 of the License, or (at your
010  option) any later version.
011
012  Granite Data Services is distributed in the hope that it will be useful, but
013  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015  for more details.
016
017  You should have received a copy of the GNU Library General Public License
018  along with this library; if not, see <http://www.gnu.org/licenses/>.
019*/
020
021package org.granite.tide.spring;
022
023import org.granite.logging.Logger;
024import org.springframework.beans.factory.config.BeanDefinition;
025import org.springframework.beans.factory.support.AbstractBeanDefinition;
026import org.springframework.beans.factory.support.BeanDefinitionBuilder;
027import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
028import org.springframework.beans.factory.xml.ParserContext;
029import org.springframework.util.StringUtils;
030import org.w3c.dom.Element;
031
032/**
033 * @author William Drai
034 */
035public class TidePersistenceBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
036        
037        private static final Logger log = Logger.getLogger(TidePersistenceBeanDefinitionParser.class);
038
039    @Override
040    protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
041        
042        builder.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR);
043        
044        // Set the default ID if necessary
045        if (!StringUtils.hasText(element.getAttribute(ID_ATTRIBUTE)))
046            element.setAttribute(ID_ATTRIBUTE, "org.granite.tide.spring.SpringPersistenceManager");
047        
048        builder.setScope("request");
049        
050        builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
051        
052        String transactionManager = element.getAttribute("transaction-manager");
053        if (transactionManager != null)
054                builder.addConstructorArgReference(transactionManager);
055        else {
056                log.info("No transaction-manager specified, use 'transactionManager' default");
057                builder.addConstructorArgReference("transactionManager");
058        }
059    }
060
061    @Override
062    protected String getBeanClassName(Element element) {
063        return "org.granite.tide.spring.SpringPersistenceManager";
064    }
065}