#####################################################################
#####################################################################
## 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 $" ##
####################################


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

import types, traceback

#################################################
## implementation of                           ##
## setOfString_ProxyRefTuples                         ##
#################################################

#######################################################
def ValueIsNotOfType_setOfString_ProxyRefTuples_( vsv, propertyValue, propertyClass, propertyInstance = None):
    if not isinstance( propertyValue, types.ListType):
        if propertyInstance:
            vsv.errMsg( 'Value "%s" of property "%s" is not a list.' % ( propertyValue, propertyInstance.propertyInstanceKey))
        else:
            vsv.errMsg( 'Value "%s" of property "%s" is not a list.' % ( propertyValue, propertyInstance.propertyInstanceKey))
        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
        memberNumber = 0
        for member in tuple:
            memberNumber += 1
            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 memberNumber == 2:  ## only the second member of the pair is a proxy reference
                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:
                        traceback.print_stack()
                        vsv.errMsg( 'Value "%s" of "%s" 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.' % ( propertyValue, propertyInstance.propertyInstanceKey, member, propertyInstance.topicMap.topicMapName))
###
                        vsv.inspect( proxyId = propertyInstance.proxyId)
###

                        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_setOfString_ProxyRefTuples_ValueTypeToXMLContentString( vsv, propertyInstance):
    return vsv.cPickleDumpObjectAsXMLContentString( propertyInstance._value)

#######################################################
def XMLContentStringToInstanceOf_setOfString_ProxyRefTuples_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.

    oldSetOfString_ProxyRefTuples = 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']

    newSetOfString_ProxyRefTuples = []

    for oldTuple in oldSetOfString_ProxyRefTuples:
        newTupleList = []
        memberNumber = 0
        for oldMember in oldTuple:
            memberNumber += 1
            if memberNumber == 1:
                newTupleList.append( oldMember)
            else:  ## memberNumber == 2: replace the proxyId
                xmlProxyNum = mem2xmlProxyIdDict[ int( oldMember)]
                newProxyNum = (xmlProxyNum + firstImportedProxyId) - 1
                newMember = '00%s' % ( newProxyNum)

                newTupleList.append( newMember)

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

        newSetOfString_ProxyRefTuples.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 newSetOfString_ProxyRefTuples

