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

com.gs.fw.common.mithra.notification.MithraDatabaseIdentifierExtractor Maven / Gradle / Ivy

There is a newer version: 18.1.0
Show newest version
/*
 Copyright 2016 Goldman Sachs.
 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 com.gs.fw.common.mithra.notification;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.gs.collections.impl.map.mutable.UnifiedMap;
import com.gs.fw.common.mithra.MithraBusinessException;
import com.gs.fw.common.mithra.MithraObjectPortal;
import com.gs.fw.common.mithra.attribute.Attribute;
import com.gs.fw.common.mithra.attribute.SourceAttributeType;
import com.gs.fw.common.mithra.connectionmanager.IntSourceConnectionManager;
import com.gs.fw.common.mithra.connectionmanager.ObjectSourceConnectionManager;
import com.gs.fw.common.mithra.connectionmanager.SourcelessConnectionManager;
import com.gs.fw.common.mithra.finder.*;
import com.gs.fw.common.mithra.util.InternalList;



public class MithraDatabaseIdentifierExtractor implements MapperStack
{

    private int tableNumber = 1;
    private static final String tablePrefix = "t";
    public static final String DEFAULT_DATABASE_ALIAS = tablePrefix + "0";
    private Map sourceOperationMap;
    private Map sourceOperationMapCopy;
    private Map equalitySourceOperationMap;
    private Map mapperStackToJoinClauseMap;
    private Map databaseIdentifierMap;
    private MapperStackImpl mapperStackImpl = new MapperStackImpl();
    private InternalList joinClauses = null;
    private String aliasPrefix = "t";
    private boolean useExplicitJoins = true;

    private  final JoinClause DEFAULT_EXPLICIT_JOIN_CLAUSE = new ExplicitJoinClause(null, 0).makeReadOnly();
    private  final JoinClause DEFAULT_IMPLICIT_JOIN_CLAUSE = new ImplicitJoinClause(null, 0);
    private boolean copySourceOnSet;

    public MithraDatabaseIdentifierExtractor()
    {
    }

    public MithraDatabaseIdentifierExtractor(String aliasPrefix)
    {
        this.aliasPrefix = aliasPrefix;
    }

    public boolean isUseExplicitJoins()
    {
        return useExplicitJoins;
    }

    public void setUseExplicitJoins(boolean useExplicitJoins)
    {
        this.useExplicitJoins = useExplicitJoins;
    }

    public Map extractDatabaseIdentifierMap(RelatedFinder finder, Set sourceAttributeValueSet)
    {
        this.databaseIdentifierMap = new UnifiedMap();
        for(Iterator it = sourceAttributeValueSet.iterator(); it.hasNext();)
        {
           this.createDatabaseIdentifier(finder.getMithraObjectPortal(), it.next());
        }
        return databaseIdentifierMap;
    }

    public void restoreMapperStack(ObjectWithMapperStack objectWithMapperStack)
    {
        this.mapperStackImpl = (MapperStackImpl) objectWithMapperStack.getMapperStack().clone();
    }

    public void restoreMapperStack(MapperStackImpl mapperStack)
    {
        this.mapperStackImpl = mapperStack;
    }

    public Map extractDatabaseIdentifierMap(Operation op)
    {
        this.databaseIdentifierMap = new UnifiedMap();
        registerOperations(op);
        this.processSourceAttributeEqualities();
        int count = this.getSourceAttributeValueCount();

        for(int i = 0; i < count; i++)
        {
            MithraObjectPortal emptyKeyPortal;
            MapperStackImpl emptyKey = MapperStackImpl.EMPTY_MAPPER_STACK_IMPL;
            SourceOperation sourceOperation = this.getSourceOperation(emptyKey);
            if(sourceOperation == null)
            {
                emptyKeyPortal = op.getResultObjectPortal();
                createDatabaseIdentifier(emptyKeyPortal);
            }
            else
            {
                emptyKeyPortal = sourceOperation.getAttribute().getOwnerPortal();
                createDatabaseIdentifier(emptyKey, emptyKeyPortal, i);
            }

            if(mapperStackToJoinClauseMap != null)
            {

                for(Iterator it = mapperStackToJoinClauseMap.keySet().iterator(); it.hasNext();)
                {
                    MapperStackImpl key = (MapperStackImpl)it.next();
                    if(key.getMapperStack().size() > 0)
                    {
                        SourceOperation relatedSourceOperation = this.getSourceOperation(key);
                        MithraObjectPortal portal = key.getLastMapper().getFromPortal();
                        if(relatedSourceOperation == null)
                        {
                              createDatabaseIdentifier(portal);
                        }
                        else
                        {
                             createDatabaseIdentifier(key, portal, i);
                        }
                    }
                }
            }
        }
        return databaseIdentifierMap;
    }

    public void registerOperations(Operation op)
    {
        op.registerOperation(this, true);
    }

    public void registerRelatedAttributeEquality(Attribute attribute)
    {
        if (this.mapperStackToJoinClauseMap != null && !this.mapperStackImpl.isEmpty() && !this.mapperStackImpl.isAtContainerBoundry())
        {
            JoinClause joinClause = (JoinClause) this.mapperStackToJoinClauseMap.get(this.mapperStackImpl);
            joinClause.addAttribute(attribute);
        }
    }

    public void registerRelatedAttributeEqualityFromMapper(Attribute attribute)
    {
        if (this.mapperStackToJoinClauseMap != null && !this.mapperStackImpl.isEmpty() && !this.mapperStackImpl.isAtContainerBoundry())
        {
            JoinClause joinClause = (JoinClause) this.mapperStackToJoinClauseMap.get(this.mapperStackImpl);
            joinClause.addAttributeFromMapper(attribute);
        }
    }

    private void createDatabaseIdentifier(MithraObjectPortal portal)
    {
        this.createDatabaseIdentifier(portal, null);
    }

    private void createDatabaseIdentifier(MithraObjectPortal portal, Object sourceAttributeValue)
    {
        if (portal.isForTempObject()) return;
        RelatedFinder finder = portal.getFinder();

        String databaseIdentifier = this.getDatabaseIdentifier(portal, sourceAttributeValue);

        DatabaseIdentifierKey dbidKey = new DatabaseIdentifierKey(sourceAttributeValue, finder);
        databaseIdentifierMap.put(dbidKey, databaseIdentifier);
    }

    private void createDatabaseIdentifier(MapperStackImpl mapper, MithraObjectPortal portal, int queryNumber)
    {
        if (portal.isForTempObject()) return;
        RelatedFinder finder = portal.getFinder();

        Object sourceAttributeValue = this.getSourceAttributeValueForSelectedObject(mapper, queryNumber);
        String databaseIdentifier = this.getDatabaseIdentifier(portal, sourceAttributeValue);

        DatabaseIdentifierKey dbidKey = new DatabaseIdentifierKey(sourceAttributeValue, finder);
        databaseIdentifierMap.put(dbidKey, databaseIdentifier);
    }

    private String getDatabaseIdentifier(MithraObjectPortal portal, Object sourceAttributeValue)
    {
        String databaseIdentifier = portal.getPureNotificationId();
        if (databaseIdentifier != null) return databaseIdentifier;
        Object connectionManager = portal.getDatabaseObject().getConnectionManager();
        SourceAttributeType sourceAttributeType = portal.getFinder().getSourceAttributeType();
        if(sourceAttributeValue == null)
        {
            databaseIdentifier = ((SourcelessConnectionManager)connectionManager).getDatabaseIdentifier();
        }
        else if(sourceAttributeType.isStringSourceAttribute())
        {
            databaseIdentifier = ((ObjectSourceConnectionManager)connectionManager).getDatabaseIdentifier(sourceAttributeValue);
        }
        else
        {
            databaseIdentifier = ((IntSourceConnectionManager)connectionManager).getDatabaseIdentifier(((Integer)sourceAttributeValue).intValue());
        }
        return databaseIdentifier;
    }

    private Object getSourceAttributeValueForSelectedObject(MapperStackImpl mapper, int queryNumber)
    {
        return this.getSourceAttributeValue(mapper, queryNumber);
        //return this.getSourceAttributeValue(MapperStackImpl.EMPTY_MAPPER_STACK_IMPL, queryNumber);
    }


    private Object getSourceAttributeValue(MapperStackImpl mapperStack, int queryNumber)
    {
        SourceOperation so = getSourceOperation(mapperStack);
        if (so == null)
        {
            throw new MithraBusinessException("could not find source attribute");
            //throw new MithraBusinessException("could not find source attribute for " + this.getLastMapperClassName(mapperStack));
        }
        return so.getSourceAttributeValue(null, queryNumber, mapperStack.isEmpty() || so == getSourceOperation(MapperStackImpl.EMPTY_MAPPER_STACK_IMPL));
    }

    public SourceOperation getSourceOperation(MapperStackImpl mapperStack)
    {
        processSourceAttributeEqualities();
        return (SourceOperation) this.getSourceOperationMap().get(mapperStack);
    }

    private void processSourceAttributeEqualities()
    {
        if (this.equalitySourceOperationMap != null)
        {
            ArrayList mapperStacksToProcess = new ArrayList(this.getSourceOperationMap().keySet());
            for (int i = 0; i < mapperStacksToProcess.size(); i++)
            {
                MapperStackImpl mapperStack = (MapperStackImpl) mapperStacksToProcess.get(i);
                processAliasEquality(mapperStack, mapperStacksToProcess);
            }
            this.equalitySourceOperationMap = null;
        }
    }

    public boolean hasEqualitySourceOperations()
    {
        return this.equalitySourceOperationMap != null && !this.equalitySourceOperationMap.isEmpty();
    }

    protected int getSourceAttributeValueCount()
    {
        SourceOperation so = getSourceOperation(MapperStackImpl.EMPTY_MAPPER_STACK_IMPL);
        if (so == null) return 1;
        return so.getSourceAttributeValueCount();
    }

    private void processAliasEquality(MapperStackImpl mapperStack, ArrayList aliasesToProcess)
    {
        ArrayList equalities = (ArrayList) this.equalitySourceOperationMap.get(mapperStack);
        if (equalities != null)
        {
            for (int i = 0; i < equalities.size(); i++)
            {
                MapperStackImpl rightMapperStack = (MapperStackImpl) equalities.get(i);
                SourceOperation op = (SourceOperation) this.getSourceOperationMap().get(mapperStack);
                this.setSourceOperation(rightMapperStack, op);
                if (rightMapperStack.isAtContainerBoundry())
                {
                    MapperStackImpl copy = (MapperStackImpl) rightMapperStack.clone();
                    while(copy.isAtContainerBoundry()) copy.popMapperContainer();
                    this.setSourceOperation(copy, op);
                }
                if (!aliasesToProcess.contains(rightMapperStack)) aliasesToProcess.add(rightMapperStack);
            }
        }
    }

    public void setEqualitySourceOperation()
    {
        MapperStackImpl leftList = (MapperStackImpl) this.mapperStackImpl.clone();
        leftList.popMapper();
        while (leftList.isAtContainerBoundry()) leftList.popMapperContainer();
        MapperStackImpl rightList = (MapperStackImpl) this.mapperStackImpl.clone();

        addEqualitySourceOperation(leftList, rightList);
        addEqualitySourceOperation(rightList, leftList);
    }

    private void addEqualitySourceOperation(MapperStackImpl left, MapperStackImpl right)
    {
        ArrayList existing = (ArrayList) this.getEqualitySourceOperationMap().get(left);
        if (existing == null)
        {
            existing = new ArrayList(4);
            this.getEqualitySourceOperationMap().put(left, existing);
        }
        existing.add(right);
    }

    public boolean isMappedAlready(Mapper mapper)
    {
        if (this.mapperStackToJoinClauseMap != null)
        {
            MapperStackImpl copy = (MapperStackImpl) this.mapperStackImpl.clone();
            copy.pushMapper(mapper);
            JoinClause joinClause = (JoinClause) this.mapperStackToJoinClauseMap.get(copy);
            return joinClause != null && joinClause.isMapped();
        }
        return false;
    }

    private Map getOrCreateMapperStackToDatabaseAliasMap()
    {
        if (mapperStackToJoinClauseMap == null) mapperStackToJoinClauseMap = new UnifiedMap(3);
        return mapperStackToJoinClauseMap;
    }

    public Map getRawMapperStackToJoinClauseMap()
    {
        return mapperStackToJoinClauseMap;
    }

    public void reset()
    {
        this.copySourceOnSet = true;
        if (this.sourceOperationMapCopy != null)
        {
            this.sourceOperationMap = this.sourceOperationMapCopy;
            this.sourceOperationMapCopy = null;
        }
        if (this.joinClauses != null)
        {
            for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy