def convert_string_to_type_asserions(self, predicate_uuid):
     """ Converts xsd:string objects to type objects in the Assertions table
     """
     output = False
     try:  # look for the predicate item
         pred = Predicate.objects.get(uuid=predicate_uuid)
         pdata_type = pred.data_type
     except Predicate.DoesNotExist:
         pdata_type = False
     if pdata_type == 'id':
         # yes, the predicate has data_type = 'id', so it can take types
         bad_assertions = Assertion.objects\
                                   .filter(predicate_uuid=predicate_uuid,
                                           object_type='xsd:string')
         output = len(bad_assertions)
         for bad_ass in bad_assertions:
             new_ass = bad_ass
             tm = TypeManagement()
             tm.get_make_type_pred_uuid_content_uuid(predicate_uuid,
                                                     bad_ass.object_uuid)
             new_ass.object_uuid = tm.oc_type.uuid
             new_ass.object_type = 'types'
             Assertion.objects\
                      .filter(hash_id=bad_ass.hash_id).delete()
             new_ass.save()
     return output
Exemple #2
0
 def make_type_event_from_type_label_records(self, type_pred_uuid,
                                             type_field_num,
                                             start_field_num,
                                             stop_field_num):
     """ make event records from types identified by
         the predicate uuid for a type and its field number
     """
     type_list = ImportCell.objects\
                           .filter(source_id=self.source_id,
                                   field_num=type_field_num)
     if len(type_list) > 0:
         for type_row in type_list:
             row = type_row.row_num
             type_label = type_row.record
             start_date = self.get_date_record(start_field_num, row)
             stop_date = self.get_date_record(stop_field_num, row)
             if start_date is not False\
                and stop_date is not False:
                 tmo = TypeManagement()
                 tmo.project_uuid = self.project_uuid
                 tmo.source_id = self.source_id
                 type_obj = tmo.get_make_type_within_pred_uuid(
                     type_pred_uuid, type_label)
                 tet = TimeEventType()
                 tet.uuid = type_obj.uuid
                 tet.start_date = start_date
                 tet.stop_date = stop_date
                 tet.source_id = self.source_id
                 tet.project_uuid = self.project_uuid
                 tet.create_type_event()
Exemple #3
0
 def make_type_event_from_type_label_records(self,
                                             type_pred_uuid,
                                             type_field_num,
                                             start_field_num,
                                             stop_field_num):
     """ make event records from types identified by
         the predicate uuid for a type and its field number
     """
     type_list = ImportCell.objects\
                           .filter(source_id=self.source_id,
                                   field_num=type_field_num)
     if len(type_list) > 0:
         for type_row in type_list:
             row = type_row.row_num
             type_label = type_row.record
             start_date = self.get_date_record(start_field_num,
                                               row)
             stop_date = self.get_date_record(stop_field_num,
                                              row)
             if start_date is not False\
                and stop_date is not False:
                 tmo = TypeManagement()
                 tmo.project_uuid = self.project_uuid
                 tmo.source_id = self.source_id
                 type_obj = tmo.get_make_type_within_pred_uuid(type_pred_uuid,
                                                               type_label)
                 tet = TimeEventType()
                 tet.uuid = type_obj.uuid
                 tet.start_date = start_date
                 tet.stop_date = stop_date
                 tet.source_id = self.source_id
                 tet.project_uuid = self.project_uuid
                 tet.create_type_event()
 def convert_string_to_type_asserions(self, predicate_uuid):
     """ Converts xsd:string objects to type objects in the Assertions table
     """
     output = False
     try:  # look for the predicate item
         pred = Predicate.objects.get(uuid=predicate_uuid)
         pdata_type = pred.data_type
     except Predicate.DoesNotExist:
         pdata_type = False
     if pdata_type == 'id':
         # yes, the predicate has data_type = 'id', so it can take types
         bad_assertions = Assertion.objects\
                                   .filter(predicate_uuid=predicate_uuid,
                                           object_type='xsd:string')
         output = len(bad_assertions)
         for bad_ass in bad_assertions:
             new_ass = bad_ass
             tm = TypeManagement()
             tm.get_make_type_pred_uuid_content_uuid(
                 predicate_uuid, bad_ass.object_uuid)
             new_ass.object_uuid = tm.oc_type.uuid
             new_ass.object_type = 'types'
             Assertion.objects\
                      .filter(hash_id=bad_ass.hash_id).delete()
             new_ass.save()
     return output
Exemple #5
0
 def make_type_ld_annotations(self,
                              sub_type_pred_uuid,
                              sub_type_f_num,
                              rel_pred,
                              obj_le_f_num):
     """ Makes linked data annotations
         for a type in an import
     """
     rels = []
     sub_type_list = ImportCell.objects\
                               .filter(source_id=self.source_id,
                                       field_num=sub_type_f_num)
     if len(sub_type_list) > 0:
         distinct_records = {}
         for cell in sub_type_list:
             if cell.rec_hash not in distinct_records:
                 distinct_records[cell.rec_hash] = {}
                 distinct_records[cell.rec_hash]['rows'] = []
                 distinct_records[cell.rec_hash]['imp_cell_obj'] = cell
             distinct_records[cell.rec_hash]['rows'].append(cell.row_num)
         for rec_hash_key, distinct_type in distinct_records.items():
             # iterate through the distinct types and get associated linked data
             type_label = distinct_type['imp_cell_obj'].record
             rows = distinct_type['rows']
             if len(type_label) > 0:
                 # the type isn't blank, so we can use it
                 pc = ProcessCells(self.source_id, 0)
                 ld_entities = pc.get_field_records(obj_le_f_num, rows)
                 for ld_hash_key, distinct_ld in ld_entities.items():
                     obj_uri = distinct_ld['imp_cell_obj'].record
                     if len(obj_uri) > 8:
                         if obj_uri[:7] == 'http://'\
                            or obj_uri[:8] == 'https://':
                             # we have a valid linked data entity
                             #
                             # now get the UUID for the type
                             tm = TypeManagement()
                             tm.project_uuid = self.project_uuid
                             tm.source_id = self.source_id
                             sub_type = tm.get_make_type_within_pred_uuid(sub_type_pred_uuid,
                                                                          type_label)
                             rel = {'subject_label': type_label,
                                    'subject': sub_type.uuid,
                                    'object_uri': obj_uri}
                             rels.append(rel)
     if len(rels) > 0:
         for rel in rels:
             new_la = LinkAnnotation()
             new_la.subject = rel['subject']
             new_la.subject_type = 'types'
             new_la.project_uuid = self.project_uuid
             new_la.source_id = self.source_id
             new_la.predicate_uri = rel_pred
             new_la.object_uri = rel['object_uri']
             new_la.creator_uuid = ''
             new_la.save()
             web_le = WebLinkEntity()
             web_le.check_add_link_entity(rel['object_uri'])
Exemple #6
0
 def make_type_ld_annotations(self,
                              sub_type_pred_uuid,
                              sub_type_f_num,
                              rel_pred,
                              obj_le_f_num):
     """ Makes linked data annotations
         for a type in an import
     """
     rels = []
     sub_type_list = ImportCell.objects\
                               .filter(source_id=self.source_id,
                                       field_num=sub_type_f_num)
     if len(sub_type_list) > 0:
         distinct_records = {}
         for cell in sub_type_list:
             if cell.rec_hash not in distinct_records:
                 distinct_records[cell.rec_hash] = {}
                 distinct_records[cell.rec_hash]['rows'] = []
                 distinct_records[cell.rec_hash]['imp_cell_obj'] = cell
             distinct_records[cell.rec_hash]['rows'].append(cell.row_num)
         for rec_hash_key, distinct_type in distinct_records.items():
             # iterate through the distinct types and get associated linked data
             type_label = distinct_type['imp_cell_obj'].record
             rows = distinct_type['rows']
             if len(type_label) > 0:
                 # the type isn't blank, so we can use it
                 pc = ProcessCells(self.source_id, 0)
                 ld_entities = pc.get_field_records(obj_le_f_num, rows)
                 for ld_hash_key, distinct_ld in ld_entities.items():
                     obj_uri = distinct_ld['imp_cell_obj'].record
                     if len(obj_uri) > 8:
                         if obj_uri[:7] == 'http://'\
                            or obj_uri[:8] == 'https://':
                             # we have a valid linked data entity
                             #
                             # now get the UUID for the type
                             tm = TypeManagement()
                             tm.project_uuid = self.project_uuid
                             tm.source_id = self.source_id
                             sub_type = tm.get_make_type_within_pred_uuid(sub_type_pred_uuid,
                                                                          type_label)
                             rel = {'subject_label': type_label,
                                    'subject': sub_type.uuid,
                                    'object_uri': obj_uri}
                             rels.append(rel)
     if len(rels) > 0:
         for rel in rels:
             new_la = LinkAnnotation()
             new_la.subject = rel['subject']
             new_la.subject_type = 'types'
             new_la.project_uuid = self.project_uuid
             new_la.source_id = self.source_id
             new_la.predicate_uri = rel_pred
             new_la.object_uri = rel['object_uri']
             new_la.creator_uuid = ''
             new_la.save()
             web_le = WebLinkEntity()
             web_le.check_add_link_entity(rel['object_uri'])
Exemple #7
0
 def make_type_relations(self, sub_type_pred_uuid, sub_type_f_num, rel_pred,
                         obj_type_pred_uuid, obj_type_f_num):
     """ Makes semantic relationships between
         different types in an import
     """
     rels = {}
     sub_type_list = ImportCell.objects\
                               .filter(source_id=self.source_id,
                                       field_num=sub_type_f_num)
     for sub_type_obj in sub_type_list:
         sub_type_text = sub_type_obj.record
         row = sub_type_obj.row_num
         if len(sub_type_text) > 0:
             tm = TypeManagement()
             tm.project_uuid = self.project_uuid
             tm.source_id = self.source_id
             sub_type = tm.get_make_type_within_pred_uuid(
                 sub_type_pred_uuid, sub_type_text)
             obj_type_list = ImportCell.objects\
                                       .filter(source_id=self.source_id,
                                               field_num=obj_type_f_num,
                                               row_num=row)[:1]
             if len(obj_type_list) > 0:
                 obj_type_text = obj_type_list[0].record
                 if len(obj_type_text) > 0 \
                    and sub_type_text != obj_type_text:
                     tmo = TypeManagement()
                     tmo.project_uuid = self.project_uuid
                     tmo.source_id = self.source_id
                     obj_type = tmo.get_make_type_within_pred_uuid(
                         obj_type_pred_uuid, obj_type_text)
                     # make a uri for this, since we're making a link assertion
                     obj_uri = URImanagement.make_oc_uri(
                         obj_type.uuid, 'types')
                     # the following bit is so we don't make the
                     # same link assertions over and over.
                     rel_id = str(sub_type.uuid) + ' ' + str(obj_type.uuid)
                     if rel_id not in rels:
                         rels[rel_id] = {
                             'subject': sub_type.uuid,
                             'object_uri': obj_uri
                         }
     # now make the link data annotation relating these types.
     for rel_id, rel in rels.items():
         new_la = LinkAnnotation()
         new_la.subject = rel['subject']
         new_la.subject_type = 'types'
         new_la.project_uuid = self.project_uuid
         new_la.source_id = self.source_id
         new_la.predicate_uri = rel_pred
         new_la.object_uri = rel['object_uri']
         new_la.creator_uuid = ''
         new_la.save()
Exemple #8
0
 def db_save_reconcile_entity_predicates_types(self, act_dir):
     """ saves predicates and type items to the
         Open Context database, and / or reconciles these
         items with previously saved items from the same project
     """
     key = self.oc_config_entity_types
     json_obj = self.fm.get_dict_from_file(key, act_dir)
     if json_obj is None:
         print('Need to 1st generate an attributes file from the ArchEnts!')
         ok = False
     else:
         # we have JSON with dictionary for the entity_types
         self.entity_types = json_obj
         make_entity_types_assertions = False
         for faims_ent_type_id, ent_dict in json_obj.items():
             if isinstance(ent_dict['item_type'], str) \
                and ent_dict['add_type_as_attribute']:
                 # OK we have some items that need entity types made as
                 # a descriptive attribute
                 make_entity_types_assertions = True
                 break
         if make_entity_types_assertions:
             # we have entity_types that need to have a descriptive
             # predicate, so create a new predicate in Open Context
             # to describe entity_types for this project
             sup_dict = LastUpdatedOrderedDict()
             sup_dict[self.reconcile_key] = self.ent_type_pred_sup_id
             pm = PredicateManagement()
             pm.project_uuid = self.project_uuid
             pm.source_id = self.source_id
             pm.sup_dict = sup_dict
             pm.sup_reconcile_key = self.reconcile_key
             pm.sup_reconcile_value = self.ent_type_pred_sup_id
             pred_obj = pm.get_make_predicate(
                 self.FAIMS_ENTITY_TYPE_PREDICATE_LABEL, 'variable', 'id')
             if pred_obj is not False:
                 # we reconciled or created the predicate!
                 # now we mint oc_types for all the entity_types
                 predicate_uuid = str(pred_obj.uuid)
                 for faims_ent_type_id, ent_dict in json_obj.items():
                     if isinstance(ent_dict['item_type'], str) \
                        and ent_dict['add_type_as_attribute']:
                         # OK, we have an item entity type to be used as a description
                         sup_dict = LastUpdatedOrderedDict()
                         sup_dict[self.reconcile_key] = faims_ent_type_id
                         tm = TypeManagement()
                         tm.project_uuid = self.project_uuid
                         tm.source_id = self.source_id
                         tm.sup_dict = sup_dict
                         tm.sup_reconcile_key = self.reconcile_key
                         tm.sup_reconcile_value = faims_ent_type_id
                         type_obj = tm.get_make_type_within_pred_uuid(
                             predicate_uuid, ent_dict['label'])
                         if type_obj is not False:
                             # we have reconciled the type!
                             ent_dict['type_uuid'] = str(type_obj.uuid)
                             ent_dict['predicate_uuid'] = predicate_uuid
                             self.entity_types[faims_ent_type_id] = ent_dict
             # now save the results
             self.fm.save_serialized_json(key, act_dir, self.entity_types)
