From 167bccbc3f7f1046e6a08ba68be5794b8ecf703c Mon Sep 17 00:00:00 2001 From: astanley Date: Thu, 6 Mar 2025 09:54:30 -0400 Subject: [PATCH] Numerous updates --- FoxmlWorker.py | 4 +-- ImportProcessor.py | 53 ++++++++++++++++++++++-------- ImportUtilities.py | 80 ++++++++++++++++++++++++++++++++++++---------- 3 files changed, 104 insertions(+), 33 deletions(-) diff --git a/FoxmlWorker.py b/FoxmlWorker.py index b14a2f8..ca717e0 100644 --- a/FoxmlWorker.py +++ b/FoxmlWorker.py @@ -132,5 +132,5 @@ class FWorker: if __name__ == '__main__': - FW = FWorker('inputs/sample_foxml.xml') - print(FW.get_dc()) + FW = FWorker('assets/sample_fox.xml') + print(FW.get_rels_ext_values()) diff --git a/ImportProcessor.py b/ImportProcessor.py index eb8a96c..03166c5 100644 --- a/ImportProcessor.py +++ b/ImportProcessor.py @@ -27,7 +27,7 @@ class ImportProcessor: 'islandora:newspaperIssueCModel': ['OBJ', 'PDF'], 'islandora:sp-audioCModel': ['OBJ'], } - self.iu = IU.ImportUtilities() + self.iu = IU.ImportUtilities(namespace) self.ms = IS.ImportServerUtilities(namespace) self.namespace = namespace self.export_dir = '/opt/islandora/upei_migration/export' @@ -95,24 +95,49 @@ class ImportProcessor: writer.writerow(row) # Prepares worksheets for workbench ingest. - def prepare_worksheet(self, content_model, output_file): - details = self.iu.get_worksheet_details(self.namespace, content_model) + def prepare_intial_ingest_worksheet(self, output_file): + details = self.iu.get_worksheet_details() + if not details: # Check if details is None or empty + print("No worksheet details found.") + return + with open(output_file, 'w', newline='') as csvfile: - writer = csv.DictWriter(csvfile, fieldnames=self.fieldnames) + writer = csv.DictWriter(csvfile, fieldnames=['id', 'title', 'field_pid', 'field_model']) writer.writeheader() + id = 1 + for detail in details: - mods = self.iu.extract_from_mods(detail['field_pid']) - row = mods | detail - node_id = self.iu.get_nid_from_pid(self.namespace, row['field_member_of']) - row['id'] = row['field_pid'] - dc = self.iu.get_dc_values(detail['field_pid'], self.namespace) - if 'title' not in row: - row['title'] = dc['title'] - if node_id: - row['field_member_of'] = node_id - writer.writerow(row) + if not detail or not detail.get('field_pid'): # Add safer checks for detail and field_pid + continue + # Fetch DC values and ensure it's valid + dc = self.iu.get_dc_values(detail['field_pid']) + if not dc or 'title' not in dc: # Ensure dc is not None and has a 'title' key + print(f"Warning: Missing DC values for PID {detail['field_pid']}") + continue + + # Prepare the row for CSV + row = { + 'id': id, + 'title': dc['title'], + 'field_pid': detail['field_pid'], + 'field_model': detail.get('field_model', 'Unknown') # Use .get() with fallback for safety + } + writer.writerow(row) + id += 1 + def prepare_relationship_worksheet(self, output_file): + realtionships = self.iu.get_relationships() + with open(output_file, 'w', newline='') as csvfile: + writer = csv.DictWriter(csvfile, fieldnames=['node_id', 'field_member_of']) + writer.writeheader() + for relatiopship in realtionships: + row = {} + row['node_id'] = relatiopship['node_id'] + row['field_member_of'] = relatiopship['member_of'] + writer.writerow(row) + writer.writeheader() MP = ImportProcessor('ivoices') +MP.prepare_relationship_worksheet('ivoices_relationships.csv') diff --git a/ImportUtilities.py b/ImportUtilities.py index 360d1ee..97136a4 100644 --- a/ImportUtilities.py +++ b/ImportUtilities.py @@ -35,7 +35,7 @@ class ImportUtilities: with open(csv_file, newline='') as csvfile: reader = csv.DictReader(csvfile) for row in reader: - command = f"update {table} set node_id = '{row['ID']}' where pid = '{row['PID']}'" + command = f"update {table} set nid = '{row['ID']}' where pid = '{row['PID']}'" cursor.execute(command) self.conn.commit() @@ -122,9 +122,12 @@ class ImportUtilities: pids.append(row['pid']) return pids - # Utility function to prepare database selections for workbenchl - def get_worksheet_details(self, table, content_model): - command = f"Select * from {table} where content_model = 'islandora:{content_model}'" + # Utility function to prepare database selections for workbench + def get_worksheet_details(self, content_model=None): + command = f"Select * from {self.namespace}" + if content_model is not None: + command = f"{command} where content_model = '{content_model}'" + cursor = self.conn.cursor() details = [] for row in cursor.execute(command): @@ -146,23 +149,28 @@ class ImportUtilities: 'sequence': 'field_weight', } content_map = { - 'islandora:collectionCModel': 'collection', - 'islandora:sp_large_image_cmodel': 'image', - 'islandora:sp-audioCModel': 'audio', - 'islandora:pageCModel': 'page', + 'islandora:collectionCModel': 'Collection', + 'islandora:sp_large_image_cmodel': 'Image', + 'islandora:sp-audioCModel': 'Audio', + 'islandora:pageCModel': 'Page', 'islandora:bookCModel': 'Paged Content', 'islandora:compoundCModel': 'Compound Object', 'islandora:sp_pdf': 'Digital Document', - 'islandora:sp_basic_image': 'image', - 'islandora:newspaperCModel': 'newspaper', + 'islandora:sp_basic_image': 'Image', + 'islandora:newspaperCModel': 'Newspaper', 'islandora:newspaperIssueCModel': 'Publication Issue', - 'islandora:newspaperPageCModel': 'page', + 'islandora:newspaperPageCModel': 'Page', 'islandora:oralhistoriesCModel': 'Compound Object', 'islandora:sp_videoCModel': 'Video', 'ir:thesisCModel': 'Digital Document', 'islandora:rootSerialCModel': 'Compound Object', 'islandora:intermediateCModel': 'Compound Object', - 'ir:citationCModel': 'Citation' + 'ir:citationCModel': 'Citation', + 'islandora:audioCModel|islandora:sp-audioCModel': 'Audio', + 'islandora:slideCModel|islandora:sp_large_image_cmodel': 'Image', + 'islandora:videoCModel|islandora:sp_videoCModel': 'Video', + 'islandora:audioCModel': 'Audio', + } cleaned_line = {} @@ -240,9 +248,9 @@ class ImportUtilities: return result['node_id'] if result is not None else '' # Get key - value pairs from stored dublin core. - def get_dc_values(self, pid, table): + def get_dc_values(self, pid): cursor = self.conn.cursor() - result = cursor.execute(f"select dublin_core from {table} where pid = '{pid}'") + result = cursor.execute(f"select dublin_core from {self.namespace} where pid = '{pid}'") dc = result.fetchone()['dublin_core'] root = ET.fromstring(dc) namespaces = { @@ -255,8 +263,46 @@ class ImportUtilities: dc_vals[tag] = value return dc_vals + def add_title(self): + cursor = self.conn.cursor() + command = f"SELECT PID, title from {self.namespace} where title is null" + pids = cursor.execute(command).fetchall() + for pid in pids: + dc = self.get_dc_values(pid['PID']) + query = f"UPDATE {self.namespace} SET title = ? WHERE pid = ?" + values = (dc.get('title'), pid['PID']) + cursor.execute(query, values) + self.conn.commit() + -if __name__ == '__main__': - MU = ImportUtilities() + def get_relationships(self): + cursor = self.conn.cursor() + cursor.execute("SELECT pid, nid FROM ivoices") + pairs = cursor.fetchall() # Fetch all rows + + relationships = [] - MU.add_node_ids('msvu', 'inputs/nid-pid.csv') + for pid, nid in pairs: + if not pid: + continue + cursor.execute("SELECT collection_pid FROM ivoices WHERE pid = ?", (pid,)) + collection_pid = cursor.fetchone() + + if collection_pid: # Ensure there is a valid collection_pid + collection_pid = collection_pid[0] # Extract the value + + cursor.execute("SELECT nid FROM ivoices WHERE pid = ?", (collection_pid,)) + nid_result = cursor.fetchone() + + if nid_result: # Ensure a valid nid is found + relationships.append({ + 'node_id': nid, + 'member_of': nid_result[0] # Extract nid value + }) + + return relationships + + +if __name__ == '__main__': + MU = ImportUtilities('ivoices') + MU.add_node_ids('ivoices', 'inputs/ivoice-nid-pid.csv')