--- /dev/null
+# -*- coding: utf-8 -*-
+#
+# CREM Database migrator
+
+# Copyright (C) 2009 Samalyse SARL
+# Author: Olivier Guilyardi <olivier samalyse com>
+
+# This software is governed by the CeCILL license under French law and
+# abiding by the rules of distribution of free software. You can use,
+# modify and/ or redistribute the software under the terms of the CeCILL
+# license as circulated by CEA, CNRS and INRIA at the following URL
+# "http://www.cecill.info".
+
+# As a counterpart to the access to the source code and rights to copy,
+# modify and redistribute granted by the license, users are provided only
+# with a limited warranty and the software's author, the holder of the
+# economic rights, and the successive licensors have only limited
+# liability.
+
+# In this respect, the user's attention is drawn to the risks associated
+# with loading, using, modifying and/or developing or reproducing the
+# software by the user in light of its specific status of free software,
+# that may mean that it is complicated to manipulate, and that also
+# therefore means that it is reserved for developers and experienced
+# professionals having in-depth computer knowledge. Users are therefore
+# encouraged to load and test the software's suitability as regards their
+# requirements in conditions enabling the security of their systems and/or
+# data to be ensured and, more generally, to use and operate it in the
+# same conditions as regards security.
+
+# The fact that you are presently reading this means that you have had
+# knowledge of the CeCILL license and that you accept its terms.
+
+from telemeta.core import *
+from api import IDataMigrator
+from core import DataMigrator
+
+class CollectionsMigrator(DataMigrator):
+ """Perform a preliminary raw copy of the collection table"""
+
+ implements(IDataMigrator)
+
+ flat_map = [
+ ('Ref', 'reference'),
+ ('Cote', 'old_code'),
+ ('Cote', 'code'),
+ ('Titre', 'title'),
+ ('Transcrip_Trad', 'alt_title'),
+ ('Nb_de_pieces', 'physical_items_num'),
+ ('Auteur_Compil', 'creator'),
+ ('Auteur_Notice', 'booklet_author'),
+ ('Notice', 'booklet_description'),
+ ('Collecteur', 'collector'),
+ ('Num_Dans_Collec', 'publisher_serial'),
+ ('Ref_Biblio', 'external_references'),
+ ('Commentaire', 'comment'),
+ ('Autres_Cotes', 'alt_ids'),
+ ('Duree_approx', 'approx_duration'),
+ ('Tri DiBm', 'doctype_code'),
+ ('Travail', 'travail'),
+ ('CompilFacePlage', 'state'),
+ ('DeposantCNRS', 'cnrs_contributor'),
+ ('Fiches', 'items_done'),
+ ('A informer_07_03_', 'a_informer_07_03')
+ ]
+
+ enums_map = [
+ ('Format', 'physical_format'),
+ ('Reedition', 'publishing_status'),
+ #('Editeur', 'publisher'),
+ #('Collect_Série', 'publisher_collection'),
+ ('Mode_Acqui', 'acquisition_mode'),
+ ('Redacteur_Fiche', 'metadata_author'),
+ ('Saisie_Fiche', 'metadata_writer'),
+ ('Droit_Utiliser', 'legal_rights'),
+ ('Terrain_ou_Autr', 'recording_context'),
+ ('Numerisation', 'ad_conversion')
+ ]
+
+ def get_name(self):
+ return "collections:copy"
+
+ def build_flat_assignments(self, map):
+ assign = []
+ for f1, f2 in map:
+ f2 = '`%s`' % f2
+ f1 = '`%s`' % f1
+ assign.append((f2, f1))
+
+ return assign
+
+ def build_enum_assignments(self, src_table, map):
+ assign = []
+ for src_field, target_base in map:
+ target_field = '`%s_id`' % target_base
+ if target_base[-1] == 's':
+ enum_table = target_base
+ else:
+ enum_table = target_base + 's'
+
+ subquery = "COALESCE((SELECT id FROM `%s`.`%s` AS e WHERE %s.`%s` = e.value), -1)" % (
+ self.target_db_name, enum_table, src_table, src_field)
+
+ assign.append((target_field, subquery))
+
+ return assign
+
+ def process(self):
+
+ target_fields = []
+ src_fields = []
+
+ flat = self.build_flat_assignments(self.flat_map)
+ target_fields += [str(a[0]) for a in flat]
+ src_fields += [str(a[1]) for a in flat]
+
+ enum = self.build_enum_assignments('s', self.enums_map)
+ target_fields += [str(a[0]) for a in enum]
+ src_fields += [str(a[1]) for a in enum]
+
+ query = "INSERT INTO %s.media_collections (\n %s\n)\nSELECT \n %s\n FROM %s.Support AS s" % (
+ self.target_db_name, ",\n ".join(target_fields), ",\n ".join(src_fields),
+ self.src_db_name)
+
+ self.target_cursor.execute(query)