Ejemplo n.º 1
0
 def __init__(self, rdf_data_type=None, **kwargs):
     if rdf_data_type is None:
         _class_uri = kwargs.get("class_uri")
         _prop_uri = kwargs.get("prop_uri")
         if _prop_uri:
             rdf_data_type = self._find_type(_class_uri, _prop_uri)
     self.lookup = uri(rdf_data_type)
     #! What happens if none of these replacements?
     val = self.lookup.replace(str(XSD), "").\
             replace("xsd:", "").\
             replace("rdf:", "").\
             replace(str(RDF), "")
     if "http" in val:
         val = "string"
     self.prefix = "xsd:{}".format(val)
     self.py_prefix = "xsd_%s" % val
     self.iri = iri("{}{}".format(str(XSD), val))
     self.name = val
     if val.lower() == "literal" or val.lower() == "langstring":
         self.prefix = "rdf:{}".format(val)
         self.iri = iri(str(RDF) + val)
     elif val.lower() == "object":
         self.prefix = "objInject"
         #! Why is uri a new property if an object?
         self.uri = "objInject"
def calculate_default_value(field):
    '''calculates the default value based on the field default input'''
    _calculation_string = field.get("kds_defaultVal", field.get("defaultVal"))
    _return_val = None
    if _calculation_string is None:
        return None
    if _calculation_string.startswith("item_class"):
        _return_val = iri(uri(field.get("kds_classUri",
                                        field.get("classUri"))))
    else:
        _calc_params = _calculation_string.split('+')
        _base = _calc_params[0].strip()
        if len(_calc_params) > 1:
            _add_value = float(_calc_params[1].strip())
        else:
            _add_value = 0
        if _base == 'today':
            _return_val = datetime.datetime.now().date() +\
                    datetime.timedelta(days=_add_value)
        elif _base == 'now':
            _return_val = datetime.datetime.now() +\
                    datetime.timedelta(days=_add_value)
        elif _base == 'time':
            _return_val = datetime.datetime.now().time() +\
                    datetime.timedelta(days=_add_value)
    return _return_val
Ejemplo n.º 3
0
 def __init__(self, rdf_data_type=None, **kwargs):
     if rdf_data_type is None:
         _class_uri = kwargs.get("class_uri")
         _prop_uri = kwargs.get("prop_uri")
         if _prop_uri:
             rdf_data_type = self._find_type(_class_uri, _prop_uri)
     self.lookup = uri(rdf_data_type)
     #! What happens if none of these replacements?
     val = self.lookup.replace(str(XSD), "").\
             replace("xsd:", "").\
             replace("rdf:", "").\
             replace(str(RDF), "")
     if "http" in val:
         val = "string"
     self.prefix = "xsd:{}".format(val)
     self.py_prefix = "xsd_%s" % val
     self.iri = iri("{}{}".format(str(XSD), val))
     self.name = val
     if val.lower() == "literal" or val.lower() == "langstring":
         self.prefix = "rdf:{}".format(val)
         self.iri = iri(str(RDF) + val)
     elif val.lower() == "object":
         self.prefix = "objInject"
         #! Why is uri a new property if an object?
         self.uri = "objInject"
def calculate_default_value(field):
    '''calculates the default value based on the field default input'''
    _calculation_string = field.get("kds_defaultVal", field.get("defaultVal"))
    _return_val = None
    if _calculation_string is None:
        return None
    if _calculation_string.startswith("item_class"):
        _return_val = iri(uri(field.get("kds_classUri",field.get("classUri"))))
    else: 
        _calc_params = _calculation_string.split('+')
        _base = _calc_params[0].strip()
        if len(_calc_params) > 1:
            _add_value = float(_calc_params[1].strip())
        else:
            _add_value = 0
        if _base == 'today':
            _return_val = datetime.datetime.now().date() +\
                    datetime.timedelta(days=_add_value)
        elif _base == 'now':
            _return_val = datetime.datetime.now() +\
                    datetime.timedelta(days=_add_value)
        elif _base == 'time':
            _return_val = datetime.datetime.now().time() +\
                    datetime.timedelta(days=_add_value)
    return _return_val
Ejemplo n.º 5
0
    def set_obj_data(self, **kwargs):
        ''' sets the data for the current form paramters

        **keyword arguments
        subject_uri: the URI for the subject
        class_uri: the rdf class of the subject
        '''
        _class_uri = kwargs.get("class_uri", self.data_class_uri)
        _lookup_class_uri = _class_uri
        subject_uri = kwargs.get("subject_uri", self.data_subject_uri)
        if not is_not_null(subject_uri):
            self.query_data = {}
            return None
        _subform_data = {}
        _parent_field = None
        # test to see if a sub_obj is part of the form.
        '''if self.has_subobj:
            for _field in self.rdf_field_list:
                if _field.type == 'FieldList':
                    for _entry in _field.entries:
                        if _entry.type == 'FormField':
                            _sub_rdf_obj = _entry.form
                            _parent_field = _field
            # if the sub_obj get its data
            if _sub_rdf_obj:
                _subform_data = _sub_rdf_obj.query_data'''
        # send the form to the query generator and get the query data back
        if kwargs.get('query_data') is None:
            return None
            '''self.query_data = convert_obj_to_rdf_namespace(\
                    convert_spo_to_dict(get_data(self, **kwargs)))'''
        else:
            self.query_data = kwargs.get('query_data')
        _data_value = None
        # cycle through the query data and add the data to the fields
        for _item in make_list(self.query_data):
            for _prop in self.rdf_field_list:
                _prop_uri = _prop.kds_propUri
                _class_uri = iri(uri(_prop.kds_classUri))
                for _subject in _item:
                    if _class_uri in _item[_subject].get("rdf_type"):
                        _prop.query_data = _item[_subject].get(_prop_uri)
                        _prop.subject_uri = _subject
                        for _processor in _prop.kds_processors:
                            run_processor(_processor, self, _prop, "load")
                    if _prop.processed_data is not None:
                        #print(_prop_uri, " __ ", _prop.query_data, "--pro--", _prop.processed_data)
                        _prop.old_data = _prop.processed_data
                        _prop.processed_data = None
                    else:
                        _prop.old_data = _prop.query_data
                        #print(_prop_uri, " __ ", _prop.query_data, "--old--", _prop.old_data)
                    if _prop.data is None and _prop.old_data is not None:
                        _prop.data = _prop.old_data
def create_data_sparql_query(obj, **kwargs):
    ''' generates the sparql query for getting an object's data '''
    if not DEBUG:
        debug = False
    else:
        debug = False
    if debug: print("START create_data_sparql_query -----------------------\n")
    if debug:
        print("*** kwargs ***: \n%s \n*** obj ***:\n%s" %
              (pp.pformat(kwargs), pp.pformat(obj.__dict__)))
    from rdfframework import RdfDataType
    subject_uri = kwargs.get("subject_uri", obj.data_subject_uri)
    _class_uri = kwargs.get("class_uri", obj.data_class_uri)
    _formated_val = None
    _lookup_triple = ""
    if obj.rdf_instructions.get("kds_subjectUriTransform"):
        if obj.rdf_instructions.get("kds_subjectUriTransform") == \
                "kdr_UidToRepositoryUri":
            id_value = kwargs.get("id_value")
            if kwargs.get("id_value"):
                _subject_uri = uid_to_repo_uri(id_value)
                subject_uri = _subject_uri
                obj.data_subject_uri = _subject_uri
        elif obj.rdf_instructions.get("kds_subjectUriTransform") == \
                "kdr_UidToTriplestoreUri":
            id_value = kwargs.get("id_value")
            if kwargs.get("id_value"):
                rdf_class = getattr(rdfw(), obj.data_class_uri)
                subject_uri = rdf_class.uri_patterner(id_value)
                obj.data_subject_uri = subject_uri

    elif kwargs.get("id_value") or obj.rdf_instructions.get(
            "kds_lookupPropertyUri"):
        # find the details for formating the sparql query for the supplied
        # id_value or lookup via a property Value
        id_value = kwargs.get("id_value")
        if not id_value:
            id_value = subject_uri
        _kds_propUri = obj.rdf_instructions.get("kds_lookupPropertyUri")
        _rdf_class_uri = obj.rdf_instructions.get("kds_lookupPropertyClass")
        if not _rdf_class_uri:
            _rdf_class_uri = obj.rdf_instructions.get("kds_lookupClassUri")
        _rdf_class = getattr(rdfw(), _rdf_class_uri)
        _rdf_prop = _rdf_class.kds_properties[_kds_propUri]
        _range = make_list(_rdf_prop.get("rdfs_range"))[0]
        _formated_val = RdfDataType(_range.get("rangeClass")).sparql(id_value)
        _lookup_triple = "\t{}\n\t{}\n\t".format(
            make_triple("?subject", "a", iri(uri(_rdf_class.kds_classUri))),
            make_triple("?subject", iri(uri(_kds_propUri)), _formated_val))
        subject_uri = "?subject"

    subject_lookup = kwargs.get("subject_lookup")
    if subject_lookup:
        # subject lookup will pull a subject and all of its related data
        _kds_propUri = iri(uri(subject_lookup.kds_propUri))
        _data_type = uri(make_list(subject_lookup.rdfs_range)[0])
        _prop_value = RdfDataType(_data_type).sparql(\
                str(subject_lookup.data))
        _sparql = render_without_request("sparqlRelatedItemDataTemplate.rq",
                                         prefix=rdfw().get_prefix(),
                                         kds_propUri=_kds_propUri,
                                         prop_value=_prop_value)
        return _sparql
    _lookup_class_uri = _class_uri
    _sparql_args = None
    _sparql_constructor = copy.deepcopy(obj.dependancies)
    if debug:
        print("+++++++++++++++++++++++ Dependancies:")
        pp.pprint(_sparql_constructor)
    _base_subject_finder = None
    _linked_class = None
    _linked_prop = False
    _sparql_elements = []
    _subform_data = {}
    _data_list = obj.is_subobj
    _parent_field = None
    if is_not_null(subject_uri):
        # find the primary linkage between the supplied subjectId and
        # other form classes

        for _rdf_class in _sparql_constructor:
            for _prop in _sparql_constructor[_rdf_class]:
                try:
                    if _class_uri == _prop.get("kds_classUri"):
                        _sparql_args = _prop
                        _linked_class = _rdf_class
                        _sparql_constructor[_rdf_class].remove(_prop)
                        if _rdf_class != _lookup_class_uri:
                            _linked_prop = True
                except:
                    pass
        # generate the triple pattern for linked class
        if debug:
            print("+++++++++++++++++++++++ SPARQL Constructor")
            pp.pprint(_sparql_constructor)
        if _sparql_args:
            # create a binding for multi-item results
            if _data_list:
                _list_binding = "BIND(?classID AS ?itemID) ."
            else:
                _list_binding = ''
            # provide connection triples for the id subject and associated
            # rdf class
            format_string = "{}BIND({} AS ?baseSub) .\n\t{}\n\t{}\n\t{}"
            _base_subject_finder = format_string.format(
                _lookup_triple, iri(subject_uri),
                make_triple("?baseSub", "a", iri(uri(_lookup_class_uri))),
                make_triple("?classID",
                            iri(uri(_sparql_args.get("kds_propUri"))),
                            "?baseSub"), _list_binding)
            # if there is a linkage between subject_uri and another associated
            # property in object
            if _linked_prop:
                # create a binding for multi-item results
                if _data_list:
                    _list_binding = "BIND(?s AS ?itemID) ."
                else:
                    _list_binding = ''
                format_string = \
                            "{}BIND({} AS ?baseSub) .\n\t{}\n\t{}\n\t{}\n\t?s ?p ?o ."
                _sparql_elements.append(format_string.format(\
                                _lookup_triple,
                                iri(subject_uri),
                                make_triple("?baseSub",
                                            "a",
                                            iri(uri(_lookup_class_uri))),
                                make_triple("?s",
                                            iri(uri(_sparql_args.get("kds_propUri"))),
                                            "?baseSub"),
                                _list_binding))
        # iterrate though the classes used in the object and generate the
        # spaqrl triples to pull the data for that class
        for _rdf_class in _sparql_constructor:
            if _rdf_class == _class_uri:
                if _data_list:
                    _list_binding = "BIND(?s AS ?itemID) ."
                else:
                    _list_binding = ''
                if is_not_null(_lookup_triple) and is_not_null(_list_binding):
                    format_string = \
                            "{}BIND({} AS ?basesub).\n\t{}\n\t{}\n\t{}\n\t?s ?p ?o ."
                    _sparql_elements.append(
                        format_string.format(
                            _lookup_triple, iri(subject_uri),
                            make_triple('?baseSub', 'a',
                                        iri(uri(_lookup_class_uri))),
                            make_triple(
                                '?classID',
                                iri(uri(_sparql_args.get("kds_propUri"))),
                                '?s'), "BIND(?classID AS ?itemID) ."))
                else:
                    format_string = \
                                "\t{}BIND({} AS ?s) .\n\t{}\n\t{}\n\t?s ?p ?o ."
                    _sparql_elements.append(
                        format_string.format(
                            _lookup_triple, iri(subject_uri),
                            make_triple("?s", "a",
                                        iri(uri(_lookup_class_uri))),
                            _list_binding))
            for _prop in _sparql_constructor[_rdf_class]:
                if _rdf_class == _class_uri:
                    if _data_list:
                        _list_binding = "BIND(?s AS ?itemID) ."
                    else:
                        _list_binding = ''
                    format_string = \
                                "\t{}BIND({} AS ?baseSub) .\n\t{}\n\t{}\n\t{}\n\t?s ?p ?o ."
                    _sparql_arg = format_string.format(\
                            _lookup_triple,
                            iri(subject_uri),
                            make_triple("?baseSub",
                                        "a",
                                        iri(uri(_lookup_class_uri))),
                            make_triple("?baseSub",
                                        iri(uri(_prop.get("kds_propUri"))),
                                        "?s"),
                            _list_binding)
                    _sparql_elements.append(_sparql_arg)
                elif _rdf_class == _linked_class:
                    _sparql_elements.append("\t{}\n\t{}\n\t?s ?p ?o .".format(
                        _base_subject_finder,
                        make_triple("?classID",
                                    iri(uri(_prop.get("kds_propUri"))), "?s")))
                '''**** The case where an ID looking up a the triples for
                    a non-linked related is not functioning i.e. password
                    ClassID not looking up person org triples if the org
                    class is not used in the form. This may not be a
                    problem ... the below comment out is a start to solving
                     if it is a problem

                    elif _linked_class != self.get_class_name(prop.get(\
                            "classUri")):
                    _sparql_elements.append(
                        "\t" +_base_subject_finder + "\n " +
                        "\t"+ make_triple("?classID", iri(prop.get(\
                                "propUri")), "?s") + "\n\t?s ?p ?o .")'''

        # merge the sparql elements for each class used into one combined
        # sparql union statement
        _sparql_unions = "{{\n{}\n}}".format("\n} UNION {\n".join(\
                _sparql_elements))
        if _data_list:
            _list_binding = "?itemID"
        else:
            _list_binding = ''
        # render the statment in the jinja2 template
        _sparql = render_without_request("sparqlItemTemplate.rq",
                                         prefix=rdfw().get_prefix(),
                                         query=_sparql_unions,
                                         list_binding=_list_binding)
        if debug:
            print("SPARQL query")
            print(_sparql)
        if debug: print("END create_data_sparql_query ---------------------\n")
        return _sparql
Ejemplo n.º 7
0
    def set_obj_data(self, **kwargs):
        ''' sets the data for the current form paramters

        **keyword arguments
        subject_uri: the URI for the subject
        class_uri: the rdf class of the subject
        '''
        if not DEBUG:
            debug = False
        else:
            debug = True
        if debug: print("START Form.set_obj_data rdfforms.py --------------\n")
        _class_uri = kwargs.get("class_uri", self.data_class_uri)
        _lookup_class_uri = _class_uri
        subject_uri = kwargs.get("subject_uri", self.data_subject_uri)
        if not is_not_null(subject_uri) and not kwargs.get('query_data'):
            self.query_data = {}
            return None
        _subform_data = {}
        _parent_field = None
        # test to see if a sub_obj is part of the form.
        '''if self.has_subobj:
            for _field in self.rdf_field_list:
                if _field.type == 'FieldList':
                    for _entry in _field.entries:
                        if _entry.type == 'FormField':
                            _sub_rdf_obj = _entry.form
                            _parent_field = _field
            # if the sub_obj get its data
            if _sub_rdf_obj:
                _subform_data = _sub_rdf_obj.query_data'''
        # send the form to the query generator and get the query data back
        if kwargs.get('query_data') is None:
            return None
            '''self.query_data = convert_obj_to_rdf_namespace(\
                    convert_spo_to_dict(get_data(self, **kwargs)))'''
        else:
            self.query_data = kwargs.get('query_data')
        _data_value = None
        # cycle through the query data and add the data to the fields
        for _item in make_list(self.query_data):
            for _prop in self.rdf_field_list:
                _prop_uri = _prop.kds_propUri
                _class_uri = iri(uri(_prop.kds_classUri))
                for _subject in _item:
                    if _class_uri in _item[_subject].get("rdf_type"):
                        _prop.query_data = _item[_subject].get(_prop_uri)
                        _prop.subject_uri = _subject
                        for _processor in _prop.kds_processors:
                            run_processor(_processor, self, _prop, "load")
                    if _prop.processed_data is not None:
                        #print(_prop_uri, " __ ", _prop.query_data, "--pro--", _prop.processed_data)
                        _prop.old_data = _prop.processed_data
                        _prop.processed_data = None
                    else:
                        _prop.old_data = _prop.query_data
                        #print(_prop_uri, " __ ", _prop.query_data, "--old--", _prop.old_data)
                    if _prop.data is None and _prop.old_data is not None:
                        _prop.data = _prop.old_data
                #pp.pprint(_prop.__dict__)
        if debug: print("END Form.set_obj_data rdfforms.py --------------\n")
    def _make_unique_value_qry(self, form, field):
        debug = False
        _sparql_args = []
        # determine the property and class details of the field
        _prop_uri = field.kds_propUri
        _class_uri = field.kds_classUri
        _range = field.rdfs_range

        # make the base triples for the query
        if _prop_uri:
            _data_value = RdfDataType(None,
                                      class_uri=_class_uri,
                                      prop_uri=_prop_uri).sparql(field.data)
            _sparql_args.append(make_triple("?uri", "a", iri(uri(_class_uri))))
            _sparql_args.append(make_triple("?uri",
                                            iri(uri(_prop_uri)),
                                            _data_value))
        # see if the form is based on a set of triplestore data. if it is
        # remove that triple from consideration in the query
        
        if hasattr(form, "data_subject_uri"):
            _subject_uri = form.data_subject_uri
            _lookup_class_uri = form.data_class_uri
            # if the subject class is the same as the field class
            if _lookup_class_uri == _class_uri and _subject_uri:
                _sparql_args.append("FILTER(?uri!={}) .".format(\
                        iri(_subject_uri)))
            if debug: x=y
            # If not need to determine how the subject is related to the field
            # property
            elif _subject_uri:
                # class links shows the relationship between the classes in a form
                _class_links = form.dependancies
                _linked_lookup_class_uri = None
                # cycle through the class links to find the subject linkage
                for _rdf_class in _class_links:
                    for _prop in _class_links[_rdf_class]:
                        if _lookup_class_uri == _prop.get("kds_classUri"):
                            _linked_lookup_class_uri = _rdf_class
                            _linked_lookup_prop = _prop.get("kds_propUri")
                            break
                # if there is a direct link between the subject class and
                # field class add the sparql arguments
                if _linked_lookup_class_uri == _class_uri:
                    _sparql_args.append(\
                            "OPTIONAL{{?uri {} ?linkedUri}} .".format(\
                                    iri(uri(_linked_lookup_prop))))
                else:
                    # find the indirect linkage i.e.
                    #    field in class A that links to class B with a lookup
                    #    subject in class C
                    for _rdf_class in _class_links:
                        for _prop in _class_links[_rdf_class]:
                            if _class_uri == _prop.get("kds_classUri"):
                                _linked_field_class_uri = _rdf_class
                                _linked_field_prop = _prop.get("kds_propUri")
                                break
                    if _linked_lookup_class_uri == _linked_field_class_uri:
                        _sparql_args.append("OPTIONAL {")
                        _sparql_args.append("?pass {} ?uri .".format(\
                                iri(uri(_linked_field_prop))))
                        _sparql_args.append("?pass {} ?linkedUri .".format(\
                                iri(uri(_linked_lookup_prop))))
                        _sparql_args.append("} .")
                _sparql_args.append(\
                        "BIND(IF(bound(?linkedUri),?linkedUri,'') AS ?link)")
                _sparql_args.append("FILTER(?link!={}).".format(\
                        iri(_subject_uri)))
        return '''{}\nSELECT (COUNT(?uri)>0 AS ?uniqueValueViolation)
{{\n{}\n}}\nGROUP BY ?uri'''.format(rdfw().get_prefix(),
                                    "\n\t".join(_sparql_args))
def create_data_sparql_query(obj, **kwargs):
    ''' generates the sparql query for getting an object's data '''
    if not DEBUG:
        debug = False
    else:
        debug = False
    if debug: print("START create_data_sparql_query -----------------------\n")
    if debug: print("*** kwargs ***: \n%s \n*** obj ***:\n%s" % 
            (pp.pformat(kwargs), pp.pformat(obj.__dict__)))
    from rdfframework import RdfDataType
    subject_uri = kwargs.get("subject_uri", obj.data_subject_uri)
    _class_uri = kwargs.get("class_uri", obj.data_class_uri)
    _formated_val = None
    _lookup_triple = ""
    if obj.rdf_instructions.get("kds_subjectUriTransform"):
        if obj.rdf_instructions.get("kds_subjectUriTransform") == \
                "kdr_UidToRepositoryUri":
            id_value = kwargs.get("id_value") 
            if kwargs.get("id_value"):
                _subject_uri = uid_to_repo_uri(id_value)
                subject_uri = _subject_uri
                obj.data_subject_uri = _subject_uri
        elif obj.rdf_instructions.get("kds_subjectUriTransform") == \
                "kdr_UidToTriplestoreUri":
            id_value = kwargs.get("id_value") 
            if kwargs.get("id_value"):
                rdf_class = getattr(rdfw(), obj.data_class_uri)
                subject_uri = rdf_class.uri_patterner(id_value)
                obj.data_subject_uri = subject_uri
                    
    elif kwargs.get("id_value") or obj.rdf_instructions.get("kds_lookupPropertyUri"):
        # find the details for formating the sparql query for the supplied
        # id_value or lookup via a property Value
        id_value = kwargs.get("id_value")
        if not id_value:
            id_value = subject_uri
        _kds_propUri = obj.rdf_instructions.get("kds_lookupPropertyUri")
        _rdf_class_uri = obj.rdf_instructions.get("kds_lookupPropertyClass")
        if not _rdf_class_uri: 
            _rdf_class_uri = obj.rdf_instructions.get("kds_lookupClassUri")
        _rdf_class = getattr(rdfw(),_rdf_class_uri)
        _rdf_prop = _rdf_class.kds_properties[_kds_propUri]
        _range = make_list(_rdf_prop.get("rdfs_range"))[0]
        _formated_val = RdfDataType(_range.get("rangeClass")).sparql(id_value)
        _lookup_triple = "\t{}\n\t{}\n\t".format(
                make_triple("?subject",
                            "a",
                            iri(uri(_rdf_class.kds_classUri))),
                make_triple("?subject",
                            iri(uri(_kds_propUri)),
                            _formated_val))            
        subject_uri = "?subject"
        
    subject_lookup = kwargs.get("subject_lookup")
    if subject_lookup:
        # subject lookup will pull a subject and all of its related data
        _kds_propUri = iri(uri(subject_lookup.kds_propUri))
        _data_type = uri(make_list(subject_lookup.rdfs_range)[0])
        _prop_value = RdfDataType(_data_type).sparql(\
                str(subject_lookup.data))
        _sparql = render_without_request("sparqlRelatedItemDataTemplate.rq",
                                         prefix=rdfw().get_prefix(),
                                         kds_propUri=_kds_propUri,
                                         prop_value=_prop_value)
        return _sparql          
    _lookup_class_uri = _class_uri
    _sparql_args = None
    _sparql_constructor = copy.deepcopy(obj.dependancies)
    if debug:
        print("+++++++++++++++++++++++ Dependancies:")
        pp.pprint(_sparql_constructor)
    _base_subject_finder = None
    _linked_class = None
    _linked_prop = False
    _sparql_elements = []
    _subform_data = {}
    _data_list = obj.is_subobj
    _parent_field = None
    if is_not_null(subject_uri):
        # find the primary linkage between the supplied subjectId and
        # other form classes
        
        for _rdf_class in _sparql_constructor:
            for _prop in _sparql_constructor[_rdf_class]:
                try:
                    if _class_uri == _prop.get("kds_classUri"):
                        _sparql_args = _prop
                        _linked_class = _rdf_class
                        _sparql_constructor[_rdf_class].remove(_prop)
                        if _rdf_class != _lookup_class_uri:
                            _linked_prop = True
                except:
                    pass
        # generate the triple pattern for linked class
        if debug:
            print("+++++++++++++++++++++++ SPARQL Constructor")
            pp.pprint(_sparql_constructor)
        if _sparql_args:
            # create a binding for multi-item results
            if _data_list:
                _list_binding = "BIND(?classID AS ?itemID) ."
            else:
                _list_binding = ''
            # provide connection triples for the id subject and associated
            # rdf class
            format_string = "{}BIND({} AS ?baseSub) .\n\t{}\n\t{}\n\t{}"
            _base_subject_finder = format_string.format(
                        _lookup_triple,
                        iri(subject_uri),
                        make_triple("?baseSub",
                                    "a",
                                    iri(uri(_lookup_class_uri))),
                        make_triple("?classID",
                                    iri(uri(_sparql_args.get("kds_propUri"))),
                                    "?baseSub"),
                        _list_binding)
           # if there is a linkage between subject_uri and another associated
           # property in object
            if _linked_prop:
                # create a binding for multi-item results
                if _data_list:
                    _list_binding = "BIND(?s AS ?itemID) ."
                else:
                    _list_binding = ''
                format_string = \
                            "{}BIND({} AS ?baseSub) .\n\t{}\n\t{}\n\t{}\n\t?s ?p ?o ."
                _sparql_elements.append(format_string.format(\
                                _lookup_triple,
                                iri(subject_uri),
                                make_triple("?baseSub",
                                            "a",
                                            iri(uri(_lookup_class_uri))),
                                make_triple("?s",
                                            iri(uri(_sparql_args.get("kds_propUri"))),
                                            "?baseSub"),
                                _list_binding))
        # iterrate though the classes used in the object and generate the
        # spaqrl triples to pull the data for that class
        for _rdf_class in _sparql_constructor:
            if _rdf_class == _class_uri:
                if _data_list:
                    _list_binding = "BIND(?s AS ?itemID) ."
                else:
                    _list_binding = ''
                if is_not_null(_lookup_triple) and is_not_null(_list_binding):
                    format_string = \
                            "{}BIND({} AS ?basesub).\n\t{}\n\t{}\n\t{}\n\t?s ?p ?o ."
                    _sparql_elements.append(format_string.format(
                            _lookup_triple,
                            iri(subject_uri),
                            make_triple('?baseSub','a',iri(uri(_lookup_class_uri))),
                            make_triple('?classID',iri(uri(_sparql_args.get("kds_propUri"))),'?s'),
                            "BIND(?classID AS ?itemID) ."))
                else:
                    format_string = \
                                "\t{}BIND({} AS ?s) .\n\t{}\n\t{}\n\t?s ?p ?o ."
                    _sparql_elements.append(format_string.format(
                                _lookup_triple,
                                iri(subject_uri),
                                make_triple("?s", "a", iri(uri(_lookup_class_uri))),
                                _list_binding))
            for _prop in _sparql_constructor[_rdf_class]:
                if _rdf_class == _class_uri:
                    if _data_list:
                        _list_binding = "BIND(?s AS ?itemID) ."
                    else:
                        _list_binding = ''
                    format_string = \
                                "\t{}BIND({} AS ?baseSub) .\n\t{}\n\t{}\n\t{}\n\t?s ?p ?o ."
                    _sparql_arg = format_string.format(\
                            _lookup_triple,
                            iri(subject_uri),
                            make_triple("?baseSub",
                                        "a",
                                        iri(uri(_lookup_class_uri))),
                            make_triple("?baseSub",
                                        iri(uri(_prop.get("kds_propUri"))),
                                        "?s"),
                            _list_binding)
                    _sparql_elements.append(_sparql_arg)
                elif _rdf_class == _linked_class:
                    _sparql_elements.append(
                        "\t{}\n\t{}\n\t?s ?p ?o .".format(
                            _base_subject_finder,
                            make_triple("?classID",
                                        iri(uri(_prop.get("kds_propUri"))),
                                        "?s")
                            )
                        )


                '''**** The case where an ID looking up a the triples for
                    a non-linked related is not functioning i.e. password
                    ClassID not looking up person org triples if the org
                    class is not used in the form. This may not be a
                    problem ... the below comment out is a start to solving
                     if it is a problem

                    elif _linked_class != self.get_class_name(prop.get(\
                            "classUri")):
                    _sparql_elements.append(
                        "\t" +_base_subject_finder + "\n " +
                        "\t"+ make_triple("?classID", iri(prop.get(\
                                "propUri")), "?s") + "\n\t?s ?p ?o .")'''

        # merge the sparql elements for each class used into one combined
        # sparql union statement
        _sparql_unions = "{{\n{}\n}}".format("\n} UNION {\n".join(\
                _sparql_elements))
        if _data_list:
            _list_binding = "?itemID"
        else:
            _list_binding = ''
        # render the statment in the jinja2 template
        _sparql = render_without_request("sparqlItemTemplate.rq",
                                         prefix=rdfw().get_prefix(),
                                         query=_sparql_unions,
                                         list_binding=_list_binding)
        if debug:
            print("SPARQL query")
            print(_sparql)
        if debug: print("END create_data_sparql_query ---------------------\n")
        return _sparql
Ejemplo n.º 10
0
 def __format_data_for_save(self, processed_data, pre_save_data):
     ''' takes the processed data and formats the values for the sparql
         query '''
     if not DEBUG:
         debug = False
     else:
         debug = False
     if debug: print("START RdfClass.__format_data_for_save -----------\n")
     _save_data = []
     #if "obi_recipient" in pre_save_data.keys():
     #    x=y
     if debug: print("format data***********\n")
     if debug: pp.pprint(processed_data)
     # cycle throught the properties in the processed data    
     for _prop_uri, prop in processed_data.items():
         # if the property is maked for deletion add it to the save list
         if isinstance(prop, NotInFormClass):
             pass
         elif isinstance(prop, DeleteProperty):
             _save_data.append([_prop_uri, prop])
         # if the property is a file object send it to the repository and
         # add the new uri to the save data list
         elif isinstance(prop, FileStorage):
             _file_iri = save_file_to_repository(\
                     prop, pre_save_data[_prop_uri][0].get('old'))
             _save_data.append([_prop_uri, _file_iri])
         # otherwise determine the range of the property and format it in 
         # the correct sparl format
         else:
             # some properties have more than one option for the object 
             # value i.e. schema:image can either store a ImageObject or
             # a Url to an image. We need to determine the range options
             _range_list = make_list(self.kds_properties[_prop_uri].get(\
                     "rdfs_range", [{}]))
             _storage_types = set()
             _data_types = set()
             # cycle through the range_list and get the sets of options
             for _range_dict in _range_list:
                 _storage_types.add(_range_dict.get('storageType'))
                 if _range_dict.get('storageType') == "literal":
                     _data_types.add(_range_dict.get('rangeClass'))
             _data_type = "xsd_string"
             for _type in _data_types:
                 if "xsd" in _type:
                     _data_type = _type
             # cycle through the items in the current prop
             _value_list = make_list(prop)
             for item in _value_list:
                 if 'object' in _storage_types or 'blanknode' in \
                         _storage_types:
                     uri_test = uri(item)
                     if debug: print(_prop_uri, " - ", uri_test)
                     if uri_test.startswith("http"):
                         _save_data.append([_prop_uri, iri(uri(item))])
                     elif 'literal' in _storage_types:
                         _save_data.append([_prop_uri, RdfDataType(\
                                 _data_type).sparql(str(item))])
                     else:
                         _save_data.append([_prop_uri, iri(uri(item))])
                 else:
                     _item = str(item).encode('unicode_escape')
                     _save_data.append([_prop_uri, RdfDataType(\
                             _data_type).sparql(str(item))])
     if debug: print("END RdfClass.__format_data_for_save -----------\n")
     return _save_data
Ejemplo n.º 11
0
 def _generate_save_query(self, save_data_obj, subject_uri=None):
     if not DEBUG:
         debug = False
     else:
         debug = False
     if debug: print("START RdfClass._generate_save_query -------------\n")
     _save_data = save_data_obj.get("data")
     # find the subject_uri positional argument or look in the save_data_obj
     # or return <> as a new node
     if not subject_uri:
         subject_uri = iri(uri(save_data_obj.get('subjectUri', "<>")))
     _save_type = self.kds_storageType
     if subject_uri == "<>" and _save_type.lower() == "blanknode":
         _save_type = "blanknode"
     else:
         _save_type = "object"
     new_status = False
     if self.kds_saveLocation == "triplestore" and subject_uri == "<>":
         subject_uri = iri(self.new_uri())
         new_status = True
     _bn_insert_clause = []
     _insert_clause = ""
     _delete_clause = ""
     _where_clause = ""
     _prop_set = set()
     i = 1
     if debug: print("save data in generateSaveQuery\n", \
                 pp.pprint(_save_data))
     # test to see if there is data to save
     if len(_save_data) > 0:
         for prop in _save_data:
             _prop_set.add(uri(prop[0]))
             _prop_iri = iri(uri(prop[0]))
             if not isinstance(prop[1], DeleteProperty):
                 _obj_val = uri(prop[1])
                 _insert_clause += "{}\n".format(\
                                     make_triple(subject_uri, _prop_iri, _obj_val))
                 _bn_insert_clause.append("\t{} {}".format(_prop_iri, _obj_val))
         if subject_uri != '<>' and not new_status:
             for prop in _prop_set:
                 _prop_iri = iri(uri(prop))
                 _delete_clause += "{}\n".format(\
                                 make_triple(subject_uri, _prop_iri, "?"+str(i)))
                 _where_clause += "OPTIONAL {{ {} }} .\n".format(\
                                 make_triple(subject_uri, _prop_iri, "?"+str(i)))
                 i += 1
         else:
             _obj_val = iri(uri(self.kds_classUri))
             _insert_clause += make_triple(subject_uri, "a", _obj_val) + \
                     "\n"
             _bn_insert_clause.append("\t a {}".format(_obj_val))
         if _save_type == "blanknode":
             _save_query = "[\n{}\n]".format(";\n".join(_bn_insert_clause))
         else:
             if subject_uri != '<>' and not new_status:
                 save_query_template = Template("""{{ prefix }}
                                 DELETE \n{
                                 {{ _delete_clause }} }
                                 INSERT \n{
                                 {{ _insert_clause }} }
                                 WHERE \n{
                                 {{ _where_clause }} }""")
                 _save_query = save_query_template.render(
                     prefix=rdfw().get_prefix(),
                     _delete_clause=_delete_clause,
                     _insert_clause=_insert_clause,
                     _where_clause=_where_clause)
             else:
                 _save_query = "{}\n\n{}".format(
                     rdfw().get_prefix("turtle"),
                     _insert_clause)
         if debug: print(_save_query)
         if debug: print("END RdfClass._generate_save_query -------------\n")
         return {"query":_save_query, "subjectUri":subject_uri, 
                 "new_status":new_status}
     else:
         if debug: print("END RdfClass._generate_save_query -------------\n")
         return {"subjectUri":subject_uri}
Ejemplo n.º 12
0
    def validate_primary_key(self, rdf_obj, old_data):
        '''query to see if PrimaryKey is Valid'''
        if not DEBUG:
            debug = False
        else:
            debug = False
        if debug: print("START RdfClass.validate_primary_key --------------\n")
        if debug: print("old_data:\n",json.dumps(old_data,indent=4)) 
        if old_data is None:
            old_data = {}
        _prop_name_list = []
        if hasattr(self, "kds_primaryKey"):
            pkey = self.kds_primaryKey
            if isinstance(pkey, dict):
                pkey = pkey.get("kds_keyCombo",[])
            pkey = make_list(pkey)
        else:
            pkey = []
        if debug: print(self.kds_classUri, " PrimaryKeys: ", pkey, "\n")
        if len(pkey) < 1:
            if debug: print("END RdfClass.validate_primary_key -NO pKey----\n")
            return ["valid"]
        else:
            _calculated_props = self._get_calculated_properties()
            _old_class_data = self._select_class_query_data(old_data)
            _new_class_data = {}
            _query_args = [make_triple("?uri", "a", \
                    iri(uri(self.kds_classUri)))]
            _multi_key_query_args = [make_triple("?uri", 
                                                 "a",
                                                 iri(uri(self.kds_classUri)))]
            _key_changed = False
            _prop_uri_list = []
            _key_props = []
            # get primary key data from the form data
            for prop in rdf_obj:
                if prop.kds_propUri in pkey:
                    _new_class_data[prop.kds_propUri] = prop.data
                    _prop_name_list.append(prop.kds_formLabelName)
                    _key_props.append(prop)
        
            for key in pkey:
                _object_val = None
                #get the _data_value to test against
                _data_value = _new_class_data.get(key, _old_class_data.get(key))
                if is_not_null(_data_value):
                    _range_obj = make_list(self.kds_properties[key].get(\
                            "rdfs_range", [{}]))[0]
                    _data_type = _range_obj.get('storageType')
                    _range = _range_obj.get('rangeClass')
                    if debug: print("_data_type: ", _data_type)
                    if _data_type == 'literal':
                        _object_val = RdfDataType(_range).sparql(_data_value)
                    else:
                        _object_val = iri(uri(_data_value))
                else:
                    # if data is missing from the key fields exit method and 
                    # return valid. *** The object value does not exist and 
                    #                   will be generated when another class 
                    #                   is saved
                    if debug: print(\
                        "END RdfClass.validate_primary_key - NO data-------\n")
                    return ["valid"]
                # if the old_data is not equel to the newData re-evaluate
                # the primaryKey
                
                # if the old value is not equal to the new value need to test 
                # the key
                # if the new value is to be calculated, i.e. a dependant class
                # generating a value then we don't need to test the key.
                # however if there is a supplied value and it is listed as a 
                # calculated property we need to test. 
                if (_old_class_data.get(key) != _new_class_data.get(key)) and \
                        ((key not in _calculated_props) or \
                        _new_class_data.get(key) is not None):
                    _key_changed = True
                    if _object_val:
                        _query_args.append(make_triple("?uri", iri(uri(key)), \
                                _object_val))
                        _multi_key_query_args.append(make_triple("?uri", \
                                iri(uri(key)), _object_val))
                else:
                    if _object_val:
                        _multi_key_query_args.append(make_triple("?uri", \
                                iri(uri(key)), _object_val))
                    else:
                        _key_changed = False
            # if the primary key changed in the form we need to
            # query to see if there is a violation with the new value

            if _key_changed:
                if len(pkey) > 1:
                    args = _multi_key_query_args
                else:
                    args = _query_args
                sparql = '''
                         {}\nSELECT DISTINCT (COUNT(?uri)>0 AS ?keyViolation)
                         {{\n{}\n}}\nGROUP BY ?uri'''.format(\
                                rdfw().get_prefix(),
                                "\n".join(args)) 
                if debug: print("----------- PrimaryKey query:\n", sparql)
                _key_test_results =\
                        requests.post(\
                                self.triplestore_url,
                                data={"query": sparql, "format": "json"})
                if debug: print("_key_test_results: ", _key_test_results.json())
                _key_test = _key_test_results.json().get('results').get( \
                        'bindings', [])
                if debug: print(_key_test)
                if len(_key_test) > 0:
                    _key_test = _key_test[0].get('keyViolation', {}).get( \
                            'value', False)
                else:
                    _key_test = False

                
                if not _key_test:
                    if debug: print(\
                        "END RdfClass.validate_primary_key - Key Passed --\n")
                    return ["valid"]
                else:
                    error_msg = "This {} aleady exists.".format(
                                        " / ".join(_prop_name_list))
                    for prop in _key_props:
                        if hasattr(prop, "errors"):
                            if isinstance(prop.errors, list):
                                prop.errors.append(error_msg)
                            else:
                                prop.errors = [error_msg]
                        else:
                            setattr(prop, "errors", [error_msg])
                    return [{"errorType":"primaryKeyViolation",
                             "formErrorMessage": error_msg,
                             "errorData":{"class": self.kds_classUri,
                                          "propUri": pkey}}]
            if debug: print(\
                "START RdfClass.validate_primary_key - Skipped Everything--\n")
            return ["valid"]