Exemple #9
0
 def create_concept_parents(self, new_hierachic_list):
     """ Creates new types for
     superior (more general) types from a list
     of types that have hiearchies implicit in their labels
     once the superior types are created,
     linked data annotations noting hierarchy are stored
     """
     parent_children_pairs = []
     for manifest in new_hierachic_list:
         try:
             oc_type = OCtype.objects.get(uuid=manifest.uuid)
         except OCtype.DoesNotExist:
             oc_type = False
         if (oc_type is not False):
             child_parts = manifest.label.split(self.HIERARCHY_DELIM)
             act_delim = ''
             act_new_label = ''
             current_parent = False
             for label_part in child_parts:
                 act_new_label = act_new_label + act_delim + label_part
                 act_delim = self.HIERARCHY_DELIM
                 type_manage = TypeManagement()
                 type_manage.project_uuid = oc_type.project_uuid
                 type_manage.source_id = self.source_id
                 ptype = type_manage.get_make_type_within_pred_uuid(
                     oc_type.predicate_uuid, act_new_label)
                 if (current_parent is not False):
                     parent_child = {
                         'parent': current_parent,
                         'child': ptype.uuid
                     }
                     parent_children_pairs.append(parent_child)
                 current_parent = ptype.uuid
             if (len(parent_children_pairs) > 0):
                 # now make some linked data annotations
                 for parent_child in parent_children_pairs:
                     if (parent_child['parent'] is not False):
                         new_la = LinkAnnotation()
                         new_la.subject = parent_child['child']
                         new_la.subject_type = 'types'
                         new_la.project_uuid = oc_type.project_uuid
                         new_la.source_id = self.source_id
                         new_la.predicate_uri = self.p_for_superobjs
                         new_la.object_uri = URImanagement.make_oc_uri(
                             parent_child['parent'], 'types')
                         new_la.creator_uuid = ''
                         new_la.save()
     return parent_children_pairs
Exemple #10
0
 def create_concept_parents(self, new_hierachic_list):
     """ Creates new types for
     superior (more general) types from a list
     of types that have hiearchies implicit in their labels
     once the superior types are created,
     linked data annotations noting hierarchy are stored
     """
     parent_children_pairs = []
     for manifest in new_hierachic_list:
         try:
             oc_type = OCtype.objects.get(uuid=manifest.uuid)
         except OCtype.DoesNotExist:
             oc_type = False
         if(oc_type is not False):
             child_parts = manifest.label.split(self.HIERARCHY_DELIM)
             act_delim = ''
             act_new_label = ''
             current_parent = False
             for label_part in child_parts:
                 act_new_label = act_new_label + act_delim + label_part
                 act_delim = self.HIERARCHY_DELIM
                 type_manage = TypeManagement()
                 type_manage.project_uuid = oc_type.project_uuid
                 type_manage.source_id = self.source_id
                 ptype = type_manage.get_make_type_within_pred_uuid(oc_type.predicate_uuid,
                                                                    act_new_label)
                 if(current_parent is not False):
                     parent_child = {'parent': current_parent,
                                     'child': ptype.uuid}
                     parent_children_pairs.append(parent_child)
                 current_parent = ptype.uuid
             if(len(parent_children_pairs) > 0):
                 # now make some linked data annotations
                 for parent_child in parent_children_pairs:
                     if(parent_child['parent'] is not False):
                         new_la = LinkAnnotation()
                         new_la.subject = parent_child['child']
                         new_la.subject_type = 'types'
                         new_la.project_uuid = oc_type.project_uuid
                         new_la.source_id = self.source_id
                         new_la.predicate_uri = self.p_for_superobjs
                         new_la.object_uri = URImanagement.make_oc_uri(parent_child['parent'], 'types')
                         new_la.creator_uuid = ''
                         new_la.save()
     return parent_children_pairs
Exemple #11
0
 def db_save_reconcile_predicates_types(self, act_dir):
     """ saves predicates and type items to the
         Open Context database, and / or reconciles these
         items with previously saved items from the same project
     """
     key = self.oc_config_attributes
     json_obj = self.fm.get_dict_from_file(key, act_dir)
     if json_obj is None:
         print('Need to 1st generate an attributes file from the ArchEnts!')
         ok = False
     else:
         # we have JSON with dictionary for the attributes
         ok = True
         self.attributes = json_obj
         for faims_id_pred, attrib_dict in json_obj.items():
             # default to always making a predicate and a type for attributes
             sup_dict = LastUpdatedOrderedDict()
             sup_dict[self.reconcile_key] = faims_id_pred
             pm = PredicateManagement()
             pm.project_uuid = self.project_uuid
             pm.source_id = self.source_id
             pm.sup_dict = sup_dict
             pm.sup_reconcile_key = self.reconcile_key
             pm.sup_reconcile_value = faims_id_pred
             pred_obj = pm.get_make_predicate(attrib_dict['label'],
                                              attrib_dict['predicate_type'],
                                              attrib_dict['data_type'])
             if pred_obj is not False:
                 # we reconciled the predicate!
                 self.attributes[faims_id_pred]['predicate_uuid'] = str(
                     pred_obj.uuid)
                 if 'objects' in attrib_dict:
                     for faims_id_type, type_dict in attrib_dict[
                             'objects'].items():
                         sup_dict = LastUpdatedOrderedDict()
                         sup_dict[self.reconcile_key] = faims_id_type
                         tm = TypeManagement()
                         tm.project_uuid = self.project_uuid
                         tm.source_id = self.source_id
                         tm.sup_dict = sup_dict
                         tm.sup_reconcile_key = self.reconcile_key
                         tm.sup_reconcile_value = faims_id_type
                         type_obj = tm.get_make_type_within_pred_uuid(
                             pred_obj.uuid, type_dict['label'])
                         if type_obj is not False:
                             # we have reconciled the type!
                             type_dict['type_uuid'] = str(type_obj.uuid)
                             type_dict['predicate_uuid'] = str(
                                 pred_obj.uuid)
                             self.attributes[faims_id_pred]['objects'][
                                 faims_id_type] = type_dict
         # now save the results
         self.fm.save_serialized_json(key, act_dir, self.attributes)
Exemple #12
0
 def make_type_relations(self, sub_type_pred_uuid,
                               sub_type_f_num,
                               rel_pred,
                               obj_type_pred_uuid,
                               obj_type_f_num):
     """ Makes semantic relationships between
         different types in an import
     """
     rels = {}
     sub_type_list = ImportCell.objects\
                               .filter(source_id=self.source_id,
                                       field_num=sub_type_f_num)
     for sub_type_obj in sub_type_list:
         sub_type_text = sub_type_obj.record
         row = sub_type_obj.row_num
         if len(sub_type_text) > 0:
             tm = TypeManagement()
             tm.project_uuid = self.project_uuid
             tm.source_id = self.source_id
             sub_type = tm.get_make_type_within_pred_uuid(sub_type_pred_uuid,
                                                          sub_type_text)
             obj_type_list = ImportCell.objects\
                                       .filter(source_id=self.source_id,
                                               field_num=obj_type_f_num,
                                               row_num=row)[:1]
             if len(obj_type_list) > 0:
                 obj_type_text = obj_type_list[0].record
                 if len(obj_type_text) > 0 \
                    and sub_type_text != obj_type_text:
                     tmo = TypeManagement()
                     tmo.project_uuid = self.project_uuid
                     tmo.source_id = self.source_id
                     obj_type = tmo.get_make_type_within_pred_uuid(obj_type_pred_uuid,
                                                                   obj_type_text)
                     # make a uri for this, since we're making a link assertion
                     obj_uri = URImanagement.make_oc_uri(obj_type.uuid, 'types')
                     # the following bit is so we don't make the
                     # same link assertions over and over.
                     rel_id = str(sub_type.uuid) + ' ' + str(obj_type.uuid)
                     if rel_id not in rels:
                         rels[rel_id] = {'subject': sub_type.uuid,
                                         'object_uri': obj_uri}
     # now make the link data annotation relating these types.
     for rel_id, rel in rels.items():
         new_la = LinkAnnotation()
         new_la.subject = rel['subject']
         new_la.subject_type = 'types'
         new_la.project_uuid = self.project_uuid
         new_la.source_id = self.source_id
         new_la.predicate_uri = rel_pred
         new_la.object_uri = rel['object_uri']
         new_la.creator_uuid = ''
         new_la.save()
Exemple #13
0
 def add_type(self, predicate_uuid, type_uuid, string_uuid, label):
     """ adds the predicate """
     tm = TypeManagement()
     tm.source_id = self.source_id
     tm.project_uuid = self.project_uuid
     tm.suggested_uuid = type_uuid
     tm.suggested_content_uuid = string_uuid
     type_obj = tm.get_make_type_within_pred_uuid(predicate_uuid, label)
     return type_obj
Exemple #14
0
 def add_type(self, predicate_uuid, type_uuid, string_uuid, label):
     """ adds the predicate """
     tm = TypeManagement()
     tm.source_id = self.source_id
     tm.project_uuid = self.project_uuid
     tm.suggested_uuid = type_uuid
     tm.suggested_content_uuid = string_uuid
     type_obj = tm.get_make_type_within_pred_uuid(predicate_uuid, label)
     return type_obj
 def reconcile_type_cell(self,
                         predicate_uuid,
                         content_uuid,
                         imp_cell,
                         row_num=False):
     """ reconciles a distinct type,
         with consideration for how the 
     """
     output = False
     if len(imp_cell.record) > 0 \
        and predicate_uuid is not False \
        and content_uuid is not False:
         output = True
         tm = TypeManagement()
         tm.project_uuid = imp_cell.project_uuid
         tm.source_id = imp_cell.source_id
         self.oc_type = tm.get_make_type_pred_uuid_content_uuid(predicate_uuid,
                                                                content_uuid)
         # self.source_id = self.oc_type.source_id
         if self.oc_type.uuid != imp_cell.fl_uuid \
            or self.oc_type.content_uuid != imp_cell.l_uuid:    # update the reconcilted UUID for import cells with same rec_hash
             if row_num is False:
                 up_cells = ImportCell.objects\
                                      .filter(source_id=self.source_id,
                                              field_num=imp_cell.field_num,
                                              rec_hash=imp_cell.rec_hash)
             else:
                 up_cells = ImportCell.objects\
                                      .filter(source_id=self.source_id,
                                              field_num=imp_cell.field_num,
                                              row_num=row_num)
             for up_cell in up_cells:
                 # save each cell with the correct UUID
                 up_cell.fl_uuid = self.oc_type.uuid
                 up_cell.l_uuid = self.oc_type.content_uuid
                 up_cell.uuids_save()
     return output
