/*
 * JBoss, Home of Professional Open Source
 * Copyright 2007, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags.
 * See the copyright.txt in the distribution for a full listing
 * of individual contributors.
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU General Public License, v. 2.0.
 * This program is distributed in the hope that it will be useful, but WITHOUT A
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License,
 * v. 2.0 along with this distribution; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 *
 * (C) 2007,
 * @author JBoss Inc.
 */
package org.jboss.txbridge.ba.helper;

import org.jboss.txbridge.ba.annotation.BADataManagement;
import org.jboss.txbridge.ba.datamgmt.DataManagerImpl;
import org.jboss.txbridge.ba.datamgmt.DataManager;
import org.jboss.txbridge.ba.data.TaskDescription;
import org.apache.log4j.Logger;

import java.lang.reflect.Field;

/**
 * This component provides a method that can be used to inject a datamgmt manager to an object on
 * which a datamgmt action is to be executed. Such datamgmt manager can be used to retrieve
 * and additional data, which may be required by the datamgmt action.
 *
 * @author Maciej P. Machulak (mmachulak@redhat.com)
 * @version 0.1
 */
public class DataManagement
{
    // Logger
    private static Logger log = Logger.getLogger(DataManagement.class);

    public static synchronized Object injectCompensationManager(Object targetObject, TaskDescription taskDesc)
    {
        // Inject the transaction identifier
        log.info("injectCompensationManager()");
        log.info(taskDesc);
        try
        {
            Class clazz = targetObject.getClass();
            log.info("Injecting datamgmt manager");
            Field[] fields = clazz.getDeclaredFields();
            for (Field singleField : fields)
            {
                BADataManagement cm = singleField.getAnnotation(BADataManagement.class);
                if (cm != null)
                {
                    if (singleField.getType().equals(DataManager.class))
                    {
                        log.info("DataManager object found...");
                        log.info("Setting field...");
                        singleField.setAccessible(true);
                        log.info("Injecting...");
                        singleField.set(targetObject,new DataManagerImpl(taskDesc.getTaskId(),taskDesc.getParticipant()));
                        log.info("Returning...");
                        return targetObject;
                    }
                }
            }
        }
        catch (IllegalAccessException iae)
        {
            iae.printStackTrace();
            return null;
        }
        return null;
    }
}