Ejemplo n.º 13
0
 def __format_data_for_save(self, processed_data, pre_save_data):
     ''' takes the processed data and formats the values for the sparql
         query '''
     if not DEBUG:
         debug = False
     else:
         debug = False
     if debug: print("START RdfClass.__format_data_for_save -----------\n")
     _save_data = []
     #if "obi_recipient" in pre_save_data.keys():
     #    x=y
     if debug: print("format data***********\n")
     if debug: pp.pprint(processed_data)
     # cycle throught the properties in the processed data
     for _prop_uri, prop in processed_data.items():
         # if the property is maked for deletion add it to the save list
         if isinstance(prop, NotInFormClass):
             pass
         elif isinstance(prop, DeleteProperty):
             _save_data.append([_prop_uri, prop])
         # if the property is a file object send it to the repository and
         # add the new uri to the save data list
         elif isinstance(prop, FileStorage):
             _file_iri = save_file_to_repository(\
                     prop, pre_save_data[_prop_uri][0].get('old'))
             _save_data.append([_prop_uri, _file_iri])
         # otherwise determine the range of the property and format it in
         # the correct sparl format
         else:
             # some properties have more than one option for the object
             # value i.e. schema:image can either store a ImageObject or
             # a Url to an image. We need to determine the range options
             _range_list = make_list(self.kds_properties[_prop_uri].get(\
                     "rdfs_range", [{}]))
             _storage_types = set()
             _data_types = set()
             # cycle through the range_list and get the sets of options
             for _range_dict in _range_list:
                 _storage_types.add(_range_dict.get('storageType'))
                 if _range_dict.get('storageType') == "literal":
                     _data_types.add(_range_dict.get('rangeClass'))
             _data_type = "xsd_string"
             for _type in _data_types:
                 if "xsd" in _type:
                     _data_type = _type
             # cycle through the items in the current prop
             _value_list = make_list(prop)
             for item in _value_list:
                 if 'object' in _storage_types or 'blanknode' in \
                         _storage_types:
                     uri_test = uri(item)
                     if debug: print(_prop_uri, " - ", uri_test)
                     if uri_test.startswith("http"):
                         _save_data.append([_prop_uri, iri(uri(item))])
                     elif 'literal' in _storage_types:
                         _save_data.append([_prop_uri, RdfDataType(\
                                 _data_type).sparql(str(item))])
                     else:
                         _save_data.append([_prop_uri, iri(uri(item))])
                 else:
                     _item = str(item).encode('unicode_escape')
                     _save_data.append([_prop_uri, RdfDataType(\
                             _data_type).sparql(str(item))])
     if debug: print("END RdfClass.__format_data_for_save -----------\n")
     return _save_data
Ejemplo n.º 14
0
 def _generate_save_query(self, save_data_obj, subject_uri=None):
     if not DEBUG:
         debug = False
     else:
         debug = False
     if debug: print("START RdfClass._generate_save_query -------------\n")
     _save_data = save_data_obj.get("data")
     # find the subject_uri positional argument or look in the save_data_obj
     # or return <> as a new node
     if not subject_uri:
         subject_uri = iri(uri(save_data_obj.get('subjectUri', "<>")))
     _save_type = self.kds_storageType
     if subject_uri == "<>" and _save_type.lower() == "blanknode":
         _save_type = "blanknode"
     else:
         _save_type = "object"
     new_status = False
     if self.kds_saveLocation == "triplestore" and subject_uri == "<>":
         subject_uri = iri(self.new_uri())
         new_status = True
     _bn_insert_clause = []
     _insert_clause = ""
     _delete_clause = ""
     _where_clause = ""
     _prop_set = set()
     i = 1
     if debug:            print("save data in generateSaveQuery\n", \
           pp.pprint(_save_data))
     # test to see if there is data to save
     if len(_save_data) > 0:
         for prop in _save_data:
             _prop_set.add(uri(prop[0]))
             _prop_iri = iri(uri(prop[0]))
             if not isinstance(prop[1], DeleteProperty):
                 _obj_val = uri(prop[1])
                 _insert_clause += "{}\n".format(\
                                     make_triple(subject_uri, _prop_iri, _obj_val))
                 _bn_insert_clause.append("\t{} {}".format(
                     _prop_iri, _obj_val))
         if subject_uri != '<>' and not new_status:
             for prop in _prop_set:
                 _prop_iri = iri(uri(prop))
                 _delete_clause += "{}\n".format(\
                                 make_triple(subject_uri, _prop_iri, "?"+str(i)))
                 _where_clause += "OPTIONAL {{ {} }} .\n".format(\
                                 make_triple(subject_uri, _prop_iri, "?"+str(i)))
                 i += 1
         else:
             _obj_val = iri(uri(self.kds_classUri))
             _insert_clause += make_triple(subject_uri, "a", _obj_val) + \
                     "\n"
             _bn_insert_clause.append("\t a {}".format(_obj_val))
         if _save_type == "blanknode":
             _save_query = "[\n{}\n]".format(";\n".join(_bn_insert_clause))
         else:
             if subject_uri != '<>' and not new_status:
                 save_query_template = Template("""{{ prefix }}
                                 DELETE \n{
                                 {{ _delete_clause }} }
                                 INSERT \n{
                                 {{ _insert_clause }} }
                                 WHERE \n{
                                 {{ _where_clause }} }""")
                 _save_query = save_query_template.render(
                     prefix=rdfw().get_prefix(),
                     _delete_clause=_delete_clause,
                     _insert_clause=_insert_clause,
                     _where_clause=_where_clause)
             else:
                 _save_query = "{}\n\n{}".format(
                     rdfw().get_prefix("turtle"), _insert_clause)
         if debug: print(_save_query)
         if debug:
             print("END RdfClass._generate_save_query -------------\n")
         return {
             "query": _save_query,
             "subjectUri": subject_uri,
             "new_status": new_status
         }
     else:
         if debug:
             print("END RdfClass._generate_save_query -------------\n")
         return {"subjectUri": subject_uri}
