'Store this item in the DB'

def store(self, item_table=None):

    if (item_table is None):
        return None
    db = current.db
    row = db((item_table.item_id == self.item_id)).select(item_table.id, limitby=(0, 1)).first()
    if row:
        record_id = row.id
    else:
        record_id = None
    record = Storage(job_id=self.job.job_id, item_id=self.item_id, tablename=self.tablename, record_uid=self.uid, error=(self.error or ''))
    if (self.element is not None):
        element_str = current.xml.tostring(self.element, xml_declaration=False)
        record.update(element=element_str)
    if (self.data is not None):
        data = Storage()
        for f in self.data.keys():
            table = self.table
            if (f not in table.fields):
                continue
            fieldtype = str(self.table[f].type)
            if ((fieldtype == 'id') or s3_has_foreign_key(self.table[f])):
                continue
            data.update({f: self.data[f]})
        data_str = cPickle.dumps(data)
        record.update(data=data_str)
    ritems = []
    for reference in self.references:
        field = reference.field
        entry = reference.entry
        store_entry = None
        if entry:
            if (entry.item_id is not None):
                store_entry = dict(field=field, item_id=str(entry.item_id))
            elif (entry.uid is not None):
                store_entry = dict(field=field, tablename=entry.tablename, uid=str(entry.uid))
            if (store_entry is not None):
                ritems.append(json.dumps(store_entry))
    if ritems:
        record.update(ritems=ritems)
    citems = [c.item_id for c in self.components]
    if citems:
        record.update(citems=citems)
    if self.parent:
        record.update(parent=self.parent.item_id)
    if record_id:
        db((item_table.id == record_id)).update(**record)
    else:
        record_id = item_table.insert(**record)
    return record_id