Exemple #16
0
 def db_save_reconcile_predicates_types(self, act_dir):
     """ saves predicates and type items to the
         Open Context database, and / or reconciles these
         items with previously saved items from the same project
     """
     key = self.oc_config_attributes
     json_obj = self.fm.get_dict_from_file(key, act_dir)
     if json_obj is None:
         print('Need to 1st generate an attributes file from the ArchEnts!')
         ok = False
     else:
         # we have JSON with dictionary for the attributes
         ok = True
         self.attributes = json_obj
         for faims_id_pred, attrib_dict in json_obj.items():
             # default to always making a predicate and a type for attributes    
             sup_dict = LastUpdatedOrderedDict()
             sup_dict[self.reconcile_key] = faims_id_pred
             pm = PredicateManagement()
             pm.project_uuid = self.project_uuid
             pm.source_id = self.source_id
             pm.sup_dict = sup_dict
             pm.sup_reconcile_key = self.reconcile_key
             pm.sup_reconcile_value = faims_id_pred
             pred_obj = pm.get_make_predicate(attrib_dict['label'],
                                              attrib_dict['predicate_type'],
                                              attrib_dict['data_type'])
             if pred_obj is not False:
                 # we reconciled the predicate!
                 self.attributes[faims_id_pred]['predicate_uuid'] = str(pred_obj.uuid)
                 if 'objects' in attrib_dict:
                     for faims_id_type, type_dict in attrib_dict['objects'].items():
                         sup_dict = LastUpdatedOrderedDict()
                         sup_dict[self.reconcile_key] = faims_id_type
                         tm = TypeManagement()
                         tm.project_uuid = self.project_uuid
                         tm.source_id = self.source_id
                         tm.sup_dict = sup_dict
                         tm.sup_reconcile_key = self.reconcile_key
                         tm.sup_reconcile_value = faims_id_type
                         type_obj = tm.get_make_type_within_pred_uuid(pred_obj.uuid,
                                                                      type_dict['label'])
                         if type_obj is not False:
                             # we have reconciled the type!
                             type_dict['type_uuid'] = str(type_obj.uuid)
                             type_dict['predicate_uuid'] = str(pred_obj.uuid)
                             self.attributes[faims_id_pred]['objects'][faims_id_type] = type_dict
         # now save the results
         self.fm.save_serialized_json(key,
                                      act_dir,
                                      self.attributes)
