Although the indecent of Bat Sheva was not considered a sin, on the level where Dovid Hamelech was holding it was considered a flaw and he did ferocious Teshuva to cleanse his soul from it, at the same time opening the gateway for future sinners to repent. For six months Dovid accepted upon himself leprosy. During this period, the Shichina separated itself from him and the members of the Sanhedrin abstained from him. For another 22 years Dovid lost his Ruach Hakodesh. Each day during these years, Dovid would fast and cry, each night eating his bread dipped in ashes. For 13 of these years he was bed bound with great suffering and each day they would replace seven pillows from under him after they became soaked in puss, blood and fluids.
He also graciously accepted the great humility of being dethroned by Avshalem, going as far as to thank Hashem for it in Tehilim Chapter 3. He also accepted the severe curse of Shimi ben Gira who himself was holding at a fearsome level. During all these years Dovid's enemies gloried thinking that Hashem had abandoned him, until he shook the entire upper universe with his awesome Teshuva. It was staggering that Dovid did such Teshuva for a minor flaw, he therefore merited the aspect of Moshiach, to become the 4th foot of the chariot, head to all those with Roach Hakodesh and Royalty was given to his descendants forever. (Taharas Hakodesh 275 Pesach Hatshuva)