Ejemplo n.º 15
0
    def validate_primary_key(self, rdf_obj, old_data):
        '''query to see if PrimaryKey is Valid'''
        if not DEBUG:
            debug = False
        else:
            debug = False
        if debug: print("START RdfClass.validate_primary_key --------------\n")
        if debug: print("old_data:\n", json.dumps(old_data, indent=4))
        if old_data is None:
            old_data = {}
        _prop_name_list = []
        if hasattr(self, "kds_primaryKey"):
            pkey = self.kds_primaryKey
            if isinstance(pkey, dict):
                pkey = pkey.get("kds_keyCombo", [])
            pkey = make_list(pkey)
        else:
            pkey = []
        if debug: print(self.kds_classUri, " PrimaryKeys: ", pkey, "\n")
        if len(pkey) < 1:
            if debug: print("END RdfClass.validate_primary_key -NO pKey----\n")
            return ["valid"]
        else:
            _calculated_props = self._get_calculated_properties()
            _old_class_data = self._select_class_query_data(old_data)
            _new_class_data = {}
            _query_args = [make_triple("?uri", "a", \
                    iri(uri(self.kds_classUri)))]
            _multi_key_query_args = [
                make_triple("?uri", "a", iri(uri(self.kds_classUri)))
            ]
            _key_changed = False
            _prop_uri_list = []
            _key_props = []
            # get primary key data from the form data
            for prop in rdf_obj:
                if prop.kds_propUri in pkey:
                    _new_class_data[prop.kds_propUri] = prop.data
                    _prop_name_list.append(prop.kds_formLabelName)
                    _key_props.append(prop)

            for key in pkey:
                _object_val = None
                #get the _data_value to test against
                _data_value = _new_class_data.get(key,
                                                  _old_class_data.get(key))
                if is_not_null(_data_value):
                    _range_obj = make_list(self.kds_properties[key].get(\
                            "rdfs_range", [{}]))[0]
                    _data_type = _range_obj.get('storageType')
                    _range = _range_obj.get('rangeClass')
                    if debug: print("_data_type: ", _data_type)
                    if _data_type == 'literal':
                        _object_val = RdfDataType(_range).sparql(_data_value)
                    else:
                        _object_val = iri(uri(_data_value))
                else:
                    # if data is missing from the key fields exit method and
                    # return valid. *** The object value does not exist and
                    #                   will be generated when another class
                    #                   is saved
                    if debug:                        print(\
                  "END RdfClass.validate_primary_key - NO data-------\n")
                    return ["valid"]
                # if the old_data is not equel to the newData re-evaluate
                # the primaryKey

                # if the old value is not equal to the new value need to test
                # the key
                # if the new value is to be calculated, i.e. a dependant class
                # generating a value then we don't need to test the key.
                # however if there is a supplied value and it is listed as a
                # calculated property we need to test.
                if (_old_class_data.get(key) != _new_class_data.get(key)) and \
                        ((key not in _calculated_props) or \
                        _new_class_data.get(key) is not None):
                    _key_changed = True
                    if _object_val:
                        _query_args.append(make_triple("?uri", iri(uri(key)), \
                                _object_val))
                        _multi_key_query_args.append(make_triple("?uri", \
                                iri(uri(key)), _object_val))
                else:
                    if _object_val:
                        _multi_key_query_args.append(make_triple("?uri", \
                                iri(uri(key)), _object_val))
                    else:
                        _key_changed = False
            # if the primary key changed in the form we need to
            # query to see if there is a violation with the new value

            if _key_changed:
                if len(pkey) > 1:
                    args = _multi_key_query_args
                else:
                    args = _query_args
                sparql = '''
                         {}\nSELECT DISTINCT (COUNT(?uri)>0 AS ?keyViolation)
                         {{\n{}\n}}\nGROUP BY ?uri'''.format(\
                                rdfw().get_prefix(),
                                "\n".join(args))
                if debug: print("----------- PrimaryKey query:\n", sparql)
                _key_test_results =\
                        requests.post(\
                                self.triplestore_url,
                                data={"query": sparql, "format": "json"})
                if debug:
                    print("_key_test_results: ", _key_test_results.json())
                _key_test = _key_test_results.json().get('results').get( \
                        'bindings', [])
                if debug: print(_key_test)
                if len(_key_test) > 0:
                    _key_test = _key_test[0].get('keyViolation', {}).get( \
                            'value', False)
                else:
                    _key_test = False

                if not _key_test:
                    if debug:                        print(\
                  "END RdfClass.validate_primary_key - Key Passed --\n")
                    return ["valid"]
                else:
                    error_msg = "This {} aleady exists.".format(
                        " / ".join(_prop_name_list))
                    for prop in _key_props:
                        if hasattr(prop, "errors"):
                            if isinstance(prop.errors, list):
                                prop.errors.append(error_msg)
                            else:
                                prop.errors = [error_msg]
                        else:
                            setattr(prop, "errors", [error_msg])
                    return [{
                        "errorType": "primaryKeyViolation",
                        "formErrorMessage": error_msg,
                        "errorData": {
                            "class": self.kds_classUri,
                            "propUri": pkey
                        }
                    }]
            if debug:                print(\
          "START RdfClass.validate_primary_key - Skipped Everything--\n")
            return ["valid"]