+
+ ####################
+ # utility for handling relationships among the SFA objects
+
+ # subject_record describes the subject of the relationships
+ # ref_record contains the target values for the various relationships
+ # we need to manage (to begin with, this is just the
+ # slice x person (researcher) and authority x person (pi) relationships)
+ def update_driver_relations(self, api, subject_obj, ref_obj):
+ type = subject_obj.type
+ # for (k,v) in subject_obj.__dict__.items(): print k,'=',v
+ if type == 'slice' and hasattr(ref_obj, 'researcher'):
+ self.update_driver_relation(
+ api, subject_obj, ref_obj.researcher, 'user', 'researcher')
+ elif type == 'authority' and hasattr(ref_obj, 'pi'):
+ self.update_driver_relation(
+ api, subject_obj, ref_obj.pi, 'user', 'pi')
+
+ # field_key is the name of one field in the record,
+ # typically 'researcher' for a 'slice' record
+ # hrns is the list of hrns that should be linked to the subject from now on
+ # target_type would be e.g. 'user' in the 'slice' x 'researcher' example
+ def update_driver_relation(self, api, record_obj, hrns,
+ target_type, relation_name):
+ dbsession = api.dbsession()
+ # locate the linked objects in our db
+ subject_type = record_obj.type
+ subject_id = record_obj.pointer
+ # get the 'pointer' field of all matching records
+ link_id_tuples = dbsession.query(RegRecord.pointer).filter_by(
+ type=target_type).filter(RegRecord.hrn.in_(hrns)).all()
+ # sqlalchemy returns named tuples for columns
+ link_ids = [tuple.pointer for tuple in link_id_tuples]
+ api.driver.update_relation(
+ subject_type, target_type, relation_name, subject_id, link_ids)
+
+ def Register(self, api, record_dict):
+
+ logger.debug("Register: entering with record_dict={}".format(
+ printable(record_dict)))
+ normalize_input_record(record_dict)
+ logger.debug("Register: normalized record_dict={}".format(
+ printable(record_dict)))
+
+ dbsession = api.dbsession()
+ hrn, type = record_dict['hrn'], record_dict['type']
+ urn = hrn_to_urn(hrn, type)