All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibersap.execution.jca.JCAMapper Maven / Gradle / Ivy

/*
 * Copyright (c) 2008-2012 akquinet tech@spree GmbH
 *
 * This file is part of Hibersap.
 *
 * Hibersap is free software: you can redistribute it and/or modify it under the terms of the GNU
 * Lesser General Public License as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * Hibersap is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with Hibersap. If
 * not, see .
 */

package org.hibersap.execution.jca;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.resource.ResourceException;
import javax.resource.cci.IndexedRecord;
import javax.resource.cci.MappedRecord;
import javax.resource.cci.Record;
import javax.resource.cci.RecordFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibersap.bapi.BapiConstants;
import org.hibersap.execution.UnsafeCastHelper;

/**
 * @author M. Dahm
 */
public class JCAMapper
{
    private static final Log LOG = LogFactory.getLog( JCAMapper.class );

    public MappedRecord mapFunctionMapValuesToMappedRecord( String bapiName, final RecordFactory recordFactory,
                                                            final Map functionMap )
        throws ResourceException
    {
        LOG.info( "mapFunctionMapValuesToMappedRecord() functionMap=" + functionMap );

        MappedRecord mappedInputRecord = recordFactory.createMappedRecord( bapiName );

        final Map importMap = UnsafeCastHelper.castToMap( functionMap.get( BapiConstants.IMPORT ) );
        mapToMappedRecord( recordFactory, mappedInputRecord, importMap );
        final Map tableMap = UnsafeCastHelper.castToMap( functionMap.get( BapiConstants.TABLE ) );
        mapToMappedRecord( recordFactory, mappedInputRecord, tableMap );

        LOG.info( "mapFunctionMapValuesToMappedRecord() record=" + mappedInputRecord );

        return mappedInputRecord;
    }

    public void mapRecordToFunctionMap( final Map functionMap, final Map resultRecordMap )
    {
        LOG.info( "mapRecordToFunctionMap() recordMap=" + resultRecordMap );

        for ( final Entry entry : resultRecordMap.entrySet() )
        {
            final Object recordValue = entry.getValue();
            final String recordKey = entry.getKey();

            LOG.debug( "mapping " + recordValue.getClass().getName() + ": " + recordKey + "=" + recordValue );

            if ( recordValue instanceof MappedRecord )
            {
                final MappedRecord mappedResultRecord = (MappedRecord) recordValue;
                final Map resultMap = new HashMap();
                resultMap.put( mappedResultRecord.getRecordName(), mappedResultRecord );
                Map export = UnsafeCastHelper.castToMap( functionMap.get( BapiConstants.EXPORT ) );
                export.put( recordKey, recordValue );
            }
            else if ( recordValue instanceof IndexedRecord )
            {
                final IndexedRecord indexedResultRecord = (IndexedRecord) recordValue;
                List> table = new ArrayList>();

                for ( Object object : indexedResultRecord )
                {
                    MappedRecord mr = (MappedRecord) object;
                    final Map line = new HashMap();

                    @SuppressWarnings("unchecked")
                    Set keys = mr.keySet();

                    for ( String key : keys )
                    {
                        line.put( key, mr.get( key ) );
                    }
                    table.add( line );
                }

                Map tables = UnsafeCastHelper.castToMap( functionMap.get( BapiConstants.TABLE ) );
                tables.put( indexedResultRecord.getRecordName(), table );
            }
            else
            {
                Map export = UnsafeCastHelper.castToMap( functionMap.get( BapiConstants.EXPORT ) );
                export.put( recordKey, recordValue );
            }
        }
        LOG.info( "mapRecordToFunctionMap() functionMap=" + functionMap );
    }

    @SuppressWarnings("unchecked")
    private void appendToRecord( final Record record, final String fieldName, final Object value )
    {
        if ( record instanceof IndexedRecord )
        {
            ( (IndexedRecord) record ).add( value );
        }
        else
        {
            ( (MappedRecord) record ).put( fieldName, value );
        }
    }

    @SuppressWarnings("unchecked")
    private void mapToMappedRecord( final RecordFactory recordFactory, final Record record,
                                    final Map map )
        throws ResourceException
    {
        for ( final String fieldName : map.keySet() )
        {
            final Object value = map.get( fieldName );

            if ( Map.class.isAssignableFrom( value.getClass() ) )
            {
                final Map structureMap = UnsafeCastHelper.castToMap( value );
                final Record structure = recordFactory.createMappedRecord( fieldName );

                appendToRecord( record, fieldName, structure );

                mapToMappedRecord( recordFactory, structure, structureMap );
            }
            else if ( Collection.class.isAssignableFrom( value.getClass() ) )
            {
                final Collection> tableMap = UnsafeCastHelper.castToCollectionOfMaps( value );
                final IndexedRecord table = recordFactory.createIndexedRecord( fieldName );

                appendToRecord( record, fieldName, table );

                int i = 0;
                for ( final Map row : tableMap )
                {
                    MappedRecord rowRecord = recordFactory.createMappedRecord( fieldName + ":row:" + i );
                    mapToMappedRecord( recordFactory, rowRecord, row );
                    table.add( rowRecord );
                    i++;
                }
            }
            else
            {
                appendToRecord( record, fieldName, value );
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy