<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def set_metadata(self, metadata_dict):
 """ Set the metadata on a dataset **metadata_dict**: A dictionary of metadata key-vals. Transforms this dict into an array of metadata objects for storage in the DB. """

        if metadata_dict is None:
            return

        existing_metadata = []
        for m in self.metadata:
            existing_metadata.append(m.key)
            if m.key in metadata_dict:
                if m.value != metadata_dict[m.key]:
                    m.value = metadata_dict[m.key]


        for k, v in metadata_dict.items():
            if k not in existing_metadata:
                m_i = Metadata(key=str(k),value=str(v))
                self.metadata.append(m_i)

        metadata_to_delete =  set(existing_metadata).difference(set(metadata_dict.keys()))
        for m in self.metadata:
            if m.key in metadata_to_delete:
                get_session().delete(m)