Exemple #17
0
 def create_type(self, post_data):
     """ creates a type item into a project
     """
     ok = True
     required_params = [
         'source_id', 'item_type', 'predicate_uuid', 'label', 'note'
     ]
     for r_param in required_params:
         if r_param not in post_data:
             # we're missing some required data
             # don't create the item
             ok = False
             message = 'Missing paramater: ' + r_param + ''
             if self.errors['params'] is False:
                 self.errors['params'] = message
             else:
                 self.errors['params'] += '; ' + message
     # now prep the type management !
     tm = TypeManagement()
     tm.project_uuid = self.project_uuid
     uuid = False
     content_uuid = False
     if ok:
         # now check to see if this already exists
         label = post_data['label'].strip()
         type_note = post_data['note'].strip()
         tm.source_id = post_data['source_id'].strip()
         predicate_uuid = post_data['predicate_uuid'].strip()
         if self.check_non_blank_param(post_data, 'uuid')\
            and self.check_non_blank_param(post_data, 'content_uuid'):
             uuid = post_data['uuid'].strip()
             if self.check_uuid_format(uuid):
                 uuid_exists = self.check_uuid_exists(uuid)
                 if uuid_exists:
                     ok = False
                     self.errors[
                         'uuid'] = 'Cannot create a category called "' + label + '"'
                     self.errors[
                         'uuid'] += ', becuase the submitted UUID: ' + uuid
                     self.errors['uuid'] += ' already exists.'
                     note = self.errors['uuid']
                 else:
                     # ok! use this as the suggested UUID for making a new type
                     tm.suggested_uuid = uuid
                     content_uuid = self.validate_content_uuid(
                         tm, post_data)
                     if content_uuid is False:
                         ok = False
                         note = self.errors['content_uuid']
                     else:
                         # ok! the content uuid is also OK to use
                         tm.suggested_content_uuid = content_uuid
             else:
                 ok = False
                 self.errors[
                     'uuid'] = 'Cannot create a category called "' + label + '"'
                 self.errors[
                     'uuid'] += ', becuase the submitted UUID: ' + uuid
                 self.errors['uuid'] += ' is badly formed.'
                 note = self.errors['uuid']
         elif self.check_non_blank_param(post_data, 'uuid') \
             and not self.check_non_blank_param(post_data,
                                                'content_uuid'):
             # we have a uuid for the type, but not the content. We can't
             # create the type however, since we're missing a content uuid
             uuid = post_data['uuid'].strip()
             ok = False
             self.errors[
                 'uuid'] = 'Cannot create a category called "' + label + '"'
             self.errors['uuid'] += ', becuase the submitted UUID: ' + str(
                 uuid)
             self.errors['uuid'] += ' needs to have a valid Content UUID.'
             note = self.errors['uuid']
         elif not self.check_non_blank_param(post_data, 'uuid') \
             and self.check_non_blank_param(post_data,
                                            'content_uuid'):
             # we have a uuid for the content only. a weird case, but possible
             content_uuid = self.validate_content_uuid(tm, post_data)
             if content_uuid is False:
                 ok = False
                 note = self.errors['content_uuid']
             else:
                 # ok! the content uuid is also OK to use
                 tm.suggested_content_uuid = content_uuid
     else:
         label = '[Item not created]'
         uuid = False
     if ok:
         type_uuid = tm.check_exists_pred_uuid_content(
             predicate_uuid, label, True)
         if type_uuid is not False:
             # we already have this type!
             ok = False
             uuid = type_uuid
             self.errors[
                 'uuid'] = 'Cannot create a category called "' + label + '"'
             self.errors[
                 'uuid'] += ', becuase it already exists with UUID: ' + uuid
             note = self.errors['uuid']
     if ok:
         tm.source_id = post_data['source_id'].strip()
         if content_uuid is False:
             newtype = tm.get_make_type_within_pred_uuid(
                 predicate_uuid, label)
             content_uuid = newtype.content_uuid
             uuid = newtype.uuid
         else:
             tm.content = label
             newtype = tm.get_make_type_pred_uuid_content_uuid(
                 predicate_uuid, content_uuid)
             content_uuid = newtype.content_uuid
             uuid = newtype.uuid
         # now add the note if not empty
         self.add_description_note(newtype.uuid, 'types', newtype.source_id,
                                   type_note)
     self.response = {
         'action': 'create-item-into',
         'ok': ok,
         'change': {
             'uuid': uuid,
             'content_uuid': content_uuid,
             'label': label,
             'note': self.add_creation_note(ok)
         }
     }
     return self.response
