1019 # ensure eid is correctly typed in args |
1019 # ensure eid is correctly typed in args |
1020 args[key] = int(args[key]) |
1020 args[key] = int(args[key]) |
1021 return tuple(cachekey) |
1021 return tuple(cachekey) |
1022 |
1022 |
1023 def extid2eid(self, source, extid, etype, session, insert=True, |
1023 def extid2eid(self, source, extid, etype, session, insert=True, |
1024 commit=True, sourceparams=None): |
1024 sourceparams=None): |
1025 """Return eid from a local id. If the eid is a negative integer, that |
1025 """Return eid from a local id. If the eid is a negative integer, that |
1026 means the entity is known but has been copied back to the system source |
1026 means the entity is known but has been copied back to the system source |
1027 hence should be ignored. |
1027 hence should be ignored. |
1028 |
1028 |
1029 If no record is found, ie the entity is not known yet: |
1029 If no record is found, ie the entity is not known yet: |
1046 """ |
1046 """ |
1047 try: |
1047 try: |
1048 return self._extid_cache[extid] |
1048 return self._extid_cache[extid] |
1049 except KeyError: |
1049 except KeyError: |
1050 pass |
1050 pass |
1051 free_cnxset = False |
|
1052 eid = self.system_source.extid2eid(session, extid) |
1051 eid = self.system_source.extid2eid(session, extid) |
1053 if eid is not None: |
1052 if eid is not None: |
1054 self._extid_cache[extid] = eid |
1053 self._extid_cache[extid] = eid |
1055 self._type_source_cache[eid] = (etype, extid, source.uri) |
1054 self._type_source_cache[eid] = (etype, extid, source.uri) |
1056 return eid |
1055 return eid |
1057 if not insert: |
1056 if not insert: |
1058 return |
1057 return |
1059 # no link between extid and eid, create one using an internal session |
1058 # no link between extid and eid, create one using an internal session |
1060 # since the current session user may not have required permissions to |
1059 # since the current session user may not have required permissions to |
1061 # do necessary stuff and we don't want to commit user session. |
1060 # do necessary stuff and we don't want to commit user session. |
1062 # |
|
1063 # Moreover, even if session is already an internal session but is |
|
1064 # processing a commit, we have to use another one |
|
1065 if not session.is_internal_session: |
|
1066 session = self.internal_session() |
|
1067 free_cnxset = True |
|
1068 try: |
1061 try: |
1069 eid = self.system_source.create_eid(session) |
1062 eid = self.system_source.create_eid(session) |
1070 self._extid_cache[extid] = eid |
1063 self._extid_cache[extid] = eid |
1071 self._type_source_cache[eid] = (etype, extid, source.uri) |
1064 self._type_source_cache[eid] = (etype, extid, source.uri) |
1072 entity = source.before_entity_insertion( |
1065 entity = source.before_entity_insertion( |
1078 self.hm.call_hooks('before_add_entity', session, entity=entity) |
1071 self.hm.call_hooks('before_add_entity', session, entity=entity) |
1079 self.add_info(session, entity, source, extid) |
1072 self.add_info(session, entity, source, extid) |
1080 source.after_entity_insertion(session, extid, entity, sourceparams) |
1073 source.after_entity_insertion(session, extid, entity, sourceparams) |
1081 if source.should_call_hooks: |
1074 if source.should_call_hooks: |
1082 self.hm.call_hooks('after_add_entity', session, entity=entity) |
1075 self.hm.call_hooks('after_add_entity', session, entity=entity) |
1083 if commit or free_cnxset: |
|
1084 session.commit(free_cnxset) |
|
1085 return eid |
1076 return eid |
1086 except Exception: |
1077 except Exception: |
1087 if commit or free_cnxset: |
1078 # XXX do some cleanup manually so that the transaction has a |
1088 session.rollback(free_cnxset) |
1079 # chance to be commited, with simply this entity discarded |
1089 else: |
1080 self._extid_cache.pop(extid, None) |
1090 # XXX do some cleanup manually so that the transaction has a |
1081 self._type_source_cache.pop(eid, None) |
1091 # chance to be commited, with simply this entity discarded |
1082 if 'entity' in locals(): |
1092 self._extid_cache.pop(extid, None) |
1083 hook.CleanupDeletedEidsCacheOp.get_instance(session).add_data(entity.eid) |
1093 self._type_source_cache.pop(eid, None) |
1084 self.system_source.delete_info_multi(session, [entity]) |
1094 if 'entity' in locals(): |
1085 if source.should_call_hooks: |
1095 hook.CleanupDeletedEidsCacheOp.get_instance(session).add_data(entity.eid) |
1086 session._cnx.pending_operations = pending_operations |
1096 self.system_source.delete_info_multi(session, [entity]) |
|
1097 if source.should_call_hooks: |
|
1098 session._cnx.pending_operations = pending_operations |
|
1099 raise |
1087 raise |
1100 |
1088 |
1101 def add_info(self, session, entity, source, extid=None): |
1089 def add_info(self, session, entity, source, extid=None): |
1102 """add type and source info for an eid into the system table, |
1090 """add type and source info for an eid into the system table, |
1103 and index the entity with the full text index |
1091 and index the entity with the full text index |