/**
 *
 *
 *
 *  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.tranql.ejb;

import java.lang.reflect.Field;

import javax.ejb.EJBException;

import org.tranql.cache.CacheRow;
import org.tranql.cache.InTxCache;
import org.tranql.field.NestedRowAccessor;
import org.tranql.field.Row;
import org.tranql.identity.GlobalIdentity;
import org.tranql.identity.IdentityDefiner;
import org.tranql.identity.IdentityTransform;
import org.tranql.identity.IdentityTransformException;

/**
 * Accessor Class for accessing an FK field that maps to the PK of another
 * class. This PK a CompoundPK with a single field.
 *
 */
public class CMRCompoundAccessor implements CMPFieldTransform {
    private final CMPFieldTransform cmrField;

    private final CMPFieldTransform cmpField;

    private final IdentityTransform transform;

    private final IdentityDefiner identityDefiner;

    private final Class pkClass;

    private int slot;

    public CMRCompoundAccessor(CMPFieldTransform cmrField,
            CMPFieldTransform cmpField, IdentityTransform transform, IdentityDefiner identityDefiner, Class pkClass,int slot) {
        this.cmrField = cmrField;
        this.cmpField = cmpField;
        this.transform = transform;
        this.identityDefiner = identityDefiner;
        this.pkClass = pkClass;
        this.slot = slot;
    }

    public Object get(InTxCache cache, CacheRow cacheRow) {
        GlobalIdentity id = (GlobalIdentity) cmrField.get(cache, cacheRow);
        Row row = identityDefiner.extractIdentity(id);
        CMPFieldNestedRowAccessor accessor = new CMPFieldNestedRowAccessor(cmpField, slot);
        return accessor.get(cache,cacheRow);


    }

    public void set(InTxCache cache, CacheRow row, Object value) {
        GlobalIdentity id;
        Object pkObject;
        try {
            pkObject = pkClass.newInstance();
            Field[] fields = pkClass.getFields();
            fields[0].set(pkObject, value);
        } catch (IllegalArgumentException e) {
            throw new EJBException("Cannot extract global identity", e);
        } catch (IllegalAccessException e) {
            throw new EJBException("Cannot extract global identity", e);
        } catch (InstantiationException e) {
            throw new EJBException("Cannot extract global identity", e);
        }

        try {
            id = transform.getGlobalIdentity(pkObject);
        } catch (IdentityTransformException e) {
            throw new EJBException("Cannot extract global identity", e);
        }
        cmpField.set(cache, row, value);
        cmrField.set(cache, row, id);

    }
}
