#####################################################################
#####################################################################
## Versavant Topic Map Application Bus / Subject Addressing Engine ##
## Copyright 2005 Steven R. Newcomb                                ##
##                                                                 ##
## This software is licensed under the Apache License, Version 2.0 ##
## (the "License"); you may not use this file except in compliance ##
## with the License.  Copies of the License are available 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.        ##
##                                                                 ##
## Copies of the source code and the license are available at      ##
##        http://www.versavant.org                                 ##
#####################################################################
#####################################################################

####################################
cvsRevision = "$Revision: 1.2 $" ##
####################################


###################################################################
## setOfProxyRefTuples  (for a_sip properties in BIGASSERT, and x_roles properties in TRA_assert)
###################################################################

import types

#################################################
## implementation of                           ##
## setOfProxyRefTuples                         ##
#################################################

#######################################################
def ValueIsNotOfType_setOfProxyRefTuples_( vsv, propertyValue, propertyClass, propertyInstance = None):
    if not isinstance( propertyValue, types.ListType):
        vsv.errMsg( 'Value "%s" is not a list.' % ( propertyValue))
        return True
    for tuple in propertyValue:
        if not isinstance( tuple, types.TupleType):
            vsv.errMsg( 'Value is a list that contains an item (%s) that is not a tuple.' % ( tuple))
            return True
        for member in tuple:
            if not isinstance( member, types.StringType):
                vsv.errMsg( 'Value is a list that contains an item that is a tuple that contains a member (%s) that is not a string.' % ( member))
                return True
            if propertyInstance:
                proxy = vsv.isProxyId( member, propertyInstance.topicMap)
                if not proxy:
                    vsv.errMsg( 'Value is a list that contains an item that is a tuple that contains a member (%s) that is a string that is not a proxyId in topic map "%s".' % ( member, propertyInstance.topicMap.topicMapName))
                    return True
                if proxy.forwardTo:
                    vsv.errMsg( 'Value is a list that contains an item that is a tuple that contains a member (%s) that is a string that is the proxyId (in topic map "%s") of a dead proxy.  Only living proxies can be referenced.' % ( member, propertyInstance.topicMap.topicMapName))
                    return True
            else:
                proxy = vsv.isProxyId( member)
                if not proxy:
                    vsv.errMsg( 'Value is a list that contains an item that is a tuple that contains a member (%s) that is a string that is not a proxyId.' % ( member))
                    return True
                if proxy.forwardTo:
                    vsv.errMsg( 'Value is a list that contains an item that is a tuple that contains a member (%s) that is a string that is the proxyId of a dead proxy.  Only living proxies can be referenced.' % ( member))

    return False

#######################################################
def InstanceOf_setOfProxyRefTuples_ValueTypeToXMLContentString( vsv, propertyInstance):
    return vsv.cPickleDumpObjectAsXMLContentString( propertyInstance._value)

#######################################################
def XMLContentStringToInstanceOf_setOfProxyRefTuples_ValueType( vsv, string):
    ## Import a list of proxy references, adjusting them as necessary to
    ## the new proxyIds of the imported proxies to which they refer.

    oldSetOfProxyRefTuples = vsv.cPickleLoadObjectFromXMLContentString( string)

    mem2xmlProxyIdDict = vsv.currentTopicMapHoldingDict[ 'IoMapsDict'][ 'ProxyIds.mem2xml']
        ## This dictionary allows us to look up the in-xml element id of a
        ## proxy, given the proxy's proxyId as it was in the topic map
        ## from which the proxy was exported as xml.

    firstImportedProxyId = int( args[ 'firstImportedProxyId'])
        ## This is an integer which, if we were to convert it into a
        ## string of decimal digits with 2 prepended zero characters,
        ## would be the proxyId of the first (the first, that is, not
        ## counting the proxies created in order to register the TMAs
        ## of the xml topic map being imported) in-memory proxy to be
        ## imported into the being-imported-into topic map from the
        ## xml topic map that we're importing.

    propertyClassName = args[ 'propertyClassName']

    newSetOfProxyRefTuples = []

    for oldTuple in oldSetOfProxyRefTuples:
        newTupleList = []
        for oldProxyId in oldTuple:
            xmlProxyNum = mem2xmlProxyIdDict[ int( oldProxyId)]
            newProxyNum = (xmlProxyNum + firstImportedProxyId) - 1
            newProxyId = '00%s' % ( newProxyNum)

            newTupleList.append( newProxyId)

            appDict = vsv.currentTopicMapHoldingDict[ 'IoMapsDict'][ 'TMAs'][ args[ 'TMAName']]
            if not appDict.has_key( propertyClassName):
                appDict[ propertyClassName] = [ newProxyId]
            else:
                if newProxyId not in appDict[ propertyClassName]:
                    appDict[ propertyClassName].append( newProxyId)

        newSetOfProxyRefTuples.append( tuple( newTupleList))

    ## vsv.IoMapsDict[ args[ 'TMAName']][ propertyClassName] will be consulted by
    ## the ValueIsNotOfType__() function in order to avoid a logical logjam over this.
    ## The logical logjam is that we're referring to proxies that haven't been imported
    ## yet, and the ValueIsNotOfType__() function checks to see that the proxy references are valid.
    ## This gives the ValueIsNotOfType__() function something to fall back on, as it checks.
    ## See that function, above, for more.

    return newSetOfProxyRefTuples

