We want to dynamically name the iobject based on its type and
        selected facts. This is, what this function does: it collects
        all facts that have "naming" set and concatenates them in the
        order given by the "naming" argument.
        If no fact has such a flag, the type is used

def extract_name(self):
        """
        We want to dynamically name the iobject based on its type and
        selected facts. This is, what this function does: it collects
        all facts that have "naming" set and concatenates them in the
        order given by the "naming" argument.
        If no fact has such a flag, the type is used

        """
        name = None

        name_schemas = self._DCM["InfoObjectNaming"].objects.filter(iobject_type=self.iobject_type).order_by(
            'position').values_list('format_string', flat=True)

        name_components = []

        # We retrieve all facts of the object

        fact_list = self._DCM['Fact'].objects.filter(iobject_thru__iobject=self).order_by(
            'iobject_thru__node_id__name').values_list('iobject_thru__node_id__name',
                                                        'fact_term__term',
                                                        'fact_term__attribute',
                                                        'fact_values__value',
                                                        'fact_values__storage_location',
                                                        'value_iobject_id__latest__name')

        # We build a dictionary that will then be used for the format string

        fact_dict = {}
        counter = 0
        for (node_id, fact_term, attribute, value, storage_location, related_obj_name) in fact_list:
            #print fact_list[counter]
            if type(value)==type([]):
                value = "%s,..." % value
            if storage_location == dingos.DINGOS_BLOB_TABLE:
                try:
                    value = BlobStorage.objects.get(sha256=value).content
                except:
                    value = "In blob table %s" % value
            elif related_obj_name:
                value = related_obj_name

            if attribute:
                fact_term = "%s@%s" % (fact_term, attribute)

            if not fact_term in fact_dict:
                fact_dict[fact_term] = value

            # In addition to the mapping of fact term to (first) value,
            # we add a way to refer to fact terms and values by their
            # node id

            fact_dict["term_of_node_%s" % node_id] = fact_term
            fact_dict["value_of_node_%s" % node_id] = value
            if counter < 10:
                fact_dict["term_of_fact_num_%01d" % counter] = fact_term
                fact_dict["value_of_fact_num_%01d" % counter] = value
            counter += 1
        fact_dict["fact_count_equal_%s?" % counter] = ""
        fact_dict["fact_count"] = "%s" % counter

        name_found = False

        # Now, we go through the format strings and see whether one of them
        # is successfully instantiated via the fact dictionary
        #print fact_dict

        for format_string in name_schemas:
            # Massage the format string such that we can use it as Python format string

            # Escape possible '%'
            format_string = format_string.replace('%','\%')
            # Replace placeholder definitions with python string formatting
            format_string = RE_SEARCH_PLACEHOLDERS.sub("%(\\1)s", format_string)




            try:
                name = format_string % fact_dict
                name_found = True
                break
            except:
                continue

        if not name_found:
            if self.iobject_type.name == 'PLACEHOLDER':
                return "PLACEHOLDER"
            else:
                return "%s (%s facts)" % (self.iobject_type.name,counter)
        else:
            return name