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


import sys

#################################################################
## a_sip PC implementation                                     ##
## (mostly it's in ${VSVEXECDIR}/Lib/PCsetOfProxyRefTuples.py, ##
## and ValueIsInvalidFor_a_sip_Property() is recycled from     ##
## ${VSVEXECDIR}/Lib/BIGASSERT/propertyClasses.py.)            ##
#################################################################

###########################################
## t_sip PC implementation               ##
## (mostly re-uses Lib/PCproxyIdSet.py,
## and ValueIsInvalidFor_t_sip_Property() is
## recycled from ${VSVEXECDIR}/Lib/BIGASSERT/propertyClasses.py.) ##
###########################################

###########################################
## r_type PC implementation               ##
## (mostly re-uses Lib/PCproxyIdSet.py) ##
## and ValueIsInvalidFor_r_type_Property() is
## recycled from ${VSVEXECDIR}/Lib/BIGASSERT/propertyClasses.py.) ##
###########################################

###########################################
## a_type PC implementation               ##
## (mostly re-uses Lib/PCproxyIdSet.py) ##
## and ValueIsInvalidFor_a_type_Property() is
## recycled from ${VSVEXECDIR}/Lib/BIGASSERT/propertyClasses.py.) ##
###########################################

#################################################################
## r_rolePlayers PC implementation                                     ##
## (mostly it's in ${VSVEXECDIR}/Lib/PCsetOfProxyRefTuples.py) ##
#################################################################

#######################################################
def ValueIsInvalidFor_r_rolePlayers_Property( vsv, propertyValue, propertyClass, propertyInstance = None, **args):
    ## There is really nothing to be done here, except possibly to doublecheck the reverse
    ## references, but we're not doing that anywhere else yet, so why here?
    return False


###########################################
## x_roles PC implementation               ##
## (mostly re-uses ${VSVEXECDIR}/Lib/PCsetOfProxyRefTuples) ##
###########################################

#######################################################
def ValueIsInvalidFor_x_roles_Property( vsv, propertyValue, propertyClass, propertyInstance = None, **args):
    r_proxy_allowed_properties = args[ 'r_proxy_allowed_properties']
    a_proxy_allowed_properties = args[ 'a_proxy_allowed_properties']
    TMAName = propertyClass.TMA.TMAName
    topicMap = propertyClass.topicMap

    for tuple in propertyValue:
        memberCount = len( tuple)
        if memberCount != 2:
            if propertyInstance:
                vsv.errMsg( 'Tuple in %s property\'s value has %d members; should have 2 (a role and a relationship).' % ( propertyInstance.propertyInstanceKey, memberCount))
            else:
                vsv.errMsg( 'Tuple in presumptive %s property\'s value has %d members; should have 2 (a role and a relationship).' % ( vsv.catKey( TMAName, propertyClass.propertyClassName), memberCount))
            return True
        memberNumber = 0
        for proxyId in tuple:
            memberNumber += 1
            if not vsv.nowImporting:
                proxy = vsv.isProxyId( proxyId, topicMap)
                if not proxy:
                    if propertyInstance:
                        vsv.errMsg( 'Tuple in value of %s property has a member (%s) that is not a proxyId.' % ( propertyInstance.propertyInstanceKey, proxyId))
                    else:
                        vsv.errMsg( 'Tuple in value of presumptive %s property has a member (%s) that is not a proxyId.' % ( vsv.catKey( TMAName, propertyClass.propertyClassName), proxyId))
                    return True

                if memberNumber == 1:  ## this should be a reference to an r-proxy
                    for propertyInstanceKey2 in proxy.propertyInstances:
                        tmaName, propertyClassName = vsv.parsePropertyKey2( propertyInstanceKey2)
                        if TMAName == tmaName:
                            if propertyClassName not in r_proxy_allowed_properties:
                                if propertyInstance:
                                    vsv.errMsg( 'Proxy referenced as first member of tuple "%s" in value of %s property value has a "%s" property, which, as an r-proxy, it should not have.' % ( tuple, propertyInstance.propertyInstanceKey, propertyClassName))
                                else:
                                    vsv.errMsg( 'Proxy referenced as first member of tuple "%s" in value of presumptive %s property value has a "%s" property, which, as an r-proxy, it should not have.' % ( tuple, vsv.catKey( TMAName, propertyClass.propertyClassName), propertyClassName))
                                return True
                else:                  ## memberNumber == 2; this should be a reference to an a-proxy
                    for propertyInstanceKey2 in proxy.propertyInstances:
                        tmaName, propertyClassName = vsv.parsePropertyKey2( propertyInstanceKey2)
                        if TMAName == tmaName:
                            if propertyClassName not in a_proxy_allowed_properties:
                                if propertyInstance:
                                    vsv.errMsg( 'Proxy referenced as second member of tuple "%s" in value of %s property value has a "%s" property, which, as an a-proxy, it should not have.' % ( tuple, propertyInstance.propertyInstanceKey, propertyClassName))
                                else:
                                    vsv.errMsg( 'Proxy referenced as second member of tuple "%s" in value of presumptive %s property value has a "%s" property, which, as an a-proxy, it should not have.' % ( tuple, vsv.catKey( TMAName, propertyClass.propertyClassName), propertyClassName))
                                return True

    return False



###########################################
## t_assertions PC implementation               ##
## (mostly re-uses Lib/PCproxyIdSet.py) ##
## and ValueIsInvalidFor_t_assertions_Property() is
## recycled from ${VSVEXECDIR}/Lib/BIGASSERT/propertyClasses.py.) ##
###########################################



################################################################################################################
## Context check function for all TRA_assert property classes except x_roles, which needs no such function. ##
################################################################################################################

#######################################################
def ContextOf_TRA_assert_PropertyIsInvalid( vsv, propertyInstance, thisPropertyClassName):
    proxy = propertyInstance.proxy
    TMAName = propertyInstance.propertyClass.TMA.TMAName
    if thisPropertyClassName in [ 'a_sip', 'a_type']:
        allowedPropertiesInSameProxy = [ 'a_sip', 'a_type', 'x_roles']
    elif thisPropertyClassName in [ 't_sip', 't_assertions']:
        allowedPropertiesInSameProxy = [ 't_sip', 't_assertions', 'x_roles']
    elif thisPropertyClassName in [ 'r_type', 'r_rolePlayers']:
        allowedPropertiesInSameProxy = [ 'r_type', 'r_rolePlayers', 'x_roles']
    else:
        vsv.errMsg( 'unrecognized "thisPropertyClassName": %s' % ( thisPropertyClassName))
        return True
    for propertyInstanceKey2 in proxy.propertyInstances:
        tmaName, propertyClassName = vsv.parsePropertyKey2( propertyInstanceKey2)
        if tmaName == TMAName:
            if propertyClassName not in allowedPropertiesInSameProxy:
                vsv.errMsg( 'Proxy %s has a "%s" property, so it should not also have a "%s" property, but it does.' % ( proxy.proxyId, thisPropertyClassName, vsv.catKey( TMAName, propertyClassName)))
                return True
    return False