Exemple #18
0
 def db_save_reconcile_entity_predicates_types(self, act_dir):
     """ saves predicates and type items to the
         Open Context database, and / or reconciles these
         items with previously saved items from the same project
     """
     key = self.oc_config_entity_types
     json_obj = self.fm.get_dict_from_file(key, act_dir)
     if json_obj is None:
         print('Need to 1st generate an attributes file from the ArchEnts!')
         ok = False
     else:
         # we have JSON with dictionary for the entity_types
         self.entity_types = json_obj
         make_entity_types_assertions = False
         for faims_ent_type_id, ent_dict in json_obj.items():
             if isinstance(ent_dict['item_type'], str) \
                and ent_dict['add_type_as_attribute']:
                 # OK we have some items that need entity types made as
                 # a descriptive attribute
                 make_entity_types_assertions = True
                 break
         if make_entity_types_assertions:
             # we have entity_types that need to have a descriptive
             # predicate, so create a new predicate in Open Context
             # to describe entity_types for this project
             sup_dict = LastUpdatedOrderedDict()
             sup_dict[self.reconcile_key] = self.ent_type_pred_sup_id
             pm = PredicateManagement()
             pm.project_uuid = self.project_uuid
             pm.source_id = self.source_id
             pm.sup_dict = sup_dict
             pm.sup_reconcile_key = self.reconcile_key
             pm.sup_reconcile_value = self.ent_type_pred_sup_id
             pred_obj = pm.get_make_predicate(self.FAIMS_ENTITY_TYPE_PREDICATE_LABEL,
                                              'variable',
                                              'id')
             if pred_obj is not False:
                 # we reconciled or created the predicate!
                 # now we mint oc_types for all the entity_types
                 predicate_uuid = str(pred_obj.uuid)
                 for faims_ent_type_id, ent_dict in json_obj.items():
                     if isinstance(ent_dict['item_type'], str) \
                        and ent_dict['add_type_as_attribute']:
                         # OK, we have an item entity type to be used as a description
                         sup_dict = LastUpdatedOrderedDict()
                         sup_dict[self.reconcile_key] = faims_ent_type_id
                         tm = TypeManagement()
                         tm.project_uuid = self.project_uuid
                         tm.source_id = self.source_id
                         tm.sup_dict = sup_dict
                         tm.sup_reconcile_key = self.reconcile_key
                         tm.sup_reconcile_value = faims_ent_type_id
                         type_obj = tm.get_make_type_within_pred_uuid(predicate_uuid,
                                                                      ent_dict['label'])
                         if type_obj is not False:
                             # we have reconciled the type!
                             ent_dict['type_uuid'] = str(type_obj.uuid)
                             ent_dict['predicate_uuid'] = predicate_uuid
                             self.entity_types[faims_ent_type_id] = ent_dict
             # now save the results
             self.fm.save_serialized_json(key,
                                          act_dir,
                                          self.entity_types)
 def create_type(self, post_data):
     """ creates a type item into a project
     """
     ok = True
     required_params = ['source_id',
                        'item_type',
                        'predicate_uuid',
                        'label',
                        'note']
     for r_param in required_params:
         if r_param not in post_data:
             # we're missing some required data
             # don't create the item
             ok = False
             message = 'Missing paramater: ' + r_param + ''
             self.errors.append(message)
     # now prep the type management !
     tm = TypeManagement()
     tm.project_uuid = self.project_uuid
     uuid = False
     content_uuid = False
     if ok:
         # now check to see if this already exists
         label = post_data['label'].strip()
         type_note = post_data['note'].strip()
         tm.source_id = post_data['source_id'].strip()
         predicate_uuid = post_data['predicate_uuid'].strip()
         if self.check_non_blank_param(post_data, 'uuid')\
            and self.check_non_blank_param(post_data, 'content_uuid'):
             uuid = post_data['uuid'].strip()
             if self.check_uuid_format(uuid):
                 uuid_exists = self.check_uuid_exists(uuid)
                 if uuid_exists:
                     ok = False
                     message = 'Cannot create a category called "' + label + '"'
                     message += ', becuase the submitted UUID: ' + uuid
                     message += ' already exists.'
                     self.errors.append(message)
                     note = '; '.join(self.errors)
                 else:
                     # ok! use this as the suggested UUID for making a new type
                     tm.suggested_uuid = uuid
                     content_uuid = self.validate_content_uuid(tm,
                                                               post_data)
                     if content_uuid is False:
                         ok = False
                         note = '; '.join(self.errors)
                     else:
                         # ok! the content uuid is also OK to use
                         tm.suggested_content_uuid = content_uuid
             else:
                 ok = False
                 mesaage = 'Cannot create a category called "' + label + '"'
                 mesaage += ', becuase the submitted UUID: ' + uuid
                 mesaage += ' is badly formed.'
                 self.errors.append(message)
                 note = '; '.join(self.errors)
         elif self.check_non_blank_param(post_data, 'uuid') \
             and not self.check_non_blank_param(post_data,
                                                'content_uuid'):
             # we have a uuid for the type, but not the content. We can't
             # create the type however, since we're missing a content uuid
             uuid = post_data['uuid'].strip()
             ok = False
             mesaage = 'Cannot create a category called "' + label + '"'
             mesaage += ', becuase the submitted UUID: ' + str(uuid)
             mesaage += ' needs to have a valid Content UUID.'
             self.errors.append(message)
             note = '; '.join(self.errors)
         elif not self.check_non_blank_param(post_data, 'uuid') \
             and self.check_non_blank_param(post_data,
                                            'content_uuid'):
             # we have a uuid for the content only. a weird case, but possible
             content_uuid = self.validate_content_uuid(tm,
                                                       post_data)
             if content_uuid is False:
                 ok = False
                 note = '; '.join(self.errors)
             else:
                 # ok! the content uuid is also OK to use
                 tm.suggested_content_uuid = content_uuid
     else:
         label = '[Item not created]'
         uuid = False
     if ok:
         type_uuid = tm.check_exists_pred_uuid_content(predicate_uuid,
                                                       label,
                                                       True)
         if type_uuid is not False:
             # we already have this type!
             ok = False
             uuid = str(type_uuid)
             message = 'Cannot create a category called "' + label + '"'
             message += ', becuase it already exists with UUID: ' + uuid
             self.errors.append(message)
             note = '; '.join(self.errors)
     if ok:
         tm.source_id = post_data['source_id'].strip()
         if content_uuid is False:
             newtype = tm.get_make_type_within_pred_uuid(predicate_uuid,
                                                         label)
             content_uuid = str(newtype.content_uuid)
             uuid = str(newtype.uuid)
         else:
             tm.content = label
             newtype = tm.get_make_type_pred_uuid_content_uuid(predicate_uuid,
                                                               content_uuid)
             content_uuid = newtype.content_uuid
             uuid = newtype.uuid
         # now add the note if not empty
         self.add_description_note(newtype.uuid,
                                   'types',
                                   newtype.source_id,
                                   type_note)
     if ok:
         # now clear the cache a change was made
         self.created_uuid = uuid
         self.clear_caches()
     self.response = {'action': 'create-item-into',
                      'ok': ok,
                      'change': {'uuid': uuid,
                                 'content_uuid': content_uuid,
                                 'label': label,
                                 'note': self.add_creation_note(ok)}}
     return self.response