def create_uniqueids( self, entitytype, is_new_resource=False ): # Method that creates UniqueIDs and its correlated Entity when a new resource is saved, or else only a UniqueId when the entity is already present (this will happen if the entities are created via importer.py uniqueid_node = settings.RESOURCE_TYPE_CONFIGS()[ self.get_root().entitytypeid]['primary_name_lookup']['entity_type'] if entitytype in settings.EAMENA_RESOURCES: type = settings.EAMENA_RESOURCES[entitytype] else: type = re.split("\W+|_", entitytype)[0] if is_new_resource: entity2 = archesmodels.Entities() entity2.entitytypeid = archesmodels.EntityTypes.objects.get( pk=uniqueid_node) entity2.entityid = str(uuid.uuid4()) entity2.save() rule = archesmodels.Rules.objects.get( entitytypedomain=self.entitytypeid, entitytyperange=entity2.entitytypeid) archesmodels.Relations.objects.create( entityiddomain=archesmodels.Entities.objects.get( pk=self.entityid), entityidrange=entity2, ruleid=rule) uniqueidmodel = self._get_model('uniqueids') uniqueidmodelinstance = uniqueidmodel() uniqueidmodelinstance.entityid = entity2 if is_new_resource else archesmodels.Entities.objects.get( pk=self.entityid) uniqueidmodelinstance.id_type = type if self.value: # Setting ID to value already defined num = int(self.value[-settings.ID_LENGTH:]) uniqueidmodelinstance.val = str(num) else: try: last_uniqueid = archesmodels.UniqueIds.objects.filter( id_type=type).extra({ 'valint': "CAST(val as INT)" }).latest('valint') IdInt = int(last_uniqueid.val) + 1 uniqueidmodelinstance.val = str(IdInt) except ObjectDoesNotExist: uniqueidmodelinstance.val = str(1) uniqueidmodelinstance.order_date = datetime.datetime.now() uniqueidmodelinstance.save() if is_new_resource: return entity2.entityid
def _save(self): """ Saves an entity back to the db, returns a DB model instance, not an instance of self """ type = '' is_new_entity = False is_new_resource = False entitytype = archesmodels.EntityTypes.objects.get(pk = self.entitytypeid) try: uuid.UUID(self.entityid) except(ValueError): is_new_entity = True if entitytype.isresource: is_new_resource = True self.entityid = str(uuid.uuid4()) entity = archesmodels.Entities() entity.entitytypeid = entitytype entity.entityid = self.entityid entity.save() if is_new_resource: contains_eamena_id = False for child in self.child_entities: if child.entitytypeid == 'EAMENA_ID.E42': contains_eamena_id = True if not contains_eamena_id: newid = self.create_uniqueids(str(entitytype), is_new_resource) self.append_child(Entity().get(newid, archesmodels.Entities.objects.get(pk = entity.entityid))) else: if entity.entitytypeid.businesstablename == 'uniqueids': try: archesmodels.UniqueIds.objects.get(pk=self.entityid) except ObjectDoesNotExist: self.create_uniqueids(self.get_root().entitytypeid, is_new_resource=False) is_new_entity = False columnname = entity.entitytypeid.getcolumnname() if columnname != None: themodel = self._get_model(entity.entitytypeid.businesstablename) themodelinstance = themodel() themodelinstance.entityid = entity self.businesstablename = entity.entitytypeid.businesstablename # if we need to populate the E32 nodes then this is the code to do it, # but it really slows down the save so i'm leaving it commented for now # if (isinstance(themodelinstance, archesmodels.Domains)): # setattr(themodelinstance, columnname, self.value) # themodelinstance.save() # concept = Concept(themodelinstance.val.conceptid).get_context() # if concept: # if len(self.child_entities) == 0: # rule = archesmodels.Rules.objects.filter(entitytypedomain_id=self.entitytypeid) # if len(rule) == 1: # self.add_child_entity(rule[0].entitytyperange_id, rule[0].propertyid_id, concept.id, '') # elif len(self.child_entities) == 1: # self.child_entities[0].value = concept.id if not (isinstance(themodelinstance, archesmodels.Files)) and not (isinstance(themodelinstance, archesmodels.UniqueIds)): # Validating dates if isinstance(themodelinstance, archesmodels.Dates) and is_new_entity ==True: # don't attempt to parse string if this is already a datetime if __builtin__.type(self.value) != datetime.datetime: try: datetime.datetime.strptime(self.value, '%Y-%m-%d') except ValueError: try: d = datetime.datetime.strptime(self.value,'%d-%m-%Y') self.value = d.strftime('%Y-%m-%d') except ValueError: raise ValueError("The value inserted is not a date") setattr(themodelinstance, columnname, self.value) themodelinstance.save() self.label = self.value if (isinstance(themodelinstance, archesmodels.Domains)): self.value = themodelinstance.getlabelid() self.label = themodelinstance.getlabelvalue() else: # Saving of files must be handled specially # Because on subsequent saves of a file resource, we post back the file path url (instead of posting the file like we originally did), # we want to prevent the path from being saved back to the database thus screwing up the file save process # This block should only be entered when initally uploading a file via the application, or when inserting records via a .arches file if isinstance(self.value, (InMemoryUploadedFile, TemporaryUploadedFile)) or is_new_entity: setattr(themodelinstance, columnname, self.value) themodelinstance.save() self.value = themodelinstance.geturl() self.label = themodelinstance.getname() # this is some extra rough logic that was implemented during the v1-v2 # migration effort. During json load, file data was just stored as paths # to the S3 bucket. To handle this, we download the file locally, open it, # set it as a Django File object and then set the entity's values to it and save. elif (isinstance(themodelinstance, archesmodels.Files)): tempfile_name = self.value.split("/")[-1] urllib.urlretrieve(self.value, tempfile_name) with open(tempfile_name, "r") as f: setattr(themodelinstance, columnname, File(f)) themodelinstance.save() self.value = themodelinstance.geturl() self.label = themodelinstance.getname() os.remove(tempfile_name) for child_entity in self.child_entities: child = child_entity._save() try: rule = archesmodels.Rules.objects.get(entitytypedomain = entity.entitytypeid, entitytyperange = child.entitytypeid, propertyid = child_entity.property) except archesmodels.Rules.DoesNotExist as e: print "entitytypedomain:", entity.entitytypeid print "entitytyperange:", child.entitytypeid # print "propertyid:", child_entity.property raise e archesmodels.Relations.objects.create(entityiddomain = entity, entityidrange = child, ruleid = rule) return entity
def _save(self): """ Saves an entity back to the db, returns a DB model instance, not an instance of self """ is_new_entity = False entitytype = archesmodels.EntityTypes.objects.get(pk=self.entitytypeid) try: uuid.UUID(self.entityid) except (ValueError): is_new_entity = True self.entityid = str(uuid.uuid4()) entity = archesmodels.Entities() entity.entitytypeid = entitytype entity.entityid = self.entityid entity.save() columnname = entity.entitytypeid.getcolumnname() if columnname != None: themodel = self._get_model(entity.entitytypeid.businesstablename) themodelinstance = themodel() themodelinstance.entityid = entity self.businesstablename = entity.entitytypeid.businesstablename # if we need to populate the E32 nodes then this is the code to do it, # but it really slows down the save so i'm leaving it commented for now # if (isinstance(themodelinstance, archesmodels.Domains)): # setattr(themodelinstance, columnname, self.value) # themodelinstance.save() # concept = Concept(themodelinstance.val.conceptid).get_context() # if concept: # if len(self.child_entities) == 0: # rule = archesmodels.Rules.objects.filter(entitytypedomain_id=self.entitytypeid) # if len(rule) == 1: # self.add_child_entity(rule[0].entitytyperange_id, rule[0].propertyid_id, concept.id, '') # elif len(self.child_entities) == 1: # self.child_entities[0].value = concept.id if not (isinstance(themodelinstance, archesmodels.Files)): setattr(themodelinstance, columnname, self.value) themodelinstance.save() self.label = self.value if (isinstance(themodelinstance, archesmodels.Domains)): self.value = themodelinstance.getlabelid() self.label = themodelinstance.getlabelvalue() else: # Saving of files must be handled specially # Because on subsequent saves of a file resource, we post back the file path url (instead of posting the file like we originally did), # we want to prevent the path from being saved back to the database thus screwing up the file save process # This block should only be entered when initally uploading a file via the application, or when inserting records via a .arches file if isinstance(self.value, (InMemoryUploadedFile, TemporaryUploadedFile)) or is_new_entity: setattr(themodelinstance, columnname, self.value) themodelinstance.save() self.value = themodelinstance.geturl() self.label = themodelinstance.getname() for child_entity in self.child_entities: child = child_entity._save() rule = archesmodels.Rules.objects.get( entitytypedomain=entity.entitytypeid, entitytyperange=child.entitytypeid, propertyid=child_entity.property) archesmodels.Relation.objects.get_or_create(entityiddomain=entity, entityidrange=child, ruleid=rule) return entity
def createBacklogIds(): entitytype = archesmodels.EntityTypes.objects.get(pk="ACTOR.E39") type = 'ACTOR' all_entities = archesmodels.Entities.objects.filter( entitytypeid__exact=entitytype) entities = [] errors = [] for count, entity in enumerate(all_entities, 1): if count % 5000 == 0: print "%s resources inspected" % count try: relation = archesmodels.Relations.objects.get( ruleid=archesmodels.Rules.objects.get( entitytypedomain=entitytype, entitytyperange="EAMENA_ID.E42").ruleid, entityiddomain=entity.entityid) except ObjectDoesNotExist: entities.append(entity) print "There are %s resources and %s which do not have a EAMENA_ID.E42" % ( all_entities.count(), len(entities)) for count, entity in enumerate(entities, 1): if count % 1000 == 0: print "%s UniqueIds created" % count entity2 = archesmodels.Entities() entity2.entitytypeid = archesmodels.EntityTypes.objects.get( pk="EAMENA_ID.E42") entity2.entityid = str(uuid.uuid4()) entity2.save() rule = archesmodels.Rules.objects.get( entitytypedomain=entity.entitytypeid, entitytyperange=entity2.entitytypeid, propertyid='P1') archesmodels.Relations.objects.get_or_create(entityiddomain=entity, entityidrange=entity2, ruleid=rule) uniqueidmodel = Entity._get_model('uniqueids') uniqueidmodelinstance = uniqueidmodel() uniqueidmodelinstance.entityid = entity2 uniqueidmodelinstance.id_type = type try: lastID = uniqueidmodel.objects.filter(id_type__exact=type).latest() IdInt = int(lastID.val) + 1 uniqueidmodelinstance.val = str(IdInt) except ObjectDoesNotExist: print "The resource %s has been assigned the first ID with entityid %s" % ( entity.entityid, entity2.entityid) uniqueidmodelinstance.val = str(1) uniqueidmodelinstance.order_date = datetime.datetime.now() uniqueidmodelinstance.save() zerosLength = settings.ID_LENGTH if settings.ID_LENGTH > len( uniqueidmodelinstance.val) else len(uniqueidmodelinstance.val) value = type + "-" + uniqueidmodelinstance.val.zfill(zerosLength) # ReindexResource(entity.entityid, entity2.entityid, value) try: resource = Resource().get(entity.entityid) resource.index() except Exception as e: if e not in errors: errors.append(e) if len(errors) > 0: print errors[0], ':', len(errors)